#include <bits/stdc++.h>
using namespace std;
enum Color { WHITE, GRAY, BLACK };
vector<int> min_cost;
int dfs(int node, vector<vector<int>> &adj, vector<bool> &visited,
        vector<bool> &rec_stack, vector<int> &cost) {
  int new_cost = cost[node];
  if (!visited[node]) {
    visited[node] = 1;
    rec_stack[node] = 1;
    for (auto &next_node : adj[node]) {
      if (!visited[next_node]) {
        new_cost = min(new_cost, dfs(next_node, adj, visited, rec_stack, cost));
      } else if (rec_stack[next_node]) {
        rec_stack[next_node] = 0;
        if (node != next_node) {
          return new_cost;
        }
      }
      if (rec_stack[node] == 0) {
        rec_stack[node] = 1;
        min_cost.push_back(new_cost);
      }
    }
  }
  rec_stack[node] = 0;
  return new_cost;
}
int main() {
  int n;
  cin >> n;
  vector<vector<int>> adj(n + 1);
  vector<bool> vis(n + 1, 0);
  vector<bool> rec_stack(n + 1, 0);
  vector<int> cost(n + 1);
  int b_node;
  for (int i = 1; i <= n; i++) {
    cin >> cost[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> b_node;
    adj[i].emplace_back(b_node);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i, adj, vis, rec_stack, cost);
    }
  }
  long long ans = 0;
  for (auto &x : min_cost) {
    ans += x;
  }
  cout << ans;
}
