#include <bits/stdc++.h>
using namespace std;
const int MAXN = 212345;
int c[MAXN];
int v[MAXN];
int g[MAXN];
bool vis[MAXN];
queue<int> q;
int dfs(int x) {
  if (vis[x]) return 0x3F3F3F3F;
  vis[x] = true;
  return min(c[x], dfs(v[x]));
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) scanf("%d", c + i);
  for (int i = 0; i < n; ++i) {
    scanf("%d", v + i);
    g[--v[i]]++;
  }
  for (int i = 0; i < n; ++i)
    if (g[i] == 0) q.push(i);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    g[v[x]]--;
    if (g[v[x]] == 0) q.push(v[x]);
  }
  int ans = 0;
  for (int i = 0; i < n; ++i)
    if (g[i] != 0 && !vis[i]) ans += dfs(i);
  cout << ans << endl;
  return 0;
}
