#include <bits/stdc++.h>
using namespace std;
const long long int inf = 2222222222222222222LL;
const long long int mod = 1000000007LL;
long long int n = 0, m = 0, ans = 0, cnt = 0, tmp = 0, ma = -inf, mi = inf;
string s;
bool ok, flag;
long long int dx[5] = {1, -1, 0, 0, 0}, dy[5] = {0, 0, 1, -1, 0};
long long int ddx[9] = {1, -1, 0, 0, 1, 1, -1, -1, 0},
              ddy[9] = {0, 0, 1, -1, 1, -1, 1, -1, 0};
long long int to[2 * 111111], c[2 * 111111];
bool visited[2 * 111111];
vector<vector<long long int> > a;
map<long long int, long long int> mp;
void dfs(long long int now, long long int start) {
  if (visited[now]) return;
  if (mp[now] != 0 && mp[now] != start) return;
  if (mp[now] == start) {
    visited[now] = true;
    a[cnt].push_back(now);
  }
  mp[now] = start;
  dfs(to[now], start);
}
int main(void) {
  cin >> n;
  for (long long int i = (long long int)(1); i < (long long int)(n + 1); i++)
    scanf("%lld", &c[i]);
  for (long long int i = (long long int)(1); i < (long long int)(n + 1); i++)
    scanf("%lld", &to[i]);
  vector<long long int> b;
  a.push_back(b);
  for (long long int i = (long long int)(1); i < (long long int)(n + 1); i++) {
    tmp = cnt;
    dfs(i, i);
    if (a[cnt].size() > 0) {
      cnt++;
      a.push_back(b);
    }
  }
  for (long long int i = (long long int)(0LL);
       i < (long long int)(a.size() - 1); i++) {
    mi = inf;
    for (long long int j = (long long int)(0LL);
         j < (long long int)(a[i].size()); j++) {
      mi = min(mi, c[a[i][j]]);
    }
    ans += mi;
  }
  cout << ans << endl;
  return 0;
}
