#include <bits/stdc++.h>
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int n;
int c[N];
int dfn[N], low[N], timestamp;
int stk[N], top;
bool in_stk[N];
int id[N];
vector<int> edge[N];
int res[N];
int out[N];
void tarjan(int u) {
  dfn[u] = low[u] = ++timestamp;
  stk[++top] = u, in_stk[u] = true;
  for (auto w : edge[u]) {
    if (!dfn[w]) {
      tarjan(w);
      low[u] = min(low[u], low[w]);
    } else if (in_stk[w])
      low[u] = min(low[u], dfn[w]);
  }
  if (dfn[u] == low[u]) {
    int y;
    do {
      y = stk[top--];
      in_stk[y] = false;
      id[y] = u;
      res[u] = min(res[u], c[y]);
    } while (u != y);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  memset(res, INF, sizeof(res));
  for (int i = 1; i <= n; ++i) cin >> c[i];
  for (int i = 1; i <= n; ++i) {
    int v;
    cin >> v;
    edge[i].push_back(v);
  }
  for (int i = 1; i <= n; ++i) {
    if (!dfn[i]) tarjan(i);
  }
  for (int i = 1; i <= n; ++i) {
    for (auto w : edge[i]) {
      int fu = id[i];
      int fv = id[w];
      if (fu == fv) continue;
      out[fu]++;
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    int fu = id[i];
    if (fu != i || out[fu]) continue;
    ans += res[fu];
  }
  cout << ans << '\n';
  return 0;
}
