Tasks and deadlines
Time: 1 s · Memory: 256 MB
- Sorting
Statement
You have tasks; task takes time and has deadline . You do the tasks one after another without breaks, starting at time . If a task is finished at time , you get points (possibly negative). What is the maximum total number of points?
Input format
The first line contains (). The next lines contain and ().
Output format
A single number — the maximum total points.
Examples
Input
3 6 10 8 15 5 12
Output
2
Input
4 4 2 3 5 2 7 4 5
Output
-10
Note
The total is . The first part does not depend on the order, so the sum of finishing times must be minimised: do the tasks by increasing duration. If a short task follows a long one, swapping them decreases the sum. Deadlines affect the answer but not the order.
In the second example the order is and the answer is . The answer reaches — a 64-bit type is needed.
Source: CSES, problem 1630.
