#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
const int MAXN = 1e6 + 10;
const long long MOD = 1e9 + 7;
const long long MOD2 = 998244353;
const long long INF = 8e18;
const int LOG = 20;
long long pw(long long a, long long b, long long mod) {
  return (!b ? 1
             : (b & 1 ? (a * pw(a * a % mod, b / 2, mod)) % mod
                      : pw(a * a % mod, b / 2, mod)));
}
vector<pair<int, int> > G[MAXN];
vector<int> M;
int mark[MAXN], U[MAXN], H[MAXN], is_C[MAXN];
long long C[MAXN];
void DFS(int v, int last = 0) {
  M.push_back(v);
  mark[v] = 1;
  U[v] = H[v];
  for (auto y : G[v]) {
    int u = y.first, id = y.second;
    if (id == last) continue;
    if (!mark[u]) {
      H[u] = H[v] + 1;
      DFS(u, id);
      if (U[u] > H[v]) {
        is_C[id] = 1;
      }
      U[v] = min(U[v], U[u]);
    } else {
      U[v] = min(U[v], H[u]);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> C[i];
  }
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    G[a].push_back(make_pair(i, i));
    G[i].push_back(make_pair(a, i));
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!mark[i]) {
      long long cu = 1e18;
      M.clear();
      DFS(i);
      for (auto x : M) {
        bool is_OK = 0;
        for (auto u : G[x]) {
          if (!is_C[u.second]) {
            is_OK = 1;
          }
        }
        if (is_OK) {
          cu = min(cu, C[x]);
        }
      }
      ans += cu;
    }
  }
  cout << ans;
  return 0;
}
