CodeForces 301A Yaroslav and Sequence(数字变号的最大和)

A. Yaroslav and Sequence(原题目)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Yaroslav has an array, consisting of (2·n - 1) integers. In a single operation Yaroslav can change the sign of exactly n elements in the array. In other words, in one operation Yaroslav can select exactly n array elements, and multiply each of them by -1.

Yaroslav is now wondering: what maximum sum of array elements can be obtained if it is allowed to perform any number of described operations?

Help Yaroslav.

Input

The first line contains an integer n (2 ≤ n ≤ 100). The second line contains (2·n - 1) integers — the array elements. The array elements do not exceed 1000 in their absolute value.

Output

In a single line print the answer to the problem — the maximum sum that Yaroslav can get.

Examples
input
2
50 50 50
output
150
input
2
-1 -100 -1
output
100
Note

In the first sample you do not need to change anything. The sum of elements equals 150.

In the second sample you need to change the sign of the first two elements. Then we get the sum of the elements equal to 100.



题意:

有2 * n - 1个整数,每次可以将其中 n 个数变号,操作次数不限,问最后得到的最大和。


思路:
真的没什么思路,也考虑到了正负数的个数,但是总错,还是看了宇神的参考题解。

看注释部分。


代码:

#include<stdio.h>
const int MYDD=1103;

int main() {
	int n;
	while(scanf("%d",&n)!=EOF) {
		int numpositive=0;//正数个数
		int numminus=0;//负数个数
		int min=1e9+1103;//记录绝对值最小的数字
		int sum=0;//所有数字的和
		int m=2*n-1;
		while(m--) {
			int a;
			scanf("%d",&a);
			if(a<0)		a=-a,numminus++;
			else		numpositive++;
			sum+=a;
			if(a<min)	min=a;
		}
		if(n%2) {// n为奇数
			printf("%d\n",sum);
//当n为奇数的时候,变换n个可以一次增加或减少1个负数,这样可以直到有n个负数,最后化为正数 
		} else {
//当n为偶数的时候,变换n个可以一次增加或减少2个负数
			if(numminus%2)//奇数个负数
				printf("%d\n",sum-2*min);
				
			else
				printf("%d\n",sum);
		}
	}
	return 0;
}

后:

*************************

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值