The Minimum Game solution codechef starters 12

The Minimum Game solution codechef starters 12

 

Alice and Bob have invented a new game. They have a rooted tree having N nodes (rooted at node 1), and the ith node has the value Ai associated with it (1-indexed). The value associated with the root node is 0.

They start with a marker at the root node, and a score of 0. Alice and Bob moves alternatively, with Alice moving first. In one move, they can move the pointer from a node to one of its child node, and add the value associated with that child node to the score. If the marker reaches a leaf node, the game ends.

Alice wants to minimize the score of the game, whereas Bob wants to maximize the score. They were about to start the game when the Charlie arrives. He looked at the game, and declared the game to be boring. However, Charlie asked them – “What will be the score at the end of the game, if each one of you can skip at most K moves?”

Can you help Alice and Bob to answer Charlie?

Input Format

  • The first line of input contains a single integer T, denoting the number of testcases. The description of the T testcases follows.
  • The first line of each test case contains two space separated integers N and K.
  • The second line of each test case contains N space separated integers A1,A2,...,AN. It is guaranteed that A1=0.
  • The following N1 lines contains two space separated integers x and y, which denotes that there is an edge between nodes x and y.

Output Format

For each testcase, output in a single line the answer to the Charlie’s problem.

Constraints

  • 1T104
  • 1N105
  • 0K200
  • 109Ai109
  • 1x,yN
  • The sum of N over all test cases does not exceed 105

Sample Input 1 

3
3 1
0 1 2
1 2
1 3
4 0
0 1 2 3
1 2
1 3
2 4
7 0
0 2 1 4 2 7 3
1 2
1 3
2 4
2 5
3 6
3 7

Sample Output 1 

1
2
6

Explanation

Test Case 1: Alice can move the marker to node 2, and hence making score =1. If Alice skips her chance, then Bob can move the marker to node 3, and hence making score = 2. Therefore, Alice will not skip her chance.

Test Case 2: If Alice moves the marker to node 2, then Bob will move the marker to node 4, making score = 4. If Alice moves the marker to node 3, the game will end, and the score will be 2.

Leave a Comment