E. Negatives and Positives

time limit per test

2 seconds

memory limit per test

256 megabytes

Given an array aa consisting of nn elements, find the maximum possible sum the array can have after performing the following operation any number of times:

  • Choose 22 adjacent elements and flip both of their signs. In other words choose an index ii such that 1≤i≤n−11≤i≤n−1 and assign ai=−aiai=−ai and ai+1=−ai+1ai+1=−ai+1.

Input

The input consists of multiple test cases. The first line contains an integer tt (1≤t≤10001≤t≤1000) --- the number of test cases. The descriptions of the test cases follow.

The first line of each test case contains an integer nn (2≤n≤2⋅1052≤n≤2⋅105) --- the length of the array.

The following line contains nn space-separated integers a1,a2,...,ana1,a2,...,an (−109≤ai≤109−109≤ai≤109).

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, output the maximum possible sum the array can have after performing the described operation any number of times.

Example

Input

Copy

复制代码

5

3

-1 -1 -1

5

1 5 -5 0 2

3

1 2 3

6

-1 10 9 8 7 6

2

-1 -1

Output

Copy

复制代码
1
13
6
39
2

Note

For the first test case, by performing the operation on the first two elements, we can change the array from −1,−1,−1−1,−1,−1 to 1,1,−11,1,−1, and it can be proven this array obtains the maximum possible sum which is 1+1+(−1)=11+1+(−1)=1.

For the second test case, by performing the operation on −5−5 and 00, we change the array from 1,5,−5,0,21,5,−5,0,2 to 1,5,−(−5),−0,2=1,5,5,0,21,5,−(−5),−0,2=1,5,5,0,2, which has the maximum sum since all elements are non-negative. So, the answer is 1+5+5+0+2=131+5+5+0+2=13.

For the third test case, the array already contains only positive numbers, so performing operations is unnecessary. The answer is just the sum of the whole array, which is 1+2+3=61+2+3=6.

解题说明:此题是一道数学题,找规律能发现可以采用贪心算法,不管执行了多少次的操作,数组中负数个数的奇偶性总是保持不变。因此如果一开始数组中有偶数个负数,那么我们总是可以通过若干次的操作使得负数的个数变成0,因此答案就是所有数的绝对值之和。如果一开始数组中有奇数个负数,那么最终至少要有一个负数,所有可以让数组中绝对值最小的那个数为负数(,那么答案就是其他数的绝对值之和再减去这个最小数的绝对值。

cpp 复制代码
#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

typedef long long LL;


int main()
{
	int t;
	int n;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d", &n);
		int cnt = 0, minv = 2e9;
		LL s = 0;
		while (n--)
		{
			int x;
			scanf("%d", &x);
			if (x < 0)
			{
				cnt++;
				x *= -1;
			}
			s += x;
			minv = min(minv, x);
		}
		if (cnt & 1)
		{
			s -= 2 * minv;
		}
		printf("%lld\n", s);
	}
	return 0;
}
相关推荐
JJJennie77723 分钟前
MAI Gateway私有化部署全攻略:大模型网关如何接入?附架构图
算法·gateway·ai网关·魔芋ai
東隅已逝,桑榆非晚30 分钟前
算法3.双指针法-快乐数
c++·笔记·学习·算法
专注仿真40 分钟前
卷积神经网络(CNN)算法详解
人工智能·python·算法·代码·大模型训练
FII工业富联科技服务44 分钟前
多智能体如何进入制造现场?从业务协同到数字孪生验证
大数据·人工智能·算法·机器人·制造·数字孪生
≮傷£≯√1 小时前
httpserver_QNetWorkClient
算法
Evand J1 小时前
【PMSM例程11】永磁同步电机(PMSM)基于UKF的转速与转子位置估计。附下载链接
算法·matlab·卡尔曼滤波·永磁同步电机·ukf·pmsm·位置估计
货拉拉技术1 小时前
记忆即智能:重构对话式AI Agent认知基座的四层渐进式记忆与自进化系统
人工智能·算法
杜 硕1 小时前
从零开始的C++生活——第六讲——02
c++·算法·生活
小超人诞生日记1 小时前
C++ STL 反向迭代器:从源码框架到适配器实现
c++·算法
青少儿编程课堂2 小时前
蓝桥杯青少组图形化 + 全国青少年信息素养大赛 + 电子学会Scratch考级:校园成语接龙大挑战解析
c++·python·算法·bfs·信息学竞赛