#include <bits/stdc++.h>
using namespace std;
void fast() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long n, c[300005], arr[300005];
vector<long long> adjList[300005];
long long visited[300005];
vector<long long> v;
map<long long, long long> m;
long long cnt;
void dfs(long long node) {
  visited[node] = 1;
  m[node] = 1;
  v.push_back(node);
  bool f1 = false, f2 = false;
  long long val;
  for (auto it : adjList[node]) {
    if (visited[it] == 1) f1 = true;
    if (m.count(it) == 1) {
      f2 = true;
      val = it;
    }
    if (visited[it] == 0 && m.count(it) == 0) {
      dfs(it);
    }
  }
  if (f1 && !f2) return;
  if (f2) {
    for (long long i = v.size() - 1; i >= 0; i--) {
      cnt = min(cnt, c[v[i]]);
      if (v[i] == val) break;
    }
    return;
  }
}
int32_t main() {
  fast();
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (long long i = 1; i <= n; i++) {
    cin >> arr[i];
    adjList[i].push_back(arr[i]);
  }
  for (long long i = 0; i < n + 1; i++) {
    visited[i] = 0;
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    if (visited[i] == 0) {
      cnt = 9e18;
      v.clear();
      m.clear();
      dfs(i);
      if (cnt != 9e18) ans += cnt;
    }
  }
  cout << ans << "\n";
  return 0;
}
