#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
const int MOD = 1000000007;
const int maxn = 200005;
int n;
int c[maxn];
int g[maxn], vis[maxn];
int dfs(int u) {
  vis[u] = -1;
  int v = g[u];
  if (vis[v] == 0) {
    int ans = dfs(v);
    vis[u] = 1;
    return ans;
  } else if (vis[v] == -1) {
    int p = v, mi = INF;
    for (;;) {
      mi = min(mi, c[p]);
      vis[p] = 1;
      p = g[p];
      if (p == v) return mi;
    }
  } else {
    vis[u] = 1;
    return 0;
  }
}
int main() {
  while (~scanf("%d", &n)) {
    for (int i = (1); i <= (n); ++i) scanf("%d", &c[i]);
    for (int i = (1); i <= (n); ++i) scanf("%d", &g[i]);
    memset(vis, 0, sizeof(vis));
    long long ans = 0;
    for (int i = 1; i <= n; ++i)
      if (!vis[i]) {
        ans += dfs(i);
      }
    printf("%d\n", ans);
  }
  return 0;
}
