#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
vector<int> adj[N];
vector<int> color(N);
stack<int> st;
vector<vector<int>> cycles;
void dfs(int u) {
  st.push(u);
  color[u] = 1;
  for (auto x : adj[u]) {
    if (color[x] == 0) {
      dfs(x);
    } else if (color[x] == 1) {
      vector<int> res;
      while (st.top() != x) {
        res.push_back(st.top());
        st.pop();
      }
      res.push_back(x);
      cycles.push_back(res);
    }
  }
  color[u] = 2;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> c(n + 1), a(n + 1);
  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++) {
    adj[i].push_back(a[i]);
  }
  for (int i = 1; i <= n; i++) {
    if (color[i] == 0) dfs(i);
  }
  int ans = 0;
  for (auto cc : cycles) {
    int mn = 1e9;
    for (auto x : cc) mn = min(mn, c[x]);
    ans += mn;
  }
  cout << ans << "\n";
  return 0;
}
