#include <bits/stdc++.h>
using namespace std;
const int n_max = 2e5 + 5;
int idegree[n_max], cost[n_max], trans[n_max];
int main() {
  int n;
  queue<int> q;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &cost[i]);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &trans[i]);
    ++idegree[trans[i]];
  }
  for (int i = 1; i <= n; ++i) {
    if (idegree[i] == 0) q.push(i);
  }
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    --idegree[trans[x]];
    if (idegree[trans[x]] == 0) q.push(trans[x]);
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (idegree[i] != 0) {
      int temp = cost[i], u = trans[i];
      idegree[i] = 0;
      while (idegree[u] != 0) {
        temp = min(temp, cost[u]);
        idegree[u] = 0;
        u = trans[u];
      }
      ans += temp;
    }
  }
  printf("%d\n", ans);
}
