#include <bits/stdc++.h>
using namespace std;
int n, c[200001], bs[200001], v[200001], cst[200001], rez;
void track(int start, int crt) {
  if (crt == start) return;
  cst[bs[start]] = min(cst[bs[start]], c[crt]);
  track(start, v[crt]);
}
int dfs(int nod) {
  if (bs[nod] && cst[bs[nod]] != 0x3f3f3f3f)
    return bs[nod];
  else if (bs[nod]) {
    cst[bs[nod]] = c[nod];
    track(nod, v[nod]);
    return bs[nod];
  }
  bs[nod] = bs[0];
  return bs[nod] = dfs(v[nod]);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> c[i];
  for (int i = 1; i <= n; ++i) cin >> v[i];
  memset(cst, 0x3f3f3f3f, sizeof cst);
  for (int i = 1; i <= n; ++i)
    if (!bs[i]) {
      ++bs[0];
      dfs(i);
      if (bs[i] != bs[0]) --bs[0];
    }
  for (int i = 1; i <= bs[0]; ++i) rez += cst[i];
  cout << rez;
  return 0;
}
