Maximise the Subsequence Sum Problem Code: SIGNFLIP Codechef Solutions
Maximise the Subsequence Sum Problem Code: SIGNFLIP
Chef has an array containing integers. The integers of the array can be positive, negative, or even zero.
Chef allows you to choose at most elements of the array and multiply them by .
Find the maximum sum of a subsequence you can obtain if you choose the elements of the subsequence optimally.
Note: A sequence is a subsequence of a sequence if can be obtained from by deletion of several (possibly, zero or all) elements. For example, is a subsequence of and , but not a subsequence of and . Note that empty sequence is also a subsequence.
Input Format
- The first line of the input contains a single integer denoting the number of test cases. The description of test cases follows.
- The first line of each test case contains two space-separated integers .
- The second line of each test case contains space-separated integers
Output Format
For each test case, print a single line containing one integer - the maximum sum of a subsequence you can obtain.
Constraints
- Sum of over all test cases does not exceed
Sample Input 1
3
6 2
6 -10 -1 0 -4 2
5 0
-1 -1 -1 -1 -1
3 3
1 2 3
Sample Output 1
22
0
6
Explanation
Test case : It is optimal to multiply with and then take the subsequence .
Test case : It is optimal to choose empty subsequence with a sum equal to .
Test case : We can take subsequence . Here, we do not multiply with any element.
Comments
Post a Comment