cswork / OAOpen practice workspace

AmazonOA #388

Minimal Cost to Increase Package Size

Estimated HardJudge-ready

Topics (AI-labelled)Stacks & HeapsGreedy

Item i has initial size size[i]; each increase by 1 costs cost[i], and sizes cannot decrease. Make all final sizes pairwise distinct and find the minimum total cost.

Input

The first line contains n, the second line contains n sizes, and the third line contains n unit costs. OCR141/142/143 and the supplementary text of the same problem restore the complete original bounds: 1≤n≤200000, 1≤size[i]≤10^9, 1≤cost[i]≤10000.

Output

Output a single integer answer.

Example 1

Input:

4
2 3 2 2
2 4 5 1

Output:

7

Example 2

Input:

2
1 1
1 9

Output:

1

Example 3

Input:

2
1 100
3 2

Output:

0

Example notes

The minimum for the first example is 7: the item with cost 5 stays at 2, the item with cost 4 stays at 3, the item with cost 2 goes from 2 to 4 for 4, and the item with cost 1 goes from 2 to 5 for 3. The "1+4=7" explanation in the original supplementary text contains an arithmetic error; this site uses the correct process. The answers to the other two examples are 1 and 0.

Editorial

Loading solution…