#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N], b[N], c[N], d[N], e[N], p, x, y, tot, n, t = 1, g;
long long ans = 0;
int inline read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
void dfs(int v, int g, int f) {
  d[v] = g;
  e[f] = v;
  if (!d[a[v]]) {
    dfs(a[v], g, f + 1);
    return;
  }
  if (d[a[v]] == g) {
    y = f;
    p = 1;
    for (int i = 1; i < f; i++)
      if (a[v] == e[i]) {
        x = i;
        break;
      }
    if (!x) x = f;
  }
}
inline void init() {
  n = read();
  for (int i = 1; i <= n; i++) b[i] = read();
  for (int i = 1; i <= n; i++) a[i] = read();
}
void inline slv(int i) {
  g = 1e9;
  x = y = p = 0;
  t++;
  dfs(i, t, 1);
  if (p) {
    for (int j = x; j <= y; j++) g = min(g, b[e[j]]);
    ans += g;
  }
}
int main() {
  init();
  for (int i = 1; i <= n; i++) {
    tot = 0;
    if (d[i] || d[a[i]]) {
      d[i] = 1;
      continue;
    }
    if (a[i] == i) {
      d[i] = 1;
      ans += b[i];
      continue;
    }
    slv(i);
  }
  printf("%I64d", ans);
}
