Chain Reactions

아직 제출이 없습니다메모리 제한1024 MB

문제

Wile lives alone in the desert, so he entertains himself by building complicated machines that run on chain reactions. Each machine consists of NN modules indexed 1,2,,N1,2,\dots,N. 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 NN modules has a fun factor F_iF\_i. 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 44 modules with fun factors F_1=60F\_1=60, F_2=20F\_2=20, F_3=40F\_3=40, and F_4=50F\_4=50 and module 11 points at the abyss, modules 22 and 33 at module 11, and module 44 at module 22. There are two initiators (33 and 44) that Wile must trigger, in some order.

As seen above, if Wile manually triggers module 44 first, modules 44, 22, and 11 will get triggered in the same chain reaction, for a fun of max(50,20,60)=60\max{(50,20,60)}=60. Then, when Wile triggers module 33, module 33 will get triggered alone (module 11 cannot get triggered again), for a fun of 4040, and an overall fun for the session of 60+40=10060+40=100.

However, if Wile manually triggers module 33 first, modules 33 and 11 will get triggered in the same chain reaction, for a fun of max(40,60)=60\max{(40,60)}=60. Then, when Wile triggers module 44, modules 44 and 22 will get triggered in the same chain reaction, for a fun of max(50,20)=50\max{(50,20)}=50, and an overall fun for the session of 60+50=11060+50=110.

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.

입력

The first line of the input gives the number of test cases, TT. TT test cases follow, each described using 3 lines. Each test case starts with a line with a single integer NN, the number of modules Wile has. The second line contains NN integers F_1,F_2,,F_NF\_1,F\_2,\dots,F\_N where F_iF\_i is the fun factor of the ii-th module. The third line contains NN integers P_1,P_2,,P_NP\_1,P\_2,\dots,P\_N. If P_i=0P\_i=0, that means module ii points at the abyss. Otherwise, module ii points at module P_iP\_i.

출력

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the maximum fun Wile can have by manually triggering the initiators in the best possible order.

제한

  • 1T1001≤T≤100.
  • 1F_i1091≤F\_i≤10^9.
  • 0P_ii10≤P\_i≤i-1, for all ii.

힌트

Sample Case #1 is the one explained in the problem statement.

In Sample Case #2, there are 44 initiators (modules 22 through 55), so there are 44 chain reactions. Activating them in order 33, 55, 44, 22 yields chains of fun 33, 55, 44, 22 for an overall fun of 1414. 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 55 initiators is 44, 55, 77, 66, 88.