#include <bits/stdc++.h>
using namespace std;
void guan() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
const long long mod = 998244353;
const long long mod2 = mod - 1;
const int INF = 0x7FFFFFFF;
const long long LINF = 0x7FFFFFFFFFFFFFFF;
const double pi = acos(-1.0);
const long long maxx = 1LL << 61;
const double eps = 1e-8;
const int maxn = 1010101;
char s[maxn];
long long nxt[maxn], c[maxn];
int vis[maxn];
vector<int> g[maxn];
int in[maxn];
int main() {
  guan();
  int t;
  long long n;
  memset(vis, 0, sizeof(vis));
  memset(in, 0, sizeof(in));
  while (cin >> n) {
    for (int i = 1; i <= n; i++) cin >> c[i];
    for (int i = 1; i <= n; i++) {
      int v;
      cin >> nxt[i];
      in[nxt[i]]++;
    }
    queue<int> q;
    for (int i = 1; i <= n; i++) {
      if (in[i] == 0) q.push(i);
    }
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      vis[u] = 1;
      int v = nxt[u];
      in[v]--;
      if (in[v] == 0) q.push(v);
    }
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
      if (vis[i]) continue;
      long long pos = i, tc = INF;
      while (!vis[pos]) {
        tc = min(tc, c[pos]);
        vis[pos] = 1;
        pos = nxt[pos];
      }
      ans += tc;
    }
    cout << ans << endl;
  }
  cin >> t;
  return 0;
}
