#include <bits/stdc++.h>
using namespace std;
long long c[200005], a[200005], vis[200005], ans;
void dfs(long long x) {
  if (vis[x] == 2) {
    long long cost = c[x];
    for (long long nxt = a[x]; nxt != x; nxt = a[nxt]) cost = min(cost, c[nxt]);
    ans += cost;
    return;
  } else if (vis[x] == 1)
    return;
  vis[x] = 2;
  dfs(a[x]);
  vis[x] = 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> c[i];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  for (long long i = 0; i < n; i++) dfs(i);
  cout << ans;
  return 0;
}
