#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx")
using namespace std;
const long long INF = 2e9 + 13;
const long long MOD = 1e9 + 7;
int N;
int go[300005];
long long C[300005];
bool visit[300005], insta[300005];
vector<vector<int> > cyc;
bool f = 0;
vector<int> v;
vector<int> nil;
void dfs(int cur) {
  if (visit[cur]) return;
  if (f) return;
  v.push_back(cur);
  visit[cur] = insta[cur] = 1;
  int nxt = go[cur];
  if (insta[nxt]) {
    f = 1;
    cyc.push_back(nil);
    int i = 0;
    while (v[i] != nxt) i++;
    for (; i < v.size(); i++) {
      int x = v[i];
      if (insta[x]) {
        insta[x] = 0;
        cyc.back().push_back(x);
      }
    }
    return;
  }
  dfs(nxt);
  insta[cur] = 0;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 1; i <= N; i++) cin >> C[i];
  for (int i = 1; i <= N; i++) cin >> go[i];
  for (int i = 1; i <= N; i++)
    if (!visit[i]) {
      f = 0;
      v.clear();
      dfs(i);
    }
  long long ans = 0;
  for (auto c : cyc) {
    long long mnV = 1e9;
    for (int x : c) mnV = min(mnV, C[x]);
    ans += mnV;
  }
  cout << ans << endl;
}
