#714
Unrated
Bessie's Function
시간 제한
2s
메모리 제한
1024MB
제출
0
정답
0
맞힌 사람
0
정답 비율
0.0%

문제

Bessie has a special function f(x)f(x) that takes as input an integer in [1,N][1, N] and returns an integer in [1,N][1, N] (1N21051 \le N \le 2 \cdot 10^5). Her function f(x)f(x) is defined by NN integers a1aNa_1 \ldots a_N where f(x)=axf(x) = a_x (1aiN1 \le a_i \le N).

Bessie wants this function to be idempotent. In other words, it should satisfy f(f(x))=f(x)f(f(x)) = f(x) for all integers x[1,N]x \in [1, N].

For a cost of cic_i, Bessie can change the value of aia_i to any integer in [1,N][1, N] (1ci1091 \le c_i \le 10^9). Determine the minimum total cost Bessie needs to make f(x)f(x) idempotent.

입력

The first line contains NN.

The second line contains NN space-separated integers a1,a2,,aNa_1,a_2,\dots,a_N.

The third line contains NN space-separated integers c1,c2,,cNc_1,c_2,\dots,c_N.

출력

Output the minimum total cost Bessie needs to make f(x)f(x) idempotent.

예제 입력 1

5
2 4 4 5 3
1 1 1 1 1

예제 출력 1

3

예제 입력 2

8
1 2 5 5 3 3 4 4
9 9 2 5 9 9 9 9

예제 출력 2

7

점수

Subtasks:

Input 3: N20N\le 20Inputs 4-9: aiia_i\ge iInputs 10-15: All aia_i are distinct.Inputs 16-21: No additional constraints.

Additionally, in each of the last three subtasks, the first half of tests will satisfy ci=1c_i=1 for all ii.

코드 제출

코드를 제출하려면 로그인이 필요합니다.

로그인
내 제출
제출 내역이 없습니다.
맞은 사람
아직 맞은 사람이 없습니다.
난이도 투표
Unrated0명 투표
로그인 후 AC 받으면 투표할 수 있습니다.
전체 제출
제출 내역이 없습니다.