#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using str = string;
const ll mod = (ll)(1e9 + 7);
int main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> cost(n), next(n), us(n);
  for (auto &it : cost) {
    cin >> it;
  }
  for (auto &it : next) {
    cin >> it;
    --it;
  }
  int ans = 0;
  int now = 0;
  for (int i = 0; i < n; ++i) {
    if (us[i]) continue;
    ++now;
    int v = i;
    for (; !us[v]; v = next[v]) {
      us[v] = now;
    }
    if (us[v] != now) continue;
    int res = cost[v];
    int start = v;
    v = next[v];
    for (; v != start; v = next[v]) {
      res = min(res, cost[v]);
    }
    ans += res;
  }
  cout << ans;
}
