#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e5 + 11;
typedef int i_N[maxN];
int N;
i_N A, C, deg, visit;
int main() {
  cin >> N;
  for (int i = 1; i <= N; i++) cin >> C[i];
  for (int i = 1; i <= N; i++) cin >> A[i], deg[A[i]]++;
  for (int u = 1; u <= N; u++)
    if (!deg[u] && !visit[u]) {
      int v = u;
      while (!visit[v] && !deg[v]) {
        visit[v] = 1;
        deg[A[v]]--;
        v = A[v];
      }
    }
  int ans = 0;
  for (int u = 1; u <= N; u++)
    if (!visit[u]) {
      int tmp = C[u];
      visit[u] = 1;
      for (int v = A[u]; v != u; v = A[v]) {
        visit[v] = 1;
        tmp = min(tmp, C[v]);
      }
      ans += tmp;
    }
  cout << ans;
}
