Google Code Jam '22 Qualification Round Problem D - Chain Reactions
View as PDFWile lives alone in the desert, so he entertains himself by building complicated machines that run on chain reactions. Each machine consists of  modules indexed 
. Each module may point at one other module with a lower index. If not, it points at the abyss.
Modules that are not pointed at by any others are called initiators. Wile can manually trigger initiators. When a module is triggered, it triggers the module it is pointing at (if any) which in turn may trigger a third module (if it points at one), and so on, until the chain would hit the abyss or an already triggered module. This is called a chain reaction.
Each of the  modules has a fun factor 
. The fun Wile gets from a chain reaction is the largest fun factor of all modules that triggered in that chain reaction. Wile is going to trigger each initiator module once, in some order. The overall fun Wile gets from the session is the sum of the fun he gets from each chain reaction.
For example, suppose Wile has  modules with fun factors 
, 
, 
, and 
 and module 
 points at the abyss, modules 
 and 
 at module 
, and module 
 at module 
. There are two initiators (
 and 
) that Wile must trigger, in some order.

As seen above, if Wile manually triggers module  first, modules 
, 
, and 
 will get triggered in the same chain reaction, for a fun of 
. Then, when Wile triggers module 
, module 
 will get triggered alone (module 
 cannot get triggered again), for a fun of 
, and an overall fun for the session of 
.

However, if Wile manually triggers module  first, modules 
 and 
 will get triggered in the same chain reaction, for a fun of 
. Then, when Wile triggers module 
, modules 
 and 
 will get triggered in the same chain reaction, for a fun of 
, and an overall fun for the session of 
.
Given the fun factors and the setup of the modules, compute the maximum fun Wile can get if he triggers the initiators in the best possible order.
Input Specification
The first line of the input gives the number of test cases, . 
 test cases follow, each described using 
 lines. Each test case starts with a line with a single integer 
, the number of modules Wile has. The second line contains 
 integers 
 where 
 is the fun factor of the 
 module. The third line contains 
 integers 
. If 
, that means module 
 points at the abyss. Otherwise, module 
 points at module 
.
Output Specification
For each test case, output one line containing Case #x: y, where  is the test case number (starting from 
) and 
 is the maximum fun Wile can have by manually triggering the initiators in the best possible order.
Limits
.
.
, for all 
.
Test Set 1
Time limit: 5 seconds.
.
Test Set 2
Time limit: 5 seconds.
.
Test Set 3
Time limit: 10 seconds.
.
Sample Input
3
4
60 20 40 50
0 1 1 2
5
3 2 1 4 5
0 1 1 1 0
8
100 100 100 90 80 100 90 100
0 1 2 1 2 3 1 3
Sample Output
Case #1: 110
Case #2: 14
Case #3: 490
Explanation for Sample
Sample Case #1 is the one explained in the problem statement.
In Sample Case #2, there are  initiators (modules 
 through 
), so there are 
 chain reactions. Activating them in order 
 yields chains of fun 
 for an overall fun of 
. Notice that we are summing the four highest fun numbers in the input, so there is no way to get more than that.
In Sample Case #3, an optimal activation order of the  initiators is 
.
Note
This problem has different time limits for different batches. If you exceed the Time Limit for any batch, the judge will incorrectly display >10.000s regardless of the actual time taken. Refer to the Limits section for batch-specific time limits.
Comments