Game with Balls and Boxes

아직 제출이 없습니다시간 제한2초메모리 제한1024 MB

문제

There are NN boxes and NN balls. You are playing a game that goes as follows.

The boxes are enumerated by sequential integers from 11 to NN, and the balls are also enumerated by sequential integers from 11 to NN. The ii-th box initially contains the ball P_iP\_i.

Each box is either open or closed. Initially, all boxes are closed.

Then two rounds of ball movement are performed. In each round, you:

  1. Select zero or more boxes and open them. To open the box ii for the first round, you pay A_iA\_i coins. To open the box ii for the second round, you pay B_iB\_i coins.
  2. Move the balls freely between the open boxes. However, each box must contain exactly one ball when the move is complete.
  3. Close all open boxes.

After two rounds, for each ii, the box ii must contain the ball ii. Find the minimal sum of coins you shall pay to complete the game.

입력

The first line of input contains one integer NN (1N1051 \le N \le 10^5).

The second line contains NN integers P_1,P_2,,P_NP\_1, P\_2, \ldots, P\_N: here, P_iP\_i is the number of the ball that was initially placed in ii-th box (1P_iN1 \le P\_i \le N, P_iP_jP\_i \ne P\_j if iji \ne j).

The third line contains NN integers A_1,A_2,,A_NA\_1, A\_2, \ldots, A\_N: here, A_iA\_i is the price of opening the ii-th box for the first round (1A_i1091 \le A\_i \le 10^9).

The fourth line contains NN integers B_1,B_2,,B_NB\_1, B\_2, \ldots, B\_N: here, B_iB\_i is the price of opening the ii-th box for the second round (1B_i1091 \le B\_i \le 10^9).

출력

Print one integer: the minimal sum of coins you need to pay to have ii-th ball in the ii-th box for each ii after two rounds.