#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long linf = 0x3f3f3f3f3f3f3f3f;
const int maxn = 2e5 + 11;
const int maxm = 1e3 + 11;
const int mod = 1e9 + 7;
inline long long rd() {
  long long res, ch = 0;
  while (!(ch >= '0' && ch <= '9')) ch = getchar();
  res = ch - '0';
  while ((ch = getchar()) >= '0' && ch <= '9') res = res * 10 + ch - '0';
  return res;
}
inline long long qpow(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) {
      res *= a;
      res %= mod;
    }
    b >>= 1;
    a = a * a % mod;
  }
  return res;
}
inline long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int n;
queue<int> q;
int a[maxn], p[maxn], in[maxn], vis[maxn], ans = 0;
void topsort() {
  while (!q.empty()) {
    int x = q.front();
    vis[x] = 1;
    q.pop();
    in[p[x]]--;
    if (in[p[x]] == 0) q.push(p[x]);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      int s = p[i], f = a[i];
      while (s != i) {
        vis[s] = 1;
        f = min(f, a[s]);
        s = p[s];
      }
      ans += f;
    }
  }
}
int main() {
  n = rd();
  for (int i = 1; i <= n; i++) a[i] = rd();
  for (int i = 1; i <= n; i++) p[i] = rd(), in[p[i]]++;
  for (int i = 1; i <= n; i++) {
    if (in[i] == 0) q.push(i);
  }
  topsort();
  cout << ans << endl;
}
