Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> A(n, 0); for (int i = 0; i<n; i++) cin >> A[i]; long long cnt1=0, acm1=0, cnt2=0, acm2=0; for(int i = 0; i<n; i++) { acm1+=A[i]; acm2+=A[i]; if(i%2) { if(acm1>0); else { cnt1 += abs(acm1) + 1; acm1 = 1; } if(acm2<0); else { cnt2 += abs(acm2) + 1; acm2 = -1; } } else { if(acm1<0); else { cnt1 += abs(acm1) + 1; acm1 = -1; } if(acm2>0); else { cnt2 += abs(acm2) + 1; acm2 = 1; } } cout << min(cnt1, cnt2) << "\n"; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) *a, = map(int, input().split()) is_plus = False if a[0] > 0 else True s = a[0] ans = 0 for i in range(1, n): t = 0 if is_plus and s+a[i] < 0: t = abs(s-a[i]) - 1 a[i] += t elif not is_plus and s+a[i] > 0: t = abs(s+a[i]) + 1 a[i] -= t s += a[i] ans += t is_plus = not is_plus if s != 0: print(ans) else: print(ans-1 if a[-1] < 0 else 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
python3
n = int(input()) a = list(map(int, input().split())) a1 = [0] * n a1[0] = a[0] 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 b * (b + a[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]) else: a1[i] = a[i] b += a1[i] a2 = [0] * n a2[0] = -f(a[0]) ans1 = abs(a2[0] - a[0]) b1 = a2[0] for i in range(1, n): if b1 * (b1 + a[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]) else: 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a; cin >> a; int sum = 0; int x = 0; if (a > 0) { sum += a; for (int t = 1; t < n; t++) { int temp; cin >> temp; sum += temp; if (t % 2 == 1 && sum >= 0) { int s = sum + 1; sum = -1; x += s; } else if (t % 2 == 0 && sum <= 0) { int s = 1 - sum; sum = 1; x += s; } } } else { sum += a; for (int t = 1; t < n; t++) { int temp; cin >> temp; sum += temp; if (t % 2 == 0 && sum >= 0) { int s = sum + 1; sum = -1; x += s; } else if (t % 2 == 1 && sum <= 0) { int s = 1 - sum; sum = 1; x += s; } } } cout << x << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
#!usr/bin/env python3 from collections import defaultdict from collections import deque from heapq import heappush, heappop import sys import math import bisect import random import itertools sys.setrecursionlimit(10**5) stdin = sys.stdin bisect_left = bisect.bisect_left bisect_right = bisect.bisect_right def LI(): return list(map(int, stdin.readline().split())) def LF(): return list(map(float, stdin.readline().split())) def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split())) def II(): return int(stdin.readline()) def IF(): return float(stdin.readline()) def LS(): return list(map(list, stdin.readline().split())) def S(): return list(stdin.readline().rstrip()) def IR(n): return [II() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] def FR(n): return [IF() for _ in range(n)] def LFR(n): return [LI() for _ in range(n)] def LIR_(n): return [LI_() for _ in range(n)] def SR(n): return [S() for _ in range(n)] def LSR(n): return [LS() for _ in range(n)] mod = 1000000007 inf = float('INF') #A def A(): a = input().split() a = list(map(lambda x: x.capitalize(), a)) a,b,c = a print(a[0]+b[0]+c[0]) return #B def B(): a = II() b = II() if a > b: print("GREATER") if a < b: print("LESS") if a == b: print("EQUAL") return #C def C(): II() a = LI() def f(suma, b): for i in a[1:]: if (suma + i) * suma < 0: suma += i continue b += abs(suma + i) + 1 suma = -1 * (suma > 0) or 1 return b if a[0] == 0: ans = f(1, 1) else: ans = f(a[0], 0) if a[0] == 0: ans = min(ans, f(-1, 1)) else: ans = min(ans, f(-a[0], 2 * abs(a[0]))) print(ans) return #D def D(): s = S() for i in range(len(s) - 1): if s[i] == s[i+1]: print(i + 1, i + 2) return for i in range(len(s) - 2): if s[i] == s[i + 2]: print(i + 1, i + 3) return print(-1, -1) return #Solve if __name__ == '__main__': C()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1 << 29; const long long MOD = 1000000007; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int n; cin >> n; long long a[100100]; for (int i = 0; i < n; ++i) { cin >> a[i]; } long long count1 = 0; long long su1 = 0; for (int j = 0; j < n; ++j) { su1 += a[j]; if (j % 2 == 0 && su1 <= 0) { count1 += -su1 + 1; su1 = 1; } else if (j % 2 == 1 && su1 >= 0) { count1 += su1 + 1; su1 = -1; } } long long count2 = 0; long long su2 = 0; for (int j = 0; j < n; ++j) { su1 += a[j]; if (j % 2 == 0 && su2 >= 0) { count1 += -su2 + 1; su2 = 1; } else if (j % 2 == 1 && su2 <= 0) { count2 += -su2 + 1; su2 = 1; } } cout << min(su1, su2) << 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; typedef std::priority_queue<int> IntPrioQueue; typedef std::priority_queue<int, std::vector<int>, std::greater<int> > IntReversePrioQueue; int dx4[4] = {1, 0, -1, 0}; int dy4[4] = {0, 1, 0, -1}; int dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1}; int dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1}; void solve(void) { int n; cin >> n; long long accsums1[n]; long long accsums2[n]; long long temp0; scanf("%lld\n", &temp0); accsums1[0] = accsums2[0] = temp0; for (int i = 0; i <= n - 1 - 1; i++) { long long temp; scanf("%lld\n", &temp); accsums1[i + 1] = temp + accsums1[i]; accsums2[i + 1] = temp + accsums1[i]; } long long ans1 = 0; for (int i = 0; i <= n - 1; i++) { if ((i % 2 == 0 and accsums1[i] > 0) or (i % 2 != 0 and accsums1[i] < 0)) continue; if (i % 2 == 0) { long long diff = 1 - accsums1[i]; ans1 += diff; for (int j = i + 1; j <= n - 1; j++) accsums1[j] += diff; } else { long long diff = 1 + accsums1[i]; ans1 += diff; for (int j = i + 1; j <= n - 1; j++) accsums1[j] -= diff; } } long long ans2 = 0; for (int i = 0; i <= n - 1; i++) { if ((i % 2 == 0 and accsums2[i] < 0) or (i % 2 != 0 and accsums2[i] > 0)) continue; if (i % 2 != 0) { long long diff = 1 - accsums2[i]; ans2 += diff; for (int j = i + 1; j <= n - 1; j++) accsums2[j] += diff; } else { long long diff = 1 + accsums2[i]; ans2 += diff; for (int j = i + 1; j <= n - 1; j++) accsums2[j] -= diff; } } cout << min(ans1, ans2) << '\n'; } int main(void) { solve(); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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); bool fla = false; for (int i = 0; i < N; i++) { if (a.at(i) != 0) { if ((a.at(i) > 0) && (i % 2 == 0)) fla = true; else if (i % 2 == 1) fla = true; break; } } int t = 0, res = 0; for (int i = 0; i < N; i++) { int b = a.at(i); if (fla) { if (t + b <= 0) { b = t * -1 + 1; res += b - a.at(i); } } else { if (t + b >= 0) { b = t * -1 - 1; res += abs(b - a.at(i)); } } t += b; fla = !fla; } cout << res << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n), pref(n + 1); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { pref[i + 1] = pref[i] + a[i]; } long long ans = 0; vector<long long> mod(n + 1); long long res = 0; for (int i = 2; i <= n; i++) { mod[i] += mod[i - 1]; long long now = pref[i] + mod[i]; long long prev = pref[i - 1] + mod[i - 1]; if (now == 0) { if (prev > 0) { res += 1; mod[i] -= 1; } else { res += 1; mod[i] += 1; } } else { if (prev > 0 && now > 0) { res += now + 1; mod[i] -= now + 1; } if (prev < 0 && now < 0) { res += 1 - now; mod[i] += 1 - now; } } } cout << res << '\n'; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (ll i = 0; i < n; i++) #define repl(i, l, r) for (ll i = (l); i < (r); i++) #define fi first #define se second #define all(x) (x).begin(), (x).end() #define CST(x) cout << fixed << setprecision(x) using ll = long long; using vl = vector<ll>; using vvl = vector<vector<ll>>; using pl = pair<ll, ll>; const ll MOD = 1000000007; const int inf = 1e9 + 10; const ll INF = 4e18; ll f(ll a, ll c, ll d) { ll p = a / c, q = a / d, r = a / (c / __gcd(c, d) * d); return a - (p + q - r); } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); int n; cin >> n; ll a[n]; rep(i, n) cin >> a[i]; ll now = max(1LL, a[0]), ans = (now == a[0] ? 0 : 1 - now); ll b[n]; b[0] = now; rep(i, n - 1) { now += a[i + 1]; if (now * b[i] < 0) b[i + 1] = now; else if (b[i] > 0) { now = -1, b[i + 1] = now; ans += abs(b[i] + 1 + a[i + 1]); } else { now = 1, b[i + 1] = now; ans += abs(1 - b[i] - a[i + 1]); } } now = min(-1LL, a[0]); ll ans1 = (now == a[0] ? 0 : 1 + now); b[0] = now; rep(i, n - 1) { now += a[i + 1]; if (now * b[i] < 0) b[i + 1] = now; else if (b[i] > 0) { now = -1, b[i + 1] = now; ans1 += abs(b[i] + 1 + a[i + 1]); } else { now = 1, b[i + 1] = now; ans1 += abs(1 - b[i] - a[i + 1]); } } cout << min(ans, ans1) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; int func(vector<long long> a, int fugo) { long long ans = 0; long long offset = 0; for (int i = 0; i < n; i++) { if (i % 2 == fugo) { if (a[i] <= offset) { ans += offset - (a[i] - 1); offset = a[i] - 1; } } else { if (a[i] >= offset) { ans += (a[i] + 1) - offset; offset = a[i] + 1; } } printf("[%d]", a[i]); } printf("%d ", ans); return ans; } int main() { cin >> n; vector<long long> a; int sum_tmp = 0; for (int i = 0; i < n; i++) { int tmp; cin >> tmp; sum_tmp += tmp; a.push_back(sum_tmp); } int ans = min(func(a, 0), func(a, 1)); cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using std::cin; using std::cout; using std::endl; using std::min; using std::vector; static long solve(int N, vector<int> &a) { long count_a = 0; long count_b = 0; int sum_a = 0; int sum_b = 0; int sign_a = 1; int sign_b = -1; for (int n = 0; n < N; n++) { int val = a[n]; sum_a += val; sum_b += val; if (sum_a * sign_a <= 0) { count_a += 1 - (sum_a * sign_a); sum_a = sign_a; } if (sum_b * sign_b <= 0) { count_b += 1 - (sum_b * sign_b); sum_b = sign_b; } sign_a = -sign_a; sign_b = -sign_b; } return min(count_a, count_b); } int main() { int N; cin >> N; vector<int> a(N); for (int n = 0; n < N; n++) { cin >> a[n]; } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <stdio.h> #include<math.h> int main(void) { // your code goes here long int a[100000]; long int n,count= 0,count1=0; scanf("%d",&n); int i; for(i = 0;i<n;i++) { scanf("%ld",&a[i]); }; long int new = a[0]+a[1]; if(new==0) { new = -1; count++;} int pos; if(new <0||new == 0) pos= 0; else if(new>0) pos = 1; for(i = 2;i<n;i++) { new = a[i]+new; // printf("%d",new); pos = !pos; if(new>0&& pos==0) {count+=abs(new)+1; new= -1;} else if(new<0 && pos ) {count+=abs(new)+1; new = 1; } else if(new ==0) { count++; if(pos==0) new = -1; else new = 1; } } count1+=abs(a[0]+a[1])+1; if(a[0]+a[1]<0) new = 1; else if(a[0]+a[1]>0 ||a[0]+a[1]==0) { new = 1; count1++;} for(i = 2;i<n;i++) { new = a[i]+new; // printf("%d",new); if(new>0&& pos==0) {count1+=abs(new)+1; new= -1;} else if(new<0 && pos ) {count1+=abs(new)+1; new = 1; } else if(new ==0) { count1++; if(pos==0) new = -1; else new = 1; } pos =!pos; } printf("%ld",count<count1?count:count1); 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 op = 0; long long sum = 0LL; cin >> sum; for (int i = 1; i < n; i++) { long long a; cin >> a; if (!(sum * (sum + a) < 0)) { long long tmp_a = sum < 0 ? abs(sum) + 1 : -1 * (abs(sum) + 1); op += abs(tmp_a - a); sum = sum + tmp_a; } else { sum += a; } } cout << op << 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())) s = A[0] ans1 = 0 for k in range(1,N): if s*(s+A[k])<=-1: s += A[k] else: if s > 0: if s + A[k] >= 0: ans1 += s + A[k] + 1 s = -1 elif s < 0: if s + A[k] <= 0: ans1 += 1 - s - A[k] s = 1 if A[0] > 0: ans2 = A[0] + 1 A[0] = -1 for k in range(1,N): if s*(s+A[k])<=-1: s += A[k] else: if s > 0: if s + A[k] >= 0: ans2 += s + A[k] + 1 s = -1 elif s < 0: if s + A[k] <= 0: ans2 += 1 - s - A[k] s = 1 else: ans2 = 1 - A[0] A[0] = 1 for k in range(1,N): if s*(s+A[k])<=-1: s += A[k] else: if s > 0: if s + A[k] >= 0: ans2 += s + A[k] + 1 s = -1 elif s < 0: if s + A[k] <= 0: ans2 += 1 - s - A[k] s = 1 print(min(ans1,ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define rep(i, n) REP(i, 0, n) #define ALL(v) v.begin(), v.end() #define MSG(a) cout << #a << " " << a << endl; #define REP(i, x, n) for (int i = x; i < n; i++) #define OP(m) cout << m << endl typedef long long ll; typedef unsigned long long ull; int main() { int n; cin >> n; int a[n]; rep(i, n) cin >> a[i]; ll cnt1 = 0, cnt2 = 0; ll sum = a[0]; rep(i, n) { if (i % 2 == 0 && sum < 0) sum = 1; //符号がプラスになるべきところ。 else if (i % 2 == 1 && sum > 0) sum = -1; //符号がマイナスになるべきところ。 cnt1 += abs(sum) + 1; } ll sum = a[0]; rep(i, n) { if (i % 2 == 0 && sum > 0) sum = -1; //符号マイナスになるべきところ。 else if (i % 2 == 1 && sum < 0) sum = 1; //符号がプラスになるべきところ。 cnt2 += abs(sum) + 1; } OP(min(cnt1, cnt2)); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double pie = acos(-1.0); template <typename T> T Max(T x, T y) { return (x > y) ? x : y; } template <typename T> T Min(T x, T y) { return (x > y) ? y : x; } int gcd(int n1, int n2) { if (n2 != 0) return gcd(n2, n1 % n2); else return n1; } template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } clock_t time_p = clock(); void rtime() { time_p = clock() - time_p; cerr << "Time Taken : " << (float)1000 * (time_p) / CLOCKS_PER_SEC << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; long long int arr[n]; for (int i = 0; i < int(n); i++) cin >> arr[i]; long long int ans = 0; long long int sum = arr[0]; for (int i = 1; i < n; i++) { if (sum < 0) { sum = sum + arr[i]; if (sum > 0) continue; else { ans = ans + abs(sum) + 1; sum = 1; } } else { sum = sum + arr[i]; if (sum < 0) continue; else { ans = ans + sum + 1; sum = -1; } } } cout << ans; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int A[100100]; for (int i = 0; i < N; ++i) { cin >> A[i]; } int mode; if (A[0] > 0) mode = 0; else mode = 1; int ans = 0; int total = A[0]; for (int i = 1; i < N; ++i) { mode ^= 1; int _total = total + A[i]; if (mode == 0 && _total <= 0 || mode == 1 && _total >= 0) { _total = mode == 0 ? 1 : -1; ans += abs(_total - (total + A[i])); } total = _total; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
open Batteries let () = let n = Scanf.scanf "%d " (fun a -> a) in let a_lst = Array.to_list @@ Array.init n (fun _ -> Scanf.scanf "%d " (fun a -> a)) in let rec aux sum sign cnt l = match l with | [] -> cnt | hd :: tl -> match sign with | `Plus -> if sum + hd <= 0 then aux sum sign (cnt+1) (hd+1::tl) else aux (sum+hd) `Minus cnt tl | `Minus -> if sum + hd >= 0 then aux sum sign (cnt+1) (hd-1::tl) else aux (sum+hd) `Plus cnt tl in Printf.printf "%d\n" @@ aux 0 (if List.hd a_lst > 0 then `Plus else `Minus) 0 a_lst
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; class Main { int n; int[] a; public static void main(String[] args) { Scanner sc = new Scanner(System.in); Main m = new Main(sc); m.solve(); sc.close(); } Main(Scanner sc) { n = sc.nextInt(); a = new int[n]; for(int i=0;i<n;i++){ a[i] = sc.nextInt(); } } void solve() { int cnt1 = (a[0]>0)?0:(Math.abs(a[0])+1); int sign = 1; long sum = (a[0]>0)?a[0]:1; for(int i=1;i<n;i++){ sum += a[i]; if(sum*sign>=0){ cnt1 += Math.abs(sum) + 1; sum = -sign; } sign *= -1; } int cnt2 = (a[0]<0)?0:(Math.abs(a[0])+1); sign = -1; sum = (a[0]<0)?a[0]:-1; for(int i=1;i<n;i++){ sum += a[i]; if(sum*sign>=0){ cnt2 += Math.abs(sum) + 1; sum = -sign; } sign *= -1; } System.out.println(Math.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
python3
n = int(input()) a = list(map(int, input().split())) l = len(a) b = [] for i in range(l): b.append(a[i]) ans = 0 Ans = 0 summary = a[0] Summary = b[0] if(summary == 0): a[0] = 1 ans+= 1 b[0] = -1 else: b[0] = int(-a[0]/ abs(a[0])) Ans+= abs(a[0]- b[0]) 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] 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
cpp
#include <iostream> #include <vector> #include <string> #include <cstring> #include <math.h> #include <limits.h> #include <map> #include <algorithm> #include <functional> using namespace std; int main() { int n; vector<long long> A; int j; bool is_plus; long long ans = 0; long long sum = 0; cin >> n; S.push_back(0); for ( int i = 0; i < n; i++ ) { long long a; cin >> a; A.push_back(a); } // for ( j = 0; j < n; j++ ) { // if ( abs(A[j]) ) { break; } // } // if ( j == n ) { // cout << A.size()*2-1 << endl; // return 0; // } // if ( j ) { // ans += ( j+1 )*2 - 1; // sum = ( A[j] > 0 ) ? -1: 1; // } // else { // sum = 0; // ans = 0; // } for ( int i = 0; i < n; i++ ) { if ( !i ) { sum = A[i]; continue; } bool is_plus = sum > 0; sum += A[i]; if ( sum == 0 ) { ans += 1; sum = is_plus ? -1 : 1; } else if ( is_plus == (sum > 0) ) { ans += abs(sum)+1; sum = is_plus ? -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; const long long INF = (long long)1e9; const long long MOD = (long long)1e9 + 7; vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1}; int main() { long long N, cnt = 0; bool F = false; cin >> N; long long a[N]; for (long long i = 0; i < N; i++) cin >> a[i]; long long sum = 0; if (a[0] == 0) { a[0]++; cnt++; F = true; } bool f; if (a[0] > 0) f = false; else f = true; for (long long i = 0; i < N; i++) { sum += a[i]; if (sum >= 0 && f) { cnt += abs(sum) + 1; sum = -1; } else if (sum <= 0 && !f) { cnt += abs(sum) + 1; sum = 1; } if (sum > 0) f = true; else f = false; } if (F) { sum = 0; long long CNT = 1; a[0] = -1; if (a[0] > 0) f = false; else f = true; for (long long i = 0; i < N; i++) { sum += a[i]; if (sum >= 0 && f) { CNT += abs(sum) + 1; sum = -1; } else if (sum <= 0 && !f) { CNT += abs(sum) + 1; sum = 1; } if (sum > 0) f = true; else f = false; } cnt = min(CNT, cnt); } 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; #define mod 1000000007 #define ll long long #define mp make_pair #define pb push_back #define ff first #define ss second #define set0(a) memset ((a), 0 , sizeof(a)) #define set1(a) memset((a),-1,sizeof (a)) #define pi pair<int, int> #define ps pair<string, string> #define pl pair<long, long> #define pll pair<long long, long long> #define vll vector<long long> #define vl vector<long> #define vi vector<int> #define vs vector<string> #define vps vector< ps > #define vpi vector< pi > #define vpl vector< pl > #define vpll vector< pll > #define flash ios_base::sync_with_stdio(false); cin.tie(NULL); #define tc(t) for(long long l=0;l<t;l++) #define rep(i,s,n,d) for(long long i=s;i<n;i=i+d) bool sortbysec(const pll &a, const pll &b) { return (a.second < b.second); } void func(void) { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); } int main(){ ll n; cin>>n; ll a[n]; rep(i,0,n,1){ cin>>a[i]; } ll sum[n]={}; sum[0]=a[0]; rep(i,1,n,1){ sum[i]=sum[i-1]+a[i]; } ll sum1=a[0]; ll count1=0; rep(i,1,n,1){ if(sum1*(sum1+a[i])>=0){ ll d=1; if(sum1<0){ d=1; }else{ d=-1; } int dif=abs(sum1+a[i]-d); count1=count1+dif; sum1=d; } else{ sum1=sum1+a[i]; } } cout<<count1<<endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MX = 1e5 + 5, INF = 5 << 28, MOD = 1e9 + 7; long long N; vector<long long> A; void input() { cin >> N; A.resize(N); for (long long i = (long long)(0); i <= (long long)(N - 1); ++i) { cin >> A[i]; } } void solve() { long long ans = INF; long long fugo; for (long long fg = (long long)(0); fg <= (long long)(1); ++fg) { if (fg == 1) { fugo = 1; } else fugo = 0; long long prev = 0; long long s = 0; long long ans1 = 0; for (long long i = (long long)(0); i <= (long long)(N - 1); ++i) { s += A[i]; if (fugo) { if (s > 0) { ans1 += 0; } else if (s == 0) { ans1 += 1; s = 1; } else { ans1 += abs(s) + 1; s = 1; } } else { if (s > 0) { ans1 += (abs(s) + 1); s = -1; } else if (s == 0) { ans1 += 1; s = -1; } else { ans1 += 0; } } prev = s; fugo ^= 1; } ans = min(ans1, ans); } cout << ans << endl; } signed main() { input(); solve(); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) import copy a2 = copy.copy(a) ans1 = 0 #偶数インデックスが正 for i in range(n): s1 = sum(a[:i+1]) if i % 2 == 0: if s1 <= 0: x = abs(s1) + 1 ans1 += x a[i] += x else: continue else: if s1 >= 0: x = abs(s1) + 1 ans1 += x a[i] -= x ans2 = 0 #偶数インデックスが負 for i in range(n): s2 = sum(a2[:i+1]) if i % 2 == 1: if s2 <= 0: x = abs(s2) + 1 ans2 += x a2[i] += x else: continue else: if s2 >= 0: x = abs(s2) + 1 ans2 += x a2[i] -= x 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())) s = [a[0]] for i in range(1, n): s.append(s[-1]+a[i]) cnt_a = 0 base = 0 for i in range(n): if (base + s[i] > 0) != (i % 2 == 0): cnt_a += (1 + abs(base + s[i])) base += (1 + abs(base + s[i])) * (-1) ** i cnt_b = 0 base = 0 for i in range(n): if (base + s[i] > 0) == (i % 2 == 0): cnt_b += (1 + abs(base + s[i])) base -= (1 + abs(base + s[i])) * (-1) ** i print(min(cnt_a, cnt_b))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, a[100005], dp[100005]; cin >> n; int sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; dp[i] = sum; } int diff = 0, ans = 0; for (int i = 1; i < n; i++) { if (dp[i] + diff == 0) { if (dp[i - 1] + diff < 0) diff++, ans++; if (dp[i - 1] + diff >= 0) diff--, ans++; } if ((dp[i - 1] + diff) / abs(dp[i - 1] + diff) == (dp[i] + diff) / abs(dp[i] + diff)) { if (dp[i] + diff >= 0) { ans += abs(dp[i] + diff) + 1; diff -= abs(dp[i] + diff) + 1; } else { ans += abs(dp[i] + diff) + 1; diff += abs(dp[i] + diff) + 1; } } } if (dp[n - 1] == 0) { if (dp[n - 2] + diff < 0) ans++; if (dp[n - 2] + diff >= 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
python3
# nの把握 n = int(input()) # 数列の把握 AL = list(map(int, input().split())) """ 条件1:すべてのi(1≦i≦n) に対し、第 1 項から第 i 項までの和は 0 でない 条件2:すべてのi(1≦i≦n−1) に対し、i 項までの和と i+1 項までの和の符号が異なる 条件2より、i項までの総和とi+1項までの総和は、正負又は負正という順番で進行する。 条件を満たさないとき、iについて、総和が1or-1となるまで手順を実施する。 """ #正負を確かめる関数 def PN(AL): ISUM = 0 P = 0 for i in range(n): ISUM += AL[i] if i % 2 == 0: if ISUM > 0: pass else: P += abs(1 - ISUM) ISUM = 1 else: if ISUM < 0: pass else: P += abs(-1 - ISUM) ISUM = 1 return P #負正を確かめる関数 def NP(AL): ISUM = 0 P = 0 for i in range(n): ISUM += AL[i] if i % 2 == 0: if ISUM < 0: pass else: P += abs(-1 - ISUM) ISUM = -1 else: if ISUM > 0: pass else: P += abs(1- ISUM) ISUM = 1 return P ANSWER = min(PN(AL), NP(AL)) print(ANSWER)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> template <class T, class S> void cmin(T &a, const S &b) { if (a > b) a = b; } template <class T, class S> void cmax(T &a, const S &b) { if (a < b) a = b; } using namespace std; signed main() { long long int n; cin >> n; vector<long long int> v(n); bool flag = false; for (long long int i = 0; i < n; i++) cin >> v[i]; vector<long long int> sum(n); long long int ans = 0; for (long long int i = 0; i < n; i++) sum[i] = v[i]; for (long long int i = 0; i < n; i++) { if (!i) { if (sum[0] >= 0) flag = true; else flag = false; continue; } sum[i] += sum[i - 1]; if (flag) { if (sum[i] < 0) flag = false; else { ans += (abs(sum[i]) + 1); sum[i] = -1; flag = false; } } else { if (sum[i] > 0) flag = true; else { ans += (abs(sum[i]) + 1); sum[i] = 1; flag = true; } } } 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 (int i = 0; i < N; i++) cin >> A.at(i); int sum = A.at(0); int ans = 0; for (int i = 1; i < N; i++) { if (sum == 0) continue; if (sum > 0 && sum + A.at(i) >= 0) { ans += abs(sum + A.at(i)) + 1; sum = -1; } else if (sum < 0 && sum + A.at(i) <= 0) { ans += abs(sum + A.at(i)) + 1; sum = 1; } else sum += A.at(i); } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const long long inf = 5e15; struct query { long long type; long long value; query(long long a = 0, long long b = 0) : type(a), value(b) {} }; long long INF = (1LL << 60); struct segtree { long long SIZE; vector<query> s; vector<long long> t; vector<long long> u; segtree(long long n = 1) { SIZE = 1; while (SIZE < n) SIZE *= 2; s.clear(); t.clear(); u.clear(); s.resize(SIZE * 2, query()); t.resize(SIZE * 2, 0); u.resize(SIZE * 2, 0); } void func(long long k, long long l, long long r, query q) { if (q.type == 1) { if (s[k].type == 0) s[k] = q; else s[k].value += q.value; t[k] += q.value * (r - l); u[k] += q.value; } if (q.type == 2) { s[k] = q; t[k] = q.value * (r - l); u[k] = q.value; } } void compute(long long k, long long l, long long r) { query q = s[k]; s[k] = query(); if (q.type == 0 || r - l == 1) return; long long m = (l + r) / 2; func(k * 2 + 1, l, m, q); func(k * 2 + 2, m, r, q); } void Update(long long a, long long b, query x, long long k, long long l, long long r) { if (b <= l || r <= a) return; compute(k, l, r); if (a <= l && r <= b) { func(k, l, r, x); } else { long long m = (l + r) / 2; Update(a, b, x, k * 2 + 1, l, m); Update(a, b, x, k * 2 + 2, m, r); t[k] = t[k * 2 + 1] + t[k * 2 + 2]; u[k] = max(u[k * 2 + 1], u[k * 2 + 2]); } } long long Dfs(long long type, long long a, long long b, long long k, long long l, long long r) { if (b <= l || r <= a) { if (type == 1) return 0; if (type == 2) return -inf; } compute(k, l, r); if (a <= l && r <= b) { if (type == 1) return t[k]; if (type == 2) return u[k]; } else { long long m = (l + r) / 2; long long lv = Dfs(type, a, b, k * 2 + 1, l, m); long long rv = Dfs(type, a, b, k * 2 + 2, m, r); if (type == 1) return lv + rv; if (type == 2) return max(lv, rv); } } void Add(long long a, long long b, long long x) { Update(a, b, query(1, x), 0, 0, SIZE); } void Set(long long a, long long b, long long x) { Update(a, b, query(2, x), 0, 0, SIZE); } long long Getsum(long long a, long long b) { return Dfs(1, a, b, 0, 0, SIZE); } long long Getmax(long long a, long long b) { return Dfs(2, a, b, 0, 0, SIZE); } }; int main() { long long n; cin >> n; vector<pair<long long, long long> > a(n); for (long long i = 0; i < n; i++) { long long x, d; cin >> x >> d; a[i] = {x, x + d}; } sort(a.begin(), a.end()); segtree d(n + 1); d.Set(0, n + 1, 0); d.Set(n, n + 1, n); vector<long long> dp(n + 1); vector<long long> ans(n + 1); dp[0] = 1; ans[0] = dp[0]; for (long long i = 0; i < n; i++) { long long x = a[n - i - 1].second; long long it = upper_bound(a.begin(), a.end(), pair<long long, long long>(x, 0)) - a.begin(); if (it == n - i) { d.Set(n - i - 1, n - i, it); } else { d.Set(n - i - 1, n - i, max(d.Getmax(n - i, it), it)); } long long id = n - d.Getmax(n - i - 1, n - i); (dp[i + 1] = ans[id]) %= mod; (ans[i + 1] = ans[i] + dp[i + 1]) %= mod; } cout << ans[n] << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using llong = long long; void input(vector<int>& rvnNum) { int nSize; cin >> nSize; rvnNum.resize(nSize); for (int& rnElm : rvnNum) cin >> rnElm; } llong calcMinOpeTimes(const vector<int>& cnrvnNum) { vector<llong> vnOpeTimes(2); for (int nEvnOdd = 0; nEvnOdd < vnOpeTimes.size(); nEvnOdd++) { llong nOpeTimes = 0; llong nCumlSum = 0; for (int n = 0; n < cnrvnNum.size(); n++) { nCumlSum += cnrvnNum[n]; if ( n % 2 == nEvnOdd ) if ( nCumlSum > 0 ); else { nOpeTimes += 1 - nCumlSum; nCumlSum = 1; } else if ( nCumlSum < 0 ); else { nOpeTimes += nCumlSum - (-1); nCumlSum = -1; } else; } vnOpeTimes[nEvnOdd] = nOpeTimes; } auto itElm = min_element(begin(vnOpeTimes), end(vnOpeTimes)); return *itElm; } int main() { vector<int> vnNum; input(vnNum); cout << calcMinOpeTimes(vnNum) << 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 = 1e9; int main() { int n; cin >> n; int a[100010]; for (int i = (0); i < (int)n; i++) { cin >> a[i]; } long long ans = 0; long long sum = a[0]; for (int i = (1); i < (int)n; i++) { long long tmp; tmp = sum; sum += a[i]; if (sum >= 0 && tmp > 0) { if (sum < tmp) { ans += sum + 1; sum = -1; } else { ans += tmp + 1; sum = a[i] - 1; } } else if (sum <= 0 && tmp < 0) { if (sum > tmp) { ans += -sum + 1; sum = 1; } else { ans += -tmp + 1; sum = 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; using ll = long long; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int ans1 = 0; int sum = 0; for (int i = 0; i < n; i++) { int cur = a[i]; if (i % 2) { if (sum + cur > 0) cur = -(sum + 1); } else { if (sum + cur == 0) cur++; else if (sum + cur < 0) cur = -sum + 1; } sum += cur; ans1 += abs(a[i] - cur); } int ans2 = 0; sum = 0; for (int i = 0; i < n; i++) { int cur = a[i]; if (i % 2 == 0) { if (sum + cur > 0) cur = -(sum + 1); } else { if (sum + cur == 0) cur++; else if (sum + cur < 0) cur = -sum + 1; } sum += cur; ans2 += abs(a[i] - cur); } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) S = [] S.append(0) cnt = 0 P =[] x=0 y=0 for i in range(n): if a[i] != 0: if i%2 ==0: x=1 else: x=-1 if a[i] >0: y=1 else: y=-1 break if x*y>=0: P.append(1) else: P.append(-1) for i in range(n-1): P.append(-P[i]) for i in range(n): if (S[i] + a[i])*P[i]<=0: cnt += abs(P[i]-S[i]-a[i]) a[i] = P[i]-S[i] S.append(S[i]+a[i]) print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; int a[n], c[n]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) c[i] = a[i]; int count = 0, count2 = 0, sum = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (sum + a[i] <= 0) { int b = 1 - sum; count += abs(b - a[i]); a[i] = b; } sum += a[i]; } else { if (sum + a[i] >= 0) { int b = -1 - sum; count += abs(b - a[i]); a[i] = b; } sum += a[i]; } } sum = 0; for (int i = 0; i < n; i++) { if (i % 2 == 1) { if (sum + c[i] <= 0 || c[i] <= 0) { int b = 1 - sum; count2 += abs(b - c[i]); c[i] = b; } sum += c[i]; } else { if (sum + c[i] >= 0 || c[i] >= 0) { int b = -1 - sum; count2 += abs(b - c[i]); c[i] = b; } sum += c[i]; } } cout << min(count, count2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long cnt1 = 0, cnt2 = 0; long long sumv = a[0]; for (int i = 1; i < n; i++) { if (i % 2 == 0 && sumv < 0) { sumv = 1; cnt1 += abs(sumv) + 1; } else if (i % 2 == 1 && sumv > 0) { sumv = -1; cnt1 += abs(sumv) + 1; } sumv += a[i]; } sumv = a[0]; for (int i = 1; i < n; i++) { if (i % 2 == 0 && sumv > 0) { sumv = -1; cnt2 += abs(sumv) + 1; } else if (i % 2 == 1 && sumv < 0) { sumv = 1; cnt2 += abs(sumv) + 1; } sumv += a[i]; } cout << min(cnt1, 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
python3
import numpy as np import copy n = int(input()) a = list(map(int,input().split())) b=copy.deepcopy(a) cntm=0 cntp=0 sum_a=0 sum_b=0 if a[0]==0: cntm+=1 cntp+=1 a[0]=1 b[0]=-1 for i in range(n-1): sum_a += a[i] sum_b += b[i] if abs(sum_a) >= abs(a[i+1]) or sum_a*a[i+1]>=0: cntp += abs(-sum_a-np.sign(sum_a) -a[i+1]) a[i+1]=-sum_a-np.sign(sum_a) if abs(sum_b) >= abs(b[i+1]) or sum_b*b[i+1]>=0: cntm += abs(-sum_b-np.sign(sum_b) -b[i+1]) b[i+1]=-sum_b-np.sign(sum_b) print(min(cntm, cntp))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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 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]; long cnt1 = num_operate(n, 1, &a.front()) + 1; long cnt2 = num_operate(n, -1, &a.front()) + 1; long cnt3 = num_operate(n, sum, &a.front()); cout << min({cnt1, cnt2, cnt3}) << 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; vector<int> a; cin >> n; int A; for (int i = 0; i < n; i++) { cin >> A; a.push_back(A); } int s = 0; int ans1 = 0; int count = 0; for (int i = 0; i < n; i++) { ans1 += a[i]; if (i % 2 == 0) { if (ans1 >= 0) { count += ans1 + 1; ans1 = -1; } } else { if (ans1 <= 0) { count += 1 - ans1; ans1 = 1; } } } int count2 = 0; ans1 = 0; for (int i = 0; i < n; i++) { ans1 += a[i]; if (i % 2 != 0) { if (ans1 >= 0) { count2 += ans1 + 1; ans1 = -1; } } else { if (ans1 <= 0) { count2 += 1 - ans1; ans1 = 1; } } } cout << min(count, count2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int solve(); int adds(int i, int num); int show(); vector<long> adder; int N; int main() { cin >> N; adder = vector<long>(N + 1); adder.at(0) = 0; long buf; for (int i = 0; i < N; i++) { cin >> buf; adder.at(i + 1) = adder.at(i) + buf; } solve(); } int solve() { int flag = 0; int count = 0; for (int i = 0; i < N;) { show(); if (adder.at(i + 1) > 0) { if (flag != 1) { flag = 1; i++; } else { adds(i + 1, -1); count++; } } else if (adder.at(i + 1) < 0) { if (flag != -1) { flag = -1; i++; } else { adds(i + 1, 1); count++; } } else { if (flag == -1) { adds(i + 1, 1); count++; flag == 1; i++; } else if (flag == 1) { adds(i + 1, -1); count++; flag == -1; i++; } else { if (i == 0) { adds(i + 1, 1); } } } } cout << count << endl; return 0; } int adds(int i, int num) { for (int j = i; j < N + 1; j++) { adder.at(j) += num; } return 0; } int show() { 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> template <class T, class S> void cmin(T &a, const S &b) { if (a > b) a = b; } template <class T, class S> void cmax(T &a, const S &b) { if (a < b) a = b; } using namespace std; signed main() { long long int n; cin >> n; vector<long long int> v(n), sum(n); for (long long int i = 0; i < n; i++) cin >> v[i]; long long int ans = 0; bool used = true, flag = false; for (long long int i = 0; i < n; i++) { if (i) sum[i] = v[i] + sum[i - 1]; else sum[i] = v[i]; if (used) { if (sum[i] > 0) { flag = true; used = false; continue; } if (sum[i] < 0) { flag = false; used = false; continue; } ans++; continue; } if (flag) { if (sum[i] < 0) { flag = false; continue; } if (sum[i] >= 0) { flag = false; ans += abs(sum[i]) + 1; sum[i] = -1; continue; } } else { if (sum[i] > 0) { flag = true; continue; } if (sum[i] <= 0) { flag = false; ans += abs(sum[i]) + 1; sum[i] = 1; continue; } } } 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 solve(); int adds(int i, int num); int show(); vector<long> adder; int N; int main() { cin >> N; adder = vector<long>(N + 1); adder.at(0) = 0; long buf; for (int i = 0; i < N; i++) { cin >> buf; adder.at(i + 1) = adder.at(i) + buf; } solve(); } int solve() { int flag = 0; int count = 0; int buf; for (int i = 0; i < N;) { show(); if (adder.at(i + 1) > 0) { if (flag != 1) { flag = 1; i++; } else { buf = abs(adder.at(i + 1)) + 1; adds(i + 1, -buf); count += buf; } } else if (adder.at(i + 1) < 0) { if (flag != -1) { flag = -1; i++; } else { buf = abs(adder.at(i + 1)) + 1; adds(i + 1, buf); count += buf; } } else { if (flag == -1) { adds(i + 1, 1); count++; flag == 1; i++; } else if (flag == 1) { adds(i + 1, -1); count++; flag == -1; i++; } else { if (i == 0) { adds(i + 1, 1); } } } } cout << count << endl; return 0; } int adds(int i, int num) { for (int j = i; j < N + 1; j++) { adder.at(j) += num; } return 0; } int show() { return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<long long> A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } vector<long long> S1(N); vector<long long> S2(N); S1[0] = A[0]; S2[0] = A[0]; int cnt1 = 0; int cnt2 = 0; for (int i = 0; i < N; i++) { if (i) { S1[i] = S1[i - 1] + A[i]; S2[i] = S2[i - 1] + A[i]; } if (!(i % 2)) { if (S1[i] <= 0) { cnt1 += 1 - S1[i]; S1[i] = 1; } if (S2[i] >= 0) { cnt2 += S2[i] + 1; S2[i] = -1; } } else { if (S1[i] >= 0) { cnt1 += S1[i] + 1; S1[i] = -1; } if (S2[i] <= 0) { cnt2 += 1 - S2[i]; S2[i] = 1; } } } cout << min(cnt1, 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
python3
n = int(input()) a = [int(i) for i in input().split()] ans1 = 0 ans2 = 0 summ = 0 summ2 = 0 a2 = a[:] for i in range(n): if i % 2 == 0: if summ + a[i] <= 0: ans1 += abs(summ + a[i]) + 1 a[i] = -summ + 1 summ = 1 else: summ += a[i] else: if summ + a[i] >= 0: ans1 += abs(summ + a[i]) + 1 a[i] = -summ -1 summ = -1 else: summ += a[i] for i in range(n): if i % 2 != 0: if summ2 + a2[i] <= 0: ans2 += abs(summ2 + a2[i]) + 1 a2[i] = -summ2 + 1 summ2 = 1 else: summ2 += a[i] else: if summ2 + a2[i] >= 0: ans2 += abs(summ2 + a2[i]) + 1 a2[i] = -summ2 -1 summ2 = -1 else: summ2 += a2[i] print(min(ans1, ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; // ABC 6-C // http://abc006.contest.atcoder.jp/tasks/abc006_3 public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = in.nextInt(); } long answer = 0; if (nums[0] == 0) { answer = solve(nums, 0, 0); } else { answer = solve(nums, nums[0], 1); } System.out.println(answer); // // long sum = 0; // long answer = 0; // // for (int i = 0; i < n; i++) { // int a = in.nextInt(); // // if (sum < 0 && sum + a < 0) { // answer += 1 + Math.abs(sum + a); // sum = 1; // } else if (sum > 0 && sum + a > 0) { // answer += 1 + sum + a; // sum = -1; // } else if (sum + a == 0) { // answer++; // if (sum < 0) { // sum = 1; // } else { // sum = -1; // } // } else { // sum += a; // } // } // System.out.println(answer); } public static long solve(int[] nums, long sum, int index) { if (index == nums.length) { return 0; } if (sum < 0 && sum + nums[index] < 0) { return 1 + Math.abs(sum + nums[index]) + solve(nums, 1, index + 1); } else if (sum > 0 && sum + nums[index] > 0) { return 1 + sum + nums[index] + solve(nums, -1, index + 1); } else if (sum + nums[index] == 0) { if (sum < 0) { return 1 + solve(nums, 1, index + 1); } else if (sum > 0) { return 1 + solve(nums, -1, index + 1); } else { return 1 + Math.min(solve(nums, 1, index + 1), solve(nums, -1, index + 1)); } } else if (sum == 0) { return 1 + Math.min(solve(nums, 1, index + 1), solve(nums, -1, index + 1)); } { return solve(nums, sum + nums[index], index + 1); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int, input().split())) def solve(isPosi,N,A): ans = 0 sm = A[0] if sm>0: isPosi = True elif sm<0: isPosi = False for a in A[1:]: if isPosi: sm += a if sm >= 0: ans += abs(-1-sm) sm = -1 isPosi = False else: sm += a if sm <= 0: ans += abs(1-sm) sm = 1 isPosi = True return ans ans_p = solve(True,N,A) ans_n = solve(False,N,A) print(min(ans_p,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 sum, ans, n, i; int a[100005]; int main() { cin >> n; for (i = 1; i <= n; i++) { cin >> a[i]; } ans = 0; sum = 0; for (i = 1; i <= n; i++) { if (a[i] == 0) { sum++; } else break; } if (sum % 2 == 0 && sum != 0) { if (a[sum + 1] > 0) { a[1] = 1; ans += 1; } else { a[1] = -1; ans += 1; } } else if (sum % 2 != 0) { if (a[sum + 1] > 0) { a[1] = -1; ans += 1; } else { a[1] = 1; ans += 1; } } sum = a[1]; for (i = 2; i <= n; i++) { if (sum == 0) { if (a[i - 1] > 0) { ans++; sum--; } else { ans++; sum++; } } if (sum > 0) { if (a[i] + sum >= 0) { ans += a[i] + sum + 1; sum = -1; } else { sum += a[i]; } } else { if (a[i] + sum <= 0) { ans += abs(a[i] + sum) + 1; sum = 1; } else { sum += a[i]; } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 0x7fffffff; const int maxn = 1e5 + 10; int a[maxn]; int n; int main() { scanf("%d", &n); for (int i = 0; i < (n); ++i) { scanf("%d", &a[i]); } long long ans = 0; if (a[0] == 0 && a[1] > 0) { a[0] = -1; ++ans; } if (a[0] == 0 && a[1] <= 0) { a[0] = 1; ++ans; } long long t = a[0]; for (int i = 1; i < n; ++i) { if (t < 0) { t += a[i]; if (t <= 0) { ans += 1 - (t); t = 1; } continue; } t += a[i]; if (t >= 0) { ans += t + 1; t = -1; } } printf("%lld\n", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int N; cin >> N; vector<int> A(N); for (long long i = 0; i < long long(N); i++) { cin >> A[i]; } int ansA = 0; int sumA = 0; for (long long i = 0; i < long long(N); i++) { sumA += A[i]; if (i % 2 == 0) { if (sumA <= 0) { ansA += (abs(sumA) + 1); sumA += (abs(sumA) + 1); } } else { if (sumA >= 0) { ansA += (abs(sumA) + 1); sumA -= (abs(sumA) + 1); } } } int ansB = 0; int sumB = 0; for (long long i = 0; i < long long(N); i++) { sumB += A[i]; if (i % 2 != 0) { if (sumB <= 0) { ansB += (abs(sumB) + 1); sumB += (abs(sumB) + 1); } } else { if (sumB >= 0) { ansB += (abs(sumB) + 1); sumB -= (abs(sumB) + 1); } } } cout << min(ansA, ansB) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# coding: utf-8 # Here your code N = int(input()) a = [int(i) for i in input().split()] result_1 = 0 before_sum =a[0] after_sum =a[0] for i in range(1,N): before_sum = after_sum after_sum = before_sum + a[i] if before_sum * after_sum > 0: if after_sum < 0: result_1 += 1 - after_sum after_sum = 1 elif after_sum > 0: result_1 += 1 + after_sum after_sum = -1 elif before_sum * after_sum == 0: result_1 += 1 if before_sum < 0: after_sum = 1 else: after_sum = -1 before_sum =int(-a[0]/abs(a[0])) after_sum =before_sum result_2 = 1 + abs(before_sum) for i in range(1,N): before_sum = after_sum after_sum = before_sum + a[i] if before_sum * after_sum > 0: if after_sum < 0: result_2 += 1 - after_sum after_sum = 1 elif after_sum > 0: result_2 += 1 + after_sum after_sum = -1 elif before_sum * after_sum == 0: result_2 += 1 if before_sum < 0: after_sum = 1 else: after_sum = -1 print(min(result_1,result_2-1))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = [int(i) for i in input().split()] c = 10**15 for i in range(2): A = [-a for a in A] if A[0] != 0: ans = 0 S = A[0] f = A[0]//abs(A[0]) else: ans = 1 S = 1 f = 1 for a in A[1:]: S += a if S == 0: ans += 1 S = -f else: if S//abs(S) != f*(-1): ans += abs(S)+1 S = -f f *= -1 c = min(ans, c) print(c)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> int main(void) { int n, a[10001]; int i, s; long int c1, c2; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } s = 0; c1 = 0; for (i = 0; i < n; i++) { s += a[i]; if (i % 2 == 0) { while (s >= 0) { s--; c1++; } } else { while (s <= 0) { s++; c1++; } } } s = 0; c2 = 0; for (i = 0; i < n; i++) { s += a[i]; if (i % 2 != 0) { while (s >= 0) { s--; c2++; } } else { while (s <= 0) { s++; c2++; } } } if (c1 < c2) printf("%ld\n", c1); else printf("%ld\n", c2); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MOD = 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 = 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; const int INF = 1e9; const int MOD = 1e9 + 7; const long long LINF = 1e18; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < (int)(n); i++) cin >> a[i]; long long sum = 0; int flag = 1; long long ans = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (sum == 0) { ans += 1; } else if ((flag == 1) ^ (sum > 0) == 1) { ans += (abs(sum) + 1); if (flag) sum = 1; else sum = -1; } flag = (flag + 1) % 2; } sum = 0; flag = 0; long long temp = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (sum == 0) { temp += 1; } else if ((flag == 1) ^ (sum > 0) == 1) { temp += (abs(sum) + 1); if (flag) sum = 1; else sum = -1; } flag = (flag + 1) % 2; } cout << min(temp, 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 numpy as np n = int(input()) A = [int(i) for i in input().split()] def j(B,sign): ans,cur = 0,[0] for i in range(n): cur.append(cur[i]+B[i]) cs = np.sign(cur[i+1]) if cs != sign: if sign == -1: ans += -(-cur[i]-1)+B[i] cur[i+1] = -1 else: ans += -cur[i]+1-B[i] cur[i+1] = 1 sign *= -1 return ans print(j(A,np.sign(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
def main(): N = int(input()) A = [int(i) for i in input().split()] ans1 = 0 S = [0] * N S[0] = A[0] for i in range(1, N): S[i] = S[i-1] + A[i] if S[i]*S[i-1] < 0: continue if S[i-1] > 0: S[i] = -1 ans1 += abs((-1) - (S[i-1]+A[i])) else: S[i] = 1 ans1 += abs(1 - (S[i-1]+A[i])) S2 = [0] * N ans2 = 0 if A[0] > 0: S[0] = -1 ans2 += abs((-1) - A[i]) else: S[0] = 1 ans2 += abs(1 - A[i]) ans2 = 0 for i in range(1, N): S2[i] = S2[i-1] + A[i] if S2[i]*S2[i-1] < 0: continue if S2[i-1] > 0: S2[i] = -1 ans2 += abs((-1) - (S2[i-1]+A[i])) else: S2[i] = 1 ans2 += abs(1 - (S2[i-1]+A[i])) print(min(ans1, ans2)) 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
UNKNOWN
#include <bits/stdc++.h> int main() { int n, i, ans1 = 0, ans2 = 0, sum = 0; long long int a[100010]; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%lld", &a[i]); } for (i = 1; i <= n; i++) { sum += a[i]; if (i % 2 == 1 && sum <= 0) { ans1 += 1 - sum; sum = 1; } else if (i % 2 == 0 && sum >= 0) { ans1 += sum + 1; sum = -1; } } sum = 0; for (i = 1; i <= n; i++) { sum += a[i]; if (i % 2 == 1 && sum >= 0) { ans2 += sum + 1; sum = -1; } else if (i % 2 == 0 && sum <= 0) { ans2 += 1 - sum; sum = 1; } } if (ans1 > ans2) { printf("\n%d\n\n", ans2); } else { printf("\n%d\n\n", ans1); } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); vector<long long> a; for (int i = 0; i < n; i++) { long long an; scanf("%lld", &an); a.push_back(an); } long long op_count = 0; long long now_sum = 0; if (a[0] == 0) { int numOfZeros = 1; while (a[numOfZeros] == 0) { numOfZeros++; } if (a[numOfZeros] > 0) { if (numOfZeros % 2 == 0) { a[0] = 1; } else { a[0] = -1; } } else { if (numOfZeros % 2 == 0) { a[0] = -1; } else { a[0] = 1; } } op_count++; } long long adding = a[0] > 0 ? -1 : 1; for (int i = 0; i < n; i++) { now_sum += a[i]; adding *= -1; if (now_sum == 0) { a[i] += adding; now_sum += adding; op_count++; continue; } if (adding > 0) { const long long last = 1 - now_sum; if (last > 1) { a[i] += last; now_sum += last; op_count += abs(last); } } else { const long long last = -1 - now_sum; if (last < -1) { a[i] += last; now_sum += last; op_count += abs(last); } } } printf("%lld\n", op_count); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import copy n=int(input()) a=[0]+list(map(int,input().split())) b=copy.copy(a) psum=0 nsum=0 sum1=0 sum2=0 for i in range(1,n+1): if i%2!=0: psum+=max([0,1-sum1-a[i]]) a[i]+=max([0,1-sum1-a[i]]) sum1+=a[i] else: psum+=max([0,1+sum1+a[i]]) a[i]-=max([0,1+sum1+a[i]]) sum1+=a[i] for i in range(1,n+1): if i%2!=0: nsum+=max([0,1+sum2+b[i]]) b[i]-=max([0,1+sum2+b[i]]) sum2+=b[i] else: nsum+=max([0,1-sum2-b[i]]) a[i]+=max([0,1-sum2-b[i]]) sum2+=b[i] print(min([psum,nsum]))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoder { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); int[] a = new int[n]; int[] sum = new int[n]; string[] lines = Console.ReadLine().Split(' '); for (int i = 0; i < n; i++) { a[i] = int.Parse(lines[i]); } int ans1 = 0; int ans2 = 0; int sign = 1; sum[0] = a[0]; for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + a[i]; if (sign > 0) { if (sum[i] >= 0) { ans1 += sum[i] + 1; sum[i] = -1; } } else { if (sum[i] <= 0) { ans1 -= sum[i] - 1; sum[i] = 1; } } sign = -sign; } sign = -1; sum[0] = a[0]; for (int i = 1; i < n; i++) { if (sign > 0) { if (sum[i] >= 0) { ans2 += sum[i] + 1; sum[i] = -1; } } else { if (sum[i] <= 0) { ans2 -= sum[i] - 1; sum[i] = 1; } } sign = -sign; } Console.WriteLine(Math.Min(ans1, ans2)); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < (int)(n); i++) cin >> a.at(i); vector<long long> b(n); for (int i = 0; i < (int)(n); i++) b.at(i) = a.at(i); long long cnt = 0; long long wa = 0; long long wa2 = 0; wa = a.at(0); long long cntA = 0; long long cntB = 0; if (wa != 0) { for (int i = 0; i < n - 1;) { wa2 = wa + a.at(i + 1); if (wa > 0) { if (wa2 < 0) { i++; wa = wa2; } else if (wa2 > 0) { cnt += abs(-1 - wa2); a.at(i + 1) -= abs(-1 - wa2); } else if (wa2 == 0) { cnt += abs(-1 - wa2); a.at(i + 1) -= abs(-1 - wa2); } } else if (wa < 0) { if (wa2 < 0) { cnt += abs(1 - wa2); a.at(i + 1) += abs(1 - wa2); } else if (wa2 > 0) { i++; wa = wa2; } else if (wa2 == 0) { cnt += abs(1 - wa2); a.at(i + 1) += abs(1 - wa2); } } } cout << cnt << endl; } else { wa = 1; for (int i = 0; i < n - 1;) { wa2 = wa + a.at(i + 1); if (wa > 0) { if (wa2 < 0) { i++; wa = wa2; } else if (wa2 > 0) { cnt += abs(-1 - wa2); a.at(i + 1) -= abs(-1 - wa2); } else if (wa2 == 0) { cnt += abs(-1 - wa2); a.at(i + 1) -= abs(-1 - wa2); } } else if (wa < 0) { if (wa2 < 0) { cnt += abs(1 - wa2); a.at(i + 1) += abs(1 - wa2); } else if (wa2 > 0) { i++; wa = wa2; } else if (wa2 == 0) { cnt += abs(1 - wa2); a.at(i + 1) += abs(1 - wa2); } } } wa = -1; for (int i = 0; i < n - 1;) { wa2 = wa + b.at(i + 1); if (wa > 0) { if (wa2 < 0) { i++; wa = wa2; } else if (wa2 > 0) { cntB += abs(-1 - wa2); b.at(i + 1) -= abs(-1 - wa2); } else if (wa2 == 0) { cntB += abs(-1 - wa2); b.at(i + 1) -= abs(-1 - wa2); } } else if (wa < 0) { if (wa2 < 0) { cntB += abs(1 - wa2); b.at(i + 1) += abs(1 - wa2); } else if (wa2 > 0) { i++; wa = wa2; } else if (wa2 == 0) { cntB += abs(1 - wa2); b.at(i + 1) += abs(1 - wa2); } } } if (cnt < cntB) cout << cnt << endl; else cout << 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < (int)(n); i++) cin >> a[i]; int sum = 0; int count = 0; int tmp = 0; for (int i = 0; i < (int)(n); i++) { if (i % 2 == 0) { sum += a[i]; if (sum <= 0) { count += abs(sum) + 1; sum += abs(sum) + 1; } } if (i % 2 == 1) { sum += a[i]; if (sum >= 0) { count += abs(sum) + 1; sum -= abs(sum) + 1; } } } int count2 = 0; sum = 0; for (int i = 0; i < (int)(n); i++) { if (i % 2 == 1) { sum += a[i]; if (sum <= 0) { count2 += abs(sum) + 1; sum += abs(sum) + 1; } } if (i % 2 == 0) { sum += a[i]; if (sum >= 0) { count2 += abs(sum) + 1; sum -= abs(sum) + 1; } } } cout << min(count, count2) << 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 inf = 1e9; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; long long int ans = inf; long long int tmp = 0; long long int cnt = 0; for (int i = 0; i < n; i++) { tmp += a[i]; if (i % 2 == 0 && tmp <= 0) { cnt += (1 - tmp); tmp = 1; } else if (i % 2 == 1 && tmp >= 0) { cnt += (1 + tmp); tmp = -1; } } ans = min(ans, cnt); tmp = 0; cnt = 0; for (int i = 0; i < n; i++) { tmp += a[i]; if (i % 2 == 1 && tmp <= 0) { cnt += (1 - tmp); tmp = 1; } else if (i % 2 == 0 && tmp >= 0) { cnt += (1 + tmp); tmp = -1; } } ans = min(ans, cnt); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N=int(input()) A=list(map(int,input().split())) cur=A[0] ans=0 isplus=True ind=1 if cur<0: isplus=False if cur==0: # すべて0の場合 allzero=True firstNotZero=0 firstNotZeroInd=0 for i in range(N): if A[i]!=0: firstNotZero=A[i] firstNotZeroInd=i allzero=False break if allzero: print(N) exit(0) # 0以外が出てくる場合 if firstNotZero>0: cur=-1 ind=firstNotZeroInd isplus=False else: cur=1 ind=firstNotZeroInd isplus=True for i in range(ind,N): if isplus: if cur+A[i]>=0: diff=abs((cur+A[i])-(-1)) ans+=diff cur=-1 else: cur+=A[i] isplus=False else: if cur+A[i]<=0: diff=abs((cur+A[i])-1) ans+=diff cur=1 else: cur+=A[i] isplus=True 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()) data = input().split() for i in range(n): data[i] = int(data[i]) count = 0 sum = data[0] if(sum == 0): i = 1 while(data[i] == 0): i += 1 if(i % 2 == 0): if(data[i] > 0): sum += 1 else: sum -= 1 else: if(data[i] > 0): sum -= 1 else: sum += 1 i = 1 while(i < n): while(sum * (sum + data[i]) >= 0): if sum > 0: data[i] -= 1 count += 1 elif sum < 0: data[i] += 1 count += 1 sum += data[i] if(sum == 0): if(data[i] > 0): sum += 1 else: sum -= 1 i += 1 print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } template <class T> inline T sqr(T x) { return x * x; } int main(void) { int n; cin >> n; long long a[n + 1]; for (int i = 1; i <= n; ++i) { cin >> a[i]; } long long S1, S2; long long ans[2]; if (a[1] == 0) { S1 = 1; ans[0] = 1; for (int i = (2); i < (n + 1); ++i) { S2 = S1 + a[i]; if ((S1 < 0 && S2 > 0) || (S1 > 0 && S2 < 0)) { S1 = S2; } else { ans[0] += llabs(S2) + 1; if (S1 < 0) S2 = 1; else S2 = -1; S1 = S2; } } S1 = -1; ans[1] = 1; for (int i = (2); i < (n + 1); ++i) { S2 = S1 + a[i]; if ((S1 < 0 && S2 > 0) || (S1 > 0 && S2 < 0)) { S1 = S2; } else { ans[1] += llabs(S2) + 1; if (S1 < 0) S2 = 1; else S2 = -1; S1 = S2; } } ans[0] = min(ans[0], ans[1]); printf("%lld/n", ans[0]); } else { S1 = a[1]; ans[0] = 0; for (int i = (2); i < (n + 1); ++i) { S2 = S1 + a[i]; if ((S1 < 0 && S2 > 0) || (S1 > 0 && S2 < 0)) { S1 = S2; } else { ans[0] += llabs(S2) + 1; if (S1 < 0) S2 = 1; else S2 = -1; S1 = S2; } } printf("%lld/n", ans[0]); } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int main(void) { int n; std::cin >> n; std::vector<long long int> a(n); for (int i = 0; i < n; i++) { std::cin >> a[i]; } long long int count = 0; if (a[0] == 0) { count++; if (a[1] > 0) { a[0] = -1; } else { a[0] = +1; } } long long int sum = a[0]; for (int i = 1; i < n; i++) { if (sum > 0) { sum += a[i]; if (sum >= 0) { count += sum + 1; sum = -1; } } else { sum += a[i]; if (sum <= 0) { count += -sum + 1; sum = 1; } } } std::cout << count << std::endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
solver::[Int]->Int solver xs = let h = head xs in minimum [(check_tot (abs (h-1)) 1 (tail xs)) ,(check_tot (abs (h+1)) (-1) (tail xs)) ,(check_tot 0 h (tail xs))] main::IO() main=do _<-getLine datc<-getLine print (solver (map read (words datc))) --おそい。Step_sumを作る事無く、シーケンシャルにいく --今のカウント手数、ここまでの修正されたトータル(これはゼロでない事が保証される)、食べるリスト。 check_tot::Int -> Int -> [Int] -> Int check_tot st _ [] = st check_tot st tot xs | (tot > 0)&&((tot+(head xs))>=0) = let dec = (tot+(head xs))+1 in check_tot (dec+st) (-1) (tail xs) | (tot > 0)&&((tot+(head xs)) <0) = check_tot st (tot+(head xs)) (tail xs) | (tot < 0)&&((tot+(head xs)) >0) = check_tot st (tot+(head xs)) (tail xs) | (tot < 0)&&((tot+(head xs))<=0) = let inc = 1-(tot+(head xs)) in check_tot (inc+st) 1 (tail xs)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(int 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]; } long long aw = 0; if (a[0] == 0) { a[0] = 1; aw++; } s[0] = a[0]; long long x; for (i = 1; i < n; i++) { s[i] = s[i - 1] + a[i]; if (s[i - 1] > 0) { if (s[i] < 0) { continue; } else { x = s[i - 1] + a[i] + 1; if (x < 0) { x = -x; } aw += x; a[i] = -1 - s[i - 1]; s[i] = s[i - 1] + a[i]; } } else { if (s[i] > 0) { continue; } else { x = s[i - 1] + a[i] - 1; if (x < 0) { x = -x; } aw += x; a[i] = 1 - s[i - 1]; s[i] = s[i - 1] + 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; const int INF = 1000000001; const double PI = 3.141592653589; const long long LMAX = 1000000000000001; long long gcd(long long a, long long b) { if (a < b) swap(a, b); while ((a % b) != 0) { a = b; b = a % b; } return b; } int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<vector<long long>> dp(2, vector<long long>(n, 0)); vector<long long> sum(2, a[0]); if (sum[0] > 0) sum[1] = -1; else sum[1] = 1; dp[1][0] = abs(a[0]) + 1; for (int j = 0; j < 2; j++) { for (int i = 1; i < n; i++) { if (sum[j] > 0) { if (sum[j] + a[i] < 0) { dp[j][i] = dp[j][i - 1]; sum[j] += a[i]; } else { dp[j][i] = dp[j][i - 1] + abs(sum[j] + a[i]) + 1; sum[j] = -1; } } else if (sum[j] < 0) { if (sum[j] + a[i] > 0) { dp[j][i] = dp[j][i - 1]; sum[j] += a[i]; } else { dp[j][i] = dp[j][i - 1] + abs(sum[j] + a[i]) + 1; sum[j] = 1; } } } } cout << min(dp[0][n - 1], dp[1][n - 1]) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) as_ = list(map(int, input().split())) ans = 0 sum_ = as_[0] if sum_ == 0 and as_[1] >= 0: sum_ -= 1 ans += 1 elif sum_ == 0 and as_[1] < 0: sum_ += 1 ans += 1 else: pass for i in range(1, n): new_sum_ = sum_+as_[i] if sum_ > 0 and new_sum_ >= 0: as_[i] -= (1+new_sum_) ans += 1+new_sum_ sum_ = -1 elif sum_ < 0 and new_sum_ <= 0: as_[i] += (1-new_sum_) ans += 1-new_sum_ sum_ = 1 else: sum_ = new_sum_ 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> static const long long MOD_NUM = 1000000007; template <class _T> static void getint(_T& a) { std::cin >> a; } template <class _T> static void getint(_T& a, _T& b) { std::cin >> a >> b; } template <class _T> static void getint(_T& a, _T& b, _T& c) { std::cin >> a >> b >> c; } template <class _T> static _T tp_abs(_T a) { if (a < (_T)0) { a *= (_T)-1; } return a; } static void exec(); int main() { exec(); fflush(stdout); return 0; } static void exec() { int N; getint(N); std::vector<long long> ai(N); for (int i = 0; i < N; i++) { getint(ai[i]); } long long ans = 0; long long sum = ai[0]; if (ai[0] == 0) { if (ai[1] > 0) { sum--; } else { sum++; } ans++; } for (int i = 1; i < N; i++) { int bfrSign = (sum > 0) ? 1 : -1; sum += ai[i]; if ((bfrSign > 0) && (sum >= 0)) { ans += (tp_abs(sum) + 1); sum = -1; } else if ((bfrSign < 0) && (sum <= 0)) { ans += (tp_abs(sum) + 1); sum = 1; } } printf("%lld\n", ans); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; int a[n]; long long int s = 0; long long int ans = INT_MAX; int i; for (i = 0; i < n; i++) cin >> a[i]; s = a[0]; long long int p = 0; if (s > 0) { for (i = 1; i < n; i++) { if (i % 2) { if (s + a[i] < 0) { s += a[i]; } else { p += 1 + s + a[i]; s = -1; } } else { if (s + a[i] > 0) s += a[i]; else { p += 1 - s - a[i]; s = 1; } } } s = -1; ans = min(ans, p); p = a[0] + 1; for (i = 1; i < n; i++) { if (i % 2 == 0) { if (s + a[i] < 0) { s += a[i]; } else { p += 1 + s + a[i]; s = -1; } } else { if (s + a[i] > 0) s += a[i]; else { p += 1 - s - a[i]; s = 1; } } } ans = min(ans, p); cout << ans << endl; } else if (s < 0) { for (i = 1; i < n; i++) { if (i % 2 == 0) { if (s + a[i] < 0) { s += a[i]; } else { p += 1 + s + a[i]; s = -1; } } else { if (s + a[i] > 0) s += a[i]; else { p += 1 - s - a[i]; s = 1; } } } s = 1; ans = min(ans, p); p = -1 * a[0] + 1; for (i = 1; i < n; i++) { if (i % 2 == 1) { if (s + a[i] < 0) { s += a[i]; } else { p += 1 + s + a[i]; s = -1; } } else { if (s + a[i] > 0) s += a[i]; else { p += 1 - s - a[i]; s = 1; } } } ans = min(ans, p); cout << ans << endl; } else { p = 1; s = 1; for (i = 1; i < n; i++) { if (i % 2) { if (s + a[i] < 0) { s += a[i]; } else { p += 1 + s + a[i]; s = -1; } } else { if (s + a[i] > 0) s += a[i]; else { p += 1 - s - a[i]; s = 1; } } } s = -1; ans = min(ans, p); p = 1; for (i = 1; i < n; i++) { if (i % 2 == 0) { if (s + a[i] < 0) { s += a[i]; } else { p += 1 + s + a[i]; s = -1; } } else { if (s + a[i] > 0) s += a[i]; else { p += 1 - s - a[i]; s = 1; } } } ans = min(ans, p); cout << ans << endl; } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for (long long &x : a) cin >> x; long long ans = 0; if (a[0] == 0 && a[1] < 0) { a[0] = 1; ans++; } if (a[0] == 0 && a[1] > 0) { a[0] = -1; ans++; } long long 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--; ans++; } } if (i % 2 == 0) { while (sum <= 0) { sum++; ans++; } } } } if (a[0] < 0) { for (int i = 1; i < n; i++) { sum += a[i]; if (i % 2 == 1) { while (sum <= 0) { sum++; ans++; } } if (i % 2 == 0) { while (sum >= 0) { sum--; ans++; } } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void sum(int *N, long *S, int n); int main() { int *N; long *S; long count_eve = 0, count_odd = 0, n; int j = 0, k = 0; cin >> n; N = new int[n]; S = new long[n]; for (int i = 0; i < n; i++) { cin >> N[i]; } sum(N, S, n); int del1 = 0, del2 = 0; while (j != n) { if (j % 2 == 0 && S[j] + del1 <= 0) { count_eve += abs(S[j] + del1) + 1; del1 += abs(S[j] + del1) + 1; } else if (j % 2 == 1 && S[j] + del1 >= 0) { count_eve += abs(S[j] + del1) + 1; del1 += -abs(S[j] + del1) - 1; } j++; } sum(N, S, n); while (k != n) { if (k % 2 == 0 && S[k] + del2 >= 0) { count_odd += abs(S[k] + del2) + 1; del2 += -abs(S[k] + del2) - 1; } else if (k % 2 == 1 && S[k] + del2 <= 0) { count_odd += abs(S[k] + del2) + 1; del2 += abs(S[k] + del2) + 1; } k++; } cout << min(count_eve, count_odd) << endl; delete[] N; delete[] S; return 0; } void sum(int *N, long *S, int n) { S[0] = N[0]; for (int i = 1; i < n; i++) S[i] = S[i - 1] + N[i]; } void add(int *S, int n, int del, int k) { for (int i = k; i < n + 1; i++) S[i] += del; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; int main() { cin >> n; int a[n + 1]; for (int i = 0; i < n; i++) cin >> a[i]; bool flag; long long sum; long long ans1 = 0; sum = a[0]; flag = true; for (int i = 1; i < n; i++) { sum += a[i]; if (flag && sum <= 0) { ans1 += -sum + 1; sum = 1; } else if (!flag && sum >= 0) { ans1 += sum + 1; sum = -1; } flag = !flag; } long long ans2 = 0; sum = a[0]; flag = false; for (int i = 1; i < n; i++) { sum += a[i]; if (flag && sum <= 0) { ans2 += -sum + 1; sum = 1; } else if (!flag && sum >= 0) { ans2 += sum + 1; sum = -1; } flag = !flag; } 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> // debug #define d(x) cerr << #x ":" << x << endl; #define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl; #define ddd(x, y, z) cerr << "(" #x "," #y "," #z "):(" << x << "," << y << "," << z << ")" << endl; #define dump(v) cerr<<#v":[ ";for (auto macro_vi:v){cerr<<macro_vi<< " ";}cerr<<"]"<<endl; #define ddump(v) cerr<<#v":"<<endl;for(auto macro_row:v) {cerr<<"["; for (auto macro__vi:macro_row){cerr<<macro__vi<< " ";}cerr<<"]"<<endl;} #define damp(m) for(auto macro_pair:m){cerr<< macro_pair.first<<":"<<macro_pair.second<<endl;} #define dpair(p) cerr << #p":"<<"(" << p.first << "," << p.second << ")" << endl; // iterate #define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repr(i, 0, n) #define reprrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--) #define reprev(i, n) reprrev(i, 0, n) #define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++) #define all(v) v.begin(), v.end() // etc #define chmin(mi, val) mi = min(mi, val); #define chmax(ma, val) ma = max(ma, val); using ll = long long; using namespace std; int main(){ cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<ll> v(n, 0); rep(i,n)cin >> v[i]; vector<int> p1(n+1,1), p2(n+1,1); rep(i,n+1){ if(i%2==0) p1[i]*=-1; } rep(i,n+1){ if(i%2==1) p2[i]*=-1; } // dump(p1) // dump(p2) priority_queue<int, vector<int>, greater<int> > pq; // dump(v) vector<ll>sum_until(n+1,0); int cnt; // pat1 cnt = 0; for(int i=1; i<=n; i++){ sum_until[i] = sum_until[i-1] + v[i-1]; // dump(sum_until) if(sum_until[i]*p1[i] < 0){ int plus = abs(sum_until[i]); dd(i,plus*p1[i]) d(sum_until[i]) sum_until[i] += plus*p1[i] + p1[i]; d(sum_until[i]) cnt += abs(plus*p1[i]) +1; } else if(sum_until[i] == 0){ sum_until[i] = p1[i]; cnt += 1; } dump(sum_until) d(cnt) pq.push(cnt); p1 = p2; // dump(sum_until) cnt = 0; for(int i=1; i<=n; i++){ d(i) sum_until[i] = sum_until[i-1] + v[i-1]; // dump(sum_until) if(sum_until[i]*p1[i] < 0){ int plus = abs(sum_until[i]); dd(i,plus*p1[i]) d(sum_until[i]) sum_until[i] += plus*p1[i] + p1[i]; d(sum_until[i]) cnt += abs(plus*p1[i]) +1; } else if(sum_until[i] == 0){ sum_until[i] = p1[i]; cnt += 1; } } pq.push(cnt); d(cnt) dump(sum_until) cout << pq.top() << 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())) ttl = a[0] cst = 0 if a[0]>=0: flg = 1 elif a[0]<0: flg = -1 for i in range(1,n): ttl += a[i] if ttl*flg < 0: flg *= -1 else: if flg > 0: memo = abs(ttl)+1 ttl -= memo cst += memo elif flg < 0: memo = abs(ttl)+1 ttl += memo cst += memo flg *= -1 print(cst)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) b=list(map(int,input().split())) a=b condition='' cnt=0 wa=0 for i in range(n): wa+=a[i] if i == 0: if a[i]>0: condition='minus' else: condition='plus' elif condition == 'plus': condition='minus' if wa<=0: cnt+=abs(wa)+1 a[i]+=abs(wa)+1 wa+=abs(wa)+1 elif condition == 'minus': condition='plus' if wa>=0: cnt+=abs(wa)+1 a[i]-=abs(wa)-1 wa-=abs(wa)-1 print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> A(n); for (int i = 0; i < n; i++) { cin >> A.at(i); } int cntA, cntB, sigA, sigB; cntA = cntB = 0; ; for (int i = 0; i < n; i++) { if (i == 0) { if (A.at(i) > 0) { sigA = A.at(i); sigB = -1; cntB += abs(A.at(i)) + 1; } else { sigB = A.at(i); sigA = 1; cntA += abs(A.at(i)) + 1; } continue; } if (sigA > 0) { if (sigA + A.at(i) >= 0) { cntA += abs(sigA + A.at(i)) + 1; sigA = -1; } else { sigA += A.at(i); } if (sigB + A.at(i) <= 0) { cntB += abs(sigB + A.at(i)) + 1; sigB = 1; } else { sigB += A.at(i); } } else { if (sigA + A.at(i) <= 0) { cntA += abs(sigA + A.at(i)) + 1; sigA = 1; } else { sigA += A.at(i); } if (sigB + A.at(i) > 0) { cntB += abs(sigB + A.at(i)) + 1; sigB = -1; } else { sigB += A.at(i); } continue; } } 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, count, sum, x, bsum, s, count2; vector<int> a, b; cin >> n; a.resize(n); b.resize(n); cin >> a[0]; for (i = 1; i < n; i++) { cin >> a[i]; } b = a; count = 0; count2 = 0; if (a[0] == 0) { a[0] = 1; count = 1; } sum = 0; for (int i = 0; i < n - 1; i++) { sum += a[i]; if (sum + a[i + 1] == 0) { count++; if (sum > 0) { a[i + 1]--; } else { a[i + 1]++; } } if (sum * (sum + a[i + 1]) > 0) { if (sum > 0) { x = sum + 1; s = x + a[i + 1]; a[i + 1] = -x; count += abs(s); } else { x = sum - 1; s = x + a[i + 1]; a[i + 1] = -x; count += abs(s); } } } x = abs(b[0]) + 1; if (b[0] >= 0) { b[0] = -1; } else { b[0] = 1; } count2 = x; sum = 0; for (int i = 0; i < n - 1; i++) { sum += b[i]; if (sum + b[i + 1] == 0) { count2++; if (sum > 0) { b[i + 1]--; } else { b[i + 1]++; } } if (sum * (sum + b[i + 1]) > 0) { if (sum > 0) { x = sum + 1; s = x + b[i + 1]; b[i + 1] = -x; count2 += abs(s); } else { x = sum - 1; s = x + b[i + 1]; a[i + 1] = -x; count2 += abs(s); } } } cout << min(count, count2); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int n; int i; int sum; bool default_flag; bool plus_flag, minus_flag; int ope_count; cin >> n; vector<int> a(n); for (i = 0; i < n; i++) { cin >> a.at(i); } sum = 0; ope_count = 0; default_flag = true; plus_flag = false; minus_flag = false; for (i = 0; i < n; i++) { sum += a.at(i); if (default_flag == true) { default_flag = false; if (sum > 0) { plus_flag = true; } else if (sum < 0) { minus_flag = true; } else if (sum == 0) { ope_count++; if (a.at(i + 1) <= 0) { sum++; plus_flag = true; } else if (a.at(i + 1) > 0) { sum--; minus_flag = true; } } } else if (plus_flag == true) { while (sum >= 0) { ope_count++; sum--; } plus_flag = false; minus_flag = true; } else if (minus_flag == true) { while (sum <= 0) { ope_count++; sum++; } plus_flag = true; minus_flag = false; } } cout << ope_count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxx = 1e5 + 7; ll n, ans = 1ll << 60; ll a[maxx]; int main() { cin >> n; for(int i = 1; i <= n; i++) cin >> a[i]; int flag = 0; LL sum = 0; LL res = 0; for(int i = 1; i <= n; i++) { sum += a[i]; if(flag == 0){ if(sum <= 0) { res = res + 1 - sum; sum = 1; } } else { if(sum >= 0){ res = res + sum + 1; sum = -1; } } flag ^= 1; } ans = min(ans, res); res = 0; sum = 0; flag = 0; for(int i = 1; i <= n; i++) { sum += a[i]; if(flag == 1){ if(sum <= 0) { res = res + 1 - sum; sum = 1; } } else { if(sum >= 0){ res = res + sum + 1; sum = -1; } } flag ^= 1; } ans = min(ans, res); 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
/** * purpose : ABC 59 C * author : kyomukyomupurin * created : 2018-10-22 02:22:55 **/ #include <bits/stdc++.h> using namespace std; #define int64 long long int main(){ ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; int64 a[N] = {}; for (int i = 0; i < N; ++i) cin >> a[i]; // i(odd) > 0 int64 sum1 = 0; int64 cost1 = 0; for (int i = 0; i < N; ++i) { sum1 += a[i]; if (i % 2 == 1 && sum1 <= 0) { cost1 += -1 * sum1 + 1; sum1 = 1; } else if (i % 2 == 0 && sum1 >= 0) { cost1 += sum1 + 1; sum1 = -1; } } //i(even) > 0 int64 sum2 = 0; int64 cost2 = 0; for (int i = 0; i < N; ++i) { sum2 += a[i]; if (i % 2 == 0 && sum2 <= 0){ cost2 += -1 * sum2 + 1; sum2 = 1; } else if (i % 2 == 1 && sum2 >= 0){ cost2 += sum2 + 1; sum2 = 1; } } cout << min(cost1, cost2) << '\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; 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, 100000> A; string S; array<int, 100000> S1; array<int, 100000> S2; cin >> N; for (int i = 0; i < N; ++i) { cin >> A[i]; if (i == 0) S1[i] = A[i]; else S1[i] = S2[i - 1] + A[i]; S2[i] = S[i]; } for (int i = 0; i < N; ++i) { if (i % 2 == 0) { if (S1[i] <= 0) { num += -S1[i] + 1; S1[i] = 1; if (i < N - 1) S1[i + 1] = S1[i] + A[i + 1]; } } else { if (S1[i] >= 0) { num += -S1[i] - 1; S1[i] = -1; if (i < N - 1) S1[i + 1] = S1[i] + A[i + 1]; } } } int tmp = 0; for (int i = 0; i < N; ++i) { if (i % 2 == 1) { if (S2[i] <= 0) { tmp += -S2[i] + 1; S2[i] = 1; if (i < N - 1) S2[i + 1] = S2[i] + A[i + 1]; } } else { if (S2[i] >= 0) { tmp += -S2[i] - 1; S2[i] = -1; if (i < N - 1) S2[i + 1] = S2[i] + A[i + 1]; } } } cout << num << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
main :: IO() main = do getLine lis <- words <$> getLine let numlis = map read lis print $ solve numlis 0 solve :: [Integer] -> Integer -> Integer solve [] x = 0 solve (x:xs) 0 = solve xs x solve (x:xs) m | m > 0 = max 0 (sum+1) + solve xs (min sum (-1)) | m < 0 = max 0 (1-sum) + solve xs (max sum 1) where sum = m+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
using System; using System.Collections.Generic; using System.Linq; namespace AtCoderBeginnerContest { public class Program { public static void Main(string[] args) { // C var n = long.Parse(Console.ReadLine()); var a = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray(); long sum = a[0]; long count = 0; for (int i = 1; i < n; i++) { // 差し引き0になってしまうとき if (sum + a[i] == 0) { if (i == n - 1 || a[i + 1] > 0) sum = -1; else sum = 1; count++; } // sumとsum+a[i]の符号が違うとき else if ((sum > 0 && sum+a[i] < 0) || (sum < 0 && sum + a[i] > 0)) { sum = sum + a[i]; } // sumとsum+a[i]の符号が同じとき else { if (Math.Abs(sum + a[i]) > Math.Abs(sum)) { count += Math.Abs(sum) + 1; sum = sum > 0 ? sum = -1 + a[i] : sum = 1 + a[i]; } else { count += Math.Abs(sum+a[i]) + 1; sum = sum > 0 ? sum = -1 : sum = 1; } } } Console.WriteLine(count); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, ans, countv = 0, countu = 0, sumv = 0, sumu = 0; cin >> n; vector<int> v(n), u(n); for (int i = 0; i < n; i++) { cin >> v.at(i); u.at(i) = v.at(i); } for (int j = 0; j < n; j++) { sumv += v.at(j); if (j % 2 == 0) { while (sumv <= 0) { sumv++; countv++; } } if (j % 2 == 1) { while (sumv >= 0) { sumv--; countv++; } } } for (int j = 0; j < n; j++) { sumu += u.at(j); if (j % 2 == 0) { while (sumu >= 0) { sumu--; ; countu++; } } if (j % 2 == 1) { while (sumu <= 0) { sumu++; countu++; } } } cout << min(countv, countu) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> int main(void) { int n; long sum1 = 0; long sum2 = 0; long tmp; long lcount = 0; long rcount = 0; int a[100000]; char input[1500000]; int i = 0, j = 0; int cp = 0, tcp = 0; char tp[12]; tp[12] = '\0'; fgets(input, 1500000, stdin); n = atoi(input); fgets(input, 1500000, stdin); for (i = 0; i < n; i++) { while (input[cp] != ' ' && input[cp] != '\n') { tp[tcp] = input[cp]; tcp++; cp++; } tp[tcp] = '\0'; tcp = 0; cp++; a[i] = atoi(tp); } tmp = a[0]; for (i = 1; i < n; i++) { if (i % 2 == 0) { tmp += a[i]; while (tmp > -1) { lcount++; tmp--; } } else { tmp += a[i]; while (tmp < 1) { lcount++; tmp++; } } } tmp = a[0]; for (i = 1; i < n; i++) { if (i % 2 == 1) { tmp += a[i]; while (tmp > -1) { rcount++; tmp--; } } else { tmp += a[i]; while (tmp < 1) { rcount++; tmp++; } } } printf("%ld\n", lcount > rcount ? rcount : lcount); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
import java.util.Scanner object Main extends App { val sc = new Scanner(System.in) val n = sc.nextInt() val a = new Array[Int](n) for(i <- 0 until n) a(i) = sc.nextInt() var cnt = 0 var ans = 0 for(i <- 0 until n){ val now = a(i) cnt += now if(i % 2 == 0){ if(cnt < 1){ ans += 1 - cnt cnt = 1 } } else{ if(cnt > -1){ ans += cnt - (-1) cnt = -1 } } } cnt = 0 var ans2 = 0 for(i <- 0 until n){ val now = a(i) cnt += now if(i % 2 != 0){ if(cnt < 1){ ans2 += 1 - cnt cnt = 1 } } else{ if(cnt > -1){ ans2 += cnt - (-1) cnt = -1 } } } println(Math.min(ans, ans2)) }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = list(map(int, input().split())) plus_ans = 0 minus_ans = 0 for i in range(len(A)): if i == 0: if A[0] <= 0: plus_ans += A[0] + 1 sum = 1 else: sum = A[0] else: if sum > 0: if sum + A[i] >= 0: plus_ans += abs(sum + A[i]) + 1 sum = -1 else: sum += A[i] elif sum < 0: if sum + A[i] <= 0: plus_ans += abs(sum + A[i]) + 1 sum = 1 else: sum += A[i] for i in range(len(A)): if i == 0: if A[0] >= 0: minus_ans += A[0] + 1 sum = -1 else: sum = A[0] else: if sum > 0: if sum + A[i] >= 0: minus_ans += abs(sum + A[i]) + 1 sum = -1 else: sum += A[i] elif sum < 0: if sum + A[i] <= 0: minus_ans += abs(sum + A[i]) + 1 sum = 1 else: sum += A[i] print(min(plus_ans, minus_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(); int sum=0; int count=0; for(int i=0;i<n-1;i++){ sum+=a[i]; if(sum>0){ if(sum+a[i+1]>=0){ count+=sum+a[i+1]+1; a[i+1]-=sum+a[i+1]+1; } }else if(sum<0){ if(sum+a[i]<=0){ count+=sum+a[i+1]+1; a[i+1]-=sum+a[i+1]+1; } } } System.out.println(count); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; signed main() { int n; cin >> n; int a[n]; for (size_t i = 0; i < n; i++) { cin >> a[i]; } int s = 0; int res = 0; for (size_t i = 0; i < n; i++) { int s2 = s + a[i]; if (i % 2 == 0) { if (s2 <= -1) { s = s2; } else { s = -1; res += (s2 + 1); } } else { if (s2 >= 1) { s = s2; } else { s = 1; res += (-s2 + 1); } } } s = 0; int res2 = 0; for (size_t i = 0; i < n; i++) { int s2 = s + a[i]; if (i % 2 == 1) { if (s2 <= -1) { s = s2; } else { s = -1; res2 += (s2 + 1); } } else { if (s2 >= 1) { s = s2; } else { s = 1; res2 += (-s2 + 1); } } } cout << min(res, res2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using lli = long long int; using ulli = unsigned long long int; vector<lli> N, rN; lli in, n, d = 0, dp, pm; ulli ans = 0; int main() { cin >> n; for (lli l = 0; l < n; l++) { cin >> in; if (l == 0) { N.push_back(in); } else { N.push_back(N[l - 1] + in); } } for (lli l = 1; l < (lli)N.size(); l++) { dp = d; if (N[l - 1] + dp < 0) { if (N[l] + dp < 0) { d += 1 - N[l] - dp; ans += 1 - N[l] - dp; } else if (N[l] + dp == 0) { d += 1; ans += 1; } } else if (N[l - 1] + dp > 0) { if (N[l] + dp > 0) { d -= N[l] + dp + 1; ans += N[l] + dp + 1; } else if (N[l] + dp == 0) { d -= 1; ans += 1; } } else { for (lli m = l; m < (lli)N.size(); m++) { if (N[m] > 0) { pm = (m - l) % 2; break; } else if (N[m] < 0) { pm = (m - l + 1) % 2; break; } if (m == (lli)N.size() - 1) { pm = (m + 1) % 2; break; } } if (pm == 0) { d += 1; ans += 1; } else if (pm == 1) { d -= 1; ans += 1; } } } cout << ans; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; int sum = 0; int ans0 = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0 && sum <= 0) { ans0 += abs(sum) + 1; sum = 1; } else if (i & 2 == 1 && sum >= 0) { ans0 += abs(sum) + 1; sum = -1; } } sum = 0; int ans1 = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0 && sum >= 0) { ans1 += abs(sum) + 1; sum = -1; } else if (i % 2 == 1 && sum <= 0) { ans1 += abs(sum) + 1; sum = 1; } } cout << min(ans0, ans1) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) l = map(int, input().split()) ll = [] for q in range(len(l)): ll.append(l[q]) print(ll) sums1 = [0] * n count1 = 0 sums1[0] = ll[0] for i in range(1, len(ll)): sums1[i] = sums1[i-1] + ll[i] if l[0] == 0: for v in range(len(sums1)): sums1 += [1] * len(sums1) count1 += 1 for k in range(1, len(sums1)): while sums1[k] == 0 or sums1[k] * sums1[k-1] > 0: if sums1[k] == 0: for p in range(k, len(sums1)): sums1[p] += -(sums1[k-1]/abs(sums1[k-1])) count1 += 1 if sums1[k] * sums1[k-1] > 0: for p in range(k, len(sums1)): sums1[p] += (abs(sums1[k])+1) * (-(sums1[k])/abs(sums1[k])) count1 += abs(sums1[k])+1 sums2 = [0] * n count2 = 0 sums2[0] = ll[0] for i in range(1, len(ll)): sums2[i] = sums2[i-1] + ll[i] if l[0] == 0: for v in range(len(sums2)): sums2 -= [1] * len(sums2) count2 += 1 for k in range(1, len(sums2)): while sums2[k] == 0 or sums2[k] * sums2[k-1] > 0: if sums2[k] == 0: for p in range(k, len(sums2)): sums2[p] += -sums2[k-1]/abs(sums2[k-1]) count2 += 1 if sums2[k] * sums2[k-1] > 0: for p in range(k, len(sums1)): sums2[p] += -(abs(sums2[k])+1) * ((sums2[k])/abs(sums2[k])) count2 += abs(sums1[k])+1 print(min(count2, count2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using lint = long long; using uint = unsigned int; using ulint = unsigned long long; using ldouble = long double; using pii = pair<int, int>; using pli = pair<lint, lint>; using pdd = pair<double, double>; using pld = pair<ldouble, ldouble>; using v1i = vector<int>; using v1li = vector<lint>; using v2i = vector<vector<int>>; using v2li = vector<vector<lint>>; using v3i = vector<vector<vector<int>>>; using v3li = vector<vector<vector<lint>>>; using v1b = vector<bool>; using v2b = vector<vector<bool>>; using v3b = vector<vector<vector<bool>>>; using v1c = vector<char>; using v2c = vector<vector<char>>; using v3c = vector<vector<vector<char>>>; constexpr lint mod1 = 1e9 + 7; constexpr lint mod2 = 998244353; int main() { lint n, p = 0, q = 0, r = 0, s = 0; cin >> n; v1i v(n), w(n), a(n), b(n); for (int i = 0; i < n; ++i) cin >> v[i]; w[0] = v[0]; for (int i = 0; i < n - 1; ++i) w[i + 1] = w[i] + v[i + 1]; for (int i = 0; i < n; ++i) { a[i] = w[i]; a[i] += r; if (i % 2 == 0) { if (w[i] < 0) { p += 1 - w[i]; r += 1 - w[i]; } } else { if (w[i] > 0) { p += w[i] + 1; r -= w[i] + 1; } } } for (int i = 0; i < n; ++i) { a[i] = w[i]; a[i] += r; if (i % 2 == 1) { if (w[i] < 0) { q += 1 - w[i]; s += 1 - w[i]; } } else { if (w[i] > 0) { q += w[i] + 1; s -= w[i] + 1; } } } cout << min(p, q) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <iostream> #include <vector> long body(std::vector<long>& a){ long ans = 0; std::vector<long> s(a.size()); s.at(0) = a.at(0); for(unsigned long i = 1; i < a.size(); i++){ s.at(i) = s.at(i-1) + a.at(i); } long diff = 0; for(unsigned long i = 1; i < s.size(); i++){ s.at(i) += diff; // update long n = 0; if(s.at(i-1) > 0 && s.at(i) >= 0){ n = s.at(i) + 1; ans += n; diff -= n; s.at(i) += diff; }else if(s.at(i-1) < 0 && s.at(i) <= 0){ n = - s.at(i) + 1; ans += n; diff += n; s.at(i) += diff; } } return ans; } int main(int argc, char **argv) { long n; std::cin >> n; std::vector<long> a(n); for(long i = 0; i < n; i++){ std::cin >> a.at(i); } if(a.at(0) != 0){ ans = body(a); }else{ a.at(0) = -1; long ans_a = body(a) + 1; a.at(0) = 1; long ans_b = body(a) + 1; ans = std::min(ans_a, ans_b); } std::cout << ans << std::endl; }