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> #pragma GCC optimize("O3,no-stack-protector") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx") using namespace std; using Graph = vector<vector<int64_t>>; const double pi = M_PI; const int64_t MOD = 1000000007; int64_t calc(const vector<int64_t> &a, int64_t n, int64_t tem) { int64_t ans = 0; if (tem == 0) { if (0 <= a[1]) { tem = -1; ans++; } else { tem = 1; ans++; } } for (int i = 1; i < n; i++) { if ((0 < tem + a[i] && tem < 0) || (tem + a[i] < 0 && 0 < tem)) { tem += a[i]; } else { if (0 <= tem + a[i] && 0 <= tem) { ans += abs(-1 - (tem + a[i])); tem = -1; } else { ans += abs(1 - (tem + a[i])); tem = 1; } } } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int64_t n; cin >> n; vector<int64_t> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int64_t aa = a[0], bb, ansdel; if (a[0] != 1) { if (0 <= a[0]) { bb = -1; } else { bb = 1; } ansdel = abs(a[0]) + 1; } int64_t ans = min(calc(a, n, aa), calc(a, n, bb) + ansdel); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int, input().split())) currentSum = 0 count1 = 0 count2 = 0 count3 = 0 count4 = 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 currentSum = 0 for i in range(N): restSum = currentSum currentSum += A[i] if currentSum <= 0 and restSum < 0: count3 += abs(currentSum) + 1 currentSum = 1 elif currentSum >= 0 and restSum > 0: count3 += abs(currentSum) + 1 currentSum = -1 elif A[i] <= 0 and restSum == 0: count3 += abs(currentSum) + 1 currentSum = 1 currentSum = 0 for i in range(N): restSum = currentSum currentSum += A[i] if currentSum <= 0 and restSum < 0: count4 += abs(currentSum) + 1 currentSum = 1 elif currentSum >= 0 and restSum > 0: count4 += abs(currentSum) + 1 currentSum = -1 elif A[i] >= 0 and restSum == 0: count4 += abs(currentSum) + 1 currentSum = -1 print(count1, count2, count3, count4)
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; typedef std::priority_queue<int> IntPrioQueue; typedef std::priority_queue<int, std::vector<int>, std::greater<int> > IntReversePrioQueue; int dx4[4] = {1, 0, -1, 0}; int dy4[4] = {0, 1, 0, -1}; int dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1}; int dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1}; void solve(void) { int n; cin >> n; long long accsums1[n]; long long accsums2[n]; long long temp0; scanf("%lld\n", &temp0); accsums1[0] = accsums2[0] = temp0; for (int i = 0; i <= n - 1 - 1; i++) { long long temp; scanf("%lld\n", &temp); accsums1[i + 1] = temp + accsums1[i]; accsums2[i + 1] = temp + accsums1[i]; } long long ans1 = 0; for (int i = 0; i <= n - 1; i++) { if ((i % 2 == 0 and accsums1[i] > 0) or (i % 2 != 0 and accsums1[i] < 0)) continue; if (i % 2 == 0) { long long diff = 1 - accsums1[i]; ans1 += diff; for (int j = i + 1; j <= n - 1; j++) accsums1[j] += diff; } else { long long diff = 1 + accsums1[i]; ans1 += diff; for (int j = i + 1; j <= n - 1; j++) accsums1[j] -= diff; } } long long ans2 = 0; for (int i = 0; i <= n - 1; i++) { if ((i % 2 == 0 and accsums2[i] < 0) or (i % 2 != 0 and accsums2[i] > 0)) continue; if (i % 2 != 0) { long long diff = 1 - accsums2[i]; ans2 += diff; for (int j = i + 1; j <= n - 1; j++) accsums2[j] += diff; } else { long long diff = 1 + accsums2[i]; ans2 += diff; for (int j = i + 1; j <= n - 1; j++) accsums2[j] -= diff; } } cout << min(ans1, ans2) << '\n'; printf("Debug\n"); } int main(void) { 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
python3
n=int(input()) a=list(map(int,input().split())) now=a[0] if now==0: c=1 flag=1 for i in range(1,n): if not a[i]==0: flag=abs(a[i])//a[i] #print(flag) if i%2==1: flag*=-1 now=flag else: flag=abs(a[0])//a[0] c=0 #print(c,now,flag) for i in range(1,n): tmp=now+a[i] if not tmp*flag<0: c+=abs(flag*-1-tmp) now=flag*-1 else: now=tmp flag*=-1 #print(c,now,flag) 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, chk; long long ans = 0, ans2 = 0; scanf("%d", &n); vector<int> a(n); for (auto& e : a) scanf("%d", &e); chk = a[0]; for (int i = 1; i < n; i++) { if (i % 2) { chk += a[i]; if (chk >= 0) { ans += chk + 1; chk = -1; } } else { chk += a[i]; if (chk <= 0) { ans += -1 * chk + 1; chk = 1; } } } chk = a[0]; for (int i = 1; i < n; i++) { if (i % 2 == 0) { chk += a[i]; if (chk >= 0) { ans2 += chk + 1; chk = -1; } } else { chk += a[i]; if (chk <= 0) { ans2 += -1 * chk + 1; chk = 1; } } } if (a[0] == 0) printf("%lld\n", min(ans, ans2)); else if (a[0] > 0) printf("%lld\n", ans); else printf("%lld\n", 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; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int count = 0; int ans = 0; for (int i = 0; i < n; i++) { count += a[i]; if (i % 2 == 0) { if (count <= 0) { ans += abs(count - 1); count = 1; } } else { if (count >= 0) { ans += abs(count + 1); count = -1; } } } int count2 = 0; int ans2 = 0; for (int i = 0; i < n; i++) { count2 += a[i]; if (i % 2 == 1) { if (count2 <= 0) { ans2 += abs(count2 - 1); count2 = 1; } } else { if (count2 >= 0) { ans2 += abs(count2 + 1); count2 = -1; } } } cout << min(ans, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
parseInt(x) = parse(Int, x) function main() n = readline() |> parseInt a = map(parseInt, split(readline())) b = Array{Int}(n) b[1] = a[1] k = 0 for i in 2:n b[i] = a[i]+b[i-1] if b[i]*b[i-1] >= 0 if b[i-1] < 0 k += abs(b[i]-1) b[i] = 1 else k += abs(b[i]+1) b[i] = -1 end end end print(k) end 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; const int INF = 1001001001; const long long LINF = 1001001001001001001ll; const int MOD = 1000000007; 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; } long long sign(long long A) { return (A > 0) - (A < 0); } int main() { long long n; cin >> n; vector<long long> a(n); for (int i = 0; i < (n); ++i) cin >> a[i]; long long ans = 0; long long sum = 0; int sig; for (int i = 0; i < (n); ++i) { sum += a[i]; if (i == 0) { sig = sign(sum); continue; } if (sig == -sign(sum)) { sig = sign(sum); continue; } long long diff = -sig - sum; sum += diff; ans += abs(diff); sig = sign(sum); } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int calc(bool firstPositive, int a[], int n) { bool positive = firstPositive; int cost = 0; long sum = 0; for (int i = 0; i < n; ++i) { bool sumpos = (sum + a[i]) >= 0; if (sumpos != positive) { while (((sum + a[i]) >= 0) == sumpos) { a[i] += sumpos ? -1 : 1; ++cost; } } if ((sum + a[i]) == 0) { a[i] += sumpos ? -1 : 1; ++cost; } sum += a[i]; positive = !positive; } return cost; } int main(int argc, char *argv[]) { int n; std::cin >> n; int a[1 << 15], b[1 << 15]; for (int i = 0; i < n; ++i) { std::cin >> a[i]; b[i] = a[i]; } std::cout << std::min(calc(true, a, n), calc(false, b, n)) << std::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
python2
n=input() a=map(int,raw_input().split()) #print n #print a a1_sum=0 a1=[] for i in a: a1_sum+=i a1.append(a1_sum) #print a1 #+,- b=[] for i,val in enumerate(a1): for j in b: val+=j if i%2==0: if val>0: pass else: t_val=1-val b.append(t_val) else: if val<0: pass else: t_val=-1-val b.append(t_val) ans1=0 if len(b)==0: ans1=0 else: for i in b: ans1+=abs(i) #-,+ b=[] for i,val in enumerate(a1): for j in b: val+=j if i%2==0: if val<0: pass else: t_val=-1-val b.append(t_val) else: if val>0: pass else: t_val=1-val b.append(t_val) ans2=0 if len(b)==0: ans2=0 else: for i in b: ans2+=abs(i) print min(ans1,ans2)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int d[n]; for (int i = 0; i < n; i++) { cin >> d[i]; } int count = 0; int sum = d[0]; int f = 0; if (d[0] > 0) { f = -1; } if (d[0] < 0) { f = 1; } for (int i = 1; i < n; i++) { sum += d[i]; if (sum == 0) { if (f == 1) { count++; f = -1; sum = 1; continue; } if (f == -1) { count++; f = 1; sum = -1; continue; } } if (sum > 0) { if (f == 1) { f = -1; continue; } if (f == -1) { count += sum + 1; sum = -1; f = 1; continue; } } if (sum < 0) { if (f == -1) { f = 1; continue; } if (f == 1) { count += 1 - sum; sum = 1; f = -1; continue; } } } cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int ans = 0; int sum = 0; for (int i = 0; i < n; i++) { if (i == 0) { sum = a[i]; continue; } if (sum > 0) { if (sum + a[i] >= 0) { ans += sum + a[i] + 1; a[i] -= sum + a[i] + 1; } } else { if (sum + a[i] <= 0) { ans += sum + a[i] + 1; a[i] += sum + a[i] - 1; } } sum += a[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; long long A[100100]; int n; long long count(long long sum) { long long num = 0; for (int i = 1; i < n; i++) { long long a = A[i]; if (sum > 0) { sum += a; if (sum >= 0) { num += (sum + 1); sum = -1; } } else { sum += a; if (sum <= 0) { num += (-sum + 1); sum = 1; } } } return num; } int main() { long long num = 0; cin >> n; for (int i = 0; i < n; i++) { long long a; cin >> a; A[i] = a; } if (A[0] != 0) { num = count(A[0]); } else { num = 1 + min(count(1), count(-1)); } cout << num << 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 unsigned long long MOD = 1000000000 + 7; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } int cnt0 = 0; int sum = 0; for (int i = 0; i < n; i++) { sum += a.at(i); if (i % 2 == 0 && sum >= 0) { cnt0 += 1 + sum; sum = -1; } else if (i % 2 == 1 && sum <= 0) { cnt0 += 1 - sum; sum = 1; } } int cnt1 = 0; sum = 0; for (int i = 0; i < n; i++) { sum += a.at(i); if (i % 2 == 0 && sum <= 0) { cnt1 += 1 - sum; sum = 1; } else if (i % 2 == 1 && sum >= 0) { cnt1 += 1 + sum; sum = -1; } } cout << min(cnt0, cnt1) << 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())) sum_a = a[0] cnt = 0 for i in range(1,n): if sum_a*(sum_a+a[i]) < 0: sum_a += a[i] else: tmp = a[i] a[i] = int(-sum_a -a[i-1]/abs(a[i-1])) sum_a += a[i] cnt += abs(a[i]-tmp) print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using long long = long long; template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long INF = 1e9; long long n; vector<long long> a; long long solve(int x) { long long res = 0; long long sum = x; for (long long i = 0; i < (n - 1); ++i) { if (sum < 0) { if (sum + a[i + 1] > 0) { sum += a[i + 1]; } else { res += 1 - sum - a[i + 1]; sum = 1; } } else { if (sum + a[i + 1] < 0) { sum += a[i + 1]; } else { res += 1 + sum + a[i + 1]; sum = -1; } } } return res; } int main() { cin >> n; a.resize(n); for (long long i = 0; i < (n); ++i) cin >> a[i]; long long ans = INF; chmin(ans, solve(a[0])); chmin(ans, solve(1) + abs(a[0] - 1)); chmin(ans, solve(-1) + abs(a[0] + 1)); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> v(n); for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); i += 1 - 2 * ((0) > (n))) cin >> v[i]; long long ans = LLONG_MAX; long long res1 = 0, res2 = 0; long long som = v[0]; if (v[0] >= 0) { for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 1) if (som < 0) continue; else { res1 += som + 1; som = -1; } else if (som > 0) continue; else { res1 += 1 - som; som = 1; } } res2 = v[0] + 1; som = -1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 0) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } } if (v[0] == 0) ans = min(res1, res2); if (v[0] <= 0) { for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 0) if (som < 0) continue; else { res1 += som + 1; som = -1; } else if (som > 0) continue; else { res1 += 1 - som; som = 1; } } res2 = 1 - v[0]; som = 1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 1) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } } if (v[0] == 0) cout << min({ans, res1, res2}); else cout << min(res1, res2); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(int, input().split())) sum_now=a[0] sum_before=-a[0] count=0 for i in range(n): while sum_now*sum_before>=0: if sum_before==0: sum_now=-a[1]/abs(a[1]) count+=1 else: count+=abs(int(sum_now))+1 sum_now=-sum_before/abs(sum_before) if i!=n-1: sum_before=sum_now sum_now=sum_now+a[i+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; using ll = long long; using ull = unsigned long long; using ld = long double; static constexpr int mod = (int)1e9 + 7; static constexpr int inf = 100100100; static constexpr ll linf = 1e18; static constexpr double eps = 1e-9; static constexpr double pi = 3.14159265359; template <typename T> int sgn(T val) { return (T(0) < val) - (val < T(0)); } int main() { ll N; cin >> N; vector<ll> A; for (ll i = 0; i < N; ++i) { ll a; cin >> a; A.push_back(a); } ll ans = 0; ll S = A[0]; if (S == 0) { S = 1; ans += 1; } for (ll i = 0; i < N - 1; ++i) { int s1 = sgn(S); S += A[i + 1]; int s2 = sgn(S); if (s1 != s2) { if (s2 == 0) { if (s1 > 0) { S = -1; ans += 1; } else { S = 1; ans += 1; } } } else { ans += abs(S) + 1; if (s2 > 0) { S = -1; } else { S = 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
cpp
#include <bits/stdc++.h> using namespace std; int n; int func(vector<int> a, int fugo) { int ans = 0; int offset = 0; for (int i = 0; i < n; i++) { if (i % 2 == fugo) { if (a[i] <= offset) { ans += offset - (a[i] - 1); offset = a[i] - 1; } } else { if (a[i] >= offset) { ans += (a[i] + 1) - offset; offset = a[i] + 1; } } } return ans; } int main() { cin >> n; vector<int> a; int sum_tmp = 0; for (int i = 0; i < n; i++) { int tmp; cin >> tmp; sum_tmp += tmp; a.push_back(sum_tmp); } int ans = min(func(a, 0), func(a, 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
cpp
#include <bits/stdc++.h> using namespace std; int n; long long a[100005]; void solver() { long long cnt = 0; long long sum = a[0]; for (int i = 1; i < n; ++i) { if (sum > 0) { if (sum + a[i] >= 0) { cnt += abs(sum + a[i]) + 1; sum = -1; } else { sum = sum + a[i]; } } else if (sum < 0) { if (sum + a[i] <= 0) { cnt += abs(sum + a[i]) + 1; sum = 1; } else { sum = sum + a[i]; } } } cout << cnt << endl; } int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } solver(); }
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 j = 0; j < N; j++) { cin >> a[j]; } int c1 = 0, c2 = 0; int sum = 0; sum = a[0]; for (int i = 1; i < N; i++) { if (sum < 0 && i == 1) { c1 += 1 - sum; sum = 1; } else sum += a[i]; if (i % 2 == 1 && sum > 0) { c1 += +1 + sum; sum = -1; } if (i % 2 == 0 && sum < 0) { c1 += 1 - sum; sum = 1; } if (sum == 0 && i % 2 == 1) { c1++; sum = -1; } if (sum == 0 && i % 2 == 0) { c1++; sum = 1; } } sum = a[0]; for (int i = 1; i < N; i++) { if (sum > 0 && i == 1) { c2 += +1 + sum; sum = -1; } else sum += a[i]; if (i % 2 == 1 && sum < 0) { c2 += 1 - sum; sum = 1; } if (i % 2 == 0 && sum > 0) { c2 += +1 + sum; sum = -1; } if (sum == 0 && i % 2 == 1) { c2++; sum = 1; } if (sum == 0 && i % 2 == 0) { c2++; sum = -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 <bits/stdc++.h> using namespace std; int main() { bool ch = false; long long N, i; long long ans = 0, count = 0; cin >> N; long long a[N]; cin >> a[0]; ans += a[0]; if (ans > 0) ch = true; else ch = false; if (ans == 0) { count += 1; ans = -1; } for (i = 1; i < N; i++) { cin >> a[i]; if (ch) { if (ans >= -a[i]) { count += ans + a[i] + 1; ans = -1; } else ans += a[i]; ch = false; } else { if (ans <= -a[i]) { count += -ans - a[i] + 1; ans = 1; } else ans += a[i]; ch = true; } } long long con = 0; if (a[0] > 0) { ans = -1; ch = false; } else { ans = 1; ch = true; } con = a[0] + 1; for (i = 1; i < N; i++) { if (ch) { if (ans >= -1 * a[i]) { con += ans + a[i] + 1; ans = -1; } else ans += a[i]; ch = false; } else { if (-1 * ans >= a[i]) { con += -1 * ans - a[i] + 1; ans = 1; } else ans += a[i]; ch = true; } } cout << min(count, con) << 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; 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; } int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long cnt1 = 0, sum; if (a[0] > 0) sum = a[0]; else sum = 1, cnt1 += abs(a[0]) + 1; for (int i = 1; i < n; i++) { long long t = sum + a[i]; if (sum > 0 && t > 0) { if (a[i] >= 0) cnt1 += a[i] + sum + 1; else cnt1 += abs(abs(sum) - abs(a[i])) + 1; sum = -1; } else if (sum < 0 && t < 0) { if (a[i] < 0) cnt1 += a[i] + sum + 1; else cnt1 += abs(abs(sum) - abs(a[i])) + 1; sum = 1; } else if (t == 0) { cnt1++; if (sum > 0) sum = -1; else sum = 1; } else sum += a[i]; } long long cnt2 = 0; if (a[0] < 0) sum = a[0]; else sum = -1, cnt2 += abs(a[0]) + 1; for (int i = 1; i < n; i++) { long long t = sum + a[i]; if (sum > 0 && t > 0) { if (a[i] >= 0) cnt2 += a[i] + sum + 1; else cnt2 += abs(abs(sum) - abs(a[i])) + 1; sum = -1; } else if (sum < 0 && t < 0) { if (a[i] < 0) cnt2 += a[i] + sum + 1; else cnt2 += abs(abs(sum) - abs(a[i])) + 1; sum = 1; } else if (t == 0) { cnt2++; if (sum > 0) sum = -1; else sum = 1; } else sum += a[i]; } cout << min(cnt1, cnt2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> constexpr int kMod = 1000000007; constexpr int kNmax = 1e5 + 1; int sum[kNmax]; int main() { int n; std::cin >> n; std::cin >> sum[0]; for (int i = 1; i < n; ++i) { int a; std::cin >> a; sum[i] = sum[i - 1] + a; } long long cnt1 = 0, cnt2 = 0; long long offset = 0; for (int i = 0; i < n; ++i) { long long v = sum[i] + offset; if (i % 2 == 0) { if (v <= 0) { offset += (-v + 1); cnt1 += (-v + 1); } } else { if (v >= 0) { offset -= v + 1; cnt1 += v + 1; } } } offset = 0; for (int i = 0; i < n; ++i) { long long v = sum[i] + offset; if (i % 2 == 0) { if (v >= 0) { offset -= v + 1; cnt2 += v + 1; } } else { if (v <= 0) { offset += (-v + 1); cnt2 += (-v + 1); } } } std::cout << std::min(cnt1, cnt2) << std::endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys input = sys.stdin.readline sys.setrecursionlimit(2147483647) INF=float("inf") MOD=10**9+7 # A = [ int(input()) for _ in range(N) ] ############################## N = int(input()) A = list(map(int, input().split())) def get_count(summary): count = 0 for i in range(1, N): # print(summary) # 次はマイナス if summary > 0: # 条件を満たしてる? if (summary + A[i]) < 0: summary += A[i] else: # プラスになっちゃってるので修正 summary += A[i] count += abs(-1-summary) summary = -1 # 次はプラス else: if (summary + A[i]) > 0: summary += A[i] else: # マイナスになっちゃってるので修正 summary += A[i] count += abs(1-summary) summary = 1 return count if A[0] > 0: plus = get_count(A[0]) minus = get_count(-1) minus += (A[0]+1) elif A[0] == 0: plus = get_count(1)+1 minus = get_count(-1)+1 else: minus = get_count(A[0]) plus = get_count(1) plus += (abs(plus)+1) print(min(plus, minus))
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 x = 0; x < (N); x++) { cin >> A.at(x); } long long sign = A.at(0); long long ans = 0; for (int x = 0; x < (N - 1); x++) { if (sign < 0) { if (sign + A.at(x + 1) <= 0) { if (x == 0) { if (abs(A.at(x + 1) - (abs(sign) + 1)) < abs(sign - (abs(A.at(x + 1)) + 1))) { ans += abs(A.at(x + 1) - (abs(sign) + 1)); A.at(x + 1) = abs(sign) + 1; } else { ans += abs(sign - (abs(A.at(x + 1)) + 1)); } } else { ans += abs(A.at(x + 1) - (abs(sign) + 1)); A.at(x + 1) = abs(sign) + 1; } } } else { if (sign + A.at(x + 1) >= 0) { if (x == 0) { if (abs(A.at(x + 1) - (abs(sign) + 1)) < abs(sign - (abs(A.at(x + 1)) + 1))) { ans += abs(A.at(x + 1) - (-abs(sign) - 1)); A.at(x + 1) = -abs(sign) - 1; } else { ans += abs(sign - (abs(A.at(x + 1)) + 1)); } } else { ans += abs(A.at(x + 1) - (-abs(sign) - 1)); A.at(x + 1) = -abs(sign) - 1; } } } sign += A.at(x + 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
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]; long long sum = a[0]; long long ans = 0; if (a[0] == 0) { sum = 1; ans++; } for (int i = 1; i < n; i++) { long long sum1 = sum + a[i]; if (sum1 == 0 || sum / abs(sum) == sum1 / abs(sum1)) { int sign = sum / abs(sum); sign *= -1; ans += abs(sum1 - sign); sum1 = sign; } sum = sum1; } if (a[0] == 0) { long long ans2 = 0; sum = -1; ans2++; for (int i = 1; i < n; i++) { long long sum1 = sum + a[i]; if (sum1 == 0 || sum / abs(sum) == sum1 / abs(sum1)) { int sign = sum / abs(sum); sign *= -1; ans2 += abs(sum1 - sign); sum1 = sign; } sum = sum1; } if (ans > ans2) ans = ans2; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) L = [0 for _ in range(n)] L[0] = a[0] for i in range(1, n): L[i] = L[i-1] + a[i] delay = 0 all_over = 0 anti_delay = 0 anti_all_over = 0 if a[0] != 0: antiL = L[:] sign = (a[0] > 0) - (a[0] < 0) for i in range(1, n): L[i] += delay if L[i] <= 0 and sign == -1: delay += 1 - L[i] all_over += 1 - L[i] L[i] = 1 elif L[i] >= 0 and sign == 1: delay -= L[i] + 1 all_over += L[i] + 1 L[i] = -1 sign *= -1 if antiL[0] < 0: sign = 1 anti_delay = 1 - antiL[0] anti_all_over = 1 - antiL[0] else: sign = 1 anti_delay = antiL[0] - 1 anti_all_over = antiL[0] - 1 for i in range(1, n): antiL[i] += anti_delay if antiL[i] <= 0 and sign == -1: anti_delay += 1 - antiL[i] anti_all_over += 1 - antiL[i] antiL[i] = 1 elif antiL[i] >= 0 and sign == 1: anti_delay -= antiL[i] + 1 anti_all_over += antiL[i] + 1 antiL[i] = -1 sign *= -1 print(min(anti_all_over, all_over)) else: posL = L[:] negL = L[:] pos_delay, neg_delay = 1, -1 pos_all_over, neg_all_over = 1, 1 sign = 1 for i in range(1, n): posL[i] += pos_delay if posL[i] <= 0 and sign == -1: pos_delay += 1 - posL[i] pos_all_over += 1 - posL[i] posL[i] = 1 elif posL[i] >= 0 and sign == 1: pos_delay -= posL[i] + 1 pos_all_over += posL[i] + 1 posL[i] = -1 sign *= -1 sign = -1 for i in range(1, n): negL[i] += neg_delay if negL[i] <= 0 and sign == -1: neg_delay += 1 - negL[i] neg_all_over += 1 - negL[i] negL[i] = 1 elif negL[i] >= 0 and sign == 1: neg_delay -= negL[i] + 1 neg_all_over += negL[i] + 1 negL[i] = -1 sign *= -1 print(min(pos_all_over, neg_all_over))
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; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for(int i=0; i<n; i++){ a[i] = sc.nextInt(); } int[] a_sum = new int[n]; a_sum[0] = a[0]; for(int i=1; i<n; i++){ a_sum[i] = a_sum[i-1] + a[i]; } int[] a_sum_copy = new int[n]; for(int i=0; i<n; i++){ a_sum_copy[i] = a_sum[i]; } //奇数番目までの和を正に、偶数番目までの和を負にする int cnt1 = 0; int cnt2 = 0; for(int i=0; i<n; i++){ a_sum[i] += cnt1 + cnt2; if(i%2==0){ if(a_sum[i]<0){ cnt1 += (-a_sum[i]) + 1; a_sum[i] = 1; }else if(a_sum[i]==0){ cnt1 += 1; a_sum[i] = 1; } }else if(i%2==1){ if(a_sum[i]>0){ cnt2 -= a_sum[i] + 1; a_sum[i] = -1; }else if(a_sum[i]==0){ cnt2 -= 1; a_sum[i] = -1; } } } //奇数番目までの和を負に、偶数番目までの和を正にする int cnt3 = 0; int cnt4 = 0; for(int i=0; i<n; i++){ a_sum_copy[i] += cnt3 + cnt4; if(i%2==1){ if(a_sum_copy[i]<0){ cnt3 += (-a_sum_copy[i]) + 1; a_sum_copy[i] = 1; }else if(a_sum_copy[i]==0){ cnt3 += 1; a_sum_copy[i] = 1; } }else if(i%2==0){ if(a_sum_copy[i]>0){ cnt4 -= a_sum_copy[i] + 1; a_sum_copy[i] = -1; }else if(a_sum_copy[i]==0){ cnt4 -= 1; a_sum_copy[i] = -1; } } } if(cnt1-cnt2>cnt3-cnt4){ System.out.print(cnt3-cnt4); }else{ System.out.print(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
UNKNOWN
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; namespace C { public class Program { static void Main(string[] args) { var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }; Console.SetOut(sw); Solve(); Console.Out.Flush(); } public static void Solve() { var N = int.Parse(Console.ReadLine()); var A = Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray(); var answer = (long)1e18; var t = 1; for (var k = 0; k < 2; k++) { t ^= 1; var step = 0; var sum = 0; var prev = t == 0; for (var i = 0; i < N; i++) { sum += A[i]; if (prev == (sum > 0)) { step += Math.Abs(sum) + 1; sum = prev ? -1 : 1; } prev = sum > 0; } answer = Math.Min(answer, step); } Console.WriteLine(answer); } } }
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()) num_list = list(map(int, input().split())) count = 0 sum_ = num_list[0] if sum_ > 0: for i in range(1, n): sum_ += num_list[i] if i%2 == 0: if sum_ <= 0: sum_ += abs(sum_) + 1 count += abs(sum_) + 1 else: if sum_ >= 0: sum_ -= abs(sum_) + 1 count += abs(sum_) + 1 print(count) elif sum_ < 0: for i in range(1, n): sum_ += num_list[i] if i%2 == 1: if sum_ <= 0: sum_ += abs(sum_) + 1 count += abs(sum_) + 1 else: if sum_ >= 0: sum_ += -abs(sum_) - 1 count += abs(sum_) + 1 print(count) else: sum_ = 1 for i in range(1, n): sum_ += num_list[i] if i%2 == 0: if sum_ <= 0: sum_ += abs(sum_) + 1 count += abs(sum_) + 1 else: if sum_ >= 0: sum_ -= -abs(sum_) - 1 count += abs(sum_) + 1 count1 = count sum_ = -1 for i in range(1, n): sum_ += num_list[i] if i%2 == 1: if sum_ <= 0: sum_ += abs(sum_) + 1 count += abs(sum_) + 1 else: if sum_ >= 0: sum_ += abs(sum_) + 1 count += abs(sum_) + 1 count2 = count 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 = [int(i) for i in input().split()] ff = -1 for i in range(n): if A[i] != 0: ff = i break if A[0] != 0: ans = 0 S = A[0] f = A[0]//abs(A[0]) else: if ff == -1: ans = 1 S = 1 f = 1 else: if ff % 2 == 0: ans = 1 S = 1 f = 1 else: ans = 1 S = -1 f = -1 for a in A[1:]: S += a if S == 0: ans += 1 S = -f else: if S/abs(S) != f*(-1): ans += abs(S)+1 S = -f f *= -1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<bits/stdc++.h> using namespace std; #define mod 1000000007 #define ll long long #define mp make_pair #define pb push_back #define ff first #define ss second #define set0(a) memset ((a), 0 , sizeof(a)) #define set1(a) memset((a),-1,sizeof (a)) #define pi pair<int, int> #define ps pair<string, string> #define pl pair<long, long> #define pll pair<long long, long long> #define vll vector<long long> #define vl vector<long> #define vi vector<int> #define vs vector<string> #define vps vector< ps > #define vpi vector< pi > #define vpl vector< pl > #define vpll vector< pll > #define flash ios_base::sync_with_stdio(false); cin.tie(NULL); #define tc(t) for(long long l=0;l<t;l++) #define rep(i,s,n,d) for(long long i=s;i<n;i=i+d) bool sortbysec(const pll &a, const pll &b) { return (a.second < b.second); } void func(void) { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); } int main(){ ll n; cin>>n; ll a[n]; rep(i,0,n,1){ cin>>a[i]; } ll sum[n]={}; sum[0]=a[0]; rep(i,1,n,1){ sum[i]=sum[i-1]+a[i]; } int sum1=a[0]; int count1=0; rep(i,1,n,1){ if(sum1*(sum1+a[i])>=0){ int d=1; if(sum1<0){ d=1; }else{ d=-1; } int dif=abs(sum1+a[i]-d); count1=count1+dif; sum1=d; } else{ sum1=sum1+a[i]; } } cout<<count1<<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
package main import ( "bufio" "fmt" "os" "strconv" ) var sc = bufio.NewScanner(os.Stdin) func nextInt() int { sc.Scan() i, _ := strconv.Atoi(sc.Text()) return i } func main() { sc.Split(bufio.ScanWords) n := nextInt() a := make([]int, n) for i := 0; i < n; i++ { a[i] = nextInt() } sum, cnt := a[0], 0 m, l := 0, 1 if a[0] > 0 { m, l = 1, 0 } for i := 1; i < n; i++ { sum += a[i] if i%2 == m && sum >= 0 { cnt += sum + 1 sum = -1 } if i%2 == l && sum <= 0 { cnt += 1 - sum sum = 1 } } fmt.Println(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
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ABC059Sequence { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); long[] a = new long[n]; string[] vals = Console.ReadLine().Split(' '); for (int i = 0; i < n; i++) a[i] = long.Parse(vals[i]); long num = 0; if(a[0] == 0) { if(a[1] >= 0) { a[0]--; } else { a[0]++; } num++; } long[] cum = new long[n+1]; for(int i=1; i < n; i++) { cum[i] = cum[i - 1] + a[i - 1]; long t; if(cum[i] > 0) { t = cum[i] * -1 - 1; } else { t = cum[i] * -1 + 1; } //Console.WriteLine("target: {0}", t); long u; if(t > 0) { if(a[i] < t) { u = t - a[i]; //Console.WriteLine("u={0}", u); a[i] += u; num += u; } } else { if(a[i] > t) { u = a[i] - t; //Console.WriteLine("u=-{0}", u); a[i] -= u; num += u; } } } Console.WriteLine(num); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; int main(int argc, char const *argv[]) { int n; std::cin >> n; std::vector<int> v(n); std::vector<int> sums(2, 0); for (size_t i = 0; i < n; i++) { std::cin >> v[i]; sums[i % 2] += v[i]; } ull ans = 0; if (sums[0] > sums[1] && v[0] <= 0) { ans = ans + abs(v[0]) + 1; v[0] = 1; } else if (sums[0] < sums[1] && v[0] >= 0) { ans = ans + abs(v[0]) + 1; v[0] = -1; } ll now, pre; now = pre = v[0]; for (size_t i = 1; i < n; i++) { now += v[i]; if (pre * now >= 0) { if (pre > 0) { ans = ans + abs(now) + 1; now = -1; } else if (pre < 0) { ans = ans + abs(now) + 1; now = 1; } } pre = now; } std::cout << ans << '\n'; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> int main(void) { int n; long sum1 = 0; long sum2 = 0; long tmp; long lcount = 0; long rcount = 0; long a[100000]; char input[1500000]; int i = 0, j = 0; int cp = 0, tcp = 0; char tp[12]; tp[12] = '\0'; fgets(input, 1500000, stdin); n = atoi(input); fgets(input, 1500000, stdin); for (i = 0; i < n; i++) { while (input[cp] != ' ' && input[cp] != '\n') { tp[tcp] = input[cp]; tcp++; cp++; } tp[tcp] = '\0'; tcp = 0; cp++; a[i] = atoi(tp); } tmp = a[0]; for (i = 1; i < n; i++) { if (i % 2 == 0) { tmp += a[i]; if (tmp > -1) { lcount += tmp + 1; tmp = -1; } } else { tmp += a[i]; if (tmp < 1) { lcount += 1 - tmp; tmp = 1; } } } tmp = a[0]; for (i = 1; i < n; i++) { if (i % 2 == 1) { tmp += a[i]; if (tmp > -1) { rcount += tmp + 1; tmp = -1; } } else { tmp += a[i]; if (tmp < 1) { rcount += 1 - tmp; tmp = 1; } } } printf("%ld\n", lcount > rcount ? rcount : lcount); 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]; } long long ans = 0; long long sum = 0; for (int i = 0; i < n; i++) { if (i == 0) { sum = a[i]; continue; } if (sum > 0) { if (sum + a[i] >= 0) { ans += sum + a[i] + 1; sum += a[i] - (sum + a[i] + 1); continue; } } else { if (sum + a[i] <= 0) { ans -= sum + a[i] - 1; sum += a[i] - (sum + a[i] - 1); continue; } } sum += a[i]; } sum = 0; int tmp = 0; for (int i = 0; i < n; i++) { if (i == 0) { sum = a[i] > 0 ? -1 : 1; tmp += a[i] > 0 ? a[i] + 1 : -(a[i] - 1); continue; } if (sum > 0) { if (sum + a[i] >= 0) { tmp += sum + a[i] + 1; sum += a[i] - (sum + a[i] + 1); continue; } } else { if (sum + a[i] <= 0) { tmp -= sum + a[i] - 1; sum += a[i] - (sum + a[i] - 1); continue; } } sum += a[i]; } cout << (ans < tmp ? ans : tmp) << 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<int> List(n); for (int i = 0; i < n; i++) { cin >> List.at(i); } int cnt = 0; int Sign = 0; for (int i = 0; i < n; i++) { if (i == 0) { Sign = List.at(i); continue; } if (Sign > 0) { if (Sign + List.at(i) >= 0) { cnt += abs(Sign + List.at(i)) + 1; Sign = -1; } else { Sign += List.at(i); } } else { if (Sign + List.at(i) <= 0) { cnt += abs(Sign + List.at(i)) + 1; Sign = 1; } else { Sign += List.at(i); } continue; } } cout << cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import numpy as np n = int(input()) a = list(map(int, input().split())) s = [] ss = a[0] cnt = 0 c = 1 if(a[0] != 0): s.append(a[0]) else: while (a[c] == 0): c += 1 cnt += 2 cnt += 1 if(np.sign(a[c]) == 1): ss = -1 for i in range(c): if(i%2 != c%2): s.append(-1) else: s.append(1) else: ss = 1 for i in range(c): if(i%2 != c%2): s.append(1) else: s.append(-1) for i in range(c, n): ss += a[i] if(ss == 0): if(np.sign(s[i-1]) == -1): ss += 1 cnt += 1 s.append(1) else: ss -= 1 cnt += 1 s.append(-1) elif(np.sign(s[i-1]) == np.sign(ss)): if(np.sign(s[i-1]) == -1): cnt += abs(1 - ss) ss = 1 s.append(1) else: cnt += abs(-1-ss) ss = -1 s.append(-1) else: s.append(ss) print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) l=[int(i) for i in input().split()] sm=l[0] req=0 pr=1 if sm>0: pass else: req+=-sm+1 sm=1 for i in range(1,n): if pr==1: n1=sm+l[i] if n1<0: sm+=l[i] pass else: sm+=l[i] req+=sm+1 sm=-1 else: n1=sm+l[i] if n1>0: sm+=l[i] pass else: sm+=l[i] req+=(-sm+1) sm=1 pr=1-pr req1=0 pr=0 sm=l[0] if sm<0: pass else: req1+=sm+1 for i in range(1,n): if pr==1: n1=sm+l[i] if n1<0: sm+=l[i] pass else: sm+=l[i] req1+=sm+1 sm=-1 else: n1=sm+l[i] if n1>0: sm+=l[i] pass else: sm+=l[i] req1+=(-sm+1) sm=1 pr=1-pr print(min(req,req1))
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(); long long int sum = vec[0] + vec[1]; int ans = 0; for (long long int i = 2; i < n; i++) { if (sum > 0) { if (sum + vec[i] >= 0) { ans += 1 + (sum + vec[i]); sum = -1; } else { sum += vec[i]; } } else if (sum <= 0) { if (sum + vec[i] <= 0) { ans += 1 - (sum + vec[i]); 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, as2; copy(as.begin(), as.end(), back_inserter(as1)); copy(as.begin(), as.end(), back_inserter(as2)); as1[0] = 1; as2[0] = -1; cout << solve(as) << " " << solve(as1) + abs(1 - as[0]) << " " << solve(as2) + abs(-1 - as[0]) << endl; 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 <bits/stdc++.h> using namespace std; long long cal(int b0, int n, long long* a, long long ans) { long long b[n]; b[0] = b0; for (int i = 1; i < n; i++) { b[i] = b[i - 1] + a[i]; if (b[i] == 0) { ans++; b[i] = -1 * b[i - 1] / b[i - 1]; } if (a[i] * b[i - 1] > 0 || (abs(a[i]) - abs(b[i - 1])) < 0) { ans += abs(a[i] + b[i - 1]) + 1; b[i] = -1 * b[i - 1] / b[i - 1]; } } return ans; } int main() { int n; cin >> n; long long a[n], ans = 0; for (int i = 0; i < n; i++) cin >> a[i]; if (a[0] != 0) { cout << cal(a[0], n, a, ans) << endl; } else { ans++; cout << (cal(1, n, a, ans) > cal(-1, n, a, ans) ? cal(1, n, a, ans) : cal(-1, n, a, ans)) << endl; return 0; } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long cnt = 0; if (a[0] == 0) { for (int j = 1; j < n; j++) { if (a[j] == 0 && j < n - 1) continue; else if (a[j] == 0 && j == n - 1) { a[0] = 1; cnt++; break; } else { cnt++; if (j % 2 == 0 && a[j] > 0) a[0] = 1; else if (j % 2 == 0 && a[j] < 0) a[0] = -1; else if (j % 2 != 0 && a[j] > 0) a[0] = -1; else if (j % 2 != 0 && a[j] > 0) a[0] = 1; } } } long long sumi = a[0]; long long sump = a[0]; for (int i = 0; i < n - 1; i++) { sump += a[i + 1]; if (sumi < 0) { if (sump <= 0) { cnt += 1 - sump; sump = 1; } } else if (sumi > 0) { if (sump >= 0) { cnt += sump + 1; sump = -1; } } sumi = sump; } 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() { int n; cin >> n; vector<int> a(n); vector<int> cumSum(n); for (int i = 0; i < (int)(n); i++) cin >> a[i]; cumSum[0] = a[0]; for (int i = 1; i < n; i++) { cumSum[i] = cumSum[i - 1] + a[i]; } int evenAns = 0; int oddAns = 0; int offset = 0; int ans = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (cumSum[i] + offset > 0) { } else if (cumSum[i] + offset <= 0) { int x = 1 - offset - cumSum[i]; evenAns += abs(x); offset = offset + x; } } else { if (cumSum[i] + offset < 0) { } else if (cumSum[i] + offset >= 0) { int x = -1 - offset - cumSum[i]; evenAns += abs(x); offset = offset + x; } } } offset = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (cumSum[i] + offset < 0) { } else if (cumSum[i] + offset >= 0) { int x = -1 - offset - cumSum[i]; oddAns += abs(x); offset = offset + x; } } else { if (cumSum[i] + offset > 0) { } else if (cumSum[i] + offset <= 0) { int x = 1 - offset - cumSum[i]; oddAns += abs(x); offset = offset + x; } } } cout << min(evenAns, oddAns) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long a[100000]; long long b[100001]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] += a[i]; b[i + 1] = b[i]; } long long sum = 0; if (b[0] == 0) { int i = 0; while (a[i] == 0) { i++; } if (i % 2 == 0) { b[0] = 1; } else { b[0] = -1; } sum++; } long long sum2 = 0; for (int i = 1; i < n; i++) { if (b[i] == 0) { if (b[i - 1] > 0) { sum2 -= 1; b[i] = -1; sum++; } else { sum2++; b[i] = 1; sum++; } } else { if (b[i - 1] < 0 && b[i] < 0) { sum2 += (0 - b[i] + 1); sum += (0 - b[i] + 1); b[i] = 1; } else if (b[i - 1] > 0 && b[i] > 0) { sum2 -= (b[i] + 1); sum += (b[i] + 1); b[i] = -1; } } b[i + 1] += sum2; } cout << sum << endl; cin >> 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
python3
n=int(input()) A=list(map(int,input().split())) a,ans=A[0],0 for i in range(1,n): if a*(a+A[i])<=-1:a=a+A[i] else: ans+=abs(a+A[i])+1 if a<=-1:a=1 else:a=-1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) cnt = 0 sum = [0]*n sum[0] = a[0] for i in range(1,n): sum[i] = sum[i-1]+a[i] if sum[i]*sum[i-1]<0: continue elif sum[i-1]*a[i]<0: cnt += abs(sum[i-1])-abs(a[i])+1 sum[i] = -(sum[i-1]//abs(sum[i-1])) else: cnt += abs(sum[i-1])+abs(a[i])+1 sum[i] = -(sum[i-1]//abs(sum[i-1])) print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<long long int> A(N); long long int s = 0LL; vector<long long int> V(N); for (int i = 0; i < N; i++) { cin >> A[i]; s += A[i]; V[i] = s; } long long int tmp1 = 0LL; long long int tmp2 = 0LL; for (int i = 0; i < N - 1; i++) { if (i % 2 == 0) { if (V[i] > 0) continue; else { tmp1 += 1LL - V[i]; V[i] = 1LL; V[i + 1] = 1LL + A[i + 1]; } } else { if (V[i] < 0) continue; else { tmp1 += -1LL - V[i]; V[i] = -1LL; V[i + 1] = -1LL + A[i + 1]; } } } for (int i = 0; i < N - 1; i++) { if (i % 2 == 1) { if (V[i] > 0) continue; else { tmp2 += 1LL - V[i]; V[i] = 1LL; V[i + 1] = 1LL + A[i + 1]; } } else { if (V[i] < 0) continue; else { tmp2 += -1LL - V[i]; V[i] = -1LL; V[i + 1] = -1LL + A[i + 1]; } } } cout << min(tmp1, tmp2); }
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{ static int N; static int[] a; public static void main(String args[]){ Scanner sc = new Scanner(System.in); N = sc.nextInt(); a = new int[N]; int[] s1 = new int[N]; int[] s2 = new int[N]; for(int i = 0; i < N; i++){ a[i] = sc.nextInt(); if(i == 0){ s1[i] = a[i]; s2[i] = a[i]; } else { s1[i] = a[i] + s1[i-1]; s2[i] = a[i] + s2[i-1]; } } sc.close(); System.out.println(Math.min(plus(s1), minus(s2))); } static int plus(int[] s){ int count = 0; for(int i = 0; i < N; i++){ if(i%2==0){ if(s[i] > 0) continue; else{ count+= 1-s[i]; for(int j = i+1; j < N; j++){ s[j] += 1-s[i]; } } } if(i%2!=0){ if(s[i] < 0) continue; else{ count+= s[i] + 1; for(int j = i+1; j < N; j++){ s[j] -= s[i] + 1; } } } } return count; } static int minus(int[] s){ int count = 0; for(int i = 0; i < N; i++){ if(i%2!=0){ if(s[i] > 0) continue; else{ count+= 1-s[i]; for(int j = i+1; j < N; j++){ s[j] += 1-s[i]; } } } if(i%2==0){ if(s[i] < 0) continue; else{ count+= s[i] + 1; for(int j = i+1; j < N; j++){ s[j] -= s[i] + 1; } } } } return 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, 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; sum = 0; if (a[0] == 0) { a[0] = 1; count++; } for (int i = 0; i < n - 1; i++) { bsum = sum; sum += a[i]; if (sum * (sum + a[i + 1]) >= 0) { x = abs(sum + a[i + 1]) + 1; s = abs(sum); if (a[i] * a[i + 1] < 0) { if (s - 1 > x) { a[i] = a[i] > 0 ? a[i] - x : a[i] + x; sum = bsum + a[i]; } else { a[i] = a[i] > 0 ? a[i] - (s - 1) : a[i] + (s - 1); sum = bsum + a[i]; s = x - (s - 1); a[i + 1] = a[i + 1] > 0 ? a[i + 1] + s : a[i + 1] - s; } } else { if (a[i + 1] == 0) { if (sum < 0) a[i + 1] = s + 1; else a[i + 1] = -s - 1; } else { a[i + 1] = a[i + 1] > 0 ? -s - 1 : s + 1; } } count += x; } } sum = 0; count2 = 0; if (b[0] == 0) { b[0] = -1; count2++; } else { count2 = abs(b[0]) + 1; b[0] = b[0] > 0 ? -1 : 1; } for (int i = 0; i < n - 1; i++) { bsum = sum; sum += b[i]; if (sum * (sum + b[i + 1]) >= 0) { x = abs(sum + b[i + 1]) + 1; s = abs(sum); if (b[i] * b[i + 1] < 0) { if (s - 1 > x) { b[i] = b[i] > 0 ? b[i] - x : b[i] + x; sum = bsum + b[i]; } else { b[i] = b[i] > 0 ? b[i] - (s - 1) : b[i] + (s - 1); sum = bsum + b[i]; s = x - (s - 1); b[i + 1] = b[i + 1] > 0 ? b[i + 1] + s : b[i + 1] - s; } } else { if (b[i + 1] == 0) { if (sum < 0) b[i + 1] = s + 1; else b[i + 1] = -s - 1; } else { b[i + 1] = b[i + 1] > 0 ? -s - 1 : s + 1; } } count2 += x; } } 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 <bits/stdc++.h> static int solve(const std::vector<int>& va, int initSum, int initCnt = 0) { int sum = initSum; int 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
python3
n=int(input()) A=list(map(int,input().split())) S=A[0] ans=0 for i in A[1:]: if S*(S+i)<0: S+=i else: ans+=abs(S+i)+1 if S<0: S=1 else: S=-1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = [int(x) for x in input().split()] sum_b = 0 cnt_b = 0 sum_c = 0 cnt_c = 0 for i,a in enumerate(A): sum_b += a if (sum_b > 0) != ((i % 2) > 0): cnt_b += abs(sum_b) + 1 sum_b = 1 if ((i % 2) > 0) else -1 sum_c += a if (sum_c > 0) == ((i % 2) > 0): cnt_c += abs(sum_c) + 1 sum_c = -1 if ((i % 2) > 0) else 1 print(min(cnt_b, cnt_c))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n; cin >> n; vector<int> a(n); for (int i = (int)(0); i < (int)(n); i++) cin >> a[i]; int ans = 0; int sum = a[0]; for (int i = (int)(0); i < (int)(n - 1); i++) { if (sum < 0) { if (a[i + 1] <= abs(sum)) { ans += abs(sum) - a[i + 1] + 1; a[i + 1] += abs(sum) - a[i + 1] + 1; } sum += a[i + 1]; } else if (sum > 0) { if (a[i + 1] >= -sum) { ans += a[i + 1] + sum + 1; a[i + 1] -= a[i + 1] + sum + 1; } sum += a[i + 1]; } else { if (a[i + 1] > 0) { ans += 1; sum += a[i + 1] - 1; } else if (a[i + 1] < 0) { ans += 1; sum += a[i + 1] + 1; } else { ans += 3; if (a[i + 2] >= 0) sum += -1; else sum += 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
cpp
#include <bits/stdc++.h> using namespace std; using ll = int64_t; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int DX[] = {1, 1, 0, -1, -1, -1, 0, 1}; int DY[] = {0, -1, -1, -1, 0, 1, 1, 1}; int n; ll hoge(ll a[]) { ll ans = 0; ll temp = 0; for (int(i) = 0; (i) < (n); (i)++) { if (temp > 0 && temp + a[i] > 0) { ans += abs(-1 - temp - a[i]); temp = -1; } else if (temp < 0 && temp + a[i] < 0) { ans += abs(1 - temp - a[i]); temp = 1; } else if (temp + a[i] == 0) { if (temp > 0) { temp = -1; } else { temp = 1; } ans += 1; } else { temp += a[i]; } } return ans; } void solve() { cin >> n; ll a[n]; for (int(i) = 0; (i) < (n); (i)++) cin >> a[i]; ll ans1 = hoge(a); ll temp = 0; if (a[0] > 0) { temp += (a[0] * (-1) - 1); a[0] = -1; } else if (a[0] < 0) { temp = (a[0] * (-1) + 1); a[0] = 1; } else { temp = 1; a[0] = -1; } ll ans2 = hoge(a) + temp; cout << min(ans1, ans2) << endl; } int main() { 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
UNKNOWN
using System; using System.Linq; class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); var array = Console.ReadLine().Split(' ').Select(i => int.Parse(i)).ToList(); int ans = 0; // array[0]での符号 int sign0 = 0; for (int i = 0; i < n; i++) { var sum = array.Take(i+1).Sum(); if(sum != 0) { sign0 = (sum > 0) ? 1 : -1; int j = i; while (j > 0) { sign0 *= -1; j--; } break; } } // もし最後まで0なら調整 if(sign0 == 0) { array[0] += 1; sign0 = 1; ans += 1; } int sign = -sign0; int Sum = 0; // メインloop for (int i = 0; i < n; i++) { sign *= -1; Sum += array[i]; if (Sum * sign <= 0) { ans -= (Sum * sign-1); Sum = 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
python3
n = int(input()) A = list(map(int, input().split())) ans = 0 pv = A[0] flg = (1 if pv >= 0 else -1) for i in A[1:]: pv += i if pv >= 0 and flg == 1: ans += pv + 1 pv = -1 flg = -1 elif pv <= 0 and flg == -1: ans += abs(pv) + 1 pv = 1 flg = 1 elif flg == 1: flg = -1 else: flg = 1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) A=list(map(int,input().split())) def solve(a,f): ans=0 for i in range(1,n): a[i]+=a[i-1] if not f: if a[i]>=0: ans+=a[i]+1 a[i]=-1 else: if a[i]<=0: ans+=a[i]*-1+1 a[i]=1 # print(a,ans,f) f^=True return ans print(min(solve(A,True),solve(A,False)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N_MAX = 100000; int N; int a[N_MAX]; int All; int sum[N_MAX]; int sum2[N_MAX]; int main() { All = 0; cin >> N; for (int i = 0; i < N; i++) { cin >> a[i]; All += a[i]; sum[i] = All; sum2[i] = All; } int sigh = 1; int ans1 = 0; int dif = 0; for (int i = 0; i < N; i++) { sum[i] += dif; if (sum[i] == 0) { dif += sigh; ans1 += 1; } else if (((sum[i]) / abs((sum[i]))) != sigh) { ans1 += (abs(sum[i]) + 1); int temp = sum[i]; dif += (abs(temp) + 1) * sigh; } sigh *= -1; } sigh = -1; int ans2 = 0; dif = 0; for (int i = 0; i < N; i++) { sum2[i] += dif; if (sum2[i] == 0) { dif += sigh; ans2 += 1; } else if (((sum2[i]) / abs((sum2[i]))) != sigh) { ans2 += (abs(sum2[i]) + 1); int temp = sum2[i]; dif += (abs(temp) + 1) * sigh; } sigh *= -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
UNKNOWN
n = gets.to_i arr = gets.chomp.split(" ").map(&:to_i) $count = 0 def check(i,arr,t) if i > arr.size - 1 arr[t] += 1 $count += 1 return end if arr[i] > 0 arr[t] -= 1 $count += 1 elsif arr[i] < 0 arr[t] += 1 $count += 1 else check(i+1,arr,t) end end sum = arr[0] + arr[1] if sum == 0 check(2,arr,1) end sum = arr[0] + arr[1] (2...arr.size).each do |i| diff = sum + arr[i] # puts %(sum : #{sum}) # puts %(diff : #{diff}) if sum > 0 if diff > 0 arr[i] -= diff.abs+1 $count += diff.abs+1 elsif diff == 0 check(i+1,arr,i) end else if diff < 0 arr[i] += diff.abs+1 $count += diff.abs+1 elsif diff == 0 check(i+1,arr,i) end end sum += arr[i] end #p arr puts $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<int> a(n + 1); for (int i = 0; i < n; i++) { cin >> a.at(i); } vector<int> b(n + 1); b[0] = a[0]; int ansa = 0, ansb = 0, sum = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (b[i] >= 0) { ansa += 1 + b[i]; b[i] = -1; b[i + 1] = b[i] + a[i + 1]; } else b[i + 1] = b[i] + a[i + 1]; } else { if (b[i] <= 0) { ansa += 1 - b[i]; b[i] = 1; b[i + 1] = b[i] + a[i + 1]; } else b[i + 1] = b[i] + a[i + 1]; } } b[0] = a[0]; for (int i = 0; i < n; i++) { if (i % 2 == 1) { if (b[i] >= 0) { ansb += 1 + b[i]; b[i] = -1; b[i + 1] = b[i] + a[i + 1]; } else b[i + 1] = b[i] + a[i + 1]; } else { if (b[i] <= 0) { ansb += 1 - b[i]; b[i] = 1; b[i + 1] = b[i] + a[i + 1]; } else b[i + 1] = b[i] + a[i + 1]; } } int ans = min(ansa, ansb); 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
#include <bits/stdc++.h> long long sign(long long n) { if (n > 0) return 1; else if (n < 0) return -1; return 0; } long long myabs(long long n) { return (n > 0) ? n : (-n); } long long calc(long long *a, int n, long long bias, long long cost) { int i; assert(a[0] != 0); long long sum = a[0]; long long sum_eval_prev = sum + bias; for (i = 1; i < n; i++) { sum += a[i]; long long sum_eval = sum + bias; if (sign(sum_eval_prev) == sign(sum_eval) || sign(sum_eval) == 0) { bias += -1 * sign(sum_eval_prev) - sum_eval; cost += myabs(-1 * sign(sum_eval_prev) - sum_eval); } sum_eval_prev = sum + bias; } return cost; } int main(void) { int n; scanf("%d\n", &n); long long a[n]; int i; for (i = 0; i < n; i++) { scanf("%lld ", &a[i]); } long long ret; if (a[0] == 0) { long long r0, r1; a[0] = 1; r0 = calc(a, n, 0, 1); a[0] = -1; r1 = calc(a, n, 0, 1); if (r0 < r1) ret = r0; else ret = r1; } else { ret = calc(a, n, 0, 0); } printf("%lld\n", ret); 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(x) for x in input().split()] res = 0 x = 0 for i in range(n-1): x += a[i] sign = (x // abs(x)) * (-1) tmp = sign - (x + a[i+1]) if sign < 0: tmp = min(tmp, 0) else: tmp = max(tmp, 0) res += abs(tmp) a[i+1] += tmp 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
UNKNOWN
#include <bits/stdc++.h> int main(void) { int n; long long int a[100000] = {0}, fugo, dif, ans = 0, min = 100000000000000; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%ld", &a[i]); } for (fugo = 0; fugo <= 1; fugo++) { ans = 0; int b[100001] = {0}; for (int i = 0; i < n; i++) { dif = 0; b[i + 1] = b[i] + a[i]; if ((i + 1) % 2 == fugo) { if (b[i + 1] <= 0) { dif += -1 - b[i]; b[i] += dif; dif += 1 - b[i + 1] - dif; b[i + 1] += dif; ans += dif; } } else { if (b[i + 1] >= 0) { dif += b[i] - 1; b[i] -= dif; dif += b[i + 1] + 1 - dif; b[i + 1] -= dif; ans += dif; } } } if (min > ans) min = ans; } printf("%d\n", min); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vs = vector<string>; using vll = vector<long long int>; const int MOD = 1e9 + 7; int Get_Digit(long long int A) { int digits = 0; while (A > 0) { A /= 10; digits++; } return digits; } int main() { int n; cin >> n; vll a(n); for (int i = 0; i < n; i++) cin >> a[i]; int count = 0; int inc = 0; if (a[0] == 0) { count++; a[0]++; inc++; } vll sum(n); sum[0] = a[0]; for (int i = 0; i < n - 1; i++) sum[i + 1] = sum[i] + a[i + 1]; for (int i = 0; i < n - 1; i++) { sum[i + 1] += inc; if (sum[i] > 0) { while (sum[i + 1] >= 0) { count++; inc--; } } else if (sum[i] < 0) { while (sum[i + 1] <= 0) { count++; inc++; } } } 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
n = int(input()) a = list(map(int,input().split())) acc = [a[0]] for i in range(1, n): acc.append(acc[-1] + a[i]) res = 10 ** 100 def f(fir): prv_pos = fir > 0 margin = fir - a[0] ret = abs(margin) for i in range(1, n): cur = margin + acc[i] if prv_pos and cur < 0: prv_pos = not prv_pos continue if not prv_pos and cur > 0: prv_pos = not prv_pos continue if prv_pos: need = -1 sa = need - cur margin += sa ret += abs(sa) else: need = 1 sa = need - cur margin += sa ret += abs(sa) prv_pos = not prv_pos return ret res = min(res, f(1)) res = min(res, f(-1)) res = min(res, f(a[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
python3
import numpy as np n = int(input()) a = list(map(int, input().split())) c = 0 sum = a[0] for i in range (1, n): while np.sign(sum + a[i]) == np.sign(sum) or sum + a[i] == 0: c += 1 if sum > 0: a[i] -= 1 else: a[i] += 1 sum += a[i] 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
python3
#!usr/bin/env python3 from collections import defaultdict from collections import deque from heapq import heappush, heappop import sys import math import bisect import random import itertools sys.setrecursionlimit(10**5) stdin = sys.stdin bisect_left = bisect.bisect_left bisect_right = bisect.bisect_right def LI(): return list(map(int, stdin.readline().split())) def LF(): return list(map(float, stdin.readline().split())) def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split())) def II(): return int(stdin.readline()) def IF(): return float(stdin.readline()) def LS(): return list(map(list, stdin.readline().split())) def S(): return list(stdin.readline().rstrip()) def IR(n): return [II() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] def FR(n): return [IF() for _ in range(n)] def LFR(n): return [LI() for _ in range(n)] def LIR_(n): return [LI_() for _ in range(n)] def SR(n): return [S() for _ in range(n)] def LSR(n): return [LS() for _ in range(n)] mod = 1000000007 inf = float('INF') #A def A(): a = input().split() a = list(map(lambda x: x.capitalize(), a)) a,b,c = a print(a[0]+b[0]+c[0]) return #B def B(): a = II() b = II() if a > b: print("GREATER") if a < b: print("LESS") if a == b: print("EQUAL") return #C def C(): II() a = LI() def f(suma, b): for i in a[1:]: if suma * (suma + i) <= 0: suma += i continue b += (abs(suma + i) + 1) suma = (-1 * (suma > 0)) or 1 return b if a[0] == 0: ans = min(f(1, 1), f(-1, 1)) else: ans = min(f(a[0], 0), f(-a[0], 2 * abs(a[0]))) print(ans) return #D def D(): s = S() for i in range(len(s) - 1): if s[i] == s[i+1]: print(i + 1, i + 2) return for i in range(len(s) - 2): if s[i] == s[i + 2]: print(i + 1, i + 3) return print(-1, -1) return #Solve if __name__ == '__main__': 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; const int inf = 999999999; const double pi = acos(-1); long long a[100005] = {}; int main() { unsigned long long ans = 0; long long wa = 0, g = 0, k = 0; int n; cin >> n; for (int i = (0); i < (int)(n); i++) { cin >> a[i]; if (i % 2 == 0) { if (a[i] > 0) g++; else g--; } else { if (a[i] > 0) k++; else k--; } } if (abs(g) > abs(k)) { if (g > 0) { wa = a[0]; if (wa < 0) { ans += 1 - wa; wa = 1; } } else { wa = a[0]; if (wa > 0) { ans += wa + 1; wa = -1; } } } else { if (k > 0) { wa = a[0]; if (wa > 0) { ans += wa + 1; wa = -1; } } else { wa = a[0]; if (wa < 0) { ans += 1 - wa; wa = 1; } } } for (int i = (1); i < (int)(n); i++) { if (wa >= 0) { long long tes = wa + a[i]; if (tes < 0) { wa = tes; } else { ans += (unsigned long long)(-(-1 - tes)); wa = -1; } } else { long long tes = wa + a[i]; if (tes > 0) { wa = tes; } else { ans += (unsigned long long)(1 - tes); wa = 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; 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 (sum * (sum + a[i + 1]) > 0 && i < (n - 1)) { if (sum > 0) { while (sum >= 0) { ans++; sum--; } } else { while (sum < 0) { ans++; sum++; } } } else if (sum == 0) { if ((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; int main(void) { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; int op = 0; int prev_sum = 0; for (int i = 0; i < n; i++) { int new_sum = prev_sum + v[i]; if (i == 0 && v[i] != 0) prev_sum = new_sum; else if (i == 0 && v[i] == 0) { op += 1; prev_sum = 1; } else if (prev_sum >= 0 && new_sum >= 0) { op += new_sum + 1; prev_sum = -1; } else if (prev_sum <= 0 && new_sum <= 0) { op += -new_sum + 1; prev_sum = 1; } else prev_sum = new_sum; } prev_sum = 0; int op2 = 0; for (int i = 0; i < n; i++) { int new_sum = prev_sum + v[i]; if (i == 0 && v[i] != 0) prev_sum = new_sum; else if (i == 0 && v[i] == 0) { op2 += 1; prev_sum = -1; } else if (prev_sum >= 0 && new_sum >= 0) { op2 += new_sum + 1; prev_sum = -1; } else if (prev_sum <= 0 && new_sum <= 0) { op2 += -new_sum + 1; prev_sum = 1; } else prev_sum = new_sum; } cout << min(op, op2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys def input(): return sys.stdin.readline().strip() def mapint(): return map(int, input().split()) sys.setrecursionlimit(10**9) N = int(input()) As = list(mapint()) cum = As.pop(0) ans = 0 for a in As: if cum*(cum+a)>=0: ans += abs(cum+a)+1 cum = -1 if cum>0 else 1 else: cum += a print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import copy N = int(input()) a_list = list(map(int,input().split())) a_listg = copy.deepcopy(a_list) a_sumg = 0 xg = 0 for i in range(10**10): if a_listg[0] < 1: a_listg[0] += 1 xg += 1 else: break a_sumg += a_listg[0] for j in range(1,N): if j%2 != 0: for k in range(10**10): if a_sumg + a_listg[j] > -1: a_listg[j] -= 1 xg += 1 else: a_sumg += a_listg[j] break else: for l in range(10**10): if a_sumg + a_listg[j] < 1: a_listg[j] += 1 xg += 1 else: a_sumg += a_listg[j] break a_listk = copy.deepcopy(a_list) a_sumk = 0 xk = 0 for m in range(10**10): if a_listk[0] > -1: a_listk[0] -= 1 xk += 1 else: break a_sumk += a_listk[0] for n in range(1,N): if n%2 == 0: for k in range(10**10): if a_sumk + a_listk[n] > -1: a_listk[n] -= 1 xk += 1 else: a_sumk += a_listk[n] break else: for o in range(10**10): if a_sumk + a_listk[n] < 1: a_listk[n] += 1 xk += 1 else: a_sumk += a_listk[n] break print(min([xg,xk]))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); int sum=0; long count=0; for(int i=0;i<n-1;i++){ sum+=a[i]; if(sum>0){ if(sum+a[i+1]>=0){ count+=sum+a[i+1]+1; a[i+1]-=sum+a[i+1]+1; } }else if(sum<0){ if(sum+a[i+1]<=0){ count+=-1*(sum+a[i+1])+1; a[i+1]+=-1*(sum+a[i+1])+1; } } } System.out.println(count); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i = 0; i < (n); ++i) #define rep2(i,s,n) for(int i = (s); i < (n); ++i) #define ll long long #define ld long double #define P pair<ll,ll> #define all(v) v.begin(),v.end() const ll mod = 1e9+7; const ll INF = 1e18; const double pi = acos(-1.0); int main(void) { ll n; cin>>n; vector<ll> a(n); rep(i,n) cin>>a[i]; ll ans=0,sum=0; rep(i,n){ sum+=a[i]; if(i%2==0){ if(sum<=0){ ans+=abs(1-sum); sum=1; } else{ if(sum>=0){ ans+=abs(1-sum); sum=-1; } } } } ll tmp=0,sum=0; rep(i,n){ sum+=a[i]; if(i%2==1){ if(sum<=0){ tmp+=abs(1-sum); sum=1; } else{ if(sum>=0){ tmp+=abs(1-sum); sum=-1; } } } } ans = min(ans,tmp); cout<<ans<<endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v.at(i); } int s = 0; if (v.at(0) == 0) { v.at(0)++; s++; } int t = v.at(0); if (v.at(0) > 0) { for (int i = 1; i < n; i += 2) { t += v.at(i); while (t >= 0) { s++; t--; } if (i + 1 < n) { t += v.at(i + 1); while (t <= 0) { s++; t++; } } } } else { for (int i = 1; i < n; i += 2) { t += v.at(i); while (t <= 0) { s++; t++; } if (i + 1 < n) { t += v.at(i + 1); while (t >= 0) { s++; t--; } } } } 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
python3
n = int(input()) a = list(map(int, input().split())) cnt=0 for i in range(1,n): # 条件満たすまでループ for _ in range(3): print(a) now_tmp = sum(a[:i]) next_tmp = sum(a[:i+1]) print(i, now_tmp, next_tmp) # 符号が逆転していればOK かつ 現在までの総和が0でない # 異なる符号を掛けるとマイナスになる if now_tmp * next_tmp <0 and now_tmp !=0: break else: # 現在の合計がマイナスの場合 if now_tmp < 0: a[i] += -next_tmp+1 cnt +=abs(next_tmp+1) # 現在の合計がプラスの場合 elif now_tmp > 0 : a[i] += next_tmp-1 cnt +=abs(next_tmp+1) # 現在の合計が0の場合 elif now_tmp == 0 : # 1個前がプラスの場合、 if sum(a[:i-1]) > 0: a[i] += -next_tmp+1 cnt +=abs(next_tmp+1) # 1個前がマイナスの場合 else: a[i] += next_tmp+1 cnt +=abs(next_tmp+1) print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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 sum1, sum2; int ans1 = 0; int ans2 = 0; for (int i = 0; i < n; i++) { sum1 += a[i]; sum2 += a[i]; if (i % 2) { ans1 += max(1 - sum1, 0); sum1 = max(1, sum1); ans2 += max(1 + sum2, 0); sum2 = min(-1, sum2); } else { ans1 += max(1 + sum1, 0); sum1 = min(-1, sum1); ans2 += max(1 - sum2, 0); sum2 = max(1, sum2); } } 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
python3
import numpy as np n=int(input()) a=list(map(int,input().split())) r=[0] for i in range(n): r.append(r[i]+a[i]) r.pop(0) pm=[1-2*(i%2) for i in range(n)] mp=[1-2*((i+1)%2) for i in range(n)] sum1,sum2=0,0 sousa1,sousa2=0,0 for i in range(n): if np.sign(r[i]+sousa1) != pm[i]: sum1+=abs(pm[i]-r[i]-sousa1) sousa1=pm[i]-r[i]-sousa1 for i in range(n): if np.sign(r[i]+sousa2) != mp[i]: sum2+=abs(mp[i]-r[i]-sousa2) sousa2=mp[i]-r[i]-sousa2 print(min(sum1,sum2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import copy import sys input = sys.stdin.readline N = int(input()) a = list(map(int, input().split())) ans1, ans2 = 0, 0 f = a[0] for i in range(1, N): if f * (f + a[i]) < 0: f += a[i] continue ans1 += abs(f + a[i]) + 1 if f > 0: f = -1 else: f = 1 f = -a[0] for i in range(1, N): if f * (f + a[i]) < 0: f += a[i] continue ans2 += abs(f + a[i]) + 1 if f > 0: f = -1 else: f = 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
n = int(input()) numbers = list(map(int, input().split())) counter = 0 sum_in = 0 sum_in1 = numbers[0] if numbers[0] >= 0: for i in range(len(numbers)): sum_in += numbers[i] if i % 2 == 0: if sum_in <= 0: sub = abs(sum_in) + 1 sum_in += sub numbers[i] += sub counter += sub else: if sum_in >= 0: sub = abs(sum_in) + 1 sum_in -= sub numbers[i] -= sub counter += sub else: for i in range(len(numbers)): sum_in += numbers[i] if i % 2 != 0: if sum_in <= 0: sub = abs(sum_in) + 1 sum_in += sub numbers[i] += sub counter += sub else: if sum_in >= 0: sub = abs(sum_in) + 1 sum_in -= sub numbers[i] -= sub counter += sub print(str(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
python3
n = int(input()) a = list(map(int, input().split())) change = 0 if a[0]==0: i=0 while a[i]==0: i+=1 if a[i]<0: a[i-1]=1 for idx in range(i-2,-1,-1): a[idx]=a[idx+1]*-1 if a[i]>0: a[i-1]=-1 for idx in range(i-2,-1,-1): a[idx]=a[idx+1]*-1 change += i sum = a[0] for i in range(1,n): val = 0 tempsum = sum+a[i] if sum < 0 and tempsum <=0: val = 1 - tempsum if sum > 0 and tempsum >=0: val = -1 - tempsum sum = tempsum + val change += abs(val) print(change)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
input() iter_ = iter(map(int, input().rstrip("\n").split())) sum_ = next(iter_) ans = 0 for num in iter_: presum = sum_ sum_ += num while presum * sum_ >= 0: sum_ += -1 if presum > 0 else 1 ans += 1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int a[1009]; int b[1009]; int main() { int n, hh, sum = 0; while (~scanf("%d", &n)) { int s = 0; for (int g = 0; g < n; g++) { scanf("%d", &a[g]); s += a[g]; b[g] = s; } int i; for (i = 1; i < n; i++) { if (b[i] > 0 && b[i - 1] > 0 || b[i] < 0 && b[i - 1] < 0 || b[i] == 0) { hh = abs(b[i]) + 1; if (b[i] < 0) { int x = abs(b[i] + 1); for (int j = i; j < n; j++) { b[j] = b[j] + x; } } else if (b[i] > 0) { int y = abs(b[i] + 1); for (int t = i; t < n; t++) { b[t] = b[t] - y; } } else if (b[i] == 0) { if (b[i - 1] > 0) { int c = 1; for (int t = i; t < n; t++) { b[t] = b[t] - c; } } if (b[i - 1] < 0) { int c = 1; for (int t = i; t < n; t++) { b[t] = b[t] + c; } } } sum += hh; } } printf("%d\n", sum); sum = 0; } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; using ii = pair<int, int>; using vvi = vector<vi>; using vii = vector<ii>; using gt = greater<int>; using minq = priority_queue<int, vector<int>, gt>; using P = pair<ll, ll>; const ll LINF = 1e18L + 1; const int INF = 1e9 + 1; int main() { int n; cin >> n; vi a(n); for (int i = 0; i < (n); ++i) cin >> a[i]; ll ans = INF; ll sum = 0; ll num = 0; for (int i = 0; i < (n); ++i) { sum += a[i]; if (i & 1) { if (sum >= 0) { num += (sum + 1); sum = -1; } } else { if (sum <= 0) { num += (-sum + 1); sum = 1; } } } ans = min(ans, num); num = 0; sum = 0; for (int i = 0; i < (n); ++i) { sum += a[i]; if (i & 1) { if (sum <= 0) { num += (-sum + 1); sum = 1; } } else { if (sum >= 0) { num += (sum + 1); sum = -1; } } } ans = min(ans, num); 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
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 begin read(a[i]); 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; 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> using namespace std; template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } long long gcd(long long x, long long y) { long long tmp = 0; if (x < y) { tmp = x; x = y; y = tmp; } while (y > 0) { long long r = x % y; x = y; y = r; } return x; } long long lcm(long long x, long long y) { return x / gcd(x, y) * y; } const int MAX = 1e6 + 1; const long long MOD = 1e9 + 7; long long fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } long long kaijo(long long k) { long long sum = 1; for (long long i = 1; i <= k; ++i) { sum *= i; sum %= 1000000000 + 7; } return sum; } 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; } vector<bool> IsPrime; void sieve(size_t max) { if (max + 1 > IsPrime.size()) { IsPrime.resize(max + 1, true); } IsPrime[0] = false; IsPrime[1] = false; for (size_t i = 2; i * i <= max; ++i) if (IsPrime[i]) for (size_t j = 2; i * j <= max; ++j) IsPrime[i * j] = false; } struct UnionFind { vector<int> par; UnionFind(int n) : par(n, -1) {} int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return true; } int size(int x) { return -par[root(x)]; } }; long long count(int n, int a) { long long bunshi = 1; for (int i = 0; i < a; i++) { bunshi *= (n - i); bunshi %= MOD; } long long bunbo = 1; for (int i = 1; i < a + 1; i++) { bunbo *= i; bunbo %= MOD; } bunbo = modpow(bunbo, MOD - 2, MOD); return (bunshi * bunbo) % MOD; } vector<long long> divisor(long long n) { vector<long long> ret; for (long long i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(begin(ret), end(ret)); return (ret); } const long long INF = 1e18; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int flg = 1; if (a[0] > 0) flg = -1; int bs = a[0]; long long ans = 0; for (int i = 1; i < n; i++) { if (flg == -1) { if (-1 * (a[i] + bs) <= 0) { ans += a[i] + bs + 1; bs = -1; } else { bs += a[i]; } } else { if ((a[i] + bs) <= 0) { ans += -(a[i] + bs) + 1; bs = 1; } else { bs += a[i]; } } flg *= -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(void) { int64_t n; int64_t i; int64_t sum; bool default_flag; bool plus_flag, minus_flag; int64_t ope_count; cin >> n; vector<int64_t> a(n); for (i = 0; i < n; i++) { cin >> a.at(i); } sum = 0; ope_count = 0; default_flag = true; plus_flag = false; minus_flag = false; for (i = 0; i < n; i++) { sum += a.at(i); if (default_flag == true) { default_flag = false; if (a.at(i) > a.at(i + 1) && signbit(a.at(i)) == signbit(a.at(i + 1))) { while (sum >= 0) { ope_count++; sum--; } minus_flag = true; } else if (a.at(i) <= a.at(i + 1) && signbit(a.at(i)) == signbit(a.at(i + 1))) { while (sum <= 0) { ope_count++; sum++; } plus_flag = true; } else if (a.at(i) > a.at(i + 1) && signbit(a.at(i)) != signbit(a.at(i + 1))) { plus_flag = true; } else if (a.at(i) <= a.at(i + 1) && signbit(a.at(i)) != signbit(a.at(i + 1))) { minus_flag = true; } } else if (plus_flag == true) { while (sum >= 0) { ope_count++; sum--; } plus_flag = false; minus_flag = true; } else if (minus_flag == true) { while (sum <= 0) { ope_count++; sum++; } plus_flag = true; minus_flag = false; } } cout << ope_count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; vector<long long int> vec; vector<vector<long long int> > vec2; long long int MOD = 1000000007; int main() { long long int N; cin >> N; vector<long long int> vec(N, 0); for (long long int i = 0; i < N; i++) { cin >> vec[i]; } long long int ans = 0; long long int g_ans = 0; long long int k_ans = 0; long long int sum = 0; bool flg = true; for (long long int i = 0; i < N; i++) { sum += vec[i]; if (flg == true) { if (sum <= 0) { k_ans += abs(1 - sum); sum += k_ans; } flg = false; } else { if (sum >= 0) { k_ans += abs(-1 - sum); sum += -k_ans; } flg = true; } } flg = true; sum = 0; for (long long int i = 0; i < N; i++) { sum += vec[i]; if (flg == false) { if (sum <= 0) { g_ans += abs(1 - sum); sum += g_ans; } flg = true; } else { if (sum >= 0) { g_ans += abs(-1 - sum); sum += -g_ans; } flg = false; } } ans = min(k_ans, g_ans); cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; long long a[100005], dp[100005]; cin >> n; long long sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; dp[i] = sum; } long long diff = 0, ans = 0; if (dp[0] == 0) { if (dp[1] < 0) diff++, ans++; else diff--, ans++; } for (int i = 1; i < n; i++) { if (dp[i] + diff == 0) { if (dp[i - 1] + diff < 0) diff++, ans++; if (dp[i - 1] + diff > 0) diff--, ans++; continue; } if ((dp[i - 1] + diff) / llabs(dp[i - 1] + diff) == (dp[i] + diff) / llabs(dp[i] + diff)) { if (dp[i] + diff < 0) { ans += llabs(dp[i] + diff) + 1; diff += llabs(dp[i] + diff) + 1; } else { ans += llabs(dp[i] + diff) + 1; diff -= llabs(dp[i] + diff) + 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
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; using System.Numerics; using static System.Console; using static System.Convert; using static System.Math; class Program { static void Main(string[] args) { var num = ToInt32(ReadLine()); var ar = Array.ConvertAll(ReadLine().Split(' '), int.Parse); WriteLine(Min(GetC(ar, true), GetC(ar, false))); } private static int GetC(int[] ar,bool isP) { var result = 0; var sum = 0; for(var i = 0; i < ar.Length; i++) { sum += ar[i]; if (isP) { if (sum <= 0) { result += 1 - sum; sum = 1; } } else if (sum >= 0) { result += 1 + sum; sum = -1; } isP = isP ? false : true; } return result; } }
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 s[n]; for (int i = 0; i < n; i++) { cin >> s[i]; } int pos = 0; int neg = 0; int tmpsum = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { tmpsum = tmpsum + s[i]; if (tmpsum <= 0) { pos = pos + 1 - tmpsum; tmpsum = 1; } } else { tmpsum = tmpsum + s[i]; if (tmpsum >= 0) { pos = pos + tmpsum + 1; tmpsum = -1; } } } tmpsum = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { tmpsum = tmpsum + s[i]; if (tmpsum >= 0) { neg = neg + 1 + tmpsum; tmpsum = -1; } } else { tmpsum = tmpsum + s[i]; if (tmpsum <= 0) { neg = neg + 1 - tmpsum; tmpsum = 1; } } } if (pos > neg) { cout << neg << endl; } else { cout << pos << endl; } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const int N = 1e5 + 7, M = 1e7, OO = 0x3f3f3f3f; long long n, array1[2 * N]; long long solve(long long num) { long long sum = num, counter = 0; for (long long i = 1; i < n; ++i) { long long temp_sum = sum + array1[i]; if (sum > 0) { if (temp_sum >= 0) { counter += temp_sum + 1; temp_sum = -1; } } else if (sum < 0) { if (temp_sum <= 0) { counter += abs(temp_sum) + 1; temp_sum = 1; } } sum = temp_sum; } return counter; } int main() { long long i, sum = 0; scanf("%lld", &n); for (i = 0; i < n; ++i) { scanf("%lld", &array1[i]); } long long mini; if (array1[0] == 0) { mini = min(solve(1), solve(-1)); } else { long long choice1 = solve(array1[0]); long long choice2 = (array1[0] > 0) ? solve(-1) + array1[0] + 1 : solve(1) + abs(array1[0]) + 1; mini = min(choice1, choice2); } printf("%lld", mini); 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())) ans1 = 0 sum = A[0] for a in A[1:]: if (sum + a) * sum < 0: sum += a else: fugo = sum // abs(sum) nextsum = - fugo a_should_be = nextsum - sum dif = abs(a_should_be - a) sum += a_should_be ans1 += dif #print(ans1) #A[0]を反転したほうがいいパターンの処理 ans2 = A[0] + 1 sum = - (A[0] // abs(A[0])) for a in A[1:]: if (sum + a) * sum < 0: sum += a else: fugo = sum // abs(sum) nextsum = - fugo a_should_be = nextsum - sum dif = abs(a_should_be - a) sum += a_should_be ans2 += dif #print(ans2) 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
UNKNOWN
use std::io::*; use std::str::FromStr; pub fn read<T: FromStr>() -> T { let stdin = stdin(); let stdin = stdin.lock(); let token: String = stdin .bytes() .map(|c| c.expect("failed to read char") as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect(); token.parse().ok().expect("failed to parse token") } use std::cmp::{max, min}; use std::collections::BTreeMap; fn main() { let n = read::<i64>(); let mut vec_a = vec![]; for i in 0..n { vec_a.push(read::<i64>()); } let mut prev_sum = vec_a[0]; let mut ans = 0; // 最初を正にする if prev_sum <= 0 { prev_sum = 1; ans = (1 - prev_sum).abs(); } for i in 1..vec_a.len() { let b = vec_a[i as usize]; if 0 < prev_sum { if 0 <= prev_sum + b { ans += (1 + prev_sum).abs() + b; prev_sum = -1; } else { prev_sum += b; } } else if prev_sum < 0 { if prev_sum + b <= 0 { ans += (1 - prev_sum).abs() - b; prev_sum = 1; } else { prev_sum += b; } } } let plus_min = ans; // 最初を負にする prev_sum = vec_a[0]; ans = 0; if 0 <= prev_sum { prev_sum = -1; ans = (1 + prev_sum).abs(); } for i in 1..vec_a.len() { let b = vec_a[i as usize]; if 0 < prev_sum { if 0 <= prev_sum + b { ans += (1 + prev_sum).abs() + b; prev_sum = -1; } else { prev_sum += b; } } else if prev_sum < 0 { if prev_sum + b <= 0 { ans += (1 - prev_sum).abs() - b; prev_sum = 1; } else { prev_sum += b; } } } let minus_min = ans; ans = min(plus_min, minus_min); println!("{}", ans); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; int result = 0; bool isPlus = a[0] > 0 ? true : false; int sum = a[0]; for (int i = 1; i < n; i++) { int temp_sum = sum + a[i]; if (isPlus) { if (temp_sum >= 0) { result += temp_sum + 1; a[i] -= temp_sum + 1; } } else { if (temp_sum <= 0) { result += -temp_sum + 1; a[i] += -temp_sum + 1; } } isPlus = !isPlus; sum += a[i]; } cout << result << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(int,input().split())) sm=a[0] ans=0 for i in range(n-1): sm1=sm+a[i+1] if sm*sm1>=0: ans+=abs(sm1)+1 sm//=abs(sm)*(-1) else: sm=sm1 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; struct edge { int to, cost; }; const int INF = 100000000; long long int N, A[100010], B[100010], ans1, ans2, mode, sum; int main() { cin >> N; for (int i = 0; i < (N); i++) { cin >> A[i]; B[i] = A[i]; } for (int i = 0; i < (N); i++) { sum += A[i]; if (mode == 0) { if (sum <= 0) { ans1 += abs(1 - sum); sum = 1; } mode = 1; } else { if (sum >= 0) { ans1 += abs(-1 - sum); sum = -1; } mode = 0; } } sum = 0; for (int i = 0; i < (N); i++) { sum += A[i]; if (mode == 1) { if (sum <= 0) { ans2 += abs(1 - sum); sum = 1; } mode = 0; } else { if (sum >= 0) { ans2 += abs(-1 - sum); sum = -1; } mode = 1; } } cout << std::min(ans1, ans2) << endl; return 0; }