CF1206B.Make Product Equal One
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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?
输入格式
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 a single number — the minimal number of coins you need to pay to make the product equal to 1 .
输入输出样例
输入#1
2 -1 1
输出#1
2
输入#2
4 0 0 0 0
输出#2
4
输入#3
5 -5 -3 5 3 0
输出#3
13
说明/提示
In the first example, you can change 1 to −1 or −1 to 1 in 2 coins.
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.