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 

题意:给你一个数组,里面的数能进行x=-x-1操作。这个数组里的任意数字能进行任意次这样的操作,问这个数组变化后里面n个数字的乘积最大是多少。

思路:x=-x-1这个操作是以2为周期的无论进行多少次操作,只可能为一个确定的负数或者非负数,而非负数的绝对值更大一些。为了方便先把所有数都变成非负数。(这很重要)然后,当n为偶数时,直接全变成负数输出。当n为奇数时,找出最大数。最大数输出为正数,其余数输出为负数。因为对n个数的乘积而言,最大数加减一个数相对其他数而言,对结果影响是最小的。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <vector>
#include <queue>
#include <map>
#include <string>

using namespace std;

typedef long long ll;
const int maxn=1e5+7;
const ll mod= 998244353;

int a[maxn];

int main()
{
//	freopen("input.txt","r",stdin);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	
	int n;
	cin>>n;
	int Max=-1e7+7;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		if(a[i]<0) a[i]=(-1)*a[i]-1;	
		Max=max(Max,a[i]);
	} 
	int flag=0;//控制输出 
	int flag1=0;//找到第一个最大值 
	if(n%2==0)
	{
		for(int i=0;i<n;i++)
		{
			if(flag) cout<<" ";
			flag=1;
			cout<<(-1)*a[i]-1;
		}
	}
	else
	{
		for(int i=0;i<n;i++)
		{
			if(a[i]==Max&&!flag1) 
			{
				if(flag) cout<<" ";
				flag=1;
				flag1=1;
				cout<<a[i];
			}
			else 
			{
				if(flag) cout<<" ";
				flag=1;
				cout<<(-1)*a[i]-1;
			}
		}
	}
	
    return 0;
}

为了我们爱的人,和爱我们的人,无论何时,我们都要乐观向上、自信努力。

原创文章 99 获赞 15 访问量 7333

猜你喜欢

转载自blog.csdn.net/qq_45328552/article/details/103246101
今日推荐