Rearrange
InterviewTime limit1sMemory limit512 MB
Choose an ordering of the array, subtract elements from n in that order until n drops to 0 or below, and report the smallest achievable result.
Problem
Here is the code of the getMin( ) function.
int getMin(int n, int arr[]){
arr2 = rearrange(arr);
for(int i = 0; i < arr2.length(); i = i+1){
n = n-arr2[i];
if (n <= 0) break;
}
return n;
}
getMin( ) takes an integer and an integer array arr, then calls rearrange( ). The rearrange( ) function takes an integer array and returns a new array. The new array holds the same members as the old one, and the position of each member may change.
How you write rearrange( ) is not judged. Choose the arrangement that makes the return value of getMin( ) as small as possible, and report that return value.
Input
The first line contains the number of test cases . ()
Each test case takes two lines. The first line contains the integer and the length of the array arr, separated by a space. (, ) The second line contains integers, arr[0] through arr[k-1], separated by spaces. () When is , the second line is empty.
Output
For each test case, print the smallest value getMin( ) can return, one per line. The value may be negative.
Hint
In the second test case of the example, you send [70, 60, 80] to rearrange( ) and it returns [80, 70, 60]. Then and , so the loop stops and the function returns . More than one arrangement can reach the minimum, but the value you print is that single minimum.