CF1778A.Flip Flop Sum

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

Flip Flop Sum

You are given an array of nn integers a1,a2,,ana_1, a_2, \ldots, a_n. The integers are either 11 or 1-1. You have to perform the following operation exactly once on the array aa:

  • Choose an index ii (1i<n1 \leq i \lt n) and flip the signs of aia_i and ai+1a_{i+1}. Here, flipping the sign means 1-1 will be 11 and 11 will be 1-1.

What is the maximum possible value of a1+a2++ana_1 + a_2 + \ldots + a_n after applying the above operation?

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t5001 \le t \le 500). Description of the test cases follows.

The first line of each test case contains a single integer nn (2n1052 \le n \le 10^5), the length of the array aa.

The next line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (ai=1a_i = 1 or ai=1a_i = -1).

The sum of nn over all cases doesn't exceed 10510^5.

Output

For each test case, print the maximum possible sum of the array aa you can get in a separate line.

Note

In the first case, we can choose index 44 and flip the signs of a4a_4 and a5a_5. After this operation, the sum will be 1+1+1+1+1=3-1+1+1+1+1 = 3. We can't make the sum larger than this.

In the third case, the only option is to choose the index 11.

Samples

4
5
-1 1 1 -1 -1
5
1 1 -1 -1 -1
2
1 1
4
1 -1 -1 1
3
3
-2
4

在线编程 IDE

建议全屏模式获得最佳体验