#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.141592653589793238462643383;
long long int gcd(long long int a, long long int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long int Ceil(long long int a, long long int b) {
  if (a % b == 0)
    return a / b;
  else
    return a / b + 1;
}
const int MAX = 200009;
const int MOD = 1e9 + 7;
const int inf = 1e9 + 10;
long long int Abs(long long int x) {
  if (x < 0) return -x;
  return x;
}
long long int a[MAX], c[MAX], vis[MAX];
set<int> era, cyc;
void dfs1(int i) {
  vis[i] = 1;
  if (vis[a[i]] == 0)
    dfs1(a[i]);
  else if (vis[a[i]] == 1)
    cyc.insert(a[i]);
  vis[i] = 2;
}
long long int dfs2(int i) {
  if (vis[i] == 1) return 1e18;
  vis[i] = 1;
  era.insert(i);
  return min(c[i], dfs2(a[i]));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  for (int i = 0; i < (n); ++i) cin >> c[i];
  for (int i = 0; i < (n); ++i) cin >> a[i], a[i]--;
  long long int cst = 0;
  for (int i = 0; i < (n); ++i)
    if (vis[i] == 0) dfs1(i);
  (memset((vis), 0, sizeof(vis)));
  for (auto&(it) : (cyc))
    if (era.find(it) == era.end()) {
      cst += dfs2(it);
      era.insert(it);
    }
  cout << cst << '\n';
  return 0;
}
