#include <bits/stdc++.h>
using namespace std;
long long n, z, x, sta, s, p, b[200007], a[200007], us[200007];
void dfs(long long x) {
  if (us[x] != 0) {
    if (us[x] == 1) {
      long long z = x;
      long long maxx = 100000000;
      while (1) {
        maxx = min(maxx, b[z]);
        z = a[z];
        if (z == x) break;
      }
      s += maxx;
    }
    return;
  }
  us[x] = 1;
  dfs(a[x]);
  us[x] = 2;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> b[i];
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++)
    if (us[i] == 0) {
      p = i;
      dfs(i);
    }
  cout << s << endl;
}
