#include <bits/stdc++.h>
using namespace std;
int T, N, M;
int vis[200010];
int nex[200010];
int c[200010];
int ans;
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) scanf("%d", &c[i]);
  for (int i = 1; i <= N; i++) scanf("%d", &nex[i]);
  memset(vis, 0, sizeof(vis));
  ans = 0;
  for (int i = 1; i <= N; i++) {
    int u = i, mi = 123412341;
    while (vis[u] == 0) vis[u] = i, u = nex[u];
    if (vis[u] == i) {
      int v = u;
      do {
        mi = min(mi, c[v]);
        v = nex[v];
      } while (u != v);
      ans += mi;
    }
  }
  printf("%d\n", ans);
  return 0;
}
