AtCoder Regular Contest 206 (Div. 2)
B
Slime Swap
AtCoder
⌀ 1059
原题
← AC →
翻译中…

Problem Statement

翻译中…

$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

翻译中…

Output

翻译中…

Output the answer.

翻译中…

样例

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
题目信息
题号
arc206_b
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。