[Code Jam 2022 Qualification Round 2022] Chain Reactions

Chain Reactions

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
31
32
33
34
35
#include <bits/stdc++.h>
using namespace std;

long long solve(int n, vector<int>& cost, vector<int>& adj) {
vector<pair<long long,int>> chain(n + 1, make_pair(0, INT_MAX));

long long res = 0;

for(int i = n; i >= 1; i--) {
auto [sum, mi] = chain[i];

if(sum) res += sum - mi;

int ncost = max(cost[i], mi == INT_MAX ? 0 : mi);

chain[adj[i]].first += ncost;
chain[adj[i]].second = min(chain[adj[i]].second, ncost);
}

return res + chain[0].first;
}

int main() {
int tc;
cin>>tc;
for(int i = 1; i <= tc; i++) {
int n;
cin>>n;
vector<int> cost(n + 1), adj(n + 1);
for(int i = 1; i <= n;i++) cin>>cost[i];
for(int i = 1; i <= n;i++) cin>>adj[i];
cout<<"Case #"<<i<<": "<<solve(n, cost, adj)<<endl;
}
return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/02/PS/Google/chain-reactions/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.