#include <bits/stdc++.h>
using namespace std;
int par[200002];
int main() {
  int n;
  cin >> n;
  int c[n];
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  int a[n];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int x = i;
    while (!par[x]) {
      par[x] = i;
      x = a[x];
    }
    if (par[x] == i) {
      int minn = c[x], y = a[x];
      while (y != x) {
        minn = min(minn, c[y]);
        y = a[y];
      }
      ans += minn;
    }
  }
  cout << ans;
  return 0;
}
