A. Turtle Puzzle: Rearrange and Negate

 A. Turtle Puzzle: Rearrange and Negate

time limit per test
2 seconds
memory limit per test
256 megabytes

You are given an array  of  integers. You must perform the following two operations on the array (the first, then the second):

  1. Arbitrarily rearrange the elements of the array or leave the order of its elements unchanged.
  2. Choose at most one contiguous segment of elements and replace the signs of all elements in this segment with their opposites. Formally, you can choose a pair of indices , such that 1 and assign = for all  (negate elements). Note that you may choose not to select a pair of indices and leave all the signs of the elements unchanged.

What is the maximum sum of the array elements after performing these two operations (the first, then the second)?

Input

The first line of the input contains a single integer  (11000) — the number of test cases. The descriptions of the test cases follow.

The first line of each test case contains a single integer  (150) — the number of elements in array .

The second line of each test case contains  integers 1,2,, (100100) — elements of the array.

Output

For each test case, output the maximum sum of the array elements after sequentially performing the two given operations.

input
Copy
8
3
-2 3 -3
1
0
2
0 1
1
-99
4
10 -2 -3 7
5
-1 -2 -3 -4 -5
6
-41 22 -69 73 -15 -50
12
1 2 3 4 5 6 7 8 9 10 11 12
output
Copy
8
0
1
99
22
15
270
78
Note

In the first test case, you can first rearrange the array to get [3,2,3] (operation 1), then choose =2,=3 and get the sum 3+((2)+(3))=8 (operation 2).

In the second test case, you can do nothing in both operations and get the sum 0.

In the third test case, you can do nothing in both operations and get the sum 0+1=1.

In the fourth test case, you can first leave the order unchanged (operation 1), then choose =1,=1 and get the sum (99)=99 (operation 2).

In the fifth test case, you can first leave the order unchanged (operation 1), then choose =2,=3 and get the sum 10+((2)+(3))+7=22 (operation 2).

In the sixth test case, you can first leave the order unchanged (operation 1), then choose =1,=5 and get the sum ((1)+(2)+(3)+(4)+(5))=15 (operation 2).

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int test,n;
  7. cin>>test;
  8. while(test--){
  9. int sum=0;
  10. int i=0;
  11. cin>>n;
  12. int a[n];
  13. for( i=0;i<n;i++)
  14. {
  15. cin>>a[i];
  16. sum = sum + abs(a[i]);
  17. }
  18. cout << sum << endl;
  19. sum=0;
  20. }
  21. return 0;
  22. }

Comments

Popular posts from this blog

Codeforce Problem 1703A. YES or YES?

1535A. Fair Playoff

Aptitude test assistant programmer 2018