#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 7;
int father[MAXN], a[MAXN], c[MAXN], x[MAXN];
int mfind(int x) {
  if (x != father[x]) {
    father[x] = mfind(father[x]);
  }
  return father[x];
}
int dfs(int x, int y) {
  if (x == y) return c[x];
  return min(dfs(a[x], y), c[x]);
}
int main() {
  int n;
  while (~scanf("%d", &n)) {
    memset(x, 0, sizeof(x));
    for (int i = 1; i <= n; i++) {
      father[i] = i;
      scanf("%d", &c[i]);
    }
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) {
      if (mfind(a[i]) == mfind(i)) {
        x[i] = 1;
        continue;
      }
      father[mfind(a[i])] = mfind(i);
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      if (x[i]) ans += dfs(a[i], i);
    }
    printf("%d\n", ans);
  }
  return 0;
}
