We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a94d361 commit cbb4871Copy full SHA for cbb4871
chainreactions.py3
@@ -0,0 +1,20 @@
1
+def chain_reaction():
2
+ N = int(input())
3
+ F = [0]+list(map(int, input().split()))
4
+ P = list(map(int, input().split()))
5
+ adj = [[] for _ in range(N+1)]
6
+ for u, v in enumerate(P, 1):
7
+ adj[v].append(u)
8
+ result = 0
9
+ for v in reversed(range(N)):
10
+ if not adj[v]:
11
+ continue
12
+ u = min(adj[v], key=F.__getitem__)
13
+ F[v] = max(F[v], F[u])
14
+ result += sum(F[nu] for nu in adj[v] if nu != u)
15
+ result += F[0]
16
+ return result
17
+
18
+for case in range(int(input())):
19
+ print('Case #%d: %s' % (case+1, chain_reaction()))
20
0 commit comments