#include <bits/stdc++.h>
using namespace std;
long long modx(long long Base, long long exponent) {
  long long ans = 1;
  if (Base == 1) return Base;
  while (exponent) {
    if (exponent & 1) ans = (ans * Base) % 1000000007LL;
    Base = (Base * Base) % 1000000007LL;
    exponent = exponent >> 1;
  }
  return ans;
}
long long inmodx(long long num) { return (modx(num, 1000000007LL - 2LL)); }
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  if (a.first.first / 1100 < b.first.first / 1100) return 1;
  if (a.first.first / 1100 > b.first.first / 1100) return 0;
  if (a.first.second <= b.first.second) return 1;
  return 0;
}
const int N = (2e5) + 9;
const int M = (N << 2) + 9;
const int LOGN = ((int)log2(N)) + 3;
const int LOGM = ((int)log2(M)) + 3;
const int BUCK = 450;
int v[N];
int c[N];
int vis[N];
int val = 0;
void dfs(int cur, int comp) {
  vis[cur] = comp;
  if (vis[v[cur]] == comp) {
    int pt;
    val = c[cur];
    pt = v[cur];
    while (pt != cur) {
      val = min(val, c[pt]);
      pt = v[pt];
    }
    return;
  }
  if (vis[v[cur]] > 0) {
    val = 0;
    return;
  }
  dfs(v[cur], comp);
  return;
}
void solve() {
  int n, i, x, ans = 0;
  cin >> n;
  for (i = (1); i < (n + 1); i++) cin >> c[i];
  for (i = (1); i < (n + 1); i++) cin >> v[i];
  for (i = (1); i < (n + 1); i++)
    if (!vis[i]) {
      val = 1000000011;
      dfs(i, i);
      ans += val;
    }
  cout << ans << '\n';
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1, cs;
  for (cs = (1); cs < (t + 1); cs++) {
    solve();
  }
  return 0;
}
