I - Array Negations Gym - 102152I

You are given an array aa of nn integers, and an integer kk. You have to make kknegation operations such that at each operation you need to choose an element aiaifrom the array and replace it with −ai−ai.

Your task is to find the optimal way to make the kk negation operations such that at the end the sum of the array aa is as maximal as possible. Can you?

Input

The first line contains an integer TT (1≤T≤1001≤T≤100) specifying the number of test cases.

The first line of each test case contains two integers nn and kk (1≤n≤1041≤n≤104, 0≤k≤1040≤k≤104), in which nn is the size of the array, and kk is the number of negation operations to be made.

Then a line follows contains nn integers a1,⋯,ana1,⋯,an (−100≤ai≤100−100≤ai≤100), giving the array aa.

Output

For each test case, print a single line containing the maximum sum of array aa after making the required number of negation operations.

Example

Input

3
3 1
4 6 2
4 2
-1 0 2 1
5 2
1 7 -4 2 -3

Output

8
4
17

Note

In the first test case, the optimal way is to make the negation operation on a3a3. After this, the array will be = [4,6,−2][4,6,−2], and its sum is 88.

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int a[10010];
int main()
{
	int T;
	scanf("%d",&T); 
	while(T--)
	{
		int n,k;
		scanf("%d%d",&n,&k);
		memset(a,0,sizeof(a));
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}
		sort(a,a+n);
		int j=0;
		while(a[j]<0&&j<n)
		{
			j++;
			
		}
		//printf("**%d**\n",j); 
		int sum=0;
		if(j>=k)
		{
			for(int i=0;i<n;i++)
			{
				if(i<k) sum-=a[i];//把k写成了j,没长脑子吧。。。。 
				else sum+=a[i] ;
			}
		}
		else
		{
			for(int i=0;i<j;i++)
			{
				a[i]=-a[i];
			}
			int w=k-j;
			sort(a,a+n);
			for(int i=1;i<n;i++) sum+=a[i];
			if(w%2==0) sum+=a[0];
			else
			{
				sum-=a[0];
			}
		}
		printf("%d\n",sum);
	}
	return 0;
 } 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值