#include <bits/stdc++.h>
using namespace std;
int n;
int gia[200010], mvt[200010];
int d[200010];
int kq;
void solve() {
  int itest = 1;
  kq = 0;
  for (int i = 1; i <= n; i++) {
    if (d[i] == 0) {
      int vt = i;
      while (d[vt] == 0) {
        d[vt] = itest;
        vt = mvt[vt];
      }
      if (d[vt] == itest) {
        int minn = gia[vt];
        int vt1 = mvt[vt];
        while (vt1 != vt) {
          minn = min(minn, gia[vt1]);
          vt1 = mvt[vt1];
        }
        kq += minn;
      }
      itest++;
    }
  }
}
void output() { printf("%d\n", kq); }
int main() {
  while (scanf("%d", &n) > 0) {
    for (int i = 1; i <= n; i++) {
      scanf("%d", &gia[i]);
    }
    for (int i = 1; i <= n; i++) {
      scanf("%d", &mvt[i]);
    }
    solve();
    output();
  }
  return 0;
}
