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; const long long MOD = 1e9 + 7; template <typename T> void putv(vector<T>& V) { for (auto x : V) cout << x << " "; cout << endl; } template <class T> vector<T> getv(long long n) { vector<T> Vector_temp; for (int(i) = 0; (i) < (n); (i)++) { T input; cin >> input; Vector_temp.emplace_back(input); } return Vector_temp; } long long gcd(long long c, long long b) { while (1) { if (c % b != 0) { long long tmp = b; b = c % b; c = tmp; } else { return b; } } } int main() { long long n; cin >> n; vector<long long> a((n), 0); ; a = getv<long long>(n); long long sum = 0; sum = a[0]; int b = -1; if (sum > 0) { b = 1; } long long ans = 0; for (int(i) = 1; (i) < n; (i)++) { sum += a[i]; if (b > 0) { if (sum >= 0) { ans += sum + 1; cout << (sum + 1) << endl; ; sum = -1; } b = -1; } else { if (sum <= 0) { ans += ((-sum) + 1); cout << ((-sum) + 1) << endl; sum = 1; } b = 1; } } cout << (ans) << endl; ; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int MOD = pow(10, 9) + 7; using namespace std; int in() { int temp; scanf("%d", &temp); return temp; } long long lin() { long long temp; scanf("%lld", &temp); return temp; } int main() { int N = in(); vector<int> vec; vector<int> csum; vec.push_back(in()); csum.push_back(vec.back()); for (auto i = 1; i < N; i++) { vec.push_back(in()); csum.push_back(csum.back() + vec.back()); } int cumOper = 0; int sign = 1; int count = 0; int signi; for (auto i = 0; i < N; i++) { if ((csum[i] + cumOper) == 0) { count++; cumOper -= sign; } signi = (csum[i] + cumOper) / abs(csum[i] + cumOper); if (signi != sign) { sign = signi; continue; } if (sign == 1) { count += (csum[i] + cumOper + 1); cumOper += (-1) * (csum[i] + cumOper + 1); } else { count += (-(csum[i] + cumOper) + 1); cumOper += (1) * (-(csum[i] + cumOper) + 1); } sign = -sign; } int temp = count; sign = -1; count = 0; signi; for (auto i = 0; i < N; i++) { if ((csum[i] + cumOper) == 0) { count++; cumOper -= sign; } signi = (csum[i] + cumOper) / abs(csum[i] + cumOper); if (signi != sign) { sign = signi; continue; } if (sign == 1) { count += (csum[i] + cumOper + 1); cumOper += (-1) * (csum[i] + cumOper + 1); } else { count += (-(csum[i] + cumOper) + 1); cumOper += (1) * (-(csum[i] + cumOper) + 1); } sign = -sign; } cout << min(count, temp) << 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); for (int i = 0; i < N; i++) { cin >> data[i]; } int count_odd, count_even; count_odd = 0; count_even = 0; for (int i = 0; i < N; i++) { if (i % 2 == 0) { if (data[i] >= 0) { count_odd += (data[i] + 1); } } else { if (data[i] <= 0) { count_odd -= (data[i] - 1); } } } for (int i = 0; i < N; i++) { if (i % 2 == 0) { if (data[i] <= 0) { count_even -= (data[i] - 1); } } else { if (data[i] >= 0) { count_even += (data[i] + 1); } } } cout << min(count_even, count_odd) << 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 argc, const char* argv[]) { int n; cin >> n; int a[100010]; for (int i = 0; i < n; ++i) cin >> a[i]; long long int res = 0; bool plus = false; long long int sum = a[0]; if (a[0] > 0) plus = true; else if (a[0] < 0) plus = false; int j = 1; while (sum == 0) { if (a[j] > 0) { ++res; sum = (j % 2 == 0) ? 1 : -1; plus = (j % 2 == 0) ? true : false; } else if (a[j] < 0) { ++res; sum = (j % 2 == 0) ? -1 : 1; plus = (j % 2 == 0) ? false : true; } ++j; if (j == n) { cout << 1 + 2 * (n - 1) << endl; return 0; } } for (int i = 0; i < n - 1; ++i) { if (sum + a[i + 1] > 0) { if (plus == true) { res += sum + a[i + 1] + 1; sum = -1; plus = false; } else { sum += a[i + 1]; plus = true; } } else if (sum + a[i + 1] < 0) { if (plus == false) { res += -(sum + a[i + 1] - 1); sum = 1; plus = true; } else { sum += a[i + 1]; plus = false; } } else if (sum + a[i + 1] == 0) { if (plus == true) { ++res; sum = -1; plus = false; } else { ++res; sum = 1; plus = true; } } cout << "sum : " << sum << " a[i] : " << a[i] << " ans : " << res << endl; ; } 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
python3
import functools n=int(input()) a=list(map(int,input().split())) ans=0 res=0#0が正、1が負 if a[0]<0: res=1 for i in range(n-1): aa=functools.reduce(lambda x,y:x+y,a[:i+2]) if aa<0: if res==1: adj = 1-aa a[i+1]=a[i+1]+adj ans=ans+abs(adj) res = 0 else: res=1 if aa>0: if res==0: adj = -1-aa a[i+1]=a[i+1]+adj ans=ans+abs(adj) res = 1 else: res=0 if aa==0: if res == 1: a[i+1]=a[i+1]+1 ans+=1 res=0 else: a[i+1]=a[i+1]-1 ans+=1 res=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; long long gcd(long long x, long long y) { long long tmp = 0; if (x < y) { tmp = x; x = y; y = tmp; } while (y > 0) { long long r = x % y; x = y; y = r; } return x; } long long lcm(long long x, long long y) { return x / gcd(x, y) * y; } long long kaijo(long long k) { long long sum = 1; for (long long i = 1; i <= k; ++i) { sum *= i; sum %= 1000000000 + 7; } return sum; } int main() { int n; cin >> n; int a[n]; long long k = 0; cin >> a[0]; long long sum = a[0]; for (int i = 1; i < n; i++) { cin >> a[i]; if (sum * (sum + a[i]) >= 0) { long long tmp; if (sum < 0) { tmp = (sum - 1) * (-1); k += tmp - a[i]; sum += tmp; } else { tmp = (sum + 1) * (-1); k += a[i] - tmp; sum += tmp; } } else { sum += a[i]; } } cout << k << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } long long s1, a1 = 0, s2, a2 = 0; if (a[0] > 0) { s1 = a[0]; s2 = -1; a2 = a[0] + 1; } else if (a[0] < 0) { s1 = 1; s2 = a[0]; a1 = 1 - a[0]; } else { s1 = 1; s2 = -1; a1 = 1; a2 = 1; } for (int i = 1; i < n; ++i) { if (s1 > 0) { if (s1 + a[i] >= 0) { a1 += s1 + a[i] + 1; s1 = -1; } else s1 += a[i]; } else { if (s1 + a[i] <= 0) { a1 = 1 - a1 - a[i]; s1 = 1; } else s1 += a[i]; } } for (int i = 1; i < n; ++i) { if (s2 > 0) { if (s2 + a[i] >= 0) { a2 += s2 + a[i] + 1; s2 = -1; } else s2 += a[i]; } else { if (s2 + a[i] <= 0) { a2 = 1 - a2 - a[i]; s2 = 1; } else s2 += a[i]; } } cout << min(a1, a2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
object Main extends App { import scala.math._ val n = readInt val a = readLine.split(" ").map{_.toInt} //start != 0 def solve(start:Int):Long = { var ans:Long = 0 var sum:Long = start.toLong for (i <- 1 until n) { val presum = sum sum += a(i) if (sum == 0) { ans += 1 sum = -presum/abs(presum) } else { val check = (presum/abs(presum))*(sum/abs(sum)) if (check > 0) { ans += abs(sum)+1 sum = -sum/abs(sum) } } } ans } if (a(0) == 0) println(min(solve(1),solve(-1))+1) else println(solve(a(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
# # Written by NoKnowledgeGG @YlePhan # ('ω') # #import math #mod = 10**9+7 #import itertools #import fractions #import numpy as np #mod = 10**4 + 7 """def kiri(n,m): r_ = n / m if (r_ - (n // m)) > 0: return (n//m) + 1 else: return (n//m)""" """ n! mod m 階乗 mod = 1e9 + 7 N = 10000000 fac = [0] * N def ini(): fac[0] = 1 % mod for i in range(1,N): fac[i] = fac[i-1] * i % mod""" """mod = 1e9+7 N = 10000000 pw = [0] * N def ini(c): pw[0] = 1 % mod for i in range(1,N): pw[i] = pw[i-1] * c % mod""" """ def YEILD(): yield 'one' yield 'two' yield 'three' generator = YEILD() print(next(generator)) print(next(generator)) print(next(generator)) """ """def gcd_(a,b): if b == 0:#結局はc,0の最大公約数はcなのに return a return gcd_(a,a % b) # a = p * b + q""" """def extgcd(a,b,x,y): d = a if b!=0: d = extgcd(b,a%b,y,x) y -= (a//b) * x print(x,y) else: x = 1 y = 0 return d""" def readInts(): return list(map(int,input().split())) mod = 10**9 + 7 def main(): n = int(input()) A = readInts() # 符号 positive? #po_ = True # 変わったか変わってないか if A[0] >= 0: # if positive po_ = True else: # negative po_ = False Cost = 0 ANS = [0] * (n+1) ANS[0] = A[0] for i in range(1,n): #print(ANS[i-1],po_,ANS[i-1] + A[i],Cost) if ANS[i-1]+A[i] > 0 and not po_: # sumがpositiveで前がnegativeだった po_ = True ANS[i] = ANS[i-1] + A[i] # これで終わり elif ANS[i-1]+A[i] > 0 and po_: # posi : posi ? # 負にしなければならない Cost += abs(-1 - (ANS[i-1]+A[i])) # 先にこれやれ A[i] += -1 - (ANS[i-1] + A[i]) # -4 ANS[i] = ANS[i-1] + A[i] po_ = False elif ANS[i-1]+A[i] < 0 and not po_: #nega : nega # -1 はここ # print(A[i]) Cost += abs(1 - (ANS[i-1]+A[i])) # 先にこれやれ A[i] += 1 - (ANS[i-1] + A[i]) ANS[i] = ANS[i-1] + A[i] po_ = True elif ANS[i-1]+A[i] == 0 and po_: # nega: pos po_ = False A[i] += 1 Cost += 1 ANS[i] = ANS[i-1] + A[i] elif ANS[i-1]+A[i] < 0 and po_: po_ = False ANS[i] = ANS[i-1] + A[i] print(Cost) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def sequence(lis, first_value): count = 0 now_sum = first_value pre_sum = first_value for num in lis[1:]: now_sum += num while now_sum == 0 or now_sum * pre_sum > 0: now_sum -= pre_sum/abs(pre_sum) count += 1 pre_sum = now_sum return count n = int(input()) a = [int(i) for i in input().split()] if a[0] == 0: print(min(sequence(a, 1), sequence(a, -1))+1) else: print(sequence(a, a[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
from functools import reduce N =int(input()) A = list(map(int, input().split())) def check(A): arr1 = [0]*N ans1 = 0 for i in range(len(A)): if i ==0: arr1[0] = A[0] else: arr1[i] += arr1[i-1] + A[i] if arr1[i]*arr1[i-1] >0: if arr1[i-1] >=0: #-に arr1[i] = -1 elif arr1[i-1] <=0: # 無理やり+にする. arr1[i] = 1 ans1 += abs(A[i])+1 ans2 = 0 arr2 = [0]*N #change head. for i in range(len(A)): if i ==0: if A[0]>=0: arr2[0] = -1 elif A[0]<=0: arr2[0] = 1 ans2 += abs(A[0])+1 else: arr2[i] += arr2[i-1] + A[i] if arr2[i]*arr2[i-1] >0: if arr2[i-1]>=0: arr2[i] = -1 elif arr2[i-1]<=0: arr2[i] = 1 ans2 += abs(A[i])+1 return min(ans1,ans2) print(check(A))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int, input().split())) currentSum = 0 count1 = 0 count2 = 0 for i in range(N): restSum = currentSum currentSum += A[i] if currentSum <= 0 and restSum < 0: count1 += abs(currentSum) + 1 currentSum = 1 elif currentSum >= 0 and restSum > 0: count1 += abs(currentSum) + 1 currentSum = -1 elif currentSum == 0 and restSum == 0: count1 += 1 currentSum = -1 currentSum = 0 for i in range(N): restSum = currentSum currentSum += A[i] if currentSum <= 0 and restSum < 0: count2 += abs(currentSum) + 1 currentSum = 1 elif currentSum >= 0 and restSum > 0: count2 += abs(currentSum) + 1 currentSum = -1 elif currentSum == 0 and restSum == 0: count2 += 1 currentSum = 1 print(min(count1, count2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = [int(i) for i in input().split()] temp = a[0] ans = 0 for i in range(1, n): if temp*(temp + a[i]) < 0: temp += a[i] else: if temp > 0: ans += (temp + a[i] + 1) temp = -1 elif temp < 0: ans += abs(temp + a[i]) + 1 temp = 1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) import numpy as np ans = 0 sum0 = a[0] sum1 = a[0] for i in range(1, n): sum1 += a[i] if np.sign(sum0) != np.sign(sum1) and sum1 != 0: #合計の符号が逆となっており、0でない sum0 = sum1 pass elif sum1 == 0: #合計が0になった場合は、符号が逆になるよう1か-1を足す sum1 -= 1 * np.sign(sum0) ans += 1 sum0 = sum1 elif np.sign(sum0) == np.sign(sum1): #符号が同じ場合は、+1か-1になるまで足す if np.sign(sum1) == 1: while sum1 >= 0: sum1 -= 1 ans += 1 else: while sum1 <= 0: sum1 += 1 ans += 1 sum0 = sum1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) a1 = [a[0]] * n b = a[0] ans = 0 def f(x): if x == 0: return 0 else: return x // abs(x) for i in range(1, n): if a1[i - 1] * a[i] >= 0: a1[i] = -a[i] else: a1[i] = a[i] if b * (b + a1[i]) >= 0: a1[i] = -f(a1[i - 1]) - b if b + a1[i] == 0: a1[i] += f(a1[i]) ans += abs(a1[i] - a[i]) b += a1[i] a2 = [0] * n ans1 = abs(-f(a2[0]) - a2[0]) a2[0] = -f(a[0]) b1 = a2[0] for i in range(1, n): if a2[i - 1] * a[i] >= 0: a2[i] = -a[i] else: a2[i] = a[i] if b * (b + a2[i]) >= 0: a2[i] = -f(a2[i - 1]) - b1 if b1 + a2[i] == 0: a2[i] += f(a2[i]) ans1 += abs(a2[i] - a[i]) b1 += a2[i] 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
python3
import numpy as np n = int(input()) a = list(map(int, input().split())) c1 = 0 a0 = a[0] if a0 == 0: a[0] = 1 c1 += 1 sum = a[0] for i in range(1, n): if np.sign(sum) == -np.sign(sum + a[i]): sum += a[i] else: if sum > 0: c1 += sum + a[i] + 1 sum = -1 else: c1 += -sum - a[i] + 1 sum = 1 c2 = 0 if a0 == 0: a[0] = -1 c2 += 1 else: c2 += a[0] + 1 if a[0] > 0: a[0] = -1 else: a[0] = 1 sum = a[0] for i in range(1, n): if np.sign(sum) == -np.sign(sum + a[i]): sum += a[i] else: if sum > 0: c2 += sum + a[i] + 1 sum = -1 else: c2 += -sum - a[i] + 1 sum = 1 print(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 <typename T> void chmin(T &a, T b) { if (a > b) a = b; } template <typename T> void chmax(T &a, T b) { if (a < b) a = b; } int in() { int x; scanf("%d", &x); return x; } long long lin() { long long x; scanf("%lld", &x); return x; } int main() { int n; cin >> n; vector<long long> a(n); long long ans = 0, ans1 = 0, ans2 = 0, acum = 0; for (int i = 0; i < n; ++i) cin >> a[i]; acum = a[0]; if (a[0] <= 0) { acum = 1; ans1 += 1 - a[0]; } for (int i = 1; i <= n - 1; ++i) { if (i % 2 == 0) { if (acum + a[i] > 0) { acum += a[i]; } else { ans1 += 1 - (acum + a[i]); acum = 1; } } else { if (acum + a[i] < 0) { acum += a[i]; } else { ans1 += 1 + (acum + a[i]); acum = -1; } } } acum = a[0]; if (a[0] >= 0) { acum = -1; ans2 += -1 - a[0]; } for (int i = 1; i <= n - 1; ++i) { if (i % 2 == 1) { if (acum + a[i] > 0) { acum += a[i]; } else { ans2 += 1 - (acum + a[i]); acum = 1; } } else { if (acum + a[i] < 0) { acum += a[i]; } else { ans2 += 1 + (acum + a[i]); acum = -1; } } } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(NULL); ios::sync_with_stdio(0); int n; cin >> n; int arr[100010], pos = -1; long suma = 0, sumaAux = -1; memset(arr, 0, sizeof arr); for (int i = 0; i < n; i++) { long aux; cin >> aux; arr[i] = aux; if (suma < 0) { suma += aux; if (suma < 0) { if (pos == -1) { pos = i; sumaAux = suma; sumaAux -= aux; } } } else if (suma > 0) { suma += aux; if (suma > 0) { if (pos == -1) { pos = i; sumaAux = suma; sumaAux -= aux; } } } if (suma == 0 && pos == -1 && i != 0) { pos = i; sumaAux = 0; } if (i == 0) { suma += aux; } } if (pos == -1) { cout << 0 << '\n'; return 0; } long cont = 0; for (int i = pos; i < n; i++) { if (sumaAux < 0) { long valAux = abs(sumaAux) + 1; cont += valAux - arr[i]; sumaAux = 1; } else if (sumaAux > 0) { long valAux = -1 - sumaAux; cont += abs(valAux - arr[i]); sumaAux = -1; } } cout << cont << '\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; const int mod = 1000000007; const long long INF = 1000000000000000000; const int MOD = 998244353; long long A[200010]; int main() { int N; cin >> N; for (int i = 0; i < N; i++) cin >> A[i]; long long ans = mod, cnt = 0; int turn = 1; long long num = 0; for (int i = 0; i < N; i++) { num += A[i]; if (num * turn <= 0) { cnt += abs(num - turn); num = turn; } turn *= -1; } ans = min(ans, cnt); num = 0; cnt = 0; turn = -1; for (int i = 0; i < N; i++) { num += A[i]; if (num * turn <= 0) { cnt += abs(num - turn); num = turn; } turn *= -1; } cout << min(ans, cnt) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) i = 0 sum_before = 0 sum_after = 0 count = 0 while i < n: sum_after = sum_before + a[i] if sum_after * sum_before > 0 or sum_after == 0: count += 1 if sum_after < 0: a[i] += 1 elif sum_after > 0: a[i] -= 1 elif sum_before < 0: a[i] += 1 else: a[i] -= 1 else: i += 1 sum_before = sum_after print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; const long long INF = -10000000000; long long maxx(long long x, long long y, long long z) { return max(max(x, y), z); } long long minn(long long x, long long y, long long z) { return min(min(x, y), z); } long long gcd(long long x, long long y) { if (x % y == 0) return y; else return gcd(y, x % y); } long long lcm(long long x, long long y) { return x * (y / gcd(x, y)); } int main() { long long N; cin >> N; vector<long long> A(N); for (long long i = 0; i < N; i++) cin >> A[i]; long long cnt = 0, ans; long long sum = A[0]; for (long long i = 1; i <= N - 1; i++) { if (abs(sum * 2 + A[i]) < abs(sum) + abs(sum + A[i])) sum += A[i]; else { if (sum < 0) { cnt += abs((-1) * (sum - 1 + A[i])); sum = 1; } else { cnt += abs((-1) * (sum + 1 + A[i])); sum = -1; } } } ans = cnt; cnt = 2 * abs(A[0]); sum = (-1) * A[0]; for (long long i = 1; i <= N - 1; i++) { if (abs(sum * 2 + A[i]) < abs(sum) + abs(sum + A[i])) sum += A[i]; else { if (sum < 0) { cnt += abs((-1) * (sum - 1 + A[i])); sum = 1; } else { cnt += abs((-1) * (sum + 1 + A[i])); sum = -1; } } } cout << min(ans, cnt); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using itn = int; using ld = long double; template <class T> using vec = vector<T>; int main() { int n; cin >> n; vec<int> a(n); for (int i = 0; i < (int)(n); i++) cin >> a[i]; int ans = 0; vec<int> s(n, 0); int ii = 0; while (true) { s[ii] = a[ii]; if (s[ii] != 0) break; ++ii; } for (int i = ((int)(ii + 1)); i < ((int)(n)); i++) { s[i] = a[i] + s[i - 1]; if (s[i] * s[i - 1] > 0) { if (s[i] < 0) { ans += 1 - s[i]; s[i] = 1; } else if (s[i] > 0) { ans += s[i] + 1; s[i] = -1; } } else if (s[i] == 0) { if (s[i - 1] < 0) { ++ans; s[i] = 1; } else if (s[i - 1] > 0) { ++ans; s[i] = -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
python3
import copy import sys input = sys.stdin.readline N = int(input()) a = list(map(int, input().split())) ans1, ans2 = 0, 0 f = a[0] if f == 0: f = 1 ans1 += 1 for i in range(1, N): if f * (f + a[i]) < 0: f += a[i] continue ans1 += abs(f + a[i]) + 1 if f > 0: f = -1 else: f = 1 f = -a[0] if f == 0: f = -1 ans2 += 1 for i in range(1, N): if f * (f + a[i]) < 0: f += a[i] continue ans2 += abs(f + a[i]) + 1 if f > 0: f = -1 else: f = 1 print(min(ans1, ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) count0 = 0 memo = 0 for i in range(n): if i % 2 == 0: if a[i] <= 0: count0 += 1 - a[i] memo = 1 - a[i] else: if a[i] >= 0: count0 += a[i] + 1 memo = -(a[i] + 1) if i + 1 < n: a[i+1] = a[i+1] + a[i] + memo count1 = 0 memo = 0 for i in range(n): if i % 2 == 0: if a[i] >= 0: count1 += a[i] + 1 memo = -(a[i] + 1) else: if a[i] <= 0: count1 += 1 - a[i] memo = 1 - a[i] if i + 1 < n: a[i+1] = a[i+1] + a[i] + memo print(min(count0, count1))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define leading zero str.erase(0, min(str.find_first_not_of('0'), str.size()-1)); using namespace __gnu_pbds; using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; string text="abcdefghijklmnopqrstuvwxyz"; const int maxn=1e6+7; // .--------------. // | Try First One| // '--------------' // | .--------------. // | | | // V V | // .--------------. | // | AC. |<---. | // '--------------' | | // (True)| |(False) | | // .--------' | | | // | V | | // | .--------------. | | // | | Try Again |----' | // | '--------------' | // | | // | .--------------. | // '->| Try Next One |-------' // '--------------' ll bin_pow(ll a,ll b,ll m) { ll res=1; a%=m; while(b>0) { if(b&1) res=res*a%m; b>>=1; a=a*a%m; } return res; } bool miller_rabin(ll d,ll n) { ll a=2+rand()%(n-4); ll x=bin_pow(a,d,n); if(x==1 || x==n-1) return true; while(d!=n-1) { x=(x*x)%n; d*=2; if(x==1) return false; if(x==n-1) return true; } return false; } bool prime(ll n,ll k) { if(n==1 || n==4) return false; if(n<=3) return true; ll d=n-1; while(d%2==0) d/=2; for(int i=0; i<k; i++) { if(!miller_rabin(d,n)) return false; } return true; } int n; string s; ll ans = 0; void solve(ll x,ll y){ if(x==n+1){ ans += y; return; } ll cur = 0; for(ll i=x;i<=n;i++){ cur = (10*cur) + (s[i]-'0'); solve(i+1,y+cur); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll n; cin>>n; ll a[n+2]; for(int i=0;i<n;i++)cin>>a[i]; ll sum=0; ll cnt=0; ll ans=1<<18; for(int i=0;i<n;i++){ sum+=a[i]; if(i%2==0){ if(sum<=0){ cnt+=abs(sum)+1; sum=1; } } else{ if(sum>=0){ cnt+=abs(sum)+1; sum=-1; } } } ans=min(cnt,ans); //cout<<ans<<endl; sum=0; cnt=0; for(int i=0;i<n;i++){ sum+=a[i]; if(i%2==0){ if(sum>=0){ cnt+=abs(sum)+1; sum=-1; } } else{ if(sum<=0){ cnt+=abs(sum)+1; sum=1; } } } cout<<min(ans,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; int count1 = 0; int count2 = 0; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; long long sum = 0; if (a[0] >= 0) { for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 1) { while (sum >= 0) { sum--; count1++; } } else { while (sum <= 0) { sum++; count1++; } } } } else { for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { while (sum >= 0) { sum--; count1++; } } else { while (sum <= 0) { sum++; count1++; } } } } if (a[0] >= 0) { while (a[0] >= 0) { a[0]--; count2++; } } else { while (a[0] <= 0) { a[0]++; count2++; } } sum = a[0]; if (a[0] >= 0) { for (int i = 1; i < n; i++) { sum += a[i]; if (i % 2 == 1) { while (sum >= 0) { sum--; count2++; } } else { while (sum <= 0) { sum++; count2++; } } } } else { for (int i = 1; i < n; i++) { sum += a[i]; if (i % 2 == 0) { while (sum >= 0) { sum--; count2++; } } else { while (sum <= 0) { sum++; count2++; } } } } cout << count1 << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long LINF = 1LL << 60; const int INF = 1 << 30; int main() { long long n, l; cin >> n; vector<long long> a(n); cin >> a[0]; for (long long i = 1; i < n; i++) { cin >> l; a[i] = a[i - 1] + l; } long long tmp = 0; long long count = 0; for (long long i = 0; i < n - 1; i++) { if (i == 0 && a[i] != 1) { count += abs(a[i] - 1); tmp -= a[i] - 1; } if ((a[i] + tmp) * (a[i + 1] + tmp) >= 0) { if ((a[i + 1] + tmp) <= 0) { count += abs(a[i + 1] + tmp - 1); tmp -= (a[i + 1] + tmp - 1); } else { count += abs(a[i] + tmp + 1); tmp -= (a[i] + tmp + 1); } } } if (a[n - 1] + tmp == 0) { count++; } long long ans = count; count = 0; long long tmp2 = 0; for (long long i = 0; i < n - 1; i++) { if (i == 0 && a[i] != -1) { count += abs(a[i] + 1); tmp2 -= a[i] + 1; } if ((a[i] + tmp2) * (a[i + 1] + tmp2) >= 0) { if ((a[i + 1] + tmp2) <= 0) { count += abs(a[i + 1] + tmp2 - 1); tmp2 -= (a[i + 1] + tmp2 - 1); } else { count += abs(a[i] + tmp2 + 1); tmp2 -= (a[i] + tmp2 + 1); } } } if (a[n - 1] + tmp2 == 0) { count++; } cout << min(ans, count) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) ans = 0 temp=a[0] for i in range(n - 1): if temp > 0: temp+=a[i+1] if temp < 0: pass else: ans += (temp + 1) temp -= (temp+1) else: temp+=a[i+1] if temp > 0: pass else: ans += (temp + 1) temp += (temp + 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; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long sum = a[0]; long long ans = 0; for (int i = 1; i < n; i++) { long long 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; } sum = -1 * a[0] / abs(a[0]); long long ans2 = a[0] + 1; for (int i = 1; i < n; i++) { long long sum2 = sum + a[i]; if (sum > 0 && sum2 > 0) { ans2 += sum2 + 1; sum = -1; } else if (sum < 0 && sum2 < 0) { ans2 += -sum2 + 1; sum = 1; } else if (sum2 == 0) { ans2++; if (sum < 0) sum = 1; else sum = -1; } else sum = sum2; } cout << 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> 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[2] = {0}; for (int k = 0; k < 2; k++) { long long sum = ai[0]; if (k == 0) { if (sum <= 0) { ans[k] += (tp_abs(sum) + 1); sum = 1; } } else { if (sum >= 0) { ans[k] += (tp_abs(sum) + 1); sum = -1; } } for (int i = 1; i < N; i++) { int bfrSign = (sum > 0) ? 1 : -1; sum += ai[i]; if ((bfrSign > 0) && (sum >= 0)) { ans[k] += (tp_abs(sum) + 1); sum = -1; } else if ((bfrSign < 0) && (sum <= 0)) { ans[k] += (tp_abs(sum) + 1); sum = 1; } } } printf("%lld\n", std::max(ans[0], ans[1])); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INFF = 0x3f3f3f3f3f3f3f3f; long long a[1000010]; int n; unsigned long long solve() { unsigned long long sum = 0; long long oo = 0, flag; if (a[0] > 0) flag = -1; else if (a[0] < 0) flag = 1; for (int i = 0; i < n; i++) { oo += a[i]; if (flag == 1) { if (oo >= 0) { sum += oo + 1; oo = -1; } } if (flag == -1) { if (oo <= 0) { sum += 0 - oo + 1; oo = 1; } } flag = -flag; } return sum; } int main() { while (scanf("%d", &n) != EOF) { unsigned long long sum = INFF; for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); } if (a[0] == 0) { a[0] = 1; unsigned long long sum1 = solve(); a[0] = -1; unsigned long long sum2 = solve(); sum = min(sum, min(sum1, sum2)) + 1; } else { a[0] = 1; unsigned long long sum1 = solve() + abs(a[0] - 1); a[0] = -1; unsigned long long sum2 = solve() + abs(a[0] + 1); sum = min(sum, min(sum1, sum2)); } printf("%lld\n", sum); } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> int main(void) { int n, ans = 0; scanf("%d", &n); int a[n], sum[n]; scanf("%d", &a[0]); sum[0] = a[0]; for (int i = 1; i < n; i++) { scanf("%d", &a[i]); sum[i] = a[i] + sum[i - 1]; if (sum[i - 1] < 0) { if (sum[i] == 0) { sum[i]++; ans++; } else if (sum[i] < 0) { ans += (abs(sum[i]) + 1); sum[i] += (abs(sum[i]) + 1); } } else { if (sum[i] == 0) { sum[i]--; ans++; } else if (sum[i] > 0) { ans += (abs(sum[i]) + 1); sum[i] -= (abs(sum[i]) + 1); } } } printf("%d\n", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1e15; signed main() { long long N; cin >> N; long long pcost = 0; long long mcost = 0; vector<long long> vec(N); for (long long i = (0); i < (N); ++i) cin >> vec[i]; vector<long long> S(N, 0); vector<long long> Sc(N, 0); S[0] = vec[0]; for (long long i = (0); i < (N - 1); ++i) S[i + 1] = S[i] + vec[i + 1]; Sc = S; for (long long i = (0); i < (N); ++i) { if (i % 2 == 0 && S[i] <= 0) { long long d = 1 - S[i]; pcost += d; for (long long j = i; j <= N - 1; j++) { S[j] += d; } continue; } if (i % 2 == 1 && S[i] >= 0) { long long d = S[i] + 1; pcost += d; for (long long j = i; j <= N - 1; j++) { S[j] -= d; } continue; } if (i % 2 == 0 && Sc[i] >= 0) { long long d = 1 + Sc[i]; mcost += d; for (long long j = i; j <= N - 1; j++) { Sc[j] -= d; } continue; } if (i % 2 == 1 && Sc[i] <= 0) { long long d = 1 - Sc[i]; mcost += d; for (long long j = i; j <= N - 1; j++) { Sc[j] += d; } continue; } } cout << min(mcost, pcost) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int ans; int t; cin >> t; int sum = t; for (int i = 0; i < n - 1; i++) { cin >> t; if (sum == 0) { if (t > 0) { ans++; sum -= 1; } else { ans++; sum += 1; } } if (sum > 0) { if (sum + t > 0) { ans += (sum + t + 1); t -= (sum + t + 1); } else if (sum + t == 0) { ans++; t -= 1; } } else if (sum < 0) { if (sum + t < 0) { ans += abs(sum + t + 1); t += abs(sum + t + 1); } else if (sum + t == 0) { ans++; t += 1; } } sum += t; } 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; struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); }; } aaaaaaa; int MOD = 1e9 + 7; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int lcm(int a, int b) { return (a * b) / gcd(a, b); } int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int N; int main() { cin >> N; vector<int> a(N); for (int i = 0; i < (N); ++i) cin >> a[i]; long ans = 0, sum = 0; if (a[0] != 0) sum = a[0]; else if (a[1] > 0) sum = -1, ans++; else sum = 1, ans++; for (int i = 1; i <= (N - 1); ++i) { if ((sum + a[i]) * sum >= 0) { int k = a[i]; if (sum > 0) a[i] = -sum - 1; else a[i] = -sum + 1; ans += abs(k - a[i]); } sum += a[i]; } cout << ans << '\n'; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(int argc, const char* argv[]) { int n; cin >> n; int a[100010]; for (int i = 0; i < n; ++i) cin >> a[i]; long long int res = 0; bool plus = false; long long int sum = a[0]; if (a[0] > 0) plus = true; else if (a[0] < 0) plus = false; int j = 1; while (sum == 0) { if (a[j] > 0) { ++res; sum = (j % 2 == 0) ? 1 : -1; plus = (j % 2 == 0) ? true : false; } else if (a[j] < 0) { ++res; sum = (j % 2 == 0) ? -1 : 1; plus = (j % 2 == 0) ? false : true; } ++j; if (j == n) { cout << 1 + 2 * (n - 1) << endl; return 0; } } for (int i = 0; i < n - 1; ++i) { if (sum + a[i + 1] > 0) { if (plus == true) { res += sum + a[i + 1] + 1; sum = -1; plus = false; } else { sum += a[i + 1]; plus = true; } } else if (sum + a[i + 1] < 0) { if (plus == false) { res += -(sum + a[i + 1] - 1); sum = 1; plus = true; } else { sum += a[i + 1]; plus = false; } } else if (sum + a[i + 1] == 0) { if (plus == true) { ++res; sum = -1; plus = false; } else { ++res; sum = 1; plus = true; } } } 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 int n; cin >> n; long long int cnt; long long int x; long long int ans = 0; cin >> cnt; if (cnt == 0) { cnt++; ans++; } for (int i = 1; i < n; i++) { cin >> x; if (cnt < 0) { if (0 < cnt + x) { cnt += x; } else { ans += 1 - (cnt + x); cnt = 1; } } else if (0 < cnt) { if (cnt + x < 0) { cnt += x; } else { ans += (cnt + x) + 1; cnt = -1; } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> void showvector(vector<T> v) { for (T x : v) cout << x << " "; cout << "\n"; } template <typename T> void showvector1(vector<T> v) { long long int n = v.size(); for (long long int i = 1; i <= n - 1; i++) cout << v[i] << "\n"; } template <typename T> void showset(set<T> s) { for (T x : s) cout << x << " "; cout << "\n"; } template <class T> void showvectorpair(vector<T> v) { for (auto it = v.begin(); it != v.end(); it++) cout << it->first << " " << it->second << "\n"; cout << "\n"; } template <typename T, typename P> void showmap(map<T, P> m) { for (auto it = m.begin(); it != m.end(); it++) cout << it->first << " " << it->second << "\n"; cout << "\n"; } template <typename T> bool comp(T a, T b) { return (a > b); } template <class T> bool comppair(T a, T b) { if (a.first == b.first) return (a.second > b.second); return (a.first > b.first); } bool sameparity(long long int a, long long int b) { return (a % 2 == b % 2); } bool difparity(long long int a, long long int b) { return !(a % 2 == b % 2); } bool isprime(long long int x) { if (x <= 1) return false; for (long long int i = 2; i <= sqrt(x); i++) { if (x % i == 0) return false; } return true; } bool iseven(long long int x) { return !(x % 2); } bool isodd(long long int x) { return (x % 2); } void vfun() { long long int n, k; cin >> n; vector<long long int> v(n); for (long long int i = 0; i < n; i++) cin >> v[i]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int test = 1; while (test--) { long long int n; cin >> n; vector<long long int> v(n); for (long long int i = 0; i < n; i++) cin >> v[i]; long long int sum = v[0], psum = v[0], cnt = 0; for (long long int i = 1; i <= n - 1; i++) { sum += v[i]; if (psum > 0) { if (sum >= 0) { cnt += (sum + 1); sum = -1; } } else { if (sum <= 0) { cnt += (abs(sum) + 1); sum = 1; } } psum = sum; } long long int dcnt = abs(v[0]) + 1; if (v[0] > 0) sum = psum = -1; else if (v[0] < 0) sum = psum = 1; for (long long int i = 1; i <= n - 1; i++) { sum += v[i]; if (psum > 0) { if (sum >= 0) { dcnt += (sum + 1); sum = -1; } } else { if (sum <= 0) { dcnt += (abs(sum) + 1); sum = 1; } } psum = sum; } cout << min(dcnt, cnt) << "\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() { long long N; cin >> N; vector<long long> a(N), S(N + 7); for (int i = 0; i < N; i++) { cin >> a[i]; } long long ans = 0; S[0] = a[0]; if (S[0] == 0) { for (int i = 0; i < N; i++) { if (a[i] > 0) { if (i % 2 == 0) { S[0] = 1; ans++; break; } else { S[0] = -1; ans++; break; } } else if (a[i] < 0) { if (i % 2 == 0) { S[0] = -1; ans++; break; } else { S[0] = 1; ans++; break; } } else if (i == N - 1 && a[i] == 0) { ans = (2 * N) - 1; cout << ans << endl; return 0; } } } for (int i = 1; i < N; i++) { S[i] = S[i - 1] + a[i]; } for (int i = 1; i < N; i++) { if (S[i - 1] > 0 && S[i] >= 0) { ans += abs(S[i]) + 1; S[i] = -1; if (i != N - 1) { S[i + 1] = S[i] + a[i + 1]; } } else if (S[i - 1] < 0 && S[i] <= 0) { ans += abs(S[i]) + 1; S[i] = 1; if (i != N - 1) { S[i + 1] = S[i] + a[i + 1]; } } if (i != N - 1) { S[i + 1] = S[i] + a[i + 1]; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 999999999; const int MOD = (int)1e9 + 7; const int EPS = 1e-9; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, a; cin >> n; vector<int> A; for (int i = (0); i < (n); ++i) { cin >> a; A.push_back(a); } int mn = INF; for (int i = (0); i < (2); ++i) { int ans = 0; int sum = A[0]; if (i == 1) { if (sum > 0) { ans += sum + 1; sum = -1; } else { ans += (-sum + 1); sum = 1; } } for (int i = (1); i < (n); ++i) { a = A[i]; if (sum > 0) { sum += a; if (sum >= 0) { ans += (sum + 1); sum = -1; } } else { sum += a; if (sum <= 0) { ans += (-sum + 1); sum = 1; } } } mn = min(mn, ans); } cout << mn << 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())) l = len(a) b = [int(-a[0]/ abs(a[0]))] for i in range(1, l): b.append(a[i]) ans = 0 summary = a[0] if(summary == 0): if(a[1] > 0): summary = -1 ans+= 1 else: summary = 1 ans+= 1 for i in range(1, l): if(summary* (summary+ a[i])>= 0): if(summary > 0): ans+= a[i]+ summary+ 1 a[i] = -summary- 1 summary= -1 else: ans+= -summary+ 1- a[i] a[i] = -summary+ 1 summary= 1 else: summary+= a[i] Ans = 0 Summary = b[0] if(Summary == 0): if(b[1] > 0): Summary = -1 Ans+= 1 else: Summary = 1 Ans+= 1 for i in range(1, l): if(Summary* (Summary+ b[i])>= 0): if(Summary > 0): Ans+= b[i]+ Summary+ 1 b[i] = -Summary- 1 Summary= -1 else: Ans+= -Summary+ 1- b[i] b[i] = -Summary+ 1 Summary= 1 else: Summary+= b[i] print(min(ans, Ans))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner 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(); } sc.close(); int count = 0; int sum = a[0]; if(sum==0){ if(a[1]>0){ sum=-1; }else{ sum=1; } count++; } for(int i=1; i<N; i++){ int diff = checkSum(sum, sum+a[i]); count += Math.abs(diff); sum = sum+a[i]+diff; } System.out.println(count); } private static int checkSum(int a, int b){ if ( a>0 && b>=0){ return -(b+1); }else if( a<0 && b<=0){ return -(b-1); }else{ 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(L): # L[0]!=0を起点とする cnt = 0 s = L[0] for i in range(1,len(L)): a = L[i] if(s>0 and s+a>=0): L[i] = -s-1 cnt += (s+a+1) s = -1 elif(s<0 and s+a<=0): L[i] = -s+1 cnt += (-s-a+1) s = 1 else: s += a return cnt def ans(L): a = L[0] c0,c1=0,0 if (a>0): c0 = resolve(L) c1 = (a+1) + resolve([-1]+L[1:]) elif (a<0): c0 = resolve(L) c1 = (-a+1) + resolve([1]+L[1:]) else: c0 = 1 + resolve([1]+L[1:]) c1 = 1 + resolve([-1]+L[1:]) 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
UNKNOWN
import java.util.* fun main(args: Array<String>) { val sc = Scanner(System.`in`) val n = sc.nextInt() val a = (0 until n).map { sc.next().toLong() } println(problem059c(n, a)) } fun problem059c(n: Int, a: List<Long>): Long { val count1 = compute(n, a) // val a = a.toMutableList() // var a0 = a[0] // var count = 0L // if (a0 > 0) { // val tmp = a0 + 1 // a[0] = a0 - tmp // count += tmp // } else { // val tmp = a0 - 1 // a[0] = a0 - tmp // count -= tmp // } // val count2 = compute(n, a) + count return count1 }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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(), " "); int sum = Integer.parseInt(str.nextToken()); int flg; int count = 0; if( sum > 0) flg = 1; else flg = -1; for(int i = 0; i < n-1; i++){ int tmp = 0; sum += Integer.parseInt(str.nextToken()); //System.out.print(sum+" "); if(flg == 1){ //次は負 //System.out.print(sum+" "); if(sum >= 0){ //System.out.print(sum+" "); tmp += (sum + 1); sum = -1; } flg = -1; } else{ //次は正 if(sum <= 0){ //System.out.print(sum+" "); tmp += 1+ (-sum); sum = 1; } flg = 1; } //System.out.print(tmp+" "); count += tmp; } System.out.println(count); } catch (IOException e) { System.out.println("error"); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
import std.stdio, std.conv, std.algorithm, std.range, std.array, std.string, std.uni, std.bigint, std.math; void main() { auto n = readln.chomp.to!uint; auto an = readln.split.to!(int[]); min(func(an, 0), func(an, 1)).writeln; } uint func(int[] an, uint mod) { auto sum = 0; auto res = 0; foreach(i,a; an) { sum += a; if (i % 2 == mod && sum <= 0) { res += 1 - sum; sum = 1; } if (i % 2 != mod && sum >= 0) { res += 1 + sum; sum = -1; } } return res; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; bool dif(int a, int b) { if (a < 0 && b > 0) return true; if (a > 0 && b < 0) return true; return false; } long long int odd(vector<int> v, vector<int> &w) { long long int ans = 0; if (v[0] <= 0) while (++v[0] != 1) ; int sum = v[0]; ans = abs(v[0] - w[0]); for (int i = 1; i < v.size(); i++) { if (dif(sum, sum + v[i])) { sum += v[i]; } else { if (sum > 0) { while (sum + --v[i] >= 0) ; } else if (sum < 0) { while (sum + ++v[i] <= 0) ; } sum += v[i]; } ans += abs(v[i] - w[i]); } return ans; } long long int even(vector<int> v, vector<int> &w) { long long int ans = 0; if (v[0] >= 0) while (--v[0] != -1) ; int sum = v[0]; ans = abs(v[0] - w[0]); for (int i = 1; i < v.size(); i++) { if (dif(sum, sum + v[i])) { sum += v[i]; } else { if (sum > 0) { while (sum + --v[i] >= 0) ; } else if (sum < 0) { while (sum + ++v[i] <= 0) ; } sum += v[i]; } ans += abs(v[i] - w[i]); } return ans; } int main() { int n; cin >> n; vector<int> v(n), cpy; for (int &i : v) cin >> i; cpy = v; long long int ans = min(odd(v, cpy), even(v, cpy)); cout << ans; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long Sum(vector<long long>& a, int index) { long long s = 0; for (int i = 0; i <= index; i++) { s += a[i]; } return s; } int main(int argc, char* argv[]) { int n; cin >> n; vector<long long> a(n, 0); vector<long long> s(n, 0); int i; for (i = 0; i < n; i++) { cin >> a[i]; } s[0] = Sum(a, 0); long long aw = 0; for (i = 1; i < n; i++) { s[i] = Sum(a, i); if (s[i - 1] > 0) { if (s[i] < 0) { continue; } else { aw += abs(s[i - 1] + a[i] + 1); a[i] = -1 - s[i - 1]; s[i] = Sum(a, i); } } else { if (s[i] > 0) { continue; } else { aw += abs(s[i - 1] + a[i] - 1); a[i] = 1 - s[i - 1]; s[i] = Sum(a, i); } } } cout << aw << 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) { if(vector[0] > 0) sum1 = vector[0]; //初項 else { sum1 = 1; answer += abs(1-vector[0]); } } else if(sum1 < 0) { if(sum1 + vector[i] > 0){ //和の符号がデフォルトで異なるとき // answer -> そのまま sum1 += vector[i]; } else { answer1 += abs((-1)*sum1+1 - vector[i]); // vector[i] -> -sum1+1 までincrimentすると和は1 sum1 = 1; } } else { if(sum1 + vector[i] < 0) { //answer->そのまま sum1 += vector[i]; } else { answer1 += abs((-1)*sum1-1 - vector[i]); // vector[i] -> -sum1-1 までincrimentすると和は-1 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 = vector[0]; } } else if(sum2 < 0) { if(sum2 + vector[i] > 0){ //和の符号がデフォルトで異なるとき // answer-> そのまま sum2 += vector[i]; } else { answer2 += abs((-1)*sum2+1 - vector[i]); // vector[i] -> -sum1+1 までincrimentすると和は1 sum2 = 1; } } else { if(sum2 + vector[i] < 0) { //answer->そのまま sum2 += vector[i]; } else { answer2 += abs((-1)*sum2-1 - vector[i]); // vector[i] -> -sum1-1 までincrimentすると和は-1 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 <iostream> #include <vector> using namespace std; int main(){ int N; cin >> N; vector<long long int> A(N); long long int s = 0LL vector<long long int> V(N); for(int i = 0; i < N; i++){ cin >> A[i]; s += A[i]; V[i] = s; } //先手が+のパターン(i % 2 == 0のとき 1) long long int tmp1 = 0LL; for(int i = 0; i < N-1; i++){ if(i % 2 == 0){ if(V[i] > 0) continue; else{ tmp1 += 1LL - V[i]; V[i] = 1LL; V[i + 1] = 1LL + A[i+1]; } }else{ if(V[i] < 0) continue; else{ tmp1 += -1LL - V[i]; V[i] = -1LL; V[i + 1] = -1LL + A[i+1]; } } } for(int i = 0; i < N-1; i++){ if(i % 2 == 1){ if(V[i] > 0) continue; else{ tmp2 += 1LL - V[i]; V[i] = 1LL; V[i + 1] = 1LL + A[i+1]; } }else{ if(V[i] < 0) continue; else{ tmp2 += -1LL - V[i]; V[i] = -1LL; V[i + 1] = -1LL + A[i+1]; } } } cout << min(tmp1, tmp2); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import numpy as np dummyn = int(input()) a = list(map(int, input().split())) b_np = np.array([sum(a[:i+1]) for i in range(len(a))]) ans = 0 for i in range(len(b_np)-1): if b_np[i] * b_np[i+1] >= 0 and b_np[i] <= 0 and b_np[i] <= b_np[i+1]: ans += (1 - b_np[i+1]) b_np[i+1:] += (1 - b_np[i+1]) elif b_np[i] * b_np[i+1] >= 0 and b_np[i] < 0 and b_np[i] > b_np[i+1]: ans += (1 - b_np[i]) b_np[i:] += (1 - b_np[i]) elif b_np[i] * b_np[i+1] >= 0 and b_np[i] >= 0 and b_np[i] >= b_np[i+1]: ans += (b_np[i+1] + 1) b_np[i+1:] -= (b_np[i+1] + 1) elif b_np[i] * b_np[i+1] >= 0 and b_np[i] > 0 and b_np[i] < b_np[i+1]: ans += (b_np[i] + 1) b_np[i:] -= (b_np[i] + 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.at(i); int cnt = 0, cnt2 = 0, sum = a.at(0); if (a.at(0) >= 0) for (int i = 1; i < n; i++) { if (i % 2 == 1) { while (a.at(i) >= 0 || sum + a.at(i) >= 0) { cnt++; a.at(i)--; } sum += a.at(i); } else { while (a.at(i) < 0 || sum + a.at(i) <= 0) { cnt++; a.at(i)++; } sum += a.at(i); } } else { for (int i = 1; i < n; i++) { if (i % 2 == 1) { while (a.at(i) < 0 || sum + a.at(i) <= 0) { cnt++; a.at(i)++; } sum += a.at(i); } else { while (a.at(i) >= 0 || sum + a.at(i) >= 0) { cnt++; a.at(i)--; } sum += a.at(i); } } } cout << cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long num_operate(long n, long sum, long* a) { long j; 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; } } return j; } int main() { cin.tie(0); ios::sync_with_stdio(false); long n; cin >> n; vector<long> a(n); for (long i = 0; i < n; i++) cin >> a[i]; long sum = a[0]; if (sum == 0) { long cnt1 = num_operate(n, 1, &a.front()) + 1; long cnt2 = num_operate(n, -1, &a.front()) + 1; cout << min(cnt1, cnt2) << endl; } else { long cnt = num_operate(n, sum, &a.front()); 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
import copy n = int(input()) a = [int(ai) for ai in input().split()] def search(a, flip=False): if flip: count = 0 a_sum = a[0] else: count = abs(a[0]) + 1 a_sum = - a[0] for ai in a[1:]: next_sum = a_sum + ai if next_sum * a_sum < 0: continue if a_sum < 0: a_sum = 1 elif a_sum > 0: a_sum = -1 else: c = 0 count += abs(next_sum) + 1 a_sum = next_sum + c return count print(min(search(a, False), search(a, True))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <iostream> #define T true #define F false using namespace std; int main(void) { int num, i = 1, a; long long sum, ans = 0; bool sig = T; cin >> num >> sum; if (sum < 0) sig = F; for (; i < num; i++) { scanf_s("%d", &a); if (sum == 0) { if (a >= 0) { sum--; sig = F; } else sum++; ans++; } sum += a; if (sig == T && sum >= 0) { ans += sum + 1; sum = -1; } else if (sig == F && sum <= 0) { ans += (-1) * sum + 1; sum = 1; } sig = !sig; } cout << ans << "\n"; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys input=sys.stdin.readline #input = open(sys.argv[1], "r").readline def main(): N = int(input()) A = list(map(int, input().split())) tmp = A.copy() mi = -1 for j in range(2): A = tmp.copy() s = 0 n = 0 if j == 0: # 偶数番目までの和が正 ⇒ A[0] を負にする if A[0] > 0: n += abs(A[0] +1) A[0] = -1 else: # 奇数番目までの和が正 ⇒ A[0] を正にする if A[0] < 0: n += abs(A[0] -1) A[0] = 1 s = A[0] # print(A) for i in range(1,N): if s * (s+A[i]) >= 0: if s < 0: n += abs(-s+1 -A[i]) A[i] = -s+1 else: n += abs(-s-1 -A[i]) A[i] = -s-1 s += A[i] if mi == -1: mi = n else: mi = min(mi, n) print(mi) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int t; int answer = 0; int sumi; bool flag; cin >> t; vector<int> A(t); cin >> A[0]; sumi = A[0]; if (sumi > 0) { flag = true; } else if (sumi < 0) { flag = false; } else { answer += 1; A[0] += 1; sumi += 1; flag = true; } for (int i = 1; i < t; i++) { cin >> A[i]; sumi += A[i]; if (sumi == 0) { answer += 1; if (flag) { sumi = -1; } else { sumi = 1; } } else if (sumi > 0 == flag) { answer += abs(sumi) + 1; if (sumi > 0) { sumi = -1; } else { sumi = 1; } } flag = !flag; } cout << answer << 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() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; int sum = 0; int ans = 0; for (int i = 0; i < N; ++i) { int t; cin >> t; if (i == 0) sum = t; else { if (sum < 0 && sum + t <= 0) { ans += 1 - sum - t; sum = 1; } else if (sum > 0 && sum + t >= 0) { ans += abs(-1 - sum - t); sum = -1; } else { sum += t; } } } cout << ans << "\n"; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; unsigned long long inf = (1LL << 62); long long mod = 1000000007; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long max(long long a, long long b) { if (a < b) { return b; } else return a; } long long min(long long a, long long b) { if (a < b) return a; return b; } pair<long long, long long> dx[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; vector<pair<long long, long long> > list; void floyd(int N, long long** d) { for (int k = 0; k < (int)(N + 1); k++) { for (int i = 0; i < (int)(N + 1); i++) { if (d[i + 1][k + 1] == inf) continue; for (int j = 0; j < (int)(N + 1); j++) { if (d[j + 1][k + 1] == inf) continue; d[i + 1][j + 1] = min(d[i + 1][j + 1], d[i + 1][k + 1] + d[j + 1][k + 1]); } } } } unsigned long long modpow(unsigned long long a, unsigned long long b) { long long retval = 0; if (b == 0) { return 1; } else if (b % 2 == 0) { retval = modpow(a, b / 2) % mod; return (retval * retval) % mod; } else { return ((a % mod) * modpow(a, b - 1)) % mod; } } long long pow(long long a, long long b) { long long ret; if (b == 0) return 1; else if (b % 2 == 0) { ret = pow(a, b / 2); return ret * ret; } else if (b % 2 == 1) { return a * pow(a, b - 1); } } int bit(long long a, long long n) { if (a & pow(2, n - 1) != 0) { return 1; } else { return 0; } } unsigned long long modkaijou(unsigned long long a) { if (a == 1 || a == 0) { return 1; } long long retval = 1; for (unsigned long long i = a; i >= 1; i--) { retval = (retval * (i % mod)) % mod; } return retval % mod; } unsigned long long modcomb(unsigned long long a, unsigned long long b) { if (a == 0 || b == 0) return 1; return (((modkaijou(a) * modpow(modkaijou(a - b), mod - 2)) % mod) * modpow(modkaijou(b), mod - 2)) % mod; } class DisjointSet { public: vector<int> p, rank, num; DisjointSet() {} DisjointSet(int size) { rank.resize(size, 0); p.resize(size, 0); num.resize(size, 0); for (int i = 0; i < (int)(size); i++) { p[i] = i; rank[i] = 0; num[i] = 1; } } bool same(int x, int y) { return findSet(x) == findSet(y); } void unite(int x, int y) { if (!same(x, y)) link(findSet(x), findSet(y)); } void link(int x, int y) { if (rank[x] > rank[y]) { p[y] = x; num[x] += num[y]; } else { p[x] = y; num[y] += num[x]; if (rank[x] == rank[y]) { rank[y]++; } } } long long NumberOfElements(int x) { return num[findSet(x)]; } int findSet(int x) { if (x != p[x]) { p[x] = findSet(p[x]); num[x] = 1; } return p[x]; } }; bool compare_b(pair<long long, long long> a, pair<long long, long long> b) { if (a.first < b.first) { return true; } else if (a.first == b.first) { return a.second < b.second; } else { return false; } } int ketasuu(int a) { int ret = 1; int val = a; while (val / 10 != 0) { ret += 1; val /= 10; } return ret; } typedef struct trio { long long a, b, y; } ti; string to_binary(long long a) { long long val = a; string retval = ""; retval = to_string(val % 2); while (val / 2 != 0) { val /= 2; retval = (to_string(val % 2)) + retval; } return retval; } int ctoi(char a) { return a - '0'; } string S, T; int a[100005]; int sum[100005]; int main() { int N, M; long long g = 0; long long p1, p2; cin >> N; for (int i = 0; i < (int)(N); i++) { cin >> a[i + 1]; sum[i + 1] = sum[i] + a[i + 1]; } long long ans = 0; long long offset = 0; for (int i = 0; i < (int)(N); i++) { if (sum[i + 1] + offset <= 0 && (i + 1) % 2 == 0) { ans += (1 - sum[i + 1] - offset); offset += (1 - sum[i + 1] - offset); } else if (sum[i + 1] + offset >= 0 && (i + 1) % 2 == 1) { ans += (sum[i + 1] + offset + 1); offset += -(sum[i + 1] + offset + 1); } } long long ans2 = ans; offset = 0; ans = 0; for (int i = 0; i < (int)(N); i++) { if (sum[i + 1] + offset <= 0 && (i + 1) % 2 == 1) { ans += (1 - sum[i + 1] - offset); offset += (1 - sum[i + 1] - offset); } else if (sum[i + 1] + offset >= 0 && (i + 1) % 2 == 0) { ans += (sum[i + 1] + offset + 1); offset += -(sum[i + 1] + offset + 1); } } cout << min(ans, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
module Main(main) where import Control.Applicative import Control.Monad import Data.List import Data.Array import Data.Char import qualified Data.Set as S main = do n <- (read::String -> Int) <$> getLine arr <- map (read::String -> Int) . words <$> getLine print $ calcCost arr calcCost::[Int] -> Int calcCost (x:xs) | x /= 0 = snd $ foldl (\(s, c) v -> let next = calcNext s in if next * v > 0 && abs v > abs next then (s + v, c) else (s + next, c + abs (next - v))) (x, 0) xs | otherwise = let plus = 1 + calcCost (1:xs) minus = 1 + calcCost ((-1):xs) in min plus minus calcNext::Int -> Int calcNext a = if a > 0 then -a - 1 else -a + 1
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; int a[100000]; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } int sum1, sum2; int cnt = 0; sum1 = 0; for (int i = 0; i < n; i++) { sum1 += a[i]; if (sum1 == 0) { if (a[i + 1] >= 0) a[i]++; else a[i]--; cnt++; } if (i < n - 1) { sum2 = sum1 + a[i + 1]; if (sum1 * sum2 >= 0) { int a_p = a[i + 1]; if (sum1 > 0) a[i + 1] = -sum1 - 1; else if (sum1 < 0) a[i + 1] = -sum1 + 1; cnt += abs(a[i + 1] - a_p); } } } cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long a[n], s1[n], s2[n], m = 0, p = 0; for (long long i = 0; i < n; i++) { cin >> a[i]; if (i == 0) s1[i] = a[i]; else s1[i] = s1[i - 1] + a[i]; s2[i] = s1[i]; } for (long long i = 0; i < n; i++) { if (i % 2 == 0) { if (s1[i] >= 0) { long long k = s1[i] + 1; for (long long j = i; j < n; j++) s1[j] -= k; m += k; } } else { if (s1[i] <= 0) { long long k = 1 - s1[i]; for (long long j = i; j < n; j++) s1[j] += k; m += k; } } } for (long long i = 0; i < n; i++) { if (i % 2 == 1) { if (s2[i] >= 0) { long long k = s2[i] + 1; for (long long j = i; j < n; j++) s2[j] -= k; p += k; } } else { if (s2[i] <= 0) { long long k = 1 - s2[i]; for (long long j = i; j < n; j++) s2[j] += k; p += k; } } } cout << min(m, p); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll N = 0; cin >> N; vector<ll> A(N, 0); for (ll i = 0; i < N; i++) { cin >> A.at(i); } ll ans = 0; vector<ll> sum(N, 0); sum.at(0) = A.at(0); ll ansa = abs(A.at(0) + (abs(A.at(0)) / A.at(0))); vector<ll> suma(N, 0); suma.at(0) = -1 * (abs(A.at(0)) / A.at(0)); for (size_t i = 1; i < N; i++) { sum.at(i) = sum.at(i - 1) + A.at(i); if (sum.at(i) * sum.at(i - 1) < 0) { continue; } else { ans += abs(sum.at(i) + (abs(A.at(i - 1)) / A.at(i - 1))); sum.at(i) = -1 * (abs(A.at(i - 1)) / A.at(i - 1)); } } for (size_t i = 1; i < N; i++) { suma.at(i) = suma.at(i - 1) + A.at(i); if (suma.at(i) * suma.at(i - 1) < 0) { continue; } else { ansa += abs(suma.at(i) + (abs(A.at(i - 1)) / A.at(i - 1))); suma.at(i) = -1 * (abs(A.at(i - 1)) / A.at(i - 1)); } } cout << min(ans, ansa) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long solve(const vector<long long> data, int sign) { int n = data.size(); vector<long long> a = data; long long result = 0; long long sum = 0; for (int i = 0; i < n; i++) { long long k = (sum + a[i]) * sign; if (i % 2 == 0) { if (k <= 0) { result += 1 - k; a[i] += (1 - k) * sign; } } else { if (k >= 0) { result += 1 + k; a[i] += (1 + k) * sign; } } sum += a[i]; } return result; } int main(void) { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; cout << min(solve(a, 1), solve(a, -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; int main() { int N; cin >> N; vector<long long>(A); for (int x = 0; x < (N); x++) { long long aa; cin >> aa; (A).push_back(aa); } if (A.at(0) == 0) { long long ans1 = 0, ans2 = 0; vector<long long>(B); for (int x = 0; x < (N); x++) { B.at(x) = A.at(x); } A.at(0) = 1; B.at(0) = -1; for (int y = (1); y < (N); y++) { A.at(y) += A.at(y - 1); if (A.at(y) * A.at(y - 1) >= 0) { ans1 += abs(A.at(y)) + 1; if (A.at(y - 1) < 0) { A.at(y) = 1; } else { A.at(y) = -1; } } } for (int y = (1); y < (N); y++) { B.at(y) += B.at(y - 1); if (B.at(y) * B.at(y - 1) >= 0) { ans2 += abs(B.at(y)) + 1; if (B.at(y - 1) < 0) { B.at(y) = 1; } else { B.at(y) = -1; } } } cout << min(ans1, ans2) << endl; } else { long long ans = 0; for (int y = (1); y < (N); y++) { A.at(y) += A.at(y - 1); if (A.at(y) * A.at(y - 1) >= 0) { ans += abs(A.at(y)) + 1; if (A.at(y - 1) < 0) { A.at(y) = 1; } else { A.at(y) = -1; } } } cout << ans << endl; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (long long(i) = 0; (i) < (long long)(n); ++(i)) cin >> a[i]; int ruiseki, cnt; if (a[0] > 0) { cnt = 0; ruiseki = a[0]; } else { cnt = (1 - a[0]); ruiseki = 1; } for (long long(i) = 1; (i) < (long long)(n); ++(i)) { if (i % 2 == 0) { if (ruiseki + a[i] <= 0) { cnt += (1 - (ruiseki + a[i])); ruiseki = 1; } else { ruiseki += a[i]; } } else { if (ruiseki + a[i] >= 0) { cnt += (1 + (ruiseki + a[i])); ruiseki = -1; } else { ruiseki += a[i]; } } } int cnt2; if (a[0] < 0) { cnt2 = 0; ruiseki = a[0]; } else { cnt2 = (1 + a[0]); ruiseki = -1; } for (long long(i) = 1; (i) < (long long)(n); ++(i)) { if (i % 2 == 1) { if (ruiseki + a[i] <= 0) { cnt2 += (1 - (ruiseki + a[i])); ruiseki = 1; } else { ruiseki += a[i]; } } else { if (ruiseki + a[i] >= 0) { cnt2 += (1 + (ruiseki + a[i])); ruiseki = -1; } else { ruiseki += a[i]; } } } 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 main() { int N; cin >> N; vector<long long> L(N); for (int i = 0; i < N; i++) { cin >> L.at(i); } long long v = 0, res = 0; bool change_flag = true; for (int i = 0; i < N; i++) { if (i == 0) { v = L.at(i); } else { if (v > 0 && v + L.at(i) >= 0) { while (true) { if (v + L.at(i) < 0) { v += L.at(i); break; } else { L.at(i)--; res++; } } } else if (v < 0 && v + L.at(i) <= 0) { while (true) { if (v + L.at(i) > 0) { v += L.at(i); break; } else { L.at(i)++; res++; } } } else { v += L.at(i); } } } cout << res << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(int , input().split())) res=[0]*n res[0]=a[0] ng=0 ps=0 for i in range(1,n): res[i]=res[i-1]+a[i] for i in range(n-1): s=res[i]-ng+ps if s*(res[i+1]+ps-ng)>=0: if s>0: ng+=abs(a[i+1]+s)+1 else: ps+=abs(a[i+1]+s)+1 print(ps+ng)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); } } fast; template <typename T> inline size_t maxElement(T beginIt, T endIt) { return max_element(beginIt, endIt); } template <typename T> inline size_t minElement(T beginIt, T endIt) { return min_element(beginIt, endIt); } template <typename T> inline size_t maxIndex(T beginIt, T endIt) { return distance(beginIt, *max_element(beginIt, endIt)); } template <typename T> inline size_t minIndex(T beginIt, T endIt) { return distance(beginIt, *min_element(beginIt, endIt)); } template <typename T> inline int sum(T beginIt, T endIt) { return accumulate(beginIt, endIt, 0); } template <typename T> inline int mean(T beginIt, T endIt) { return sum(beginIt, endIt) / distance(beginIt, endIt); } template <typename T> inline void debug(T x) { cerr << x << " " << "(L:" << 17 << ")" << endl; } signed main(void) { int num = 0; int N; array<int, 10000> A; string S; cin >> N; for (int i = 0; i < N; ++i) { cin >> A[i]; } int tmp = A[0]; for (int i = 1; i < N; ++i) { if (tmp > 0) { if (A[i] >= -tmp) { num += abs(-tmp - 1 - A[i]); A[i] = -tmp - 1; } } else { if (A[i] <= -tmp) { num += abs(-tmp + 1 - A[i]); A[i] = -tmp + 1; } } tmp += A[i]; } cout << num << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int s1 = 0, s2 = 0, ans[] = {0, 0}; int a, b; for (int i = 0; i < N; ++i) { cin >> a; b = a; if (i % 2) { s1 += a; s2 += a; if (s1 >= 0) { ans[1] += s1 + 1; a -= s1 + 1; s1 = -1; } if (!a) { --a; --s1; ++ans[1]; } if (s2 <= 0) { ans[0] -= s2 - 1; b -= s2 - 1; s2 = 1; } if (!b) { ++b; ++s2; ++ans[0]; } } else { s1 += a; s2 += a; if (s2 >= 0) { ans[0] += s2 + 1; a -= s2 + 1; s2 = -1; } if (!a) { ++ans[0]; --a; --s2; } if (s1 <= 0) { ans[1] -= s1 - 1; b -= s1 + 1; s1 = 1; } if (!b) { ++ans[1]; ++b; ++s1; } } } cout << min(ans[0], ans[1]) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int sign(long long n) { return ((n > 0) - (n < 0)); } int main() { int n; scanf("%d", &n); long long sum = 0; long long ans = 0; for (int i = 0; i < n; i++) { int a; scanf("%d", &a); if (sum == 0) { if (a == 0) { a++; ans++; } } else if (sum + a == 0) { a - sign(sum); ans++; } else if (sign(sum + a) + sign(sum) != 0) { while (sign(sum + a) + sign(sum) != 0) { if (sign(sum) > 0) { a--; } else { a++; } ans++; } } sum += a; cerr << 38 << " " << "a" << ": " << a << endl; ; cerr << 39 << " " << "sum" << ": " << sum << endl; } printf("%llu\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
python3
n = input() a = [int(i) for i in input.solit()] X = 0 ans = 0 for i in a: X += i if X > 0: b = -1 - X ans += b - a(i+1) else X < 0: b = 1 - X ans += b - a(i+1) return ans 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; long countpl = 0, countmi = 0; long a[100100], b[100100], c[100100]; for (int i = 0; i < n; i++) { cin >> a[i]; } b[0] = a[0]; c[0] = a[0]; for (int i = 1; i < n; i++) { b[i] = b[i - 1] + a[i]; if (i % 2 == 0) { if (b[i] < 1) { countpl += 1 - b[i]; b[i] = 1; } } else { if (b[i] > -1) { countpl += b[i] - (-1); b[i] = -1; } } c[i] = c[i - 1] + a[i]; if (i % 2 == 0) { if (c[i] > -1) { countmi += c[i] - (-1); c[i] = -1; } } else { if (c[i] < 1) { countmi += 1 - c[i]; c[i] = 1; } } } cout << min(countpl, countmi) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
package main import ( "bufio" "fmt" "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], ans_1 = a[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 } } sum = make([]int, n) if a[0] < 0 { sum[0], ans_2 = 1, 1-a[0] } else { sum[0], ans_2 = -1, a[0]-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
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 int64 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([]int64, n) sum[0] = int64(a[0]) for i := 1; i < n; i++ { sum[i] += int64(a[i]) + sum[i-1] if (0 <= sum[i-1] && 0 <= sum[i]) || (sum[i-1] <= 0 && sum[i] <= 0) { // NGパターン if sum[i] < 0 { ans += 1 - sum[i] sum[i] = 1 } else { ans += sum[i] + 1 if sum[i-1] < 0 { sum[i] = 1 } else { sum[i] = -1 } } } } fmt.Println(ans) } /* ---------------------------------------- */ 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> using namespace std; const double PI = 3.1415926535897932384626433832795; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool isDiffer(long long a, long long b) { if (b == 0) return false; if (((a > 0) && (b < 0)) || ((a < 0) && (b > 0))) return true; else return false; } int main() { ios::sync_with_stdio(false); long long n; cin >> n; vector<long long> v; for (int i = 0; i < n; i++) { long long t; cin >> t; v.push_back(t); } long long os = v[0]; long long ans = 0; for (int i = 1; i < n; i++) { if (!isDiffer(os, v[i] + os)) { long long ob = (os >= 0) ? -1 : 1; ans += abs(ob - os - v[i]); v[i] = ob - os; } os += v[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 dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int n; vector<long long> a(200000); long long solve(vector<long long> a, bool flag) { long long sum = a[0], ans; if (flag) ans = 0; else { ans = abs(a[0]) + 1; if (a[0] < 0) { sum = 1; } else { sum = -1; } } for (int i = 1; i < n; i++) { long long tmp = sum; sum += a[i]; if (sum >= 0 && tmp > 0) { ans += abs(sum) + 1; sum = -1; } else if (sum <= 0 && tmp < 0) { ans += abs(sum) + 1; sum = 1; } } return ans; } int main() { cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; cout << min(solve(a, true), solve(a, false)) << 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> #pragma GCC optimize("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } const int inf = INT_MAX / 2; const long long infl = 1LL << 60; 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; } enum PosiNega { POSITIVE = 0, NEGATIVE = 1 }; int solve(int N, int *a, PosiNega odd_posinega) { int ans = 0; int sum = 0; PosiNega posi_nega = odd_posinega; for (int i = 0; i < N; i++) { sum += a[i]; if (POSITIVE == posi_nega) { if (0 >= sum) { ans += abs(1 - sum); sum = 1; } posi_nega = NEGATIVE; } else { if (0 <= sum) { ans += abs(-1 - sum); sum = -1; } posi_nega = POSITIVE; } } return ans; } void _main() { int N; cin >> N; int a[N]; for (int i = 0; i < N; i++) cin >> a[i]; int candidate1 = solve(N, a, POSITIVE); int candidate2 = solve(N, a, NEGATIVE); int ans = (candidate1 < candidate2) ? candidate1 : candidate2; cout << ans << "\n"; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; signed main() { long long n; cin >> n; long long a[n + 1]; for (long long i = 0; i < (n); i++) cin >> a[i + 1]; a[0] = 0; for (long long i = 0; i < (n); i++) { a[i + 1] += a[i]; } long long cnta = 0; long long cntb = 0; long long sum = 0; for (long long i = 1; i < n + 1; i++) { if (i % 2 == 0) { if (0 <= (a[i] + sum)) { cnta += (a[i] + sum + 1); sum -= (a[i] + sum + 1); } } else { if ((a[i] + sum) <= 0) { cnta += (1 - (a[i] + sum)); sum += (1 - (a[i] - sum)); } } } sum = 0; for (long long i = 1; i < n + 1; i++) { if (i % 2 == 1) { if (0 <= (a[i] + sum)) { cntb += (a[i] + sum + 1); sum -= (a[i] + sum + 1); } } else { if ((a[i] + sum) <= 0) { cntb += (1 - (a[i] + sum)); sum += (1 - (a[i] - sum)); } } } cout << min(cnta, cntb) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import numpy as np n = int(input()) a = list(map(int,input().split())) #print (a) sum = 0 count = 0 p = 0 for i in range(n): sum += a[i] if i>0: p = a[i] if sum >=0: if (sum - a[i]) > 0: a[i] = -(sum-a[i])-1 count += np.abs(p-a[i]) sum = -1 if sum <= 0: if (sum - a[i]) < 0: a[i] = -(sum-a[i])+1 count += np.abs(p-a[i]) sum = 1 #print (sum) 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 = [int(i) for i in input().split()] sum1 = sum2 = a[0] cnt1 = cnt2 = 0 for i in range(1,n): if sum1>0: if sum1+a[i]>=0: cnt1+=abs(sum1+a[i]+1) sum1 = -1 else: sum1 +=a[i] else: if sum1+a[i]<=0: cnt1+=abs(sum1+a[i]-1) sum1 = 1 else: sum1+=a[i] for i in range(1,n): if sum2>0: if sum2+a[i]>=0: cnt2+=abs(sum2+a[i]-1) sum2 = 1 else: sum2 +=a[i] else: if sum2+a[i]<=0: cnt2+=abs(sum2+a[i]+1) sum2 = -1 else: sum2+=a[i] 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; static const long long INF = 1000000000000000000; int main() { int n; cin >> n; long long A[n]; long long sum[n]; long long ans = 0; for (int i = 0; i < n; i++) { cin >> A[i]; sum[i] = 0; } long long minans = INF; long long temp = 0; int p[2] = {1, -1}; if (A[0] == 0) { for (int k = 0; k < 2; k++) { ans = 0; ans++; A[0] = p[k]; sum[0] = A[0]; for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + A[i]; if (sum[i - 1] * sum[i] < 0) continue; if (sum[i - 1] * sum[i] == 0) { if (sum[i - 1] < 0) { ans++; sum[i] = 1; continue; } else if (sum[i - 1] > 0) { sum[i] = -1; ans++; continue; } } if (sum[i - 1] < 0) { temp = sum[i]; sum[i] = 1; ans = ans + 1 + (-temp); continue; } else if (sum[i - 1] > 0) { temp = sum[i]; sum[i] = -1; ans = ans + 1 + temp; continue; } } minans = min(minans, ans); } } ans = 0; sum[0] = A[0]; for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + A[i]; if (sum[i - 1] * sum[i] < 0) continue; if (sum[i - 1] * sum[i] == 0) { if (sum[i - 1] < 0) { ans++; sum[i] = 1; continue; } else if (sum[i - 1] > 0) { sum[i] = -1; ans++; continue; } } if (sum[i - 1] < 0) { temp = sum[i]; sum[i] = 1; ans = ans + 1 + (-temp); continue; } else if (sum[i - 1] > 0) { temp = sum[i]; sum[i] = -1; ans = ans + 1 + temp; continue; } } minans = min(minans, ans); ans = 0; if (A[0] > 0) { temp = A[0]; A[0] = -1; ans = temp + 1; sum[0] = A[0]; } else { temp = A[0]; A[0] = 1; ans = -temp + 1; sum[0] = A[0]; } for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + A[i]; if (sum[i - 1] * sum[i] < 0) continue; if (sum[i - 1] * sum[i] == 0) { if (sum[i - 1] < 0) { ans++; sum[i] = 1; continue; } else if (sum[i - 1] > 0) { sum[i] = -1; ans++; continue; } } if (sum[i - 1] < 0) { temp = sum[i]; sum[i] = 1; ans = ans + 1 + (-temp); continue; } else if (sum[i - 1] > 0) { temp = sum[i]; sum[i] = -1; ans = ans + 1 + temp; continue; } } minans = min(minans, ans); cout << minans << 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 a[100010]; int main() { int n, i, j, k; long long s, x, y; while (scanf("%d", &n) != EOF) { for (i = 0; i < n; i++) scanf("%d", &a[i]); s = 0; x = a[0]; for (i = 1; i < n; i++) { y = x; x = x + a[i]; if (x < 0 && y > 0) continue; if (y < 0 && x > 0) continue; if (y < 0) { s = s - x + 1; x = 1; } else if (y > 0) { s = s + x + 1; x = -1; } } printf("%lld\n", s); } 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 int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; vector<long long int> B(N); B[0] = A[0]; for (int i = 1; i < N; i++) B[i] = B[i - 1] + A[i]; int ans = 0; int base = 0; for (int i = 1; i < N; i++) { if ((B[i] + base) * (B[i - 1] + base) > 0) { if (B[i] + base > 0) { if (B[i] > B[i - 1]) { ans += abs(B[i - 1] + base) + 1; base -= abs(B[i - 1] + base) + 1; } else { ans += abs(B[i] + base) + 1; base -= abs(B[i] + base) + 1; } continue; } else if (B[i] + base < 0) { if (B[i] < B[i - 1]) { ans += abs(B[i - 1] + base) + 1; base += abs(B[i - 1] + base) + 1; } else { ans += abs(B[i] + base) + 1; base += abs(B[i] + base) + 1; } continue; } } if (B[i - 1] + base == 0) { if (B[i] + base > 0) { ans += 1; base -= 1; continue; } else if (B[i] + base < 0) { ans += 1; base += 1; continue; } } if (i == N - 1 && B[i] + base == 0) ans++; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(NULL); ios::sync_with_stdio(0); int n; cin >> n; int arr[100010]; for (int i = 0; i < n; i++) { int aux; cin >> aux; arr[i] = aux; } int initA = arr[0]; int initB = 0; int countA = 0; int countB = 0; if (initA > 0) { initB = -1; countB += initA + 1; } else if (initA < 0) { initB = 1; countB += abs(initA) + 1; } if (arr[0] == 0) { initA = 1; initB = -1; countA++; countB++; } int sumaA = initA; int sumaB = initB; for (int i = 1; i < n; i++) { if (sumaA > 0) { sumaA += arr[i]; sumaB += arr[i]; if (sumaA >= 0) { countA += sumaA + 1; sumaA = -1; } if (sumaB <= 0) { countB += abs(sumaB) + 1; sumaB = 1; } } else { sumaB += arr[i]; sumaA += arr[i]; if (sumaA <= 0) { countA += abs(sumaA) + 1; sumaA = 1; } if (sumaB >= 0) { countB += sumaB + 1; sumaB = -1; } } } cout << min(countA, countB) << '\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> const int MOD = pow(10, 9) + 7; using namespace std; int in() { int temp; scanf("%d", &temp); return temp; } long long lin() { long long temp; scanf("%lld", &temp); return temp; } int solve(vector<int> csum, int N, int sig) { int cumOper = 0; int sign = sig; int count = 0; int signi; for (auto i = 0; i < N; i++) { if ((csum[i] + cumOper) == 0) { count++; cumOper -= sign; } signi = (csum[i] + cumOper) / abs(csum[i] + cumOper); if (signi != sign) { sign = signi; continue; } if (sign == 1) { count += (csum[i] + cumOper + 1); cumOper += (-1) * (csum[i] + cumOper + 1); } else { count += (-(csum[i] + cumOper) + 1); cumOper += (1) * (-(csum[i] + cumOper) + 1); } sign = -sign; } return count; } int main() { int N = in(); vector<int> vec; vector<int> csum; vec.push_back(in()); csum.push_back(vec.back()); for (auto i = 1; i < N; i++) { vec.push_back(in()); csum.push_back(csum.back() + vec.back()); } int tempp; int tempn; tempp = solve(csum, N, 1); tempn = solve(csum, N, -1); cout << min(tempp, tempn) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; public class Main { private static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int n = sc.nextInt(); long[] a = new long[n]; for (int i = 0;i < n;i++) a[i] = sc.nextLong(); long ret = calc(a,true); ret = Math.min(calc(a,false),ret); System.out.println(ret); } private static long calc(long[] a, boolean b) { long sum = a[0]; long ret = 0; if (sum==0) { sum = 1; ret++; } if (b) { ret = Math.abs(sum)+1; if (sum<0) { sum = 1; } else { sum = -1; } } long tmp = 0; for (int i = 1;i < a.length;i++) { long num = a[i]; tmp = sum; sum += num; if ((tmp<0&&sum>=0)||(tmp>=0&&sum<0)) continue; long l = Math.abs(sum)+1; if (sum>=0) { sum -= l; } else { sum += l; } ret += l; } if (sum==0) ret++; return 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
UNKNOWN
N = gets.to_i A = gets.split.map(&:to_i) sum = A[0] ans = 0 A[1..-1].each do |a| nsum = sum + a v = sum * nsum if v >= 0 if sum > 0 nsum = -1 else nsum = 1 end ans += (a - (sum + 1)).abs end sum = nsum end puts 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())) tmp = a[0] ans = 0 for i in range(1, n): if tmp * (tmp + a[i]) < 0: tmp += a[i] else: ans += abs(tmp + a[i]) + 1 if tmp < 0: tmp = 1 else: tmp = - 1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) As = list(map(int, input().split())) """ i番目を+1すると、 i+k番目は+(k+1)される 回数を保持しておいて、後で足す? """ from itertools import accumulate acc = list(accumulate(As)) # print(acc) prev = acc[0] ans = 0 cnt = 0 #累積カウント for a in acc[1:]: a += cnt # print("cnt",cnt,"pre",prev,"a",a) if prev > 0 and a >= 0: cnt -= (a+1) ans += a+1 prev = -1 elif prev < 0 and a <= 0: cnt += abs(a-1) ans += abs(a-1) prev = 1 else: prev = 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
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); } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n, ci, counter = 0; bool isPositive; cin >> n; vector<int> a(n); for (int(i) = (0); (i) <= (n - 1); ++(i)) cin >> a[i]; ci = a[0]; long long counter1, counter2; counter1 = counter2 = 0; isPositive = true; for (int(i) = (1); (i) <= (n - 1); ++(i)) { if (ci == 0) { ci += 1; ++counter1; } ci += a[i]; if (isPositive && ci > 0) { counter1 += abs(ci) + 1; ci -= abs(ci) + 1; } else if (!isPositive && ci < 0) { counter1 += abs(ci) + 1; ci += abs(ci) + 1; } else if (ci == 0) { if (isPositive) { --ci; ++counter1; } else { ++ci; ++counter1; } } isPositive = !isPositive; } ci = a[0]; isPositive = false; for (int(i) = (1); (i) <= (n - 1); ++(i)) { if (ci == 0) { ci -= 1; ++counter2; } ci += a[i]; if (isPositive && ci > 0) { counter2 += abs(ci) + 1; ci -= abs(ci) + 1; } else if (!isPositive && ci < 0) { counter2 += abs(ci) + 1; ci += abs(ci) + 1; } else if (ci == 0) { if (isPositive) { --ci; ++counter2; } else { ++ci; ++counter2; } } isPositive = !isPositive; } counter = min(counter1, counter2); cout << counter << 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())) ans1 = 0 sum = A[0] if A[0] != 0 else 1 # A[0]=0 のときは1にする。下で-1にするパターンも試している。なお0は不可。 for a in A[1:]: if (sum + a) * sum < 0: sum += a else: nextsum = 1 if sum < 0 else -1 # fugo = sum // abs(sum) 割り算のときは0に気をつけろ! a_should_be = nextsum - sum dif = abs(a_should_be - a) sum = nextsum ans1 += dif #A[0]を反転したほうがいいパターンの処理 ans2 = abs(A[0]) + 1 sum = 1 if A[0] < 0 else -1 #ある意味、ここでA[0]=0のときに-1をおいていることをやっている for a in A[1:]: if (sum + a) * sum < 0: sum += a else: nextsum = 1 if sum < 0 else -1 a_should_be = nextsum - sum dif = abs(a_should_be - a) sum = nextsum ans2 += dif print(min(ans1, ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = list(map(int, input().split())) ans = 1e16 for s in (1, -1): res, acc = 0, 0 for a in A: acc += a if acc * s <= 0: res += abs(acc-s) acc = s s *= -1 print("%d " % acc, end="") print() ans = min(ans, res) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } sc.close(); // +-+-+...の場合 long sum1 = a[0]; long count1 = 0; for (int i = 1; i < n; i++) { sum1 += a[i]; if (i % 2 == 0) { if (sum1 <= 0) { count1 += (1 - sum1); sum1 = 1; } } else { if (0 <= sum1) { count1 += (sum1 + 1); sum1 = -1; } } } // -+-+_...の場合 long sum2 = a[0]; long count2 = 0; for (int i = 1; i < n; i++) { sum2 += a[i]; if (i % 2 == 0) { if (0 <= sum2) { count2 += (sum2 + 1); sum2 = -1; } } else { if (sum2 <= 0) { count2 += (1 - sum2); sum2 = 1; } } } System.out.println(Math.min(count1, count2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) A=list(map(int,input().split())) a,memo1=A[0],0 if a<0: memo1+=1-a a=1 for i in range(2,n): if a*(a+A[i])<=-1:a=a+A[i] else: memo1+=abs(a+A[i])+1 if a>=1:a=-1 else:a=-1 a,memo2=A[0],0 if a>0: memo2+=a+1 a=-1 for i in range(2,n): if a*(a+A[i])<=-1:a=a+A[i] else: memo2+=abs(a+A[i])+1 if a>=1:a=-1 else:a=-1 print(min(memo1,memo2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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(L): # L[0]!=0を起点とする cnt = 0 s = L[0] for i in range(1,len(L)): a = L[i] if(s>0 and s+a>=0): L[i] = -s-1 cnt += (s+a+1) s = -1 elif(s<0 and s+a<=0): L[i] = -s+1 cnt += (-s-a+1) s = 1 else: s += a return cnt def ans(L): a = L[0] c0,c1=0,0 if (a>0): c0 = resolve(L) c1 = (a+1) + resolve([-1]+L[1:]) elif (a<0): c0 = resolve(L) c1 = (-a+1) + resolve([1]+L[1:]) else: c0 = 1 + resolve([1]+L[1:]) c1 = 1 + resolve([-1]+L[1:]) return(min(c0,c1)) def main(): N = int(input()) List = [int(x) for x in input().split(' ')] print(ans(List)) main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) ans=0 a=list(map(int,input().split())) sum=a[0] for i in range(1,n): if (sum+a[i])*sum<0:continue else: if sum+a[i]>=1: ans+=abs(sum+a[i]+1) a[i]-=sum+a[i]+1 sum=-1 elif sum+a[i]<=-1: ans+=abs(sum+a[i]+1) a[i]+=sum+a[i] sum=1 else: if sum<0: ans+=1 a[i]+=1 sum=1 else: ans+=1 a[i]-=1 sum=-1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1000000; int main() { int n; cin >> n; vector<int> a(n); for (long long int i = 0; i < n; i++) cin >> a[i]; int ans_plus = 0; vector<int> sum_plus(n); if (a[0] > 0) { sum_plus[0] = a[0]; } else { ans_plus += 1 - a[0]; sum_plus[0] = 1; } for (int i = 0; i < n - 1; ++i) { sum_plus[i + 1] = sum_plus[i] + a[i]; if (sum_plus[i + 1] * sum_plus[i] > 0) { if (sum_plus[i] > 0) { ans_plus += sum_plus[i] + 1; sum_plus[i] = -1; } else if (sum_plus[i] < 0) { ans_plus += abs(sum_plus[i] - 1); sum_plus[i] = 1; } } else if (sum_plus[i + 1] == 0) { if (sum_plus[i] > 0) { sum_plus[i + 1] = -1; } else if (sum_plus[i] < 0) { sum_plus[i + 1] = 1; } ans_plus++; } } int ans_minus = 0; vector<int> sum_minus(n); if (a[0] < 0) { sum_minus[0] = a[0]; } else { ans_minus += 1 + a[0]; sum_minus[0] = -1; } for (int i = 0; i < n - 1; ++i) { sum_minus[i + 1] = sum_minus[i] + a[i]; if (sum_minus[i + 1] * sum_minus[i] > 0) { if (sum_minus[i] > 0) { ans_minus += sum_minus[i] + 1; sum_minus[i] = -1; } else if (sum_minus[i] < 0) { ans_minus += abs(sum_minus[i] - 1); sum_minus[i] = 1; } else if (sum_minus[i + 1] == 0) { if (sum_minus[i] > 0) { sum_minus[i + 1] = -1; } else if (sum_minus[i] < 0) { sum_minus[i + 1] = 1; } ans_minus++; } } } std::cout << min(ans_minus, ans_plus) << 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
UNKNOWN
using System; using System.Linq; namespace ABC059 { class C { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); int[] a = Console.ReadLine().Split().Select(int.Parse).ToArray(); int sum = 0; bool isCompleted = true; // 入力段階で条件が成立しているか確認 for (int i = 0; i < n - 1; i++) { sum += a[i]; if (0 <= sum && 0 <= sum + a[i + 1]) isCompleted = false; if (sum <= 0 && sum + a[i + 1] <= 0) isCompleted = false; } if (sum == 0) isCompleted = false; if (isCompleted) { Console.WriteLine(0); return; } // メイン処理 int count = 0; sum = 0; for (int i = 0; i < n - 1; i++) { sum += a[i]; if (0 <= sum && 0 <= sum + a[i + 1]) { while (0 <= sum + a[i + 1]) { a[i + 1] -= 1; count++; } } if (sum <= 0 && sum + a[i + 1] <= 0) { while (sum + a[i + 1] <= 0) { a[i + 1] += 1; count++; } } } if (sum == 0) { count++; } Console.WriteLine(count); } } }