#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n;
int cost[maxn];
int pai[maxn];
int comp[maxn], cc;
int root[maxn];
int cyc_s[maxn];
bool mark[maxn];
void dfs_root(int u) {
  mark[u] = true;
  int p = pai[u];
  if (comp[p]) {
    comp[u] = comp[p];
    return;
  }
  if (mark[p]) {
    ++cc;
    root[cc] = u, cyc_s[cc] = p, comp[u] = cc;
    return;
  }
  dfs_root(p);
  comp[u] = comp[p];
}
int dfs_min(int u) {
  if (root[comp[u]] == u) return cost[u];
  return min(cost[u], dfs_min(pai[u]));
}
int main(void) {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &cost[i]);
  for (int i = 1; i <= n; i++) scanf("%d", &pai[i]);
  for (int i = 1; i <= n; i++)
    if (!mark[i]) dfs_root(i);
  int ans = 0;
  for (int i = 1; i <= cc; i++) ans += dfs_min(cyc_s[i]);
  printf("%d\n", ans);
}
