#abc360c. C - Move It

C - Move It

Score : 250250 points

问题陈述

NN 个编号为 11NN 的盒子和 NN 个编号为 11NN 的物品。物品 ii (1iN)(1 \leq i \leq N) 位于盒子 AiA_i 并且重量为 WiW_i

你可以重复执行选择一个物品并将其移动到另一个盒子的操作,次数可以是零次或多次。如果被移动的物品重量为 ww,则该操作的成本为 ww

找出使每个盒子恰好包含一个物品所需的最小总成本。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

There are NN boxes numbered 11 to NN and NN items numbered 11 to NN. Item ii (1iN)(1 \leq i \leq N) is in box AiA_i and has a weight of WiW_i.

You can repeatedly perform the operation of choosing an item and moving it to another box zero or more times. If the weight of the item being moved is ww, the cost of the operation is ww.

Find the minimum total cost required to make each box contain exactly one item.

Constraints

  • 1N105 1 \leq N \leq 10^{5}
  • 1AiN 1 \leq A_i \leq N (1iN)(1 \leq i \leq N)
  • 1Wi104 1 \leq W_i \leq 10^{4} (1iN)(1 \leq i \leq N)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \ldots ANA_N

W1W_1 W2W_2 \ldots WNW_N

Output

Print the minimum total cost required to make each box contain exactly one item.

Sample Input 1

5
2 2 3 3 5
33 40 2 12 16

Sample Output 1

35

With the following two moves, you can make each box contain exactly one item:

  • Move item 11 from box 22 to box 11. The cost is 3333.
  • Move item 33 from box 33 to box 44. The cost is 22.

The total cost of these two moves is 3535. It is impossible to make each box contain exactly one item with a cost less than 3535, so print 3535.

Sample Input 2

12
3 6 7 4 12 4 8 11 11 1 8 11
3925 9785 9752 3587 4013 1117 3937 7045 6437 6208 3391 6309

Sample Output 2

17254