#include <bits/stdc++.h>
using namespace std;
int w[200005];
vector<int> grafo[200005];
int visited[200000];
pair<int, int> dfs(int v) {
  if (visited[v] == 2) return {0, w[v]};
  visited[v] = 1;
  if (grafo[v].size() == 0) {
    visited[v] = 2;
    return {-1, w[v]};
  }
  int u = grafo[v][0];
  if (visited[u] == 2) {
    visited[v] = 2;
    return {0, w[u]};
  }
  if (visited[u] and u != v) return {1, u};
  dfs(u);
}
int dfs2(int v, int pai, int peso) {
  int u = grafo[v][0];
  if (visited[u] == 2) return 0;
  if (u == pai) return peso;
  dfs2(u, pai, min(peso, w[u]));
}
int dfs3(int v, int pai, int peso) {
  visited[v] = 2;
  w[v] = peso;
  int u = grafo[v][0];
  if (u == pai) return peso;
  dfs2(u, pai, peso);
}
int dfs4(int v, int peso) {
  visited[v] = 2;
  w[v] = peso;
  if (grafo[v].size() == 0) return 0;
  int u = grafo[v][0];
  dfs4(u, peso);
}
int main() {
  int n, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) scanf("%d", &w[i + 1]);
  for (int x = 1; x <= n; x++) {
    int y;
    scanf("%d", &y);
    if (x == y) continue;
    grafo[x].push_back(y);
  }
  for (int i = 1; i <= n; i++) {
    if (visited[i] != 0) continue;
    pair<int, int> aux = dfs(i);
    if (aux.first == 0) continue;
    if (aux.first == 1) {
      int peso = dfs2(aux.second, aux.second, w[aux.second]);
      dfs3(aux.second, aux.second, peso);
      ans += peso;
    } else {
      dfs4(i, aux.second);
      ans += aux.second;
    }
  }
  cout << ans << endl;
  return 0;
}
