#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 5000 + 7;
const long long MOD = 1e9 + 7;
int pre[300500];
int find(int x) {
  if (pre[x] != x)
    return pre[x] = find(pre[x]);
  else
    return x;
}
void join(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx != fy) {
    pre[fx] = fy;
  }
  return;
}
long long a[300500];
long long v[300500];
long long solve(int i) {
  long long check = a[i];
  long long min1 = v[i];
  while (1) {
    if (i == check) {
      break;
    }
    min1 = min(v[check], min1);
    check = a[check];
  }
  return min1;
}
int main() {
  int n;
  int T;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%I64d", &v[i]);
  }
  for (int i = 1; i <= n; i++) {
    pre[i] = i;
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    if (find(i) == find(a[i])) {
      ans += solve(i);
    } else {
      join(i, a[i]);
    }
  }
  printf("%I64d\n", ans);
}
