Bessie's Function

시간 제한2초메모리 제한2048 MB

문제

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.