#include <bits/stdc++.h>
using namespace std;
int n, a[200010], c[200010], vis[200010], ans;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> c[i];
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) {
    int x = i, v, mn;
    while (!vis[x]) vis[x] = i, x = a[x];
    if (vis[x] != i) continue;
    v = x;
    mn = c[x];
    while (a[x] != v) x = a[x], mn = min(mn, c[x]);
    ans += mn;
  }
  cout << ans;
}
