#include <bits/stdc++.h>
using namespace std;
const int NMAX = 200005;
vector<int> g[NMAX];
int cost[NMAX], in[NMAX];
bool visited[NMAX];
vector<int> G[NMAX];
queue<int> q;
long long ans;
void dfs(int node) {
  visited[node] = 1;
  for (auto it : G[node])
    if (!visited[it]) dfs(it);
  if (in[node]) ans = min(ans, 1LL * cost[node]);
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> cost[i];
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    g[i].push_back(x);
    G[x].push_back(i);
    G[i].push_back(x);
    if (x != i) in[x]++;
  }
  for (int i = 1; i <= n; i++)
    if (in[i] == 0) q.push(i);
  while (q.size()) {
    int node = q.front();
    q.pop();
    int to = g[node][0];
    in[to]--;
    if (in[to] == 0) q.push(to);
  }
  long long sol = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      ans = 200000000000;
      dfs(i);
      sol += 1LL * ans;
    }
  }
  cout << sol;
  return 0;
}
