#include <bits/stdc++.h>
using namespace std;
int c[200005];
int a[200005];
int par[200005];
int no[200005];
vector<int> adj[200005];
bool visited[200005];
vector<int> roots;
int cnt = 1;
void init() { return; }
void dfs(int src) {
  no[src] = cnt;
  visited[src] = true;
  for (auto it : adj[src]) {
    if (visited[it] && no[it] == no[src]) {
      roots.push_back(it);
    }
    if (!visited[it]) {
      dfs(it);
    }
  }
  return;
}
int solve(int src) {
  visited[src] = true;
  int ans = c[src];
  for (auto it : adj[src]) {
    if (!visited[it]) {
      ans = min(ans, solve(it));
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  init();
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
    adj[i].push_back(a[i]);
    par[a[i]] = i;
  }
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      dfs(i);
      cnt++;
    }
  }
  memset(visited, false, sizeof(visited));
  int ans = 0;
  for (auto it : roots) {
    if (!visited[it]) ans += solve(it);
  }
  cout << ans << endl;
  return 0;
}
