{"raw_statement":[{"iden":"problem statement","content":"$N$ slimes are arranged in a line. The size of the $i$\\-th slime from the left is $P_i$, and its color is $C_i$. Here, the sizes of the slimes are distinct.\nA sequence of slimes is called a good sequence when it satisfies the following condition:\n\n*   By repeatedly performing the operation of swapping the positions of two adjacent slimes of **different colors**, it is possible to arrange the slimes in ascending order of size.\n\nTo make the sequence of slimes a good sequence, you can perform the following operation any number of times (possibly zero):\n\n*   Choose a slime, and change the color of the slime to any integer between $1$ and $N$, inclusive. This operation costs $x$, where $x$ is the color of the slime immediately before this operation.\n\nFind the minimum total cost required to make the sequence of slimes a good sequence."},{"iden":"constraints","content":"*   All input values are integers.\n*   $1 \\leq N \\leq 2\\times 10^5$\n*   $1\\leq P_i \\leq N$\n*   $1\\leq C_i \\leq N$\n*   $P$ is a permutation of $(1,\\ldots,N)$."},{"iden":"input","content":"The input is given from Standard Input in the following format:\n\n$N$\n$P_1$ $\\ldots$ $P_N$\n$C_1$ $\\ldots$ $C_N$"},{"iden":"sample input 1","content":"4\n3 1 2 4\n1 2 1 3"},{"iden":"sample output 1","content":"1\n\nChange the color of the $3$\\-rd slime to $2$. Then, by swapping the $1$\\-st and $2$\\-nd slimes, and swapping the $2$\\-nd and $3$\\-rd slimes, it is possible to arrange the slimes in ascending order of size, making it a good sequence.\nThe required cost is $1$, since the color of the $3$\\-rd slime immediately before the operation was $1$."},{"iden":"sample input 2","content":"10\n3 7 10 1 9 5 4 8 6 2\n6 4 8 4 6 8 8 4 8 8"},{"iden":"sample output 2","content":"28"}],"translated_statement":null,"sample_group":[],"show_order":["default"],"formal_statement":null,"simple_statement":null,"has_page_source":true}