#include <bits/stdc++.h>
using namespace std;
const int maxn = 2 * (1e5 + 5);
int to[maxn];
long long cal[maxn], ans;
int vis1[maxn];
void doit(int x) {
  int start = x;
  int nxt = to[x];
  long long minn = cal[start];
  while (nxt != start) {
    minn = min(minn, cal[nxt]);
    nxt = to[nxt];
  }
  ans += minn;
}
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    memset(vis1, 0, sizeof(vis1));
    for (int i = 1; i <= n; i++) {
      scanf("%d", &cal[i]);
    }
    for (int i = 1; i <= n; i++) {
      scanf("%d", &to[i]);
    }
    ans = 0;
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      if (vis1[i] == 0) {
        cnt++;
        int start = i;
        int nxt = to[i];
        vis1[start] = cnt;
        if (nxt == start) {
          doit(nxt);
          continue;
        }
        while (nxt != start) {
          if (vis1[nxt] != 0) {
            if (vis1[nxt] != vis1[start]) {
              break;
            } else {
              doit(nxt);
              break;
            }
          }
          vis1[nxt] = cnt;
          nxt = to[nxt];
        }
        if (nxt == start) {
          doit(nxt);
        }
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
