#include <bits/stdc++.h>
using namespace std;
vector<int> order;
int visited[200005];
int c[200005];
int inf = 1e9 + 7;
int minimum;
int ans = 0;
void dfsreverse(int start, vector<int> revadj[], int visited[]) {
  visited[start] = 1;
  for (int i = 0; i < revadj[start].size(); i++) {
    if (visited[revadj[start][i]] == 0)
      dfsreverse(revadj[start][i], revadj, visited);
  }
  order.push_back(start);
}
void dfs(int start, int id, vector<int> adj[], int visited[]) {
  visited[start] = id;
  minimum = min(minimum, c[start]);
  for (int j = 0; j < adj[start].size(); j++) {
    if (visited[adj[start][j]] == 0) dfs(adj[start][j], id, adj, visited);
  }
}
void dfs3(int start, vector<int> dag[], int visited[]) {
  visited[start] = 1;
  if (dag[start].size() == 0) {
    ans += c[start];
  } else {
    for (int i = 0; i < dag[start].size(); i++) {
      if (visited[dag[start][i]] == 0) dfs3(dag[start][i], dag, visited);
    }
  }
}
int main() {
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++) cin >> c[i];
  for (int i = 1; i <= n; i++) cin >> a[i];
  vector<int> adj[n + 1];
  vector<int> revadj[n + 1];
  for (int i = 1; i <= n; i++) {
    adj[i].push_back(a[i]);
    revadj[a[i]].push_back(i);
  }
  for (int i = 0; i <= n; i++) visited[i] = 0;
  for (int i = 1; i <= n; i++) {
    if (visited[i] == 0) dfsreverse(i, revadj, visited);
  }
  reverse(order.begin(), order.end());
  for (int i = 0; i <= n; i++) visited[i] = 0;
  int u;
  for (int i = 0; i < order.size(); i++) {
    u = order[i];
    if (visited[u] == 0) {
      minimum = inf;
      dfs(u, u, adj, visited);
      c[u] = minimum;
    }
  }
  int scc[n + 1];
  for (int i = 0; i <= n; i++) scc[i] = visited[i];
  vector<int> dag[n + 1];
  vector<int> reversedag[n + 1];
  for (int i = 1; i <= n; i++) {
    if (scc[i] != scc[a[i]]) dag[scc[i]].push_back(scc[a[i]]);
    reversedag[scc[a[i]]].push_back(scc[i]);
  }
  for (int i = 0; i <= n; i++) visited[i] = 0;
  for (int i = 1; i <= n; i++) {
    if (visited[scc[i]] == 0) {
      dfs3(scc[i], dag, visited);
    }
  }
  cout << ans << endl;
}
