306494: CF1206B. Make Product Equal One
Memory Limit:256 MB
Time Limit:1 S
Judge Style:Text Compare
Creator:
Submit:0
Solved:0
Description
Make Product Equal One
题意翻译
## 题目描述 给你一个有 $n$ 个数的数组。你可以用 $x$(x为任意正整数) 的代价将数组中的任意一个数增加或减少 $x$ ,你可以重复多次此操作。现在需要你用若干次操作使得 $a_1·a_2·...·a_n = 1$ (数组的乘积为1)。 比如,当 $n=3$ 和数组为 [**1,-3,0**] 时,我们最少需要花费 $3$ 的代价:用 $2$ 的代价把 -$3$ 增加到 -$1$ ,再用 $1$ 的代价把 $0$ 减少到 -$1$ ,数组就变成了 [**1,-1,-1**] ,然后 $1·(-1)·(-1)=1$ 。 现在询问最少需要花费多少的代价使得数组的乘积为 $1$ 。 ## 输入格式 输入共两行。 第一行输入一个数 $n$ ,表示数组的数字个数。 第二行输入 $n$ 个数 $a_i$ ,表示该数组。 ## 输出格式 输出一个数,表示使得数组的乘积为 $1$ 的最少的花费。 ## 数据范围 $1\leq n\leq 10^5$ $-10^9\leq a_i\leq 10^9$题目描述
You are given $ n $ numbers $ a_1, a_2, \dots, a_n $ . 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 $ a_1 \cdot a_2 $ $ \dots $ $ \cdot a_n = 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\cdot (-1) \cdot (-1) = 1 $ . What is the minimum cost we will have to pay to do that?输入输出格式
输入格式
The first line contains a single integer $ n $ ( $ 1 \le n \le 10^5 $ ) — the number of numbers. The second line contains $ n $ integers $ a_1, a_2, \dots, a_n $ ( $ -10^9 \le a_i \le 10^9 $ ) — 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