-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy path1027D-MouseHunt.cpp
More file actions
30 lines (23 loc) · 827 Bytes
/
1027D-MouseHunt.cpp
File metadata and controls
30 lines (23 loc) · 827 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <cstdio>
#include <vector>
int main(){
long n; scanf("%ld", &n);
std::vector<long> cost(n + 1, 0); for(long p = 1; p <= n; p++){scanf("%ld", &cost[p]);}
std::vector<long> before(n + 1, 0);
std::vector<long> next(n + 1, 0); for(long p = 1; p <= n; p++){scanf("%ld", &next[p]); ++before[next[p]];}
long sum(0);
for(long p = 1; p <= n; p++){if(before[p] == 1 && next[p] == p){sum += cost[p];}}
std::vector<long> vis(n + 1, 0);
for(long p = 1; p <= n; p++){
if(before[p]){continue;}
long rm(p), pay(1e9); bool upd(false);
while(vis[rm] < 2){
if(vis[rm] == 1){pay = (pay < cost[rm]) ? pay : cost[rm]; upd = true;}
++vis[rm];
rm = next[rm];
}
sum += upd * pay;
}
printf("%ld\n", sum);
return 0;
}