Nick and Array CodeForces - 1180B

Nick and Array

Nick had received an awesome array of integers a=[a1,a2,…,an]a=[a1,a2,…,an] as a gift for his 55 birthday from his mother. He was already going to explore its various properties but after unpacking he was disappointed a lot because the product a1⋅a2⋅…ana1⋅a2⋅…an of its elements seemed to him not large enough.

He was ready to throw out the array, but his mother reassured him. She told him, that array would not be spoiled after the following operation: choose any index ii (1≤i≤n1≤i≤n ) and do ai:=−ai−1ai:=−ai−1 .

For example, he can change array [3,−1,−4,1][3,−1,−4,1] to an array [−4,−1,3,1][−4,−1,3,1] after applying this operation to elements with indices i=1i=1 and i=3i=3 .

Kolya had immediately understood that sometimes it's possible to increase the product of integers of the array a lot. Now he has decided that he wants to get an array with the maximal possible product of integers using only this operation with its elements (possibly zero, one or more times, as many as he wants), it is not forbidden to do this operation several times for the same index.

Help Kolya and print the array with the maximal possible product of elements a1⋅a2⋅…ana1⋅a2⋅…an which can be received using only this operation in some order.

If there are multiple answers, print any of them.

Input

The first line contains integer nn (1≤n≤1051≤n≤105 ) — number of integers in the array.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (−106≤ai≤106−106≤ai≤106 ) — elements of the array

Output

Print nn numbers — elements of the array with the maximal possible product of elements which can be received using only this operation in some order from the given array.

If there are multiple answers, print any of them.

Examples

Input

4
2 2 2 2

Output

-3 -3 -3 -3 

Input

1
0

Output

0 

Input

3
-3 -3 2

Output

-3 -3 2 

题意:给定一串序列,每个数可以选择进行ai:=−ai−1的运算,求可以得到最大乘积的序列。

思路:该变化会改变符号和绝对值的大小,正数变成负数的时候绝对值会变大,因此要想乘积最大,就使序列的绝对值尽可能地达到最大,把所有数都变成负数,如果n是偶数,那么序列就会有偶数个负数,所达到的乘积是正数,如果n是奇数,那么再将一个负数转换成正数,保证其乘积的符号为正,(我wawawawawa了,因为我一直觉得应该转换那个绝对值除了-1以外最小的。。)其实应该转换绝对值最大的,因为全部转换成负数之后其乘积的绝对值达到最大,当将其中一个数(绝对值为m)转换成正数的时候,其乘积的变化为(m-1)/m。当m最大时保证其达到的乘积最大。

#include<iostream>

using namespace std;

int main(){
	int n,j;
	int a[100010];
	cin>>n;
	int minn=0;
	for(int i=0;i<n;i++){
		cin>>a[i];
		if(a[i]>=0) a[i]=-1*a[i]-1;
		
		if(a[i]<minn){
			minn=a[i];
			j=i;
		}
		
	}
	if(n%2!=0){
		a[j]=-1*a[j]-1;
	}
	cout<<a[0];
	for(int i=1;i<n;i++){
		cout<<" "<<a[i];
	}
	cout<<endl;
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值