#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;
template <typename T>
using vt = std::vector<T>;
template <typename T>
using vvt = vt<vt<T>>;
template <typename T>
using vvvt = vt<vvt<T>>;
using vi = vt<int>;
using vvi = vvt<int>;
using vvvi = vvvt<int>;
using vl = vt<ll>;
using vvl = vvt<ll>;
using vvvl = vvvt<ll>;
using vb = vt<bool>;
using vpii = vt<pii>;
static inline void Solve();
int main(int argc, char* argv[]) {
  {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
  };
  Solve();
  return 0;
}
constexpr int MAXC = 10000;
void Solve() {
  int n;
  cin >> n;
  vi c(n, 0), a(n, 0);
  for (int i = 0; i < n; i++) cin >> c[i];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  int ans = 0;
  vi st(n, 0);
  for (int i = 0; i < n; i++) {
    if (st[i]) continue;
    int j = i;
    while (st[j] == 0) {
      st[j] = 1;
      j = a[j];
    }
    if (st[j] == 1) {
      int cost = MAXC;
      int k = j;
      while (true) {
        cost = min(cost, c[k]);
        st[k] = 2;
        k = a[k];
        if (k == j) break;
      }
      ans += cost;
    }
    j = i;
    while (st[j] != 2) {
      st[j] = 2;
      j = a[j];
    }
  }
  cout << ans << endl;
}
