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.

**Input:**

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[0], A[1], …, A[N-1] where A[i] denotes the number of ticket with ith ticket seller.

**Output:**

For each test case, print in a new line, the maximum amount they can earn by selling K tickets. Print your answer modulo 10^{9} + 7

**Constraints:**

1 <= T <= 10^{3}

1 <= N <= 10^{5}

1 <= A_{i}, K <= 10^{6}

**Example:**

**Input:**

2

5 3

4 3 6 2 4

6 2

5 3 5 2 4 4

**Output:**

15

10

**Explanation:**

Assume 0-based indexing

**Testcase 1:**

Firstly, 2^{nd} 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 0^{th}, 2^{nd} or 4^{th} 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.

**Testcase 2:**

Firstly, 0^{th} or 2^{nd} ticket seller can sell his one ticket at price = 5. Let us assume that 2^{nd} ticket seller sells his ticket. Now, he would be left with 4 tickets each with price = 4. Now, 0^{th} 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.

Author: sajalagrawal

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.

makeitdonesolo | 64 |

ShubhamJain18 | 64 |

Bishnu Dev Panda | 54 |

DanishBansal | 50 |

abhithakur588 | 50 |

Bishnu Dev Panda | 777 |

yk12 | 512 |

okayboss | 490 |

DevMittal | 472 |

VijethaShastri | 431 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4925 |

Quandray | 4547 |

Login to report an issue on this page.