#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, ans;
int arr[N];
int cost[N];
bool vis[N];
int minn(int x) {
int vis[N] = {0}, ans = INT_MAX;
while(vis[x] == 0)
ans = min(ans, cost[x]),vis[x] = 1, x = arr[x];
return ans;
}
void dfs(int x) {
bool vis2[N] = {0};
while(vis[x] == 0)
vis[x] = vis2[x] = 1, x = arr[x];
if(vis2[x]) ans += minn(x);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> cost[i];
for (int i = 1; i <= n; i++) cin >> arr[i];
for (int i = 1; i <= n; i++) if(!vis[i]) dfs(i);
cout << ans;
}