#include <bits/stdc++.h>
using namespace std;
vector<bool> instak;
vector<vector<int>> adj, adj2, scc;
vector<int> dfn, lown, comp;
int tim;
stack<int> k;
void tarjan(int nod) {
  instak[nod] = 1;
  k.push(nod);
  dfn[nod] = lown[nod] = tim++;
  for (int i = 0; i < adj[nod].size(); i++) {
    int ch = adj[nod][i];
    if (dfn[ch] == -1) {
      tarjan(ch);
      lown[nod] = min(lown[nod], lown[ch]);
    } else if (instak[ch])
      lown[nod] = min(lown[nod], dfn[ch]);
  }
  if (dfn[nod] == lown[nod]) {
    scc.push_back(vector<int>());
    int x = -1;
    while (x != nod) {
      x = k.top();
      k.pop();
      instak[x] = 0;
      scc.back().push_back(x);
      comp[x] = scc.size() - 1;
    }
  }
}
int main() {
  int n, c[200005];
  cin >> n;
  for (int i = 0; i < n; i++) cin >> c[i];
  dfn = lown = comp = vector<int>(n, -1);
  adj = vector<vector<int>>(n, vector<int>());
  instak = vector<bool>(n, 0);
  scc.clear();
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    x--;
    adj[i].push_back(x);
  }
  for (int i = 0; i < n; i++)
    if (dfn[i] == -1) tarjan(i);
  adj2 = vector<vector<int>>(scc.size(), vector<int>());
  for (int i = 0; i < n; i++)
    for (int j = 0; j < adj[i].size(); j++)
      if (comp[i] != comp[adj[i][j]]) adj2[comp[i]].push_back(comp[adj[i][j]]);
  int sum = 0;
  for (int i = 0; i < scc.size(); i++)
    if (!adj2[i].size()) {
      int mn = c[scc[i][0]];
      for (int j = 0; j < scc[i].size(); j++) mn = min(mn, c[scc[i][j]]);
      sum += mn;
    }
  cout << sum << endl;
  return 0;
}
