
If there are 3 stocks and the profit on them is [1,3,-2], and you are allowed to buy at most 1 stock, you can buy the second one and make a profit of 3.
The first line contains 'T', denoting the number of test cases.
For each Test :
The first line contains two space-separated integers, ‘N’ and ‘K’.
The second line contains ‘N’ integers, with the ith of them representing how much returns you get on the ith stock.
For each case, print one integer, the maximum profit you can make.
You are not required to print the expected output. It has already been taken care of. Just implement the function.
1 <= 'T' <= 10
1 <= ’K’ <= 'N' <= 10^5
-10^4 <= A[i] <= 10^4, i ∈ (1,N)
Note - The sum of 'N' over all test cases does not exceed 10^5.
Time Limit: 1 sec
Algorithm:
Each time, we can greedily try and pick the stock that gives us the largest profit. For this, we can sort the array of profits in descending order and start picking.
We can pick the ‘K’ largest profits to maximize our overall profit. However, there might be a chance that some of the ‘K’ largest stocks give negative profits, i.e loss. In that case, we only pick all the positive profits from the ‘K’ largest ones.