#include <bits/stdc++.h>
using namespace std;
int cost[200000 + 5], jump[200000 + 5], mark[200000 + 5];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, i, j, k, temp, ticks = 0, ans = 0;
  cin >> n;
  for (i = 1; i <= n; ++i) cin >> cost[i];
  for (i = 1; i <= n; ++i) cin >> jump[i];
  for (i = 1; i <= n; ++i) {
    if (mark[i]) continue;
    ++ticks;
    j = i;
    mark[j] = ticks;
    while (true) {
      k = j;
      j = jump[j];
      if (mark[j]) {
        if (mark[j] != ticks) break;
        temp = cost[j];
        while (j != k) {
          j = jump[j];
          temp = min(temp, cost[j]);
        }
        ans += temp;
        break;
      } else
        mark[j] = ticks;
    }
  }
  cout << ans;
  return 0;
}
