


Let arr = [-2, 0, 5, -1, 2]
and K = 4
We will output 10 as we replace -2 by 2, -1 by 1, 0 by 0, and 0 by 0.
We can apply the flip operation to the same element multiple times
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test cases follow.
The first line of each test case contains ‘N’ denoting the number of elements in arr.
The second line of each test case contains ‘N’ space-separated integers denoting the elements of ‘arr’
The third line of each test case contains an integer ‘K’ which denotes the number of flips to be done.
For each test case, return an integer which is the largest sum after ‘K’ flips.
Output for each test case will be printed in a separate line.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 5000
-10^6 <= arr[i] <=10^6
1 <= K <= N
Time limit: 1 second
The key idea in solving this problem is that it is only beneficial to flip only the negative numbers to maximize our sum. Also, we can flip the positive numbers twice to again get a positive number.
We can find the negative numbers one by one as follows:
The key idea in solving this problem is to realize that we can simply sort the array to find the first ‘K’ elements
The algorithm will be -