#include <bits/stdc++.h>
using namespace std;
inline void read(int &num) {
  num = 0;
  char ch;
  int flag = 1;
  while ((ch = getchar()) < '0' || ch > '9')
    if (ch == '-') flag *= -1;
  while (ch >= '0' && ch <= '9') num = num * 10 + ch - '0', ch = getchar();
  num *= flag;
}
const int MAXN = 200005;
int n, num[MAXN], v[MAXN], st[MAXN], tot;
long long ans;
bool vis[MAXN], had[MAXN];
void dfs(int u) {
  if (vis[u]) {
    int mn = 0x7f7f7f7f;
    do {
      mn = min(mn, num[st[tot]]);
      had[st[tot]] = 1;
    } while (st[tot--] != u);
    ans += mn;
    return;
  }
  vis[u] = 1;
  st[++tot] = u;
  if (had[v[u]]) {
    had[u] = 1;
    return;
  }
  dfs(v[u]);
  had[u] |= had[v[u]];
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
  for (int i = 1; i <= n; i++)
    if (!vis[i]) dfs(i);
  printf("%I64d\n", ans);
}
