#P2091. 排序

排序

Description

Little A has nn objects arranged in a row, each with volume VV and mass MM. The volumes of the nn objects are within 1n1 \sim n and are all distinct, but the masses may be the same.

Now Little A needs to reorder the nn objects in increasing order of volume. His sorting method is: each operation swaps two objects. Each swap costs the sum of the masses of the two objects.

Little A wants to know the minimum total energy he must spend to sort the objects.

Input Format

The first line contains a positive integer nn, the number of objects.

The second line contains nn positive integers; the ii-th number is the volume of the ii-th object from left to right.

The third line contains nn positive integers; the ii-th number is the mass of the ii-th object from left to right.

Output Format

Output a single integer, the minimum total energy Little A will spend.

3
1 3 2
2 2 3

5

Hint

Test Point nn mm
121 \sim 2 n=2000n = 2000 m=1m = 1
33 m10m \leq 10
44 m10000000m \leq 10000000
575 \sim 7 n=200000n = 200000 m=1m = 1
88 m10m \leq 10
9109 \sim 10 m10000000m \leq 10000000

Translated by ChatGPT 5