Make Product Equal One--Codeforce

 

                               Make Product Equal One

                                                           time limit per test   1 second

                                                           memory limit per test 256 megabytes

input

       standard input

output

        standard output

You are given n numbers a1,a2,…,an. With a cost of one coin you can perform the following operation:

Choose one of these numbers and add or subtract 1 from it.

In particular, we can apply this operation to the same number several times.

We want to make the product of all these numbers equal to 1, in other words, we want a1⋅a2 … ⋅an=1.

For example, for n=3

and numbers [1,−3,0] we can make product equal to 1 in 3 coins: add 1 to second element, add 1 to second element again, subtract 1 from third element, so that array becomes [1,−1,−1]. And 1⋅(−1)⋅(−1)=1.

What is the minimum cost we will have to pay to do that?

Input

The first line contains a single integer n(1≤n≤105) — the number of numbers.

The second line contains n integers a1,a2,…,an (−109≤ai≤109) — the numbers.

Output

Output a single number — the minimal number of coins you need to pay to make the product equal to 1.

Examples

Input

2
-1 1

Output

2

Input

4
0 0 0 0

Output

4

Input

5
-5 -3 5 3 0

Output

13

Note

In the first example, you can change 1 to −1 or −1 to 1 in 2coins.

In the second example, you have to apply at least 4 operations for the product not to be 0.

In the third example, you can change −5 to −1 in 4 coins, −3 to −1 in 2 coins, 5 to 1 in 4 coins, 3 to 1 in 2 coins, 0 to 1 in 1

coin.

 

解题说明:此题在数组数值变化次数最少的情况下保证题目乘积为1。

思路:

我们首先把数组的数值根据其正负变为 -1 和1 ,然后统计0,1,-1 的个数,并统计结果(ans)【统计把数据变为-1 或1 的消耗】

 

因为题目要求是数值的数值乘积为1,故

如果 -1 的个数为偶数时,只需要把0变成1 =》0的个数=》 ans+=zero(零的个数)

如果 -1 的个数为奇数时,思路时把-1 的个数变为偶数,然后按照偶数的情况计算即可,变化的情况有2种

1>原数组中有0  (0  => -1)   zero(零的个数);ans+=zero;

2>原数组中无0  (-1 => 1) ans+=2

故ans为最后的结果,时间效率为O(n)

代码如下

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+50;
ll n,zs,fs,zero,ans,t;
int main()
{
   cin>>n;
   ll ans=0;
   for(ll i=0;i<n;i++)
   {
      cin>>t;
      if(t==0) zero++;
      else if(t>=1){
        ans+=t-1;    zs++; //正数
      }
      else if(t<=-1){
        ans-=t+1;    fs++; //负数
      }
   }
   if(fs%2==0)
    ans+=zero;
   else{
    if(zero){ 
            ans+=1; zero--; ans+=zero;
    }
    else
        ans+=2;
   }
   cout<<ans<<endl;
    return 0;
}



 

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章