#include <bits/stdc++.h>
using namespace std;
void DFS(long long V);
long long Min(vector<long long> v);
long long c[200100];
long long a[200100];
long long Vis1[200100];
bool Vis2[200100];
vector<vector<long long> > Cycles;
int main() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) scanf("%I64d", &c[i]);
  for (long long i = 1; i <= n; i++) scanf("%I64d", &a[i]);
  for (long long i = 1; i <= n; i++)
    if (!Vis1[i]) DFS(i);
  long long N = Cycles.size();
  long long Answer = 0;
  for (long long i = 0; i < N; i++) Answer += Min(Cycles[i]);
  cout << Answer;
}
vector<long long> GetCycle(long long V) {
  vector<long long> Res;
  for (long long i = V; !Vis2[i]; i = a[i]) Vis2[i] = true, Res.push_back(i);
  return Res;
}
void DFS(long long V) {
  for (long long i = V;; i = a[i]) {
    if (Vis1[i] == 1) break;
    if (Vis1[i] == 2) {
      Cycles.push_back(GetCycle(i));
      break;
    }
    Vis1[i] = 2;
  }
  for (long long i = V; Vis1[i] != 1; i = a[i]) Vis1[i] = 1;
}
long long Min(vector<long long> v) {
  long long N = v.size();
  long long Res = 1e9;
  for (long long i = 0; i < N; i++) Res = min(Res, c[v[i]]);
  return Res;
}
