#abc256e. E - Takahashi's Anguish

E - Takahashi's Anguish

Score : 500500 points

问题描述

设有 NN 名编号为 11NN 的人。
高桥决定选择一个整数序列 P=(P1,P2,,PN)P = (P_1, P_2, \dots, P_N),该序列是 11NN 的一个排列,并按照顺序将糖果送给第 P1P_1 号、第 P2P_2 号、\dots 和第 PNP_N 号的人。
由于第 ii 号人不喜欢第 XiX_i 号人,如果高桥在送给第 ii 号人糖果之前先送给第 XiX_i 号人,则第 ii 号人的沮丧值为 CiC_i;否则,第 ii 号人的沮丧值为 00
高桥可以任意选择序列 PP。请问他们的沮丧值之和的最小可能值是多少?

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There are NN people numbered 11 through NN.
Takahashi has decided to choose a sequence P=(P1,P2,,PN)P = (P_1, P_2, \dots, P_N) that is a permutation of integers from 11 through NN, and give a candy to Person P1P_1, Person P2P_2, \dots, and Person PNP_N, in this order.
Since Person ii dislikes Person XiX_i, if Takahashi gives a candy to Person XiX_i prior to Person ii, then Person ii gains frustration of CiC_i; otherwise, Person ii's frustration is 00.
Takahashi may arbitrarily choose PP. What is the minimum possible sum of their frustration?

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 1XiN1 \leq X_i \leq N
  • XiiX_i \neq i
  • 1Ci1091 \leq C_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

X1X_1 X2X_2 \dots XNX_N

C1C_1 C2C_2 \dots CNC_N

Output

Print the answer.

Sample Input 1

3
2 3 2
1 10 100

Sample Output 1

10

If he lets P=(1,3,2)P = (1, 3, 2), only Person 22 gains a positive amount of frustration, in which case the sum of their frustration is 1010.
Since it is impossible to make the sum of frustration smaller, the answer is 1010.

Sample Input 2

8
7 3 5 5 8 4 1 2
36 49 73 38 30 85 27 45

Sample Output 2

57

update @ 2024/3/10 10:53:56