{"problem":{"name":"Takahashi's Anguish","description":{"content":"There are $N$ people numbered $1$ through $N$.   Takahashi 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 Per","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"abc256_e"},"statements":[{"statement_type":"Markdown","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?\n\n## Constraints\n\n*   $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.\n\n## Input\n\nInput 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$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"abc256_e","tags":[],"sample_group":[["3\n2 3 2\n1 10 100","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$."],["8\n7 3 5 5 8 4 1 2\n36 49 73 38 30 85 27 45","57"]],"created_at":"2026-03-03 11:01:14"}}