Problem Description
Description
985 has 2 * n - 1 integers. Each time, he can change the signs of n of them, with no limit on the number of operations. What is the maximum sum he can obtain?
Input
The first line contains an integer t, indicating there are t test cases.
Each test case occupies two lines: the first line contains an integer n, and the next line contains 2*n-1 integers a[].
Note: 1 <= t <= 32, 1 <= n <= 1e3, -1e3 <= a[] <= 1e3.
Output
Output an integer representing the maximum sum that can be obtained.
Sample Input
| |
Sample Output
| |
Problem Analysis
Negative-number changes when n=3
(0 means negative, 1 means positive)
With one negative:
| |
With two negatives:
| |
With three negatives:
| |
Summary of the pattern:
| Negatives | Positives |
|---|---|
| 0 | 2n-1 |
| n | n-1 |
| 1 | 2n-2 |
| n-1 | n |
| 3 | 2n-4 |
| n-3 | n+2 |
| … | … |
It can be observed that when n is odd,
all negative numbers can always be turned into positive numbers.
When n is even
each operation can change an even number of negative numbers
So if the number of negative numbers is even,
they can also all be turned into positive numbers.
When the number of negative numbers is odd,
one number will always remain negative
(but it is not necessarily one of the original negative numbers)
To maximize the total sum,
we can make the number with the smallest absolute value negative.
| |
Problem link: ZZULI 1899 985’s Maximum Sum Problem