#include <bits/stdc++.h>
using namespace std;
vector<long long> v1;
vector<long long>::iterator upper, lower, it;
vector<vector<long long>> adj(200005);
unordered_map<long long, long long> mp;
long long a[200005] = {0};
stack<long long> st;
long long vis[200005] = {0}, cycle[200005] = {0};
long long ans = 0;
void cycles(long long node) {
  stack<long long> st1 = st;
  long long ans1 = a[node];
  cycle[node] = 1;
  while (!st1.empty() && st1.top() != node) {
    ans1 = min(ans1, a[st1.top()]);
    cycle[st1.top()] = 1;
    st1.pop();
  }
  ans += ans1;
}
void dfs(vector<vector<long long>> &v, long long child) {
  long long i;
  for (i = 0; i < v[child].size(); i++) {
    if (vis[v[child][i]] == 0) {
      st.push(v[child][i]);
      vis[v[child][i]] = 1;
      dfs(v, v[child][i]);
    } else if (vis[v[child][i]] == 1 && cycle[v[child][i]] == 0) {
      cycles(v[child][i]);
    }
  }
  vis[st.top()] = 2;
  st.pop();
}
void solve() {
  long long n = 0, m = 0, i = 0, j = 0, l = 0, r = 0, x = 0, y = 0, q = 0,
            z = 0, len = 0, k = 0, p = 0;
  char s[300005], t[300005];
  vector<vector<long long>> v(200005);
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (i = 1; i <= n; i++) {
    cin >> x;
    v[i].push_back(x);
  }
  for (i = 1; i <= n; i++) {
    if (!vis[i]) {
      vis[i] = 1;
      st.push(i);
      dfs(v, i);
    }
  }
  cout << ans;
}
bool imp = false;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  if (imp) cin >> t;
  while (t--) solve();
  return 0;
}
