#include <bits/stdc++.h>
using namespace std;
int n, start, cnt, total_cost, x, min_value, current_room;
vector<int> cost, next_room;
int main() {
  cin >> n;
  vector<int> visited(n);
  for (int i = 1; i <= n; i++) {
    cin >> x;
    cost.push_back(x);
  }
  for (int i = 1; i <= n; i++) {
    cin >> x;
    next_room.push_back(x - 1);
  }
  for (int i = 0; i < n; i++)
    if (!visited[i]) {
      cnt++;
      current_room = i;
      while (!visited[current_room]) {
        visited[current_room] = cnt;
        current_room = next_room[current_room];
      }
      if (visited[current_room] != cnt) continue;
      start = current_room;
      min_value = cost[start];
      current_room = next_room[start];
      while (start != current_room) {
        min_value = min(min_value, cost[current_room]);
        current_room = next_room[current_room];
      }
      total_cost += min_value;
    }
  cout << total_cost;
  return 0;
}
