#include <bits/stdc++.h>
using namespace std;
int n, c[200005], a[200005];
int b[200005];
stack<int> s;
void find_loops(int cur) {
  if (b[cur] == 0) {
    b[cur] = 1;
    find_loops(a[cur]);
    b[cur] = 2;
  } else if (b[cur] == 1) {
    s.push(cur);
  }
}
int dfs(int cur) {
  int ans;
  if (b[cur] == 0) {
    b[cur] = 1;
    ans = min(c[cur], dfs(a[cur]));
    b[cur] = 2;
  } else if (b[cur] == 1) {
    ans = 1000000007;
  } else if (b[cur] == 2) {
    ans = 0;
  }
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", c + i);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i);
  long long fin = 0;
  for (int i = 1; i <= n; ++i) find_loops(i);
  memset(b, 0, sizeof(b));
  while (s.size()) {
    fin += dfs(s.top());
    s.pop();
  }
  cout << fin << endl;
}
