#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  vector<int> cost(n), to(n);
  for (int i = 0; (i) < (int)(n); (i)++) {
    scanf("%d", &cost[i]);
  }
  for (int i = 0; (i) < (int)(n); (i)++) {
    scanf("%d", &to[i]);
    to[i]--;
  }
  vector<int> color(n, -1);
  vector<bool> used(n);
  int k = 0;
  vector<int> ss;
  function<void(int)> dfs = [&](int u) {
    used[u] = true;
    if (!used[to[u]]) {
      dfs(to[u]);
    } else {
      if (color[to[u]] == -1) {
        color[to[u]] = k++;
        ss.push_back(to[u]);
      }
    }
    color[u] = color[to[u]];
  };
  for (int i = 0; (i) < (int)(n); (i)++)
    if (!used[i]) {
      dfs(i);
    }
  vector<vector<int>> cyc;
  vector<int> tmp;
  used.assign(n, false);
  function<void(int)> dfs2 = [&](int u) {
    used[u] = true;
    tmp.push_back(u);
    if (!used[to[u]]) dfs2(to[u]);
  };
  for (int i = 0; (i) < (int)(ss.size()); (i)++) {
    dfs2(ss[i]);
    cyc.push_back(tmp);
    tmp.clear();
  }
  long long ans = 0;
  for (auto c : cyc) {
    int mi = 10000000;
    for (auto i : c) {
      mi = min(mi, cost[i]);
    }
    ans += mi;
  }
  printf("%lld\n", ans);
  return 0;
}
