There are N ticket sellers each of whom has a certain amount of tickets. The price of a ticket is the number of tickets remaining with the ticket seller. They can sell a total of K tickets. Find the maximum amount they can earn by selling K tickets.
The first line of the input consists of a single integer T denoting the number of test cases. Each of the test case consists of two lines. First line of each test case consists of two integers N and K. The next line contains space separated N integers A, A, …, A[N-1] where A[i] denotes the number of ticket with ith ticket seller.
For each test case, print in a new line, the maximum amount they can earn by selling K tickets. Print your answer modulo 109 + 7
1 <= T <= 103
1 <= N <= 105
1 <= Ai, K <= 106
4 3 6 2 4
5 3 5 2 4 4
Assume 0-based indexing
Firstly, 2nd ticket seller can sell his one ticket at price = 6. Then, he will have 5 tickets with price = 5. He can again sell one ticket with price = 5. Now, he will have 4 tickets with price = 4. At last, any of the 0th, 2nd or 4th ticket seller can sell one ticket as all of them are priced at 4. Hence, the total amount after selling 3 tickets = 6 + 5 + 4 = 15.
Firstly, 0th or 2nd ticket seller can sell his one ticket at price = 5. Let us assume that 2nd ticket seller sells his ticket. Now, he would be left with 4 tickets each with price = 4. Now, 0th ticket seller will sell his one ticket at price = 5 and now he will have 4 tickets each with price = 4. Hence, the total amount after selling 2 tickets = 5 + 5 = 10.
If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.