#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000024;
int arr[MAX_N];
vector<int> all, val;
vector<int> vis;
int mi;
int s;
void dfs(int pos, bool is) {
  if (is) {
    vis[pos] = 2;
    mi = min(mi, val[pos]);
    if (pos == s) {
      vis[pos] = 2;
      return;
    }
    dfs(all[pos], 1);
  } else {
    mi = val[pos];
    if (vis[all[pos]] != 0) {
      if (vis[all[pos]] == 2) {
        vis[pos] = 2;
        mi = 0;
        return;
      }
      s = pos;
      vis[pos] = 2;
      dfs(all[pos], 1);
    } else {
      vis[pos] = 1;
      dfs(all[pos], 0);
      vis[pos] = 2;
    }
  }
}
int main() {
  int n;
  cin >> n;
  all.push_back(0);
  val.push_back(0);
  vis.push_back(0);
  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    val.push_back(tmp);
  }
  for (int i = 1; i <= n; i++) {
    vis.push_back(0);
    int tmp;
    cin >> tmp;
    all.push_back(tmp);
  }
  int res = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      mi = 999999;
      dfs(i, 0);
      res += mi;
    }
    vis[i] = 2;
  }
  cout << res << endl;
  getchar();
  return 0;
}
