#include <bits/stdc++.h>
using namespace std;
long long n, dem = 0, Count = 0, low[300000], num[300000], b[300000], C[300000],
             D[300000];
bool d_[300000], D_[300000];
vector<long long> a[300000], Ver[300000];
stack<long long> s;
void tarjan(long long u) {
  num[u] = low[u] = ++dem;
  s.push(u);
  for (auto v : a[u]) {
    if (num[v])
      low[u] = min(low[u], num[v]);
    else {
      tarjan(v);
      low[u] = min(low[u], low[v]);
    }
  }
  if (low[u] == num[u]) {
    long long tmp;
    Count++;
    do {
      tmp = s.top();
      s.pop();
      b[tmp] = Count;
      low[tmp] = 1e18;
      num[tmp] = 1e18;
    } while (tmp != u);
  }
}
void DFS(long long u, long long pre) {
  D_[u] = false;
  if (Ver[u].size() == 0) d_[u] = true;
  for (int i = 0; i < Ver[u].size(); i++) {
    long long v = a[u][i];
    if (v != pre && D_[v]) DFS(v, u);
  }
}
int main() {
  cin >> n;
  for (long long i = 1; i <= n; i++) cin >> C[i];
  for (long long i = 1; i <= n; i++) {
    long long x;
    cin >> x;
    a[i].push_back(x);
  }
  memset(num, 0, sizeof num);
  for (long long i = 1; i <= n; i++)
    if (!num[i]) tarjan(i);
  fill(D + 1, D + Count + 1, 1e18);
  for (long long i = 1; i <= n; i++) D[b[i]] = min(D[b[i]], C[i]);
  for (long long u = 1; u <= n; u++)
    for (int j = 0; j < a[u].size(); j++) {
      int v = a[u][j];
      if (b[u] != b[v]) Ver[b[u]].push_back(b[v]);
    }
  long long res = 0;
  memset(d_, false, sizeof d_);
  memset(D_, true, sizeof D_);
  for (int i = 1; i <= Count; i++)
    if (D_[i]) DFS(i, i);
  for (int i = 1; i <= Count; i++)
    if (d_[i]) res += D[i];
  cout << res;
  return 0;
}
