#include <bits/stdc++.h>
using namespace std;
int nextt[200007], c[200007], vis[200007];
long long ans, n;
template <class T>
void read(T &x) {
  int f = 0;
  x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    f |= (ch == '-');
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  x = f ? -x : x;
}
int MIN(int now) {
  int min = c[now];
  for (int i = nextt[now];; i = nextt[i]) {
    if (i == now)
      return min;
    else if (c[i] < min)
      min = c[i];
  }
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++) read(c[i]);
  for (int i = 1; i <= n; i++) {
    read(nextt[i]);
    if (nextt[i] == i) vis[i] = -1, ans += c[i];
  }
  for (int now = 1; now <= n; now++) {
    if (vis[now]) continue;
    for (int i = now;; i = nextt[i]) {
      if (vis[i]) {
        if (vis[i] == now) ans += MIN(i);
        break;
      }
      vis[i] = now;
    }
  }
  printf("%I64d", ans);
  return 0;
}
