Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<bits/stdc++.h> using namespace std; int main(){ long n; cin >> n; long a[n],sum = 0; for(long i = 0;i < n;i++){ cin >> a[i]; } long kekka = 0; for(long i = 0;i < n;i++){ sum += a[i]; if(i % 2 == 0 && sum >= 0){ kekkka += sum + 1; sum = -1; } if(i % 2 == 1 && sum <= 0){ kekka += -sum + 1; sum = 1; } } long result = 0; sum = 0; for(long i = 0;i < n;i++){ sum += a[i]; if(i % 2 == 0 && sum <= 0){ result += -sum + 1; sum = 1; } if(i % 2 == 1 && sum >= 0){ result += sum + 1; sum = -1; } } cout << min(kekka,result) << 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()) b = [int(x) for x in input().split()] a = list() temp = 0 count1 = 0 count2 = 0 a = b if a[0] == 0: a[0] = 1 count1 = 1 sum = a[0] for i in range(1, n): if abs(a[i]) <= abs(sum) or a[i] * sum >= 0: if sum > 0: temp = -1 * abs(sum) - 1 count1 += abs(temp - a[i]) else: temp = abs(sum) + 1 count1 += abs(temp - a[i]) a[i] = temp sum += a[i] count2 = abs(a[0]) + 1 a = b if a[0] == 0: a[0] = 1 count1 = 1 if a[0] > 0: a[0] = -1 else: a[0] = 1 sum = a[0] for i in range(1, n): if abs(a[i]) <= abs(sum) or a[i] * sum >= 0: count2 += abs(sum - a[i]) + 1 if sum > 0: temp = -1 * abs(sum) - 1 count2 += abs(temp - a[i]) else: temp = abs(sum) + 1 count2 += abs(temp - a[i]) a[i] = temp sum += a[i] 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int A[n]; for (int i = 0; i < n; i++) { cin >> A[i]; } int countA = 0; int countB = 0; int part = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && A[i] + part <= 0) { countA += 1 - (A[i] + part); part = 1; } else if (i % 2 == 1 && A[i] + part >= 0) { countA += A[i] + part + 1; part = -1; } else part += A[i]; } part = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && A[i] + part >= 0) { countB += A[i] + part + 1; part = -1; } else if (i % 2 == 1 && A[i] + part <= 0) { countB += 1 - (A[i] + part); part = 1; } else part += A[i]; } cout << min(countA, countB) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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; static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; template <typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; } signed main() { long long n; cin >> n; vector<long long> a(n); for (long long(i) = 0; (i) < (long long)(n); (i)++) cin >> a[i]; long long sum = 0; long long prev = 0; sum += a[0]; long long ans = 0; for (long long(i) = (long long)(1); (i) < (long long)(n); (i)++) { prev = sum; sum += a[i]; if (prev * sum < 0) { continue; } else { if (sum > 0) { ans += sum + 1; sum = -1; } else if (sum < 0) { ans += abs(sum) + 1; sum = 1; } else { ans++; sum = (prev < 0 ? 1 : -1); } } } sum = 0; prev = 0; long long ans2 = 0; sum += a[0]; if (sum > 0) { ans2 += sum + 1; sum = -1; } else if (sum < 0) { ans2 += abs(sum) + 1; sum = 1; } else { ans2++; sum = 1; } for (long long(i) = (long long)(1); (i) < (long long)(n); (i)++) { prev = sum; sum += a[i]; if (prev * sum < 0) { continue; } else { if (sum > 0) { ans2 += sum + 1; sum = -1; } else if (sum < 0) { ans2 += abs(sum) + 1; sum = 1; } else { ans2++; sum = (prev < 0 ? 1 : -1); } } } sum = 0; prev = 0; long long ans3 = 0; sum += a[0]; if (sum > 0) { ans3 += sum + 1; sum = -1; } else if (sum < 0) { ans3 += abs(sum) + 1; sum = 1; } else { ans3++; sum = -1; } for (long long(i) = (long long)(1); (i) < (long long)(n); (i)++) { prev = sum; sum += a[i]; if (prev * sum < 0) { continue; } else { if (sum > 0) { ans3 += sum + 1; sum = -1; } else if (sum < 0) { ans3 += abs(sum) + 1; sum = 1; } else { ans3++; sum = (prev < 0 ? 1 : -1); } } } cout << min(ans, min(ans3, 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
if __name__ == '__main__': # C n = int(input()) a0 = input().split() a = [int(i) for i in a0] s1 = 0 s2 = 0 m1 = 0 m2 = 0 o = 1 e = -1 for i in a: s1 += i if s1 == 0: m1 += 1 elif o * s1 < 0: m1 += abs(s1) + 1 s1 = o o *= -1 else: o *= -1 s2 += i if s2 == 0: m2 += 1 elif e * s2 < 0: m2 += abs(s2) + 1 s2 = e e *= -1 else: e *= -1 print(min(m1, m2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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 = 1; i <= n; i++) cin >> a[i]; vector<int> a1; a1 = a; int sum1 = 0, ans1 = 0; for (int i = 1; i <= n; i++) { sum1 += a1[i]; if (i % 2 == 1 && sum1 <= 0) { int plus = 1 - sum1; sum1 += 1; ans1 += plus; continue; } if (i % 2 == 0 && sum1 >= 0) { int minus = 1 + sum1; sum1 = -1; ans1 += minus; } } vector<int> a2; a2 = a; int sum2 = 0, ans2 = 0; for (int i = 1; i <= n; i++) { sum2 += a2[i]; if (i % 2 == 1 && sum2 >= 0) { int minus = 1 + sum2; sum2 = -1; ans2 += minus; continue; } else if (i % 2 == 0 && sum2 <= 0) { int plus = 1 - sum2; sum2 = 1; ans2 += plus; } } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; class C { public: template <typename T> int sgn(T val) { return (T(0) < val) - (val < T(0)); } void solve(std::istream& in, std::ostream& out) { ios::sync_with_stdio(false); int n; in >> n; vector<long long int> a(n), p(n); for (int i = 0; i < n; ++i) { in >> a[i]; } long long int steps = 0; long long int steps2 = 0; p[0] = a[0]; if (a[0] != 0) { for (int i = 0; i < n - 1; ++i) { p[i + 1] = p[i] + a[i + 1]; if (sgn(p[i]) == -1) { if (sgn(p[i + 1]) != 1) { steps += -p[i + 1] + 1; p[i + 1] = 1; } } else if (sgn(p[i]) == 1) { if (sgn(p[i + 1]) != -1) { steps += p[i + 1] + 1; p[i + 1] = -1; } } } } else { p[0] = 1; for (int i = 0; i < n - 1; ++i) { p[i + 1] = p[i] + a[i + 1]; if (sgn(p[i]) == -1) { if (sgn(p[i + 1]) != 1) { steps += -p[i + 1] + 1; p[i + 1] = 1; } } else if (sgn(p[i]) == 1) { if (sgn(p[i + 1]) != -1) { steps += p[i + 1] + 1; p[i + 1] = -1; } } } p[0] = -1; for (int i = 0; i < n - 1; ++i) { p[i + 1] = p[i] + a[i + 1]; if (sgn(p[i]) == -1) { if (sgn(p[i + 1]) != 1) { steps2 += -p[i + 1] + 1; p[i + 1] = 1; } } else if (sgn(p[i]) == 1) { if (sgn(p[i + 1]) != -1) { steps2 += p[i + 1] + 1; p[i + 1] = -1; } } } steps = min(steps, steps2); } if (p[n - 1] == 0) { ++steps; } out << steps << endl; } }; int main() { C solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); 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 copy n = int(input()) a = [int(i) for i in input().split()] b=a.copy() s0p = a[0] s0n = b[0] countp = 0 countn = 0 if a.count(0)==n: print(2*n+1) exit() """ if s0p<=0: while s0p<=0: s0p+=1 countp+=1 if s0n>=0: while s0n>=0: s0n-=1 countn+=1 """ for i in range(1,n): s1 = s0p+a[i] if s0p*s1>=0: if s1>0: a[i]-=(abs(s1)+1) countp+=(abs(s1)+1) elif s1<0: a[i]+=(abs(s1)+1) countp+=(abs(s1)+1) elif s1==0: if s0p>0: a[i]-=1 countp+=1 elif s0p<0: a[i]+=1 countp+=1 s0p += a[i] for i in range(1,n): s1 = s0n+b[i] if s0n*s1>=0: if s1>0: b[i]-=(abs(s1)+1) countn+=(abs(s1)+1) elif s1<0: b[i]+=(abs(s1)+1) countn+=(abs(s1)+1) elif s1==0: if s0n>0: b[i]-=1 countn+=1 elif s0n<0: b[i]+=1 countn+=1 s0n += b[i] print(countp if countp<=countn else(countn))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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; const int MAX_N = 1.0e5 + 100; int a[MAX_N]; int main() { cin >> N; for (int i = 0; i < N; i++) cin >> a[i]; int e_sum = 0; int even = 0; for (int i = 0; i < N; i++) { e_sum += a[i]; if (i % 2 == 0 && e_sum < 0) { even += abs(e_sum) + 1; e_sum = 1; } else if (i % 2 == 1 && e_sum > 0) { even += abs(e_sum) + 1; e_sum = -1; } } int o_sum = 0; int odd = 0; for (int i = 0; i < N; i++) { o_sum += a[i]; if (i % 2 == 1 && o_sum <= 0) { odd += abs(o_sum) + 1; o_sum = 1; } else if (i % 2 == 0 && o_sum >= 0) { odd += abs(o_sum) + 1; o_sum = -1; } } cout << min(even, odd) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n], b[n]; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; } int case1, total; if (a[0] > 0) { case1 = 0; total = a[0]; } else { case1 = 1 - a[0]; total = 1; } for (int i = 1; i < n; i++) { int ttmp = total + a[i]; int atmp = a[i]; if ((ttmp * total) < 0 && ttmp != 0) { total = ttmp; } else { if (total > 0) { a[i] = -total - 1; } else { a[i] = -total + 1; } total += a[i]; } case1 += abs(a[i] - atmp); } int case2; if (b[0] < 0) { case2 = 0; total = b[0]; } else { case2 = -1 - b[0]; total = -1; } for (int i = 1; i < n; i++) { int ttmp = total + b[i]; int atmp = b[i]; if ((ttmp * total) < 0 && ttmp != 0) { total = ttmp; } else { if (total > 0) { b[i] = -total - 1; } else { b[i] = -total + 1; } total += b[i]; } case2 += abs(b[i] - atmp); } cout << min(case1, case2) << 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
input() a = list(map(int, input().split())) s = a[0] ret = 0 for i in a[1:]: if s * (s+i) < 0: s = s+i else: t = s + i k = 1 if t > 0 else -1 ret += k * t + 1 s = -k print(ret)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def resolve(SL): # L[0]!=0を起点とする cnt = 0 for i in range(len(SL)-1): s0 = SL[i] s1 = SL[i+1] if(s0>0 and s1>=0): SL[(i+1):] = [s-(s1+1) for s in SL[(i+1):]] cnt += (s1+1) elif(s0<0 and s1<=0): SL[(i+1):] = [s+(-s1+1) for s in SL[(i+1):]] cnt += (-s1+1) # print(SL) return cnt def ans(L): SL = [sum(L[:(i+1)]) for i in range(len(L))] c0,c1=0,0 if (L[0]>0): c0 = resolve(SL) c1 = (L[0]+1) + resolve(list(map(lambda x:x-(L[0]+1), SL))) elif (L[0]<0): c0 = resolve(L) c1 = (-L[0]+1) + resolve(list(map(lambda x:x+(-L[0]+1), SL))) else: c0 = 1 + resolve(list(map(lambda x:x+1, SL))) c1 = 1 + resolve(list(map(lambda x:x-1, SL))) return(min(c0,c1)) N = int(input()) L = [int(x) for x in input().split(' ')] print(ans(L))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); long[] A = new long[N]; for (int i = 0; i < N; i++) { A[i] = sc.nextInt(); } System.out.println( solve(N, A) ); } private static long solve(int N, long[] A) { long a0 = A[0]; if( a0 > 0 ) { long p = solve1(N, A, a0, 0); long m = solve1(N, A, -1, a0 + 1); return Math.min(p, m); } else if( a0 < 0 ) { long p = solve1(N, A, 1, a0 + 1); long m = solve1(N, A, a0, 0); return Math.min(p, m); } else { long p = solve1(N, A, 1, 1); long m = solve1(N, A, -1, 1); return Math.min(p, m); } } private static long solve1(int N, long[] A, long sum, long ans) { for (int i = 1; i < N; i++) { long a = A[i]; if( sum > 0 ) { // 次はminusになるのを期待 if( a + sum >= 0 ) { // sumが-1になるような値にまで変更する // a + sum が 5 の場合、6 だけ操作すると -1 にできる long diff = a + sum + 1L; ans += diff; sum = -1; } else { sum += a; } } else { if( a + sum <= 0 ) { long diff = Math.abs(a + sum) + 1L; ans += diff; sum = 1; } else { sum += a; } } } return 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 checkSign(int A) { return (int)(A > 0) - (int)(A < 0); } int main() { int N; cin >> N; vector<int> a(N); for (int i = 0; i < N; i++) { cin >> a.at(i); } int res1 = 0; int sum = 0; int sign = 1; for (int i = 0; i < N; i++) { int tmp = a.at(i); if (checkSign(sum + a.at(i)) == 0 || checkSign(sum + a.at(i)) == checkSign(sum)) { tmp = sign * (abs(sum) + 1); res1 += abs(tmp - a.at(i)); } sum += tmp; sign *= -1; } int res2 = 0; sum = 0; sign = -1; for (int i = 0; i < N; i++) { int tmp = a.at(i); if (checkSign(sum + a.at(i)) == 0 || checkSign(sum + a.at(i)) == checkSign(sum)) { tmp = sign * (abs(sum) + 1); res2 += abs(tmp - a.at(i)); } sum += tmp; sign *= -1; } cout << min(res1, res2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } long long Sum = a[0], ans = 0; if (Sum == 0) { ans++; Sum++; } for (int i = 0; i < n - 1; i++) { if (Sum < 0 && Sum + a[i + 1] <= 0) { ans += -(Sum + a[i + 1]) + 1; Sum = 1; } else if (Sum > 0 && Sum + a[i + 1] >= 0) { ans += Sum + a[i + 1] + 1; Sum = -1; } else { Sum += a[i + 1]; } } long long S1 = a[0], t1 = 0; if (S1 == 0) { ans++; Sum--; } for (int i = 0; i < n - 1; i++) { if (S1 < 0 && S1 + a[i + 1] <= 0) { t1 += -(S1 + a[i + 1]) + 1; S1 = 1; } else if (S1 > 0 && S1 + a[i + 1] >= 0) { t1 += S1 + a[i + 1] + 1; S1 = -1; } else { S1 += a[i + 1]; } } ans = min(ans, t1); 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 n; cin >> n; long long a[n]; for (long i = 0; i < n; i++) { cin >> a[i]; } long long sum_pn, sum_np; long long count_pn, count_np, count; sum_pn = 0; sum_np = 0; count_pn = 0; count_np = 0; for (long i = 0; i < n; i++) { sum_pn += a[i]; sum_np += a[i]; long long sign; sign = ((i) % 2 * 2 - 1); if (sum_pn * (sign) <= 0) { count_pn = count_pn - sign * sum_pn + 1; sum_pn = sum_pn - sum_pn + sign; } if (sum_np * (-sign) <= 0) { count_np = count_np + sign * sum_np + 1; sum_np = sum_np - sum_np - sign; } cout << "i sum_pn sum_np " << i << " " << sum_pn << " " << sum_np << endl; cout << " sign count_pn np " << sign << " " << count_pn << " " << count_np << endl; } if (count_pn < count_np) { count = count_pn; } else { count = count_np; } cout << count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long int; const ll INF = (1LL << 32); const ll MOD = (ll)1e9 + 7; const double EPS = 1e-9; ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; ll dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; ll n; ll solve(vector<ll> a) { ll sum = a[0]; ll ans = 0; for (ll i = (1); i < (n); i++) { if (sum > 0 and (sum + a[i]) > 0) { while (sum + a[i] != -1) { a[i]--; ans++; } } else if (sum < 0 and (sum + a[i]) < 0) { while (sum + a[i] != 1) { a[i]++; ans++; } } sum += a[i]; } if (sum == 0) ans++; return ans; } signed main() { ios::sync_with_stdio(false); cin >> n; vector<ll> a; for (ll i = 0; i < n; i++) { ll x; cin >> x; a.push_back(x); } ll start = a[0]; auto ac = a; ll fa1 = solve(a); ac[0] = ac[0] *= -1; ll fa2 = solve(ac); fa2 += start + 1; cout << min(fa1, fa2) << 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()) count=0 A=[int(i) for i in input().split()] if A[0]==0: A[0]+=1 count+=1 elif A[0]<0: for i in range(len(A)): A[i]*=(-1) for i in range(1,len(A)): if i%2==0: while sum(A[:i+1])<=0: A[i]+=1 count+=1 else: while sum(A[:i+1])>=0: A[i]-=1 count+=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 uint = unsigned int; using ll = long long; using ull = unsigned long long; void solve() {} int main() { int n; vector<int> a, a2; cin >> n; int x; for (int i = 0; i < n; i++) { cin >> x; a.push_back(x); } bool pl = false; bool mi = false; bool zero = false; if (a[0] > 0) { pl = true; } else if (a[0] < 0) { mi = true; } else { zero = true; } ll sum = a[0]; ll sum2 = a[0]; ll ans = 0; ll ans2 = 0; if (zero) { copy((a).begin(), (a).end(), back_inserter(a2)); for (int i = 1; i < n; i++) { sum += a[i]; if (i % 2 == 1) { if (sum >= 0) { int tmp = a[i]; a[i] -= sum + 1; ans += sum + 1; sum -= tmp; sum += a[i]; } } else { if (sum <= 0) { int tmp = a[i]; a[i] += (-1) * sum + 1; ans += (-1) * sum + 1; sum -= tmp; sum += a[i]; } } } for (int i = 1; i < n; i++) { sum2 += a2[i]; if (i % 2 == 1) { if (sum <= 0) { int tmp = a2[i]; a2[i] += (-1) * sum + 1; ans2 += (-1) * sum + 1; sum -= tmp; sum += a2[i]; } } else { if (sum >= 0) { int tmp = a2[i]; a2[i] -= sum + 1; ans2 += sum + 1; sum -= tmp; sum += a2[i]; } } } if (ans2 < ans) { ans = ans2; } } for (int i = 1; i < n; i++) { sum += a[i]; if (pl) { if (i % 2 == 1) { if (sum >= 0) { int tmp = a[i]; a[i] -= sum + 1; ans += sum + 1; sum -= tmp; sum += a[i]; } } else { if (sum <= 0) { int tmp = a[i]; a[i] += (-1) * sum + 1; ans += (-1) * sum + 1; sum -= tmp; sum += a[i]; } } } else if (mi) { if (i % 2 == 1) { if (sum <= 0) { int tmp = a[i]; a[i] += (-1) * sum + 1; ans += (-1) * sum + 1; sum -= tmp; sum += a[i]; } } else { if (sum >= 0) { int tmp = a[i]; a[i] -= sum + 1; ans += sum + 1; sum -= tmp; sum += a[i]; } } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=[int(i) for i in input().split(" ")] (o,s) = (0,0) for i in range(n): k=a[i] if (s+a[i]==0): a[i]= -s+1 if (s<0) else 1+s elif ((s<0 and s+a[i]<0) or (s>0 and s+a[i]>0)): a[i]=-s+1 if (s+a[i]<0) else -s-1 o+=abs(k-a[i]) s+=a[i] print(o)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = list(map(int, input().split())) ans = 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 ans += dif print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
n = gets.to_i arr = gets.chomp.split(" ").map(&:to_i) $count = [0,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 flg = true 2.times do |j| tmp_arr = Marshal.load(Marshal.dump(arr)) sum = tmp_arr[0] + tmp_arr[1] if sum == 0 if flg tmp_arr[1] -= 1 else tmp_arr[1] += 1 end $count[j] += 1 end if flg tmp_arr[1] -= sum+1 if sum > 0 else tmp_arr[1] += sum+1 if sum < 0 end sum = tmp_arr[0] + tmp_arr[1] (2...tmp_arr.size).each do |i| diff = sum + tmp_arr[i] # puts %(sum : #{sum}) # puts %(diff : #{diff}) if sum > 0 if diff > 0 tmp_arr[i] -= diff.abs+1 $count[j] += diff.abs+1 elsif diff == 0 tmp_arr[i] -= 1 $count[j] += 1 end else if diff < 0 tmp_arr[i] += diff.abs+1 $count[j] += diff.abs+1 elsif diff == 0 tmp_arr[i] += 1 $count[j] += 1 end end sum += tmp_arr[i] # p tmp_arr end flg = false end #p $count #p arr puts $count.min
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# encoding:utf-8 import copy import random import bisect #bisect_left これで二部探索の大小検索が行える import fractions #最小公倍数などはこっち import math mod = 10**9+7 n = int(input()) a = [int(i) for i in input().split()] sums = [0 for i in range(n)] tmp = 0 if a[0] > 0: status_pos = True else: status_pos = False ans = 0 for i in range(n): tmp += a[i] if status_pos and tmp <= 0: ans += 1-tmp tmp = 1 elif status_pos == False and tmp >= 0: ans += 1+tmp tmp = -1 status_pos = not(status_pos) ans2 = abs(a[0])+1 if a[0] > 0: a[0] = -1 else: a[0] = 1 tmp = a[0] if a[0] > 0: status_pos = True else: status_pos = False for i in range(1,n): tmp += a[i] if status_pos and tmp <= 0: ans2 += 1-tmp tmp = 1 elif status_pos == False and tmp >= 0: ans2 += 1+tmp tmp = -1 status_pos = not(status_pos) print(min(ans,ans2)) """ 1 -2 2 -2 1 -1 1 1 """
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) num = a[0] ans = 0 if a[0]>0: for i in range(1,n): num += a[i] if i%2==1: if num>=0: ans += num+1 num = -1 else: if num<=0: ans += 1-num num = 1 elif a[0]<0: for i in range(1,n): num += a[i] if i%2==1: if num <= 0: ans += 1-num num = 1 else: if num >= 0: ans += num+1 num = -1 else: num = 0 ansp = 1 ansn = 1 for i in range(1,n): num += a[i] if i%2==1: if num>=0: ansp += num+1 num = -1 else: if num<=0: ansp += 1-num num = 1 for i in range(1,n): num += a[i] if i%2==1: if num <= 0: ansm += 1-num num = 1 else: if num >= 0: ansm += num+1 num = -1 ans = min(ansp,ansm) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n + 1]; for (int i = 0; i < n; i++) cin >> a[i]; int sum = a[0]; int c = 1; int ans0 = 0, ans1 = 0; for (int i = 1; i < n; i++) { sum += a[i]; if (sum * c < 1) { ans0 += 1 - sum * c; sum = c; } c *= -1; } c = -1; sum = a[0]; for (int i = 1; i < n; i++) { sum += a[i]; if (sum * c < 1) { ans1 += 1 - sum * c; sum = c; } c *= -1; } if (ans0 < ans1) cout << ans0 << endl; else cout << ans1 << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def resolve(): # 整数 1 つ n = int(input()) # 整数複数個 # a, b = map(int, input().split()) # 整数 N 個 (改行区切り) # N = [int(input()) for i in range(N)] # 整数 N 個 (スペース区切り) A = list(map(int, input().split())) # 整数 (縦 H 横 W の行列) # A = [list(map(int, input().split())) for i in range(H)] sumi = A[0] cnt = 0 for i in range(1, n-1): sumi += A[i] suminext = sumi + A[i+1] if sumi * suminext < 0: continue else: change = abs(suminext) +1 cnt += change if sumi < 0: A[i+1] = A[i+1] + change else: A[i+1] = A[i+1] - change print(cnt) resolve()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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 m, i, si0; int64_t t = 0, ans = 0; cin >> m; vector<int64_t> x(m), s(2); for (i = 0; i < m; i++) { cin >> x.at(i); s.at(i % 2) += x.at(i); } if (s.at(0) >= s.at(1)) si0 = 1; else si0 = -1; for (i = 0; i < m; i++) { t += x.at(i); if (i % 2 == 0 && si0 == 1 && t <= 0) { ans += abs(t) + 1; t = 1; } else if (i % 2 == 0 && si0 == -1 && t >= 0) { ans += abs(t) + 1; t = -1; } else if (i % 2 == 1 && si0 == 1 && t >= 0) { ans += abs(t) + 1; t = -1; } else if (i % 2 == 1 && si0 == -1 && t <= 0) { ans += abs(t) + 1; t = 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
java
import java.util.Scanner; class Main{ static int[] dh = {0, 0, 1, -1, -1, -1, 1, 1}; static int[] dw = {-1, 1, 0, 0, -1, 1, -1, 1}; 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 ans = 0; int sum = 0; for(int i = 0; i < n; i++) { sum += a[i]; if(sum == 0) { if(sum - a[i] < 0) sum += 1; else sum -= 1; ans++; } if(i == 0) continue; if(sum - a[i] < 0 && sum < 0) { ans += Math.abs(sum) + 1; sum = 1; } else if(sum - a[i] > 0 && sum > 0){ ans += Math.abs(sum) + 1; sum = -1; } } System.out.println(ans); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
n = gets.to_i arr = gets.chomp.split(" ").map(&:to_i) $count = 0 def check(i,arr) if i > arr.size - 1 arr[1] += 1 $count += 1 return end if arr[i] > 0 arr[1] -= 1 $count += 1 elsif arr[i] < 0 arr[1] += 1 $count += 1 else check(i+1,arr) end end num = arr[0] + arr[1] if num == 0 check(2,arr) end num = arr[0] + arr[1] (2...arr.size).each do |i| diff = num + arr[i] # puts %(num : #{num}) # puts %(diff : #{diff}) if num > 0 if diff > 0 arr[i] -= diff.abs+1 $count += diff.abs+1 end else if diff < 0 arr[i] += diff.abs+1 $count += diff.abs+1 end end if diff == 0 if num > 0 arr[i] -= 1 else arr[i] += 1 end $count += 1 end num += 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; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int t; if (a[0] >= 0) t = 1; else t = -1; int sum = a[0]; long long ans = 0; for (int i = 1; i < n; i++) { int sum2 = sum + a[i]; if (sum > 0 && sum2 > 0) { ans += sum2 + 1; sum = -1; } else if (sum < 0 && sum2 < 0) { ans += -sum2 + 1; sum = 1; } else if (sum2 == 0) { ans++; if (sum < 0) sum = 1; else sum = -1; } else sum = sum2; } cout << ans; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import numpy as np n = int(input()) a = list(map(int, input().split())) cum_a = [0 for _ in range(n)] cum_a[0] = a[0] for i in range(n-1): cum_a[i+1] = cum_a[i] + a[i+1] cum_a = np.array(cum_a) count = 0 flag = 0 def r_flag(n): if n > 0: return 1 elif n < 0: return -1 else: return 0 flag = r_flag(cum_a[0]) for i in range(1, len(cum_a)): tmp_flag = r_flag(cum_a[i]) if tmp_flag * flag == -1: flag = tmp_flag elif tmp_flag * flag == 1: count += abs(-1*tmp_flag-(cum_a[i])) cum_a[i:] += -1*tmp_flag-(cum_a[i]) flag = -1 * tmp_flag else: try: next_flag = r_flag(cum_a[i+1]) if next_flag == 1: cum_a[i:] -= 1 count += 1 else: cum_a[i:] += 1 count += 1 except: count += 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> const long long INF = (1ll << 60); const int MAXN = (1e+5) + 5; using namespace std; long long n, sum, ans, a[MAXN], b[MAXN]; int main() { cin >> n >> a[1]; b[1] = a[1]; for (int i = 2; i <= n; i++) { cin >> a[i]; if (b[i - 1] > 0) { if (b[i - 1] + a[i] < 0) b[i] = b[i - 1] + a[i]; else { b[i] = -1; ans += b[i - 1] + a[i] + 1; } } else { if (b[i - 1] + a[i] > 0) b[i] = b[i - 1] + a[i]; else { b[i] = 1; ans += 1 - b[i - 1] - 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; int main() { int N; cin >> N; vector<int> data(N); int y; cin >> y; data.at(0) = y; for (int i = 1; i < N; i++) { int x; cin >> x; data.at(i) = data.at(i - 1) + x; } int sei_ans = 0; int hu_ans = 0; int zyoutai = 0; for (int i = 0; i < N; i++) { if (i % 2 == 0) { int a = max(0, 1 - data.at(i) + zyoutai); zyoutai += a; sei_ans += a; } else { int a = 1 + max(0, data.at(i) + zyoutai); zyoutai -= a; sei_ans += a; } } for (int i = 0; i < N; i++) { if (i % 2 != 0) { int a = max(0, 1 - data.at(i) + zyoutai); zyoutai += a; hu_ans += a; } else { int a = 1 + max(0, data.at(i) + zyoutai); zyoutai -= a; hu_ans += a; } } cout << min(sei_ans, hu_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 main(void) { double num[10 * 10 * 10 * 10 * 10]; int i, n, ssign; double sum = 0; double count = 0; double fsum, fnum; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%lf", &num[i]); } if (num[0] == 0) { num[0]++; count++; } for (i = 1; i < n; i++) { sum += num[i - 1]; fsum = fabs(sum); fnum = fabs(num[i]); while (1) { if (fsum > fnum) { if (sum < 0) { num[i]++; count++; } else if (sum > 0) { num[i]--; count++; } } else if (fsum == fnum) { if (sum < 0) { num[i]++; count++; } else { num[i]--; count++; } } else if (fsum < fnum && sum > 0 && num[i] > 0) { num[i]--; count++; } else if (fsum < fnum && sum < 0 && num[i] < 0) { num[i]++; count++; } else break; } } for (i = 0; i < n; i++) { sum += num[i]; if (sum == 0.0) { if ((sum - num[i]) > 0) num[i]--; else num[i]++; count++; } } printf("%f\n", count); 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 const long long MOD_NUM = 1000000007; template <class _T> static void getint(_T& a) { std::cin >> a; } template <class _T> static void getint(_T& a, _T& b) { std::cin >> a >> b; } template <class _T> static void getint(_T& a, _T& b, _T& c) { std::cin >> a >> b >> c; } template <class _T> static _T tp_abs(_T a) { if (a < (_T)0) { a *= (_T)-1; } return a; } static void exec(); int main() { exec(); fflush(stdout); return 0; } static void exec() { int N; getint(N); std::vector<long long> ai(N); for (int i = 0; i < N; i++) { getint(ai[i]); } long long ans = 0; long long sum = ai[0]; if (ai[0] == 0) { for (int i = 1; i < N; i++) { if (ai[i] > 0) { if (i % 2) { sum++; } else { sum--; } ans++; break; } else if (ai[i] < 0) { if (i % 2) { sum--; } else { sum++; } ans++; break; } } if (ans == 0) { sum++; ans++; } } for (int i = 1; i < N; i++) { int bfrSign = (sum > 0) ? 1 : -1; sum += ai[i]; if ((bfrSign > 0) && (sum >= 0)) { ans += (tp_abs(sum) + 1); sum = -1; } else if ((bfrSign < 0) && (sum <= 0)) { ans += (tp_abs(sum) + 1); sum = 1; } } printf("%lld\n", ans); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
package main import ( "bufio" "fmt" "math" "os" "strconv" ) const pi = math.Pi var mod int = pow(10, 9) + 7 var Umod uint64 = 1000000007 var ans_1, ans_2 int func main() { reader.Split(bufio.ScanWords) n, _ := strconv.Atoi(read()) a := make([]int, n) for i := 0; i < n; i++ { a[i], _ = strconv.Atoi(read()) } sum := make([]int, n) sum[0] = a[0] if sum[0] != 0 { for i := 1; i < n; i++ { sum[i] += a[i] + sum[i-1] if 0 < sum[i-1] && 0 <= sum[i] { // NGパターン ans_1 += sum[i] + 1 sum[i] = -1 } else if sum[i-1] < 0 && sum[i] <= 0 { // NGパターン ans_1 += 1 - sum[i] sum[i] = 1 } } fmt.Println(ans_1) } else { sum[0], ans_1 = -1, 1 for i := 1; i < n; i++ { sum[i] += a[i] + sum[i-1] if 0 < sum[i-1] && 0 <= sum[i] { // NGパターン ans_1 += sum[i] + 1 sum[i] = -1 } else if sum[i-1] < 0 && sum[i] <= 0 { // NGパターン ans_1 += 1 - sum[i] sum[i] = 1 } } sum = make([]int, n) sum[0], ans_2 = 1, 1 for i := 1; i < n; i++ { sum[i] += a[i] + sum[i-1] if 0 < sum[i-1] && 0 <= sum[i] { // NGパターン ans_2 += sum[i] + 1 sum[i] = -1 } else if sum[i-1] < 0 && sum[i] <= 0 { // NGパターン ans_2 += 1 - sum[i] sum[i] = 1 } } fmt.Println(min(ans_1, ans_2)) } } /* ---------------------------------------- */ var reader = bufio.NewScanner(os.Stdin) func read() string { reader.Scan() return reader.Text() } func lcm(x, y int) int { return (x / gcd(x, y)) * y } func gcd(x, y int) int { if x%y == 0 { return y } else { r := x % y return gcd(y, r) } } var fac [1000000]int var finv [1000000]int var inv [1000000]int func combination_init() { fac[0], fac[1] = 1, 1 finv[0], finv[1] = 1, 1 inv[1] = 1 // invは a^(-1) mod p // pをaで割ることを考える // p/a*(a) + p%a = p // p/a*(a) + p%a = 0 (mod p) // -p%a = p/a*(a) (mod p) // -p%a *a^(-1)= p/a (mod p) // a^(-1)= p/a * (-p%a)^(-1) (mod p) // a^(-1) = for i := 2; i < 1000000; 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 } } func combination(x, y int) int { if x < y { return 0 } if fac[0] != 1 { combination_init() } return fac[x] * (finv[y] * finv[x-y] % mod) % mod //return fac[x] / (fac[y] * fac[x-y]) } func permutation(x, y int) int { if x < y { return 0 } if fac[0] != 1 { combination_init() } return fac[x] * (finv[x-y] % mod) % mod //return fac[x] / fac[x-y] } func max(x ...int) int { var res int = x[0] for i := 1; i < len(x); i++ { res = int(math.Max(float64(x[i]), float64(res))) } return res } func min(x ...int) int { var res int = x[0] for i := 1; i < len(x); i++ { res = int(math.Min(float64(x[i]), float64(res))) } return res } func pow(x, y int) int { return int(math.Pow(float64(x), float64(y))) } func abs(x int) int { return int(math.Abs(float64(x))) } func floor(x int) int { return int(math.Floor(float64(x))) } func ceil(x int) int { return int(math.Ceil(float64(x))) } type SortBy [][]int func (a SortBy) Len() int { return len(a) } func (a SortBy) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a SortBy) Less(i, j int) bool { return a[i][0] < a[j][0] } type PriorityQueue []int func (h PriorityQueue) Len() int { return len(h) } func (h PriorityQueue) Less(i, j int) bool { return h[i] < h[j] } func (h PriorityQueue) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *PriorityQueue) Push(x interface{}) { *h = append(*h, x.(int)) } func (h *PriorityQueue) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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> long body(std::vector<long>& a) { long ans = 0; std::vector<long> s(a.size()); s.at(0) = a.at(0); for (unsigned long i = 1; i < a.size(); i++) { s.at(i) = s.at(i - 1) + a.at(i); } long diff = 0; for (unsigned long i = 1; i < s.size(); i++) { s.at(i) += diff; long n = 0; if (s.at(i - 1) > 0 && s.at(i) >= 0) { n = s.at(i) + 1; ans += n; diff -= n; s.at(i) += diff; } else if (s.at(i - 1) < 0 && s.at(i) <= 0) { n = -s.at(i) + 1; ans += n; diff += n; s.at(i) += diff; } } return ans; } int main(int argc, char** argv) { long n; std::cin >> n; std::vector<long> a(n); for (long i = 0; i < n; i++) { std::cin >> a.at(i); } long a0 = a.at(0); long ans_a, ans_b; { if (a.at(0) > 0) { ans_a = body(a); } else { a.at(0) = 1; ans_a = body(a) + (-a0 + 1); } } { if (a.at(0) < 0) { ans_b = body(a); } else { a.at(0) = -1; ans_b = body(a) + (a0 + 1); } } long ans = std::min(ans_a, ans_b); std::cout << ans << 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
cpp
#include <bits/stdc++.h> const bool debug = false; using namespace std; const long long 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; } int main(void) { cin.tie(0); ios::sync_with_stdio(false); long long n; cin >> n; vector<long long> a(n); for (int(i) = 0; (i) < (n); (i)++) { cin >> a[i]; } function<long long()> solve = [&]() { long long sum = 0, cnt = 0; for (int(i) = 0; (i) < (n); (i)++) { if (i == 0) { sum = a[0]; continue; } if (sum > 0 && sum + a[i] >= 0) { cnt += sum + a[i] + 1; sum = -1; } else if (sum < 0 && sum + a[i] <= 0) { cnt += -(sum + a[i]) + 1; sum = 1; } else { sum += a[i]; } } return cnt; }; long long res; if (a[0] == 0) { a[0] = 1; res = solve(); a[0] = -1; chmin(res, solve()); res++; } else { res = solve(); } cout << res << 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, sum, b, c; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } if (a[0] <= 0) { b = 1 - a[0]; sum = 1; } else { sum = a[0]; b = 0; } for (int i = 1; i < n; i++) { if (i % 2) { if (-sum <= a[i]) { b += abs(a[i] + sum + 1); sum = -1; } else { sum += a[i]; } } else { if (-sum >= a[i]) { b += abs(a[i] + sum - 1); sum = 1; } else { sum += a[i]; } } } if (a[0] >= 0) { c = 1 - a[0]; sum = -1; } else { sum = a[0]; c = 0; } for (int i = 1; i < n; i++) { if (i % 2) { if (-sum >= a[i]) { c += abs(a[i] + sum - 1); sum = 1; } else { sum += a[i]; } } else { if (-sum <= a[i]) { c += abs(a[i] + sum + 1); sum = -1; } else { sum += a[i]; } } } cout << min(b, 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; template <class T> bool umin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template <class T> bool umax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <class A> void read(vector<A> &v); template <class A, size_t S> void read(array<A, S> &a); template <class T> void read(T &x) { cin >> x; } void read(double &d) { string t; read(t); d = stod(t); } void read(long double &d) { string t; read(t); d = stold(t); } template <class H, class... T> void read(H &h, T &...t) { read(h); read(t...); } template <class A> void read(vector<A> &x) { for (auto &a : x) read(a); } template <class A, size_t S> void read(array<A, S> &x) { for (auto &a : x) read(a); } string to_string(char c) { return string(1, c); } string to_string(bool b) { return b ? "true" : "false"; } string to_string(const char *s) { return string(s); } string to_string(string s) { return s; } string to_string(vector<bool> v) { string res; for (long long i = (0); (1) > 0 ? i < ((long long)(v).size()) : i > ((long long)(v).size()); i += (1)) res += char('0' + v[i]); return res; } template <size_t S> string to_string(bitset<S> b) { string res; for (long long i = (0); (1) > 0 ? i < (S) : i > (S); i += (1)) res += char('0' + b[i]); return res; } template <class T> string to_string(T v) { bool f = 1; string res; for (auto &x : v) { if (!f) res += ' '; f = 0; res += to_string(x); } return res; } template <class A> void write(A x) { cout << to_string(x); } template <class H, class... T> void write(const H &h, const T &...t) { write(h); write(t...); } void print() { write("\n"); } template <class H, class... T> void print(const H &h, const T &...t) { write(h); if (sizeof...(t)) write(' '); print(t...); } template <class T, class U> void vti(vector<T> &v, U x, size_t n) { v = vector<T>(n, x); } template <class T, class U> void vti(vector<T> &v, U x, size_t n, size_t m...) { v = vector<T>(n); for (auto &a : v) vti(a, x, m); } void solve() { long long n; read(n); vector<long long> a(n); read(a); long long ans = 0; if (!a[0]) { a[0] = (a[1] < 0) ? 1 : -1; ans = 1; } long long sum = a[0]; for (long long i = (1); (1) > 0 ? i < (n) : i > (n); i += (1)) { long long x = sum; long long y = sum + a[i]; if (x > 0 && y > 0 || x < 0 && y < 0 || !y) { long long t = (sum > 0) ? sum + 1 : sum - 1; ans += abs(a[i] + t); sum = (sum > 0) ? -1 : 1; } else { sum += a[i]; } } print(ans); } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); long long tt = 1; for (long long i = (0); (1) > 0 ? i < (tt) : i > (tt); i += (1)) { solve(); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } long long ans = 0; long long sum = 0; long long tmp = 0; for (int j = 0; j < 2; j++) { for (int i = 0; i < n; i++) { if (j == 0 && i == 0) { sum = a[i]; continue; } else if (j == 1 && i == 0) { tmp = ans; ans = 0; sum = a[i] > 0 ? -1 : 1; ans += a[i] > 0 ? a[i] + 1 : -(a[i] - 1); continue; } if (sum > 0) { if (sum + a[i] >= 0) { ans += sum + a[i] + 1; sum = -1; continue; } } else { if (sum + a[i] <= 0) { ans -= sum + a[i] - 1; sum = 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<long long> vector; long long temp; for (int i = 0; i < n; i++) { cin >> temp; vector.push_back(temp); } long long answer1 = 0; long long answer2 = 0; long long sum1 = 0; long long sum2 = 0; for (int i = 0; i < n; i++) { if (i == 0) { sum1 = vector[0]; } else if (sum1 < 0) { if (sum1 + vector[i] > 0) { sum1 += vector[i]; } else { answer1 += abs((-1) * sum1 + 1 - vector[i]); sum1 = 1; } } else { if (sum1 + vector[i] < 0) { sum1 += vector[i]; } else { answer1 += abs((-1) * sum1 - 1 - vector[i]); sum1 = -1; } } } for (int i = 0; i < n; i++) { if (i == 0) { if (vector[0] > 0) { sum2 = -1; answer2 += abs(-1 - vector[0]); } else { sum2 = 1; answer2 += abs(1 - vector[0]); } } else if (sum2 < 0) { if (sum2 + vector[i] > 0) { sum2 += vector[i]; } else { answer2 += abs((-1) * sum2 + 1 - vector[i]); sum2 = 1; } } else { if (sum2 + vector[i] < 0) { sum2 += vector[i]; } else { answer2 += abs((-1) * sum2 - 1 - vector[i]); sum2 = -1; } } } cout << min(answer1, answer2) << 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 sum, ans, n, i; int a[100005]; int main() { cin >> n; for (i = 1; i <= n; i++) { cin >> a[i]; } ans = 0; sum = 0; for (i = 1; i <= n; i++) { if (a[i] == 0) { sum++; } else break; } if (sum % 2 == 0) { if (a[sum + 1] > 0) { a[1] = 1; ans += 1; } else { a[1] = -1; ans += 1; } } else { if (a[sum + 1] > 0) { a[1] = -1; ans += 1; } else { a[1] = 1; ans += 1; } } sum = a[1]; for (i = 2; i <= n; i++) { if (sum == 0) { if (a[i - 1] > 0) { ans++; sum--; } else { ans++; sum++; } } if (sum > 0) { if (a[i] + sum >= 0) { ans += a[i] + sum + 1; sum = -1; } else { sum += a[i]; } } else { if (a[i] + sum <= 0) { ans += abs(a[i] + sum) + 1; sum = 1; } else { sum += a[i]; } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) ans = 0 o = 0 for i in range(n): o += a[i-1] if i == 0: if a[i] == 0: f = "+" a[i] = 1 elif a[0] > 0: f = "+" elif a[0] < 0: f = "-" else: if f == "+": if a[i] + o > 0: c = -1 - o ans += abs(c - a[i]) a[i] = c f = "-" else: if a[i] + o == 0: a[i] -= 1 ans += 1 f = "-" elif f == "-": if a[i] + o < 0: c = 1 - o ans += abs(c - a[i]) a[i] = c f = "+" else: if a[i] + o == 0: a[i] += 1 ans += 1 f = "+" #print(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
UNKNOWN
import scala.io.StdIn import scala.annotation.tailrec object Main extends App { val n = StdIn.readInt val a = StdIn.readLine.split(" ").map(_.toInt) val ans1 = a.tail./:(a.head,0)((acc,i) => { val (bsum, bcnt) = acc val sum = bsum + i val cnt = if(bsum < 0 && sum < 0) 1 - sum else if(bsum >= 0 && sum >= 0) -1 - sum else if(sum == 0) if(bsum < 0) 1 else -1 else 0 // println(sum + " " + cnt) (sum+cnt, bcnt+cnt.abs) })._2 val ans2 = a.tail./:(-a.head,a.head.abs+1)((acc,i) => { val (bsum, bcnt) = acc val sum = bsum + i val cnt = if(bsum < 0 && sum < 0) 1 - sum else if(bsum >= 0 && sum >= 0) -1 - sum else if(sum == 0) if(bsum < 0) 1 else -1 else 0 // println(sum + " " + cnt) (sum+cnt, bcnt+cnt.abs) })._2 println(math.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 a(int arr[], int n) { int sum = arr[0]; int c = 0; for (int i = 1; i < n; i++) { if (sum > 0) { if (sum + arr[i] < 0) sum = sum + arr[i]; else { c += (sum + arr[i]) + 1; sum = -1; } } else { if (sum + arr[i] > 0) sum = sum + arr[i]; else { c += abs(sum + arr[i]) + 1; sum = 1; } } } return c; } int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); ; int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; int ans1 = a(arr, n); int ans2 = 1 + abs(arr[0]); arr[0] = -arr[0]; ans2 += a(arr, n); cout << min(ans1, ans2); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long change_num(long long p[], int N) { long long res = 0; long long next_sum; long long sum = p[0]; for (int i = 1; i < N; i++) { next_sum = sum + p[i]; if ((sum < 0 && next_sum > 0) || (sum > 0 && next_sum < 0)) { sum = next_sum; continue; } if (sum > 0 && next_sum >= 0) { res += next_sum + 1; sum = -1; continue; } if (sum < 0 && next_sum <= 0) { res += 1 - next_sum; sum = 1; continue; } } return res; } int main() { int N; cin >> N; long long a[N]; for (int i = 0; i < N; i++) cin >> a[i]; long long ans = 0; long long sum = a[0]; long long plus_ans = 0; long long minus_ans = 0; if (a[0] == 0) { plus_ans = 1; a[0] = 1; plus_ans += change_num(a, N); minus_ans = 1; a[0] = -1; minus_ans += change_num(a, N); } else if (a[0] > 0) { plus_ans = 0; plus_ans += change_num(a, N); minus_ans = 1 + a[0]; a[0] = -1; minus_ans += change_num(a, N); } else { plus_ans = 1 - a[0]; a[0] = 1; plus_ans += change_num(a, N); minus_ans = 0; minus_ans += change_num(a, N); } if (plus_ans < minus_ans) { ans = plus_ans; } else { ans = minus_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; const int Inf = 1e9; const double EPS = 1e-9; int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } int lcm(int a, int b) { return a * b / gcd(a, b); } int bitCount(long bits) { bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555); bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333); bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f); bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff); return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff); } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; int ans = Inf; int cnt = 0; vector<int> a(n), b(n); for (int i = 0; i < (int)n; ++i) { cin >> a[i]; b[i] = a[i]; } int sum = 0; for (int i = 0; i < (int)n; ++i) { sum += a[i]; if (i % 2 == 1 && sum >= 0) { int diff = abs(sum) + 1; cnt += diff; sum = -1; } else if (i % 2 == 0 && sum <= 0) { int diff = abs(sum) + 1; cnt += diff; sum = 1; } } ans = min(ans, cnt); cnt = 0; sum = 0; for (int i = 0; i < (int)n; ++i) { sum += a[i]; if (i % 2 == 0 && sum >= 0) { int diff = abs(sum) + 1; cnt += diff; sum = -1; } else if (i % 2 == 1 && sum <= 0) { int diff = abs(sum) + 1; cnt += diff; sum = 1; } } ans = min(ans, cnt); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using ll = long long; using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < (int)(n); i++) cin >> a.at(i); ll ans = 0, sum = 0; for (int i = 0; i < (int)(n); i++) { int now = a.at(i); if (i == 0) { if (now == 0) { if (a.at(1) > 0) sum++; else sum--; ans++; } else { sum += now; } continue; } if ((sum < 0 && sum + now > 0) || (sum > 0 && sum + now < 0)) { sum += now; } else { int add = abs(sum + now) + 1; if (sum < 0) sum = 1; else sum = -1; ans += add; } } 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 t; vector<int> answer(2, 0); long long sumi = 0; bool flag = false; cin >> t; vector<long long> A(t); for (int i = 0; i < t; i++) { cin >> A[i]; } for (int j = 0; j < 2; j++) { for (int i = 0; i < t; i++) { sumi += A[i]; if (sumi == 0) { answer[j] += 1; if (flag) { sumi = -1; } else { sumi = 1; } } else if (sumi > 0 == flag) { answer[j] += abs(sumi) + 1; if (sumi > 0) { sumi = -1; } else { sumi = 1; } } flag = !flag; } flag = true; sumi = 0; } cout << min(answer[0], answer[1]) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1000000001; const double PI = 3.141592653589; const long long LMAX = 1000000000000001; long long gcd(long long a, long long b) { if (a < b) swap(a, b); while ((a % b) != 0) { a = b; b = a % b; } return b; } int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<vector<long long>> dp(2, vector<long long>(n, 0)); vector<long long> sum(2, a[0]); if (sum[0] > 0) sum[1] = -1; else sum[1] = 1; dp[1][0] = abs(a[0]) + 1; for (int j = 0; j < 2; j++) { for (int i = 1; i < n; i++) { if (sum[j] > 0) { if (sum[j] + a[i] < 0) { dp[j][i] = dp[j][i - 1]; sum[j] += a[i]; } else { dp[j][i] = dp[j][i - 1] + abs(sum[j] + a[i]) + 1; sum[j] = -1; } } else { if (sum[j] + a[i] > 0) { dp[j][i] = dp[j][i - 1]; sum[j] += a[i]; } else { dp[j][i] = dp[j][i - 1] + abs(sum[j] + a[i]) + 1; sum[j] = 1; } } } } cout << min(dp[0][n - 1], dp[1][n - 1]) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #define forx(i,a,b) for(int i=(a);i<(b);i++) #define rep(j,n) for(int j=0;j<(n);j++) typedef long long ll; int main() { int n,ansa=0,ansb=0,suma=0;sumb=0,cin>>n; bool plus=true; rep(i,n){ int a,b; cin>>b; a=b; while(plus&&suma+a<=0){ a++; ansa++; } while(!plus&&suma+a>=0){ a--; ansa++; } while(plus&&sumb+b>=0){ b++; ansb++; } while(!plus&&sumb+b<=0){ b--; ansb++; } suma+=a; sumb+=b; plus=!plus; } cout<<min(ansa,ansb)<<endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } const long long INF = 1ll << 60; const long long MOD = 1e9 + 7ll; const double PI = 3.14159265358979323846; int main() { int n; cin >> n; vector<long long> A(n); for (int i = 0; i < n; i++) scanf("%lld", &A.at(i)); long long res1 = 0ll; long long res2 = 0ll; vector<long long> sum1(n); vector<long long> sum2(n); if (A.at(0) == 0) { sum1.at(0) = 1ll; sum2.at(0) = -1ll; } else if (A.at(0) > 0) { sum1.at(0) = A.at(0); sum2.at(0) = -1ll; res2 += A.at(0) + 1ll; } else { sum1.at(0) = A.at(0); sum2.at(0) = 1ll; res2 += -A.at(0) + 1ll; } for (int i = 1; i < n; i++) { sum1.at(i) = sum1.at(i - 1) + A.at(i); if (sum1.at(i) == 0) { if (sum1.at(i - 1) < 0) sum1.at(i) = 1ll; else sum1.at(i) = -1ll; res1++; } if (sum1.at(i) * sum1.at(i - 1) > 0) { if (sum1.at(i) < 0) { res1 += -sum1.at(i) + 1ll; sum1.at(i) = 1ll; } else { res1 += sum1.at(i) + 1ll; sum1.at(i) = -1ll; } } sum2.at(i) = sum2.at(i - 1) + A.at(i); if (sum2.at(i) == 0) { if (sum2.at(i - 1) < 0) sum2.at(i) = 1ll; else sum2.at(i) = -1ll; res2++; } if (sum2.at(i) * sum2.at(i - 1) > 0) { if (sum2.at(i) < 0) { res2 += -sum2.at(i) + 1ll; sum2.at(i) = 1ll; } else { res2 += sum2.at(i) + 1ll; sum2.at(i) = -1ll; } } } long long res = min(res1, res2); cout << res << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int solve(std::vector<int> &v, bool positiveStart) { int sum = v[0]; int count = 0; if (positiveStart && sum < 0) { count = 1 - sum; sum = 1; } if (!positiveStart && sum > 0) { count = sum - (-1); sum = -1; } for (int i = 1; i < v.size(); i++) { int tmp = sum + v[i]; if (sum * tmp < 0) { sum = tmp; continue; } int next_sum = (-1) * sum / abs(sum); count += abs(next_sum - tmp); sum = next_sum; } return count; } int main() { int n; std::cin >> n; std::vector<int> v(n); for (int i = 0; i < v.size(); i++) { std::cin >> v[i]; } int count = std::min(solve(v, true), solve(v, false)); std::cout << count << std::endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int main(int argc, char const *argv[]) { int N; std::cin >> N; long long a[N]; for (size_t i = 0; i < N; i++) { std::cin >> a[i]; } int sum_1 = 0; int ans_1 = 0; for (size_t i = 0; i < N; i++) { if (i % 2 == 0 && sum_1 + a[i] >= 0) { ans_1 += sum_1 + a[i] + 1; sum_1 = -1; } else if (i % 2 == 1 && sum_1 + a[i] <= 0) { ans_1 += 1 - sum_1 - a[i]; sum_1 = 1; } else { sum_1 += a[i]; } } int sum_2 = 0; int ans_2 = 0; for (size_t i = 0; i < N; i++) { if (i % 2 == 0 && sum_2 + a[i] <= 0) { ans_2 += 1 - sum_2 - a[i]; sum_2 = 1; } else if (i % 2 == 1 && sum_2 + a[i] >= 0) { ans_2 += sum_2 + a[i] + 1; sum_2 = -1; } else { sum_2 += a[i]; } } std::cout << std::min(ans_1, ans_2) << '\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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; std::vector<int64_t> data(n); for (int i = 0; i < n; i++) { cin >> data.at(i); } int answer = 0; int64_t sum_a = data.at(0); for (int i = 1; i < n; i++) { sum_a += data.at(i); if (data.at(0) > 0) { if (i % 2 != 0 && sum_a >= 0) { while (sum_a >= 0) { sum_a--; answer++; } } if (i % 2 == 0 && sum_a <= 0) { while (sum_a <= 0) { sum_a++; answer++; } } } if (data.at(0) < 0) { if (i % 2 != 0 && sum_a <= 0) { while (sum_a <= 0) { sum_a++; answer++; } } if (i % 2 == 0 && sum_a >= 0) { while (sum_a >= 0) { sum_a--; answer++; } } } } cout << answer << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MAXN = 100 * 1000 + 10; int main() { long long n, f = 0, z = 0, s = 0, sum = 0; cin >> n; long long b[n]; for (long long i = 0; i < n; i++) { cin >> b[i]; if (i % 2 == 0) { f += b[i]; } else { z += b[i]; } } if (f > z) { if (b[0] <= 0) { s += -1 * b[0] + 1; b[0] = 1; } } else { if (b[0] >= 0) { s += b[0] + 1; b[0] = -1; } } for (long long i = 0; i < n - 1; i++) { sum += b[i]; if (sum < 0 && sum + b[i + 1] < 0) { s += -1 * (sum + b[i + 1]); b[i + 1] += -1 * (sum + b[i + 1]); } else if (sum > 0 && sum + b[i + 1] >= 0) { s += sum + b[i + 1] + 1; b[i + 1] -= sum + b[i + 1]; } if (sum + b[i + 1] == 0) { if (sum < 0) { b[i + 1] += 1; } else { b[i + 1] -= 1; } s++; } } cout << s; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a; for (int i = 0; i < n; i++) { long long ai; cin >> ai; a.push_back(ai); } long long count = 0; if (a.at(0) == 0) { a.at(0) = 1; count = 1; } long long sum = a.at(0); for (int i = 0; i < n - 1; i++) { long long next_sum = sum + a.at(i + 1); if (sum > 0 && next_sum >= 0) { long long diff = 1 + next_sum; count += diff; a.at(i + 1) -= diff; next_sum -= diff; } else if (sum < 0 && next_sum <= 0) { long long diff = 1 - next_sum; count += diff; a.at(i + 1) += diff; next_sum += diff; } sum = next_sum; } cout << count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 7; long long n, g, ans, t; int main() { cin >> n; cin >> t; g = t; while (--n) { cin >> t; if (g > 0) { g += t; if (g > -1) { ans += g + 1; g = -1; } } else { g += t; if (g < 1) { ans += 1 - g; g = 1; } } } cout << ans; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) a = list(map(int, input().split())) ans = 0 _a = [a[0]] prev_sign = 1 if a[0] < 0: prev_sign = -1 for i in range(1, N): c = a[i] if c >= 0 and prev_sign > 0: ans += abs(c - (-1)) c = -1 elif c <= 0 and prev_sign < 0: ans += abs(c - 1) c = 1 if c > 0: prev_sign = 1 else: prev_sign = -1 _a.append(c) __a = [_a[0]] acm_sum = _a[0] for i in range(1, N): c = _a[i] if abs(acm_sum) >= abs(c): if c < 0: c = -1*(abs(acm_sum)+1) else: c = abs(acm_sum)+1 acm_sum += c ans += abs(c - _a[i]) __a.append(c) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long n; scanf("%ld", &n); vector<long> a(n); for (long i = 0; i < n; i++) scanf(" %ld", &a[i]); long sum = a[0]; long j = 0; for (long i = 1; i < n; i++) { if (sum * (sum + a[i]) < 0) sum += a[i]; else { j += abs(sum + a[i]) + 1; if (sum < 0) sum = 1; else if (sum > 0) sum = -1; } } printf("%ld\n", j); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int, input().split())) currentSum = 0 count1 = 0 count2 = 0 for i in range(N): restSum = currentSum currentSum += A[i] if currentSum <= 0 and restSum < 0: count1 += abs(currentSum) + 1 currentSum = 1 elif currentSum >= 0 and restSum > 0: count1 += abs(currentSum) + 1 currentSum = -1 elif currentSum == 0 and restSum == 0: count1 += 1 currentSum = -1 for i in range(N): restSum = currentSum currentSum += A[i] if currentSum <= 0 and restSum < 0: count2 += abs(currentSum) + 1 currentSum = 1 elif currentSum >= 0 and restSum > 0: count2 += abs(currentSum) + 1 currentSum = -1 elif currentSum == 0 and restSum == 0: count2 += 1 currentSum = -1 print(min(count1, count2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# -*- coding: utf-8 -*- n = int(input()) an = list(map(int, input().split())) sum = an[0] ans = 0 for i in range(1,n): if sum * (sum + an[i]) < 0: sum += an[i] else: if sum > 0: ans += abs(sum + an[i] + 1) sum = -1 else: ans += abs(sum + an[i] - 1) sum = 1 ans1 = ans sum = -an[0] ans = an[0] for i in range(1, n): if sum * (sum + an[i]) < 0: sum += an[i] else: if sum > 0: ans += abs(sum + an[i] + 1) sum = -1 else: ans += abs(sum + an[i] - 1) sum = 1 print(min([ans1,ans]))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; long a[110000], s[110000]; cin >> n; for (int(i) = (0); (i) < (n); (i)++) cin >> a[i]; long t = 0; for (int(i) = (0); (i) < (n); (i)++) { t += a[i]; s[i] = t; } int diff; long cnt = 0; for (int(i) = (1); (i) < (n); (i)++) { if (s[i - 1] < 0 && s[i] <= 0) { diff = 1 - s[i]; s[i] = 1; cnt += diff; for (int(j) = (i + 1); (j) < (n); (j)++) s[j] += diff; } else if (s[i - 1] > 0 && s[i] >= 0) { diff = s[i] + 1; s[i] = -1; cnt += diff; for (int(j) = (i + 1); (j) < (n); (j)++) s[j] -= diff; } } 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
python3
n = int(input()) a = list(map(int,input().split())) count=0 x = 0 if a[0]>0: for i in range(n): if i%2==0: x+=a[i] if x<0: count+=-1*x+1 a[i]+=-1*x+1 x+=-1*x+1 else: x+=a[i] if x>0: count+=x+1 a[i]+=-1*x-1 x+=-1*x-1 else: for i in range(n): if i%2==0: x+=a[i] if x>0: count+=x+1 a[i]+=-1*x-1 x+=-1*x-1 else: x+=a[i] if x<0: count+=-1*x+1 a[i]+=-1*x+1 x+=-1*x+1 if x==0: count+=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
python3
n=int(input()) b=list(map(int,input().split())) a=b[:] condition='' cnt=0 wa=0 for i in range(n): wa+=a[i] if i == 0: if a[i]>0: condition='minus' else: condition='plus' elif condition == 'plus': condition='minus' if wa<=0: cnt+=abs(wa)+1 a[i]+=abs(wa)+1 wa+=abs(wa)+1 elif condition == 'minus': condition='plus' if wa>=0: cnt+=abs(wa)+1 a[i]-=abs(wa)+1 wa-=abs(wa)+1 cnt1=cnt a=b[:] condition='' cnt=0 wa=0 for i in range(n): a[i]=a[i]/abs(a[i])*(-1) cnt+=abs(a[i])+1 wa+=a[i] if i == 0: if a[i]>0: condition='minus' else: condition='plus' elif condition == 'plus': condition='minus' if wa<=0: cnt+=abs(wa)+1 a[i]+=abs(wa)+1 wa+=abs(wa)+1 elif condition == 'minus': condition='plus' if wa>=0: cnt+=abs(wa)+1 a[i]-=abs(wa)+1 wa-=abs(wa)+1 cnt2=cnt print(min(cnt1,cnt2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; enum State { Plus, Minus, Zero }; State GetState(int sum) { State state; if (sum > 0) state = Plus; else if (sum == 0) state = Zero; else state = Minus; return state; } int main() { int n; cin >> n; vector<long long> a(n); cin >> a[0]; unsigned long long count = 0; State state = GetState(a[0]); if (state == Zero) { a[0] = 1; state = Plus; count++; } long long sum = a[0]; for (int i = 1; i < n; i++) { cin >> a[i]; State nextState = GetState(sum + a[i]); switch (nextState) { case Plus: if (state == Plus) { long long bf_a = a[i]; a[i] = -1 - sum; count += abs(a[i] - bf_a); nextState = Minus; } break; case Minus: if (state == Minus) { long long bf_a = a[i]; a[i] = 1 - sum; count += abs(a[i] - bf_a); nextState = Plus; } break; case Zero: if (state == Plus) { long long bf_a = a[i]; a[i] = -1 - sum; count += abs(a[i] - bf_a); nextState = Minus; } else if (state == Minus) { long long bf_a = a[i]; a[i] = 1 - sum; count += abs(a[i] - bf_a); nextState = Plus; } default: break; } sum += a[i]; state = nextState; } if (sum == 0) count++; cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
object Main { def main(args: Array[String]): Unit = { import scala.io.StdIn.readLine val _ = readLine val datA = readLine.split(" ").map(_.toLong) val work = datA.map(a => (a, 0L)).foldLeft((0L, 0L)){ case ((sum, count),(a, _)) => if (sum > 0) { if (sum + a >= 0) (-1, count + math.abs(sum + a) + 1) else (sum + a, count) } else if (sum < 0) { if (sum + a <= 0) (1, count + math.abs(sum + a) + 1) else (sum + a, count) } else { (a, count) } } val ans = work._2 println(ans) } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long counter = 0; long a[100100], b[100100]; bool hugou = true; for (int i = 0; i < n; i++) { cin >> a[i]; } b[0] = a[0]; if (b[0] == 0) { counter++; b[0]++; hugou = true; } if (b[0] < 0) { hugou = false; } if (hugou) { for (int i = 1; i < n; i++) { b[i] = b[i - 1] + a[i]; if (i % 2 == 0) { if (b[i] < 1) { counter += 1 - b[i]; b[i] = 1; } } else { if (b[i] > -1) { counter += b[i] - (-1); b[i] = -1; } } } } else { for (int i = 1; i < n; i++) { b[i] = b[i - 1] + a[i]; if (i % 2 == 0) { if (b[i] > -1) { counter += b[i] - (-1); b[i] = -1; } } else { if (b[i] < 1) { counter += 1 - b[i]; b[i] = 1; } } } } cout << counter << 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
# input n = int(input()) A = list(map(int, input().split())) # 偶数番目が+ A_even = A S_even = 0 ans_even = 0 for i in range(n): if (S_even + A_even[i]) * ((-1) ** (i - 1)) > 0: S_even += A_even[i] continue A_even[i] = (-1) ** (i - 1) - S_even ans_even += abs(A[i] - A_even[i]) S_even = (-1) ** (i - 1) # 奇数番目が+ A_odd = A S_odd = 0 ans_odd = 0 for i in range(n): if (S_odd + A_odd[i]) * ((-1) ** i) > 0: S_odd += A_odd[i] continue A_odd[i] = (-1) ** i - S_odd ans_odd += abs(A[i] - A_odd[i]) S_odd = (-1) ** i ans = int(min(ans_even, ans_odd)) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int INF = 100100100; const double PI = 3.14159265358979323846; int main() { int n, a[100001] = {0}; int res = 0; cin >> n; for (int i = 0; i < (n); ++i) cin >> a[i]; int minPlus = 0, minMinus = 0; long long sumPlus = 0, sumMinus = 0; for (int i = 0; i < (n); ++i) { sumPlus += a[i]; if (i % 2 == 0 && sumPlus <= 0) { minPlus += 1 - sumPlus; sumPlus = 1; } else if (i % 2 == 1 && sumPlus >= 0) { minPlus += sumPlus + 1; sumPlus = -1; } } for (int i = 0; i < (n); ++i) { sumMinus += a[i]; if (i % 2 == 0 && sumMinus >= 0) { minMinus += sumMinus + 1; sumMinus = -1; } else if (i % 2 == 1 && sumMinus <= 0) { minMinus += 1 - sumMinus; sumMinus = 1; } } res = minPlus < minMinus ? minPlus : minMinus; cout << res << 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; int main() { long long n, array1[2 * N], counter = 0, i, sum = 0; scanf("%lld", &n); for (i = 0; i < n; ++i) { scanf("%lld", &array1[i]); } if (array1[0] == 0) { if (array1[1] >= 0) { array1[0] = -1; } else { array1[0] = 1; } counter++; } sum = array1[0]; for (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; } printf("%lld", counter); 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 n; cin >> n; int i; long a[n], su, cnt, cnt2; su = 0; cnt = 0; cnt2 = 0; for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 0; i < n; i++) { su += a[i]; if (a[0] >= 0) { if (i % 2 == 0) { if (su <= 0) { cnt += 1 - su; su = 1; } } else { if (su >= 0) { cnt += su + 1; su = -1; } } } else { if (i % 2 == 0) { if (su >= 0) { cnt += su + 1; su = -1; } } else { if (su <= 0) { cnt += 1 - su; su = 1; } } } } for (i = 0; i < n; i++) { su += a[i]; if (a[0] > 0) { if (i % 2 == 0) { if (su <= 0) { cnt2 += 1 - su; su = 1; } } else { if (su >= 0) { cnt2 += su + 1; su = -1; } } } else { if (i % 2 == 0) { if (su >= 0) { cnt2 += su + 1; su = -1; } } else { if (su <= 0) { cnt2 += 1 - su; su = 1; } } } } cout << min(cnt, cnt2) << 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 dy[] = {0, 0, 1, -1, 0}; int dx[] = {1, -1, 0, 0, 0}; template <class T = int> T in() { T x; cin >> x; return (x); } template <class T> void print(T& x) { cout << x << '\n'; } const int MOD = (int)1e9 + 7; const int MAX = 510000; long long fac[MAX], finv[MAX], inv[MAX]; void COMint() { 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 gcd(long long a, long long b) { if (b == 0) return a; if (a > b) { swap(a, b); } return gcd(a, b % a); } long long lcm(long long a, long long b) { long long g; g = gcd(a, b); return a * b / g; } double ep(int all, double p) { return all * (1 / p); } int main() { ios::sync_with_stdio(false); cin.tie(0); int N = in(); vector<int> a(N); for (int i = 0; i < (N); ++i) { cin >> a[i]; } int sum; sum = 0; int ans; ans = 0; int fugo; fugo = 1; for (int i = 0; i < (N); ++i) { sum += a[i]; if (sum * fugo == 0) { ans++; sum = fugo; } else if (sum * fugo < 0) { ans += abs(sum - fugo); sum = fugo; } fugo *= -1; } fugo = -1; sum = 0; int ans_; ans_ = 0; for (int i = 0; i < (N); ++i) { sum += a[i]; if (sum * fugo == 0) { ans_++; sum = fugo; } else if (sum * fugo < 0) { ans_ += abs(sum - fugo); sum = fugo; } fugo *= -1; } print(min(ans, ans_)); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n,ans; cin>>n; vector<int>a(n); for(int i=0;i<n;i++){ cin>>a.at(i); } for(int i=0;i<n-1;i++){ while(a.at(i)*a.at(i+1)>=0){ if(a.at(i)>0){ a.at(i+1)--; ans++; } if(a.at(i)<0){ a.at(i+1)++; ans++; } } int p=0; for(int j=i+1;j>=0;j--)p+=a.at(j);//sum if(p==0){ if(a.at(i)>0)a.at(i+1)++; else a.at(i+1)--; ans++; } cout<<ans<<endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# -*- coding: utf-8 -*- # 整数の入力 n=int(input()) a=input().split() counter=0 S=a[0] # 出力 for i in range(1,n): S+=a[i] if S<0 and S+int(a[i])<=0: counter+=-S-int(a[i])+1 a[i]=-S+1 elif S>0 and S+int(a[i])>=0: counter+=S+int(a[i])+1 a[i]=-S-1 print(counter)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) list_a = list(map(int, input().split())) sum_a = list_a[0] count = 0 for i in range(1, n): if sum_a > 0: if sum_a + list_a[i] >= 0: diff = abs(-1 - list_a[i] - sum_a) else: diff = 0 sum_a = sum_a + list_a[i] - diff count += diff else: if sum_a + list_a[i] <= 0: diff = abs(1 - sum_a - list_a[i]) else: diff = 0 sum_a = sum_a + list_a[i] + diff count += diff 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
python3
n = int(input()) a = list([int(i) for i in input().split()]) sum_a = [] count = 0 sum_a.append(a[0]) for i in range(1,n): sum_a.append(sum_a[i-1] + a[i]) if sum_a[i] == 0: if sum_a[i-1] < 0: sum_a[i] += 1 count += 1 else: sum_a[i] -= 1 count += 1 elif sum_a[i-1] * sum_a[i]>0: if sum_a[i] > 0: sum_a[i] = -1 count += 1 + abs(sum_a[i-1]+a[i]) else: sum_a[i] = 1 count += 1 + abs(sum_a[i-1]+a[i]) print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
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; 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; } } } println!("{}", ans); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int,input().split())) s = A[0] ans = 0 for k in range(1,N): if s*(s+A[k])<=-1: s += A[k] else: if s > 0: if s + A[k] >= 0: ans += s + A[k] + 1 s = -1 elif s < 0: if s + A[k] <= 0: ans += 1 - s - A[k] 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
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]; bool positive; if (a[0] > 0) positive = true; else positive = false; int total, cnt = 0; for (int i = 1; i < n; ++i) { total = accumulate(a.begin(), a.begin() + i, 0) + a[i]; if (positive && total >= 0) { a[i] -= (total + 1); cnt += (total + 1); positive = false; } else if (!positive && total <= 0) { a[i] += (abs(total) + 1); cnt += (abs(total) + 1); positive = true; } else positive = !positive; } cout << cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> sum(N,0); int now; cin >> now; sum[0] = now; for (int i=1; i<N; i++) { cin >> now; sum[i] = sum[i-1] + now; } long change = 0; long ansp = 0; int i = 0; while (i<N) { ansp += max(1-(sum[i]+change),0); change += max(1-(sum[i]+change),0); i++; if (i==N) { break; } ansp += max((sum[i]+change)+1,0); change -= max((sum[i]+change)+1,0); i++; } change = 0; long ansm = 0; i = 0; while (i<N) { ansm += max((sum[i]+change)+1,0); change -= max((sum[i]+change)+1,0); i++; if (i==N) { break; } ansm += max(1-(sum[i]+change),0); change += max(1-(sum[i]+change),0); i++; } cout << min(ansp,ansm) << 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[100004]; int main() { scanf("%d", &n); for (int i = (1); i <= (int)(n); ++i) scanf("%lld", &a[i]); long long ans = 0; if (!a[1]) ++ans; for (int i = (2); i <= (int)(n); ++i) { if (a[i - 1] > 0) { if (a[i] + a[i - 1] < 0) { a[i] += a[i - 1]; continue; } ans += abs(a[i] + 1 + a[i - 1]); a[i] = -1; } else { if (a[i] + a[i - 1] > 0) { a[i] += a[i - 1]; continue; } ans += abs(a[i] - 1 + a[i - 1]); a[i] = 1; } } printf("%lld\n", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int N; cin >> N; vector<long long> a(N); for (int i = 0; i < N; i++) { cin >> a.at(i); } int ans1 = 0; int ans2 = 0; long long mysum1 = 0; long long mysum2 = 0; for (int i = (0); i < (N); ++i) { mysum1 += a.at(i); if ((i % 2 == 0) & (mysum1 <= 0)) { ans1 += abs(mysum1) + 1; mysum1 = 1; } else if ((i % 2 == 1) & (mysum1 >= 0)) { ans1 += abs(mysum1) + 1; mysum1 = -1; } } for (int i = (0); i < (N); ++i) { mysum2 += a.at(i); if ((i % 2 == 1) & (mysum2 <= 0)) { ans2 += abs(mysum2) + 1; mysum2 = 1; } else if ((i % 2 == 0) & (mysum2 >= 0)) { ans2 += abs(mysum2) + 1; mysum2 = -1; } } cout << min(ans1, ans2) << '\n'; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> as(n); for (int i = 0; i < n; i++) { cin >> as[i]; } long long kotae1 = 0; int sum1 = 0; long long kotae2 = 0; int sum2 = 0; for (int i = 0; i < n; i++) { sum1 += as[i]; if (i % 2 == 0) { if (sum1 <= 0) { kotae1 += (1 - sum1); sum1 = 1; } } else { if (sum1 >= 0) { kotae1 += (sum1 - (-1)); sum1 = -1; } } } for (int i = 0; i < n; i++) { sum2 += as[i]; if (i % 2 == 1) { if (sum2 <= 0) { kotae2 += (1 - sum2); sum2 = 1; } } else { if (sum2 >= 0) { kotae2 += (sum2 - (-1)); sum2 = -1; } } } int kotae = 0; kotae = kotae1 < kotae2 ? kotae1 : kotae2; cout << kotae << 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
# # abc059 c # import sys from io import StringIO import unittest class TestClass(unittest.TestCase): def assertIO(self, input, output): stdout, stdin = sys.stdout, sys.stdin sys.stdout, sys.stdin = StringIO(), StringIO(input) resolve() sys.stdout.seek(0) out = sys.stdout.read()[:-1] sys.stdout, sys.stdin = stdout, stdin self.assertEqual(out, output) def test_入力例_1(self): input = """4 1 -3 1 0""" output = """4""" self.assertIO(input, output) def test_入力例_2(self): input = """5 3 -6 4 -5 7""" output = """0""" self.assertIO(input, output) def test_入力例_3(self): input = """6 -1 4 3 2 -5 4""" output = """8""" self.assertIO(input, output) def resolve(): N = int(input()) A = list(map(int, input().split())) ans = 0 s = A[0] f = A[0] // abs(A[0]) for i in range(1, N): a = A[i] if f == 1 and s+a >= 0: ans += abs(s+a) + 1 s = -1 elif f == -1 and s+a <= 0: ans += abs(s+a) + 1 s = 1 else: s += a f = s // abs(s) print(ans) if __name__ == "__main__": # unittest.main() resolve()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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<int> a; int N; signed main() { cin >> N; for (int i = 0; i < (int)(N); i++) { int t; cin >> t; a.push_back((t)); } int res = 0; int cnt = 0; int sum = 0; for (int i = 0; i < (int)(N); i++) { sum += a[i]; if (sum <= 0) { cnt += -sum + 1; sum = 1; } i++; if (i == N) break; sum += a[i]; if (sum >= 0) { cnt += sum + 1; sum = -1; } } res = cnt; sum = 0; cnt = 0; for (int i = 0; i < (int)(N); i++) { sum += a[i]; if (sum >= 0) { cnt += sum + 1; sum = -1; } i++; if (i == N) break; sum += a[i]; if (sum <= 0) { cnt += -sum + 1; sum = 1; } } res = min(res, cnt); cout << res << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# -*- coding: utf-8 -*- import sys import numpy as np read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines N = int(readline()) A = list(map(int,readline().split())) S = np.cumsum(A) ans1 = 0 ans2 = 0 # 偶数項-,奇数項を+にする for i in range(N): if i & 1: if S[i] >= 0: ans1 += S[i]+1 S -= S[i]+1 else: if S[i] <= 0: ans1 += 1-S[i] S += 1-S[i] S = np.cumsum(A) for i in range(N): if i & 1: if S[i] <= 0: ans2 += 1-S[i] S += 1-S[i] else: if S[i] >= 0: ans2 += S[i]+1 S -= S[i]+1 print(min(ans1,ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const double EPS = 1e-9; const int INF = 1 << 29; long long int a[100054]; int main() { int n; cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; long long int ans = 0; long long int sum = a[1]; for (int i = 2; i <= n; ++i) { if (sum < 0) { long long int num = 1 - sum; if (a[i] < num) ans += num - a[i], a[i] = num; } else { long long int num = -1 - sum; if (a[i] > num) ans += a[i] - num, a[i] = num; } 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; class MYCP { public: static const long long TEISUU = 1000 * 1000 * 1000 + 7; static long long DebugFlag; static string MakeString_LongLong(vector<long long> const& numbers, string const& str) { if (numbers.size() == 0) return ""; string result = "" + to_string(numbers[0]); for (long long i = 1; i < numbers.size(); i++) { result += str; result += to_string(numbers[i]); } return result; } static string MakeString_LongLong(vector<long long> const& numbers) { if (numbers.size() == 0) return ""; string result = "" + to_string(numbers[0]); for (long long i = 1; i < numbers.size(); i++) { result += " "; result += to_string(numbers[i]); } return result; } static string MakeString_VectorString(vector<string> const& str) { string result = ""; for (long long i = 0; i < str.size(); i++) { result += str[i] + "\n"; } return result; } static vector<string> MyReadLineSplit(long long n) { vector<string> str(n); for (long long i = 0; i < n; i++) { std::cin >> str[i]; } return str; } static vector<long long> ReadInts(long long number) { vector<long long> a(number); for (int i = 0; i < number; i++) { std::cin >> a[i]; } return a; } static bool PrimeCheck_Int(long long number) { if (number < 2) return false; for (unsigned long long i = 2; i * i <= number; i++) { if (number % i == 0) return false; } return true; } static vector<long long> MakePrimeList(long long n) { vector<long long> list; long long i, j, p; bool flag; for (i = 2; i <= n; i++) { flag = true; for (j = 0; j < list.size(); j++) { if (!(list[j] * list[j] <= i)) break; if (i % list[j] == 0) { flag = false; break; } } if (flag) list.push_back(i); } return list; } static vector<string> split(string const& str, char sep) { vector<std::string> v; auto first = str.begin(); while (first != str.end()) { auto last = first; while (last != str.end() && *last != sep) last++; v.push_back(string(first, last)); if (last != str.end()) last++; first = last; } return v; } static long long Sum(vector<long long> a) { long long i, sum = 0; for (i = 0; i < a.size(); i++) { sum += a[i]; } return sum; } static bool Komoji(char a) { if (a >= 'a' && a <= 'z') return true; return false; } static bool Oomoji(char a) { if (a >= 'A' && a <= 'Z') return true; return false; } static long long KiriageWarizan(long long a, long long b) { long long result = a / b; if (a % b > 0) result++; return result; } static long long GreatestCommonFactor(long long a, long long b) { long long temp; if (a < b) { temp = b; b = a; a = temp; } while (true) { temp = a % b; a = b; b = temp; if (b == 0) break; } return a; } static long long LeastCommonMultiple(long long a, long long b) { return (a / GreatestCommonFactor(a, b)) * b; } static vector<vector<long long> > PrimeFactorization(long long n) { vector<long long> p_list, s_list; long long i, j, k, count; for (i = 2; n > 1; i++) { if (i * i > n) { p_list.push_back(n); s_list.push_back(1); break; } if (n % i == 0) { count = 0; while (n % i == 0) { n /= i; count++; } p_list.push_back(i); s_list.push_back(count); } } vector<vector<long long> > result; result.push_back(p_list); result.push_back(s_list); return result; } static long long Combination(long long n, long long r) { r = min(r, n - r); vector<long long> p(n + 1, 0); long long i, j, k, a, b, c; for (i = 1; i <= r; i++) { auto temp = MYCP::PrimeFactorization(i); for (j = 0; j < temp[0].size(); j++) { p[temp[0][j]] -= temp[1][j]; } a = i + n - r; temp = MYCP::PrimeFactorization(a); for (j = 0; j < temp[0].size(); j++) { p[temp[0][j]] += temp[1][j]; } } long long result = 1; for (i = 0; i < p.size(); i++) { if (p[i] > 0) { for (j = 0; j < p[i]; j++) { result *= i; result %= MYCP::TEISUU; } } } return result; } static long long sign(long long x) { if (x > 0) return 1; if (x < 0) return -1; return 0; } static long long DebugPrintf(string output) { if (MYCP::DebugFlag != 0) { std::cout << output << endl; } return MYCP::DebugFlag; } static long long DebugCin() { long long a; if (MYCP::DebugFlag != 0) { cin >> a; } return a; } }; long long MYCP::DebugFlag = 0; class Syakutori { private: vector<long long> list; public: void MakeArray(vector<long long> data) { long long i; list = data; list.push_back(0); list[0] = 0; for (i = 1; i < list.size(); i++) { list[i] = list[i - 1] + data[i - 1]; } } long long Sum(long long start, long long end) { if (end < start) { std::cout << "startがendより大きいです"; return 0; } if (start < 0 || end >= list.size()) { std::cout << "範囲が異常"; return 0; } return list[end] - list[start]; } }; int main(void) { MYCP::DebugFlag = 0; long long i, j, k, n, m; long long count = 0; cin >> n; auto a = MYCP::ReadInts(n); if (a[0] == 0) { a[0]++; count++; } long long sum = a[0], next; for (i = 1; i < n; i++) { long long sign = MYCP::sign(sum); sign *= MYCP::sign(sum + a[i]); if (sign != -1) { sign = MYCP::sign(sum) * (-1); k = sign - sum; count += abs(a[i] - k); a[i] = k; } sum += a[i]; } cout << count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) a = [int(i) for i in input().split()] sam = a[0] old = sam num = 0 for i in range(1, len(a)): sam += a[i] if sam >= 0 and old > 0: num += (abs(sam) + 1) sam -= (sam + 1) elif sam <= 0 and old < 0: num += (abs(sam) + 1) sam -= (sam + 1) old = sam print(sam) print(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
UNKNOWN
solver::[Integer]->Integer solver xs = if head xs == 0 then min (check_tot 1 1 (tail xs)) (check_tot 1 (-1) (tail xs)) else check_tot 0 (head xs) (tail xs) main::IO() main=do _<-getLine datc<-getLine print (solver (map read (words datc))) --おそい。Step_sumを作る事無く、シーケンシャルにいく --今のカウント手数、ここまでの修正されたトータル(これはゼロでない事が保証される)、食べるリスト。 check_tot::Integer -> Integer -> [Integer] -> Integer check_tot st _ [] = st check_tot st tot xs | (tot > 0)&&((tot+(head xs))>=0) = let dec = (tot+(head xs))+1 in check_tot (dec+st) (-1) (tail xs) | (tot > 0)&&((tot+(head xs)) <0) = check_tot st (tot+(head xs)) (tail xs) | (tot < 0)&&((tot+(head xs)) >0) = check_tot st (tot+(head xs)) (tail xs) | (tot < 0)&&((tot+(head xs))<=0) = let inc = 1-(tot+(head xs)) in check_tot (inc+st) 1 (tail xs)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def sign(x): if x>0: return 1 elif x<0: return -1 else: return 0 n = int(input()) a = [int(i) for i in input().split()] s = [sum(a[:i+1]) for i in range(n)] sp = [s[i] for i in range(n)] sm = [s[i] for i in range(n)] P0=0 M0=0 if s[0]==0: P0+=1 M0+=1 for j in range(n): sp[j]+=1 sm[j]+=1 elif s[0]>0: M0+=abs(s[0]+1) for j in range(n): sm[j]-=(abs(s[0])+1) else: P0+=abs(s[0]+1) for j in range(n): sp[j]+=(abs(s[0])+1) for i in range(1,n): if sm[i-1]*sm[i]>=0: M0+=abs(sm[i])+1 h=sign(sm[i-1])*(-1)*(abs(sm[i])+1) for j in range(i,n): sm[j]+=h if sp[i-1]*sp[i]>=0: P0+=abs(sp[i])+1 h=sign(sp[i-1])*(-1)*(abs(sp[i])+1) for j in range(i,n): sp[j]+=h print(min([M0,P0]))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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; vector<long long> a(n); for (long long i = 0; i < (long long)n; ++i) cin >> a[i]; long long ans = 0; long long sum = 0; for (long long i = 0; i < (long long)n; ++i) { if (sum < 0 and sum + a[i] <= 0) { ans += -(sum + a[i]) + 1; a[i] = -sum + 1; } else if (sum > 0 and sum + a[i] >= 0) { ans += sum + a[i] + 1; a[i] = -sum - 1; } sum += a[i]; } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num_count = sc.nextInt(); int[] array = new int[num_count]; int first_plus_cost = 0; int sum = 0; for(int i = 0;i < num_count;i++){ array[i] = sc.nextInt(); } for(int i = 0;i < num_count;i++){ int temp = sum; temp += array[i]; if(i % 2 == 0 && temp <= 0){ int cost = 1 - temp; first_plus_cost += cost; temp += cost; }else if(i % 2 == 1 && temp >= 0){ int cost = 1 + temp; first_plus_cost += cost; temp -= cost; } sum = temp; } int second_plus_cost = 0; sum = 0; for(int i = 0;i < num_count;i++){ int temp = sum; temp += array[i]; if(i % 2 == 0 && temp >= 0){ int cost = 1 + temp; second_plus_cost += cost; temp -= cost; }else if(i % 2 == 1 && temp <= 0){ int cost = 1 - temp; second_plus_cost += cost; temp += cost; } sum = temp; } int min_cost = first_plus_cost < second_plus_cost ? first_plus_cost : second_plus_cost; System.out.println(min_cost); sc.close(); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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 ans1 = 0; int ans2 = 0; int sum1 = 0; int sum2 = 0; for (int i = 0; i < n; i++) { sum1 += a[i]; sum2 += a[i]; if (i % 2 == 0) { if (sum1 > 0) { } else { ans1 += abs(sum1) + 1; sum1 = 1; } if (sum2 < 0) { } else { ans2 += abs(sum2) + 1; sum2 = -1; } } else { if (sum1 < 0) { } else { ans1 += abs(sum1) + 1; sum1 = 1; } if (sum2 > 0) { } else { ans2 += abs(sum2) + 1; sum2 = -1; } } } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); StringTokenizer str = new StringTokenizer(br.readLine(), " "); long[] A = new long[n]; for(int i = 0; i < n ; i++) { A[i] = Integer.parseInt(str.nextToken()); } long c = func(A,n); System.out.println(c); } catch (IOException e) { System.out.println("error"); } } static long func(long[] A,int n){ long sum = 0; long c1 = 0,c2 = 0; for(int i = 0; i < n; i++){ if(i%2==0 && sum+A[i]>=0){ //次は負 c1 += (sum+A[i] + 1); sum = -1; } else if(i%2==1 && sum+A[i]<=0){ c1 += 1+(-sum-A[i]); sum = 1; } else{ sum+=A[i]; } } sum = 0; for(int i = 0; i < n; i++){ if(i%2==0 && sum+A[i]<=0){ //次は負 c2 += (sum+A[i] + 1); sum = 1; } else if(i%2==1 && sum+A[i]>=0){ c2 += 1+(-sum-A[i]); sum = -1; } else{ sum+=A[i]; } } return Math.min(c1,c2); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <class T> int former(const vector<T> &v, T x) { return upper_bound(v.begin(), v.end(), x) - v.begin() - 1; } template <class T> int latter(const vector<T> &v, T x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } const long long LLINF = 1LL << 60; const int INTINF = 1 << 30; const int MAX = 510000; const int MOD = 1000000007; 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; } struct UnionFind { vector<long long> par; UnionFind(long long n) : par(n, -1) {} void init(long long n) { par.assign(n, -1); } long long root(long long x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(long long x, long long y) { return root(x) == root(y); } bool merge(long long x, long long 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; } long long size(long long x) { return -par[root(x)]; } }; template <typename T> vector<T> dijkstra(int s, vector<vector<pair<int, T> > > &G) { const T INF = numeric_limits<T>::max(); using P = pair<T, int>; int n = G.size(); vector<T> d(n, INF); vector<int> b(n, -1); priority_queue<P, vector<P>, greater<P> > q; d[s] = 0; q.emplace(d[s], s); while (!q.empty()) { P p = q.top(); q.pop(); int v = p.second; if (d[v] < p.first) continue; for (auto &e : G[v]) { int u = e.first; T c = e.second; if (d[u] > d[v] + c) { d[u] = d[v] + c; b[u] = v; q.emplace(d[u], u); } } } return d; } vector<vector<int> > bfs(vector<string> &s, int sy, int sx, char wall, int dir) { int h = s.size(), w = s.front().size(); vector<vector<int> > dp(h, vector<int>(w, -1)); using P = pair<int, int>; queue<P> q; dp[sy][sx] = 0; q.emplace(sy, sx); int dy[] = {1, -1, 0, 0, 1, 1, -1, -1}; int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; auto in = [&](int y, int x) { return 0 <= y && y < h && 0 <= x && x < w; }; while (!q.empty()) { int y, x; tie(y, x) = q.front(); q.pop(); for (int k = 0; k < dir; k++) { int ny = y + dy[k], nx = x + dx[k]; if (!in(ny, nx) || s[ny][nx] == wall) continue; if (~dp[ny][nx]) continue; dp[ny][nx] = dp[y][x] + 1; q.emplace(ny, nx); } } return dp; } int64_t power(int64_t x, int64_t n, int64_t mod) { int64_t ret = 1; while (n > 0) { if (n & 1) (ret *= x) %= mod; (x *= x) %= mod; n >>= 1; } return ret; } vector<int> sieve_of_eratosthenes(int n) { vector<int> primes(n); for (int i = 2; i < n; ++i) primes[i] = i; for (int i = 2; i * i < n; ++i) if (primes[i]) for (int j = i * i; j < n; j += i) primes[j] = 0; return primes; } std::vector<long long> divisor(long long n) { std::vector<long long> ret; for (long long i = 1; i * i <= n; ++i) { if (n % i == 0) { ret.push_back(i); if (i != 1 && i * i != n) { ret.push_back(n / i); } } } return ret; } const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; int main(void) { long long n; cin >> n; vector<long long> a(n); for (long long i = 0, i_len = (n); i < i_len; ++i) cin >> a[i]; long long sum = 0; long long count = 0; long long ans = 0; for (long long i = 0, i_len = (n); i < i_len; ++i) { sum += a[i]; if (i % 2 == 0) if (sum < 0) { ans += 1 - sum; sum += ans; } if (i % 2 != 0) if (sum > 0) { ans += sum + 1; sum -= ans; } } sum = 0; for (long long i = 0, i_len = (n); i < i_len; ++i) { sum += a[i]; if (i % 2 == 0) if (sum > 0) { count += sum + 1; sum -= count; } if (i % 2 != 0) if (sum < 0) { count += 1 - sum; sum += count; } } chmin(ans, count); 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() { long long n, a[100010] = {}; cin >> n; long long odd = 0, even = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; if (i % 2 == 0) even += a[i]; else odd += a[i]; } if (odd > even) { odd = 1; even = -1; } else { odd = -1; even = 1; } long long sum = 0, ans = 0; for (int i = 0; i < n; ++i) { sum += a[i]; if (i % 2 == 0) { if (sum * even > 0) { continue; } else { ans += 1 - sum * even; sum = even; } } else { if (sum * odd > 0) { continue; } else { ans += 1 - sum * odd; sum = odd; } } } cout << ans << endl; return 0; }