#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 7;
int cost[maxn], go[maxn], vis[maxn], use[maxn];
int main() {
  int i, j, n, m, pos, nxt, ans, MIN;
  ans = 0;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &cost[i]);
  }
  for (i = 1; i <= n; i++) {
    scanf("%d", &go[i]);
  }
  memset(vis, 0, sizeof(vis));
  memset(use, 0, sizeof(use));
  for (i = 1; i <= n; i++) {
    pos = i;
    if (vis[pos]) continue;
    while (use[pos] == 0) {
      if (vis[pos]) break;
      use[pos] = 1;
      pos = go[pos];
    }
    if (!vis[pos]) {
      nxt = pos;
      MIN = cost[pos];
      nxt = go[nxt];
      while (nxt != pos) {
        MIN = min(MIN, cost[nxt]);
        nxt = go[nxt];
      }
      ans += MIN;
    }
    pos = i;
    while (!vis[pos]) {
      vis[pos] = 1;
      pos = go[pos];
    }
  }
  return !printf("%d\n", ans);
}
