#include <bits/stdc++.h>
using namespace std;
vector<int> mpp[200010], mppr[200010];
set<int> mppc[200010];
int vis[200010], ord[200010], color[200010], b[200010];
int cnt;
void dfs(int u) {
  vis[u] = 1;
  for (auto v : mpp[u]) {
    if (!vis[v]) dfs(v);
  }
  ord[u] = cnt++;
}
void dfs2(int u) {
  vis[u] = 1;
  color[u] = cnt;
  for (auto v : mppr[u]) {
    if (!vis[v]) dfs2(v);
  }
}
void adde_SCC(int u, int v) {
  mpp[u].push_back(v);
  mppr[v].push_back(u);
}
int SCC(int N) {
  memset(vis, 0, sizeof(vis));
  cnt = 1;
  for (int i = (1); i <= (N); ++i)
    if (!vis[i]) dfs(i);
  for (int i = (1); i <= (N); ++i) b[i] = i;
  sort(b + 1, b + 1 + N, [](int xx, int yy) { return ord[xx] > ord[yy]; });
  memset(vis, 0, sizeof(vis));
  cnt = 1;
  for (int i = (1); i <= (N); ++i)
    if (!vis[b[i]]) {
      dfs2(b[i]);
      cnt++;
    }
  return cnt - 1;
}
void solve(int u) {
  vis[u] = 1;
  for (auto v : mpp[u]) {
    mppc[color[u]].insert(color[v]);
    if (!vis[v]) solve(v);
  }
}
int c[200010];
int suf[200010];
int main() {
  ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(20);
  int n;
  scanf("%d", &n);
  for (int i = (1); i <= (n); ++i) scanf("%d", c + i);
  for (int i = (1); i <= (n); ++i) {
    int v;
    scanf("%d", &v);
    adde_SCC(i, v);
  }
  int tot = SCC(n);
  memset(vis, 0, sizeof(vis));
  for (int i = (1); i <= (n); ++i)
    if (!vis[i]) solve(i);
  memset(suf, 127, sizeof(suf));
  for (int i = (1); i <= (n); ++i)
    suf[color[i]] = (suf[color[i]] < c[i] ? suf[color[i]] : c[i]);
  int ans = 0;
  for (int i = (1); i <= (tot); ++i) {
    if (mppc[i].size() == 1 && mppc[i].count(i)) ans += suf[i];
  }
  printf("%d\n", ans);
  return 0;
}
