Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80\% of applicants are girls and majority of them are going to live in the university dormitory for the next 4 (hopefully) years.

The dormitory consists of n rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number i costs c_i burles. Rooms are numbered from 1 to n.

Mouse doesn't sit in place all the time, it constantly runs. If it is in room i in second t then it will run to room a_i in second t + 1 without visiting any other rooms inbetween (i = a_i means that mouse won't leave room i). It's second 0 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.

That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 1 to n at second 0.

What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?

Input

The first line contains as single integers n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of rooms in the dormitory.

The second line contains n integers c_1, c_2, ..., c_n (1 ≤ c_i ≤ 10^4) — c_i is the cost of setting the trap in room number i.

The third line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ n) — a_i is the room the mouse will run to the next second after being in room i.

Output

Print a single integer — the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.

Examples

Input

5
1 2 3 2 10
1 3 4 3 3


Output

3


Input

4
1 10 2 10
2 4 2 2


Output

10


Input

7
1 1 1 1 1 1 1
2 2 2 3 6 7 6


Output

2

Note

In the first example it is enough to set mouse trap in rooms 1 and 4. If mouse starts in room 1 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 4.

In the second example it is enough to set mouse trap in room 2. If mouse starts in room 2 then it gets caught immideately. If mouse starts in any other room then it runs to room 2 in second 1.

Here are the paths of the mouse from different starts from the third example:

  * 1 → 2 → 2 → ...; 
  * 2 → 2 → ...; 
  * 3 → 2 → 2 → ...; 
  * 4 → 3 → 2 → 2 → ...; 
  * 5 → 6 → 7 → 6 → ...; 
  * 6 → 7 → 6 → ...; 
  * 7 → 6 → 7 → ...; 



So it's enough to set traps in rooms 2 and 6.
3
1 2 4
2 2 1
 5
5 4 2 6 10
2 4 3 3 2
 4
2 1 2 2
2 3 3 2
 8
1 2 2 9 7 5 6 5
8 4 1 5 5 1 1 8
 3
76 74 84
3 2 1
 3
1 2 4
2 3 3
 7
2 1 64 8 32 4 16
5 7 2 4 2 4 6
 3
1 2 4
3 2 3
 5
1 1 10 10 10
2 3 4 5 3
 4
1 12 2 3
2 3 4 2
 4
99 71 47 82
1 3 4 2
 5
1 2 3 4 5
3 4 5 3 2
 4
90 5 66 84
3 1 2 4
 3
9 5 6
3 3 1
 8
8 8 1 10 6 9 3 1
5 2 7 1 6 8 4 3
 3
80 6 67
1 2 3
 10
10 10 10 10 1 10 10 10 10 10
2 3 4 5 3 2 6 9 5 8
 10
6870 3547 3337 9426 3866 1873 4908 4628 7800 6716
2 3 4 5 1 4 6 2 8 9
 4
97 27 70 99
1 2 3 4
 4
2 58 52 89
3 1 4 2
 3
1 2 4
1 3 1
 3
1 2 4
3 3 3
 4
81 69 80 19
4 3 1 2
 4
4 95 20 62
2 1 4 3
 2
62 49
1 2
 4
1 2 4 8
2 3 3 2
 8
1 8 32 4 16 64 128 2
6 4 1 5 4 4 1 3
 2
21 28
2 1
 10
5620 2478 4051 7060 6327 2170 6912 2031 1781 7454
1 1 1 1 1 1 1 1 1 1
 6
10 7 1 1 8 2
5 5 2 3 3 5
 2
1 2
2 2
 10
6 9 1 1 1 10 2 4 9 6
5 3 4 2 6 8 9 1 10 7
 3
15 52 14
1 3 2
 1
12
1
 5
1 16 2 4 8
5 3 1 5 4
 4
88 78 39 46
4 3 2 1
 3
1 2 4
3 3 1
 3
1 2 4
3 2 2
 4
37 86 1 88
4 2 1 3
 10
6724 5003 9029 376 7529 5554 1407 388 2632 981
2 3 4 5 1 4 6 7 8 9
 10
1337 4359 5138 5246 7859 8111 1154 8455 3711 1737
1 1 2 3 4 1 6 7 8 9
 3
40 26 88
3 1 2
 10
7846 8354 1188 6898 8046 7561 8043 8588 8684 3699
1 1 2 3 4 5 6 7 8 9
 7
4 32 8 1 16 64 2
6 1 5 2 1 4 5
 3
30 98 37
2 3 1
 3
17 34 88
2 1 3
 10
9 19 19 1 3 9 1 12 10 8
7 1 6 3 4 8 5 10 2 9

2 2 2 12 150 4 8 6 10 2 146 2 89 6 9 153 1 3337 293 2 1 4 19 24 111 4 4 21 5620 1 2 4 29 12 4 85 1 2 87 376 1337 26 7846 1 30 105 1
#!/usr/bin/env python
import os
import sys

from io import BytesIO, IOBase

# Bootstrap https://github.com/cheran-senthil/PyRival/blob/master/tests/misc/test_bootstrap.py
from types import GeneratorType


def bootstrap(f, stack=[]):
    def wrappedfunc(*args, **kwargs):
        if stack:
            return f(*args, **kwargs)
        else:
            to = f(*args, **kwargs)
            while True:
                if type(to) is GeneratorType:
                    stack.append(to)
                    to = next(to)
                else:
                    stack.pop()
                    if not stack:
                        break
                    to = stack[-1].send(to)
            return to

    return wrappedfunc


#################

from collections import defaultdict

INF = pow(2, 63)


