#include <bits/stdc++.h>
using namespace std;
vector<long long int> c(200001), a(200001);
set<long long int> st;
vector<long long int> gr[200001];
int visited[200001] = {};
long long int cost = 0;
void dfs(long long int i) {
  visited[i] = -1;
  if (visited[gr[i][0]] == -1) {
    visited[i] = 1;
    st.insert(gr[i][0]);
  } else {
    if (visited[gr[i][0]] == 0) dfs(gr[i][0]);
    visited[i] = 1;
  }
}
long long int find_min(long long int i, long long int minn, long long int j) {
  if (gr[j][0] == i) {
    return min(minn, c[j]);
  } else {
    return find_min(i, min(minn, c[j]), gr[j][0]);
  }
}
int main() {
  ios::sync_with_stdio(false);
  long long int n;
  cin >> n;
  for (long long int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (long long int i = 1; i <= n; i++) {
    cin >> a[i];
    gr[i].push_back(a[i]);
  }
  for (long long int i = 1; i <= n; i++) {
    if (visited[i] == false) dfs(i);
  }
  while (st.size()) {
    long long int i = *(st.begin());
    st.erase(st.begin());
    cost = cost + find_min(i, c[i], i);
  }
  cout << cost;
  return 0;
}
