{"problem":{"name":"Slime Swap","description":{"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. A sequence of slimes is called a good seque","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"arc206_b"},"statements":[{"statement_type":"Markdown","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.\n\n## Constraints\n\n*   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)$.\n\n## Input\n\nThe 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$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"arc206_b","tags":[],"sample_group":[["4\n3 1 2 4\n1 2 1 3","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$."],["10\n3 7 10 1 9 5 4 8 6 2\n6 4 8 4 6 8 8 4 8 8","28"]],"created_at":"2026-03-03 11:01:14"}}