#include <bits/stdc++.h>
using namespace std;
int n, co[200010], to[200010];
int cnt = 0;
int dfn[200010], low[200010];
int scc_cnt;
int sccno[200010];
int nt[200010], va[200010];
stack<int> S;
void dfs(int u) {
  dfn[u] = low[u] = ++cnt;
  S.push(u);
  int v = to[u];
  if (dfn[v] == 0) {
    dfs(v);
    low[u] = min(low[u], low[v]);
  } else if (!sccno[v]) {
    low[u] = min(low[u], dfn[v]);
  }
  int MMM = 0x3f3f3f3f;
  if (low[u] == dfn[u]) {
    ++scc_cnt;
    while (true) {
      int x = S.top();
      S.pop();
      sccno[x] = scc_cnt;
      MMM = min(MMM, co[x]);
      if (x == u) {
        break;
      }
    }
    va[scc_cnt] = MMM;
  }
}
int num[200010];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> co[i];
  for (int i = 1; i <= n; i++) cin >> to[i];
  memset(dfn, 0, sizeof(dfn));
  cnt = scc_cnt = 0;
  for (int i = 1; i <= n; ++i) {
    if (!dfn[i]) {
      dfs(i);
    }
  }
  for (int u = 1; u <= n; ++u) {
    int v = to[u];
    if (sccno[u] != sccno[v]) {
      nt[sccno[u]] = sccno[v];
      num[sccno[u]]++;
    }
  }
  queue<int> q;
  int ans = 0;
  for (int i = 1; i <= scc_cnt; i++) {
    if (num[i] == 0) {
      ans += va[i];
    }
  }
  cout << ans << endl;
  ;
  return 0;
}
