#include <bits/stdc++.h>
using namespace std;
bool chk[200020];
int c[200020];
int a[200020];
bitset<200020> b;
int findcycle(int s) {
  b.reset();
  int cur = s;
  while (1) {
    if (b[cur]) return cur;
    if (chk[cur]) return -1;
    b[cur] = 1;
    cur = a[cur];
  }
}
int run(int i) {
  int now = i;
  int cost = c[i];
  chk[now] = 1;
  do {
    now = a[now];
    chk[now] = 1;
    cost = min(cost, c[now]);
  } while (now != i);
  return cost;
}
void run2(int i) {
  int now = i;
  while (!chk[now]) {
    chk[now] = 1;
    now = a[now];
  }
}
int main() {
  int ans = 0, n, i, now;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d", &c[i]);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (i = 1; i <= n; i++) {
    if (chk[i]) continue;
    now = findcycle(i);
    if (now >= 0)
      ans += run(now);
    else
      run2(i);
  }
  printf("%d", ans);
}
