#include <bits/stdc++.h>
using namespace std;
int n;
int c[200010], ans;
int v[200010], p[200010], cnt[200010];
set<int> ss;
queue<int> q;
bitset<200010> vis;
void init() {
  ans = 0;
  ss.clear();
  vis.reset();
  memset(c, 0, sizeof(c));
  memset(v, 0, sizeof(v));
  memset(cnt, 0, sizeof(cnt));
  while (q.size()) q.pop();
  for (int i = 1; i < 200010; i++) p[i] = i;
}
int findset(int x) { return x == p[x] ? x : p[x] = findset(p[x]); }
void merge(int x, int y) {
  x = findset(x);
  y = findset(y);
  if (x == y) return;
  p[y] = x;
  c[x] = min(c[x], c[y]);
}
int main() {
  cin.tie(0), cout.sync_with_stdio(false);
  init();
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> c[i];
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    cnt[v[i]]++;
  }
  for (int i = 1; i <= n; i++)
    if (cnt[i] == 0) {
      vis[i] = 1;
      q.push(i);
    }
  while (q.size()) {
    int now = q.front();
    q.pop();
    vis[now] = 1;
    cnt[v[now]]--;
    if (cnt[v[now]] == 0) q.push(v[now]);
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) merge(i, v[i]);
  for (int i = 1; i <= n; i++)
    if (!vis[i]) ss.insert(findset(i));
  for (auto i : ss) ans += c[i];
  cout << ans << '\n';
  return 0;
}
