#include <bits/stdc++.h>
using namespace std;
bool used[200005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  int c[n];
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      int cur = i;
      int t = 1e9;
      vector<int> st;
      while (!used[cur]) {
        used[cur] = true;
        st.push_back(cur);
        cur = a[cur];
      }
      bool was = false;
      for (auto x : st) {
        if (x == cur) {
          was = true;
        }
        if (was) {
          t = min(t, c[x]);
        }
      }
      ans += (was ? t : 0);
    }
  }
  cout << ans << '\n';
  return 0;
}
