|
<p> |
|
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 <strong>N</strong> candies for sale, numbered from 0 to <strong>N</strong> - 1. |
|
The price of candy <em>i</em> is <em>i</em> 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 <em>i</em> (1 ≤ <em>i</em> < <strong>N</strong>) being candy <strong>P<sub>i</sub></strong>. |
|
</p> |
|
|
|
<p> |
|
<strong>M</strong> 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 <strong>M</strong> - 1. |
|
Unfortunately, these kids are a bit picky when it comes to sweets. Customer <em>i</em> only likes candies within the subtree rooted at candy <strong>C<sub>i</sub></strong>. |
|
This means that they're only willing to purchase a candy if it's either candy <strong>C<sub>i</sub></strong>, 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. |
|
</p> |
|
|
|
<p> |
|
For each of the <strong>M</strong> 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 <strong>N</strong> 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? |
|
</p> |
|
|
|
<p> |
|
In order to reduce the size of the input data, <strong>C<sub>0..M-1<sub></strong> may be generated as follows using given constants <strong>A</strong> and <strong>B</strong>: |
|
</p> |
|
|
|
<p> |
|
<strong>C<sub>i<sub></strong> = (<strong>A</strong> * <em>i</em> + <strong>B</strong>) % <strong>N</strong> (for <em>i</em> = 0 .. <strong>M</strong> - 1) |
|
</p> |
|
|
|
<h3>Input</h3> |
|
|
|
<p> |
|
Input begins with an integer <strong>T</strong>, the number of trees. |
|
For each tree, there is first a line containing the space-separated integers <strong>N</strong>, <strong>M</strong>, <strong>A</strong>, and <strong>B</strong>. |
|
Then <strong>N</strong> - 1 lines follow, the <em>i</em>th of which (1-indexed) contains the single integer <strong>P<sub>i</sub></strong>. |
|
</p> |
|
|
|
|
|
<h3>Output</h3> |
|
|
|
<p> |
|
For the <em>i</em>th tree, output a line containing "Case #<em>i</em>: " followed by the maximum possible sum of prices of candies which Jack can sell. |
|
</p> |
|
|
|
|
|
<h3>Constraints</h3> |
|
|
|
<p> |
|
1 ≤ <strong>T</strong> ≤ 20 <br /> |
|
1 ≤ <strong>N</strong> ≤ 200,000 <br /> |
|
0 ≤ <strong>M</strong> ≤ 1,000,000 <br /> |
|
0 ≤ <strong>A</strong>, <strong>B</strong>, <strong>P<sub>i</sub></strong>, <strong>C<sub>i</sub></strong> < <strong>N</strong> <br /> |
|
</p> |
|
|
|
<p> |
|
It is guaranteed that each test case describes a single valid tree rooted at candy 0. |
|
</b> |
|
|
|
|
|
<h3>Explanation of Sample</h3> |
|
|
|
<p> |
|
In the first case, <strong>C</strong> = [0]. |
|
The only customer can be sold any candy in candy 0's subtree, which includes candies 0 and 1. Jack should choose to sell them candy 1, which has a price of 1 dollar. |
|
</p> |
|
|
|
<p> |
|
In the second case, <strong>C</strong> = [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. |
|
</p> |
|
|
|
<p> |
|
In the third case, <strong>C</strong> = [4, 6, 0, 2]. |
|
</p> |
|
|