AT_arc206_b [ARC206B] Slime Swap
Description
$ 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.
Input Format
The input is given from Standard Input in the following format:
> $ N $ $ P_1 $ $ \ldots $ $ P_N $ $ C_1 $ $ \ldots $ $ C_N $
Output Format
Output the answer.
Explanation/Hint
### Sample Explanation 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 $ .
### 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) $ .