{"raw_statement":[{"iden":"problem statement","content":"There are $N$ people numbered $1$ through $N$.  \nTakahashi has decided to choose a sequence $P = (P_1, P_2, \\dots, P_N)$ that is a permutation of integers from $1$ through $N$, and give a candy to Person $P_1$, Person $P_2$, $\\dots$, and Person $P_N$, in this order.  \nSince Person $i$ dislikes Person $X_i$, if Takahashi gives a candy to Person $X_i$ prior to Person $i$, then Person $i$ gains frustration of $C_i$; otherwise, Person $i$'s frustration is $0$.  \nTakahashi may arbitrarily choose $P$. What is the minimum possible sum of their frustration?"},{"iden":"constraints","content":"*   $2 \\leq N \\leq 2 \\times 10^5$\n*   $1 \\leq X_i \\leq N$\n*   $X_i \\neq i$\n*   $1 \\leq C_i \\leq 10^9$\n*   All values in input are integers."},{"iden":"input","content":"Input is given from Standard Input in the following format:\n\n$N$\n$X_1$ $X_2$ $\\dots$ $X_N$\n$C_1$ $C_2$ $\\dots$ $C_N$"},{"iden":"sample input 1","content":"3\n2 3 2\n1 10 100"},{"iden":"sample output 1","content":"10\n\nIf he lets $P = (1, 3, 2)$, only Person $2$ gains a positive amount of frustration, in which case the sum of their frustration is $10$.  \nSince it is impossible to make the sum of frustration smaller, the answer is $10$."},{"iden":"sample input 2","content":"8\n7 3 5 5 8 4 1 2\n36 49 73 38 30 85 27 45"},{"iden":"sample output 2","content":"57"}],"translated_statement":null,"sample_group":[],"show_order":["default"],"formal_statement":null,"simple_statement":null,"has_page_source":true}