#include <bits/stdc++.h>
using namespace std;
int n, i, j, ans = 0;
int cost[200000 + 3], taken[200000 + 3], cycle = 0;
vector<int> v[200000 + 3];
bool visit[200000 + 3];
vector<int> V;
void dfs(int x) {
  visit[x] = 1;
  if (cycle) return;
  V.push_back(x);
  for (auto i : v[x]) {
    if (!visit[i])
      dfs(i);
    else if (visit[i]) {
      cycle = 1;
      int Min = (long long)1E8, Y = 0, f = 0, Z = 0, X;
      for (int I = V.size() - 1; I >= 0; I--) {
        if (taken[V[I]]) {
          f = 1;
          X = I;
          break;
        } else if (cost[V[I]] < Min)
          Min = cost[V[I]], Y = V[I];
        if (V[I] == i) {
          Z = 1;
          break;
        }
      }
      if (Z == 0) return;
      if (!f) ans += Min, taken[Y] = 1;
      return;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  int x;
  for (i = 1; i <= n; i++) cin >> cost[i];
  for (i = 1; i <= n; i++) {
    cin >> x;
    v[i].push_back(x);
  }
  for (int J = 1; J <= n; J++) {
    if (!visit[J]) {
      cycle = 0;
      V.clear();
      dfs(J);
    }
  }
  cout << ans;
}
