#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const long long infll = 0x7fffffffffffffff;
const int mod = (int)(1e9) + 7;
const int inf = 0x7fffffff;
const int maxn = (int)(2e5) + 7;
const int maxm = 300;
int n, c[maxn];
int hd[maxn], out[maxn], cl[maxn], cnt;
int dfn[maxn], low[maxn], vis[maxn], stk[maxn], tp, ti, tot;
vector<int> v[maxn];
struct edge {
  int to, nxt, fr;
} e[maxn << 1];
void add(int u, int v) {
  e[++cnt] = {v, hd[u], u};
  hd[u] = cnt;
}
void tarjan(int now) {
  vis[now] = 1;
  stk[++tp] = now;
  dfn[now] = low[now] = ++ti;
  for (int i = hd[now]; i; i = e[i].nxt) {
    int to = e[i].to;
    if (!dfn[to]) {
      tarjan(to);
      low[now] = min(low[now], low[to]);
    } else if (vis[to])
      low[now] = min(low[now], dfn[to]);
  }
  if (dfn[now] == low[now]) {
    ++tot;
    while (stk[tp + 1] != now) {
      cl[stk[tp]] = tot;
      v[tot].push_back(stk[tp]);
      vis[stk[tp--]] = 0;
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    add(i, x);
  }
  for (int i = 1; i <= n; i++)
    if (!dfn[i]) tarjan(i);
  for (int i = 1; i <= n; i++) {
    int x = cl[e[i].fr], y = cl[e[i].to];
    if (x == y) continue;
    out[x]++;
  }
  long long res = 0;
  for (int i = 1; i <= tot; i++) {
    if (!out[i]) {
      long long mi = inf;
      for (auto x : v[i]) mi = min(mi, 1LL * c[x]);
      res += mi;
    }
  }
  printf("%lld", res);
  return 0;
}
