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; int d[n]; for (int i = 0; i < n; i++) cin >> d[i]; int sume; int counte; for (int i = 0; i < n; i++) { sume += d[i]; if (i % 2 == 0) { if (sume <= 0) counte += 1 - sume; sume = 1; } else { if (sume >= 0) counte += sume + 1; sume = -1; } } int sumo; int counto; for (int i = 0; i < n; i++) { sumo += d[i]; if (i % 2 == 1) { if (sumo <= 0) counto += 1 - sumo; sumo = 1; } else { if (sumo >= 0) counto += sumo + 1; sumo = -1; } } cout << min(counte, counto) << 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() { freopen("testcase", "r", stdin); int N, temp; vector<int> a; scanf("%d", &N); int start = 0; bool v = false; for (int i = 0; i < N; i++) { scanf("%d", &temp); if (temp == 0) { if (!v) { start += 1; } } else if (!v) v = true; a.push_back(temp); } long long int sum = 0, cnt = 0; if (start != 0) { cnt = 2 * (start - 1) + 1; if (a[start] > 0) { if (a[start] > 1) { sum = a[start] - 1; } else { sum = 1; cnt += 1; } } else { if (a[start] < -1) { sum = a[start] + 1; } else { sum = -1; cnt += 1; } } } else { sum = a[start]; } start++; for (size_t i = start; i != a.size(); i++) { if (sum + a[i] >= 0 && sum > 0) { cnt += sum + a[i] + 1; sum = -1; } else if (sum + a[i] <= 0 && sum < 0) { cnt += 1 - sum - a[i]; sum = 1; } else { sum += a[i]; } } if (sum == 0) cnt += 1; printf("%lld\n", cnt); 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
python2
n = input() a = map(int, raw_input().split()) sum = a[0] c = 0 if sum == 0: if a[1] > 0: sum = - 1 c +=1 if a[1] < 0: sum = 1 c += 1 if a[1] == 0: sum = 1 c += 1 for i in range(1,n): temp = sum + a[i] if temp*sum > 0: if sum > 0: c += abs(-1-sum-a[i]) sum = -1 continue if sum < 0: c += abs(1-sum-a[i]) sum = 1 continue if temp == 0: c += 1 if sum > 0: sum = -1 continue if sum < 0: sum = 1 continue if temp*sum < 0: sum = temp continue print 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; int main() { int n; cin >> n; vector<int> a(n); for (auto& x : a) { cin >> x; } bool start; for (int i = 0; i < n; i++) { if (a[i] > 0) { start = (i % 2 == 0); break; } else if (a[i] < 0) { start = (i % 2 == 1); break; } if (i == n - 1) { cout << n << endl; return 0; } } long long sum = 0; long long ans = 0; bool plus = start; for (int i = 0; i < n; i++) { sum += a[i]; if (plus && sum <= 0) { ans += 1 - sum; sum = 1; } else if (!plus && sum >= 0) { ans += sum + 1; sum = -1; } plus = !plus; } 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
python3
n = int(input()) A = list(map(int, input().split())) R = [0] * n R[0] = A[0] for i in range(1, n): R[i] = R[i-1] + A[i] # search def solve(r, inc=0): ans = 0 is_plus = (r[0] > 0) for i in range(1, n): if (is_plus and r[i]+inc < 0) or (not is_plus and r[i]+inc > 0): pass else: ans += abs(r[i]+inc) + 1 if is_plus: inc -= abs(r[i]+inc) + 1 else: inc += abs(r[i]+inc) + 1 is_plus = (r[i]+inc > 0) return ans # normal ret = solve(R, 0) # modify is_plus = (R[0] > 0) ans0 = abs(R[0]) + 1 if is_plus: for i in range(n): R[i] -= abs(R[0]) + 1 else: for i in range(n): R[i] += abs(R[0]) + 1 print(min(ret, ans0 + solve(R, 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] if sum > 0: flag = 1 else: flag = -1 ans = 0 for i in range(1, n): if flag == 1 and sum + a[i] >= 0: #print("OK1") ans += abs(sum) + a[i]+ 1 sum = -1 elif flag == -1 and sum + a[i] <= 0: #print("OK2") ans += abs(sum) - a[i] + 1 sum = 1 else: #print("OK3") sum += a[i] flag = -flag #print(ans, sum, flag) 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() { 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; } if (total + ai >= 0) { const int64_t back = ai; ai = -1 - total; num += abs(ai - back); } total += ai; } else { if (ai <= 0) { num += 1 - ai; ai = 1; } if (total + ai <= 0) { const int64_t back = ai; ai = 1 - total; num += abs(ai - back); } 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
cpp
#include <bits/stdc++.h> using namespace std; int n, i; long long sum, ans; long long int a[100005]; int main() { cin >> n; for (i = 1; i <= n; i++) { cin >> a[i]; } ans = 0; sum = 0; for (i = 1; i <= n; i++) { if (a[i] == 0) { sum++; } else break; } if (sum % 2 == 0 && sum != 0) { if (a[sum + 1] > 0) { a[1] = 1; ans += 1; } else { a[1] = -1; ans += 1; } } else if (sum % 2 != 0) { if (a[sum + 1] > 0) { a[1] = -1; ans += 1; } else { a[1] = 1; ans += 1; } } sum = 0; for (i = 1; i <= n; i++) { sum += a[i]; if (sum == 0) { if (a[i] > 0) { a[i]++; ans++; sum = 1; } else { a[i]--; ans++; sum - 1; } } } sum = a[1]; for (i = 2; i <= n; i++) { if (sum > 0) { if (a[i] + sum >= 0) { ans += a[i] + sum + 1; sum = -1; } else { sum += a[i]; } } else { if (a[i] + sum <= 0) { ans += abs(a[i] + sum) + 1; sum = 1; } else { sum += 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
python3
n=int(input()) a= (list(map(int,input().split()))) sm1=sm2=0 cnt1=cnt2=0 for i ,num in enumerate(a): sm1+=num if sm1<=0 and i%2==0: cnt1+=1-sm1 sm1=1 elif sm1>=0 and i%2!=0: cnt1+=1+sm1 sm1=-1 sm2+=num if sm2<=0 and i%2!=0: cnt2+=1-sm2 sm2=1 elif sm2>=0 and i%2==0: cnt2+=1+sm2 sm2=-1 print(min(cnt1,cnt2))
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.*; class Main{ public static void main(String args[]) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] sum = new int[n]; int[] arr = new int[n]; int ans = 0; for(int i = 0; i < n; i++){ arr[i] = sc.nextInt(); } sum[0] = arr[0]; for(int i = 1; i < n; i++){ sum[i] = sum[i-1] + arr[i]; if(sum[i-1] < 0 && sum[i] <= 0){ ans += -sum[i] + 1; sum[i] += -sum[i] + 1; }else if(sum[i-1] > 0 && sum[i] >= 0){ ans += sum[i] + 1; sum[i] -= sum[i] + 1; } // System.out.println(sum[i]); } System.out.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
python3
import sys input = sys.stdin.readline n = int(input()) a = [int(x) for x in input().split()] # スタート+ if a[0] <= 0: A1 = 1 ans1 += abs(a[0]) + 1 else: A1 = a[0] ans1 = 0 for i in range(1, n): nextA = A1 + a[i] if (A1 > 0 and nextA < 0) or (A1 < 0 and nextA > 0): A1 = nextA elif nextA == 0 and A1 > 0: ans1 += 1 A1 = -1 elif nextA == 0 and A1 < 0: ans1 += 1 A1 = 1 elif A1 > 0: ans1 += abs(nextA) + 1 A1 = -1 else: ans1 += abs(nextA) + 1 A1 = 1 # スタート- if a[0] >= 0: ans2 = abs(a[0]) + 1 A2 = -1 else: A2 = a[0] ans2 = 0 for i in range(1, n): nextA = A2 + a[i] if (A2 > 0 and nextA < 0) or (A2 < 0 and nextA > 0): A2 = nextA elif nextA == 0 and A2 > 0: ans2 += 1 A2 = -1 elif nextA == 0 and A2 < 0: ans2 += 1 A2 = 1 elif A2 > 0: ans2 += abs(nextA) + 1 A2 = -1 else: ans2 += abs(nextA) + 1 A2 = 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
python3
def solve(): n = int(input()) total,*A = map(int, input().split()) count = 0 for a in A: if total < 0: if a >= 0: if total + a <= 0: count += -(total + a) + 1 total = 1 else: total += a pass else: count += -(total + a) + 1 total = 1 else: if a <= 0: if total + a >= 0: count += total + a + 1 total = -1 else: total += a pass else: count += total + a + 1 total = -1 return count if __name__ == '__main__': print(solve())
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[110000], sumpl[110000] = {}; int summi[110000] = {}; int mi = 0, pl = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (i == 0) { sumpl[0] = a[i]; summi[0] = a[i]; } else { sumpl[i] = sumpl[i - 1] + a[i]; summi[i] = summi[i - 1] + a[i]; } if (i % 2 == 0) { if (sumpl[i] <= 0) { pl += abs(sumpl[i]) + 1; sumpl[i] = 1; } if (summi[i] >= 0) { mi += abs(summi[i]) + 1; summi[i] = -1; } } else { if (sumpl[i] >= 0) { pl += abs(sumpl[i]) + 1; sumpl[i] = -1; } if (summi[i] <= 0) { mi += abs(summi[i]) + 1; summi[i] = 1; } } } cout << (pl < mi ? pl : mi) << 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 argc, char *argv[]) { cin.tie(0); ios::sync_with_stdio(false); int n, a[100001]; cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } long long int res_even = 0, sum_a = 0; for (int i = 0; i < n; ++i) { sum_a += a[i]; if (i % 2 == 0 && sum_a <= 0) { res_even += abs(sum_a) + 1; sum_a = 1; } if (i % 2 == 1 && sum_a >= 0) { res_even += sum_a + 1; sum_a = -1; } } long long int res_odd = 0; sum_a = 0; for (int i = 0; i < n; ++i) { sum_a += a[i]; if (i % 2 == 0 && sum_a >= 0) { res_odd += sum_a + 1; sum_a = -1; } if (i % 2 == 1 && sum_a <= 0) { res_odd += abs(sum_a) + 1; sum_a += 1; } } cout << min(res_even, res_odd) << 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; signed main() { long long n, a, cnt = 0, sum; cin >> n >> sum; if (sum == 0) { long long cnt1 = 1, cnt2 = 1; sum = 1; for (long long i = 1; i < n; ++i) { cin >> a; if (sum > 0) { sum += a; if (sum >= 0) { cnt1 += sum + 1; sum = -1; } } else { sum += a; if (sum <= 0) { cnt1 += 1 - sum; sum = 1; } } } sum = -1; for (long long i = 1; i < n; ++i) { cin >> a; if (sum > 0) { sum += a; if (sum >= 0) { cnt2 += sum + 1; sum = -1; } } else { sum += a; if (sum <= 0) { cnt2 += 1 - sum; sum = 1; } } } cout << min(cnt1, cnt2) << endl; } else { for (long long i = 1; i < n; ++i) { cin >> a; if (sum > 0) { sum += a; if (sum >= 0) { cnt += sum + 1; sum = -1; } } else { sum += a; if (sum <= 0) { cnt += 1 - sum; sum = 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; int main(void) { long long n, i, j, sw, sw2, count = 0, add = 0; cin >> n; vector<long long> a(n); for (i = 0; i < n; i++) cin >> a[i]; if (a[0] > 0) sw = 1; else sw = -1; add += a[0]; for (i = 1; i < n; i++) { add += a[i]; if (sw == 1) { if (add < 0) { } else { if (a[i] >= 0) { while (add != -1) { a[i]--; add--; count++; } } else { while (add != -1) { a[i]++; add++; count++; } } } } else { if (add > 0) { } else { if (a[i] <= 0) { while (add != 1) { a[i]--; add--; count++; } } else { while (add != 1) { a[i]++; add++; count++; } } } } if (a[i] > 0) sw = 1; else sw = -1; } 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
python3
n = int(input()) a = list(map(int, input().split())) #具体的な操作を考える必要はなくて,ただ部分和をひたすら考えていけばいい answer1 = 0 answer2 = 0 sum1 = a[0] sum2 = a[0] # pmpm... for i in range(n): sum1 += a[i] if i % 2 == 1: if sum1 >= 0: answer1 += sum1 - (-1) sum1 = -1 if i % 2 == 0: if sum1 <= 0: answer1 += 1 - sum1 sum1 = 1 # mpmp... for i in range(n): sum2 += a[i] if i % 2 == 0: if sum2 >= 0: answer2 += sum2 - (-1) sum2 = -1 if i % 2 == 1: if sum2 <= 0: answer2 += 1 - sum2 sum2 = 1 print(min(answer1, answer2))
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 = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; template <typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; } signed main() { long long n; cin >> n; vector<long long> a(n); for (long long(i) = 0; (i) < (long long)(n); (i)++) cin >> a[i]; long long sum = 0; long long prev = 0; sum += a[0]; long long ans = 0; for (long long(i) = (long long)(1); (i) < (long long)(n); (i)++) { prev = sum; sum += a[i]; if (prev * sum < 0) { continue; } else { if (sum > 0) { ans += sum + 1; sum = -1; } else if (sum < 0) { ans += abs(sum) + 1; sum = 1; } else { ans++; sum = (prev < 0 ? 1 : -1); } } } sum = 0; prev = 0; long long ans2 = 0; sum += a[0]; if (sum > 0) { ans2 += sum + 1; sum = -1; } else if (sum < 0) { ans2 += abs(sum) + 1; sum = 1; } else { ans2++; sum = 1; } for (long long(i) = (long long)(1); (i) < (long long)(n); (i)++) { prev = sum; sum += a[i]; if (prev * sum < 0) { continue; } else { if (sum > 0) { ans2 += sum + 1; sum = -1; } else if (sum < 0) { ans2 += abs(sum) + 1; sum = 1; } else { ans2++; sum = (prev < 0 ? 1 : -1); } } } cout << min(ans, 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
python3
N = int(input()) A = list(map(int, input().split())) currentSum = 0 count1 = 0 count2 = 0 for i in range(N): restSum = currentSum currentSum += A[i] if currentSum <= 0 and restSum < 0: count1 += abs(currentSum) + 1 currentSum = 1 elif currentSum >= 0 and restSum > 0: count1 += abs(currentSum) + 1 currentSum = -1 elif currentSum == 0 and restSum == 0: count1 += 1 currentSum = -1 currentSum = 0 for i in range(N): restSum = currentSum currentSum += A[i] if currentSum <= 0 and restSum < 0: count2 += abs(currentSum) + 1 currentSum = 1 elif currentSum >= 0 and restSum > 0: count2 += abs(currentSum) + 1 currentSum = -1 elif currentSum == 0 and restSum == 0: count2 += 1 currentSum = -1 print(min(count1, count2))
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 S<0 and S+c<=0: counter += abs(S+c)+1 S = 1 elif 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; long long INF = (1LL << 62); long long N; vector<long long> A, W; long long S[100002] = {INF * (-1)}; long long dp[100002] = {0}; void calcDP(int n) { if (n == 1) { if (W[1] > 0) { if ((W[2] > 0) && (abs(W[1]) < abs(W[2]))) { dp[1] = abs(-1 - W[1]); W[1] = -1; } else { dp[1] = 0; } } else if (W[1] < 0) { if ((W[2] < 0) && (abs(W[1]) < abs(W[2]))) { dp[1] = abs(1 - W[1]); W[1] = 1; } else { dp[1] = 0; } } else { dp[1] = 1; if (W[2] <= 0) { W[1] = 1; } else { W[1] = -1; } } S[1] = W[1]; return; } else { S[n] = S[n - 1] + W[n]; if ((S[n - 1] < 0 && S[n] > 0) || (S[n - 1] > 0 && S[n] < 0)) { dp[n] = dp[n - 1]; } else { if (S[n - 1] > 0) { dp[n] = dp[n - 1] + abs(-1 - S[n - 1] - W[n]); W[n] = -1 - S[n - 1]; } else { dp[n] = dp[n - 1] + abs(1 - S[n - 1] - W[n]); W[n] = 1 - S[n - 1]; } S[n] = S[n - 1] + W[n]; } return; } } int main(int argc, char* argv[]) { cin.tie(0); ios::sync_with_stdio(false); cin >> N; W.push_back(0); S[0] = 0; for (int i = 1; i <= N; i++) { long long a; cin >> a; A.push_back(a); W.push_back(a); if (i == 1) { S[1] = a; } else { S[i] = S[i - 1] + a; } } for (int i = 1; i <= N; i++) { calcDP(i); } printf("%lld\n", dp[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
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; using System.Numerics; using static System.Math; using static AtCoderTemplate.MyExtensions; using static AtCoderTemplate.MyInputOutputs; using static AtCoderTemplate.MyNumericFunctions; using static AtCoderTemplate.MyAlgorithm; namespace AtCoderTemplate { public class Program { public static void Main (string[] args) { var n = ReadInt (); var a = ReadLongs (); // evenが+ var evenCount = 0L; { var sum0 = a[0] > 0 ? a[0] : 1; var count = a[0] > 0 ? 0 : Abs (a[0]); foreach (var i in Enumerable.Range (1, n - 1)) { var sum1 = sum0 + a[i]; if (IsEven (i)) { if (sum1 < 0) { count += Abs (sum1) + 1; sum0 = 1; } else if (sum1 == 0) { count += 1; sum0 = 1; } else { sum0 = sum1; } } else { if (sum1 > 0) { count += Abs (sum1) + 1; sum0 = -1; } else if (sum1 == 0) { count += 1; sum0 = -1; } else { sum0 = sum1; } } } evenCount = count; } var oddCount = 0L; { var sum0 = a[0] < 0 ? a[0] : -1; var count = a[0] < 0 ? 0 : Abs (a[0]); foreach (var i in Enumerable.Range (1, n - 1)) { var sum1 = sum0 + a[i]; if (IsOdd (i)) { if (sum1 < 0) { count += Abs (sum1) + 1; sum0 = 1; } else if (sum1 == 0) { count += 1; sum0 = 1; } else { sum0 = sum1; } } else { if (sum1 > 0) { count += Abs (sum1) + 1; sum0 = -1; } else if (sum1 == 0) { count += 1; sum0 = -1; } else { sum0 = sum1; } } } oddCount = count; } Print (Min (evenCount, oddCount)); } } public static class MyInputOutputs { /* Input & Output*/ public static int ReadInt () { return int.Parse (Console.ReadLine ()); } public static long ReadLong () { return long.Parse (Console.ReadLine ()); } public static List<int> ReadInts () { return Console.ReadLine ().Split (' ').Select (c => int.Parse (c)).ToList (); } public static List<long> ReadLongs () { return Console.ReadLine ().Split (' ').Select (c => long.Parse (c)).ToList (); } public static List<List<int>> ReadIntColumns (int n) { /* 入力例 A1 B1 A2 B2 ... An Bn 出力例 [[A1,A2,...,An], [B1,B2,...,Bn]] */ var rows = Enumerable.Range (0, n).Select (i => ReadInts ()).ToList (); var m = rows.FirstOrDefault ()?.Count () ?? 0; return Enumerable.Range (0, m).Select (i => rows.Select (items => items[i]).ToList ()).ToList (); } public static List<List<long>> ReadLongColumns (int n) { /* 入力例 A1 B1 A2 B2 ... An Bn 出力例 [[A1,A2,...,An], [B1,B2,...,Bn]] */ var rows = Enumerable.Range (0, n).Select (i => ReadLongs ()).ToList (); var m = rows.FirstOrDefault ()?.Count () ?? 0; return Enumerable.Range (0, m).Select (i => rows.Select (items => items[i]).ToList ()).ToList (); } public static void Print<T> (T item) { Console.WriteLine (item); } public static void PrintIf<T1, T2> (bool condition, T1 trueResult, T2 falseResult) { if (condition) { Console.WriteLine (trueResult); } else { Console.WriteLine (falseResult); } } public static void PrintRow<T> (IEnumerable<T> list) { /* 横ベクトルで表示 A B C D ... */ if (!list.IsEmpty ()) { Console.Write (list.First ()); foreach (var item in list.Skip (1)) { Console.Write ($" {item}"); } } Console.Write ("\n"); } public static void PrintColomn<T> (IEnumerable<T> list) { /* 縦ベクトルで表示 A B C D ... */ foreach (var item in list) { Console.WriteLine (item); } } public static void Print2DArray<T> (IEnumerable<IEnumerable<T>> sources) { foreach (var row in sources) { PrintRow (row); } } } public static class MyNumericFunctions { public static bool IsEven (int a) { return a % 2 == 0; } public static bool IsEven (long a) { return a % 2 == 0; } public static bool IsOdd (int a) { return !IsEven (a); } public static bool IsOdd (long a) { return !IsEven (a); } /// <summary> /// 順列の総数を得る /// O(N-K) /// </summary> /// <param name="n">全体の数</param> /// <param name="k">並べる数</param> /// <param name="divisor">返り値がlongを超えないようにdivisorで割った余りを得る</param> /// <returns>nPk (をdivisorで割った余り)</returns> public static long nPk (int n, int k, long divisor) { if (k > n) { return 0L; } else { return Enumerable.Range (n - k + 1, k).Aggregate (1L, ((i, m) => (i * m) % divisor)); } } public static long nPk (int n, int k) { if (k > n) { return 0L; } else { return Enumerable.Range (n - k + 1, k).Aggregate (1L, ((i, m) => (i * m))); } } /// <summary> /// 階乗を得る /// O(N) /// </summary> /// <param name="n"></param> /// <param name="divisor">返り値がlongを超えないようにdivisorで割った余りを得る</param> /// <returns>n! (をdivisorで割った余り)</returns> public static long Fact (int n, long divisor) { return nPk (n, n, divisor); } public static long Fact (int n) { return nPk (n, n); } /// <summary> /// 組み合わせの総数を得る /// </summary> /// <param name="n"></param> /// <param name="k"></param> /// <returns>nCk</returns> public static long nCk (int n, int k) { if (k > n) { return 0L; } else { return nPk (n, k) / Fact (k); } } /// <summary> /// 最大公約数を得る /// O(log N) /// </summary> /// <param name="m">自然数</param> /// <param name="n">自然数</param> /// <returns></returns> public static long GCD (long m, long n) { // GCD(m,n) = GCD(n, m%n)を利用 // m%n = 0のとき、mはnで割り切れるので、nが最大公約数 if (m <= 0L || n <= 0L) throw new ArgumentOutOfRangeException (); if (m < n) return GCD (n, m); while (m % n != 0L) { var n2 = m % n; m = n; n = n2; } return n; } /// <summary> /// 最小公倍数を得る /// O(log N) /// </summary> /// <param name="m"></param> /// <param name="n"></param> /// <returns></returns> public static long LCM (long m, long n) { var ans = checked ((long) (BigInteger.Multiply (m, n) / GCD (m, n))); return ans; } /// <summary> /// 約数列挙(非順序) /// O(√N) /// </summary> /// <param name="m">m > 0</param> /// <returns></returns> public static IEnumerable<long> Divisor (long m) { if (m == 0) throw new ArgumentOutOfRangeException (); var front = Enumerable.Range (1, (int) Sqrt (m)) .Select (i => (long) i) .Where (d => m % d == 0); return front.Concat (front.Where (x => x * x != m).Select (x => m / x)); } /// <summary> /// 公約数列挙(非順序) /// O(√N) /// </summary> /// <param name="m">m > 0</param> /// <param name="n">n > 0</param> /// <returns></returns> public static IEnumerable<long> CommonDivisor (long m, long n) { if (m < n) return CommonDivisor (n, m); return Divisor (m).Where (md => n % md == 0); } } public static class MyAlgorithm { /// <summary> /// 二分探索法 /// O(log N) /// </summary> /// <param name="list">探索するリスト</param> /// <param name="predicate">条件の述語関数</param> /// <param name="ng">条件を満たさない既知のindex</param> /// <param name="ok">条件を満たす既知のindex</param> /// <typeparam name="T">順序関係を持つ型(IComparableを実装する)</typeparam> /// <returns>条件を満たすindexの内、境界に最も近いものを返す</returns> public static int BinarySearch<T> (IList<T> list, Func<T, bool> predicate, int ng, int ok) where T : IComparable<T> { while (Abs (ok - ng) > 1) { int mid = (ok + ng) / 2; if (predicate (list[mid])) { ok = mid; } else { ng = mid; } } return ok; } /// <summary> /// 辺の集まりを操作するオブジェクト /// </summary> public class Edge { long[, ] edge; public int NodeNum { get; } public Edge (int nodeNum, long overDistance) { var edge = new long[nodeNum, nodeNum]; foreach (var i in Enumerable.Range (0, nodeNum)) { foreach (var j in Enumerable.Range (0, nodeNum)) { if (i != j) { edge[i, j] = overDistance; } else { edge[i, j] = 0; } } } this.edge = edge; this.NodeNum = nodeNum; } public Edge (Edge edge) { this.edge = new long[edge.NodeNum, edge.NodeNum]; foreach (var i in Enumerable.Range (0, edge.NodeNum)) { foreach (var j in Enumerable.Range (0, edge.NodeNum)) { this.edge[i, j] = edge.GetLength (i, j); } } this.NodeNum = edge.NodeNum; } public List<List<long>> ToList () { return Enumerable.Range (0, NodeNum).Select (i => Enumerable.Range (0, NodeNum).Select (j => edge[i, j] ).ToList () ).ToList (); } public void Add (int node1, int node2, long distance) { edge[node1, node2] = distance; } public long GetLength (int node1, int node2) { return edge[node1, node2]; } } /// <summary> /// ワーシャルフロイド法 /// O(N^3) /// </summary> /// <param name="edge">Edgeオブジェクト</param> /// <param name="nodeNum">ノードの数</param> /// <returns>各ノード間の最短距離を辺として持つEdgeオブジェクト</returns> public static Edge WarshallFloyd (Edge edge) { var res = new Edge (edge); foreach (var b in Enumerable.Range (0, edge.NodeNum)) { foreach (var a in Enumerable.Range (0, edge.NodeNum)) { foreach (var c in Enumerable.Range (0, edge.NodeNum)) { res.Add (a, c, Min (res.GetLength (a, c), res.GetLength (a, b) + res.GetLength (b, c))); } } } return res; } } public static class MyExtensions { // AppendとPrependが、.NET Standard 1.6からの追加で、Mono 4.6.2 はそれに対応して仕様はあるが、実装がない public static IEnumerable<T> Append<T> (this IEnumerable<T> source, T element) { return source.Concat (Enumerable.Repeat (element, 1)); } public static IEnumerable<T> Prepend<T> (this IEnumerable<T> source, T element) { return Enumerable.Repeat (element, 1).Concat (source); } // TakeLastとSkipLastが、.Net Standard 2.1からの追加で、Mono 4.6.2 はそれに対応していない public static IEnumerable<T> TakeLast<T> (this IEnumerable<T> source, int count) { return source.Skip (source.Count () - count); } public static IEnumerable<T> SkipLast<T> (this IEnumerable<T> source, int count) { return source.Take (source.Count () - count); } public static bool IsEmpty<T> (this IEnumerable<T> source) { return !source.Any (); } /// <summary> /// インデックスiの位置の要素からk個取り除く /// O(N) /// </summary> public static IEnumerable<T> TakeAwayRange<T> (this IEnumerable<T> source, int i, int count) { return source.Take (i).Concat (source.Skip (i + count)); } /// <summary> /// インデックスiの位置の要素を取り除く /// O(N) /// </summary> public static IEnumerable<T> TakeAwayAt<T> (this IEnumerable<T> source, int i) { return source.TakeAwayRange (i, 1); } /// <summary> /// インデックスiの位置にシーケンスを挿入する /// O(N + K) /// </summary> public static IEnumerable<T> InsertEnumAt<T> (this IEnumerable<T> source, int i, IEnumerable<T> inserted) { return source.Take (i).Concat (inserted).Concat (source.Skip (i)); } /// <summary> /// 順列を得る /// O(N!) /// </summary> public static IEnumerable<IEnumerable<T>> Perm<T> (this IEnumerable<T> source, int n) { if (n == 0 || source.IsEmpty () || source.Count () < n) { return Enumerable.Empty<IEnumerable<T>> (); } else if (n == 1) { return source.Select (i => new List<T> { i }); } else { var nexts = source.Select ((x, i) => new { next = source.Take (i).Concat (source.Skip (i + 1)), selected = source.Take (i + 1).Last () }); return nexts.SelectMany (next => Perm (next.next, n - 1).Select (item => item.Prepend (next.selected))); } } /// <summary> /// シーケンスの隣り合う要素を2引数の関数に適用したシーケンスを得る /// </summary> /// <para>O(N)</para> /// <param name="source">元のシーケンス</param> /// <param name="func">2引数関数</param> /// <example>[1,2,3,4].MapAdjacent(f) => [f(1,2), f(2,3), f(3,4)]</example> public static IEnumerable<TR> MapAdjacent<T1, TR> (this IEnumerable<T1> source, Func<T1, T1, TR> func) { var list = source.ToList (); return Enumerable.Range (1, list.Count - 1) .Select (i => func (list[i - 1], list[i])); } /// <summary> /// 累積項を要素にもつシーケンスを得る(初項は、first) /// <para>O(N)</para> /// </summary> /// <param name="source">元のシーケンス</param> /// <param name="func">2引数関数f</param> /// <param name="first">func(first, source[0])のための初項</param> /// <example> [1,2,3].Scanl1(f,0) => [0, f(0,1), f(f(0,1),2), f(f(f(0,1),2),3)]</example> public static IEnumerable<TR> Scanl<T, TR> (this IEnumerable<T> source, TR first, Func<TR, T, TR> func) { var list = source.ToList (); var result = new List<TR> { first }; foreach (var i in Enumerable.Range (0, source.Count ())) { result.Add (func (result[i], list[i])); } return result; } /// <summary> /// 累積項を要素にもつシーケンスを得る(初項は、source.First()) /// <para>O(N)</para> /// </summary> /// <param name="source">元のシーケンス</param> /// <param name="func">2引数関数f</param> /// <example> [1,2,3].Scanl1(f) => [1, f(1,2), f(f(1,2),3)]</example> public static IEnumerable<T> Scanl1<T> (this IEnumerable<T> source, Func<T, T, T> func) { var list = source.ToList (); var result = new List<T> { list[0] }; foreach (var i in Enumerable.Range (1, source.Count () - 1)) { result.Add (func (result[i - 1], list[i])); } return result; } /// <summary> /// 昇順にソートしたインデックスを得る /// </summary> /// <para>O(N * log N)</para> public static IEnumerable<int> SortIndex<T> (this IEnumerable<T> source) { return source .Select ((item, i) => new { Item = item, Index = i }) .OrderBy (x => x.Item) .Select (x => x.Index); } } }
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 dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int n; vector<long long> a(200000); long long solve(vector<long long> a) { long long ans = 0, sum = a[0]; for (int i = 1; i < n; i++) { long long tmp = sum; sum += a[i]; if (sum > 0 && tmp > 0) { ans += abs(sum) + 1; sum = -1; } else if (sum < 0 && tmp < 0) { ans += abs(sum) + 1; sum = 1; } } return ans; } int main() { cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; long long ans1 = solve(a); a[0] = (-1) * a[0]; long long ans2 = solve(a); 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; int main() { int N; cin >> N; vector<long long> L(N); for (int i = 0; i < N; i++) { cin >> L.at(i); } long long v = 0, res = 0, le = 0; bool change_flag = true; for (int i = 0; i < N; i++) { if (i == 0) { v = L.at(i); } else { if (v > 0 && v + L.at(i) > 0) { le = -1 - v - L.at(i); L.at(i) += le; v += L.at(i); res += -le; le = 0; } else if (v > 0 && v + L.at(i) == 0) { le = -1; L.at(i) += le; v += L.at(i); res += -le; le = 0; } else if (v < 0 && v + L.at(i) <= 0) { le = 1 - v - L.at(i); L.at(i) += le; v += L.at(i); res += le; le = 0; } else if (v < 0 && v + L.at(i) == 0) { le = 1; L.at(i) += le; v += L.at(i); res += le; le = 0; } else { v += L.at(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
python3
n = int(input()) a = list(map(int, input().split())) now_a = a[0] count = 0 ''' True = positive False = negative ''' # sign = True # if now_a < 0: # sign = False # # for i in range(1, n): # next_a = now_a + a[i] # if sign: # if next_a >= 0: # count += next_a + 1 # now_a = -1 # else: # now_a = next_a # sign = False # else: # if next_a <= 0: # count += abs(next_a) + 1 # now_a = 1 # else: # now_a = next_a # sign = True # print(count) sa = a[0] sign = True if sa < 0: sign = False for i in range(1, n): na = sum(a[:i+1]) if sign and na >= 0: a[i] = -1 * (na + 1) count += na + 1 elif not sign and na <= 0: a[i] = 1 - na count += 1 - na print(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
UNKNOWN
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; namespace ABC059C { class Program { // -1 4 3 2 -5 4 // -1 4 -4 2 -5 5 -> 8 static long sequentialize(int[] a, bool firstPlus) { var n = a.Length; var changeCount = 0L; if (firstPlus && a[0] <= 0) { changeCount += 1 - a[0]; a[0] = 1; } else if (!firstPlus && a[0] >= 0) { changeCount += -1 - a[0]; a[0] = -1; } var sum = a[0]; int v = a[0] > 0 ? 1 : -1; for (int i = 1; i < n; i++) { var temp = sum + a[i]; if (sum > 0 && temp > 0) { // tempを-1にしたい var prev = a[i]; a[i] = -sum - 1; changeCount += Math.Abs(prev - a[i]); } else if (sum < 0 && temp < 0) { // tempを+1にしたい var prev = a[i]; a[i] = -sum + 1; changeCount += Math.Abs(prev - a[i]); } else if (temp == 0) { changeCount += 1; a[i] = v; } sum += a[i]; v = -v; } return changeCount; } static void Solve() { var n = Input.NextInt(); var a = Input.NextInt(n).ToArray(); var b = new int[n]; a.CopyTo(b, 0); var changeCount1 = sequentialize(a, true); // 先頭の符号反転 var changeCount2 = sequentialize(b, false); Console.WriteLine(Math.Min(changeCount1, changeCount2)); } #region Competitive Template public static void Main(string[] args) { var needsFlushOutput = true; if (needsFlushOutput) { // 細かく出力しないようにする var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); } // 仮想的に標準入力をセットする // NextLine系を使っていると使えない //Input.SetText(""); Solve(); Console.Out.Flush(); } static class Input { static char[] separator = { ' ' }; public static bool IsEof { get; set; } static Queue<string> q { get; set; } static Input() { IsEof = false; q = new Queue<string>(); } /// <summary> /// 入力予約 /// </summary> /// <param name="items"></param> public static void SetText(IEnumerable<string> items) { foreach (var item in items) { SetText(item); } } /// <summary> /// 入力予約 /// </summary> /// <param name="s"></param> /// <returns></returns> public static bool SetText(string s) { if (s == null) return false; foreach (var elem in s.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries)) { q.Enqueue(elem); } return true; } /// <summary> /// 内部queueに入力からの値をsplitして格納する /// </summary> /// <returns></returns> static bool read() { var s = Console.ReadLine(); if (s == null) return false; foreach (var elem in s.Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries)) { q.Enqueue(elem); } if (!q.Any()) return read(); return true; } /// <summary> /// 次のstringを一つ読み込む /// </summary> /// <returns></returns> public static string Next() { if (!q.Any()) { if (!read()) { IsEof = true; return ""; } } return q.Dequeue(); } public static int NextInt() => int.Parse(Next()); public static long NextLong() => long.Parse(Next()); public static double NextDouble() => double.Parse(Next()); public static List<string> Next(int n) => Enumerable.Range(0, n).Select(_ => Next()).ToList(); public static List<int> NextInt(int n) => Next(n).Select(x => int.Parse(x)).ToList(); public static List<long> NextLong(int n) => Next(n).Select(x => long.Parse(x)).ToList(); public static List<double> NextDouble(int n) => Next(n).Select(x => double.Parse(x)).ToList(); public static List<string> NextLine() => Console.ReadLine().Trim().Split(separator, StringSplitOptions.RemoveEmptyEntries).ToList(); public static List<int> NextIntLine() => NextLine().Select(x => int.Parse(x)).ToList(); public static List<long> NextLongLine() => NextLine().Select(x => long.Parse(x)).ToList(); public static List<double> NextDoubleLine() => NextLine().Select(x => double.Parse(x)).ToList(); } #endregion } }
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+=key*-1+1; key=1; } if(key==0) { cun+=1; key+=1; } } else { if(key>0) { cun+=key+1; key=-1; } if(key==0) { cun+=1; key-=1; } } } else { if(z%2==1) { if(key<0) { cun+=key*-1+1; key+=1; } if(key==0) { cun+=1; key+=1; } } else { if(key>0) { cun+=key+1; 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()) a = list(map(int,input().split())) total=0 for i in range(1,n): count = 0 if sum(a[:i]) < 0: if a[i] > abs(sum(a[:i])): pass else: count += (abs(sum(a[:i])) - a[i] + 1) a[i] += count total += count if sum(a[:i]) > 0: if a[i] < (-1)*abs(sum(a[:i])): pass else: count += (abs(-1*(abs(sum(a[:i]))) - a[i] ) + 1) a[i] -= count total += count print(total)
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
def f(a) g = lambda{|car, cdr, total=0| sum = car r = [car] cdr.each{|curr| case sum <=> 0 when 1; r << new_curr = [curr, -sum-1].min sum += new_curr total += curr - new_curr when -1; r << new_curr = [curr, -sum+1].max sum += new_curr total += new_curr - curr end } p r total += 1 if sum == 0 total } x = g.(a[0], a[1..-1]) y = g.(a[0] > 0 ? -1 : 1, a[1..-1], a[0].abs+1) [x, y].min end N = gets.to_i A = gets.split.take(N).map(&:to_i) p f(A)
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; __attribute__((constructor)) void initial() { cin.tie(0); ios::sync_with_stdio(false); } int main() { int N; cin >> N; vector<int> a; for (int i = 0; i < (N); i++) { int ai; cin >> ai; a.push_back(ai); } int changeCount = 0; int sum = 0; bool nextSumPositive = a[0] > 0; for (int i = 0; i < (N); i++) { sum += a[i]; if (nextSumPositive) { if (sum <= 0) { int change = -sum + 1; changeCount += abs(change); sum += change; } } else { if (sum >= 0) { int change = -sum - 1; changeCount += abs(change); sum += change; } } nextSumPositive = !nextSumPositive; } cout << changeCount << 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())) res = 0 if a[0]==0: res += 1 if a[1]>=0: a[0] = -1 else: a[0] = 1 total = a[0] hugou = total > 0 for i in a[1:]: total += i if hugou == (total>0): res += abs(total)+1 if hugou: total = -1 else: total = 1 hugou = total>0 print(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
cpp
#include <bits/stdc++.h> using namespace std; int main() { long n; cin >> n; vector<long> a(n + 1); for (long i = 1; i <= n; i++) cin >> a.at(i); long ans = 0; for (long i = 1; i <= n - 1; i++) { if (abs(a.at(i + 1)) > abs(a.at(i)) && a.at(i + 1) * a.at(i) < 0) { a.at(i + 1) += a.at(i); } else { ans += abs(a.at(i + 1) - ((abs(a.at(i)) + 1) * (-1) * a.at(i) / abs(a.at(i)))); a.at(i + 1) = (-1) * a.at(i) / abs(a.at(i)); } } 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 calc(int a0, vector<int>& a) { int sum = a0; int count = 0; for (int i = (1); i < (a.size()); ++i) { if (sum < 0) { sum += a[i]; if (sum <= 0) { count += 1 - sum; sum = 1; } continue; } sum += a[i]; if (sum >= 0) { count += sum + 1; sum = -1; } } return count; } int main() { ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; vector<int> a(N); for (auto& ai : a) cin >> ai; int count = 0; if (a[0] == 0) { auto count1 = calc(1, a) + 1; auto count2 = calc(-1, a) + 1; count = min(count1, count2); } else { auto count1 = calc(a[0], a); auto count2 = calc(1, a) + abs(1 - a[0]); auto count3 = calc(-1, a) + abs(-1 - a[0]); count = min(count1, min(count2, count3)); } 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
python3
def main(): n=int(input()) a=list(map(int,input().split(' '))) sumcum = a[0] ans = 0 if a[0] == 0: a[0] = a[1]/abs(a[1]) ans += 1 sumcum += a[0] tmp = a[0] for i in a[1:]: if sumcum*(sumcum+i) <0: sumcum += i continue else: ans += abs(sumcum+i)+1 sumcum = -1*sumcum/abs(sumcum) print(int(ans)) if __name__ == '__main__': main()
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; long long l1[n + 1]; long long x = 0, s = 0; for (int i = 1; i <= n; i++) { cin >> l1[i]; x += l1[i]; if (i == 0 && l1[i] == 0) x++, s++; if (i >= 2) { if (x - l1[i] < 0 && x <= 0) { s += abs((-x + l1[i] + 1) - l1[i]); l1[i] = l1[i] - x + 1; x = 1; } else if (x - l1[i] > 0 && x >= 0) { s += abs(-(x - l1[i] + 1) - l1[i]); l1[i] = -(x - l1[i] + 1); x = -1; } } } cout << s << 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; const long long mod = 1000000007; long long n; vector<long long> a; signed main() { ios::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; vector<long long> a(n); for (long long i = (0); i < (n); i++) cin >> a[i]; vector<long long> b(n); b[0] = a[0]; for (long long i = (1); i < (n); i++) b[i] = b[i - 1] + a[i]; long long res1 = 0, tmp = 0; for (long long i = (0); i < (n); i++) { long long c = b[i] + tmp; if (i % 2 == 0) { if (c > 0) continue; res1 += 1 - c; tmp += 1 - c; } else { if (c < 0) continue; res1 += c + 1; tmp -= c + 1; } } long long res2 = 0; tmp = 0; for (long long i = (0); i < (n); i++) { long long c = b[i] + tmp; if (i % 2 == 0) { if (c < 0) continue; res2 += c + 1; tmp -= c + 1; } else { if (c > 0) continue; res2 += 1 - c; tmp += 1 - c; } } cout << res1 << " " << res2 << endl; cout << min(res1, res2) << 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
#k = int(input()) #s = input() #a, b = map(int, input().split()) #s, t = map(str, input().split()) #l = list(map(int, input().split())) #l = [list(map(int,input().split())) for i in range(n)] #a = [input() for _ in range(n)] import copy n = int(input()) a = list(map(int, input().split())) b = copy.copy(a) lastSum = a[0] nowSum = a[0] for i in range(1, n): nowSum += a[i] #print(nowSum) if lastSum > 0: if nowSum >=0: a[i] = a[i] - (nowSum+1) nowSum += a[i] - b[i] else: #lastSum < 0 if nowSum <= 0: a[i] = a[i] - (nowSum-1) nowSum += a[i] - b[i] lastSum += a[i] #print(a) ans = 0 for i in range(n): ans += abs(a[i]-b[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
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoder { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); int[] a = new int[n]; string[] lines = Console.ReadLine().Split(' '); for (int i = 0; i < n; i++) { a[i] = int.Parse(lines[i]); } int ans = 0; int diff = 0; int sign = (a[0] > 0 ? 1 : -1); int sum = a[0]; for (int i = 1; i < n; i++) { sum += a[i]; if ((sign == 1) && (sum > 0)) { diff = +(sum + 1); ans += diff; sum -= diff; } else if ((sign == -1) && (sum < 0)) { diff = -(sum - 1); ans += diff; sum += diff; } else if (sum == 0) { if (sign == 1) { sum--; ans++; } else { sum++; ans++; } } sign = -sign; } Console.WriteLine(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 sign, n, a, cnt = 0, acm = 0, ans = 0; cin >> n; int A[n]; for (int i = 0; i < n; i++) cin >> A[i]; sign = 1; for (int i = 0; i < n; i++) { if ((acm + A[i]) * sign < 0) acm += A[i]; else { cnt += abs(acm + A[i]) + 1; acm = -1 * sign; } sign *= -1; } ans = cnt; sign = -1; acm = 0; cnt = 0; for (int i = 0; i < n; i++) { if ((acm + A[i]) * sign < 0) acm += A[i]; else { cnt += abs(acm + A[i]) + 1; acm = -1 * sign; } sign *= -1; } ans = min(ans, cnt); cout << ans << "\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
UNKNOWN
program ec12; var a,s:array[0..100000] of longint; n,m,i,j,ans:longint; begin readln(n); ans:=0; s[0]:=0; for i:=1 to n do read(a[i]); for i:=1 to n do begin s[i]:=s[i-1]+a[i]; if i>1 then begin if s[i-1]<0 then begin if s[i]<=0 then begin if s[i]=0 then begin inc(ans); s[i]:=1; end else inc(ans,(-s[i])+1); end; end else begin if s[i]>=0 then begin if s[i]=0 then begin inc(ans); s[i]:=-1; end else begin inc(ans,s[i]+1); s[i]:=-1; end; end; end; end else inc(ans,abs(a[2]-a[1])+1); end; writeln(ans); 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
cpp
#include <bits/stdc++.h> static std::uint64_t solve(const std::vector<int>& va, int initSum, std::uint64_t initCnt = 0) { int sum = initSum; std::uint64_t cnt = initCnt; for (std::remove_reference<decltype(va)>::type::size_type i = 1; i < va.size(); i++) { auto nextSum = sum + va[i]; if (nextSum >= 0 && sum > 0) { cnt += nextSum + 1; sum = -1; } else if (nextSum <= 0 && sum < 0) { cnt += -nextSum + 1; sum = 1; } else { sum = nextSum; } } return cnt; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int n; std::cin >> n; std::vector<int> va(n); for (auto&& e : va) { std::cin >> e; } std::cout << std::min(solve(va, va[0]), solve(va, va[0] > 0 ? -1 : 1, std::abs(va[0]) + 1)) << std::endl; return EXIT_SUCCESS; }
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; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < (int)(n); i++) cin >> a[i]; long long prevArraySum, currentArraySum; long long res, res1 = 1e15; for (int first = 0; first < (int)(2); first++) { if (first % 2 && a[0] == 0) { res = 1; prevArraySum = -1; currentArraySum = -1; } else if (first % 2 == 0 && a[0] == 0) { res = 1; prevArraySum = 1; currentArraySum = 1; } else if (first % 2) { res = 0; prevArraySum = a[0]; currentArraySum = a[0]; } else if (a[0] > 0) { res = a[0] + 1; prevArraySum = -1; currentArraySum = -1; } else { res = -a[0] + 1; prevArraySum = -1; currentArraySum = -1; } for (int i = (1); i < (n); ++i) { if (prevArraySum > 0) { currentArraySum = prevArraySum + a[i]; if (currentArraySum >= 0) { res += abs(-1 - currentArraySum); prevArraySum = -1; } else { prevArraySum = currentArraySum; } } else { currentArraySum = prevArraySum + a[i]; if (currentArraySum <= 0) { res += abs(1 - currentArraySum); prevArraySum = 1; } else { prevArraySum = currentArraySum; } } } res1 = min(res, res1); } cout << res1 << 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; long long a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } long long 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 - s1 - 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 - s2 - a[i]; s2 = 1; } else s2 += a[i]; } } cout << min(a1, a2) << 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())) ans = 0 cur = a[0] if a[0] > 0: for i in range(1, n): cun = a[i] # iが偶数のときプラス if i % 2 == 0: # プラスじゃないとき if a[i] + cur <= 0: ans += -(a[i] + cur) + 1 cun = -cur + 1 # iが奇数のときマイナス else: # マイナスじゃないとき if a[i] + cur >= 0: ans += a[i] + cur + 1 cun = -cur - 1 cur += cun else: for i in range(1, n): cun = a[i] # iが偶数のときマイナス if i % 2 == 0: # マイナスじゃないとき if a[i] + cur >= 0: ans += a[i] + cur + 1 cun = -cur - 1 # iが奇数のときプラス else: # プラスじゃないとき if a[i] + cur <= 0: ans += -(a[i] + cur) + 1 cun = -cur + 1 cur += cun 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() { int N; cin >> N; vector<int> S(N + 1); for (int i = 1; i <= N; ++i) { cin >> S[i]; S[i] += S[i - 1]; } int ians = (1 << 30); for (int j = -1; j <= 1; j += 2) { int ans = 0; int add = 0; int sign = j; for (int i = 1; i <= N; ++i) { S[i] += add; int sign_i = ((S[i] >> 31) << 1) + 1; if (sign_i == sign) { ans += abs(-sign_i - S[i]); add += -sign_i - S[i]; S[i] = -sign_i; sign_i = -sign_i; } else if (S[i] == 0) { ans += 1; add += -sign; S[i] += -sign; sign_i = -sign; } sign = sign_i; } ians = min(ans, ians); } cout << ians << 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); for (int i = 0; i < n; i++) cin >> a[i]; int now = 0; int ans1 = 0; for (int i = 0; i < n; i++) { now += a[i]; if (i % 2 == 0) { if (now < 0) { ans1 += 1 - now; now = 1; } } if (i % 2 == 1) { if (now > 0) { ans1 += now + 1; now = -1; } } } now = 0; int ans2 = 0; for (int i = 0; i < n; i++) { now += a[i]; if (i % 2 == 1) { if (now <= 0) { ans2 += 1 - now; now = 1; } } if (i % 2 == 0) { if (now >= 0) { ans2 += now + 1; now = -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
cpp
#include <bits/stdc++.h> using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; void solve() {} int main() { int n; vector<int> a, a2; cin >> n; int x; for (int i = 0; i < n; i++) { cin >> x; a.push_back(x); } bool pl = false; bool mi = false; bool zero = false; if (a[0] > 0) { pl = true; } else if (a[0] < 0) { mi = true; } else { zero = true; } int sum = a[0]; int sum2 = a[0]; int ans = 0; int ans2 = 0; if (zero) { copy((a).begin(), (a).end(), back_inserter(a2)); for (int i = 1; i < n; i++) { sum += a[i]; if (i % 2 == 1) { if (sum >= 0) { int tmp = a[i]; a[i] -= sum + 1; ans += sum + 1; sum -= tmp; sum += a[i]; } } else { if (sum <= 0) { int tmp = a[i]; a[i] += (-1) * sum + 1; ans += (-1) * sum + 1; sum -= tmp; sum += a[i]; } } } for (int i = 1; i < n; i++) { sum2 += a2[i]; if (i % 2 == 1) { if (sum <= 0) { int tmp = a2[i]; a2[i] += (-1) * sum + 1; ans2 += (-1) * sum + 1; sum -= tmp; sum += a2[i]; } } else { if (sum >= 0) { int tmp = a2[i]; a2[i] -= sum + 1; ans2 += sum + 1; sum -= tmp; sum += a2[i]; } } } if (ans2 < ans) ans = ans2; } for (int i = 1; i < n; i++) { sum += a[i]; if (pl) { if (i % 2 == 1) { if (sum >= 0) { int tmp = a[i]; a[i] -= sum + 1; ans += sum + 1; sum -= tmp; sum += a[i]; } } else { if (sum <= 0) { int tmp = a[i]; a[i] += (-1) * sum + 1; ans += (-1) * sum + 1; sum -= tmp; sum += a[i]; } } } else if (mi) { if (i % 2 == 1) { if (sum <= 0) { int tmp = a[i]; a[i] += (-1) * sum + 1; ans += (-1) * sum + 1; sum -= tmp; sum += a[i]; } } else { if (sum >= 0) { int tmp = a[i]; a[i] -= sum + 1; ans += sum + 1; sum -= tmp; sum += 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
python3
n=int(input()) a=list(map(int,input().split())) b=[] for i in range(n): b.append(a[i]) ct1=0 if a[0]<=0: a[0]=1 ct1+=1-a[0] x=a[0] for i in range(1,n): y=x+a[i] if i%2==1: if y>=0: ct1+=y+1 a[i]=-x-1 else: if y<=0: ct1+=1-y a[i]=-x+1 x+=a[i] ct2=0 if b[0]>=0: b[0]=-1 ct2+=b[0]-1 x=b[0] for i in range(1,n): y=x+b[i] if i%2==0: if y>=0: ct2+=y+1 b[i]=-x-1 else: if y<=0: ct2+=1-y b[i]=-x+1 x+=b[i] print(min(ct1,ct2))
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; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } template <class T> inline T sqr(T x) { return x * x; } const double EPS = 1e-10; const double PI = acos(-1.0); const long long INF = 1000000007; template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } int main(void) { int N; cin >> N; vector<long long> a(N); for (int i = (0); i < (N); ++i) cin >> a[i]; vector<long long> s(N); s[0] = a[0]; long long ans = 0; for (int i = (0); i < (N - 1); ++i) { if ((s[i] + a[i + 1]) * s[i] >= 0) { ans += abs(s[i] + a[i + 1]) + 1; a[i + 1] += -(s[i] + a[i + 1]); if (s[i] > 0) a[i + 1]--; else a[i + 1]++; } s[i + 1] = s[i] + a[i + 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 a[100001]; int n; cin >> n; for (int i = 0; i < (int)n; i++) { cin >> a[i]; } int c1 = 0, c2 = 0; int sum = 0; int c = 1; for (int i = 0; i < (int)n; i++) { sum += a[i]; if (sum * c <= 0) { c1 += (abs(sum) + 1); sum = c; } c *= (-1); } c = -1; sum = 0; for (int i = 0; i < (int)n; i++) { sum += a[i]; if (sum * c <= 0) { c2 += abs(sum) + 1; sum = c; } c *= (-1); } cout << min(c1, c2) << 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 <algorithm> #include <iostream> #include <iomanip> #include <cstring> #include <cstdlib> #include <utility> #include <cstdio> #include <vector> #include <string> #include <queue> #include <stack> #include <cmath> #include <set> #include <map> using ll = long long; using itn = int; using namespace std; int GCD(int a, int b){ return b ? GCD(b, a%b) : a; } int main() { int n; cin >> n; int a[n]; for(int i=0; i<n; i++){ cin >> a[i]; } int asum[n+1]={}; for(int i=0; i<n; i++){ asum[i+1] = asum[i]+a[i]; } int cnt=0; int accSum=0; for(int i=0; i<n+1; i++){ cout << i << " " <<asum[i] << endl; } for(int i=1; i<n; i++){ asum[i+1]+=accSum; if(asum[i+1]*asum[i]>0){ int s=abs(asum[i+1])+1; cnt+=s; asum[i+1]<0 ? accSum+=s : accSum+=-1*s; asum[i+1]<0 ? asum[i+1]=1 : asum[i+1]=-1; }else if(asum[i+1]*asum[i]==0){ cnt+=1; asum[i]<0 ? asum[i+1]=1,accSum+=1 : asum[i+1]=-1,accSum=-1; } } 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> #define pb push_back #define mp make_pair #define fst first #define snd second #define all(cont) cont.begin(), cont.end() #define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++) #define fore(i,a,b) for(int i=a,almo5t=b;i<almo5t;++i) #define SZ(x) ((int)x.size()) #define EPS 1e-9 #define PI 3.1415926535897932384626433832795 #define MOD 1000000007 #define FIN std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) const int N = 0; typedef long long int ll; using namespace std; int opinions[15][15]; int main(){ int n;cin>>n; ll values[n] = {0}; ll sums[n+1] = {0}; sums[n+1] = 0; bool flag = true; int start = n-1; fore(i,0,n){ ll val;cin>>val; values[i] = val; sums[i+1] = val + sums[i]; if(val != 0 && flag){ start = i;flag = false; } } ll ans = 0; if(start != 0){ if(sums[start+1]>0){ sums[start] = -1; }else if(sums[start+1]<0){ sums[start] = 1; } ans = 1 + 2*(start-1); } /* fore(i,0,n){ cout<<sums[i+1]<<" "; } cout<<"\n";*/ fore(i,start,n){ sums[i+1] = sums[i]+values[i]; //cout<<sums[i+1]<<"\n"; if(sums[i+1]<0){ if(sums[i]<0){ ans += abs(values[i]+sums[i])+1; values[i] = 1; sums[i+1] = 1; } }else if(sums[i+1] > 0){ if(sums[i]>0){ ans += abs(values[i]+sums[i])+1; values[i] = -1; sums[i+1] = -1; } }else{ if(sums[i]<0){ sums[i+1] = 1; }else{ sums[i+1] = -1; } ans += 1; } //cout<<ans<<" "<<values[i]<<" "<<sums[i+1]<<" \n"; } 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
python3
N = int(input()) A = [int(_) for _ in input().split()] dp = A[0] count = 0 is_positive = dp > 0 for i in range(1, N): dp += A[i] if not (dp > 0) ^ is_positive: count += abs(dp)+1 dp ^= 1 is_positive = dp > 0 print(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 double sequence(int a[], double start) { double count = 0.0, presum = -1.0 * start, sum = 0.0; for(int i : a) { sum += (double)i; if(i == 0)sum += start; if(sum * presum > 0) { double min = Math.abs(sum) + 1; if(presum > 0)sum -= min; else sum += min; count += min; } if(sum == 0) { if(presum > 0)sum--; else sum++; ++count; } presum = sum; } return count; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n, a[]; double count = 0; n = sc.nextInt(); a = new int[n]; for(int i = 0; i < n; ++i) a[i] = sc.nextInt(); sc.close(); count = Math.min(sequence(a, (double)a[0]),sequence(a, -1.0 * a[0])); System.out.printf("%.0f\n", 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < (int)(n); i++) cin >> a[i]; long long s = a[0]; long long ans1 = 0, ans2 = 0; if (s <= 0) ans1 += -s + 1; for (int i = (1); i < (int)(n); i++) { if (i % 2 && s + a[i] >= 0) { ans1 += s + a[i] + 1; s = -1; } else if (i % 2 == 0 && s + a[i] <= 0) { ans1 += -(s + a[i]) + 1; s = 1; } else s += a[i]; } s = a[0]; if (s >= 0) ans2 += s + 1; for (int i = (1); i < (int)(n); i++) { if (i % 2 == 0 && s + a[i] >= 0) { ans2 += s + a[i] + 1; s = -1; } else if (i % 2 && s + a[i] <= 0) { ans2 += -(s + a[i]) + 1; s = 1; } else s += a[i]; } 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { bool ch = false; long long N, i; long long ans = 0, a, count = 0; cin >> N; cin >> a; ans += a; if (ans > 0) ch = true; else ch = false; for (i = 1; i < N; i++) { cin >> a; if (ch) { if (ans >= -a) { count += ans + a + 1; ans = -1; } else ans += a; ch = false; } else { if (ans <= -a) { count += -ans - a + 1; ans = 1; } else ans += a; ch = true; } } 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
python3
n = int(input()) a = list(map(int, input().split())) ans = 0 if a[0] > 0: op = "+" elif a[0] < 0: op = "-" elif a[0] == 0: ans += 1 if a[1] > 0: a[0] = -1 else: a[0] = 1 total = a[0] for i in range(1, n): if (total+a[i]) >= 0 and op == "+": ans += abs((-1 - total) - a[i]) total = -1 op = "-" elif (total+a[i]) < 0 and op == "-": ans += abs((1 - total) - a[i]) total = 1 op = "+" elif total+a[i] == 0: ans += 1 if a[1] > 0: a[0] = -1 else: a[0] = 1 else: total += a[i] if op == "+": op = "-" else: op = "+" 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 solve(vector<int> vec) { long long int n = vec.size(); int sum = vec[0]; int ans = 0; for (long long int i = 1; i < n; i++) { if (sum > 0) { if (sum + vec[i] > 0) { ans += (sum + vec[i] + 1); sum = -1; } else if (sum + vec[i] == 0) { ans++; sum = -1; } else { sum += vec[i]; } } else if (sum < 0) { if (sum + vec[i] < 0) { ans += (abs(sum + vec[i]) + 1); sum = 1; } else if (sum + vec[i] == 0) { ans++; sum = 1; } else { sum += vec[i]; } } } return ans; } int main() { int n, Ans; cin >> n; vector<int> as; for (int i = 0; i < n; i++) { int t; cin >> t; as.push_back(t); } vector<int> as1 = as; as1[0] = 1; vector<int> as2 = as; as2[0] = -1; Ans = min(solve(as), min(solve(as1) + abs(1 - as[0]), solve(as2) + abs(1 - as[0]))); 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<iostream> #include<cstdlib> using namespace std; int main(void){ int n,a; unsigned long long sum= 0,ans=0; int s; cin>>n; cin>>sum; if(sum>0) s = 1; else s = -1; for(int i=1;i<n;i++){ cin>>a; sum+=a; if(sum*s>0){ ans+=abs(sum)+1; sum = -s; } if(sum>0) s = 1; else s = -1; } if(sum==0) 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; long long int sum_0 = 0; long long int sum_1 = 0; long long int count_0 = 0; long long int count_1 = 0; long long int a; cin >> a; if (a > 0) { sum_0 = a; } else { sum_0 = 1; } if (a < 0) { sum_1 = a; } else { sum_1 = -1; } for (int i = 1; i < N; i++) { cin >> a; if (sum_0 * (sum_0 + a) >= 0) { if (sum_0 > 0) { count_0 += abs(sum_0 + a - (-1)); sum_0 = -1; } else { count_0 += abs(sum_0 + a - 1); sum_0 = 1; } } else { sum_0 += a; } if (sum_1 * (sum_1 + a) >= 0) { if (sum_1 > 0) { count_1 += abs(sum_1 + a - (-1)); sum_1 = -1; } else { count_1 += abs(sum_1 + a - 1); sum_1 = 1; } } else { sum_1 += a; } } cout << min(count_0, count_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
UNKNOWN
package main import ( "bufio" "fmt" "os" "strconv" ) func out(x ...interface{}) { // fmt.Println(x...) } var sc = bufio.NewScanner(os.Stdin) func getInt() int { sc.Scan() i, e := strconv.Atoi(sc.Text()) if e != nil { panic(e) } return i } func getString() string { sc.Scan() return sc.Text() } func f(a int) int { if a > 0 { return 1 } else if a < 0 { return -1 } return 0 } func min(a, b int) int { if a > b { return b } return a } func main() { sc.Split(bufio.ScanWords) n := getInt() a := make([]int, n) for i := 0; i < n; i++ { a[i] = getInt() } sum := 0 sign := -1 ans := [2]int{0, 0} for k := 0; k < 2; k++ { if k == 0 { sign = -1 } else { sign = 1 } for i := 0; i < n; i++ { sum += a[i] s := f(sum) out(sum, ":", s, sign) if s == 0 { out("zero") if sign == 1 { ans[k]++ sum-- } else { ans[k]++ sum++ } } else if s == sign { out("eq", "sum", sum, a) if sign == 1 { x := 1 + sum sum -= x ans[k] += x out("x+", x, sum, ans) } else { x := 1 - sum sum += x ans[k] += x out("x-", x, sum, ans) } } sign = -sign } } fmt.Println(min(ans[0], ans[1])) }
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; constexpr int MOD = 1000000007; using long long = long long; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } void print(const std::vector<int> &v) { std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; }); std::cout << std::endl; } int main() { int n; cin >> n; vector<long long> a(n); for (long long i = 0; i < (long long)n; i++) { cin >> a[i]; } long long res = (1ll << 60); long long ans = 0LL; long long s = 0; for (int i = 0; i < n; i++) { s += a[i]; if (i % 2 == 1) { if (s >= 0) { ans += s + 1; s = -1; } } else { if (s <= 0) { ans += -s + 1; s = 1; } } } res = min(res, ans); ans = 0; for (int i = 0; i < n; i++) { s += a[i]; if (i % 2 == 0) { if (s >= 0) { ans += s + 1; s = -1; } } else { if (s <= 0) { ans += -s + 1; s = 1; } } } res = min(res, ans); 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; int main() { int n; cin >> n; int A[n]; for (int i = 0; i < n; i++) { cin >> A[i]; } int countA = 0; int countB = 0; int part = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && A[i] + part <= 0) { countA += 1 - (A[i] + part); part = 1; } else if (i % 2 == 1 && A[i] + part >= 0) { countA += A[i] + part + 1; part = -1; } else part += A[i]; } for (int i = 0; i < n; i++) { if (i % 2 == 0 && A[i] + part >= 0) { countB += A[i] + part + 1; part = -1; } else if (i % 2 == 1 && A[i] + part <= 0) { countB += 1 - (A[i] + part); part = 1; } else part += A[i]; } cout << min(countA, countB) << 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); for (int i=0; i<N; i++) { cin >> a.at(i); } int ans1 = 0, ans2 = 0, sum1 = 0, sum2 = 0; for (int i=0; i<N; i++) { sum += a.at(i); if (i%2 == 0 && sum <= 0) { ans1 += 1-sum; //sumは負 sum1 = 1; } else if (i%2 != 0 && sum >= 0) { ans1 += sum+1; sum1 = -1; } } for (int i=0; i<N; i++) { sum += a.at(i); if (i%2 == 0 && sum >= 0) { ans2 += sum+1; sum2 = -1; } else if (i%2 != 0 && sum <= 0) { ans2 += 1-sum; 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
java
import java.util.Scanner; public class Main { static int ANS; static int N; public static void main(String[] args) { Scanner sc = new Scanner(System.in); N = Integer.parseInt(sc.nextLine()); long[] a = new long[N]; long[] sum = new long[N]; long tsum = 0L; for (int i = 0; i < N; i++) { long elm = Long.parseLong(sc.next()); a[i] = elm; tsum += elm; sum[i] = tsum; } solve(sum, a); System.out.println(ANS); } private static void solve(long[] sum, long[] a) { for (int i = 0; i < N - 1; i++) { long one = sum[i]; long two = sum[i + 1]; if (two == 0) { if (one >= 0) { ANS++; a[i + 1]--; sumStream(sum, a); solve(sum, a); } else { ANS++; a[i + 1]++; sumStream(sum, a); solve(sum, a); } } if (one >= 0 && two >= 0) { ANS += Math.abs(two) + 1; a[i + 1] -= Math.abs(two) + 1; sumStream(sum, a); solve(sum, a); } if (one < 0 && two < 0) { ANS += Math.abs(two) + 1; a[i + 1] += Math.abs(two) + 1; sumStream(sum, a); solve(sum, a); } } } private static void sumStream(long[] sum, long[] a) { long limit = a.length; long tsum = 0L; for (int i = 0; i < limit; i++) { tsum += a[i]; sum[i] = tsum; } } }
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> v(n), pref(n, 0); for (int i = 0; i < n; i++) { cin >> v[i]; pref[i] = pref[i - (i > 0)] + v[i]; } int x; int ans = 0; for (int i = 1; i < n; i++) { if (pref[i] > 0 && pref[i - 1] > 0) { x = abs(pref[i]) + 1; ans += x; for (int j = i; j < n; j++) { pref[j] -= x; } } else if (pref[i] < 0 && pref[i - 1] < 0) { x = abs(pref[i]) + 1; ans += x; for (int j = i; j < n; j++) { pref[j] += x; } } else if (pref[i] == 0) { if (pref[i - 1] > 0) { ans++; pref[i]--; } else { ans++; pref[i]++; } } } 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; signed main() { long long n; cin >> n; long long a[n]; for (long long i = 0; i < n; i++) { cin >> a[i]; } long long ans, sum; ans = sum = 0; for (long long i = 0; i < n; i++) { sum += a[i]; if (i < (n - 1) && sum * (sum + a[i + 1]) > 0) { if (sum > 0) { while (sum >= 0) { ans++; sum--; } } else { while (sum < 0) { ans++; sum++; } } } else if (sum == 0) { if (i < (n - 1) && (sum + a[i + 1]) > 0) sum--; else sum++; 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
cpp
#include <bits/stdc++.h> using namespace std; struct __ { __() { ios_base::Init i; ios_base::sync_with_stdio(0); cin.tie(0); } } __; int main() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; ++i) { cin >> v[i]; } vector<int> a = v; long long cnt = 0; if (v[0] <= 0) { v[0] = 1; cnt += 1 + abs(v[0]); } long long ans = v[0]; for (int i = 1; i < n; ++i) { ans += v[i]; if (i % 2 == 0 && ans <= 0) { int x = min(1 + abs(ans), abs(ans - v[i]) - 1); cnt += 1 + abs(ans); v[i] = v[i] + (1 + abs(ans) - x); v[i - 1] = v[i - 1] - x; ans = 1; } if (i % 2 == 1 && ans >= 0) { int x = min(1 + ans, ans - v[i] - 1); v[i] = v[i] - (1 + ans - x); v[i - 1] = v[i - 1] - x; cnt += 1 + ans; ans = -1; } } v = a; long long res = cnt; cnt = 0; if (v[0] >= 0) { v[0] = -1; cnt += 1 + v[0]; } ans = v[0]; for (int i = 1; i < n; ++i) { ans += v[i]; if (i % 2 == 1 && ans <= 0) { int x = min(1 + abs(ans), abs(ans - v[i]) - 1); cnt += 1 + abs(ans); v[i] = v[i] + (1 + abs(ans) - x); v[i - 1] = v[i - 1] - x; ans = 1; } if (i % 2 == 0 && ans >= 0) { int x = min(1 + ans, ans - v[i] - 1); v[i] = v[i] - (1 + ans - x); v[i - 1] = v[i - 1] - x; cnt += 1 + ans; ans = -1; } } cout << min(res, 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
fn read<T: std::str::FromStr>() -> T { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().parse().ok().unwrap() } fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>() .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect() } fn read_vec2<T: std::str::FromStr>(n: u32) -> Vec<Vec<T>> { (0..n).map(|_| read_vec()).collect() } fn main(){ let n : u64 = read(); let an = read_vec::<i64>(); let ans = std::cmp::min(solve(true, &an),solve(false, &an)); println!("{}",ans ); } fn solve(mut symbol: bool, v:&Vec<i64>) -> i64{ let mut sum = 0; let mut ans = 0; for a in v{ sum += a; if !symbol && sum >= 0 { let next = sum - -1; ans += next; sum -= next; } else if symbol && sum <= 0{ let next = 1 -sum; sum += next; ans += next; } symbol = !symbol; } 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 MaxN = 1e5 + 5; int box[MaxN]; int n; long long solve1() { long long sum = box[1]; long long res = 0; if (sum <= 0) { sum = 1; res = abs(sum) + 1; } for (int i = 2; i <= n; i++) { long long temp = box[i] + sum; if (temp * sum >= 0) { res += abs(temp) + 1; if (sum > 0) sum = -1; else sum = 1; } else sum = temp; } return res; } long long solve2() { long long sum = box[1]; long long res = 0; if (sum >= 0) { sum = -1; res = abs(sum) + 1; } for (int i = 2; i <= n; i++) { long long temp = box[i] + sum; if (temp * sum >= 0) { res += abs(temp) + 1; if (sum > 0) sum = -1; else sum = 1; } else sum = temp; } return res; } int main() { while (~scanf("%d", &n)) { for (int i = 1; i <= n; i++) scanf("%d", &box[i]); long long ans = 1LL << 60; ans = min(ans, solve1()); ans = min(ans, solve2()); 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 n; long long f(int s, vector<long long> v) { long long ans = 0LL, x, t = 0LL, y; if (v[0] == 0LL) { x = s * (1LL) - t; y = abs(x - v[0]); v[0] = x; ans += y; } t = t + v[0]; for (int i = 1; i < n; i++) { s *= (-1LL); if ((s == (-1LL) && (t + v[i]) >= 0LL) || (s == (1LL) && (t + v[i]) <= 0LL)) { x = s * (1LL) - t; y = abs(x - v[i]); v[i] = x; ans += y; } t = t + v[i]; } return ans; } int main() { while (scanf("%d", &n) == 1) { vector<long long> arr(100010); long long a; for (int i = 0; i < n; i++) { scanf("%lld", &arr[i]); } long long x = f(-1, arr); long long y = f(1, arr); long long ans = min(x, y); 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(void) { int n, a[100010]; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; long long int sum = a[0], cnt = 0; for (int i = 1; i < n; ++i) { if (sum < 0) { if (sum + a[i] > 0) { sum += a[i]; } else { cnt += abs(sum + a[i]) + 1; sum = 1; } } else { if (sum + a[i] < 0) { sum += a[i]; } else { cnt += abs(sum + a[i]) + 1; sum = -1; } } } 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 n; int sum = 0; int ans1 = 0; int ans2 = 0; int i, j; int t = 1; int main() { cin >> n; int a[100010]; for (i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; if (sum * t <= 0) { ans1 += abs(sum - t); sum = t; } t *= -1; } t = -1; sum = 0; for (i = 0; i < n; i++) { sum += a[i]; if (sum * t <= 0) { ans2 += abs(sum - t); sum = t; } t *= -1; } printf("%d\n", min(ans1, ans2)); 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 INFF = 0x3f3f3f3f3f3f3f3f; long long a[1000010]; int n; unsigned long long solve() { unsigned long long sum = 0; long long oo = 0, flag; if (a[0] > 0) flag = -1; else if (a[0] < 0) flag = 1; for (int i = 0; i < n; i++) { oo += a[i]; if (flag == 1) { if (oo >= 0) { sum += oo + 1; oo = -1; } } if (flag == -1) { if (oo <= 0) { sum += 0 - oo + 1; oo = 1; } } flag = -flag; } return sum; } int main() { while (scanf("%d", &n) != EOF) { unsigned long long sum = INFF; for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); } if (a[0] == 0) { a[0] = 1; unsigned long long sum1 = solve(); a[0] = -1; unsigned long long sum2 = solve(); sum = min(sum1, sum2) + 1; } else { unsigned long long sum0 = solve(); a[0] = 1; unsigned long long sum1 = solve() + abs(a[0] - 1); a[0] = -1; unsigned long long sum2 = solve() + abs(a[0] + 1); sum = min(sum0, min(sum1, sum2)); } printf("%lld\n", 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 dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int n; vector<long long> a(200000); long long solve(vector<long long> a) { long long ans = 0, sum = a[0]; for (int i = 1; i < n; i++) { long long tmp = sum; sum += a[i]; if (sum >= 0 && tmp > 0) { ans += abs(sum) + 1; sum = -1; } else if (sum <= 0 && tmp < 0) { ans += abs(sum) + 1; sum = 1; } } return ans; } int main() { cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; long long ans1 = solve(a); a[0] = (-1) * a[0]; long long ans2 = solve(a); 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; constexpr auto INF = 100000000000; constexpr auto mod = 1000000007; struct edge { int to, cost; }; 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 m) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } long long int c(long long int a, long long int b, long long int m) { long long int ans = 1; for (long long int i = 0; i < b; i++) { ans *= a - i; ans %= m; } for (long long int i = 1; i <= b; i++) { ans *= modinv(i, m); ans %= m; } return ans; } void dijkdtra(int s, int v, vector<int>& d, vector<vector<edge>>& G) { priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que; d[s] = 0; que.push(pair<int, int>(0, s)); while (!que.empty()) { pair<int, int> p = que.top(); que.pop(); int V = p.second; if (d[V] < p.first) continue; for (int i = 0; i < G[V].size(); i++) { edge e = G[V][i]; if (d[e.to] > d[V] + e.cost) { d[e.to] = d[V] + e.cost; que.push(pair<int, int>(d[e.to], e.to)); } } } } long long int binary_search(vector<int>& s, long long int a) { long long int l = -1; long long int r = (int)s.size(); while (r - l > 1) { long long int mid = l + (r - l) / 2; if (s[mid] >= a) r = mid; else l = mid; } return r; } int k(long long n) { int x = 0; while (n) { x += n % 10; n /= 10; } return x; } long long max(long long x, long long y) { if (x < y) return y; return x; } int main() { long long n, ans; cin >> n; vector<long long> a(n), t(n), s(n); for (int i = (0); i < (n); i++) { cin >> a[i]; t[i] = a[i]; s[i] = a[i]; } long long int w = a[0]; if (w <= 0) { w = 1; } for (int i = (1); i < (n); i++) { if (i % 2 == 0) { if (abs(w) >= a[i]) { a[i] = abs(w) + 1; } w += a[i]; } else { if (w >= abs(a[i])) { a[i] = -1 * (w + 1); } w += a[i]; } } w = t[0]; if (w >= 0) { w = -1; } for (int i = (1); i < (n); i++) { if (i % 2 == 1) { if (abs(w) >= t[i]) { t[i] = abs(w) + 1; } w += t[i]; } else { if (w >= abs(t[i])) { t[i] = -1 * (w + 1); } w += t[i]; } } long long cost1 = 0, cost2 = 0; for (int i = (0); i < (n); i++) { cost1 += abs(s[i] - a[i]); cost2 += abs(s[i] - t[i]); } ans = min(cost1, cost2); 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> const int MGN = 8; const int ARY_SZ_MAX = 10000000; using namespace std; using ll = long long; using ull = unsigned long long; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>; using vl = vector<ll>; using vvl = vector<vl>; using vd = vector<double>; using vs = vector<string>; using pii = pair<int, int>; using pll = pair<ll, ll>; using psi = pair<string, int>; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vl A(N + 1, 0); for (int i = int(0); i < int(N); ++i) cin >> A[i]; vl s(N + 1, 0); ll ans = (INT_MAX / 2); ll cnt = 0; s[0] = A[0]; for (int i = int(0); i < int(N); ++i) { if (i % 2 == 0 && s[i] <= 0) { cnt += abs(s[i]) + 1; s[i] = 1; } else if (i % 2 == 1 && s[i] >= 0) { cnt += abs(s[i]) + 1; s[i] = -1; } s[i + 1] = s[i] + A[i + 1]; } ans = min(ans, cnt); cnt = 0; s[0] = A[0]; for (int i = int(0); i < int(N); ++i) { if (i % 2 == 0 && s[i] >= 0) { cnt += abs(s[i]) + 1; s[i] = -1; } else if (i % 2 == 1 && s[i] <= 0) { cnt += abs(s[i]) + 1; s[i] = 1; } s[i + 1] = s[i] + A[i + 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
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]; if (a[0] != 0) { long long ans = 0; for (int i = 1; i < n; i++) { a[i] += a[i - 1]; if (a[i] < 0 && a[i - 1] < 0) { ans += 1 - a[i]; a[i] = 1; } if (a[i] > 0 && a[i - 1] > 0) { ans += a[i] - (-1); a[i] = -1; } if (a[i] == 0) { ans++; if (a[i - 1] < 0) a[i] = 1; else a[i] = -1; } } cout << ans << endl; return 0; } long long ans[2] = {1, 1}; for (int j = 0; j < 2; j++) { a[0] = ((j == 0) ? 1 : -1); for (int i = 1; i < n; i++) { a[i] += a[i - 1]; if (a[i] < 0 && a[i - 1] < 0) { ans[j] += 1 - a[i]; a[i] = 1; } if (a[i] > 0 && a[i - 1] > 0) { ans[j] += a[i] - (-1); a[i] = -1; } if (a[i] == 0) { ans[j]++; if (a[i - 1] < 0) a[i] = 1; else a[i] = -1; } } } cout << min(ans[0], ans[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
cpp
#include <bits/stdc++.h> using namespace std; long f(int a[], int N, bool positive) { long cnt = 0; long sum = a[0]; for (int i = 1; i < N; i++) { int next = a[i]; int nextSum = sum + next; if (positive) { if (i % 2 == 0) { if (nextSum <= 0) { int des = abs(nextSum) + 1; next += des; cnt += des; } } else { if (0 <= nextSum) { int des = abs(nextSum) + 1; next -= des; cnt += des; } } } else { if (i % 2 == 0) { if (0 <= nextSum) { int des = abs(nextSum) + 1; next -= des; cnt += des; } } else { if (nextSum <= 0) { int des = abs(nextSum) + 1; next += des; cnt += des; } } } sum += next; } return cnt; } int main() { int N; cin >> N; int a[N]; for (int i = 0; i < N; i++) cin >> a[i]; long cnt = min(f(a, N, 1), f(a, N, 0)); 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
java
import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] nums = new int[n]; for(int i = 0; i < n; i++){ nums[i] = sc.nextInt(); } int[] sums = new int[n]; int tempSum = 0; for(int i = 0; i < n; i++){ tempSum = nums[i] + tempSum; sums[i] = tempSum; } int result = 0; for(int i = 1; i < n; i++){ boolean flag = check(sums) == false; if(flag){ if(sums[i] * sums[i - 1] >= 0){ if(sums[i - 1] > 0){ result += Math.abs(-1 - sums[i]); int temp = (-1 - sums[i]); for(int j = i; j < n; j++){ sums[j] += temp; } temp = 0; } else if(sums[i - 1] < 0){ result += Math.abs(1 - sums[i]); int temp = (1 - sums[i]); for(int j = i; j < n; j++){ sums[j] += temp; } temp = 0; } } } else{ break; } } System.out.println(result); sc.close(); } public static boolean check(int[] sums){ for(int i = 0; i < sums.length; i++){ if(sums[i] == 0) return false; else if(i >= 1 && sums[i] * sums[i - 1] >= 0) return false; } return true; } }
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 a[123456]; int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; long long flag; if (a[0] > 0) flag = 1; else if (a[0] < 0) flag = -1; else { int j; for (j = 0; j < n; j++) if (a[j] != 0) break; if (a[j] > 0) { if (j % 2) flag = -1; else flag = 1; } else { if (j % 2) flag = 1; else flag = -1; } } long long cnt = 0; long long ans = 0; cnt = a[0]; if (cnt == 0) { if (flag == 1) { cnt = 1; ans++; } else { cnt = -1; ans++; } } 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 String = std::string; using llong = long long; using boolean = bool; using Pii = std::pair<int, int>; using Vi = std::vector<int>; using Vii = std::vector<Pii>; constexpr int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; constexpr int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; constexpr int INF = 0x3f3f3f3f; constexpr llong LINF = 0x3f3f3f3f3f3f3f3fLL; namespace { template <class A, class B> A power(llong x, llong n, llong mod) { llong ans = 1; while (n > 0) { if (n & 1) ans = (ans * x) % mod; x = (x * x) % mod; n >>= 1; } return ans; } template <class A, class B> A power(A x, B n) { return power(x, n, 1000000007); } template <class A> A gcd(A x, A y) { return x % y ? gcd(y, x % y) : y; } template <class A, class B> A lcm(A x, B y) { return (x / gcd(x, y) * y); } template <class A> inline A abs(A n) { return (n < 0) ? -n : n; } template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a ? a = b, true : false; } template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a ? a = b, true : false; } inline boolean isMovable(int x, int y, int w, int h) { return (x >= 0 && y >= 0 && x < w && y < h); } } // namespace namespace Rlyeh { int a[100100], left; int cnt, tmp; signed call_of_Cthulhu(signed datum) { int n; std::cin >> n; for (int i = 0; i < n; i++) { std::cin >> a[i]; } left += a[0]; for (int i = 1; i < n; i++) { boolean isNegative = left < 0; left += a[i]; if (isNegative && left <= 0) { cnt += 1 - left; left = 1; } else if (!isNegative && 0 <= left) { cnt += 1 + left; left = -1; } } std::cout << cnt << '\n'; return 0; } } // namespace Rlyeh signed main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int main_result = Rlyeh::call_of_Cthulhu(114514); 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 s = a[0] for i in range(1, n): if a[0] > 0: while s + a[i] >= 0: a[i] -= 1 cnt += 1 if a[0] < 0: while s + a[i] <= 0: a[i] += 1 cnt += 1 s += a[i] 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
cpp
#include <bits/stdc++.h> using namespace std; long long a[100000]; int n; void solve() { long long sum0; long long sum1; long long ans1 = 0; long long ans2 = 0; long long ans3 = 0; if (a[0] == 0) { ans1++; sum0 = 1; } else { sum0 = a[0]; } 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) { ans3++; sum0 = -1; } else { sum0 = -1 * a[0] / abs(a[0]); } for (int i = 1; i < n; i++) { sum1 = sum0 + a[i]; if (sum1 * sum0 < 0) { } else if (sum1 * sum0 > 0) { ans3 += abs(sum1) + 1; sum1 = -1 * sum0 / abs(sum0); } else { ans3++; sum1 = -1 * sum0 / abs(sum0); } sum0 = sum1; } cout << min(ans1, ans3) << 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, count, sum, x, bsum, s, count2; vector<int> a, b; cin >> n; a.resize(n); b.resize(n); cin >> a[0]; for (i = 1; i < n; i++) { cin >> a[i]; } b = a; count = 0; count2 = 0; if (a[0] <= 0) { count = abs(a[0]) + 1; a[0] = 1; } sum = 0; for (int i = 0; i < n - 1; i++) { sum += a[i]; if (sum + a[i + 1] == 0) { count++; if (sum > 0) { a[i + 1]--; } else { a[i + 1]++; } } if (sum * (sum + a[i + 1]) > 0) { if (sum > 0) { x = sum + 1; s = x + a[i + 1]; a[i + 1] = -x; count += abs(s); } else { x = sum - 1; s = x + a[i + 1]; a[i + 1] = -x; count += abs(s); } } } if (b[0] >= 0) { count2 = abs(b[0]) + 1; b[0] = -1; } sum = 0; for (int i = 0; i < n - 1; i++) { sum += b[i]; if (sum + b[i + 1] == 0) { count2++; if (sum > 0) { b[i + 1]--; } else { b[i + 1]++; } } if (sum * (sum + b[i + 1]) > 0) { if (sum > 0) { x = sum + 1; s = x + b[i + 1]; b[i + 1] = -x; count2 += abs(s); } else { x = sum - 1; s = x + b[i + 1]; b[i + 1] = -x; count2 += abs(s); } } } cout << min(count, count2); 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 <vector> #include <algorithm> #include <math.h> #include <numeric> #include <set> #include <string> #include <map> #include <stack> #include <queue> #include <time.h> using namespace std; typedef long long ll; #define pl pair<ll,ll> #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define rep(i,n) for(int i=0;i<(n);++i) #define foreach(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); itr++) #define dbg(x) cout << #x"="<< (x) << endl #define mp(a,b) make_pair((a),(b)) #define pb(a) push_back(a) #define in(x) cin >> x; #define all(x) (x).begin(), (x).end() #define INF 2147483600 #define fi first #define se second int main() { ll n;cin>>n; vector<ll> a(n); rep(i,n)cin>>a[i]; ll total=a[0]; ll total2=a[0]; ll ans=0; FOR(i,1,n){ total+=a[i]; // dbg(total); if(total*total2>=0){ ans+=(abs(total)+1); if(total2>0){ total=-1; }else{ total=1; } } // dbg(total); total2=total; } 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
UNKNOWN
using System; using static System.Console; using static System.Convert; class Program { static void Main(string[] args) { var length = ToInt32(ReadLine()); var result = 0; var nums = Array.ConvertAll(ReadLine().Split(' '), int.Parse); var sum = nums[0]; if (sum == 0) { sum++; result++; } var lastSum = sum; for(var i = 1; i < length; i++) { sum += nums[i]; while (!IsDifferentSign(lastSum, sum)||sum==0) { if (!IsDifferentSign(lastSum, sum)) { result += Math.Abs(sum) + 1; sum = lastSum > 0 ? -1 : 1;} if (sum == 0) { sum = lastSum > 0 ? --sum : ++sum; result++; } } lastSum = sum; } WriteLine(result); } private static bool IsDifferentSign(int lastSum,int sum) { return (lastSum > 0 && sum < 0) || (lastSum < 0 && sum > 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())) def calc_tmp_ans(tmp_ans, cum_sum): for i in range(1, N): #print(i, tmp_ans) prev_cum_sum = cum_sum cum_sum += a[i] if cum_sum * prev_cum_sum >= 0: tmp_ans += abs(cum_sum) + 1 if prev_cum_sum > 0: cum_sum = -1 else: cum_sum = 1 return tmp_ans ans = 1000000000000000 if a[0] == 0: ans = min(ans, calc_tmp_ans(1, 1)) ans = min(ans, calc_tmp_ans(-1, 1)) else: tmp_ans = 0 cum_sum = a[0] ans = min(ans, calc_tmp_ans(tmp_ans, cum_sum)) if a[0] > 0: ans = min(ans, calc_tmp_ans(a[0] + 1, -1)) else: ans = min(ans, calc_tmp_ans(abs(a[0]) + 1, 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
java
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); int n = scan.nextInt(); long[] a_ = new long[n]; for(int i = 0; i < n; i++){ a_[i] = scan.nextLong(); } long[] sum_ = new long[n]; sum_[0] = a_[0]; long count1 = 0; long count2 = 0; //sum_[0] >= 1 if(sum_[0] < 1){ count1 = 1-sum_[0]; }else{ } for(int i = 1; i < n; i++){ sum_[i] = sum_[i-1]+a_[i]; if(i % 2 != 0){ if(sum_[i] <= -1){ //OK }else{ count1 += (sum_[i]+1); sum_[i] = -1; } }else{ if(sum_[i] >= 1){ //OK }else{ count1 += (1-sum_[i]); sum_[i] = 1; } } } //sum_[0] <= -1 if(sum_[0] > -1){ count2 = sum_[0]+1; }else{ } for(int i = 1; i < n; i++){ sum_[i] = sum_[i-1]+a_[i]; if(i % 2 != 0){ if(sum_[i] >= 1){ //OK }else{ count2 += (1-sum_[i]); sum_[i] = 1; } }else{ if(sum_[i] <= -1){ //OK }else{ count2 += (sum_[i]+1); sum_[i] = -1; } } } System.out.println(Math.min(count1, count2)); } }
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; ll sum = 0; cin >> sum; ll ans = 0; for (int i = 0; i < n - 1; ++i) { int a; cin >> a; sum += a; if (((sum - a > 0) and (sum > 0)) or ((sum - a < 0) and (sum < 0))) { ll need = max(abs(sum - 1), abs(sum + 1)); sum += (sum > 0 ? -need : need); ans += need; } else if (sum == 0) { sum += (sum - a > 0 ? -1 : 1); ans += 1; } } 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
UNKNOWN
program ec12; var a,s:array[0..100000] of longint; n,m,i,j,ans,sum1,sum2,ans1:longint; begin readln(n); ans:=0; ans1:=0; s[0]:=0; for i:=1 to n do read(a[i]); if a[1]>0 then begin sum1:=a[1]; ans:=0; ans1:=a[1]+1; sum2:=-1; end else begin if a[1]=0 then begin sum1:=1; sum2:=-1; ans:=1; ans1:=1; end else begin ans:=abs(a[1])+1; sum1:=1; sum2:=a[1]; ans1:=0; end; end; for i:=2 to n do begin if sum1>0 then begin if sum1+a[i]>=0 then begin inc(ans,sum1+a[i]+1); sum1:=-1; end else sum1:=sum1+a[i]; end else begin if sum1+a[i]<=0 then begin inc(ans,abs(sum1+a[i])+1); sum1:=1; end else sum1:=sum1+a[i]; end; if sum2>0 then begin if sum2+a[i]>=0 then begin inc(ans1,sum2+a[i]+1); sum2:=-1; end else sum2:=sum2+a[i]; end else begin if sum2+a[i]<=0 then begin inc(ans1,abs(sum2+a[i])+1); sum2:=1; end else sum2:=sum2+a[i]; end; end; if sum1=0 then inc(ans); if sum2=0 then inc(ans1); if ans<ans1 then writeln(ans) else writeln(ans1); 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())) suma = A[0] count = 0 for i in range(1,N): if suma*(suma+A[i]) < 0: suma += A[i] continue else: if A[i] == 0: if suma > 0: count += suma+1 suma = -1 else: count += -suma+1 suma = 1 elif suma > 0: count += (suma+1)+A[i] suma = -1 elif suma < 0: count += (-suma+1)-A[i] suma = 1 print(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
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1001001001; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < (n); i++) cin >> a[i]; int ans = INF; int sum = 0; int count = 0; for (int i = 0; i < (n); i++) { sum += a[i]; if (i % 2 == 0) { if (sum <= 0) { count += (1 - sum); sum = 1; } } else { if (sum >= 0) { count += (sum - (-1)); sum = -1; } } } ans = min(ans, count); sum = 0; count = 0; for (int i = 0; i < (n); i++) { sum += a[i]; if (i % 2 != 0) { if (sum <= 0) { count += (1 - sum); sum = 1; } } else { if (sum >= 0) { count += (sum - (-1)); sum = -1; } } } ans = min(ans, count); 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 a, b = 0, c = 0; int n; cin >> n >> a; b += a; for (int i = 1; i < n; i++) { cin >> a; if ((a + b >= 0) == (b > 0)) { if (b > 0) { c += a + b + 1; a = -b - 1; } else if (b < 0) { c -= a + b - 1; a = -b + 1; } } b += a; } 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; scanf("%d", &n); vector<int> a(n, 0); for (int i = 0; i < n; i++) { cin >> a.at(i); } bool target_sign = false; if (a.at(0) > 0) { target_sign = true; } else { target_sign = false; } int ans = 0; int sum = a.at(0); for (int i = 1; i < n; i++) { sum += a.at(i); if (target_sign) { if (sum >= 0) { while (sum >= 0) { sum--; ans++; } } target_sign = false; } else { if (sum <= 0) { while (sum <= 0) { sum++; ans++; } } target_sign = true; } } printf("%d", 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; template <typename T> using keyVal = pair<string, T>; template <typename T> bool val_greater(const keyVal<T>& left, const keyVal<T>& right) { return left.second > right.second; } void init_global() {} int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<int> a(N); for (int i = int(0); i != int(N); i++) cin >> a[i]; vector<int> s(N, 0); int cnt = 0; if (a[0] == 0) { a[0]++; cnt++; } s[0] = a[0]; bool posit = (s[0] > 0) ? true : false; for (int i = int(1); i != int(N); i++) { posit = !posit; int ts = s[i - 1] + a[i]; if (!posit && ts >= 0) { int c = abs(ts) + 1; a[i] -= c; cnt += c; } else if (posit && ts <= 0) { int c = abs(ts) + 1; a[i] += c; cnt += c; } s[i] = s[i - 1] + a[i]; } 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 double PI = 3.1415926535897932384626433832795; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool isDiffer(long long a, long long b) { if (b == 0) return false; if (((a > 0) && (b < 0)) || ((a < 0) && (b > 0))) return true; else return false; } int main() { ios::sync_with_stdio(false); long long n; cin >> n; vector<long long> v; vector<long long> vv; for (int i = 0; i < n; i++) { long long t; cin >> t; v.push_back(t); vv.push_back(t); } long long ans[2] = {0}; for (int j = 0; j < 2; j++) { long long ob = (j == 0) ? -1 : 1; if (isDiffer(ob, v[0])) { ans[j] += llabs(ob - v[0]); v[0] = ob; } long long os = v[0]; for (int i = 1; i < n; i++) { if (!isDiffer(os, v[i] + os)) { long long ob = (os >= 0) ? -1 : 1; ans[j] += llabs(ob - os - v[i]); v[i] = ob - os; } os += v[i]; } v = vv; } cout << min(ans[0], ans[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
UNKNOWN
#include <bits/stdc++.h> int abs(int num) { return (num > 0) ? num : -num; } int main() { int n; scanf("%d", &n); int A[n]; int i; int sum = 0; int ans = 0; for (i = 0; i < n; i++) { scanf("%d", &A[i]); } for (i = 0; i < n - 1; i++) { sum += A[i]; if (sum > 0) { if (sum >= -A[i + 1]) { ans += abs(sum + A[i + 1] + 1); A[i + 1] -= abs(sum + A[i + 1] + 1); printf("%d %d\n", i, ans); } } else { if (sum <= -A[i + 1]) { ans += abs(sum + A[i + 1] - 1); A[i + 1] += abs(sum + A[i + 1] + 1); printf("%d %d\n", i, ans); } } } printf("%d\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; static const int INF = 2000000000; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < (int)(n); i++) cin >> a[i]; long long ans = 0; long long wa; if (a[0] != 0) { wa = a[0]; for (int i = 1; i < n; i++) { if (wa > 0) { wa += a[i]; if (wa < 0) continue; else { ans += wa + 1; wa = -1; } } else { wa += a[i]; if (wa > 0) continue; else { ans += 1 - wa; wa = 1; } } } cout << ans << endl; } else { long long ans1 = 1, ans2 = 1; wa = 1; for (int i = 1; i < n; i++) { if (wa > 0) { wa += a[i]; if (wa < 0) continue; else { ans1 += wa + 1; wa = -1; } } else { wa += a[i]; if (wa > 0) continue; else { ans1 += 1 - wa; wa = 1; } } } wa = -1; for (int i = 1; i < n; i++) { if (wa > 0) { wa += a[i]; if (wa < 0) continue; else { ans2 += wa + 1; wa = -1; } } else { wa += a[i]; if (wa > 0) continue; else { ans2 += 1 - wa; wa = 1; } } } if (ans1 < ans2) cout << ans1 << endl; else cout << 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N, a[100000]; cin >> N; for (int i = 0; i < N; ++i) cin >> a[i]; int counter = 0; int b[100000]; if (a[0] == 0) { ++a[0]; ++counter; b[0] = a[0]; for (int i = 1; i < N; ++i) { b[i] += b[i - 1] + a[i]; while (b[i - 1] * b[i] >= 0) { if (i % 2 == 0) { ++b[i]; } else { --b[i]; } ++counter; } } } else if (a[0] > 0) { b[0] = a[0]; for (int i = 1; i < N; ++i) { b[i] = b[i - 1] + a[i]; while (b[i - 1] * b[i] >= 0) { if (i % 2 == 0) { ++b[i]; } else { --b[i]; } ++counter; } } } else { b[0] = a[0]; for (int i = 1; i < N; ++i) { b[i] = b[i - 1] + a[i]; while (b[i - 1] * b[i] >= 0) { if (i % 2 == 0) { --b[i]; } else { ++b[i]; } ++counter; } } } cout << counter << endl; }