Logo
(追記) (追記ここまで)

33729번 - Bessie's Function 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 2048 MB22141470.000%

문제

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

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

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

입력

The first line contains $N$.

The second line contains $N$ space-separated integers $a_1,a_2,\dots,a_N$.

The third line contains $N$ space-separated integers $c_1,c_2,\dots,c_N$.

출력

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

제한

예제 입력 1

5
2 4 4 5 3
1 1 1 1 1

예제 출력 1

3

We can change $a_1 = 4,ドル $a_4 = 4,ドル $a_5 = 4$. Since all $c_i$ equal one, the total cost is equal to 3ドル,ドル the number of changes. It can be shown that there is no solution with only 2ドル$ or fewer changes.

예제 입력 2

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

예제 출력 2

7

We change $a_3 = 3$ and $a_4 = 4$. The total cost is 2ドル+5=7$.

힌트

출처

Olympiad > USA Computing Olympiad > 2024-2025 Season > USACO 2025 February Contest > Gold 1번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /