Rotten Ropes
InterviewTime limit1sMemory limit128 MB
Given the tear-off weights of n ropes, find the maximum object weight that a chosen subset can carry so that no rope in the subset breaks.
Problem
We have ropes of equal length and we want to use them to lift a heavy object. Each rope has an associated tear-off weight : if we try to lift an object heavier than with that single rope, the rope tears off. However, we may fasten several ropes to the object in parallel and lift it with all of them together. When ropes are used to lift an object of weight , each of the ropes -- regardless of its own tear-off weight -- is assumed to carry a weight of . If for a rope whose tear-off weight is , that rope tears off.
For example, three ropes with tear-off weights , , and , when all three are fastened to one object, cannot lift an object heavier than unless the weakest rope tears off; but the second rope alone can lift an object of weight at most .
Given the tear-off weights of the ropes, find the weight of the heaviest object that can be lifted by fastening some subset of the ropes so that none of them tears off.
Input
The first line contains a single integer (), the number of test cases, followed by the data for each test case. The first line of each test case contains a single integer (), the number of ropes. The next line contains integers between and , the tear-off weights of the ropes, separated by spaces.
Output
For each test case, output on its own line a single number: the largest weight that can be lifted without tearing off any chosen rope.