Codeforces Round #508 (Div. 2) D. Slime【思维】

D. Slime

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it.

Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists).

When a slime with a value xx eats a slime with a value yy, the eaten slime disappears, and the value of the remaining slime changes to x−yx−y.

The slimes will eat each other until there is only one slime left.

Find the maximum possible value of the last slime.

Input

The first line of the input contains an integer nn (1≤n≤5000001≤n≤500000) denoting the number of slimes.

The next line contains nn integers aiai (−109≤ai≤109−109≤ai≤109), where aiai is the value of ii-th slime.

Output

Print an only integer — the maximum possible value of the last slime.

Examples

input

Copy

4
2 1 2 1

output

Copy

4

input

Copy

5
0 -1 -1 -1 -1

output

Copy

4

Note

In the first example, a possible way of getting the last slime with value 44 is:

  • Second slime eats the third slime, the row now contains slimes 2,−1,12,−1,1
  • Second slime eats the third slime, the row now contains slimes 2,−22,−2
  • First slime eats the second slime, the row now contains 44

In the second example, the first slime can keep eating slimes to its right to end up with a value of 44.

 

题意: 给你一行整数数,然后每一个数x都可以吃掉它与他相邻的数y,被吃掉的数y会消失,之前的那个数x则会变成x-y,这行数最后只能剩下一个数,问这个数最后最大是多少?

题解:如果给出的n个数有正有负,则结果为所有数的绝对值;如果给出的n个数有0存在,那么结果仍是所有数的绝对值;如果全为负数或全为正数,则只需要选择两个对整体结果影响最小的数就可以了。还要特别注意一下n==1的情况。

如果x>0,y<0,那么最佳值为|x|+|y|;如果x>0,y>0,那么最佳值为x-y或y-x。由此可知异号之间的数在进行计算时,不会有值上的损耗。一个数与0之间的计算也不会有值上的损耗;只有同号之间才会有损耗,同号之间计算出一个负数,与之相反号的数,这时,整个数列又变成了异号纯在的数列,也就是值确定了,也就说只需选出最佳的两个数变换即可。

 

代码如下:

#include
#include
#include
#include
using namespace std;
typedef long long ll;
ll a[500005];

int main()
{
	ll n;
	scanf("%lld",&n);
	ll sum=0;
	int flag0=0,flag1=0,flag2=0;
	for(ll i=0;i0){
			flag1=1;
		}else{
			flag2=1;
		}
	}
	if(n==1){
		printf("%lld\n",a[0]);
	}else if(flag0||flag1&&flag2){
		printf("%lld\n",sum);
	}else{
	
		ll mi=min(abs(a[0])-abs(a[1]),abs(a[1])-abs(a[0]));
		ll mik=sum-abs(a[0])-abs(a[1])-mi;
		
		for(ll i=2;imik){
				
				mik=sum-min(abs(a[i])-abs(a[i-1]),abs(a[i-1])-abs(a[i]))-abs(a[i])-abs(a[i-1]);
			}
		}
		
		printf("%lld\n",mik);
		
	}
	
	
	
	return 0;
 } 

 

 

你可能感兴趣的:(#,思维)