#include <bits/stdc++.h>
using namespace std;
const long long maxn = 200010;
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long long LINF = 1e18;
long long n, m, a[maxn], c[maxn];
map<long long, long long> M, N;
string s, ss;
set<long long> S;
vector<long long> adj[maxn];
long long col[maxn], p[maxn];
long long cycle_start = -1, cycle_end = -1;
void dfs(long long s) {
  col[s] = 1;
  for (auto u : adj[s]) {
    if (col[u] == 0)
      p[u] = s, dfs(u);
    else if (col[u] == 1)
      cycle_start = u, cycle_end = s;
  }
  col[s] = 2;
}
long long print_cycle(long long s, long long d) {
  vector<long long> cycle;
  cycle.push_back(s);
  for (long long i = d; i != s; i = p[i]) cycle.push_back(i);
  long long x = LINF;
  for (auto u : cycle) x = min(x, c[u]);
  return x;
}
int32_t main() {
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> c[i];
  for (long long i = 0; i < n; i++) {
    long long x;
    cin >> x;
    x--;
    adj[i].push_back(x);
  }
  fill(col, col + maxn, 0);
  fill(p, p + maxn, -1);
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    if (col[i] != 0) continue;
    cycle_start = -1, cycle_end = -1;
    dfs(i);
    if (cycle_start == -1) continue;
    sum += print_cycle(cycle_start, cycle_end);
  }
  cout << sum;
}
