#include <bits/stdc++.h>
using namespace std;
int fmod(int x) { return x > 0 ? x : -x; }
int sqr(int x) { return x * x; }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int prices[200000];
  for (int i = 0; i < int(n); i++) cin >> prices[i];
  int rooms[200000];
  int tr;
  for (int i = 0; i < int(n); i++) {
    cin >> tr;
    rooms[i] = tr - 1;
  }
  int cycles[200000];
  for (int i = 0; i < int(n); i++) cycles[i] = -1;
  long long solution = 0LL;
  for (int i = 0; i < int(n); i++) {
    if (cycles[i] < 0) {
      int ii = i;
      while (cycles[ii] < 0) {
        cycles[ii] = 0;
        ii = rooms[ii];
      }
      if (cycles[ii] == 1) {
      } else {
        int min_pr = prices[ii];
        while (cycles[ii] != 1) {
          min_pr = min(min_pr, prices[ii]);
          cycles[ii] = 1;
          ii = rooms[ii];
        }
        solution += min_pr;
      }
      int ind = i;
      while (cycles[ind] != 1) {
        cycles[ind] = 1;
        ind = rooms[ind];
      }
    }
  }
  cout << solution << endl;
  return 0;
}
