

The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains two space-separated integers, 'N' and 'L', denoting the number of elements in the array 'ARR', and the number of bricks ordered by the customer respectively.
The second line of each test case contains 'N' space-separated integers denoting the elements of the array 'ARR'.
For each test case, print a single integer - the maximum profit Ninja can attain by the order.
Print the output of each test case in a separate line.
1 <= T <= 100
1 <= N <= 10^3
1 <= ARR[i] <= 10^3
1 <= L <= min(SUM(ARR, 10^6))
Time limit: 1 sec
The idea is to sell the bricks having maximum cost first to the customer to obtain the maximum total profit. Therefore, our approach will be to find the brick with the maximum cost for each of the L transactions and update the cost of that brick after every transaction of the brick to the customer. Note that, if there are many distinct bricks having the same cost then Ninja could choose any brick from them to sell to a customer.
In this approach, we will maintain a heap and insert the cost of all bricks into the heap. We will use the heap to find the brick with maximum cost and sell it to the customer for each of the L transactions. After selling that brick, we will update the cost of the brick sold to the customer after the transaction and insert the updated cost into the heap.
The basic idea is to always try to sell the brick having maximum cost until that brick offers maximum profit.
Therefore, our approach will be to sort the input array in descending order and maintain a pointer placed at the second element of the array ARR. The first element in the array will offer maximum profit until its cost is greater than the array’s second element. At each step, we will find the total number of transactions of bricks to the customer to reduce the value of bricks to the value being pointed by the pointer and compare the total number of transactions with L. If L is greater, then increment the total profit by profit to reduce brick’s value to the value pointed by the pointer and decrement L by the number of transactions to reduce brick’s value and move the pointer ahead, otherwise, we will find the minimum value to which value of bricks could be reduced with the remaining L and update the total profit.
Let’s take ARR=[6,5,4,2], and the pointer will be pointing at value 5, so we will try to find the number of transactions to reduce the value of brick 6 to 5, that is 1, and move the pointer ahead. After 1 transaction of brick to the customer, ARR will be [5,5,4,2], and the pointer will be pointing at value 4. We will try to find the number of transactions to reduce both bricks of value 5 to 4, that is 2. After 3 transactions of brick to the customer, ARR will be [4,4,4,2]. At each step, we will compare the number of transactions to reduce brick’s value with L.
The formula used while updating the total profit is the sum of the n natural number (n*(n+1))/2 as the cost of the sold brick to the customer is decreased by 1 after each transaction. This approach works since we are trying to sell maximum-cost bricks to the customer to obtain maximum total profit.
The intuition behind the approach is to find the smallest number K such that the total number of bricks having cost more than K is smaller than L. After each transaction of a brick, the brick’s cost is decreased by 1, so our approach is trying to a minimum common value for all distinct bricks having a maximum cost till which value of bricks can be decreased efficiently, that value is K and Ninja could sell the bricks of a type till the cost of the brick remains more than K.
The variable low stores the value such that all bricks having cost more than low are sold to the customer. Iterate over the ARR to find the profit from all brick having cost more than low and increment answer by the profit attained by Ninja that is the profit obtained while reducing the value of the brick to low and decrement L the number of orders in which cost of the brick is greater than low and return the final answer.