Slime Swap

AtCoder
IDarc206_b
Time2000ms
Memory256MB
Difficulty
$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 sequence when it satisfies the following condition: * 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. To make the sequence of slimes a good sequence, you can perform the following operation any number of times (possibly zero): * 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. Find the minimum total cost required to make the sequence of slimes a good sequence. ## Constraints * All input values are integers. * $1 \leq N \leq 2\times 10^5$ * $1\leq P_i \leq N$ * $1\leq C_i \leq N$ * $P$ is a permutation of $(1,\ldots,N)$. ## Input The input is given from Standard Input in the following format: $N$ $P_1$ $\ldots$ $P_N$ $C_1$ $\ldots$ $C_N$ [samples]
Samples
Input #1
4
3 1 2 4
1 2 1 3
Output #1
1

Change 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.
The required cost is $1$, since the color of the $3$\-rd slime immediately before the operation was $1$.
Input #2
10
3 7 10 1 9 5 4 8 6 2
6 4 8 4 6 8 8 4 8 8
Output #2
28
API Response (JSON)
{
  "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 seque...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments