#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const int mxN = 200010;
int g[mxN];
int C[mxN];
int curID;
int vis[mxN];
pair<int, int> dfs(int v) {
  if (vis[v] && vis[v] < curID) return {0, -1};
  if (vis[v] == curID) return {C[v], v};
  vis[v] = curID;
  auto to = dfs(g[v]);
  if (to.second != -1) to.first = min(to.first, C[v]);
  if (v == to.second) to.second = -1;
  return to;
}
int main(int argc, char** argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  for (int i = 0; i < N; i++) cin >> C[i];
  for (int i = 0; i < N; i++) cin >> g[i], g[i]--;
  long long res = 0;
  for (int i = 0; i < N; i++)
    if (!vis[i]) ++curID, res += dfs(i).first;
  cout << res << '\n';
  return 0;
}
