#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
int c[maxn];
int a[maxn];
bool vis[maxn];
int dfs_2(int u) {
  if (vis[u]) return inf;
  vis[u] = 1;
  int tmp = c[u];
  tmp = min(tmp, dfs_2(a[u]));
  return tmp;
}
int dfn[maxn], low[maxn], scc[maxn], num[maxn];
stack<int> s;
int block = 0, cnt = 0;
void tarjan(int u) {
  dfn[u] = ++cnt;
  low[u] = cnt;
  s.push(u);
  if (!dfn[a[u]])
    tarjan(a[u]), low[u] = min(low[u], low[a[u]]);
  else if (!scc[a[u]]) {
    low[u] = min(low[u], dfn[a[u]]);
  }
  if (dfn[u] == low[u]) {
    block++;
    while (1) {
      int v = s.top();
      s.pop();
      scc[v] = block;
      num[block]++;
      if (v == u) break;
    }
  }
}
void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &c[i]);
  int tot = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    if (a[i] == i) tot += c[i];
  }
  for (int i = 1; i <= n; ++i) {
    if (!scc[i]) tarjan(i);
  }
  for (int i = 1; i <= n; ++i) {
    if (vis[i] || num[scc[i]] == 1) continue;
    tot += dfs_2(i);
  }
  printf("%d\n", tot);
}
int main() {
  solve();
  return 0;
}
