#include <bits/stdc++.h>
using namespace std;
int n, x;
int a[200005];
int c[200005];
bool volt[200005];
vector<int> v[200005];
int ans;
int cs;
int j, k;
bool trap[200005];
int s;
void bejar(int z) {
  volt[z] = true;
  trap[z] = true;
  for (auto s : v[z]) {
    if (volt[s] == false) {
      bejar(s);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    v[a[i]].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] == i) {
      ans += c[i];
      bejar(i);
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!volt[i]) {
      volt[i] = true;
      j = i;
      while (!volt[a[j]]) {
        volt[a[j]] = true;
        j = a[j];
      }
      cs = c[a[j]];
      bool flag = false;
      if (trap[a[j]]) {
        cs = 0;
        flag = true;
      }
      s = a[j];
      k = a[j];
      while (a[k] != a[j]) {
        if (flag) break;
        if (trap[a[k]] == true) {
          cs = 0;
          flag = true;
          break;
        }
        trap[a[k]] = true;
        if (c[a[k]] < cs) s = a[k];
        cs = min(cs, c[a[k]]);
        k = a[k];
      }
      if (flag == false) {
        trap[s] = true;
        ans += cs;
      }
    }
  }
  cout << ans;
}
