#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const int mod = inf + 7;
const double eps = 1e-9;
const double pi = acos(-1.0);
long long read_long() {
  long long ans = 0;
  char c;
  bool started = false;
  bool is_negative = false;
  while (true) {
    c = getchar();
    if ((c < '0' || c > '9') && c != '-' && !started) continue;
    if ((c < '0' || c > '9') && c != '-' && started) break;
    if (started) {
      ans = ans * 10;
    }
    started = true;
    if (c == '-')
      is_negative = true;
    else
      ans += c - '0';
  }
  if (is_negative) ans *= -1;
  return ans;
}
int n, c[200200], a[200200];
bool used[200200];
bool is_cycle[200200];
int dfs(int v) {
  map<int, int> pos;
  vector<int> b;
  while (!used[v]) {
    used[v] = 1;
    b.push_back(v);
    pos[v] = (int)b.size() - 1;
    v = a[v];
  }
  if (pos.count(v)) {
    int ans = inf;
    for (int i = pos[v]; i < (int)b.size(); i++) {
      ans = min(ans, c[b[i]]);
    }
    return ans;
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", c + i);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    a[i]--;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!used[i]) ans += dfs(i);
  }
  cout << ans << endl;
  return 0;
}
