#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int maxn = 2e5 + 10;
template <class T>
inline bool read(T &ret) {
  static char c;
  static int sgn;
  if (c = getchar(), c == EOF) return 0;
  for (; c != '-' && (c < '0' || c > '9'); c = getchar())
    ;
  sgn = (c == '-') ? -1 : 1;
  ret = (c == '-') ? 0 : (c - '0');
  for (; c = getchar(), c >= '0' && c <= '9'; ret = ret * 10 + (c - '0'))
    ;
  ret *= sgn;
  return 1;
}
template <class T>
inline void out(T x) {
  static short outa[32];
  static int len = 0;
  if (x == 0) {
    putchar(48);
    return;
  }
  if (x < 0) putchar('-'), x = -x;
  for (; x; outa[len++] = x % 10, x /= 10)
    ;
  for (; len; putchar(48 + outa[--len]))
    ;
}
template <class T>
T powmod(T a, T b) {
  T res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
template <class T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
namespace linear_seq {
const int N = 10010;
long long res[N], base[N], _c[N], _md[N];
vector<int> Md;
void mul(long long *a, long long *b, int k) {
  for (int i = 0; i < k + k; i++) _c[i] = 0;
  for (int i = 0; i < k; i++)
    if (a[i])
      for (int j = 0; j < k; j++) _c[i + j] = (_c[i + j] + a[i] * b[j]) % mod;
  for (int i = k + k - 1; i >= k; i--)
    if (_c[i])
      for (int j = 0; j < ((int)(Md).size()); j++)
        _c[i - k + Md[j]] = (_c[i - k + Md[j]] - _c[i] * _md[Md[j]]) % mod;
  for (int i = 0; i < k; i++) a[i] = _c[i];
}
int solve(long long n, vector<int> a, vector<int> b) {
  long long ans = 0, pnt = 0;
  int k = ((int)(a).size());
  assert(((int)(a).size()) == ((int)(b).size()));
  for (int i = 0; i < k; i++) _md[k - 1 - i] = -a[i];
  _md[k] = 1;
  Md.clear();
  for (int i = 0; i < k; i++)
    if (_md[i] != 0) Md.push_back(i);
  for (int i = 0; i < k; i++) res[i] = base[i] = 0;
  res[0] = 1;
  while ((1ll << pnt) <= n) pnt++;
  for (int p = pnt; p >= 0; p--) {
    mul(res, res, k);
    if ((n >> p) & 1) {
      for (int i = k - 1; i >= 0; i--) res[i + 1] = res[i];
      res[0] = 0;
      for (int j = 0; j < ((int)(Md).size()); j++)
        res[Md[j]] = (res[Md[j]] - res[k] * _md[Md[j]]) % mod;
    }
  }
  for (int i = 0; i < k; i++) ans = (ans + res[i] * b[i]) % mod;
  if (ans < 0) ans += mod;
  return ans;
}
vector<int> BM(vector<int> s) {
  vector<int> C(1, 1), B(1, 1);
  long long L = 0, m = 1, b = 1;
  for (int n = 0; n < ((int)(s).size()); n++) {
    long long d = 0;
    for (int i = 0; i < L + 1; i++) d = (d + (long long)C[i] * s[n - i]) % mod;
    if (d == 0)
      ++m;
    else if (2 * L <= n) {
      vector<int> T = C;
      long long c = mod - d * powmod(b, mod - 2) % mod;
      while (((int)(C).size()) < ((int)(B).size()) + m) C.push_back(0);
      for (int i = 0; i < ((int)(B).size()); i++)
        C[i + m] = (C[i + m] + c * B[i]) % mod;
      L = n + 1 - L;
      B = T;
      b = d;
      m = 1;
    } else {
      long long c = mod - d * powmod(b, mod - 2) % mod;
      while (((int)(C).size()) < ((int)(B).size()) + m) C.push_back(0);
      for (int i = 0; i < ((int)(B).size()); i++)
        C[i + m] = (C[i + m] + c * B[i]) % mod;
      ++m;
    }
  }
  return C;
}
int gao(vector<int> a, long long n) {
  vector<int> c = BM(a);
  c.erase(c.begin());
  for (int i = 0; i < ((int)(c).size()); i++) c[i] = (mod - c[i]) % mod;
  return solve(n, c, vector<int>(a.begin(), a.begin() + ((int)(c).size())));
}
};  // namespace linear_seq
const int MAXN = 2e5 + 10;
const int MAXM = 2e5 + 10;
struct Edge {
  int to, next;
} edge[MAXM];
int head[MAXN], tot;
int Low[MAXN], DFN[MAXN], Stack[MAXN], Belong[MAXN];
int Index, top;
int scc;
bool Instack[MAXN];
int num[MAXN];
void addedge(int u, int v) {
  edge[tot].to = v;
  edge[tot].next = head[u];
  head[u] = tot++;
}
void Tarjan(int u) {
  int v;
  Low[u] = DFN[u] = ++Index;
  Stack[top++] = u;
  Instack[u] = true;
  for (int i = head[u]; i != -1; i = edge[i].next) {
    v = edge[i].to;
    if (!DFN[v]) {
      Tarjan(v);
      if (Low[u] > Low[v]) Low[u] = Low[v];
    } else if (Instack[v] && Low[u] > DFN[v])
      Low[u] = DFN[v];
  }
  if (Low[u] == DFN[u]) {
    scc++;
    do {
      v = Stack[--top];
      Instack[v] = false;
      Belong[v] = scc;
      num[scc]++;
    } while (v != u);
  }
}
void solve(int N) {
  memset(DFN, 0, sizeof(DFN));
  memset(Instack, false, sizeof(Instack));
  memset(num, 0, sizeof(num));
  Index = scc = top = 0;
  for (int i = 1; i <= N; i++)
    if (!DFN[i]) Tarjan(i);
}
void init() {
  tot = 0;
  memset(head, -1, sizeof(head));
}
long long c[maxn];
int f[maxn];
int a[maxn];
int find(int gg) { return f[gg] == gg ? gg : f[gg] = find(f[gg]); }
long long c2[maxn];
bool vis[maxn];
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    for (int i = 1; i <= n; i++) scanf("%lld", &c[i]);
    init();
    for (int i = 1; i <= n; i++) {
      read(a[i]);
      addedge(i, a[i]);
    }
    solve(n);
    for (int i = 1; i <= scc; i++) {
      f[i] = i;
      c2[i] = 0x3f3f3f3f;
      ;
      vis[i] = false;
    }
    for (int i = 1; i <= n; i++) {
      if (Belong[i] != Belong[a[i]]) f[Belong[i]] = find(Belong[a[i]]);
      c2[Belong[i]] = min(c2[Belong[i]], c[i]);
    }
    for (int i = 1; i <= scc; i++) {
      vis[find(i)] = true;
    }
    long long sum = 0;
    for (int i = 1; i <= scc; i++)
      if (vis[i]) sum += c2[i];
    out(sum);
    puts("");
  }
  return 0;
}
