#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int maxn = 2e5 + 6;
int n, x, y, ans, mn, d, p[maxn], cost[maxn], nex[maxn];
int find(int x) { return !p[x] ? x : p[x] = find(p[x]); }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 1; i < n + 1; ++i) cin >> cost[i];
  for (int i = 1; i < n + 1; ++i) cin >> nex[i];
  for (int i = 1; i < n + 1; ++i) {
    x = find(i);
    y = find(nex[i]);
    if (x != y)
      p[y] = x;
    else {
      mn = cost[i];
      d = nex[i];
      while (d != i) {
        mn = min(mn, cost[d]);
        d = nex[d];
      }
      ans += mn;
    }
  }
  cout << ans << "\n";
  return 0;
}
