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;
}



 

 


 

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