
For the array [ 4, 7, 9, 10] and ‘k’=2
In the first step, we can choose the last bag. So the answer will be 10 and the array will be [4, 7, 9, 5].
In the second step, we can choose the second last bag. So the answer will be 19 and the array will be [4, 7, 4, 5].
So the final output will be 19.
The first line contains a single integer ‘T’ denoting the number of test cases to be run. Then the test cases follow.
The first line of each test case contains two space-separated integers ‘N’ and ‘K’, representing the number of bags and the amount of time.
The second line of each test case contains ‘N’ space-separated integers representing the number of chocolates in each bag.
For each test case, print an integer representing the maximum number of chocolates you can eat in the given amount of time.
Output for each test case will be printed in a separate line.
You are not required to print anything, it has already been taken care of. Just implement the function.
1 <= T <= 100
1 <= N <= 10^5
1 <= ARR[i] <= 10^5
It is guaranteed that the sum of ‘N’ over all test cases doesn’t exceed 10^5.
Time Limit: 1 sec.
Since we have a limited amount of time and we have to maximize the number of chocolates we will try to take the maximum number of chocolates possible at each unit of time.
Algorithm:
We will use a priority queue to find the largest element in the array at each step. It will significantly reduce the time complexity from ‘N’ to ‘log(N)’. At each step, we will take the largest element from the priority queue, add it to the answer and push the half of maximum element back to the priority queue.
Algorithm: