cswork / OAOpen practice workspace

AmazonOA #259

Max Sum of Non-Overlapping Intervals

Estimated HardJudge-ready

Topics (AI-labelled)Scheduling DPBinary Search

Interval i is the closed interval [start[i],start[i]+duration[i]] with profit cost[i]. Choose pairwise non-overlapping intervals to maximize the total profit; sharing an endpoint also counts as overlapping, and choosing nothing is allowed. This closed-interval convention is restored from the source example, in which [2,4] and [4,7] are incompatible and the answer is 9.

Input

The first line contains n, followed by three lines: start, duration and cost. The original text had no numeric bounds; this site uses 1≤n≤200000, 0≤each value≤10^9; duration=0 means a single-point interval.

Output

Output a single integer, the answer.

Example 1

Input:

4
4 2 7 8
3 2 3 2
7 3 1 2

Output:

9

Example 2

Input:

2
0 1
1 1
5 7

Output:

7

Example 3

Input:

3
1 1 2
0 0 0
3 5 4

Output:

9

Example notes

The answers are 9, 7 and 9. In the second example the intervals intersect at 1, so they cannot both be chosen; in the third example, of the two points at the same position choose the one with profit 5, then choose position 2 with profit 4.

Editorial

Loading solution…