Facebook Hacker Cup 2015 Round 1
The fine people of Corpro Corp. are a festive bunch. Every holiday season, everybody buys a gift for their manager. A cynic might say that the employees are just trying to bribe their way to a better performance review, but if you asked them yourself, they'd say they just wanted to spread cheer.
The fine people of Corpro Corp. are a frugal bunch. When they buy gifts, they cooperate to collectively buy the least expensive gifts that they can. A cynic might say that the employees are cheap, but if you asked them yourself, they'd say it's the thought that counts.
There are
The employees each have a unique employee ID which is an integer from
If there exists a set of two or more employees
There are never two employees who are responsible for each other.
That would be a silly hierarchy indeed.
There are
The only thing that stops all employees from purchasing gifts that cost
For example, in a company with just 2 employees, at least
What's the minimum possible total expenditure across the whole company during the gift exchange?
Input
Input begins with an integer
Each hierarchy is made up of two lines.
The first line contains the integer
The second line contains
The
Output
For the Case #i:
followed by the smallest amount of money the entire company would need to spend.
Constraints
NOTE: The input file is about 10-20MB.
Explanation of Sample
In the first test case, the CEO will spend
In the second test case, employees #2 and #3 will spend
Sample Input
5
3
0 1 1
8
0 1 1 2 2 3 3 3
5
0 1 2 3 4
9
0 1 2 3 4 5 5 5 5
8
0 1 1 1 1 2 2 2
Sample Output
Case #1: 4
Case #2: 10
Case #3: 7
Case #4: 12
Case #5: 11
Comments
Wait, do the line of integers represent the number of higher ups or the ID number of the ith employee?