cswork / OAOpen practice workspace

AmazonOA #263

Maximize Pages Before Suspension

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyHashing

All printers start idle. Each time, activate one idle printer, which immediately prints its pages pages; then, if the number of active printers at that moment is x, every printer with threshold≤x is immediately and permanently paused, including idle printers that have not been activated yet; paused printers cannot be activated again, and the active count drops accordingly. Find the maximum total number of pages printed. The source's main sentence mistakenly wrote threshold<x, but the Note and all three complete examples consistently use ≤, and the second and third examples clearly show that idle printers are paused too; this site publicly corrects the statement on that basis.

Input

The first line contains n, the second line pages, and the third line threshold. The original text had no numeric bounds; this site uses 1≤n≤200000, 1≤pages[i]≤10^9, 1≤threshold[i]≤n.

Output

Output a single integer, the answer.

Example 1

Input:

5
4 1 5 2 3
3 3 2 3 3

Output:

14

Example 2

Input:

6
2 4 4 4 5 3
1 3 1 3 3 2

Output:

20

Example 3

Input:

4
2 6 10 13
2 1 1 1

Output:

15

Example notes

The answers are 14, 20 and 15. In the third example, printing 13 pages first permanently pauses every printer with threshold 1, after which only the 2 pages of the threshold-2 printer can still be printed.

Editorial

Loading solution…