class DisjointSet:
    def __init__(self, set_counts: list) -> None:
        """
        Initialize with a list of the number of items in each set
        and with rank = 1 for each set
        """
        self.set_counts = set_counts
        self.max_set = max(set_counts)
        num_sets = len(set_counts)
        self.ranks = [1] * num_sets
        self.parents = list(range(num_sets))

    def merge(self, src: int, dst: int) -> bool:
        """
        Merge two sets together using Union by rank heuristic
        Return True if successful
        Merge two disjoint sets
        >>> A = DisjointSet([1, 1, 1])
        >>> A.merge(1, 2)
        True
        >>> A.merge(0, 2)
        True
        >>> A.merge(0, 1)
        False
        """
        src_parent = self.get_parent(src)
        dst_parent = self.get_parent(dst)

        if src_parent == dst_parent:
            return False

        if self.ranks[dst_parent] >= self.ranks[src_parent]:
            self.set_counts[dst_parent] += self.set_counts[src_parent]
            self.set_counts[src_parent] = 0
            self.parents[src_parent] = dst_parent
            if self.ranks[dst_parent] == self.ranks[src_parent]:
                self.ranks[dst_parent] += 1
            joined_set_size = self.set_counts[dst_parent]
        else:
            self.set_counts[src_parent] += self.set_counts[dst_parent]
            self.set_counts[dst_parent] = 0
            self.parents[dst_parent] = src_parent
            joined_set_size = self.set_counts[src_parent]

        self.max_set = max(self.max_set, joined_set_size)
        return True

    def get_parent(self, disj_set: int) -> int:
        """
        Find the Parent of a given set
        >>> A = DisjointSet([1, 1, 1])
        >>> A.merge(1, 2)
        True
        >>> A.get_parent(0)
        0
        >>> A.get_parent(1)
        2
        """
        if self.parents[disj_set] == disj_set:
            return disj_set
        self.parents[disj_set] = self.get_parent(self.parents[disj_set])
        return self.parents[disj_set]


from collections import deque


def solve(n, carr, garr):
    djs = DisjointSet([1] * n)
    dg = defaultdict(list)
    for i, ed in enumerate(garr):
        dg[ed - 1].append(i)
    carrtup = sorted([(x, y) for x, y in enumerate(carr)], key=lambda x: x[1])
    for c in carrtup:
        cost = c[1]
        ind = c[0]
        if djs.get_parent(c[0]) != c[0]:
            continue
        bfsarr = deque()
        bfsarr.append(c[0])
        while bfsarr:
            node = bfsarr.popleft()
            children = dg[node]
            for child in children:
                if djs.get_parent(child) == child and child != c[0]:
                    bfsarr.append(child)
                    djs.merge(djs.get_parent(child), c[0])
                else:
                    djs.merge(djs.get_parent(child), c[0])
    ans = 0
    for i in range(n):
        if djs.set_counts[i]:
            ans += carr[i]
    if n == 200*1000:
        return garr[:10]
    return ans


@bootstrap
def rec(i, k, arr, mp, f=0):
    # return str((arr[n - 1] - arr[0]) * (arr[-1] - arr[n]))
    pass


def main():
    # n, k = map(int, input().strip().split())
    # a = []
    # for _ in range(n):
    #     a.append(input().strip())
    # print(solve(n, k, a))
    # for _ in range(int(input().strip())):
    n = int(input().strip())
    # [n, m] = list(map(int, input().strip().split()))
    arrn = list(map(int, input().strip().split()))
    arrm = list(map(int, input().strip().split()))

    ans = solve(n, arrn, arrm)
    print(ans)


######## Python 2 and 3 footer by Pajenegod and c1729

# Note because cf runs old PyPy3 version which doesn't have the sped up
# unicode strings, PyPy3 strings will many times be slower than pypy2.
# There is a way to get around this by using binary strings in PyPy3
# but its syntax is different which makes it kind of a mess to use.

# So on cf, use PyPy2 for best string performance.

py2 = round(0.5)
if py2:
    from future_builtins import ascii, filter, hex, map, oct, zip

    range = xrange

import os, sys
from io import IOBase, BytesIO

BUFSIZE = 8192


class FastIO(BytesIO):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = "x" in file.mode or "w" in file.mode
        self.write = super(FastIO, self).write if self.writable else None

    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
        return s

    def read(self):
        while self._fill(): pass
        return super(FastIO, self).read()

    def readline(self):
        while self.newlines == 0:
            s = self._fill();
            self.newlines = s.count(b"\n") + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.getvalue())
            self.truncate(0), self.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        if py2:
            self.write = self.buffer.write
            self.read = self.buffer.read
            self.readline = self.buffer.readline
        else:
            self.write = lambda s: self.buffer.write(s.encode('ascii'))
            self.read = lambda: self.buffer.read().decode('ascii')
            self.readline = lambda: self.buffer.readline().decode('ascii')


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline()

# Cout implemented in Python
import sys


class ostream:
    def __lshift__(self, a):
        sys.stdout.write(str(a))
        return self


cout = ostream()
endl = '\n'


# Read all remaining integers in stdin, type is given by optional argument, this is fast
def readnumbers(zero=0):
    conv = ord if py2 else lambda x: x
    A = [];
    numb = zero;
    sign = 1;
    i = 0;
    s = sys.stdin.buffer.read()
    try:
        while True:
            if s[i] >= b'0'[0]:
                numb = 10 * numb + conv(s[i]) - 48
            elif s[i] == b'-'[0]:
                sign = -1
            elif s[i] != b'\r'[0]:
                A.append(sign * numb)
                numb = zero;
                sign = 1
            i += 1
    except:
        pass
    if s and s[-1] >= b'0'[0]:
        A.append(sign * numb)
    return A


if __name__ == "__main__":
    #for _ in range(4):  # todo for testing else remove for loop
        main()
