VERY IMPORTANT NOTE TO ALL DEC COURSE 2022 PARTICIPANTS (due to delays in email communication): Elementary is gone, so Advanced has a diagnostic test (if you fail, no Dec Course). Syllabus of diagnostic is anything in these prerequisite notes, please review them at this link. Problemsets can be found at this link and under Contests (Collections).

Jack is a little boy with big dreams: to own the biggest candy shop in the world! He's off to a modest start with N candies for sale, numbered from 0 to N - 1. The price of candy i is i dollars. In his shop's front window, Jack has arranged his candies in the shape of a tree rooted at candy 0, with the parent of each other candy i (1 ≤ i < N) being candy Pi.

M other kids in Jack's neighbourhood have agreed to come by and be his first customers! Jack has assigned them customer numbers from 0 to M - 1. Unfortunately, these kids are a bit picky when it comes to sweets. Customer i only likes candies within the subtree rooted at candy Ci. This means that they're only willing to purchase a candy if it's either candy Ci, or one of its children, or one of its childrens' children, and so on. They also have amazing self-restraint — each customer will limit themselves to buying at most one candy.

For each of the M customers, Jack may either sell them any single candy of his choice within the customer's required subtree, or no candy at all. Each of the N candies may be sold to at most one customer. Jack is willing to do whatever it takes to achieve his dreams, even if it means extorting as much cash as possible from fellow children. As such, he'd like to choose candies to sell such that the sum of their prices is maximized. Can you help him determine this maximum sum of candy prices?

In order to reduce the size of the input data, C0..M-1 may be generated as follows using given constants A and B:

Ci = (A * i + B) % N (for i = 0 .. M - 1)

Input begins with an integer T, the number of trees. For each tree, there is first a line containing the space-separated integers N, M, A, and B. Then N - 1 lines follow, the ith of which (1-indexed) contains the single integer Pi.

For the ith tree, output a line containing "Case #i: " followed by the maximum possible sum of prices of candies which Jack can sell.

1 ≤ N ≤ 200,000

0 ≤ M ≤ 1,000,000

0 ≤ A, B, Pi, Ci < N

It is guaranteed that each test case describes a single valid tree rooted at candy 0.

In the second case, C = [2, 0, 2]. Jack should sell candy 2 to customer 0, candy 3 to customer 1, and no candy to customer 2. The sum of these candy prices is 2 + 3 = 5 dollars.

In the third case, C = [4, 6, 0, 2].

4 2 1 1 0 0 4 3 2 2 3 0 0 8 4 2 4 0 1 0 0 7 1 4 20 14 6 6 5 4 8 0 2 11 18 4 4 11 4 5 6 8 13 3 6 5 15

Case #1: 1 Case #2: 5 Case #3: 20 Case #4: 165

Subtask | Score |
---|---|

1 | 100 |

2 | 0 |