#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0;
  char c = getchar();
  bool f = 0;
  for (; c > '9' || c < '0'; f = c == '-', c = getchar())
    ;
  for (; c >= '0' && c <= '9'; x = (x << 1) + (x << 3) + c - '0', c = getchar())
    ;
  return f ? -x : x;
}
inline void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) write(x / 10);
  putchar(x % 10 + 48);
}
const int N = 200000 + 10, oo = 1e9;
int n, c[N], a[N], tmp, vis[N], Vis[N];
bool flag;
long long ans;
void dfs(int u) {
  int x = u;
  for (; !vis[x];) vis[x] = u, x = a[x];
  if (vis[x] != u) return;
  flag = 1;
  for (; !Vis[x];) tmp = min(tmp, c[x]), Vis[x] = 1, x = a[x];
}
int main() {
  n = read();
  for (int i = 1, _y = n; i <= _y; ++i) c[i] = read();
  for (int i = 1, _y = n; i <= _y; ++i) a[i] = read();
  for (int i = 1, _y = n; i <= _y; ++i)
    if (!vis[i]) {
      flag = 0, tmp = oo;
      dfs(i);
      if (flag) ans += tmp;
    }
  write(ans);
  puts("");
}
