#include <bits/stdc++.h>
using namespace std;
vector<int> parent, c;
vector<bool> seen;
vector<vector<int>> in;
int root(int x) {
  while (x != parent[x]) {
    parent[x] = parent[parent[x]];
    x = parent[x];
  }
  return x;
}
long long mincost(int u) {
  if (seen[u]) return c[u];
  seen[u] = 1;
  if (in[u].size() == 0) return c[u];
  long long ans = 0;
  for (int v : in[u]) ans += mincost(v);
  if (ans == 0) {
    int x = in[u][0];
    long long ans = c[u];
    while (x != u) {
      ans = min(ans, 1ll * c[x]);
      x = in[x][0];
    }
    return ans;
  }
  return min(1ll * c[u], ans);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  parent.resize(n);
  iota(parent.begin(), parent.end(), 0);
  in.resize(n);
  c.resize(n);
  seen.resize(n);
  vector<int> incycle, par(n);
  for (int i = 0; i < (int)n; i++) cin >> c[i];
  for (int i = 0; i < (int)n; i++) {
    int x;
    cin >> x;
    x--;
    if (root(x) == i)
      incycle.push_back(i);
    else
      parent[i] = x;
    par[i] = x;
    in[x].push_back(i);
  }
  long long ans = 0;
  for (int u : incycle) {
    long long mn = min(mincost(u), 1ll * c[u]);
    int x = par[u];
    while (x != u) {
      mn = min(mn, 1ll * c[x]);
      x = par[x];
    }
    ans += mn;
  }
  cout << ans << "\n";
}
