#include <bits/stdc++.h>
using namespace std;
int edge[200005], c[200005], mini, cycle_start;
bool vis[200005] = {false};
vector<int> V[200005];
void dfs2(int node) {
  vis[node] = true;
  for (int i = 0; i < V[node].size(); i++) {
    if (!vis[V[node][i]]) dfs2(V[node][i]);
  }
}
void process(int node) {
  while (true) {
    dfs2(node);
    mini = min(mini, c[node]);
    node = edge[node];
    if (node == cycle_start) break;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, ans = 0, i, node, done[200005] = {0};
  cin >> n;
  for (i = 1; i <= n; i++) cin >> c[i];
  for (i = 1; i <= n; i++) {
    cin >> edge[i];
    V[edge[i]].push_back(i);
  }
  for (i = 1; i <= n; i++) {
    mini = 1e9;
    if (!vis[i]) {
      node = i;
      while (true) {
        vis[node] = true;
        if (!vis[edge[node]])
          node = edge[node];
        else {
          cycle_start = edge[node];
          break;
        }
      }
      if (cycle_start >= i) {
        process(cycle_start);
        ans += mini;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
