#ABC261F. [ABC261F] Sorting Color Balls

[ABC261F] Sorting Color Balls

Score : 500500 points

Problem Statement

There are NN balls arranged from left to right. The color of the ii-th ball from the left is Color CiC_i, and an integer XiX_i is written on it.

Takahashi wants to rearrange the balls so that the integers written on the balls are non-decreasing from left to right. In other words, his objective is to reach a situation where, for every 1iN11\leq i\leq N-1, the number written on the (i+1)(i+1)-th ball from the left is greater than or equal to the number written on the ii-th ball from the left.

For this, Takahashi can repeat the following operation any number of times (possibly zero):

Choose an integer ii such that 1iN11\leq i\leq N-1. If the colors of the ii-th and (i+1)(i+1)-th balls from the left are different, pay a cost of 11. (No cost is incurred if the colors are the same). Swap the ii-th and (i+1)(i+1)-th balls from the left.

Find the minimum total cost Takahashi needs to pay to achieve his objective.

Constraints

  • 2N3×1052 \leq N \leq 3\times 10^5
  • 1CiN1\leq C_i\leq N
  • 1XiN1\leq X_i\leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

C1C_1 C2C_2 \ldots CNC_N

X1X_1 X2X_2 \ldots XNX_N

Output

Print the minimum total cost Takahashi needs to pay to achieve his objective, as an integer.

5
1 5 2 2 1
3 2 1 2 1
6

Let us represent a ball as ((Color,, Integer)). The initial situation is (1,3)(1,3), (5,2)(5,2), (2,1)(2,1), (2,2)(2,2), (1,1)(1,1). Here is a possible sequence of operations for Takahashi:

  • Swap the 11-st ball (Color 11) and 22-nd ball (Color 55). Now the balls are arranged in the order (5,2)(5,2), (1,3)(1,3), (2,1)(2,1), (2,2)(2,2), (1,1)(1,1).
  • Swap the 22-nd ball (Color 11) and 33-rd ball (Color 22). Now the balls are arranged in the order (5,2)(5,2), (2,1)(2,1), (1,3)(1,3), (2,2)(2,2), (1,1)(1,1).
  • Swap the 33-rd ball (Color 11) and 44-th ball (Color 22). Now the balls are in the order (5,2)(5,2), (2,1)(2,1), (2,2)(2,2), (1,3)(1,3), (1,1)(1,1).
  • Swap the 44-th ball (Color 11) and 55-th ball (Color 11). Now the balls are in the order (5,2)(5,2), (2,1)(2,1), (2,2)(2,2), (1,1)(1,1), (1,3)(1,3).
  • Swap the 33-rd ball (Color 22) and 44-th ball (Color 11). Now the balls are in the order(5,2)(5,2), (2,1)(2,1), (1,1)(1,1), (2,2)(2,2), (1,3)(1,3).
  • Swap the 11-st ball (Color 55) and 22-nd ball (Color 22). Now the balls are in the order (2,1)(2,1), (5,2)(5,2), (1,1)(1,1), (2,2)(2,2), (1,3)(1,3).
  • Swap the 22-nd ball (Color 55) and 33-rd ball (Color 11). Now the balls are in the order (2,1)(2,1), (1,1)(1,1), (5,2)(5,2), (2,2)(2,2), (1,3)(1,3).

After the last operation, the numbers written on the balls are 1,1,2,2,31,1,2,2,3 from left to right, which achieves Takahashi's objective.

The 11-st, 22-nd, 33-rd, 55-th, 66-th, and 77-th operations incur a cost of 11 each, for a total of 66, which is the minimum. Note that the 44-th operation does not incur a cost since the balls are both in Color 11.

3
1 1 1
3 2 1
0

All balls are in the same color, so no cost is incurred in swapping balls.

3
3 1 2
1 1 2
0

Takahashi's objective is already achieved without any operation.