#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T>
using V = vector<T>;
const ll inf = (1e18);
const ll mod = 998244353;
int GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll c, ll d) { return c / GCD(c, d) * d; }
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(5);
  }
} __init;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
V<ll> c, a;
class UF {
 public:
  vector<int> par, num;
  int find(int v) { return (par[v] == v) ? v : (par[v] = find(par[v])); }
  explicit UF(int N) : par(N), num(N, 1) {
    iota((par).begin(), (par).end(), 0);
  }
  void unite(int u, int v) {
    u = find(u), v = find(v);
    if (u == v) return;
    if (num[u] < num[v]) swap(u, v);
    num[u] += num[v];
    par[v] = u;
  }
  bool same(int u, int v) { return find(u) == find(v); }
  bool ispar(int v) { return v = find(v); }
  int size(int v) { return num[find(v)]; }
};
V<int> g;
V<bool> visit;
bool cc;
ll dfs(int cur, int last, ll now) {
  if (last == cur) {
    return now;
  } else if (!visit[a[cur]]) {
    visit[a[cur]] = true;
    return dfs(a[cur], cur, c[a[cur]]);
  } else {
    chmin(now, c[cur]);
    int next = (cc ? cur : last);
    cc = false;
    return dfs(a[cur], next, now);
  }
}
void pitsu() {
  ll n;
  cin >> n;
  c.assign(n, 0);
  a.assign(n, 0);
  visit.assign(n, false);
  g.clear();
  g.assign(n, 0);
  UF uf(n);
  V<int> cnt(n, 0);
  for (int i = 0; i < n; i++) cin >> c[i];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    uf.unite(i, a[i]);
    cnt[a[i]]++;
  }
  V<bool> used(n, false);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (cnt[i] == 0 && !used[uf.find(i)]) {
      visit[i] = true;
      used[uf.find(i)] = true;
      cc = true;
      ans += dfs(i, -1, c[i]);
    }
  }
  for (int i = 0; i < n; i++) {
    if (!used[uf.find(i)]) {
      visit[i] = true;
      used[uf.find(i)] = true;
      cc = true;
      ans += dfs(i, -1, c[i]);
    }
  }
  cout << ans << "\n";
}
int main() {
  int t;
  t = 1;
  while (t--) pitsu();
}
