#include <bits/stdc++.h>
using namespace std;
const long long int N = 5e5 + 1;
long long int mod = 1000000007;
void normalize(long long int &a) { a = (a + mod) % mod; }
long long int inf = 10000000000000000;
long long int modmul(long long int a, long long int b) {
  a = a % mod;
  b = b % mod;
  normalize(a);
  normalize(b);
  return (a * b) % mod;
}
long long int modadd(long long int a, long long int b) {
  a = a % mod;
  b = b % mod;
  normalize(a);
  normalize(b);
  return (a + b) % mod;
}
long long int modsub(long long int a, long long int b) {
  a = a % mod;
  b = b % mod;
  normalize(a);
  normalize(b);
  return (a - b + mod) % mod;
}
long long int fact(long long int n) {
  if (n == 0 || n == 1)
    return 1;
  else
    return modmul(n, fact(n - 1));
}
void solve() {
  long long int n;
  cin >> n;
  vector<long long int> c(n + 1);
  vector<long long int> vis1(n + 1);
  for (long long int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  vector<long long int> G(n + 1);
  for (long long int i = 1; i < n + 1; i++) {
    cin >> G[i];
  }
  long long int ans = 0;
  for (long long int i = 1; i <= n; i++) {
    if (vis1[i] == 0) {
      long long int ck = 0;
      vis1[i] = 1;
      map<long long int, long long int> vis;
      long long int k = i;
      vis[i] = 1;
      long long int p = i;
      k = G[k];
      while (vis[k] != 1) {
        if (vis1[k] == 1 && vis[k] == 0) {
          ck = 1;
          break;
        }
        vis1[k] = 1;
        vis[k] = 1;
        p = k;
        k = G[k];
      }
      if (ck == 0) {
        long long int min1 = c[p];
        long long int j1 = k;
        while (j1 != p) {
          min1 = min(min1, c[j1]);
          j1 = G[j1];
        }
        ans += min1;
      }
    }
  }
  cout << ans;
}
signed main() {
  long long int t;
  t = 1;
  while (t--) {
    solve();
    cout << endl;
  }
}
