#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9 + 100;
const int MX = 200503;
int N, M, k;
int arr[MX];
int cnt[MX];
vector<int> G[MX];
int vis[MX];
vector<int> sr;
void dfs(int x) {
  vis[x] = 1;
  for (int i = 0; i < G[x].size(); i++) {
    int ch = G[x][i];
    if (vis[ch] == 1) {
      sr.push_back(ch);
    } else if (!vis[ch])
      dfs(ch);
  }
  vis[x] = 2;
}
int get(int x, int p) {
  int ret = arr[x];
  vis[x] = 1;
  for (int i = 0; i < G[x].size(); i++) {
    int ch = G[x][i];
    if (vis[ch]) continue;
    ret = min(ret, get(ch, x));
  }
  return ret;
}
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &arr[i]);
  }
  for (int i = 1; i <= N; i++) {
    int x;
    scanf("%d", &x);
    G[i].push_back(x);
  }
  long long an = 0;
  for (int i = 1; i <= N; i++)
    if (vis[i] == 0) dfs(i);
  memset(vis, 0, sizeof vis);
  for (int i = 0; i < sr.size(); i++) an += get(sr[i], -1);
  cout << an << '\n';
  return 0;
}
