#include <bits/stdc++.h>
using namespace std;
const int maxm = 2e5 + 10;
struct edge {
  int u, v, next;
} g[maxm];
vector<int> vv[maxm];
int n, head[maxm], vis[maxm], dfn[maxm], low[maxm], dep;
int cnt, belong[maxm], chudu[maxm];
stack<int> stk;
void dfs(int u) {
  vis[u] = 1;
  dfn[u] = low[u] = ++dep;
  stk.push(u);
  for (int i = head[u]; i != -1; i = g[i].next) {
    int v = g[i].v;
    if (!vis[v]) {
      dfs(v);
      low[u] = min(low[u], low[v]);
    } else if (belong[v] == 0)
      low[u] = min(low[u], dfn[v]);
  }
  if (low[u] == dfn[u]) {
    ++cnt;
    while (1) {
      int d = stk.top();
      belong[d] = cnt;
      vv[cnt].push_back(d);
      stk.pop();
      if (d == u) break;
    }
  }
}
int mm[maxm];
int main() {
  int u, v;
  long long ans = 0;
  scanf("%d", &n);
  memset(head, -1, sizeof(head));
  memset(vis, 0, sizeof(vis));
  memset(dfn, 0, sizeof(dfn));
  memset(low, 0, sizeof(low));
  memset(belong, 0, sizeof(belong));
  memset(chudu, 0, sizeof(chudu));
  ans = dep = cnt = 0;
  while (stk.size()) stk.pop();
  for (int i = 1; i <= n; i++) scanf("%d", &mm[i]);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &v);
    g[i].u = i;
    g[i].v = v;
    g[i].next = head[i];
    head[i] = i;
  }
  for (int i = 1; i <= n; i++)
    if (vis[i] == 0) dfs(i);
  for (int i = 1; i <= n; i++)
    if (belong[g[i].u] != belong[g[i].v]) {
      chudu[belong[g[i].u]]++;
    }
  for (int i = 1; i <= cnt; i++) {
    if (chudu[i] == 0) {
      int tmp = 0x3f3f3f3f;
      for (int j = 0; j < vv[i].size(); j++) tmp = min(mm[vv[i][j]], tmp);
      ans += tmp;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
