P2091 Sorting

Description

Little A has $n$ objects arranged in a row, each with volume $V$ and mass $M$. The volumes of the $n$ objects are within $1 \sim n$ and are all distinct, but the masses may be the same. Now Little A needs to reorder the $n$ 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 $n$, the number of objects. The second line contains $n$ positive integers; the $i$-th number is the volume of the $i$-th object from left to right. The third line contains $n$ positive integers; the $i$-th number is the mass of the $i$-th object from left to right.

Output Format

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

Explanation/Hint

| Test Point | $n$ | $m$ | | :----------: | :----------: | :----------: | | $1 \sim 2$ | $n = 2000$ | $m = 1$ | | $3$ | $n = 2000$ | $m \leq 10$ | | $4$ | $n = 2000$ | $m \leq 10000000$ | | $5 \sim 7$ | $n = 200000$ | $m = 1$ | | $8$ | $n = 200000$ | $m \leq 10$ | | $9 \sim 10$ | $n = 200000$ | $m \leq 10000000$ | Translated by ChatGPT 5