Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } int cnt = 0; int tot = a.at(0); int k = 1; if (a.at(0) == 0) { k = -1; for (int i = 1; i < n; i++) { if (a.at(i) != 0) { k = i; break; } } if (k == -1) { cout << 1 + 2 * (n - 1) << endl; return 0; } if (a.at(k) > 0) { tot = -1; cnt = 1 + 2 * (k - 1); } else { tot = 1; cnt = 1 + 2 * (k - 1); } } for (int i = k; i < n; i++) { int after; if (tot < 0) { after = max(1 - tot, a.at(i)); cnt += abs(after - a.at(i)); } if (tot > 0) { after = min(-1 - tot, a.at(i)); cnt += abs(after - a.at(i)); } tot += after; } cout << cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<long long> A(N); for (int i = 0; i < (N); i++) cin >> A[i]; int ans1 = 0, ans2 = 0, sum = 0; for (int i = 0; i < (N); i++) { sum += A[i]; if (i % 2 == 0 && sum <= 0) { ans1 += 1 - sum; sum = 1; } else if (i % 2 != 0 && sum >= 0) { ans1 += sum + 1; sum = -1; } } sum = 0; for (int i = 0; i < (N); i++) { sum += A[i]; if (i % 2 == 0 && sum >= 0) { ans2 += sum + 1; sum = -1; } else if (i % 2 != 0 && sum <= 0) { ans2 += 1 - sum; sum = 1; } } cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int, input().split())) counter = 0 ####操作回数 A.reverse() S = 0 a = A.pop() if a==0: counter += 1 while A: b = A.pop() if b == 0: counter += 2 elif b>0: A.append(b) S = -1 break elif b<0: A.append(b) S = 1 break else: S += a while A: c = A.pop() if c>=0 and S>0: counter += abs(c+S)+1 S = -1 elif c<=0 and S<0: counter += abs(c+S)+1 S = 1 elif (c>0 and S<0) and S+c<=0: counter += abs(S+c)+1 S = 1 elif (c<0 and S>0) and S+c>=0: counter += abs(S+c)+1 S = -1 else: S += c print(counter)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int sum1 = 0, sum2 = 0; int ans1 = 0, ans2 = 0; int first; cin >> first; if (first > 0) { sum1 = first; sum2 = -1; ans2 += first + 1; } else if (first < 0) { sum1 = -first; sum2 = first; ans1 += -first * 2; } else { sum1 = 1; sum2 = -1; ans1++, ans2++; } for (int i = 0; i < n - 1; i++) { int a; cin >> a; if (sum1 > 0) { if (sum1 + a >= 0) { ans1 += abs(-sum1 - 1 - a); sum1 = -1; } else { sum1 += a; } } else if (sum1 < 0) { if (sum1 + a <= 0) { ans1 += -sum1 + 1 - a; sum1 = 1; } else { sum1 += a; } } if (sum2 > 0) { if (sum2 + a >= 0) { ans2 += abs(-sum2 - 1 - a); sum2 = -1; } else { sum2 += a; } } else if (sum2 < 0) { if (sum2 + a <= 0) { ans2 += -sum2 + 1 - a; sum2 = 1; } else { sum2 += a; } } } cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; using static System.Console; using static System.Convert; using static System.Math; class Program { static void Main(string[] args) { var num = ToInt32(ReadLine()); var ar = Array.ConvertAll(ReadLine().Split(' '), int.Parse); var res = int.MaxValue; for (var j = 0; j < 2; j++) { var isP = j == 0; var sum = 0; var r = 0; for (var i = 0; i < num; i++) { sum += ar[i]; if (isP&&sum<=0) { r += 1 - sum; sum = 1; } else if (!isP && sum >= 0) { r += 1 + sum; sum = -1; } isP = !isP; } res = Min(res, r); } WriteLine(res); } private const string ALFA = "abcdefghijklmnopqrstuvwxyz"; private const int MOD = 1000000007; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long ans=0; long ans2=0; int[] f = new int[n]; for(int i=0;i<n;i++) f[i] = sc.nextInt(); int sum1 = f[0]; int sum2 = f[0]; for(int i=0;i<n-1;i++) { sum2 += f[i+1]; if(sum1*sum2 > 0) { ans += Math.abs(sum2)+1; if(sum1 >0) sum2 = -1; else sum2 = 1; }else if(sum2 == 0) { ans += 1; if(sum1 > 0) sum2 = -1; else sum2 = 1; } sum1 = sum2; } sum1 = f[0]; sum2 = f[0]; for(int i=0;i<n-1;i++) { sum2 += f[i+1]; if(sum1*sum2 > 0) { ans2 += Math.abs(sum2)+1; if(sum1 >0) sum2 = -1; else sum2 = 1; }else if(sum2 == 0) { ans2 += 1; if(sum1 < 0) sum2 = 1; else sum2 = -1; } sum1 = sum2; } System.out.println(Math.min(ans,ans2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
solver::[Int]->Int solver xs = if head xs == 0 then min (check_tot 1 1 (tail xs)) (check_tot 1 (-1) (tail xs)) else check_tot 0 (head xs) (tail xs) main::IO() main=do _<-getLine datc<-getLine print (solver (map read (words datc))) --おそい。Step_sumを作る事無く、シーケンシャルにいく --今のカウント手数、ここまでの修正されたトータル(これはゼロでない事が保証される)、食べるリスト。 check_tot::Int -> Int -> [Int] -> Int check_tot st _ [] = st check_tot st tot xs | (tot > 0)&&((tot+(head xs))>=0) = let dec = (tot+(head xs))+1 in check_tot (dec+st) (-1) (tail xs) | (tot > 0)&&((tot+(head xs)) <0) = check_tot st (tot+(head xs)) (tail xs) | (tot < 0)&&((tot+(head xs)) >0) = check_tot st (tot+(head xs)) (tail xs) | (tot < 0)&&((tot+(head xs))<=0) = let inc = 1-(tot+(head xs)) in check_tot (inc+st) 1 (tail xs)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; vector<bool> sign = {true, false}; vector<int> cnt = {0, 0}; long total; for (int k = 0; k < 2; ++k) { vector<long> a_tmp = a; for (int i = 0; i < n; ++i) { total = accumulate(a_tmp.begin(), a_tmp.begin() + i, 0) + a_tmp[i]; if (sign[k] && total >= 0) { a_tmp[i] -= (total + 1); cnt[k] += (total + 1); sign[k] = false; } else if (!sign[k] && total <= 0) { a_tmp[i] += (abs(total) + 1); cnt[k] += (abs(total) + 1); sign[k] = true; } else sign[k] = !sign[k]; } } cout << min(cnt[0], cnt[1]) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; int count = 0; vector<int> a(100000); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } if (a[0] == 0) { if (a[1] > 0) { a[0]--; count++; } else { a[0]++; count++; } } vector<long long int> sum(100000); sum[0] = a[0]; for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + a[i]; if (sum[i] * sum[i - 1] >= 0) { if (sum[i - 1] < 0) { count += 1 - sum[i - 1]; sum[i] = 1; } else { count += sum[i - 1] + 1; sum[i] = -1; } } } cout << count; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args)throws Exception { Scanner stdIn=new Scanner(System.in); int N=stdIn.nextInt(); int a[]=new int[N]; int pla=0,key=0,cun=0; int z=0; while(z<N) { a[z]=stdIn.nextInt(); key+=a[z]; if(a[0]<0) pla=1; if(pla==0) { if(z%2==0) { if(key<0) { cun+=0-key+1; key+=0-key+1; } if(key==0) { cun+=1; key+=1; } } else { if(key>0) { cun+=key+1; key-=key+1; } if(key==0) { cun+=1; key-=1; } } } else { if(z%2==1) { if(key<0) { cun+=0-key+1; key+=0-key+1; } if(key==0) { cun+=1; key+=1; } } else { if(key>0) { cun+=key+1; key-=key+1; } if(key==0) { cun+=1; key-=1; } } } z++; } System.out.println(cun); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) lstA = list(map(int, input().split())) cntCase1, cntCase2 = 0, 0 # 偶数番目を正、奇数番目を負とする場合 accum = 0 for i in range(n): ai = lstA[i] tmpSum = accum tmpSum += ai if i % 2 == 0: if tmpSum < 0: cntCase1 += abs(tmpSum) + 1 accum = 1 else: accum = tmpSum else: if tmpSum > 0: cntCase1 += abs(tmpSum) + 1 accum = -1 else: accum = tmpSum # 偶数番目を負、奇数番目を正とする場合 accum = 0 for i in range(n): ai = lstA[i] tmpSum = accum tmpSum += ai if i % 2 == 0: if tmpSum > 0: cntCase2 += abs(tmpSum) + 1 accum = -1 else: accum = tmpSum else: if tmpSum < 0: cntCase2 += abs(tmpSum) + 1 accum = 1 else: accum = tmpSum ans = min(cntCase1, cntCase2) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) rui = [0] * n cnt = 0 if a[0] > a[1]: rui[0] = a[0] else: if a[0] >= 0: rui[0] = -1 cnt += a[0] + 1 else: rui[0] = 1 cnt += abs(a[0]) + 1 for i in range(1, n): tmp = rui[i - 1] + a[i] if tmp == 0: if rui[i - 1] > 0: rui[i] = tmp - 1 cnt += 1 else: rui[i] = tmp + 1 cnt += 1 else: if rui[i - 1] > 0 and tmp > 0: rui[i] = -1 cnt += abs(tmp) + 1 elif rui[i - 1] < 0 and tmp < 0: rui[i] = 1 cnt += abs(tmp) + 1 else: rui[i] = tmp print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
package main import ( "bufio" "fmt" "os" "strconv" ) var sc = bufio.NewScanner(os.Stdin) func Scanner() string { sc.Scan() return sc.Text() } func main() { buf := make([]byte, 0) sc.Buffer(buf, 1000000009) sc.Split(bufio.ScanWords) n, _ := strconv.Atoi(Scanner()) a := make([]int, n) for i := 0; i < n; i++ { a[i], _ = strconv.Atoi(Scanner()) } s := a[0] ans := 0 if s == 0 && a[1] > 0 { s = -1 ans += 1 } else if s == 0 && a[1] < 0 { s = 1 ans += 1 } for i := 1; i < n; i++ { t := s + a[i] if s < 0 && t < 0 { ans += 1 - t s = 1 } else if s > 0 && t > 0 { ans += t + 1 s = -1 } else if s < 0 && t == 0 { ans += 1 s = 1 } else if s > 0 && t == 0 { ans += 1 s = -1 } else { s = t } } fmt.Println(ans) }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } int sum = a.at(0); int count_eve = 0; int count_odd = 0; if (sum <= 0) { count_eve += -sum + 1; sum = 1; } for (int i = 0; i < n - 1; i++) { if (i % 2 == 0) { if (sum + a.at(i + 1) < 0) { sum += a.at(i + 1); } else { count_eve += sum + a.at(i + 1) + 1; sum = -1; } } else if (i % 2 == 1) { if (sum + a.at(i + 1) > 0) { sum += a.at(i + 1); } else { count_eve += -sum - a.at(i + 1) + 1; sum = 1; } } } sum = a.at(0); if (sum >= 0) { count_odd += sum + 1; sum = -1; } for (int i = 0; i < n - 1; i++) { if (i % 2 == 0) { if (sum + a.at(i + 1) > 0) { sum += a.at(i + 1); } else { count_odd += -sum - a.at(i + 1) + 1; sum = 1; } } else if (i % 2 == 1) { if (sum + a.at(i + 1) < 0) { sum += a.at(i + 1); } else { count_odd += sum + a.at(i + 1) + 1; sum = -1; } } } cout << min(count_eve, count_odd); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) num = a[0] ans = 0 if a[0]>0: for i in range(1,n): num += a[i] if i%2==1: if num>=0: ans += num+1 num = -1 else: if num<=0: ans += 1-num num = 1 elif a[0]<0: for i in range(1,n): num += a[i] if i%2==1: if num <= 0: ans += 1-num num = 1 else: if num >= 0: ans += num+1 num = -1 else: num = 0 ansp = 1 ansm = 1 for i in range(1,n): num += a[i] if i%2==1: if num>=0: ansp += num+1 num = -1 else: if num<=0: ansp += 1-num num = 1 for i in range(1,n): num += a[i] if i%2==1: if num <= 0: ansm += 1-num num = 1 else: if num >= 0: ansm += num+1 num = -1 ans = min(ansp,ansm) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) al = list(map(int, input().split())) m = n//2 mm = n % 2 temp = al[0] res = 0 def ddd(temp,al,m,mm,res): if temp > 0 and mm ==1: for i in range(1,m+1): temp +=al[i*2-1] if temp <0: pass else: res += temp+1 temp = -1 temp +=al[i*2] if temp >0: pass else: res +=1-temp temp = 1 print(res) exit() if temp > 0 and mm ==0: for i in range(1,m): temp +=al[i*2-1] if temp <0: pass else: res += temp+1 temp = -1 temp +=al[i*2] if temp >0: pass else: res +=1-temp temp = 1 temp += al[n-1] if temp <0: pass else: res += temp+1 temp = -1 print(res) exit() if temp < 0 and mm ==1: for i in range(1,m+1): temp +=al[i*2-1] if temp >0: pass else: res += 1-temp temp = 1 temp +=al[i*2] if temp <0: pass else: res +=temp+1 temp = -1 print(res) exit() if temp < 0 and mm ==0: for i in range(1,m): temp +=al[i*2-1] if temp >0: pass else: res += 1-temp temp = 1 temp +=al[i*2] if temp <0: pass else: res +=temp+1 temp = -1 temp += al[n-1] if temp >0: pass else: res += 1-temp temp = 1 print(res) exit() ddd(temp,al,m,mm,res)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
import qualified Data.Vector.Unboxed as VU import qualified Data.ByteString.Char8 as B import Data.Char solve :: VU.Vector Int -> Int -> Int solve vec n | VU.length vec == 2 && VU.sum vec == 0 = 1 | VU.length vec == 2 && VU.sum vec /= 0 = 0 | otherwise = fst $ minimum $ [f, g] where t = VU.take 2 vec d = VU.drop 2 vec f = VU.foldl' step (init t (negate 1)) d g = VU.foldl' step (init t 1) d init :: VU.Vector Int -> Int -> (Int, Int) init vec i | a + b == 0 = (1, a + b + i) | otherwise = (0, a + b) where a = VU.head vec b = VU.last vec step :: (Int, Int) -> Int -> (Int, Int) step (res, acc) x | acc + x == 0 = (res + 1, negate (signum acc)) | (signum acc) /= signum (acc + x) = (res, acc + x) | otherwise = let aim = negate $ signum acc y = aim - (acc + x) in (res + abs y, aim) main = do n <- readLn :: IO Int as <- VU.unfoldrN n (B.readInt . B.dropWhile isSpace) <$> B.getLine print $ solve as n
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.*; public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static void solve() { int n = ni(); int[] a = na(n); long[] sum = new long[n]; sum[0] = a[0]; long total = 0; for (int i = 1; i < n; i++) { int cur = a[i]; if (sum[i - 1] < 0) { if (cur + sum[i - 1] > 0) { sum[i] = cur + sum[i - 1]; } else { total += 1 - (cur + sum[i - 1]); sum[i] = 1; } } else { if (cur + sum[i - 1] >= 0) { total += (cur + sum[i - 1]) + 1; sum[i] = -1; } else { sum[i] = cur + sum[i - 1]; } } } System.out.println(total); } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); long G = System.currentTimeMillis(); tr(G-S+"ms"); } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double nd() { return Double.parseDouble(ns()); } private static char nc() { return (char)skip(); } private static String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private static int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private static int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
n = gets.to_i as = gets.chomp.split.map(&:to_i) ans_o = ans_e = 0 sum = as[0] 1.upto(n-1) do |i| sum += as[i] if i.even? until sum > 0 ans_e += 1 sum += 1 end else until sum < 0 ans_e += 1 sum -= 1 end end end 1.upto(n-1) do |i| sum += as[i] if i.odd? until sum > 0 ans_o += 1 sum += 1 end else until sum < 0 ans_o += 1 sum -= 1 end end end puts [ans_e,ans_o].min
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# coding: utf-8 # Here your code N = int(input()) a = [int(i) for i in input().split()] result_1 = 0 before_sum =a[0] after_sum =a[0] for i in range(1,N): before_sum = after_sum after_sum = before_sum + a[i] if before_sum * after_sum > 0: if after_sum < 0: result_1 += 1 - after_sum after_sum = 1 elif after_sum > 0: result_1 += 1 + after_sum after_sum = -1 elif before_sum * after_sum == 0: result_1 += 1 if before_sum < 0: after_sum = 1 else: after_sum = -1 before_sum =-a[0] after_sum =-a[0] result_2 = 1 + abs(before_sum) for i in range(1,N): before_sum = after_sum after_sum = before_sum + a[i] if before_sum * after_sum > 0: if after_sum < 0: result_2 += 1 - after_sum after_sum = 1 elif after_sum > 0: result_2 += 1 + after_sum after_sum = -1 elif before_sum * after_sum == 0: result_2 += 1 if before_sum < 0: after_sum = 1 else: after_sum = -1 print(min(result_1,result_2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using vvvll = vector<vvll>; using vb = vector<bool>; using vvb = vector<vb>; using mii = map<int, int>; using pqls = priority_queue<long long>; using pqlg = priority_queue<long long, vector<long long>, greater<long long>>; using mll = map<long long, long long>; using pll = pair<long long, long long>; using sll = set<long long>; long long divup(long long a, long long b); long long kaijou(long long i); long long P(long long n, long long k); long long C(long long n, long long k); long long GCD(long long a, long long b); long long LCM(long long a, long long b); bool prime(long long N); double distance(vector<long long> p, vector<long long> q, long long n); void press(vector<long long> &v); void ranking(vector<long long> &v); void erase(vector<long long> &v, long long i); void unique(vector<long long> &v); void printv(vector<long long> v); vector<ll> keta(ll x); long long modpow(long long a, long long n, long long mod); long long modinv(long long a, long long mod); vector<long long> inputv(long long n); vector<long long> yakusuu(int n); map<long long, long long> soinsuu(long long n); vector<vector<long long>> maze(long long i, long long j, vector<string> &s); vector<long long> eratos(long long n); set<long long> eraset(long long n); long long divup(long long a, long long b) { long long x = abs(a); long long y = abs(b); long long z = (x + y - 1) / y; if ((a < 0 && b > 0) || (a > 0 && b < 0)) return -z; else if (a == 0) return 0; else return z; } long long kaijou(long long i) { if (i == 0) return 1; long long j = 1; for (long long k = 1; k <= i; k++) { j *= k; } return j; } long long P(long long n, long long k) { if (n < k) return 0; long long y = 1; for (long long i = 0; i < k; i++) { y *= (n - i); } return y; } long long C(long long n, long long k) { if (n < k) return 0; return P(n, k) / kaijou(k); } long long GCD(long long a, long long b) { if (a < b) swap(a, b); long long d = a % b; if (d == 0) { return b; } return GCD(b, d); } long long LCM(long long a, long long b) { return (a / GCD(a, b)) * b; } bool prime(long long N) { if (N == 1) { return false; } if (N < 0) return false; long long p = sqrt(N); for (long long i = 2; i <= p; i++) { if (N % i == 0) { return false; } } return true; } double distance(vector<long long> p, vector<long long> q, long long n) { double x = 0; for (long long i = 0; i < n; i++) { x += pow((p.at(i) - q.at(i)), 2); } return sqrt(x); } void press(vector<long long> &v) { long long n = v.size(); vector<long long> w(n); map<long long, long long> m; for (auto &p : v) { m[p] = 0; } long long i = 0; for (auto &p : m) { p.second = i; i++; } for (long long i = 0; i < n; i++) { w.at(i) = m[v.at(i)]; } v = w; return; } void ranking(vector<long long> &v) { long long n = v.size(); map<long long, long long> m; long long i; for (i = 0; i < n; i++) { m[v.at(i)] = i; } vector<long long> w(n); i = 0; for (auto &p : m) { v.at(i) = p.second; i++; } return; } void erase(vector<long long> &v, long long i) { long long n = v.size(); if (i > n - 1) return; for (long long j = i; j < n - 1; j++) { v.at(j) = v.at(j + 1); } v.pop_back(); return; } void unique(vector<long long> &v) { long long n = v.size(); set<long long> s; long long i = 0; while (i < n) { if (s.count(v.at(i))) { erase(v, i); n--; } else { s.insert(v.at(i)); i++; } } return; } void printv(vector<long long> v) { cout << "{ "; for (auto &p : v) { cout << p << ","; } cout << "}" << endl; } vector<ll> keta(ll x) { if (x == 0) return {0}; ll n = log10(x) + 1; vll w(n, 0); for (ll i = 0; i < n; i++) { ll p; p = x % 10; x = x / 10; w[n - 1 - i] = p; } return w; } long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); } vector<long long> inputv(long long n) { vector<long long> v(n); for (long long i = 0; i < n; i++) { cin >> v[i]; } return v; } vector<long long> yakusuu(long long n) { vector<long long> ret; for (long long i = 1; i <= sqrt(n); ++i) { if (n % i == 0) { ret.push_back(i); if (i * i != n) { ret.push_back(n / i); } } } sort(ret.begin(), ret.end()); return ret; } map<long long, long long> soinsuu(long long n) { map<long long, long long> m; long long p = sqrt(n); while (n % 2 == 0) { n /= 2; if (m.count(2)) { m[2]++; } else { m[2] = 1; } } for (long long i = 3; i * i <= n; i += 2) { while (n % i == 0) { n /= i; if (m.count(i)) { m[i]++; } else { m[i] = 1; } } } if (n != 1) m[n] = 1; return m; } vector<vector<long long>> maze(ll i, ll j, vector<string> &s) { ll h = s.size(); ll w = s[0].size(); queue<vector<long long>> q; vector<vector<long long>> dis(h, vll(w, -1)); q.push({i, j}); dis[i][j] = 0; while (!q.empty()) { auto v = q.front(); q.pop(); if (v[0] > 0 && s[v[0] - 1][v[1]] == '.' && dis[v[0] - 1][v[1]] == -1) { dis[v[0] - 1][v[1]] = dis[v[0]][v[1]] + 1; q.push({v[0] - 1, v[1]}); } if (v[1] > 0 && s[v[0]][v[1] - 1] == '.' && dis[v[0]][v[1] - 1] == -1) { dis[v[0]][v[1] - 1] = dis[v[0]][v[1]] + 1; q.push({v[0], v[1] - 1}); } if (v[0] < h - 1 && s[v[0] + 1][v[1]] == '.' && dis[v[0] + 1][v[1]] == -1) { dis[v[0] + 1][v[1]] = dis[v[0]][v[1]] + 1; q.push({v[0] + 1, v[1]}); } if (v[1] < w - 1 && s[v[0]][v[1] + 1] == '.' && dis[v[0]][v[1] + 1] == -1) { dis[v[0]][v[1] + 1] = dis[v[0]][v[1]] + 1; q.push({v[0], v[1] + 1}); } } return dis; } long long modC(long long n, long long k, long long mod) { if (n < k) return 0; long long p = 1, q = 1; for (long long i = 0; i < k; i++) { p = p * (n - i) % mod; q = q * (i + 1) % mod; } return p * modinv(q, mod) % mod; } long long POW(long long a, long long n) { long long res = 1; while (n > 0) { if (n & 1) res = res * a; a = a * a; n >>= 1; } return res; } vector<long long> eratos(long long n) { if (n < 2) return {}; vll v(n - 1); for (long long i = 0; i < n - 1; i++) { v[i] = i + 2; } ll i = 0; while (i < n - 1) { ll p = v[i]; for (ll j = i + 1; j < n - 1; j++) { if (v[j] % p == 0) { v.erase(v.begin() + j); n--; } } i++; } v.resize(n - 1); return v; } set<long long> eraset(long long n) { set<long long> s; vll v = eratos(n); for (auto &t : v) { s.insert(t); } return s; } vll line(ll x1, ll y1, ll x2, ll y2) { vector<ll> v(3); v[0] = y1 - y2; v[1] = x2 - x1; v[2] = -x1 * (y1 - y2) + y1 * (x1 - x2); return v; } double dis(vll v, ll x, ll y) { double s = sqrt(v[0] * v[0] + v[1] * v[1]); return (double)abs(v[0] * x + v[1] * y + v[2]) / s; } ll const mod = 1e9 + 7; int main() { ll n; cin >> n; auto a = inputv(n); ll l = 0; ll res = 0; for (long long i = 0; i < n; i++) { if (l == 0) { if (a[0] == 0) { for (long long j = 0; j < n; j++) { if (a[j] != 0) { a[0] = a[j] / abs(a[j]); if (j % 1) a[0] *= (-1); res++; break; } } } if (!a[0]) { a[0] = 1; res++; } l += a[0]; } else if (l < 0) { if (a[i] < -l + 1) { res += -l + 1 - a[i]; a[i] = -l + 1; l = 1; } else { l += a[i]; } } else if (l > 0) { if (a[i] > -l - 1) { res += abs(a[i] - (-l - 1)); a[i] = -l - 1; l = -1; } else { l += a[i]; } } } cout << res << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n; cin >> n; vector<int> a(n); for (int i = (0); i < (n); i++) cin >> a[i]; int ans = 0; int sum_prev = a[0]; if (sum_prev == 0) { if (a[1] >= 0) sum_prev = -1; else sum_prev = 1; ans = 1; } for (int i = (1); i < (n); i++) { int sum_now = sum_prev + a[i]; if (sum_prev * sum_now >= 0) { ans += abs(sum_now) + 1; if (sum_prev > 0) sum_now = -1; else sum_now = 1; } sum_prev = sum_now; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; bool isplus(long long a) { return (a > 0) ? true : false; } int main() { int n; cin >> n; long long a[n]; long long sum = 0; long long ans = 0; long long finalans = 0; bool is_plus = true; for (int i = 0; i < n; i++) { cin >> a[i]; } sum = a[0]; is_plus = isplus(sum); for (int i = 1; i < n; i++) { sum += a[i]; if (sum == 0) { if (is_plus) { sum--; ans++; } else { sum++; ans++; } } if (is_plus) { if (isplus(sum)) { ans += sum + 1; sum -= sum + 1; } } else { if (!isplus(sum)) { ans += -sum + 1; sum += -sum + 1; } } is_plus = !is_plus; } finalans = ans; ans = 0; sum = 0; is_plus = isplus(sum); for (int i = 0; i < n; i++) { sum += a[i]; if (sum == 0) { if (is_plus) { sum--; ans++; } else { sum++; ans++; } } if (is_plus) { if (isplus(sum)) { ans += sum + 1; sum -= sum + 1; } } else { if (!isplus(sum)) { ans += -sum + 1; sum += -sum + 1; } } is_plus = !is_plus; } finalans = min(ans, finalans); cout << finalans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long a[100000]; int n; void solve() { long long sum0 = a[0]; long long sum1; long long ans1 = 0; long long ans2 = 0; if (a[0] == 0) { ans1++; sum0 = 1; } for (int i = 1; i < n; i++) { sum1 = sum0 + a[i]; if (sum1 * sum0 < 0) { } else if (sum1 * sum0 > 0) { ans1 += abs(sum1) + 1; sum1 = -1 * sum0 / abs(sum0); } else { ans1++; sum1 = -1 * sum0 / abs(sum0); } sum0 = sum1; } if (a[0] == 0) { ans2++; sum0 = -1; } for (int i = 1; i < n; i++) { sum1 = sum0 + a[i]; if (sum1 * sum0 < 0) { } else if (sum1 * sum0 > 0) { ans2 += abs(sum1) + 1; sum1 = -1 * sum0 / abs(sum0); } else { ans2++; sum1 = -1 * sum0 / abs(sum0); } sum0 = sum1; } cout << min(ans1, ans2) << endl; return; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } solve(); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { @SuppressWarnings("resource") Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } int count = 0,sabun = 0; int[] imos = new int[a.length]; Arrays.fill(imos, 0); for (int i = 0; i < a.length; i++) { imos[i] = a[i]; } for (int i = 0; i < a.length-1; i++) { imos[i+1] += imos[i]; } // System.out.println("init"); // for (int i = 0; i < imos.length; i++) { // System.out.println(imos[i]); // } if (imos[0] == 0) { for (int i = 0; i < imos.length; i++) { imos[i] += 1; } } // System.out.println("init:finish"); // for (int i = 1; i < a.length; i++) { if (imos[i-1]*imos[i] > 0) { if (imos[i-1] < 0) { sabun = -imos[i-1]-1; } else { sabun = -imos[i-1]-1; } count += Math.abs(sabun); for (int j = i; j < imos.length; j++) { imos[j] += sabun; } } if (imos[i-1]*imos[i] == 0) { count += 1; if (imos[i-1] < 0) { sabun = 1; } else { sabun = -1; } for (int j = i; j < imos.length; j++) { imos[j] += sabun ; } } } for (int i = 0; i < a.length; i++) { // System.out.println(imos[i]); } System.out.println(count); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import itertools def sign(num): if num < 0: return -1 elif num > 0: return 1 else: return 0 N = input() a_i = list(map(int, input().split())) # a_sum = [a_i[0]] # for i, a in enumerate(a_i[1:]): # i += 1 # a_sum.append(a_sum[-1]+a) # # signs = [1, -1] # changes = 0 # # for i, sum_i in enumerate(a_sum): # if sum_i != 0: # signs[i%2] = sign(sum_i) # signs[i%2+1] = -sign(sum_i) # break signs = [sign(a_i[0]), -sign(a_i[0])] a_sum = 0 changes = 0 for i, a in enumerate(a_i): a_sum += a if sign(a_sum) != signs[i%2]: changes += abs(a_sum) + 1 a_sum = signs[i%2] print(changes) # # for i, sum_i in enumerate(a_sum): # if i == 0: # signs = [sign(sum_i), -sign(sum_i)] # elif sign(sum_i) != signs[i%2]: # a_sum[i:] = [num + (abs(sum_i) + 1) * signs[i%2] for num in a_sum[i:]] # changes += abs(sum_i) + 1 # # print(a_sum) # print(changes)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<bits/stdc++.h> using namespace std; int main(){ long long N,sum,ans,ans2; cin>>N; sum=0; vector<long long>x(N); for(int i=0;i<N;i++){ cin>>x[i]; } for(int i=0;i<N;i++){ sum+=x[i]; if(i%2==0&&sum<=0){ ans+=1-sum; sum=1; } if(i%2==1&&sum>=0){ ans+=1+sum; sum=-1; } } ans2=ans; ans=0; sum=0; for(int i=0;i<N;i++){ sum+=x[i]; if(i%2==1&&sum<=0){ ans+=1-sum; sum=1; } if(i%2==0&&sum>=0){ ans+=1+sum; sum=-1; } } cout<<min(sum,sum2)<<endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
import std.stdio, std.algorithm, std.conv, std.array, std.string; long check(long op, long sum, long[] as) { foreach (a; as) { if (sum < 0) { if ((sum + a) <= 0) { op += (1 - (sum + a)); sum = 1; } else { sum += a; } } else { if ((sum + a) >= 0) { op += sum + a + 1; sum = -1; } else { sum += a; } } } return op; } void main() { readln; auto as = readln.chomp.split(" ").map!(to!long).array; auto op1 = check(0, as[0], as[1..$]); auto op2 = check(as[0] < 0 ? 1 - as[0] : as[0] + 1, as[0] < 0 ? 1 : -1, as[1..$]); writeln(op1 < op2 ? op1 : op2); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void sum(int* N, int* S, int n); void add(int* S, int n, int del, int k); int main() { int *N, *S; int count_eve = 0, count_odd = 0, n; int j = 0, k = 0; cin >> n; N = new int[n]; S = new int[n]; for (int i = 0; i < n; i++) { cin >> N[i]; } sum(N, S, n); int delta1 = 0, delta2 = 0; while (j != n) { if (j % 2 == 0 && S[j] <= 0) { count_eve += abs(S[j]) + 1; add(S, n, abs(S[j]) + 1, j); } else if (j % 2 == 1 && S[j] >= 0) { count_eve += abs(S[j]) + 1; add(S, n, -abs(S[j]) - 1, j); } j++; } sum(N, S, n); while (k != n) { if (k % 2 == 0 && S[k] >= 0) { count_odd += abs(S[k]) + 1; add(S, n, -abs(S[k]) - 1, k + 1); } else if (k % 2 == 1 && S[k] <= 0) { count_odd += abs(S[k]) + 1; add(S, n, abs(S[k]) + 1, k); } k++; } cout << min(count_eve, count_odd) << endl; delete[] N; delete[] S; return 0; } void sum(int* N, int* S, int n) { S[0] = N[0]; for (int i = 1; i < n; i++) S[i] = S[i - 1] + N[i]; } void add(int* S, int n, int del, int k) { for (int i = k; i < n + 1; i++) S[i] += del; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (auto&& x : a) cin >> x; long long ans = 1e10, sum = 0, cnt = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { if (sum <= 0) { cnt += abs(sum) + 1; sum = 1; } } else { if (sum >= 0) { cnt += sum + 1; sum = -1; } } } ans = min(ans, cnt), cnt = 0, sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { if (sum >= 0) { cnt += sum + 1; sum = -1; } } else { if (sum <= 0) { cnt += abs(sum) + 1; sum = 1; } } } ans = min(ans, cnt); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import numpy as np n = int(input()) a = list(map(int, input().split())) sa = np.zeros(2) count = np.zeros(2) for i in range(n): sa += a[i] if sa[0] <= 0 and i%2 == 0: count[0] += 1 - sa[0] sa[0] = 1 elif sa[0] >= 0 and i%2 == 1: count[0] += 1 + sa[0] sa[0] = -1 if sa[1] <= 0 and i%2 == 1: count[1] += 1 - sa[1] sa[1] = 1 elif sa[1] >= 0 and i%2 == 0: count[1] += 1 + sa[1] sa[1] = -1 print(min(count))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = [int(a) for a in input().split()] ans = 0 s = A[0] if s == 0: for i in range(1, n): if A[i] > 0: s = -1 ans += 1 break elif A[i] < 0: s = 1 ans += 1 break if s == 0: ans = n else: for i in range(1, n): if s > 0: s += A[i] if s >= 0: ans += s+1 s = -1 elif s < 0: s += A[i] if s <= 0: ans -= s-1 s = 1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
from itertools import accumulate import copy N = int(input()) a = list(map(int,input().split())) a = list(accumulate(a)) ans1 = 0 ans2 = 0 b = copy.copy(a) for i in range(N): if i % 2 == 0: if a[i] > 0: pass else: ans1 += abs(a[i]) + 1 a[i+1:] = list(map(lambda n:n+abs(a[i])+1, a[i+1:])) else: if a[i] < 0: pass else: ans1 += abs(a[i]) + 1 a[i+1:] = list(map(lambda n:n-(abs(a[i])+1), a[i+1:])) for i in range(N): if i % 2 == 1: if b[i] > 0: pass else: ans2 += abs(b[i]) + 1 b[i+1:] = list(map(lambda n:n+abs(b[i])+1, b[i+1:])) else: if b[i] < 0: pass else: ans2 += abs(b[i]) + 1 b[i+1:] = list(map(lambda n:n-(abs(b[i])+1), b[i+1:])) print(min(ans1,ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int b = 0, c = 0; int sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i & 1) { if (sum >= 0) { b += sum + 1; sum = -1; } } else { if (sum <= 0) { b += -sum + 1; sum = 1; } } } sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i & 1) { if (sum <= 0) { c += -sum + 1; sum = 1; } } else { if (sum >= 0) { c += sum + 1; sum = -1; } } } cout << min(b, c) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
n = gets.to_i a = gets.split.map(&:to_i) s = 0 c = 0 a.each do |i| sign = s > 0 s += i if s == 0 c += 1 s = sign ? -1 : 1 next end if sign if s > 0 c += s + 1 s = -1 end else if s < 0 c += -s + 1 s = 1 end end end p c
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using LL = long long; using G = vector<vector<int>>; int di[] = {0, -1, 0, 1}; int dj[] = {1, 0, -1, 0}; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<int> A(n); for (int i = (0); i < (n); i++) cin >> A[i]; LL ans = 0; if (A[0] == 0) { int ans1 = 0; int b = -1; for (int i = (1); i < (n); i++) { int a = A[i]; if (b * (b + a) >= 0) { if (b + a < 0) { ans1 += 1 - (b + a); a = 1 - b; } else { ans1 += (b + a) + 1; a = -b - 1; } } b = b + a; } int ans2 = 0; b = 1; for (int i = (1); i < (n); i++) { int a = A[i]; if (b * (b + a) >= 0) { if (b + a < 0) { ans2 += 1 - (b + a); a = 1 - b; } else { ans2 += (b + a) + 1; a = -b - 1; } } b = b + a; } ans = min(ans1, ans2); } else { int b = A[0]; for (int i = (1); i < (n); i++) { int a = A[i]; if (b * (b + a) >= 0) { if (b + a < 0) { ans += 1 - (b + a); a = 1 - b; } else { ans += (b + a) + 1; a = -b - 1; } } b = b + a; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) ans = 0 cnt = [a[0]] for i in range(1,n): cnt.append(cnt[-1]+a[i]) cnt_o = cnt[::2] cnt_e = cnt[1::2] o = sum(cnt_o) e = sum(cnt_e) ans = 0 c = 0 if o >= e: for i in range(n): cnt[i] += c if (i+1) % 2 != 0: if cnt[i] > 0: pass else: ans += abs(cnt[i])+1 c += abs(cnt[i])+1 else: if cnt[i] < 0: pass else: ans += cnt[i]+1 c -= a[i]+1 else: for i in range(n): cnt[i] += c if (i+1) % 2 != 0: if cnt[i] < 0: pass else: ans += cnt[i]+1 c -= cnt[i]+1 else: if cnt[i] > 0: pass else: ans += abs(cnt[i])+1 c += abs(a[i])+1 if sum(cnt) == 0: print(ans+1) else: print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) _arr = list(map(int, input().split())) ans = [] for first in (_arr[0], -1 * _arr[1] + 1, -1 * _arr[1] - 1): arr = _arr[:] c = 0 prev = 0 for i in range(n): t = prev + arr[i] if i == 0: c += abs(arr[i] - first) arr[i] = first elif prev > 0 and t >= 0: diff = t + 1 c += diff arr[i] -= diff elif prev < 0 and t <= 0: diff = -1 * t + 1 c += diff arr[i] += diff prev += arr[i] ans.append(c) print(min(ans))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1LL << 60; int main() { int n, cnt1 = 0, cnt2 = 0; long long a, b, sum1 = 0, sum2 = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a; b = a; if (i % 2 == 0) { if (sum1 + a <= 0) { cnt1 += 1 - sum1 - a; sum1 = 1; } else { sum1 += a; } } if (i % 2 == 1) { if (sum1 + a >= 0) { cnt1 += sum1 + a + 1; sum1 = -1; } else { sum1 += a; } } if (i % 2 == 1) { if (sum2 + b <= 0) { cnt2 += 1 - sum2 - b; sum2 = 1; } else { sum2 += b; } } if (i % 2 == 0) { if (sum2 + b >= 0) { cnt2 += sum2 + b + 1; sum2 = -1; } else { sum2 += b; } } } cout << min(cnt1, cnt2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
local n = io.read("*n") local a = {} for i = 1, n do a[i] = io.read("*n") end local ret = 1000000007 for flag = 1, 2 do flag = flag * 2 - 3 local cur = 0 local cnt = 0 for i = 1, n do cur = cur + a[i] if flag < 0 then if 0 <= cur then cnt = cnt + cur + 1 cur = -1 end else if cur <= 0 then cnt = cnt - cur + 1 cur = 1 end end flag = flag * -1 end ret = math.min(ret, cnt) end print(ret)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T lcm(T a, T b) { return (a / gcd(a, b)) * b; } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } template <class T> inline T bpow(T p, T e) { long long ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p); p = (p * p); } return (T)ret; } struct data { int ele1, ele2, ele3; data() {} data(int a, int b, int c) { ele1 = a, ele2 = b, ele3 = c; } bool friend operator<(data a, data b) { return a.ele1 > b.ele1; } }; int n, m, k; long long N, M, K; long long a[100005]; int main() { int t, tc = 1; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); } long long sum = 0; int pre = 0; if (a[1] < 0) pre = 1; for (int i = 2; i <= n; i++) { a[i] = a[i] + a[i - 1]; if (a[i] < 0) { if (pre == 1) { sum += (1 - a[i]); a[i] = -1; pre = 0; } } else { if (pre == 0) { sum += (a[i] - 1); a[i] = 1; pre = 1; } } pre = 1 - pre; } printf("%lld", sum); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int zerocount(vector<int>& a, int i, int n) { if (a.at(i) != 0) { return 0; } if (i == n - 1) { a.at(i) = 1; return 1; } int count = 0; count += zerocount(a, i + 1, n); if (a.at(i + 1) > 0) { a.at(i) = -1; } else { a.at(i) = 1; } count++; return count; } int main() { int n; cin >> n; vector<int> a(n); for (auto& x : a) { cin >> x; } bool hugou; int sum = 0; int count = 0; if (a.at(0) < 0) { hugou = false; sum += a.at(0); } if (a.at(0) > 0) { hugou = true; sum += a.at(0); } if (a.at(0) == 0) { count += zerocount(a, 0, n); } for (int i = 0; i < n - 1; i++) { int i_sum; i_sum = sum + a.at(i + 1); if (i_sum >= 0) { if (hugou) { sum = -1; count += (i_sum + 1); hugou = false; } else { hugou = true; sum = i_sum; } } if (i_sum <= 0) { if (!hugou) { sum = 1; count += (-1) * (i_sum - 1); hugou = true; } else { hugou = false; sum = i_sum; } } } if (sum == 0) count++; cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[100010]; int ans = 0; int cnt = 0; int flag = 1; for (int i = 0; i < n; i++) cin >> a[i]; if (a[0] > 0) flag = 1; else if (a[0] < 0) flag = -1; cnt = a[0]; for (int i = 1; i < n; i++) { cnt += a[i]; if (cnt * flag >= 0) { ans += abs(cnt) + 1; if (flag == -1) { cnt = 1; } else { cnt = -1; } } if (flag == -1) { flag = 1; } else { flag = -1; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> v(n); for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); i += 1 - 2 * ((0) > (n))) cin >> v[i]; long long res1 = 0, res2 = 0, res3, res4; long long som = v[0]; if (v[0] > 0) { for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 1) if (som < 0) continue; else { res1 += som + 1; som = -1; } else if (som > 0) continue; else { res1 += 1 - som; som = 1; } } res2 = v[0] + 1; som = -1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 0) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } } else if (v[0] < 0) { for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 0) if (som < 0) continue; else { res1 += som + 1; som = -1; } else if (som > 0) continue; else { res1 += 1 - som; som = 1; } } res2 = 1 - v[0]; som = 1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 1) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } } else { res1 = 1; som = 1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 1) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } res2 = 1; som = -1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 0) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } } cout << min(res1, res2); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int sign(int a) { if (a > 0) return 1; else if (a < 0) return -1; else return 0; } int main(void) { int n; cin >> n; int sum = 0, s = 1; int num = 0; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { sum += a[i]; if (i != 0 && (sign(s) == sign(sum) || sum == 0)) { num += abs(sum) + 1; sum = sign(s) * -1; s *= -1; } else if (i == 0 && a[i] == 0) { num++; sum++; s = 1; } else { s = sign(sum); } } int m = num; sum = 0; num = 0; num += abs(a[0]) + 1; if (a[0] >= 0) sum = -1; else sum = 1; s = sum; for (int i = 1; i < n; i++) { sum += a[i]; if (i != 0 && (sign(s) == sign(sum) || sum == 0)) { num += abs(sum) + 1; sum = sign(s) * -1; s *= -1; } else { s = sign(sum); } } m = min(num, m); cout << m << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int func(int N, int *a) { int ocost = 0; int ecost = 0; int temp = 0; for (int i = 0; i < N; i++) { temp += a[i]; if (i % 2 == 0) { if (temp <= 0) { ocost += 1 - temp; temp = 1; } } else { if (temp >= 0) { ocost += 1 + temp; temp = -1; } } } temp = 0; for (int i = 0; i < N; i++) { temp += a[i]; if (i % 2 == 0) { if (temp >= 0) { ecost += 1 + temp; temp = -1; } } else { if (temp <= 0) { ecost += 1 - temp; temp = 1; } } } return min(ocost, ecost); } int main() { int N; cin >> N; int a[N]; for (int i = 0; i < N; i++) { cin >> a[i]; } int ans = func(N, a); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { short n; scanf("%sd", &n); short a[n]; for (int i = 0; i < n; i++) scanf(" %sd", &a[i]); int S = a[0]; int j = 0; for (int i = 1; i < n; i++) { if (S * (S + a[i]) < 0) { S += a[i]; } else { if (S < 0) { j += 1 - S - a[i]; S = 1; } else { j += S + a[i] + 1; S = -1; } } } printf("%d\n", j); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
from copy import copy n = int(input()) a = [int(x) for x in input().split()] ans1=[(-1)**i for i in range(n)] b=copy(a) res_b=0 c=copy(a) res_c=0 for i in range(n): if ans1[i]*sum(b[:i+1])>0: c[i]=-1*ans1[i]-sum(c[:i]) res_c+=abs(c[i]-a[i]) else: b[i]=ans1[i]-sum(b[:i]) res_b+=abs(b[i]-a[i]) print(min(res_b,res_c))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(int,input().split())) import sys sum=0 cnt=0 # 奇数+ for i in range(n): z+=a[i] if i%2==0: if z<0: sum=z else: cnt+=(z+1) sum=-1 else: if sum<=0: cnt+=(1-sum) sum=1 cnt_sbst=cnt # 奇数- for i in range(n): z+=a[i] if i%2==1: if sum>=0: cnt+=(z+1) sum=-1 else: if sum<=0: cnt+=(1-z) sum=1 cnt_plus=cnt ans=min(cnt_plus,cnt_sbst) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long int n, buf, max, ans; int flg, flg1; ans = 0; max = 0; buf = 0; cin >> n; vector<long long int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { if (i == 0 && a[i] == 0) { if (a[1] < 0) { a[i] = 1; ans = 1; } else if (a[1] > 0) { a[i] = -1; ans = 1; } } buf += a[i]; if (i == 0) { if (a[i] < 0) flg = -1; if (a[i] > 0) flg = 1; } else { if (buf < 0) flg = -1; if (buf > 0) flg = 1; } if (i != 0) { if (flg == flg1) { if (buf > 0) { ans += ((buf) > 0 ? (buf) : (buf * -1)) + 1; buf = -1; flg = -1; } else if (buf < 0) { ans += ((buf) > 0 ? (buf) : (buf * -1)) + 1; buf = 1; flg = 1; } else if (buf == 0) { ans += 1; if (flg1 == 1) flg = -1; if (flg1 == -1) flg = 1; } } } flg1 = flg; } max = ans; ans = 0; for (int i = 0; i < n; i++) { if (i == 0) { if (a[0] > 0) { ans = ((buf) > 0 ? (buf) : (buf * -1)) + 1; buf = -1; flg = -1; } else if (a[0] < 0) { ans = ((buf) > 0 ? (buf) : (buf * -1)) + 1; buf = 1; flg = 1; } } else { buf += a[i]; if (i == 0) { if (a[i] < 0) flg = -1; if (a[i] > 0) flg = 1; } else { if (buf < 0) flg = -1; if (buf > 0) flg = 1; } } if (i != 0) { if (flg == flg1) { if (buf > 0) { ans += ((buf) > 0 ? (buf) : (buf * -1)) + 1; buf = -1; flg = -1; } else if (buf < 0) { ans += ((buf) > 0 ? (buf) : (buf * -1)) + 1; buf = 1; flg = 1; } else if (buf == 0) { ans += 1; if (flg1 == 1) flg = -1; if (flg1 == -1) flg = 1; } } } flg1 = flg; } if (ans < max) max = ans; cout << max << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); unsigned long op = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } if (a[0] == 0) { for (int i = 1; i < n; i++) { if (a[i] == 0) { continue; } else if (a[i] > 0) { if (i % 2 == 0) { a[0] = 1; } else { a[0] = -1; } op = 1; break; } else { if (i % 2 == 0) { a[0] = -1; } else { a[0] = 1; } op = 1; break; } } if (op == 0) { a[0] = 1; op = 1; } } long sum = a[0]; for (int i = 1; i < n; i++) { if (sum > 0) { if (sum + a[i] >= 0) { op += sum + a[i] + 1; sum = -1; } else { sum += a[i]; } } else { if (sum + a[i] <= 0) { op += 1 - sum - a[i]; sum = 1; } else { sum += a[i]; } } } cout << op << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def first_positive(n,a): count = 0 sum = a[0] #正ならTrue flag = True if a[0] < 0: sum = 1 count += abs(1 - a[0]) for i in range(1,n): if sum + a[i] < 0 and flag == True: sum += a[i] flag = False continue elif sum + a[i] < 0 and flag == False: flag = True count += abs(1 - sum - a[i]) sum = 1 elif sum + a[i] >= 0 and flag == True: flag = False count += abs(-1 - sum - a[i]) sum = -1 elif sum + a[i] >= 0 and flag == False: sum += a[i] flag = True continue return count def first_negative(n,a): count = 0 sum = a[0] #正ならTrue flag = True if a[0] > 0: sum = -1 count += abs(-1 - a[0]) for i in range(1,n): if sum + a[i] < 0 and flag == True: sum += a[i] flag = False continue elif sum + a[i] < 0 and flag == False: flag = True count += abs(1 - sum - a[i]) sum = 1 elif sum + a[i] >= 0 and flag == True: flag = False count += abs(-1 - sum - a[i]) sum = -1 elif sum + a[i] >= 0 and flag == False: sum += a[i] flag = True continue return count if __name__ == '__main__': n = int(input()) a = [int(i) for i in input().split()] p = first_positive(n,a) n = first_negative(n,a) if p < n: print(p) else: print(n)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; int count = 0; cin >> N; int a[N]; for (int i = 0; i < N; i++) { cin >> a[i]; } while (true) { int num = 0; int sum = 0; for (int i = 0; i < N; i++) { num += a[i]; } for (int i = 0; i < N - 1; i++) { sum += a[i]; } if (num <= 0) { a[0] += 1; count++; } if ((sum >= 0) && (num >= 0)) { a[N] -= 1; count++; } else if ((sum <= 0) && (num <= 0)) { a[N] += 1; count++; } if (((sum > 0) && (num < 0)) || ((sum < 0) && (num > 0))) break; } cout << count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<iostream> #include<string> #include<cstdio> #include<cstring> #include<vector> #include<cmath> #include<algorithm> #include<functional> #include<iomanip> #include<queue> #include<ciso646> #include<random> #include<map> #include<set> #include<complex> #include<bitset> #include<stack> #include<unordered_map> #include<utility> #include<cassert> using namespace std; typedef long long ll; typedef unsigned long long ul; typedef unsigned int ui; typedef long double ld; const ll mod = 1000000007; const ll INF = mod * mod; #define rep(i,n) for(int i=0;i<n;i++) #define per(i,n) for(int i=n-1;i>=0;i--) #define Rep(i,sta,n) for(int i=sta;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define per1(i,n) for(int i=n;i>=1;i--) #define Rep1(i,sta,n) for(int i=sta;i<=n;i++) typedef complex<ld> Point; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair<int, int> P; typedef pair<ld, ld> LDP; typedef pair<ll, ll> LP; typedef vector<int> vec; typedef vector<string> svec; #define fr first #define sc second #define all(c) c.begin(),c.end() #define pb push_back //#define int long long int n; ll a[100100]; ll hoge(ll x) { ll sum = x; ll res = 0; rep(i, n - 1) { ll y = a[i + 1]; if(sum > 0) { if(sum + y < 0) sum += y; else { res += sum + y - (-1); sum = -1LL; } } else { if(sum + a > 0) sum += y; else { res += 1LL - (sum + y); sum = 1LL; } } } return res; } void solve() { cin >> n; rep(i, n) { cin >> a[i]; } ll c = max(a[0], 1LL), d = min(a[0], -1LL); ll ans = min(hoge(c) + (c == 1LL ? abs(a[0] - 1) : 0), hoge(d) + (d == -1LL ? abs(a[0] + 1) : 0)); cout << ans << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0); //cout << fixed << setprecision(10); //init(); solve(); //cout << "finish" << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; class Program { static string InputPattern = "InputX"; static List<string> GetInputList() { var WillReturn = new List<string>(); if (InputPattern == "Input1") { WillReturn.Add("4"); WillReturn.Add("1 -3 1 0"); //4 } else if (InputPattern == "Input2") { WillReturn.Add("5"); WillReturn.Add("3 -6 4 -5 7"); //0 } else if (InputPattern == "Input3") { WillReturn.Add("6"); WillReturn.Add("-1 4 3 2 -5 4"); //8 } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static void Main() { List<string> InputList = GetInputList(); int[] AArr = InputList[1].Split(' ').Select(X => int.Parse(X)).ToArray(); if (AArr[0] == 0) { AArr[0] = 1; long Cost1 = Solve(AArr); AArr[0] = -1; long Cost2 = Solve(AArr); Console.WriteLine(Math.Min(Cost1, Cost2)); } else { Console.WriteLine(Solve(AArr)); } } static long Solve(int[] pArr) { long Cost = 0; int RunSum = pArr[0]; for (int I = 1; I <= pArr.GetUpperBound(0); I++) { if (RunSum < 0) { RunSum += pArr[I]; if (RunSum > 0) continue; Cost += Math.Abs(RunSum) + 1; RunSum = 1; } else if (RunSum > 0) { RunSum += pArr[I]; if (RunSum < 0) continue; Cost += Math.Abs(RunSum) + 1; RunSum = -1; } } return Cost; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const int INF = INT_MAX; static const int MIN = INT_MIN; static const long long L_INF = LLONG_MAX; static const int MOD = 1000000000 + 7; static const int SIZE = 100005; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; long long a[n]; for (int i = 0; i < n; ++i) cin >> a[i]; long long even_sum = 0, odd_sum = 0; for (int i = 0; i < n; ++i) { if (i % 2) odd_sum += a[i]; else even_sum += a[i]; } int plus = 0; if (even_sum < odd_sum) plus = 1; long long sum = 0; long long res = 0; for (int i = 0; i < n; ++i) { sum += a[i]; if (i % 2 == plus) { if (sum <= 0) { res += -sum + 1; sum = 1; } } else { if (sum >= 0) { res += sum + 1; sum = -1; } } } cout << res << '\n'; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) cnt=0 for i in range(1,n): # 条件満たすまでループ for _ in range(3): print(a) now_tmp = sum(a[:i]) next_tmp = sum(a[:i+1]) print(i, now_tmp, next_tmp) # 符号が逆転していればOK かつ 現在までの総和が0でない # 異なる符号を掛けるとマイナスになる if now_tmp * next_tmp <0 and now_tmp !=0: break else: # 現在の合計がマイナスの場合 if now_tmp < 0: a[i] += next_tmp+1 cnt +=abs(next_tmp+1) # 現在の合計がプラスの場合 elif now_tmp > 0 : a[i] += -next_tmp-1 cnt +=abs(next_tmp+1) # 現在の合計が0の場合 elif now_tmp == 0 : # 1個前がプラスの場合、 if sum(a[:i-1]) > 0: a[i] += -next_tmp+1 cnt +=abs(next_tmp+1) # 1個前がマイナスの場合 else: a[i] += next_tmp+1 cnt +=abs(next_tmp+1) print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int, input().split())) ans1 = 0 s = A[0] for i in range(1, N): s += A[i] if i % 2 != 0: if s <= 0: ans1 += 1-s s = 1 else: if s >= 0: ans1 -= -1-s s = -1 ans2 = 0 s = A[0] for i in range(1, N): s += A[i] if i % 2 == 0: if s <= 0: ans2 += 1-s s = 1 else: if s >= 0: ans2 -= -1-s s = -1 print(min(ans1, ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const long long LINF = 1e18; const long long MOD = 1e9 + 7; double EPS = 1e-8; const double PI = acos(-1); int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; int main() { int n; cin >> n; long long a[int(1e5) + 5]; for (int i = 0; i < n; i++) { cin >> a[i]; } long long cnt = 0; long long sum = 0; sum += a[0]; for (int j = 1; j < n; j++) { if (sum > 0 && sum + a[j] < 0) { sum += a[j]; continue; } if (sum < 0 && sum + a[j] > 0) { sum += a[j]; continue; } if (sum < 0 && sum + a[j] <= 0) { long long dt = 1 - (sum + a[j]); cnt += dt; a[j] += dt; sum += a[j]; continue; } if (sum > 0 && sum + a[j] >= 0) { long long dt = (sum + a[j]) - (-1); cnt += dt; a[j] -= dt; sum += a[j]; continue; } } cout << cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; long long a[10010]; int main() { int N; cin >> N; for (int i = 0; i < N; i++) { cin >> a[i]; } long long sum = 0, an = 0; sum = a[0]; for (int i = 1; i < N; i++) { if (sum > 0) { if (sum + a[i] >= 0) { an += abs(a[i] - (-1 - sum)); sum = -1; } else { sum += a[i]; } } else { if (sum + a[i] <= 0) { an += abs(a[i] - (1 - sum)); sum = 1; } else { sum += a[i]; } } } sum = -a[0]; long long an2 = abs(2 * a[0]); for (int i = 1; i < N; i++) { if (sum > 0) { if (sum + a[i] >= 0) { an2 += abs(a[i] - (-1 - sum)); sum = -1; } else { sum += a[i]; } } else { if (sum + a[i] <= 0) { an2 += abs(a[i] - (1 - sum)); sum = 1; } else { sum += a[i]; } } } cout << min(an, an2); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long sum = a[0]; long long ans = 0; if (sum == 0) { if (sum + a[1] < 0) sum = 1; else sum = -1; ans++; } for (int i = 1; i < n; i++) { long long tmp = sum + a[i]; if (sum > 0 && tmp > 0) { ans += tmp + 1; sum = -1; } else if (sum < 0 && tmp < 0) { ans += -tmp + 1; sum = 1; } else if (tmp == 0) { ans++; if (sum < 0) sum = 1; else sum = -1; } else sum = tmp; } long long sum2; if (a[0] >= 0) sum2 = -1; else sum2 = 1; long long ans2 = a[0] + 1; for (int i = 1; i < n; i++) { long long tmp = sum2 + a[i]; if (sum2 > 0 && tmp > 0) { ans2 += tmp + 1; sum2 = -1; } else if (sum2 < 0 && tmp < 0) { ans2 += -tmp + 1; sum2 = 1; } else if (tmp == 0) { ans2++; if (sum2 < 0) sum2 = 1; else sum2 = -1; } else sum2 = tmp; } cout << min(ans2, ans); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; const int INF = 1001001001; int main() { int n; cin >> n; vector<int> a(n); for (long long(i) = 0; (i) < (n); (i)++) cin >> a[i]; long long s = a[0]; long long ans = 0; for (int i = 1; i < n; ++i) { long long cur = s + a[i]; if (s > 0) { if (cur >= 0) { ans += abs(cur) + 1; s = -1; } else { s += a[i]; } } else { if (cur <= 0) { ans += abs(cur) + 1; s = 1; } else { s += a[i]; } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, a[100010]; int calc(int s) { int sum = 0, res = 0; for (int i = 0; i < n; ++i, s *= -1) { sum += a[i]; if (sum * s > 0) continue; res += abs(sum - s); sum += s * abs(sum - s); } return res; } int main() { cin >> n; for (int i = 0; i < (n); ++i) cin >> a[i]; cout << min(calc(1), calc(-1)) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def ii():return int(input()) def iim():return map(int,input().split()) def iil():return list(map(int,input().split())) def ism():return map(str,input().split()) def isl():return list(map(str,input().split())) import numpy n = ii() A = iil() cum = numpy.cumsum(A) #print(cum) #print(type(cum)) now = -1*cum[0] ans = 0 ope = 0 for i,item in enumerate(cum): num = item+ope if num == 0: ans += 1 ope += 1 if now < 0 else -1 num += 1 if now < 0 else -1 elif num*now > 0: ans += abs(num)+1 ope -= (abs(num)+1)*num//abs(num) num -= (abs(num)+1)*num//abs(num) # print(ans,ope,num) now = num now = -1*cum[0] ans2 = 0 ope = 0 for i,item in enumerate(cum): num = item+ope if num == 0: ans2 += 1 ope += 1 if now < 0 else -1 num += 1 if now < 0 else -1 elif num*now > 0: ans2 += abs(num)+1 ope -= (abs(num)+1)*num//abs(num) num -= (abs(num)+1)*num//abs(num) # print(ans,ope,num) now = num print(min(ans,ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long ans = 0; int sum = a[0]; if (sum == 0) { for (int i = 1; i < n; i++) { if (a[i] > 0) sum = i % 2 ? -1 : 1; else if (a[i] < 0) sum = i % 2 ? 1 : -1; } if (sum == 0) sum = 1; ans++; } for (int i = 1; i < n; i++) { int sign = sum < 0 ? 1 : -1; sum += a[i]; if (sign * sum <= 0) { ans += -1 * sign * sum + 1; sum = sign; } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define MOD 1000000007 # define INF (1 < <29) #define MODSET(d) if ((d) >= MOD) d %= MOD; #define MODNEGSET(d) if ((d) < 0) d = ((d % MOD) + MOD) % MOD; #define MODADDSET(d) if ((d) >= MOD) d -= MOD; #define MODADDWHILESET(d) while ((d) >= MOD) d -= MOD; //defines #define FILE_IO freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #define sc1(a,type) type a; cin>>a; #define sc2(a,b,type) type a,b; cin>>a>>b; #define sc3(a, b, c,type) type a,b,c; cin>>a>>b>>c; #define sc4(a, b, c, d,type) type a ,b,c,d; cin>>a>>b>>c>>d; #define nl cout<<"\n"; #define foreach(v, c) for(__typeof( (c).begin()) v = (c).begin(); v != (c).end(); ++v) #define revforeach(v, c) for(__typeof( (c).rbegin()) v = (c).rbegin(); v != (c).rend(); ++v) #define fastio ios_base::sync_with_stdio(0);cin.tie(0); #define re(i,b) for(int i=0;i<int(b);i++) #define re1(i,b) for(int i=1;i<=int(b);i++) #define all(c) c.begin(), c.end() #define rall(c) c.rbegin(),c.rend() #define mpresent(container, element) (container.find(element) != container.end()) //for map,set..etc (returns true/false value) #define vpresent(container, element) (find(all(container),element) != container.end()) //for vectors,strings,list,deque (returns true/false value) #define eb emplace_back #define mp make_pair #define fi first #define se second #define pb push_back #define pf push_front #define ins insert #define F first #define S second #define clr clear() #define sz(x) ((int)x.size()) #define dt distance #define test(t) int t; cin>>t; while(t--) #define csb(i) __builtin_popcount(i) #define csbll(i) __builtin_popcountll(i) #define clz(x) __builtin_clz(x) #define clzl(x) __builtin_clzl(x) #define cp(x) __builtin_parity(x) #define adv(v,num) advance(v,num)//used for lists and other structures that use iterators,when you can't access elements randomly ( iterator moves num positions) #define mod 1000000007 #define MAX_ARR 1000000 #define v2d(rowsize,colsize,type,name) vector<vector<type>> name(rowsize,vector<type>(colsize)); #define digits_in(i) (ll)log10(i)+1 // gives no of digits in a number #define sqr(x) (x)*(x) //does not apply for i==0 , add an excetion contition for n==0 ( cust return count 1 for that inseted of using this function) //typedef typedef string str; typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<str> vs; typedef vector<char> vc; typedef pair<int,int> pii; typedef pair<str,int> psi; typedef pair<int,str> pis; typedef vector<pii> vii; typedef map<int,int> mii; typedef map<ll,ll> mll; typedef map<str,int> msi; typedef map<char,int> mci; typedef map<int,str> mis; typedef unordered_map<int,int> umii; typedef unordered_map<str,int> umsi; typedef unordered_map<int,str> umis; typedef unordered_map<str,str> umss; typedef unordered_map<char,int> umci; typedef set<str> ss; typedef set<int> si; typedef unordered_set<str> uss; typedef unordered_set<int> usi; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // #ifndef ONLINE_JUDGE // #include "debug.h" // #else // #define debug(args...) // #endif int main(){fastio // #ifndef ONLINE_JUDGE // FILE_IO // #endif vll v; test(t){ int temp;cin>>temp; v.pb(temp); } ll ct=0; vll v1(all(v)); re(i,sz(v)-1){ // debug(v[i] ,v[i]+v[i+1]); if( (v[i]<0 && v[i]+v[i+1]<0) || (v[i]>0 && v[i]+v[i+1]>0 )|| v[i]+v[i+1]==0 ){ if(v[i]+v[i+1]==0) ct+=1; else ct+=llabs(v[i]+v[i+1])+1; v[i+1]= v[i]>0?-1:1; } else{ v[i+1]+=v[i]; } // debug(ct); } re(i,sz(v))v1[i]*=-1; ll ct1=0; re(i,sz(v)-1){ // debug(v1[i] ,v1[i]+v1[i+1]); if( (v1[i]<0 && v1[i]+v1[i+1]<0) || (v1[i]>0 && v1[i]+v1[i+1]>0 )|| v1[i]+v1[i+1]==0 ){ if(v1[i]+v1[i+1]==0) ct1+=1; else ct1+=llabs(v1[i]+v1[i+1])+1; v1[i+1]= v1[i]>0?-1:1; } else{ v1[i+1]+=v1[i]; } // debug(ct1); } cout<<min(ct,ct1); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #define INF (1<<31) - 1 //INT_MAX/2 #define MOD 1000000007 #define PI acos(-1) using ll = long long; using ull = unsigned long long; int main(int argc, char *argv[]) { int n; std::cin >> n; std::vector<int> a(n); for (int i = 0; i < n; i++) { std::cin >> a[i]; } ll sum1 = 0; ll ans1 = 0; for (int i = 0; i < N; i++) { sum1 += a[i]; if (i%2 == 0) { if (sum <= 0) { ans1 += -sum1 + 1; sum1 = 1; } } else { if (sum1 >= 0) { ans1 += sum1 + 1; sum1 = -1; } } } ll sum2 = 0; ll ans2 = 0; for (int i = 0; i < N; i++) { sum2 += a[i]; if (i%2 == 0) { if (sum >= 0) { ans2 += sum2 + 1; sum2 = -1; } } else { if (sum2 <= 0) { ans2 += -sum2 + 1; sum2 = 1; } } } std::cout << std::min(ans1, ans2) << std::endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, a[100000], sum[2][100000], c[2]; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } if (a[0] == 0) { sum[0][0] = 1; sum[1][0] = -1; c[0] = c[1] = 1; } else { sum[0][0] = sum[1][0] = a[0]; } for (int i = 1; i < n; i++) { for (int j = 0; j < 2; j++) { if (sum[j][i - 1] > 0) { if (sum[j][i - 1] + a[i] >= 0) { c[j] += abs(sum[j][i - 1] + a[i]) + 1; sum[j][i] = -1; } else sum[j][i] = sum[j][i - 1] + a[i]; } else { if (sum[j][i - 1] + a[i] > 0) sum[j][i] = sum[j][i - 1] + a[i]; else { c[j] += abs(sum[j][i - 1] + a[i]) + 1; sum[j][i] = 1; } } } } cout << min(c[0], c[1]) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import copy import sys write = sys.stdout.write n = int(input()) A = list(map(int,input().split())) # +, -, +, ... B = copy.deepcopy(A) #-, +, -, ... sumA = [] sumB = [] cntA = 0 cntB = 0 if A[0] == 0: A[0] += 1 B[0] -= 1 elif A[0] > 0: cntB += (B[0]+1) B[0] = -1 else: cntA += abs(A[0])+1 A[0] = 1 sumA.append(A[0]) sumB.append(B[0]) #write("cntA : " + str(cntA) + " cntB : " + str(cntB) + "\n") for i in range(1, n): tempA = sumA[i-1] + A[i] tempB = sumB[i-1] + B[i] if i%2 == 1: #Aは-, Bは+ if tempA == 0: #A[i] -= 1 cntA += 1 sumA.append(-1) elif tempA > 0: #A[i] -= abs(tempA) + 1 cntA += (abs(tempA) + 1) sumA.append(-1) else: sumA.append(tempA) if tempB == 0: #B[i] += 1 cntB += 1 sumB.append(1) elif tempB < 0: #B[i] += abs(tempB) + 1 cntB += (abs(tempB) + 1) sumB.append(1) else: sumB.append(tempB) else: #Aは+, Bは- if tempA == 0: cntA += 1 sumA.append(1) elif tempA < 0: cntA += (abs(tempA) + 1) sumA.append(1) else: sumA.append(tempA) if tempB == 0: #B[i] -= 1 cntB += 1 sumB.append(-1) elif tempB > 0: #B[i] -= abs(tempB) + 1 cntB += (abs(tempB) + 1) sumB.append(-1) else: sumB.append(tempB) #write("cntA : " + str(cntA) + " cntB : " + str(cntB) + "\n") print(str(min(cntA, cntB)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = list(map(int,input().split())) sum_A = 0 ans = 0 current_zero = 0 #先頭からの0の連続数 if(n == 1): if(A[0] != 0): ans = 0 else: ans += 1 else: for i in range(n): if(i == 0): if(A[i] != 0): sum_A += A[i] else: if(A[i+1]>0): sum_A,ans = -1,1 #A[0]->-1 elif(A[i+1]<0): sum_A,ans = 1,1 #A[0]->1 else: current_zero += 1 elif(sum_A == 0): current_zero += 1 if(i == n-1): ans += 2*(current_zero-1)+1 else: if(A[i+1]>0): sum_A,ans = -1,2*(current_zero-1)+1 #A[i]->-1 elif(A[i+1]<0): sum_A,ans = 1,2*(current_zero-1)+1 #A[i]->1 else: if(sum_A > 0): judge = -1 #次の和は 負 else: judge = 1 #次の和は 正 sum_A += A[i] if(judge == 1): if(sum_A > 0): continue else: ans += abs(1-sum_A) sum_A = 1 else: #judge == -1 if(sum_A < 0): continue else: ans += abs(sum_A-(-1)) sum_A = -1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, a, sum, ans = 0; int main() { cin >> n; cin >> a; sum = a; for (int i = 1; i < n; i++) { cin >> a; if (sum > 0) { sum += a; if (sum >= 0) { ans += sum + 1; sum = -1; } } else if (sum < 0) { sum += a; if (sum <= 0) { ans += -sum + 1; sum = 1; } } } cout << ans << endl; return (0); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n,ans; cin>>n; vector<int>a(n); for(int i=0;i<n;i++){ cin>>a.at(i); } for(int i=0;i<n-1;i++){ while(a.at(i)*a.at(i+1)>=0){ if(a.at(i)>0){ a.at(i+1)--; ans++; if(p==0)p++; } if(a.at(i)<0){ a.at(i+1)++; ans++; if(p==0)p++; } } int p=0; for(int j=i+1;j>=0;j--)p+=a.at(j);//sum if(p==0){ if(a.at(i)>0)a.at(i+1)++; else a.at(i+1)--; ans++; } cout<<ans<<endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.io.IOException; import java.util.NoSuchElementException; import java.io.InputStream; import java.io.PrintWriter; @SuppressWarnings("unchecked") public class Main { static int n; static int[] a; static int f(int x) { int ope = 0; int cur = 0; for (int i = 0; i < n; i++) { // 同符号 int aim = a[i]; // 異符号 if (x * a[i] < 0) aim = x * (Math.abs(cur) + 1); // 同符号 else if (Math.abs(a[i]) <= Math.abs(cur) + 1) aim = x * (Math.abs(cur) + 1); ope += Math.max(a[i], aim) - Math.min(a[i], aim); cur += aim; // 符号反転 x *= -1; } return ope; } public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(System.out); n = nextInt(); a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); int plus = f(1); int minus = f(-1); out.println(Math.min(plus, minus)); out.flush(); } // FastScanner start static final InputStream in = System.in; static final byte[] buffer = new byte[1024]; static int ptr = 0; static int buflen = 0; static boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } static int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } static boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } static String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } static long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if(b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } static int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } static double nextDouble() { return Double.parseDouble(next()); } // FastScanner end }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N=int(input()) A=list(map(int,input().split())) now=A[0] ans=0 for i in range(1,N): if now<0 and now+A[i]<=0: ans+=abs(now)-A[i]+1 now=1 elif now>0 and now+A[i]>=0: ans+=now+A[i]+1 now=-1 else: now+=A[i] print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) ans = 0 temp=a[0] for i in range(n - 1): if temp > 0: temp+=a[i+1] if temp < 0: pass else: while temp >= 0: temp -= 1 ans += 1 else: temp+=a[i+1] if temp > 0: pass else: while temp <= 0: temp += 1 ans += 1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def count_loop(n, old_state, a_list, flag): count = 0 for i in range(1, int(n)): num_state = old_state + int(a_list[i]) if flag == 1: if num_state == 0: count += 1 old_state = -1 flag = -1 elif num_state > 0: count += num_state + 1 old_state = -1 flag = -1 else: old_state = num_state flag = -1 elif flag == -1: if num_state == 0: count += 1 old_state = 1 flag = 1 elif num_state < 0: count += abs(num_state) + 1 old_state = 1 flag = 1 else: old_state = num_state flag = 1 else: if num_state == 0: count += 1 flag = 0 elif num_state > 0: old_state = num_state flag = 1 else: old_state = num_state flag = -1 return count if __name__ == "__main__": n = input() a = input() a_list = a.split(" ") old_state = int(a_list[0]) count = 0 if old_state == 0: count += 1 c1 = count_loop(n,1,a_list,0) c2 = count_loop(n,-1,a_list,0) else: c1 = count_loop(n,old_state,a_list,1) c2 = count_loop(n,old_state,a_list,-1) if c1 <= c2: print(c1) else: print(c2)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
input() iter_ = iter(map(int, input().rstrip("\n").split())) sum_ = next(iter_) ans = 0 for num in iter_: if sum_ == 0: sum_ += 1 ans += 1 presum = sum_ sum_ += num while presum * sum_ >= 0: sum_ += -1 if presum > 0 else 1 ans += 1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1e+9 + 7; long long n, m, l; string s, t; long long d[100000], dp[100][100]; int main() { cin >> n; for (long long i = (0); i < (n); i++) cin >> d[i]; int sum1 = 0, sum2 = 0; int ans1 = 0, ans2 = 0; for (long long i = (0); i < (n); i++) { sum1 += d[i]; if (i % 2 == 0) { if (sum1 <= 0) { ans1 += -sum1 + 1; sum1 = 1; } } else { if (sum1 >= 0) { ans1 += sum1 + 1; sum1 = -1; } } } for (long long i = (0); i < (n); i++) { sum2 += d[i]; if (i % 2 == 0) { if (sum2 >= 0) { ans2 += sum2 + 1; sum2 = -1; } } else { if (sum2 <= 0) { ans2 += sum2 + 1; sum2 = 1; } } } cout << (min(ans1, ans2)) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; const int INF = 1001001001; const int MOD = 1000000007; template <typename T> void print(const T &v); int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int bf_e = 0; int cnt_e = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (bf_e + a[i] <= 0) { int num; num = 1 - bf_e - a[i]; cnt_e += num; bf_e = 1; } else { bf_e = bf_e + a[i]; } } else { if (bf_e + a[i] >= 0) { int num; num = bf_e + a[i] + 1; cnt_e += num; bf_e = -1; } else { bf_e = bf_e + a[i]; } } } int bf_o = 0; int cnt_o = 0; for (int i = 0; i < n; i++) { if (i % 2 == 1) { if (bf_o + a[i] <= 0) { int num; num = 1 - bf_o - a[i]; cnt_o += num; bf_o = 1; } else { bf_o = bf_o + a[i]; } } else { if (bf_o + a[i] >= 0) { int num; num = bf_o + a[i] + 1; cnt_o += num; bf_o = -1; } else { bf_o = bf_o + a[i]; } } } cout << min(cnt_o, cnt_e) << endl; } template <typename T> void print(T const &v) { for (int i = 0; i < v.size(); i++) { if (i) cout << " "; cout << v[i]; } cout << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using ll = long long; using namespace std; const double pi = acos(-1.0); const ll MOD = 1e9 + 7; const ll INF = 1LL << 60; void print(vector<ll> vec) { for (int i = 0; i < ((ll)(vec).size()); i++) { if (i) cout << " "; cout << vec[i]; } cout << "\n"; } ll dp[100005]; int main() { ll n; cin >> n; vector<ll> a(n); for (int i = 0; i < n; i++) cin >> a[i]; dp[0] = a[0]; ll ans = 0; for (int i = 1; i < n; i++) { if (dp[i - 1] < 0) { if (dp[i - 1] + a[i] > 0) { dp[i] = dp[i - 1] + a[i]; } else { ll ai = 1 - dp[i - 1]; ans += abs(ai - a[i]); dp[i] = 1; } } if (dp[i - 1] > 0) { if (dp[i - 1] + a[i] < 0) { dp[i] = dp[i - 1] + a[i]; } else { ll ai = -1 - dp[i - 1]; ans += abs(ai - a[i]); dp[i] = -1; } } } for (int i = 0; i < n; i++) dp[i] = 0; ll cnt = 0; if (a[0] > 0) { dp[0] = -1; cnt += abs(a[0] + 1); } else { dp[0] = 1; cnt += abs(a[0] - 1); } for (int i = 1; i < n; i++) { if (dp[i - 1] < 0) { if (dp[i - 1] + a[i] > 0) { dp[i] = dp[i - 1] + a[i]; } else { ll ai = 1 - dp[i - 1]; cnt += abs(ai - a[i]); dp[i] = 1; } } if (dp[i - 1] > 0) { if (dp[i - 1] + a[i] < 0) { dp[i] = dp[i - 1] + a[i]; } else { ll ai = -1 - dp[i - 1]; cnt += abs(ai - a[i]); dp[i] = -1; } } } ans = min(ans, cnt); cout << ans << "\n"; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) sum = a[0] plus = None if a[0] > 0: plus = True else: plus = False ans1 = 0 for i in range(1, n): plus = not(plus) sum += a[i] if plus: if sum <= 0: ans1 += abs(sum) + 1 sum = 1 else: if sum >= 0: ans1 += abs(sum) + 1 sum = -1 sum = 0 a[0] = a[0] // abs(a[0]) * -1 if a[0] > 0: plus = True else: plus = False ans2 = abs(a[0]) + 1 for i in range(1, n): plus = not(plus) sum += a[i] if plus: if sum <= 0: ans2 += abs(sum) + 1 sum = 1 else: if sum >= 0: ans2 += abs(sum) + 1 sum = -1 print(min(ans1, ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; public class Main { int res = 0; public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] arr = new int[n]; int sumeven = 0; int sumodd = 0; int counteven = 0; int countodd = 0; for (int i = 0; i < arr.length; i++) { arr[i] = in.nextInt(); sumeven+=arr[i]; sumodd+=arr[i]; if (i%2==0) { if(sumeven>=0){ counteven+=(1+sumeven); sumeven = -1; } if (sumodd<=0) { countodd+=(1-sumodd); sumodd = 1; } }else{ if (sumeven<=0) { counteven+=(1-sumeven); sumeven = 1; } if (sumodd>=0) { countodd+=(1+sumodd); sumodd = -1; } } } System.out.println(counteven < countodd ? counteven : countodd); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# -*- coding: utf-8 -*- N = int(input()) a = [int(n) for n in input().split()] count_a = 0 count_b = 0 nowsum = a[0] if nowsum != 0: for n in range(1, N): if nowsum * (nowsum + a[n]) >= 0: count_a += abs(nowsum + a[n]) + 1 if nowsum < 0: nowsum = 1 else: nowsum = -1 else: nowsum += a[n] print(count_a) else: a[0] = 1 count_a += 1 nowsum = 1 for n in range(1, N): if nowsum * (nowsum + a[n]) >= 0: count_a += abs(nowsum + a[n]) + 1 if nowsum < 0: nowsum = 1 else: nowsum = -1 else: nowsum += a[n] a[0] = -1 count_b += 1 nowsum = -1 for n in range(1, N): if nowsum * (nowsum + a[n]) >= 0: count_b += abs(nowsum + a[n]) + 1 if nowsum < 0: nowsum = 1 else: nowsum = -1 else: nowsum += a[n] print(min(count_a, count_b))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; long long a[100004]; long long solve() { long long ans = 0; for (int i = (2); i <= (int)(n); ++i) { if (a[i - 1] > 0) { if (a[i] + a[i - 1] < 0) { a[i] += a[i - 1]; continue; } ans += abs(a[i] + 1 + a[i - 1]); a[i] = -1; } else { if (a[i] + a[i - 1] > 0) { a[i] += a[i - 1]; continue; } ans += abs(a[i] - 1 + a[i - 1]); a[i] = 1; } } return ans; } int main() { scanf("%d", &n); for (int i = (1); i <= (int)(n); ++i) scanf("%lld", &a[i]); long long ans = 0; if (!a[1]) { a[1] = 1; ans = solve() + 1; a[1] = -1; ans = min(ans, solve() + 1); } else ans = solve(); printf("%lld\n", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long sum = a[0]; long long cnt = 0; if (sum == 0) { sum = (a[1] > 0 ? 1 : -1); cnt++; } for (int i = 1; i < n; i++) { long long nsum = sum + a[i]; if (sum > 0 && nsum < 0 || sum < 0 && nsum > 0) { sum = nsum; continue; } sum = (sum > 0 ? -1 : 1); cnt += (nsum == 0 ? 1 : abs(nsum) + 1); } cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MAXN = 100 * 1000 + 10; int main() { int n, f = 0, z = 0, s = 0, sum = 0; cin >> n; int b[n]; for (int i = 0; i < n; i++) { cin >> b[i]; if (i % 2 == 0) { f += b[i]; } else { z += b[i]; } } if (f > z) { if (b[0] <= 0) { s += 1 - b[0]; b[0] = 1; } if (b[1] >= 0) { s += b[1] + 1; b[1] = -1; } } else { if (b[0] >= 0) { s += b[0] + 1; b[0] = -1; } if (b[1] < 0) { s += -1 * b[1]; b[1] = 0; } } for (int i = 0; i < n - 1; i++) { sum += b[i]; if (sum < 0 && sum + b[i + 1] < 0) { s += -1 * (sum + b[i + 1]) + 1; b[i + 1] += -1 * (sum + b[i + 1]) + 1; } else if (sum > 0 && sum + b[i + 1] >= 0) { s += sum + b[i + 1] + 1; b[i + 1] -= sum + b[i + 1] + 1; } else if (sum + b[i + 1] == 0) { if (sum < 0) { b[i + 1] += 1; } else { b[i + 1] -= 1; } s++; } } cout << s; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; const int mod = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; int64_t a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int64_t s_i = a[0]; int64_t s_i_1; int64_t d = 0; int64_t c = 0; for (int i = 1; i < n; i++) { if (s_i == 0) { s_i += 1; c++; } s_i_1 = s_i + a[i]; if ((s_i_1 > 0 && s_i > 0) || (s_i < 0 && s_i_1 < 0)) { d = abs(s_i_1 - s_i); if (s_i > 0) { if (s_i_1 != 0) { s_i_1 -= d + 1; c += d + 1; } else { s_i_1 -= 1; d += 1; } } else { if (s_i_1 != 0) { s_i_1 += d + 1; c += d + 1; } else { s_i_1 += 1; c += 1; } } } s_i = s_i_1; } if (s_i == 0) { c += 1; } cout << c << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, ansa = 0, ansb = 0, suma = 0, sumb = 0; cin >> n; for (int i = 0; i < (n); i++) { int a, b; cin >> b; a = b; if (i % 2 == 0) { while (suma + a <= 0) { a++; ansa++; } while (sumb + b >= 0) { b--; ansb++; } } else { while (suma + a >= 0) { a--; ansa++; } while (sumb + b <= 0) { b++; ansb++; } } suma += a; sumb += b; } cout << min(ansa, ansb) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } int s1, a1 = 0, s2, a2 = 0; if (a[0] > 0) { s1 = a[0]; s2 = -1; a2 = a[0] + 1; } else if (a[0] < 0) { s1 = 1; s2 = a[0]; a1 = 1 - a[0]; } else { s1 = 1; s2 = -1; a1 = 1; a2 = 1; } for (int i = 1; i < n; ++i) { if (s1 > 0) { if (s1 + a[i] >= 0) { a1 += s1 + a[i] + 1; s1 = -1; } else s1 += a[i]; } else { if (s1 + a[i] <= 0) { a1 = 1 - a1 - a[i]; s1 = 1; } else s1 += a[i]; } } for (int i = 1; i < n; ++i) { if (s2 > 0) { if (s2 + a[i] >= 0) { a2 += s2 + a[i] + 1; s2 = -1; } else s2 += a[i]; } else { if (s2 + a[i] <= 0) { a2 = 1 - a2 - a[i]; s2 = 1; } else s2 += a[i]; } } return min(a1, a2); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a; cin >> a; long long sum = a; long long cnt = 0; for (int i = 1; i < n; ++i) { cout << "sum: " << sum << ", cnt: " << cnt << endl; cin >> a; int next = sum + a; int c, diff; c = diff = 0; if (sum > 0) { if (next >= 0) { diff = (-1 - sum); a = diff - a; c = diff; } } else { if (next <= 0) { diff = (1 - sum) - a; a += diff; c = diff; } } sum += a; cnt += abs(c); } cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int main(void) { int n; std::cin >> n; std::vector<long long> a(n); for (int i = 0; i < n; ++i) std::cin >> a[i]; int cost_all = 0; std::vector<long long> s(n, 0); for (int k = 0; k < 2; ++k) { int cost = 0; if (k == 0) { s[0] = (a[0] > 0 ? a[0] : 1); cost += std::abs(a[0] - s[0]); } else { s[0] = (a[0] < 0 ? a[0] : -1); cost += std::abs(a[0] - s[0]); } for (int i = 1; i < n; ++i) { s[i] = s[i - 1] + a[i]; if (s[i] * s[i - 1] < 0) continue; else { if (s[i - 1] < 0) { cost += std::abs(1 - s[i - 1] - a[i]); a[i] = 1 - s[i - 1]; } else { cost += std::abs(-1 - s[i - 1] - a[i]); a[i] = -1 - s[i - 1]; } } s[i] = s[i - 1] + a[i]; } cost_all = (cost < cost_all ? cost : cost_all); } std::cout << cost_all << std::endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 999999999; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int sum1 = 0; int count1 = 0; int sum2 = 0; int count2 = 0; for (int i = 0; i < n; i++) { sum1 += a[i]; if (i % 2 == 0) { if (sum1 < ~0) { count1 += abs(sum1) + 1; sum1 = 1; } } else { if (sum1 >= 0) { count1 += abs(sum1) + 1; sum1 = -1; } } } for (int i = 0; i < n; i++) { sum2 += a[i]; if (i % 2 == 0) { if (sum2 >= 0) { count2 += abs(sum2) + 1; sum2 = -1; } } else { if (sum2 <= 0) { count2 += abs(sum2) + 1; sum2 = 1; } } } cout << min(count1, count2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; bool dif(long long int a, long long int b) { if (a < 0 && b > 0) return true; if (a > 0 && b < 0) return true; return false; } int odd(vector<int> v, vector<int> &w) { long long int ans = 0; if (v[0] <= 0) while (++v[0] != 1) ; long long int sum = v[0]; ans = abs(v[0] - w[0]); for (int i = 1; i < v.size(); i++) { if (dif(sum, sum + v[i])) { sum += v[i]; } else { if (sum > 0) { v[i] = -1 - sum; } else if (sum < 0) { v[i] = 1 - sum; } sum += v[i]; } ans += abs(v[i] - w[i]); } return ans; } int even(vector<int> v, vector<int> &w) { long long int ans = 0; if (v[0] >= 0) while (--v[0] != -1) ; long long int sum = v[0]; ans = abs(v[0] - w[0]); for (int i = 1; i < v.size(); i++) { if (dif(sum, sum + v[i])) { sum += v[i]; } else { if (sum > 0) { v[i] = -1 - sum; } else if (sum < 0) { v[i] = 1 - sum; } sum += v[i]; } ans += abs(v[i] - w[i]); } return ans; } int main() { int n; cin >> n; vector<int> v(n), cpy; for (int &i : v) cin >> i; cpy = v; long long int ans = min(odd(v, cpy), even(v, cpy)); cout << ans; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll mod = 1e9 + 7; int n; vector<ll> a; ll cost(bool sign) { ll cst = 0, rs = 0; for (int i = 0; i < n; i++) { rs += a[i]; if (rs == 0) { cst += sign ? 1 : -1; } else { if ((rs > 0) == true && !sign) { cst += rs + 1; rs = -1; } else if ((rs > 0) == false && sign) { cst += abs(rs) + 1; rs = 1; } } sign = !sign; } return cst; } int main() { cin >> n; a.resize(n); for (ll &i : a) cin >> i; ll ans = cost(true); ans = min(ans, cost(false)); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int64_t> a(n + 10, 0); for (int i = 1; i <= n; ++i) { cin >> a[i]; } int pn = 0; int mn = 0; const int64_t first = a[1]; { int num = 0; int64_t total = 0; if (first <= 0) { num = 1 - first; total = 1; } else { total = first; } for (int i = 2; i <= n; ++i) { int64_t ai = a[i]; if (i % 2 == 0) { if (ai >= 0) { num += -(-1 - ai); ai = -1; } while (total + ai >= 0) { --ai; ++num; } total += ai; } else { if (ai <= 0) { num += 1 - ai; ai = 1; } while (total + ai <= 0) { ++ai; ++num; } total += ai; } } pn = num; } { int num = 0; int64_t total = 0; if (first >= 0) { num = -(-1 - first); total = -1; } else { total = first; } for (int i = 2; i <= n; ++i) { int64_t ai = a[i]; if (i % 2 == 0) { if (ai <= 0) { num += 1 - ai; ai = 1; } while (total + ai <= 0) { ++ai; ++num; } total += ai; } else { if (ai >= 0) { num += -(-1 - ai); ai = -1; } while (total + ai >= 0) { --ai; ++num; } total += ai; } } mn = num; } cout << min(pn, mn) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = list(map(int, input().split())) acc = [0] * n acc[0] = A[0] for i in range(1, n): acc[i] = acc[i - 1] + A[i] ans = 0 cur = acc[0] x = 0 for i in range(1, n): acc[i] += x if cur > 0: if acc[i] >= 0: ans += acc[i] + 1 x -= acc[i] + 1 acc[i] = -1 else: if acc[i] <= 0: ans += abs(acc[i]) + 1 x += abs(acc[i]) + 1 acc[i] = 1 cur = acc[i] print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long MOD = 1e9 + 7; const long LINF = 1e13; const long LLINF = 1e18; template <class T> void Swap(T& r, T& l) { T tmp = r; r = l; l = tmp; } int main() { long n; cin >> n; vector<long> a(n); vector<long> accum(n, 0); for (int i = 0; i < n; ++i) { cin >> a[i]; accum[i] = a[i]; if (i > 0) accum[i] += accum[i - 1]; } vector<long> accumtmp(n, 0); copy(accum.begin(), accum.end(), accumtmp.begin()); long ans = 0; long count = 0; long tmpcount = 0; for (int i = 1; i < n; ++i) { long accump = accumtmp[i] + tmpcount; if (i % 2 == 1) { if (accump >= 0) { long tmpc = -(-1 - accump); count += tmpc; accumtmp[i] = -1; tmpcount -= tmpc; } } else { if (accump <= 0) { long tmpc = 1 - accump; count += tmpc; tmpcount += tmpc; } } } ans = count; ans = min(ans, count); cout << ans; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long int> a(n); for (int i = 0; i < (n); ++i) cin >> a[i]; long long int ans = 1LL << 60, sum = a[0]; long long int tmp = 0; for (int i = 1; i < n; ++i) { sum += a[i]; if (sum == 0) { tmp++; if (sum - a[i] > 0) sum--; else sum++; } if (sum - a[i] >= 0 && sum >= 0) { tmp += sum + 1; sum = -1; } else if (sum - a[i] <= 0 && sum <= 0) { tmp += abs(sum) + 1; sum = 1; } } (ans = min(ans, tmp)); tmp = 2 * abs(a[0]); sum = -a[0]; for (int i = 1; i < n; ++i) { sum += a[i]; if (sum == 0) { tmp++; if (sum - a[i] > 0) sum--; else sum++; } if (sum - a[i] >= 0 && sum >= 0) { tmp += sum + 1; sum = -1; } else if (sum - a[i] <= 0 && sum <= 0) { tmp += abs(sum) + 1; sum = 1; } } (ans = min(ans, tmp)); cout << ans << endl; ; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<int> rev_a = a; int result = 0; bool isPlus = a[0] > 0 ? true : false; int sum = a[0]; for (int i = 1; i < n; i++) { int temp_sum = sum + a[i]; if (isPlus) { if (temp_sum >= 0) { result += temp_sum + 1; a[i] -= temp_sum + 1; } } else { if (temp_sum <= 0) { result += -temp_sum + 1; a[i] += -temp_sum + 1; } } isPlus = !isPlus; sum += a[i]; } sum = 0; int rev_result = 0; isPlus = rev_a[0] > 0 ? true : false; if (isPlus) { rev_result += rev_a[0] + 1; rev_a[0] -= rev_a[0] + 1; isPlus = !isPlus; } else { rev_result -= rev_a[0] + 1; rev_a[0] += rev_a[0] + 1; isPlus = !isPlus; } for (int i = 1; i < n; i++) { int temp_sum = sum + rev_a[i]; if (isPlus) { if (temp_sum >= 0) { rev_result += temp_sum + 1; rev_a[i] -= temp_sum + 1; } } else { if (temp_sum <= 0) { rev_result += -temp_sum + 1; rev_a[i] += -temp_sum + 1; } } isPlus = !isPlus; sum += rev_a[i]; } if (rev_result < result) cout << rev_result << endl; else cout << result << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = int64_t; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int DX[] = {1, 1, 0, -1, -1, -1, 0, 1}; int DY[] = {0, -1, -1, -1, 0, 1, 1, 1}; void solve() { int n; cin >> n; ll a[n], ans1 = 0, ans2 = 0; for (int(i) = 0; (i) < (n); (i)++) cin >> a[i]; int temp = 0; for (int(i) = 0; (i) < (n); (i)++) { if (temp > 0 && temp + a[i] > 0) { ans1 += abs(-1 - temp - a[i]); temp = -1; } else if (temp < 0 && temp + a[i] < 0) { ans1 += abs(1 - temp - a[i]); temp = 1; } else if (temp + a[i] == 0) { if (temp > 0) { temp = -1; } else { temp = 1; } ans1 += 1; } else { temp += a[i]; } } temp = 0; if (a[0] > 0) { ans2 += (a[0] * (-1) - 1); a[0] = a[0] * (-1) - 1; } else { ans2 = (a[0] * (-1) + 1); a[0] = a[0] * (-1) + 1; } for (int(i) = 0; (i) < (n); (i)++) { if (temp > 0 && temp + a[i] > 0) { ans2 += abs(-1 - temp - a[i]); temp = -1; } else if (temp < 0 && temp + a[i] < 0) { ans2 += abs(1 - temp - a[i]); temp = 1; } else if (temp + a[i] == 0) { if (temp > 0) { temp = -1; } else { temp = 1; } ans2 += 1; } else { temp += a[i]; } } cout << min(ans1, ans2) << endl; } int main() { solve(); return 0; }