Debt
InterviewTime limit1sMemory limit256 MB
For every group size M, choose M loans to minimize M times the largest chosen loan minus their sum, and output the total of these minima.
- Level
Medium5 of 10
- Topics
- Sorting, Prefix sum
- Solved
- No attempts yet
Problem
Mingyun goes by the nickname debtor. The Kim Woohyun lab gave it to him because he is slow to pay back what he borrows.
Mingyun has borrowed money times, and the th loan is . The lab collects its money in an unusual way.
When the lab orders Mingyun to settle debts, he picks any of the loans. If the picked amounts are , he has to pay . The part he pays beyond the amounts he actually borrowed is the extra payment, so the extra payment is .
Suppose Mingyun borrowed , and in three loans and the lab orders him to settle two debts. If he picks the first and the second loan, he pays and the extra payment is . If he picks the first and the third loan, he pays and the extra payment is .
Mingyun wants the extra payment to be as small as possible. Let be the smallest extra payment he can reach when the lab orders him to settle debts.
For with the loans in that order:
- . Any single loan works.
- . Pick and , or pick and .
- . Pick .
- . Pick .
- . Pick all five.
Given and , compute .
Input
The first line has the number of test cases ().
Each of the next lines has the number of loans () followed by the borrowed amounts (), separated by spaces.
Output
For each test case, print on its own line.
The answer and the intermediate values can exceed the 32-bit integer range, so use a 64-bit integer type.