#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
const int N = 100;
int idx[N];
int lowlink[N];
bool onstack[N];
struct edge {
  int to;
  edge(int _to) : to(_to){};
};
using node = vector<edge>;
using graph = vector<node>;
void visit(graph &g, int node, vector<vector<int> > &scc, stack<int> &st,
           vector<bool> &on, vector<int> &lowlink, vector<int> &id, int &cur) {
  lowlink[node] = id[node] = cur++;
  st.push(node);
  on[node] = true;
  for (auto e : g[node]) {
    if (id[e.to] == -1) {
      visit(g, e.to, scc, st, on, lowlink, id, cur);
      lowlink[node] = min(lowlink[node], lowlink[e.to]);
    } else if (on[e.to]) {
      lowlink[node] = min(lowlink[node], id[e.to]);
    }
  }
  if (lowlink[node] == id[node] && !st.empty()) {
    scc.push_back(vector<int>());
    while (1) {
      int t_node = st.top();
      st.pop();
      on[t_node] = false;
      scc.back().push_back(t_node);
      if (t_node == node) break;
    }
  }
}
vector<vector<int> > scc(graph &g) {
  int n = g.size();
  vector<int> id(n, -1), low(n);
  stack<int> st;
  vector<bool> on(n);
  int cur = 0;
  vector<vector<int> > scc;
  for (int i = 0; i < (int)n; ++i)
    if (id[i] == -1) visit(g, i, scc, st, on, low, id, cur);
  return scc;
}
void addedge(graph &g, int from, int to) { g[from].push_back(edge(to)); }
void calcidx(vector<vector<int> > &scc, vector<int> &idx) {
  for (int i = 0; i < (int)scc.size(); ++i) {
    for (int j = 0; j < (int)scc[i].size(); ++j) {
      idx[scc[i][j]] = i;
    }
  }
}
graph getCompressedGraph(graph &g, vector<vector<int> > &scc,
                         vector<int> &idx) {
  int n = g.size();
  int m = scc.size();
  calcidx(scc, idx);
  graph ret(m);
  for (int i = 0; i < (int)n; ++i)
    for (int j = 0; j < (int)g[i].size(); ++j)
      if (idx[i] != idx[g[i][j].to])
        ret[idx[i]].push_back(edge(idx[g[i][j].to]));
  return ret;
}
int main() {
  int n;
  scanf("%d", &n);
  vector<int> cost(n);
  for (int i = 0; i < (int)n; ++i) scanf("%d", &cost[i]);
  graph g(n);
  for (int i = 0; i < (int)n; ++i) {
    int x;
    scanf("%d", &x);
    addedge(g, i, x - 1);
  }
  auto SCC = scc(g);
  vector<int> idx(n);
  vector<int> ncost(SCC.size(), 1e9);
  for (int i = 0; i < (int)SCC.size(); ++i) {
    for (int j = 0; j < (int)SCC[i].size(); ++j) {
      idx[SCC[i][j]] = i;
      ncost[i] = min(ncost[i], cost[SCC[i][j]]);
    }
  }
  auto cg = getCompressedGraph(g, SCC, idx);
  int ret = 0;
  for (int i = 0; i < (int)cg.size(); ++i) {
    if (cg[i].size() == 0) ret += ncost[i];
  }
  cout << ret << endl;
  return 0;
}
