#include <bits/stdc++.h>
using namespace std;
void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
int main() {
  optimizeIO();
  int n;
  cin >> n;
  vector<int> costs(n);
  vector<int> parent(n);
  vector<int> nodes(n, -1);
  for (int i = 0; i < n; i++) cin >> costs[i];
  for (int i = 0; i < n; i++) {
    cin >> parent[i];
    parent[i]--;
  }
  int cost = 0;
  int mini = INT32_MAX;
  int temp = 0;
  for (int t = 0; t < n; t++) {
    if (nodes[t] == -1) {
      int i = t;
      while (parent[i] != i && nodes[i] == -1) {
        nodes[i] = temp;
        i = parent[i];
      }
      if (nodes[i] == -1) {
        nodes[i] = temp;
        cost += costs[i];
        temp++;
      } else if (nodes[i] == temp) {
        mini = costs[i];
        temp++;
        nodes[i] = temp;
        i = parent[i];
        while (nodes[i] != temp) {
          mini = min(mini, costs[i]);
          nodes[i] = temp;
          i = parent[i];
        }
        cost += mini;
        temp++;
      } else {
        temp++;
        continue;
      }
    }
  }
  cout << cost << endl;
  return 0;
}
