#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
long long cost[N], arr[N];
long long visited[N];
class DMouseHunt {
 public:
  long long res;
  long long get_min(long long head) {
    long long ans = cost[head], cur = arr[head];
    while (cur != head) ans = min(ans, cost[cur]), cur = arr[cur];
    return ans;
  }
  void dfs(long long x) {
    if (visited[x]) {
      if (visited[x] == 2) res += get_min(x);
      visited[x] = 1;
      return;
    }
    visited[x] = 2;
    dfs(arr[x]);
    visited[x] = 1;
  }
  void solve(std::istream& in, std::ostream& out) {
    long long n;
    in >> n;
    memset(visited, 0, sizeof visited);
    for (long long i = 1; i <= n; i++) in >> cost[i];
    for (long long i = 1; i <= n; i++) in >> arr[i];
    res = 0;
    for (long long i = 1; i <= n; i++) {
      if (!visited[i]) {
        dfs(i);
      }
    }
    out << res;
  }
};
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  DMouseHunt solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
