#include <bits/stdc++.h>
using namespace std;
struct node {
  int from;
  int to;
} e[200005];
int n, ttt[200005], v[200005];
int pre[200005], vis[200005], used[200005];
long long ans;
int head[200005], q[200005];
int cnt, tot, minn;
void add_edge(int x, int y) {
  e[tot] = (node){y, head[x]};
  head[x] = tot++;
}
void dfs(int x) {
  for (int i = head[x]; i != -1; i = e[i].to) {
    int to1 = e[i].from;
    if (!vis[to1]) {
      vis[to1] = -1;
      dfs(to1);
    }
  }
}
void Find(int x) {
  minn = (1 << 30);
  used[x] = 1;
  for (int i;; x = i) {
    i = ttt[x];
    if (used[i]) {
      q[0] = i;
      minn = min(minn, v[i]);
      vis[i] = -1;
      for (int j = x; j != i; j = pre[j]) {
        q[++cnt] = j;
        minn = min(minn, v[j]);
        vis[j] = -1;
      }
      return;
    }
    pre[i] = x;
    used[i] = 1;
  }
}
int main() {
  scanf("%d", &n);
  memset(head, -1, sizeof(head));
  for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &ttt[i]);
    add_edge(ttt[i], i);
  }
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      cnt = 0;
      Find(i);
      ans += minn;
      for (int i = 0; i <= cnt; i++) dfs(q[i]);
    }
  }
  printf("%d", ans);
  return 0;
}
