cswork / OAOpen practice workspace

AmazonOA #39

Get Minimal Cost (Distinct Sizes)

Estimated HardJudge-ready

Topics (AI-labelled)Stacks & HeapsGreedy

Each increase of 1 to the i-th size costs cost[i]; sizes can only increase, never decrease. Make all sizes pairwise distinct and find the minimum total cost.

Input

The first line contains n (1..200000), the second line contains n size values (1..10⁹), and the third line contains n cost values (1..10000).

Output

Output a single integer answer.

Example 1

Input:

4
2 3 3 2
2 4 5 1

Output:

7

Example 2

Input:

2
1 1
1 9

Output:

1

Example 3

Input:

2
1 4
9 9

Output:

0

Example notes

Example 1: the size-2 item with cost 2 stays at 2, the size-3 item with cost 5 stays at 3, the other 3 with cost 4 increases to 4 for 4, and the other 2 with cost 1 increases to 5 for 3, for a total cost of 7. Example 2: the expensive item stays at 1 and the cheap item increases by 1, for a cost of 1. Example 3: the sizes are already distinct, so the cost is 0.

Editorial

Loading solution…