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() { cin.sync_with_stdio(false); int n; cin >> n; long long a[n], sum[n]; for (int i = (int)(0); i < (int)(n); i++) { cin >> a[i]; if (i == 0) sum[i] = a[i]; else sum[i] = sum[i - 1] + a[i]; } long long sump = (a[0] != 0) ? a[0] : 1, sumq = (a[0] != 0) ? -a[0] : -1; long long p = (a[0] != 0) ? 0 : 1, q = (a[0] != 0) ? 0 : 1; for (int i = (int)(1); i < (int)(n); i++) { if ((sump + a[i]) * sump < 0) sump = sump + a[i]; else if (sump < 0) p += abs(1 - (sump + a[i])), sump = 1; else if (sump > 0) p += abs(-1 - (sump + a[i])), sump = -1; if ((sumq + a[i]) * sumq < 0) sumq = sumq + a[i]; else if (sumq < 0) q += abs(1 - (sumq + a[i])), sumq = 1; else if (sumq > 0) q += abs(-1 - (sumq + a[i])), sumq = -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
python3
n = int(input()) a = list(map(int, input().split())) import numpy as np na1 = np.array(a).cumsum() na2 = np.array(a).cumsum() cnt1 = 0 hoge1 = 0 cnt2 = 0 hoge2 = 0 for i in range(1, n): na1[i] += hoge1 na2[i] += hoge2 if(i % 2 == 0 and na1[i] <= 0): delta1 = abs(na1[i]) + 1 delta2 = abs(na2[i]) + 1 cnt1 = cnt1 + delta1 hoge1 += delta1 elif(i % 2 == 1 and na1[i] >= 0): delta1 = abs(na1[i]) + 1 delta2 = abs(na2[i]) + 1 cnt1 = cnt1 + delta1 hoge1 -= delta1 if(i % 2 == 1 and na2[i] <= 0): delta1 = abs(na1[i]) + 1 delta2 = abs(na2[i]) + 1 cnt2 = cnt2 + delta2 hoge2 += delta2 elif(i % 2 == 0 and na2[i] >= 0): delta1 = abs(na1[i]) + 1 delta2 = abs(na2[i]) + 1 cnt2 = cnt2 + delta2 hoge2 -= delta2 else: na1[i] print(min([cnt1,cnt2]))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, t = 0, curr = 0, prev; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; prev = curr; curr += a[i]; if (i != 0) { if (prev < 0 && curr <= 0) { t += -curr + 1; curr = 1; } else if (prev > 0 && curr >= 0) { t += curr + 1; curr = -1; } } } cout << t; 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 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 accsum = 0; long long as[n]; for (int i = 0; i <= n - 1; i++) scanf("%lld ", &as[i]); long long ans1 = 0; for (int i = 0; i <= n - 1; i++) { accsum += as[i]; if (i % 2 == 0) { long long diff = max(0LL, 1LL - accsum); ans1 += diff; accsum += diff; } else { long long diff = max(0LL, 1LL + accsum); ans1 += diff; accsum -= diff; } } accsum = 0; long long ans2 = 0; for (int i = 0; i <= n - 1; i++) { accsum += as[i]; if (i % 2 != 0) { long long diff = max(0LL, 1LL - accsum); ans2 += diff; accsum += diff; } else { long long diff = max(0LL, 1LL + accsum); ans2 += diff; accsum -= diff; } } cout << min(ans1, ans2) << '\n'; printf("Debug\n"); } int main(void) { solve(); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
parseInt(x) = parse(Int, x) function main() n = readline() |> parseInt a = map(parseInt, split(readline())) b = Array{Int}(n) k = 0 if a[1] > 0 b[1] = 1 k += abs(a[1]+1) else b[1] = -1 k += abs(a[1]-1) end for i in 2:n b[i] = a[i]+b[i-1] if b[i]*b[i-1] >= 0 if b[i-1] < 0 k += abs(b[i]-1) b[i] = 1 else k += abs(b[i]+1) b[i] = -1 end end end c = Array{Int}(n) l = 0 if a[1] > 0 c[1] = -1 l += abs(a[1]+1) else c[1] = 1 l += abs(a[1]-1) end for i in 2:n c[i] = a[i]+c[i-1] if c[i]*c[i-1] >= 0 if c[i-1] < 0 l += abs(c[i]-1) c[i] = 1 else l += abs(c[i]+1) c[i] = -1 end end end print(min(k,l)) end main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> inline T GCD(T a, T b) { T c; while (b != 0) { c = a % b; a = b; b = c; } return a; } template <typename T> inline T LCM(T a, T b) { T c = GCD(a, b); a /= c; return a * b; } template <typename T> inline T nCr(T a, T b) { T i, r = 1; for (i = 1; i <= b; i++) { r *= (a + 1 - i); r /= i; } return r; } template <typename T> inline T nHr(T a, T b) { return nCr(a + b - 1, b); } template <typename T> inline T POW(T a, T b) { int i, r = 1; for (i = 1; i <= b; i++) { r *= a; } return r; } int main(void) { cin.tie(0); ios::sync_with_stdio(false); long long n, a[100000], sum[100001], ans = 0; cin >> n; sum[0] = 0; for (int i = 0; i < (n); ++i) cin >> a[i]; if (a[0] == 0) { long long MIN = 1e15; a[0] = -1; long long tmp = 1; for (int i = 0; i < (n); ++i) { sum[i + 1] = sum[i] + a[i]; if (i != 0 and sum[i] * sum[i + 1] >= 0) { tmp += (sum[i] > 0 ? sum[i + 1] + 1 : 1 - sum[i + 1]); sum[i + 1] = (sum[i] > 0 ? -1 : 1); } } if (tmp < MIN) MIN = tmp; a[0] = 1; tmp = 1; for (int i = 0; i < (n); ++i) { sum[i + 1] = sum[i] + a[i]; if (i != 0 and sum[i] * sum[i + 1] >= 0) { tmp += (sum[i] > 0 ? sum[i + 1] + 1 : 1 - sum[i + 1]); sum[i + 1] = (sum[i] > 0 ? -1 : 1); } } if (tmp < MIN) MIN = tmp; ans = tmp; } else { for (int i = 0; i < (n); ++i) { sum[i + 1] = sum[i] + a[i]; if (i != 0 and sum[i] * sum[i + 1] >= 0) { ans += (sum[i] > 0 ? sum[i + 1] + 1 : 1 - sum[i + 1]); sum[i + 1] = (sum[i] > 0 ? -1 : 1); } } } cout << ans << "\n"; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long a[100000], b[100001]; int main() { long long n; cin >> n; for (long long i = 0; i < n; i++) { cin >> a[i]; b[i] += a[i]; b[i + 1] = b[i]; } long long sum = 0, sum2 = 0; if (b[0] == 0) { int i = 0; while (b[i] == 0) i++; if (i % 2 == 0) { b[0] = 1; sum2 = 1; } else { b[0] = -1; sum2 = -1; } sum++; } for (long long i = 1; i < n; i++) { b[i] += sum2; if (b[i] == 0) { if (b[i - 1] > 0) { sum2--; sum++; b[i] = -1; } else { sum2++; sum++; b[i] = 1; } } else { if (b[i - 1] > 0 && b[i] > 0) { sum2 -= (b[i] + 1); sum += (b[i] + 1); b[i] = -1; } else if (b[i] < 0 && b[i - 1] < 0) { sum2 += (0 - b[i] + 1); sum += (0 - b[i] + 1); b[i] = 1; } } } cout << sum << endl; cin >> n; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MX = 1e5 + 5, INF = 5 << 59, 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)(0); ++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
cpp
#include <bits/stdc++.h> using namespace std; int ch_sign(int n) { if (n == 0) return 0; return (n > 0) - (n < 0); } int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) cin >> a[i]; int sign1 = (a[0] > 0) - (a[0] < 0), sign2 = -1 * sign1; int s1 = 0, s2 = 0; int ans1 = 0, ans2 = 0; for (int i = 0; i < n; ++i) { s1 += a[i]; s2 += a[i]; sign1 *= -1; sign2 *= -1; if (ch_sign(s1) != sign1) { ans1 += abs(s1 - sign1); s1 = sign1; } if (ch_sign(s2) != sign2) { ans2 += abs(s2 - sign2); s2 = sign2; } } cout << ((ans1 > ans2) ? ans1 : ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> v; while (n--) { long long a; cin >> a; v.push_back(a); } long long sum = 0; long long loss_sum = 0; long long d = 1; if (v[0] < 0) { d = -1; } long long crr = 0; long long loss = 0; for (long long i = 0; i < v.size(); i++) { crr = sum + v[i]; if (!(crr * d > 0)) { loss = abs(crr) + 1; v[i] += loss * d; loss_sum += loss; } sum += v[i]; d *= -1; } cout << loss_sum << 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 = input() b = input().split() a = [int(b[i]) for i in range(len(b))] def check(a): sum = 0 for i in range(len(a)): if(i == 0): sum += a[0] continue if(sum > 0): sum += a[i] if(a[i] == 0 or sum >= 0): return (i, -1) elif(sum < 0): sum += a[i] if(a[i] == 0 or sum <= 0): return (i, +1) else: if(a[i-1] > 0): return (i, -1) else: return (i, +1) return True ans = 0 while(True): c = check(a) if(c == True): break a[c[0]] += c[1] ans += 1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) count = 0 sum_ = 0 for i in range(n): if sum_ * (sum_+a[i]) >=0 and i!=0: if sum_ > 0: count += sum_+a[i]+1 a[i] = -sum_-1 elif sum_ < 0: count += abs(sum_)-a[i]+1 a[i] = -sum_+1 sum_ += a[i] print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int &x : a) { cin >> x; } bool plus; int sum = 0; int count_plus = 0; plus = false; for (int i = 0; i < n; i++) { plus = !plus; sum += a.at(i); if (plus) { if (sum > 0) { continue; } else { count_plus += 1 - sum; sum = 1; } } else { if (sum < 0) { continue; } else { count_plus += 1 + sum; sum = -1; } } } int count_minus = 0; plus = true; sum = 0; for (int i = 0; i < n; i++) { plus = !plus; sum += a.at(i); if (plus) { if (sum > 0) { continue; } else { count_minus += 1 - sum; sum = 1; } } else { if (sum < 0) { continue; } else { count_minus += 1 + sum; sum = -1; } } } cout << min(count_plus, count_minus) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = input() b = input().split() a = [int(b[i]) for i in range(len(b))] def check(a): sum = 0 for i in range(len(a)): if(i == 0): if(a[i] == 0): return (i, +1) sum += a[0] continue if(sum > 0): sum += a[i] if(a[i] == 0 or sum >= 0): return (i, -1) elif(sum < 0): sum += a[i] if(a[i] == 0 or sum <= 0): return (i, +1) else: if(a[i-1] > 0): return (i, -1) else: return (i, +1) return True ans = 0 while(True): c = check(a) if(c == True): break a[c[0]] += c[1] ans += 1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) list_a = [int(x) for x in input().split()] sum = list_a[0] ans_even = 0 ans_odd = 0 for i in range(1,n) : if i%2 == 0 : if sum + list_a[i] <= 0 : ans_even += 1 - (sum + list_a[i]) sum = 1 else : sum = sum + list_a[i] else : if sum + list_a[i] > 0 : ans_even += 1 + (sum + list_a[i]) sum = -1 else : sum = sum + list_a[i] for i in range(1,n) : if i%2 == 1 : if sum + list_a[i] <= 0 : ans_odd += 1 - (sum + list_a[i]) sum = 1 else : sum = sum + list_a[i] else : if sum + list_a[i] > 0 : ans_odd += 1 + (sum + list_a[i]) sum = -1 else : sum = sum + list_a[i] print(min(ans_even, ans_odd))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n; cin >> n; ll sum = 0; cin >> sum; ll ans = 0; for (int i = 0; i < n - 1; ++i) { int a; cin >> a; sum += a; if (sum == 0) { ll need = sum - a > 0 ? -1 : 1; sum += need; ans += abs(need); } else if (((sum - a > 0) == (sum > 0)) or ((sum - a < 0) == (sum < 0))) { ll need = sum > 0 ? -(sum + 1) : -(sum - 1); sum += need; ans += abs(need); } } 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 <iostream> #include <cstdio> #define N 100005 using namespace std; typedef long long ll; ll n, b, a[N]; ll abs(ll p) {return p > 0 ? p : -p;} ll f(ll p) { ll i, s = 0; for (i = 1; i < n; i++) { if (p > 0) { p += a[i]; if (p >= 0) s += p + 1, p = -1; } else { p += a[i]; if (p <= 0) s += -p + 1, p = 1; } } return s; } int main() { ll i, t = 1e19; cin >> n; for (i = 0; i < n; i++) scanf("%lld", &a[i]); if (a[0] == 0) {cout << min(f(1), f(-1)) + 1; return 0;} if (a[0] > 0) t = f(-1) + a[0] - 1; else t = f(1) - a[0] + 1; cout << min(f(a[0]), t); 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())) flg = A[0] cnt = 0 for i in range(1,N): flg1 = flg + A[i] if flg>0 and flg1>=0: flg1 = flg1+1 flg = -1 cnt += flg1 elif flg<0 and flg1<=0: flg1 = 1-flg1 flg = 1 cnt += flg1 else: flg = flg1 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
UNKNOWN
n = gets.chomp.to_i a = gets.chomp.split(" ").map(&:to_i) ans = 0 cumulative_sum = [0] n.times do |i| tmp = cumulative_sum[i] + a[i] if tmp == 0 then ans += 1 if 0 < cumulative_sum[i] then cumulative_sum << -1 elsif 0 > cumulative_sum[i] then cumulative_sum << 1 end else if 0 < tmp && 0 < cumulative_sum[i] then ans += tmp + 1 cumulative_sum << -1 elsif 0 > tmp && 0 > cumulative_sum[i] then ans += 1-tmp cumulative_sum << 1 else cumulative_sum << tmp end end end puts ans
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<long long> A(N); vector<long long> sum(N); for (int i = 0; i < (int)(N); i++) { cin >> A[i]; sum[i] = A[i]; } for (int i = 0; i < (int)(N - 1); i++) { sum[i + 1] += sum[i]; } long long diff = 0; long long cnt = 0; for (int i = 0; i < N - 1; i++) { long long x = sum[i] + diff; long long y = sum[i + 1] + diff; if (y == 0) { if (x > 0) { diff--; } else { diff++; } cnt++; continue; } if (x > 0 && y > 0) { diff -= (y + 1); cnt += (y + 1); } else if (x < 0 && y < 0) { diff += (abs(y) + 1); cnt += (abs(y) + 1); } } cout << cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int a[maxn]; int main() { int n; long long sum, num = 1e18; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); sum = a[0]; long long cnt = 0; if (sum == 0) { int i = 1; while (a[i] == 0 && i < n) i++; if (i == n) { cnt = (n - 1) * 2 + 1; printf("%lld\n", cnt); return 0; } for (int i = 1; i < n; i++) { sum = 1; if (sum > 0) { long long t = sum + a[i]; if (t < 0) sum = t; else { long long b = abs(t + 1); cnt += b; sum = -1; } } else if (sum < 0) { long long t = sum + a[i]; if (t > 0) sum = t; else { long long b = abs(1 - t); cnt += b; sum = 1; } } num = min(num, cnt); } for (int i = 1; i < n; i++) { sum = -1; if (sum > 0) { long long t = sum + a[i]; if (t < 0) sum = t; else { long long b = abs(t + 1); cnt += b; sum = -1; } } else if (sum < 0) { long long t = sum + a[i]; if (t > 0) sum = t; else { long long b = abs(1 - t); cnt += b; sum = 1; } } num = min(num, cnt); } printf("%lld\n", num); return 0; } for (int i = 1; i < n; i++) { if (sum > 0) { long long t = sum + a[i]; if (t < 0) sum = t; else { long long b = abs(t + 1); cnt += b; sum = -1; } } else if (sum < 0) { long long t = sum + a[i]; if (t > 0) sum = t; else { long long b = abs(1 - t); cnt += b; sum = 1; } } } printf("%lld\n", cnt); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a_list = list(map(int, input().split())) total = 0 count = 0 pos_or_neg = True # True = +, False = - if sum(a_list) >= 0: if n % 2 == 0: pos_or_neg = False else: pos_or_neg = True else: if n % 2 == 0: pos_or_neg = True else: pos_or_neg = False for a in a_list: total += a if pos_or_neg: while total <= 0: total += 1 count += 1 pos_or_neg = False else: while total >= 0: total -= 1 count += 1 pos_or_neg = True 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; int main(int argc, const char* argv[]) { int n; cin >> n; int a[10010]; for (int i = 0; i < n; ++i) cin >> a[i]; int res = 0; bool plus = false; int sum = a[0]; if (a[0] > 0) plus = true; else if (a[0] < 0) plus = false; int j = 1; while (sum == 0) { if (a[j] > 0) { ++res; sum = (j % 2 == 0) ? 1 : -1; plus = (j % 2 == 0) ? true : false; } else if (a[j] < 0) { ++res; sum = (j % 2 == 0) ? -1 : 1; plus = (j % 2 == 0) ? false : true; } ++j; if (j == n) { cout << 1 + 2 * (n - 1) << endl; } } for (int i = 0; i < n - 1; ++i) { if (sum + a[i + 1] > 0) { if (plus == true) { res += sum + a[i + 1] + 1; sum = -1; plus = false; } else { sum += a[i + 1]; plus = true; } } else if (sum + a[i + 1] < 0) { if (plus == false) { res += -(sum + a[i + 1] - 1); sum = 1; plus = true; } else { sum += a[i + 1]; plus = false; } } else if (sum + a[i + 1] == 0) { if (plus == true) { ++res; sum = -1; } else { ++res; sum = 1; } } } 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
java
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = Integer.parseInt(sc.next()); ArrayList<Integer> a = new ArrayList<>(); for(int i=0; i<n; i++){ a.add(Integer.parseInt(sc.next())); } int sign = 1; int ans1 = 0; int sum1 = 0; for (int i = 0; i < n; i++) { sum1 += a.get(i); if (sum1 * sign <= 0){ ans1 += Math.abs(sum1) + 1; sum1 = sign; } sign *= -1; } sign = -1; int ans2 = 0; int sum2 = 0; for (int i = 0; i < n; i++) { sum2 += a.get(i); if (sum2 * sign <= 0){ ans2 += Math.abs(sum2) + 1; sum2 = sign; } sign *= -1; } System.out.println(Math.min(ans1, ans2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#ifdef _DEBUG #include "MyLib.h" #else #define main_C main #include "bits/stdc++.h" #include <regex> #define _USE_MATH_DEFINES #include <math.h> #define FOR(i,s,e) for (int i = int(s); i < int(e); ++i) #define REP(i,e) FOR(i,0,e) #define INF (INT_MAX/2) #define EPS (1.0e-8) #define LINF (LONG_MAX/2) const int MGN = 8; const int ARY_SZ_MAX = 10000000; using namespace std; using ll = long long; using ull = unsigned long long; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>; using vl = vector<ll>; using vvl = vector<vl>; using vd = vector<double>; using vs = vector<string>; using pii = pair<int, int>; using pll = pair<ll, ll>; using psi = pair<string, int>; // functions #endif int main_C() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vl A(N); REP(i, N) cin >> A[i]; vl s(N); A.push_back(0); s.push_back(0); // +-+- int ans = INF; int cost = 0; s[0] = A[0]; FOR(i, 1, N+1) { if (i % 2 == 0 && s[i-1] <= 0){ cost += abs(s[i-1]) + 1; s[i-1] = 1; } else if (i % 2 == 1 && s[i-1] >= 0) { cost += abs(s[i-1]) + 1; s[i-1] = -1; } s[i] = s[i-1] + A[i]; } ans = min(ans, cost); // -+-+ cost = 0; s[0] = A[0]; FOR(i, 1, N+1) { if (i % 2 == 0 && s[i-1] >= 0){ cost += abs(s[i-1]) + 1; s[i-1] = -1; } else if (i % 2 == 1 && s[i-1] <= 0) { cost += abs(s[i-1]) + 1; s[i-1] = 1; } s[i] = s[i-1] + A[i]; } ans = min(ans, cost); 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
def main(): n = int(input()) a = list(map(int, input().split())) s = a[0] + a[1] ans = 0 for i in range(1, n - 1): ai = a[i + 1] if s > 0 and s + a[i + 1] >= 0: a[i + 1] = -s - 1 if 0 > s and 0 >= s + a[i + 1]: a[i + 1] = -s + 1 s += a[i + 1] ans += abs(ai - a[i + 1]) print(ans) main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, s; int count = 0; vector<int> a; cin >> n; for (int i = 0; i < n; ++i) { cin >> s; a.emplace_back(s); } int sum = a[0]; if (a[0] == 0) { count = count + 1; } for (int i = 0; i < n - 1; ++i) { if (i > 0) { sum = sum + a[i]; } if (sum * (sum + a[i + 1]) >= 0 && abs(sum) >= abs(sum + a[i + 1])) { count = count + abs(sum + a[i + 1]) + 1; if (sum + a[i + 1] < 0) { sum = sum + abs(sum + a[i + 1]) + 1; } else { sum = sum - abs(sum + a[i + 1]) - 1; } } if (sum * (sum + a[i + 1]) >= 0 && abs(sum) < abs(sum + a[i + 1])) { count = count + abs(sum) + 1; if (sum < 0) { sum = sum + abs(sum) + 1; } else { sum = sum - abs(sum) - 1; } } } cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#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); } for (i = 0; i < n; i++) { if (i % 2 == 0) sum2 += a[i]; else sum1 += a[i]; } tmp = a[0]; if (sum1 == sum2) { if (a[0] < 0) { sum1++; } else { sum2++; } } 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
python3
n = int(input()) a = list(map(int, input().split())) def check(f): flag = f t = 0 c = 0 for i in range(n): if t + a[i] <= 0 and flag == 1: c += 1 - t - a[i] t = 1 elif t + a[i] >= 0 and flag == -1: c += 1 + t + a[i] t = -1 else: t += a[i] flag *= -1 print(t) return c total_p = check(1) total_m = check(-1) print(min(total_p, total_m))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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()] ans = 0 tmp = a[0] for i in range(1,n): #print(tmp,ans) if tmp > 0: if tmp + a[i] >= 0: ans += tmp + a[i] + 1 tmp = -1 else: tmp += a[i] else: if tmp + a[i] <= 0: ans += abs(tmp + a[i]) + 1 tmp = 1 else: tmp += a[i] print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> a(N); for (int i = 0; i < N; i++) { cin >> a.at(i); } int ans1 = 0, ans2 = 0, sum = 0; for (int i = 0; i < N; i++) { sum += a.at(i); if (i % 2 == 0 && sum <= 0) { ans1 += 1 - sum; sum = 1; } else if (i % 2 != 0 && sum >= 0) { ans1 += sum + 1; sum = -1; } } sum = 0; for (int i = 0; i < N; i++) { sum += a.at(i); if (i % 2 == 0 && sum >= 0) { ans2 += sum + 1; sum = -1; } else if (i % 2 != 0 && sum <= 0) { ans2 += 1 - sum; sum = 1; } } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) a = [int(i) for i in input().split()] count = 0 if(a[0]==0): if (a[1] > 0): a[0] -= 1 elif (a[1] < 0): a[0] += 1 count += 1 for i in range(1,N): if (sum(a[:i]) == 0): if (sum(a[:i - 1]) > 0): a[i-1] -= 1 elif (sum(a[i - 1]) < 0): a[i-1] += 1 count += 1 if(sum(a[:i-1])*sum(a[:i]) > 0): k = abs(0-sum(a[:i-1]))+1 ll = k+abs(a[i-1]) if(a[i-1] > 0): a[i-1] -= ll elif (a[i-1] < 0): a[i-1] += ll count += ll print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1LL << 60; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<int> A(n); vector<int> B(n + 1); vector<int> B2(n + 1); B[0] = 0; B2[0] = 0; for (long long i = 0; i < n; i++) { cin >> A[i]; B[i + 1] = A[i] + B[i]; B2[i + 1] = B[i + 1]; } for (long long i = 0; i < n + 1; i++) { cout << B[i] << " "; } cout << endl; int sum_p = 0; int pm = 0; for (long long i = 1; i < n + 1; i++) { int del = 0; if (i % 2 && B[i] + pm <= 0) del = abs(B[i] + pm) + 1; if (i % 2 == 0 && B[i] + pm >= 0) del = -(B[i] + pm + 1); pm += del; sum_p += abs(del); } int sum_m = 0; pm = 0; for (long long i = 1; i < n + 1; i++) { int del = 0; if (i % 2 == 0 && B2[i] + pm <= 0) del = abs(B2[i] + pm) + 1; if (i % 2 && B2[i] + pm >= 0) del = -(B2[i] + pm + 1); pm += del; sum_m += abs(del); } cout << min(sum_p, sum_m) << 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(void) { long long int n, a[100010]; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; long long int sum1 = a[0], sum2 = -a[0], cnt1 = 0, cnt2 = 2 * abs(a[0]); if (sum1 == 0) { ++cnt1; if (a[1] > 0) sum1 = -1; else sum1 = 1; } for (int i = 1; i < n; ++i) { if (sum1 < 0) { if (sum1 + a[i] > 0) { sum1 += a[i]; } else { cnt1 += abs(sum1 + a[i]) + 1; sum1 = 1; } } else { if (sum1 + a[i] < 0) { sum1 += a[i]; } else { cnt1 += abs(sum1 + a[i]) + 1; sum1 = -1; } } } if (sum2 == 0) { ++cnt2; if (a[1] > 0) sum2 = 1; else sum2 = -1; } for (int i = 1; i < n; ++i) { if (sum2 < 0) { if (sum2 + a[i] > 0) { sum2 += a[i]; } else { cnt2 += abs(sum2 + a[i]) + 1; sum2 = 1; } } else { if (sum2 + a[i] < 0) { sum2 += a[i]; } else { cnt2 += abs(sum2 + a[i]) + 1; sum2 = -1; } } } cout << min(cnt1, cnt2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
integer N integer,allocatable,dimension(:)::A integer Asum,ans,preans read*,N allocate(A(N)) read*,A Asum=0;preans=0 do i=1,N Asum=Asum+A(i) select case(mod(i,2)) case(1) if(Asum<=0)then preans=preans+abs(1-Asum) Asum=1 endif case(0) if(Asum>=0)then preans=preans+abs(-1-Asum) Asum=-1 endif end select end do ans=preans Asum=0;preans=0 do i=1,N Asum=Asum+A(i) select case(mod(i,2)) case(0) if(Asum<=0)then preans=preans+abs(1-Asum) Asum=1 endif case(1) if(Asum>=0)then preans=preans+abs(-1-Asum) Asum=-1 endif end select end do ans=min(ans,preans) print"(i0)",ans end
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# # Written by NoKnowledgeGG @YlePhan # ('ω') # #import math #mod = 10**9+7 #import itertools #import fractions #import numpy as np #mod = 10**4 + 7 """def kiri(n,m): r_ = n / m if (r_ - (n // m)) > 0: return (n//m) + 1 else: return (n//m)""" """ n! mod m 階乗 mod = 1e9 + 7 N = 10000000 fac = [0] * N def ini(): fac[0] = 1 % mod for i in range(1,N): fac[i] = fac[i-1] * i % mod""" """mod = 1e9+7 N = 10000000 pw = [0] * N def ini(c): pw[0] = 1 % mod for i in range(1,N): pw[i] = pw[i-1] * c % mod""" """ def YEILD(): yield 'one' yield 'two' yield 'three' generator = YEILD() print(next(generator)) print(next(generator)) print(next(generator)) """ """def gcd_(a,b): if b == 0:#結局はc,0の最大公約数はcなのに return a return gcd_(a,a % b) # a = p * b + q""" """def extgcd(a,b,x,y): d = a if b!=0: d = extgcd(b,a%b,y,x) y -= (a//b) * x print(x,y) else: x = 1 y = 0 return d""" def readInts(): return list(map(int,input().split())) mod = 10**9 + 7 def main(): n = int(input()) A = readInts() # 符号 positive? #po_ = True # 変わったか変わってないか if A[0] >= 0: # if positive po_ = True else: # negative po_ = False Cost = 0 ANS = [0] * (n+1) ANS[0] = A[0] for i in range(1,n): #print(ANS[i-1],po_,ANS[i-1] + A[i]) if ANS[i-1]+A[i] >= 0 and not po_: # sumがpositiveで前がnegativeだった po_ = True if ANS[i-1]+A[i] == 0: A[i] -= 1 ANS[i] = ANS[i-1] + A[i] Cost += 1 ANS[i] = ANS[i-1] + A[i] # これで終わり elif ANS[i-1]+A[i] >= 0 and po_: # posi : posi ? # 負にしなければならない Cost += abs(-1 - (ANS[i-1]+A[i])) # 先にこれやれ A[i] += -1 - (ANS[i-1] + A[i]) # -4 ANS[i] = ANS[i-1] + A[i] po_ = False elif ANS[i-1]+A[i] < 0 and not po_: #nega : nega # -1 はここ # print(A[i]) Cost += abs(1 - (ANS[i-1]+A[i])) # 先にこれやれ A[i] += 1 - (ANS[i-1] + A[i]) ANS[i] = ANS[i-1] + A[i] po_ = True elif ANS[i-1]+A[i] < 0 and po_: # nega: pos po_ = False ANS[i] = ANS[i-1] + A[i] print(Cost) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a, sumA = 0, sumB = 0, mA = 0, mB = 0; for (int i = 1; i <= n; i++) { cin >> a; sumA += a; sumB += a; if (i % 2) { if (sumA <= 0) { mA += 1 - sumA; sumA = 1; } if (sumB >= 0) { mB += 1 + sumB; sumB = -1; } } else { if (sumA >= 0) { mA += 1 + sumA; sumA = -1; } if (sumB <= 0) { mB += 1 - sumB; sumB = 1; } } } cout << min(mA, mB) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int,input().split())) ret = 0 if A[0] == 0: i = 0 while A[i] == 0: i += 1 if (i%2 == 1 and A[i] > 0) or (i%2 == 0 and A[i] < 0): A[0] = -1 else: A[0] = 1 ret += 1 s = A[0] for e in A[1:]: #print(e,ret,s,s+e) if 0 < s and 0 <= s + e: ret += abs(s + e + 1) s = -1 elif s < 0 and s + e <= 0: ret += abs(s + e - 1) s = 1 else: s += e print(ret)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n,ans; cin>>n; vector<int>a(n); for(int i=0;i<n;i++){ cin>>a.at(i); } for(int i=0;i<n-1;i++){ int sum=0; for(int j=i;j>=0;j--)sum+=a.at(j);//sum while(p*a.at(i+1)>=0||p+a.at(i+1)==0){ if(a.at(i)>0){ a.at(i+1)--; ans++; } if(a.at(i)<0){ a.at(i+1)++; ans++; } } } cout<<ans<<endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long op = 0LL; 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
cpp
#include <bits/stdc++.h> using namespace std; vector<tuple<int, int, int> > V; vector<int> W; int N; int main() { cin >> N; for (auto i = 0; i < N; i++) { int a, b; cin >> a >> b; V.push_back(tuple<int, int, int>(a, b, 0)); } for (auto i = 0; i < N; i++) { int a, b; cin >> a >> b; V.push_back(tuple<int, int, int>(a, b, 1)); } sort(V.begin(), V.end()); int ans = 0; for (auto e : V) { int x = get<0>(e); int y = get<1>(e); int c = get<2>(e); if (c == 0) { W.push_back(y); } else { sort(W.begin(), W.end()); reverse(W.begin(), W.end()); for (auto it = W.begin(); it != W.end(); it++) { if (*it < y) { ans++; W.erase(it); break; } } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) ttl = a[0] cst = 0 if a[0]>0: flg = 1 elif a[0] == 0: cst += 1 ttl += 1 flg = 1 else: flg = -1 for i in range(1,n): ttl += a[i] if ttl*flg < 0: flg *= -1 else: if flg > 0: memo = abs(ttl)+abs(-1) ttl -= memo cst += memo elif flg < 0: memo = abs(ttl)+abs(-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()) _arr = list(map(int, input().split())) ans = [] for first in (_arr[0], 1, -1): arr = _arr[:] c = 0 prev = 0 for i in range(n): t = prev + arr[i] if i == 0: arr[i] = first elif prev > 0 and t >= 0: diff = t + 1 c += diff arr[i] -= diff elif prev < 0 and t <= 0: diff = -1 * t + 1 c += diff arr[i] += diff prev += arr[i] ans.append(c) print(min(ans))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1}; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {1, 0, -1, 0}; int n; int main(int argc, char const *argv[]) { cin.tie(0); ios::sync_with_stdio(false); cin >> n; int a[n]; for (int i = (0); i < (n); ++i) cin >> a[i]; bool sign; if (a[0] >= 0) sign = true; else sign = false; int sum; int count = 0; int reminder; sum = a[0]; for (int i = (1); i < (n); ++i) { if (sign) { sum += a[i]; if (sum >= 0) sign = true; else sign = false; if (sign) { reminder = abs(-1 - sum); count += reminder; sum = -1; sign = false; } } else { sum += a[i]; if (sum >= 0) sign = true; else sign = false; if (!sign) { reminder = abs(1 - sum); count += reminder; sum = 1; sum = true; } } } if (sum == 0) count++; cout << count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int d[n]; for (int i = 0; i < n; i++) cin >> d[i]; int sume = 0; int counte = 0; for (int i = 0; i < n; i++) { sume += d[i]; if (i % 2 == 0) { if (sume <= 0) { counte += 1 - sume; sume = 1; } } else { if (sume >= 0) { counte += sume + 1; sume = -1; } } } int sumo = 0; int counto = 0; for (int i = 0; i < n; i++) { sumo += d[i]; if (i % 2 == 1) { if (sumo <= 0) { counto += 1 - sumo; sumo = 1; } } else { if (sumo >= 0) { counto += sumo + 1; sumo = -1; } } } cout << min(counte, counto) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int main() { long long int n, a[100002], i, d[100002], b; scanf("%lld", &n); for (i = 1; i <= n; i++) { scanf("%lld", &a[i]); } d[1] = a[1]; b = 0; for (i = 2; i <= n; i++) { d[i] = d[i - 1] + a[i]; if (d[i] * d[i - 1] >= 0) { if (d[i - 1] < 0) { b = b + 1 - d[i]; d[i] = 1; } if (d[i - 1] > 0) { b = b + 1 + d[i]; d[i] = -1; } } } printf("%lld\n", b); 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) { long long i, j, k, ans = 0, be, n; scanf("%lld", &n); long long a[n]; for (i = 0; i < n; ++i) scanf("%lld", &a[i]); be = a[0] > 0 ? a[0] : 1; if (a[0] < 0) ans = 1 - a[0]; for (i = 1; i < n; ++i) { if (be > 0 && -1 < be + a[i]) ans += be + a[i] + 1, be = -1; else if (be < 0 && 1 > be + a[i]) ans += 1 - be - a[i], be = 1; else be += a[i]; } long long tmp = ans; ans = 0; be = a[0] < 0 ? a[0] : -1; if (a[0] > 0) ans = a[0] + 1; for (i = 1; i < n; ++i) { if (be > 0 && -1 < be + a[i]) ans += be + a[i] + 1, be = -1; else if (be < 0 && 1 > be + a[i]) ans += 1 - be - a[i], be = 1; else be += a[i]; } printf("%lld", (ans > tmp ? tmp : 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 sign(long long int num) { if (num == 0) { return 0; } else { return num / abs(num); } } int main() { int n; cin >> n; vector<long long int> list(n); for (int i = 0; i < n; i++) { cin >> list.at(i); } long long int count1 = 0, count2 = 0, sum = 0; for (int i = 0, s = 1; i < n; i++) { sum += list.at(i); if (sum * s <= 0) { count1 += abs(sum) + 1; sum = s; } s *= -1; } for (int i = 0, s = -1; i < n; i++) { sum += list.at(i); if (sum * s <= 0) { count2 += abs(sum) + 1; sum = s; } s *= -1; } cout << min(count1, 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(void) { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; long long op = 0; long long prev_sum = 0; for (int i = 0; i < n; i++) { long long new_sum = prev_sum + v[i]; if (i == 0 && v[i] != 0) prev_sum = new_sum; else if (i == 0 && v[i] == 0) { op += 1; prev_sum = 1; } else if (prev_sum >= 0 && new_sum >= 0) { op += new_sum + 1; prev_sum = -1; } else if (prev_sum <= 0 && new_sum <= 0) { op += -new_sum + 1; prev_sum = 1; } else prev_sum = new_sum; } prev_sum = 0; long long op2 = 0; for (int i = 0; i < n; i++) { long long new_sum = prev_sum + v[i]; if (i == 0 && v[i] != 0) prev_sum = new_sum; else if (i == 0 && v[i] == 0) { op2 += 1; prev_sum = -1; } else if (prev_sum >= 0 && new_sum >= 0) { op2 += new_sum + 1; prev_sum = -1; } else if (prev_sum <= 0 && new_sum <= 0) { op2 += -new_sum + 1; prev_sum = 1; } else prev_sum = new_sum; } cout << min(op, op2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, ans; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } for (int i = 0; i < n - 1; i++) { int p = 0; for (int j = i + 1; j >= 0; j--) p += a.at(j); while (a.at(i) * a.at(i + 1) >= 0 || p == 0) { if (a.at(i) > 0) { a.at(i + 1)--; ans++; if (p == 0) p++; } if (a.at(i) < 0) { a.at(i + 1)++; ans++; if (p == 0) 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 a[100050]; int sum[100050]; int main() { int n; scanf("%d", &n); int cnt1 = 0, cnt2 = 0; memset(sum, 0, sizeof(sum)); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); sum[i] = sum[i - 1] + a[i]; } int lazy1 = 0, lazy2 = 0; for (int i = 0; i < n; i++) { int sum1 = sum[i], sum2 = sum[i]; sum1 += lazy1; sum2 += lazy2; if (i % 2 == 0 && sum1 <= 0) { lazy1 += 1 - sum1; cnt1 += 1 - sum1; } else if (i % 2 == 1 && sum1 >= 0) { lazy1 -= 1 + sum1; cnt1 += sum1 + 1; } if (i % 2 == 0 && sum2 >= 0) { lazy2 -= 1 + sum2; cnt2 += sum2 + 1; } else if (i % 2 == 1 && sum2 <= 0) { lazy2 += 1 - sum2; cnt2 += 1 - sum2; } } printf("%d\n", 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; int main() { int n, count = 0; cin >> n; vector<int> v(n), ans(n); for (size_t i = 0; i < n; i++) { cin >> v[i]; } for (size_t i = 0; i < n; i++) { if (i == 0) { ans[i] = v[i]; } else { if (ans[i - 1] < 0 && ans[i - 1] + v[i] > 0 || ans[i - 1] > 0 && ans[i - 1] + v[i] < 0) { ans[i] = ans[i - 1] + v[i]; } else if (ans[i - 1] + v[i] == 0) { if (ans[i - 1] < 0) { count++; ans[i] = 1; } else { count++; ans[i] = -1; } } else { count += abs(ans[i - 1] + v[i]) + 1; if (ans[i - 1] + v[i] < 0) { ans[i] = 1; } else if (ans[i - 1] + v[i] > 0) { ans[i] = -1; } } } } cout << count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
module Main ( main ) where import Control.Applicative import Control.Monad import Data.List import Data.Array import Data.Char main = do n <- (read :: String -> Int) <$> getLine arr <- map (read :: String -> Int) . words <$> getLine print $ calcCost arr calcCost :: [Int] -> Int calcCost (x : xs) | x /= 0 = snd $ foldl update (x, 0) xs | otherwise = 1 + min (calcCost (1 : xs)) (calcCost ((-1) : xs)) update :: (Int, Int) -> Int -> (Int, Int) update (s, c) v = let next = calcNext s in if next * v > 0 && abs v > abs next then (s + v, c) else (s + next, c + abs (next - v)) calcNext :: Int -> Int calcNext a = if a > 0 then -a - 1 else -a + 1
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; import java.util.Arrays; 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[] sum1 = new int[n]; int[] sum2 = new int[n]; sum1[0] = a[0]; sum2[0] = a[0]; int count = 0; //System.out.println("sum > 0 : "+solve1(sum1,a,count)); //System.out.println("sum < 0 : "+solve2(sum2,a,count)); count = Math.min(solve1(sum1,a,count),solve2(sum2,a,count)); System.out.println(count); } public static int solve1(int[] sum,int[] a,int count){ if(sum[0] == 0){ count++; sum[0] = 1; } for(int i = 0;i < sum.length-1;i++){ sum[i+1] = sum[i] + a[i+1]; if((i+1) % 2 == 1){ if(sum[i+1] >= 0){ count += 1 + sum[i+1]; sum[i+1] = -1; } } if((i+1) % 2 == 0){ if(sum[i+1] <= 0){ count += 1 - sum[i+1]; sum[i+1] = 1; } } } return count; } public static int solve2(int[] sum,int[] a,int count){ if(sum[0] == 0){ count++; sum[0] = -1; } for(int i = 0;i < sum.length-1;i++){ sum[i+1] = sum[i] + a[i+1]; if((i+1) % 2 == 1){ if(sum[i+1] <= 0){ count += 1 - sum[i+1]; sum[i+1] = 1; } } if((i+1) % 2 == 0){ if(sum[i+1] >= 0){ count += 1 + sum[i+1]; sum[i+1] = -1; } } } return count; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
(defun solver () (let* ((n (read)) (numv (make-array n :fill-pointer 0)) (sum 0) (presum 0) (count 0)) (loop repeat n do (vector-push (read) numv)) (loop for x across numv do (incf sum x) (loop (cond ((and (zerop sum) (plusp presum)) (decf sum) (incf count)) ((and (zerop sum) (minusp presum)) (incf sum) (incf count)) ((and (plusp sum) (plusp presum)) (decf sum) (incf count)) ((and (plusp sum) (minusp presum)) (return)) ((and (minusp sum) (plusp presum)) (return)) ((and (minusp sum) (minusp presum)) (incf sum) (incf count)) (t (return)))) (setf presum sum)) (format t "~A~%" count))) (solver)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } int sum = a.at(0), ans = 0; for (int i = 1; i < n; i++) { if (sum > 0) { if (a.at(i) + sum >= 0) { ans += a.at(i) + sum + 1; a.at(i) -= a.at(i) + sum + 1; } } else if (sum < 0) { if (a.at(i) + sum <= 0) { ans += -(a.at(i) + sum - 1); a.at(i) += -(a.at(i) + sum - 1); } } sum += a.at(i); } 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; int a[100001]; int sumo[100001]; int sume[100001]; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i + 1]; } int anso = 0; int anse = 0; sumo[0] = 0; sume[0] = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 0) { if (sume[i - 1] + a[i] > 0) sume[i] = sume[i - 1] + a[i]; if (sume[i - 1] + a[i] <= 0) { sume[i] = 1; anse += (1 - (sume[i - 1] + a[i])); } if (sumo[i - 1] + a[i] < 0) sumo[i] = sumo[i - 1] + a[i]; if (sumo[i - 1] + a[i] >= 0) { sumo[i] = -1; anso += sumo[i - 1] + a[i] + 1; } } else if (i % 2 == 1) { if (sumo[i - 1] + a[i] > 0) sumo[i] = sumo[i - 1] + a[i]; if (sumo[i - 1] + a[i] <= 0) { sumo[i] = 1; anso += (1 - (sumo[i - 1] + a[i])); } if (sume[i - 1] + a[i] < 0) sume[i] = sume[i - 1] + a[i]; if (sume[i - 1] + a[i] >= 0) { sume[i] = -1; anse += (sume[i - 1] + a[i] + 1); } } } int ans; ans = min(anso, anse); 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 ans, n, a[100000], sum; int get_sign(int x) { if (0 < x) return 1; else if (x < 0) return -1; else return 0; } int main() { cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; int standard = get_sign(a[0]); for (int i = 0; i < n; ++i) { sum += a[i]; if (standard != get_sign(sum)) { int add = abs(sum) + 1; ans += add; if (sum > 0) sum -= add; else if (sum < 0) sum += add; } else if (get_sign(sum) == 0) { ans++; sum = get_sign(a[i + 1]) * (-1); } standard *= (-1); } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import itertools n=int(input()) a=list(map(int,input().split())) ans=0 res=0#0が正、1が負 if a[0]<0: res=1 for i in range(n-1): aa=list(itertools.accumulate(a)) if aa[i+1]<0: if res==1: adj = 1-aa[i+1] a[i+1]=a[i+1]+adj ans=ans+abs(adj) res = 0 else: res=1 if aa[i+1]>0: if res==0: adj = -1-aa[i+1] a[i+1]=a[i+1]+adj ans=ans+abs(adj) res = 1 else: res=0 if aa[i+1]==0: if res == 1: a[i+1]=a[i+1]+1 ans+=1 res=0 else: a[i+1]=a[i+1]-1 ans+=1 res=1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A2 = list(map(int,input().split())) #print(A) def getSign(a): if a < 0: return -1 elif a == 0: return 0 else: return 1 counts = [] for j in range(2): A = list(A2) count = 0 sumN = A[0] beforeSign = getSign(A[0]) if j == 0: add = -A[0] - beforeSign A[0] += add count += abs(add) sumN = A[0] beforeSign = getSign(A[0]) for i in range(1,N): sumN += A[i] #print("be",i,sumN,A[i],count) if 0 <= beforeSign * sumN: add = -sumN - beforeSign A[i] += add sumN += add count += abs(add) beforeSign = getSign(sumN) #print("af",i,sumN,A[i],count) counts.append(count) print(min(counts))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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 = list(map(int, input().split())) tmp = L[0] res = 0 def diff(a,b): if a*b < 0: return True else: return False if tmp > 0: flag = True elif tmp < 0: flag = False else: if L[1] > 0: tmp = -1 res += 1 else: tmp = 1 res += 1 for i in range(1, N): n = L[i] + tmp if diff(n, tmp): tmp = n else: if tmp < 0: res = res + abs(n) + 1 tmp = 1 else: res = res + abs(n) + 1 tmp = -1 print(res)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { 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 = 0; for (int i = 0; i < n; i++) { sumv += a[i]; if (i % 2 == 0 && sumv < 0) { cnt1 += abs(sumv) + 1; sumv = 1; } else if (i % 2 == 1 && sumv > 0) { cnt1 += abs(sumv) + 1; sumv = -1; } } sumv = 0; for (int i = 0; i < n; i++) { sumv += a[i]; if (i % 2 == 0 && sumv > 0) { cnt2 += abs(sumv) + 1; sumv = -1; } else if (i % 2 == 1 && sumv < 0) { cnt2 += abs(sumv) + 1; sumv = 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 = list(map(int, input().split())) X = [a[0]] ans = 0 #+-+-のとき if a[0] >= 0: for i in range(n-1): #print(X[i], a[i+1]) num = X[i] + a[i+1] if i % 2 != 0: #iが奇数の時はnumの値は正 if num < 0: X.append(1) ans += abs(num) + 1 else: X.append(num) else: #iが偶数のときnumの値は負 if num > 0: X.append(-1) ans += abs(num) + 1 else: X.append(num) #-+-+のとき else: for i in range(n-1): #print(X[i], a[i+1]) num = X[i] + a[i+1] if i % 2 != 0: #iが奇数の時はnumの値は負 if num < 0: X.append(num) else: ans += abs(num) + 1 X.append(-1) else: #iが偶数のときnumの値は正 if num > 0: X.append(num) else: X.append(1) ans += abs(num) + 1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } long long a[100010]; signed main() { long long n; cin >> n; for (long long i = 0; i < n; i++) { cin >> a[i]; } long long ans = a[0] > 0 ? 0 : 1 - a[0]; long long wa = a[0] > 0 ? a[0] : 1; for (long long i = 1; i < n; i++) { long long nxt = abs(wa) + 1; if (wa < 0) { if (nxt > a[i]) { ans += nxt - a[i]; wa += nxt; } else { wa += a[i]; } } else { nxt *= -1; if (nxt < a[i]) { ans += a[i] - nxt; wa += nxt; } else { wa += a[i]; } } } long long ans2 = a[0] < 0 ? 0 : -1 - a[0]; wa = a[0] < 0 ? a[0] : -1; for (long long i = 1; i < n; i++) { long long nxt = abs(wa) + 1; if (wa < 0) { if (nxt > a[i]) { ans2 += nxt - a[i]; wa += nxt; } else { wa += a[i]; } } else { nxt *= -1; if (nxt < a[i]) { ans2 += a[i] - nxt; wa += nxt; } else { wa += a[i]; } } } cout << min(ans, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const vector<int> di = {-1, 0, 1, 0}; const vector<int> dj = {0, 1, 0, -1}; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a.at(i); vector<int> s(n); int sum = 0; int ans = 0; for (int i = 0; i < n; i++) { if (i == 0) { s.at(i) = a.at(i); if (s.at(i) == 0) { if (a.at(i + 1) > 0) { s.at(i) = -1; ans++; } else { s.at(i) = 1; ans++; } } } if (i > 0) { s.at(i) = s.at(i - 1) + a.at(i); if (s.at(i - 1) * s.at(i) > 0 && s.at(i) < 0) { ans += 1 - s.at(i); s.at(i) = 1; } if (s.at(i - 1) * s.at(i) > 0 && s.at(i) > 0) { ans += s.at(i) + 1; s.at(i) = -1; } if (s.at(i - 1) * s.at(i) == 0) { if (s.at(i - 1) > 0) { s.at(i) = -1; ans++; } if (s.at(i - 1) < 0) { s.at(i) = 1; ans++; } } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long solve(vector<int> A) { long res = 0; long sum = A[0]; for (int i = 1; i < A.size(); i++) { if (sum > 0) { sum += A[i]; while (sum >= 0) { res++; sum--; } } else if (sum < 0) { sum += A[i]; while (sum <= 0) { res++; sum++; } } } return res; } int main() { int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } long res; if (A[0] != 0) { res = solve(A); cout << res << endl; } else { long res_first_plus = 1, res_first_minus = 1; A[0] = 1; res_first_plus += solve(A); A[0] = -1; res_first_minus += solve(A); res = min(res_first_plus, res_first_minus); cout << res << endl; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def c(ints): for i in range(len(ints)): if ints[i] != 0: sig = 1 if ints[i] > 0 else -1 total = ints[i] mov = i j = i break if i == len(ints) - 1: return i + 1 for i_ in ints[j+1:]: tmp = total + i_ if tmp == 0: mov +=1 tmp = -sig elif sig * tmp > 0: mov += abs(tmp) + 1 tmp = -sig sig *= -1 total = tmp return mov _ = input() inp = input() inp = inp.split(' ') inp = [int(i_) for i_ in inp] c(inp)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <iostream> #define rep(i, n) for(int i = 0; i < (n); i++) using namespace std; int main(){ int n; cin >> n; ll ary[n], cnt1 = 0, cnt2 = 0, sum = 0; rep(i, n) cin >> ary[i]; // S_odd < 0, S_even > 0 rep(i, n){ sum += ary[i]; if(i % 2 && sum >= 0){ cnt1 += sum + 1; sum = -1; } else if(i % 2 == 0 && sum <= 0){ cnt1 += -sum + 1; sum = 1; } } sum = 0; // S_odd > 0, S_even < 0 rep(i, n){ sum += ary[i]; if(i % 2 == 0 && sum >= 0){ cnt2 += sum + 1; sum = -1; } else if(i % 2 && sum <= 0){ cnt2 += -sum + 1; sum = 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
i=0 while True: i+=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
UNKNOWN
using System; class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); string[] A = Console.ReadLine().Split(); long ans = 1 << 60; int sum, sign, count, a; for (int cnt = 0; cnt < 2; ++cnt) { a = int.Parse(A[0]); sign = Math.Sign(a); if (cnt == 0) { sum = a; count = 0; } else { sign *= -1; sum = sign; count = Math.Abs(a) + 1; } for (int i = 1; i < n; ++i) { a = int.Parse(A[i]); sign *= -1; if (sign * Math.Sign(a + sum) > 0) { sum += a; } else { count += Math.Abs((Math.Abs(sum) + 1) * sign - a); sum = sign; } } ans = Math.Min(ans, count); } Console.WriteLine(ans); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) numbers = list(map(int, input().split())) counter = 0 sum_i_n = 0 sum_i_n_1 = numbers[0] for i in range(len(numbers) - 1): # sum_i_n = sum(numbers[:i + 1]) # sum_i_n_1 = sum(numbers[:i + 2]) sum_i_n += numbers[i] sum_i_n_1 += numbers[i + 1] if sum_i_n == 0: numbers[i] += 1 sum_i_n += 1 sum_i_n_1 += 1 counter += 1 if sum_i_n * sum_i_n_1 >= 0: sub = abs(sum_i_n_1) + 1 counter += sub if sum_i_n_1 > 0: numbers[i + 1] -= sub sum_i_n_1 -= sub else: numbers[i + 1] += sub sum_i_n_1 += sub if sum_i_n_1 == 0: counter += 1 print(counter)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N, a[100000]; cin >> N; for (int i = 0; i < N; ++i) cin >> a[i]; int counter = 0; if (a[0] >= 0) { for (int i = 0; i < N; ++i) { if (i % 2 == 0) { while (a[i] <= 0) { ++a[i]; ++counter; } } else { while (a[i] >= 0) { --a[i]; ++counter; } } } } else { for (int i = 1; i < N; ++i) { if (i % 2 == 0) { while (a[i] >= 0) { --a[i]; ++counter; } } else { while (a[i] <= 0) { ++a[i]; ++counter; } } } } cout << counter << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
from copy import copy n = int(input()) a = [int(x) for x in input().split()] ans1=[(-1)**i for i in range(n)] ans2=[(-1)**(i+1) for i in range(n)] b=copy(a) res_b=0 for i in range(n): if ans1[i]*sum(b[:i+1])>0: pass else: b[i]=ans1[i]-sum(b[:i]) res_b+=abs(b[i]-a[i]) c=copy(a) res_c=0 for i in range(n): if ans2[i]*sum(c[:i+1])>0: pass else: c[i]=ans2[i]-sum(c[:i]) res_c+=abs(c[i]-a[i]) print(min(res_b,res_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 <stdio.h> #include<math.h> int main(void) { // your code goes here long int a[100000]; int n,count= 0; scanf("%d",&n); int i,j,max=-1000000000; for(i = 0;i<n;i++) { scanf("%ld",&a[i]); if(a[i]+a[i-1]>max&& i>0); max = a[i]+a[i-1]; } if((a[0]+a[1]-max)<(a[0]+a[1]+max)); else max = -max; for(i = 0;i<n-1;i++) { int new; new = a[i]+a[i+1]; new = abs(new-max); count+=new; max = -max; } printf("%d",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
java
import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] nums = new long[n]; for(int i = 0; i < n; i++){ nums[i] = sc.nextInt(); } long result = 0; long sum = 0; // + - + - for(int i = 0; i < n; i++){ if(i % 2 == 0 && sum + nums[i] <= 0){ result += Math.abs(1 - (sum + nums[i])); sum = 1; } else if(i % 2 == 1 && sum + nums[i] >= 0){ result += Math.abs(-1 - (sum + nums[i])); sum = -1; } else{ sum += nums[i]; } } int result2 = 0; sum = 0; // - + - + for(int i = 0; i < n; i++){ if(i % 2 == 1 && sum + nums[i] <= 0){ result2 += Math.abs(1 - (sum + nums[i])); sum = 1; } else if(i % 2 == 0 && sum + nums[i] >= 0){ result2 += Math.abs(-1 - (sum + nums[i])); sum = -1; } else{ sum += nums[i]; } } System.out.println(Math.min(result, result2)); sc.close(); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n,ans,sum; cin>>n; vector<int>a(n); for(int i=0;i<n;i++){ cin>>a.at(i); } sum=a.at(0); for(int i=0;i<n-1;i++){ while(sum>0&&sum+a.at(i+1)>=0)||(sum<0&&sum+a.at(i+1)<=0)){ if(a.at(i)<0){ a.at(i+1)++; ans++; } if(a.at(i)>0){ a.at(i+1)--; ans++; } } sum+=a.at(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
python3
def c(ints): for i in range(len(ints)): if ints[i] != 0: sig = 1 if ints[i] > 0 else -1 total = ints[i] mov = i if i > 0: mov += 1 j = i break if i == len(ints) - 1: return i + 1 for i_ in ints[j+1:]: tmp = total + i_ if tmp == 0: mov +=1 tmp = -sig elif sig * tmp > 0: mov += abs(tmp) + 1 tmp = -sig sig *= -1 total = tmp return mov _ = input() inp = input() inp = inp.split(' ') inp = [int(i_) for i_ in inp] print(c(inp))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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 P = pair<int, int>; int main() { long long n; cin >> n; long long c[2], s[2]; long long a; for (int i = 0; i < (n); ++i) { cin >> a; for (int j : {0, 1}) { s[j] += a; auto p = 1 - (i + j) % 2 * 2; if (s[j] * p <= 0) { c[j] += abs(p - s[j]); s[j] = p; } } } cout << min(c[0], c[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 = input() A = list(map(int, input().split())) def Chk(a, pos): cnt = 0 tmp = 0 for a in A: tmp += a if pos and tmp < 1: cnt += 1 - tmp tmp = 1 elif not pos and tmp > -1: cnt += 1 + tmp tmp = -1 pos = not pos return cnt print(min(Chk(A, True), Chk(a, False)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; int INF = 1e9; template <typename T> struct BIT { int n; vector<T> d; BIT(int n = 0) : n(n), d(n + 1) {} void add(int i, T x = 1) { for (i++; i <= n; i += i & -i) { d[i] += x; } } T sum(int i) { T x = 0; for (i++; i > 0; i -= i & -i) { x += d[i]; } return x; } }; int main() { int n; cin >> n; vector<int> a(n); BIT<ll> tree1(100005), tree2(100005); for (int i = 0; i < (n); ++i) { cin >> a[i]; tree1.add(i, a[i]); tree2.add(i, a[i]); } ll ans = 1e18; ll count = 0; int flag; if (a[0] > 0) flag = 1; else flag = -1; for (int i = 1; i < n; i++) { ll sum = tree1.sum(i); if (sum * flag >= 0) { tree1.add(i, -(abs(sum) + 1) * flag); count += abs(sum) + 1; } flag *= -1; } ans = min(ans, count); if (a[0] > 0) flag = 1; else flag = -1; count = 0; for (int i = 0; i < n; i++) { ll sum = tree2.sum(i); if (sum * flag >= 0) { tree2.add(i, -(abs(sum) + 1) * flag); count += abs(sum) + 1; } flag *= -1; } ans = min(ans, count); cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void sum(int *N, long long *S, int n); int main() { int *N; long long *S; ; long long count_eve = 0, count_odd = 0, n; ; int j = 0, k = 0; cin >> n; N = new int[n]; S = new long 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 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
java
import java.util.*; public class Main { public static void main(String[] args) { Main main = new Main(); main.run(); } public void run() { Scanner sc = new Scanner(System.in); int n=sc.nextInt(); long sum = sc.nextLong(); long cnt = 0; for(int i=1; i<n; i++) { long v= sc.nextLong(); if(sum * v >= 0) { cnt += v + Math.abs(sum-v) + 1; if(sum<0) { v = 1; }else { v = -1; } }else { int s = 1; if(v<0) { s = -1; } if(Math.abs(sum)>=Math.abs(v)) { cnt+=Math.abs(sum)-Math.abs(v)+1; v += s*(Math.abs(sum)-Math.abs(v)+1); } } sum += v; } System.out.println(cnt); sc.close(); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int, input().split())) cntA, sumA = 0, 0 for i in range(N): sumA += A[i] if i % 2 == 0: if sumA < 0: cntA += abs(sumA) + 1 sumA += abs(sumA) + 1 else: if sumA > 0: cntA += abs(sumA) + 1 sumA -= abs(sumA) + 1 cntB, sumB = 0, 0 for i in range(N): sumB += A[i] if i % 2 != 0: if sumB > 0: cntB += abs(sumB) + 1 sumB += abs(sumB) + 1 else: if sumB > 0: cntB += abs(sumB) + 1 sumB -= abs(sumB) + 1 print(min(cntA, cntB))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def solve(N, As): i = As[0] positive = True if As[0] < 0 else False ans = 0 if i == 0: ans = 1 i = 1 for a in As[1:]: if positive: if i + a <= 0: ans += abs(1 - (i + a)) a += 1 - (i + a) else: if i + a >= 0: ans += abs(-1 - (i + a)) a += -1 - (i + a) i += a positive = not positive return ans if __name__ == "__main__": n = int(input()) As = list(map(int, input().split(" "))) print(solve(n, As))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } vector<long long> copy_a = a; long long count = 0; long long sum = 0; if (a[0] != 0) { for (int i = 0; i < n - 1; i++) { sum += a[i]; if (sum < 0 && sum + a[i + 1] <= 0) { long long na = 1 - sum; count += abs(na - a[i + 1]); a[i + 1] = na; } if (sum > 0 && sum + a[i + 1] >= 0) { long long na = -1 - sum; count += abs(na - a[i + 1]); a[i + 1] = na; } } } else if (a[0] == 0) { a[0] = 1; long long count1 = 1; for (int i = 0; i < n - 1; i++) { sum += a[i]; if (sum < 0 && sum + a[i + 1] <= 0) { long long na = 1 - sum; count1 += abs(na - a[i + 1]); a[i + 1] = na; } if (sum > 0 && sum + a[i + 1] >= 0) { long long na = -1 - sum; count1 += abs(na - a[i + 1]); a[i + 1] = na; } } copy_a[0] = -1; long long count2 = 1; long long sum2 = 0; for (int i = 0; i < n - 1; i++) { sum2 += copy_a[i]; if (sum2 < 0 && sum2 + copy_a[i + 1] <= 0) { long long na = 1 - sum2; count2 += abs(na - copy_a[i + 1]); copy_a[i + 1] = na; } if (sum2 > 0 && sum2 + copy_a[i + 1] >= 0) { long long na = -1 - sum2; count2 += abs(na - copy_a[i + 1]); copy_a[i + 1] = na; } } count = min(count1, count2); } cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> int main(void) { long long int i, a, n, num; long long int sum = 0, bsum = 0, ans = 0, m = 0; scanf("%lld", &n); for (i = 0; i < n; i++) { scanf("%lld", &a); bsum = sum; sum += a; if (bsum > 0) { if (sum > 0) { num = sum; do { num--; ans++; m++; } while (num >= 0); sum -= m; m = 0; } if (sum = 0) { ans++; sum -= 1; } } if (bsum < 0) { if (sum < 0) { num = sum; do { num++; ans++; m++; } while (num <= 0); sum += m; m = 0; } if (sum = 0) { ans++; sum += 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; const long long mod = 1000000007; const vector<int> di = {-1, 0, 1, 0}; const vector<int> dj = {0, 1, 0, -1}; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a.at(i); vector<int> s(n); int sum = 0; int ans = 0; for (int i = 0; i < n; i++) { if (i == 0) { s.at(i) = a.at(i); if (s.at(i) == 0) { if (a.at(i + 1) > 0) { s.at(i) = -1; ans++; } else { s.at(i) = 1; ans++; } } } if (i > 0) { s.at(i) = s.at(i - 1) + a.at(i); if (s.at(i - 1) * s.at(i) > 0 && s.at(i) < 0) { ans += 1 - s.at(i); s.at(i) = 1; continue; } if (s.at(i - 1) * s.at(i) > 0 && s.at(i) > 0) { ans += s.at(i) + 1; s.at(i) = -1; continue; } if (s.at(i - 1) * s.at(i) == 0) { if (s.at(i - 1) > 0) { s.at(i) = -1; ans++; continue; } if (s.at(i - 1) < 0) { s.at(i) = 1; ans++; } } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split(' '))) result = 0 h = 0 if a[0] == 0: a[0] += 1 result += 1 h = 1 counter = a[0] for i in range(1, n): if counter < 0: counter += a[i] if counter > 0: continue else: result += 1-counter counter = 1 else: counter += a[i] if counter < 0: continue else: result += counter+1 counter = -1 out = [] out.append(result) result = 0 if h == 1: result = 1 if a[0] > 0: result += a[0] +1 counter = -1 else: result += 1-a[0] counter = 1 for i in range(1, n): if counter < 0: counter += a[i] if counter > 0: continue else: result += 1-counter counter = 1 else: counter += a[i] if counter < 0: continue else: result += counter+1 counter = -1 out.append(result) print(min(out))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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 < (int)(n); i++) { cin >> a[i]; } int s = a[0]; int ss = a[0]; long long ans = 0; for (int i = 1; i < n; i++) { s = ss; ss = s + a[i]; if (ss * s < 0) continue; if (ss == 0) { ans += 1; ss = -s / abs(s); } else { ans += abs(ss - (-ss / abs(ss))); ss = -ss / abs(ss); } } 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> a(n), sum1(n), sum2(n); long ans1 = 0, ans2 = 0; for (int i = 1; i < n; i++) { cin >> a[i]; if (i > 0) { sum1[i] = sum1[i - 1] + a[i]; sum2[i] = sum2[i - 1] + a[i]; } else sum1[0] = sum2[0] = a[0]; if (i % 2 == 0) { if (sum1[i] <= 0) { ans1 += abs(1 - sum1[i]); sum1[i] = 1; } if (sum2[i] >= 0) { ans2 += abs(-1 - sum2[i]); sum2[i] = -1; } } if (i % 2 == 1) { if (sum1[i] >= 0) { ans1 += abs(-1 - sum1[i]); sum1[i] = -1; } if (sum2[i] <= 0) { ans2 += abs(1 - sum2[i]); sum2[i] = 1; } } } cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, a, cnt[25]; long long sum, ans; int main() { scanf("%d", &n); for (int i = 1, x = 0; i <= n; i++) { scanf("%d", &a); x ^= a; for (int j = 0; j <= 16; j++) { cnt[j] += x >> j & 1; } } for (int k = 0; k <= 16; k++) { ans += (n + 1ll - cnt[k]) * cnt[k] << k; } printf("%lld", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys import numpy as np import copy sys.setrecursionlimit(10 ** 6) INF = float("inf") MOD = 10 ** 9 + 7 def input(): return sys.stdin.readline().strip() def main(): N = int(input()) A = list(map(int, input().split())) c1 = np.cumsum(A) c2 = copy.deepcopy(c1) # + - + - の順番 cnt = 0 if c1[0] > 0 else abs(c1[0]) + 1 val = 0 if c1[0] > 0 else abs(c1[0]) + 1 for i in range(N): if i % 2 == 0: # + c1[i] += val if c1[i] > 0: pass else: cnt += abs(c1[i]) + 1 val += abs(c1[i]) + 1 else: # - c1[i] += val if c1[i] < 0: pass else: cnt += c1[i] + 1 val -= c1[i] + 1 ans = cnt # - + - +の順番 cnt = 0 if c2[0] < 0 else abs(c2[0]) + 1 val = 0 if c2[0] < 0 else abs(c2[0]) + 1 for i in range(N): if i % 2 == 1: # + c2[i] += val if c2[i] > 0: pass else: cnt += abs(c2[i]) + 1 val += abs(c2[i]) + 1 else: # - c2[i] += val if c2[i] < 0: pass else: cnt += c2[i] + 1 val -= c2[i] + 1 if ans > cnt: ans = cnt print(ans) if __name__ == "__main__": main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; namespace ProgramingStydying { class Program { static void Main(string[] args) { var n = int.Parse(Console.ReadLine()); var a = Console.ReadLine().Split().Select(int.Parse).ToList(); var ans = 0; if(a[0] == 0) { ans = Math.Min(Solve(n, a, 1), Solve(n, a, -1)) + 1; } else if(a[0] > 0) { ans = Math.Min(Solve(n, a, a[0]), Solve(n, a, -1) + a[0] + 1); } else { ans = Math.Min(Solve(n, a, a[0]), Solve(n, a, 1) - a[0] + 1); } Console.WriteLine(ans); } static int Solve(int n, List<int> a, int sum) { var ans = 0; for (int i = 1; i < n; i++) { if (sum > 0) { sum += a[i]; if (sum < 0) { continue; } else { ans += sum + 1; sum = -1; } } else if (sum < 0) { sum += a[i]; if (sum > 0) { continue; } else { ans += -sum + 1; sum = 1; } } } return ans; } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) cnt = 0 total = 0 if a[0] > 0: code = -1 elif a[0] < 0: code = 1 else: if a[1] < 0: code = -1 else: code = 1 for i in range(n): total += a[i] if total != 0: if total * code < 0: code *= -1 else: cnt += total * code + 1 total = code * -1 code *= -1 else: total = code * -1 cnt += 1 code *= -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; using ll = long long; ll counter(int N, vector<ll> A, ll sum, ll ans) { for (int i = 1; i < N; i++) { if (sum > 0 && sum + A.at(i) >= 0) { ans += sum + A.at(i) + 1; sum = -1; } else if (sum < 0 && sum + A.at(i) <= 0) { ans += -(sum + A.at(i)) + 1; sum = 1; } else sum += A.at(i); } return ans; } int main() { int N; cin >> N; vector<ll> A(N); for (int i = 0; i < N; i++) cin >> A.at(i); ll sum = A.at(0); ll ans = 0; if (sum != 0) { cout << counter(N, A, sum, ans) << endl; return 0; } ans = min(counter(N, A, sum + 1, ans + 1), counter(N, A, sum - 1, ans + 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
python3
import sys input = sys.stdin.readline sys.setrecursionlimit(2147483647) INF=float("inf") MOD=10**9+7 # A = [ int(input()) for _ in range(N) ] ############################## N = int(input()) A = list(map(int, input().split())) def get_count(summary): count = 0 for i in range(1, N): # print(summary) # 次はマイナス if summary > 0: # 条件を満たしてる? if (summary + A[i]) < 0: summary += A[i] else: # プラスになっちゃってるので修正 summary += A[i] count += abs(-1-summary) summary = -1 # 次はプラス else: if (summary + A[i]) > 0: summary += A[i] else: # マイナスになっちゃってるので修正 summary += A[i] count += abs(1-summary) summary = 1 return count if A[0] > 0: plus = get_count(A[0]) minus = get_count(-1) minus += (A[0]+1) elif A[0] == 0: plus = get_count(0)+1 minus = get_count(-1)+1 else: minus = get_count(A[0]) plus = get_count(1) plus += (abs(plus)+1) print(min(plus, minus))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
//Sequence.cpp (C) #include <iostream> using namespace std; long long ans(int n, int*, int change); int main(){ int n; int a[110000]; cin >> n; for(int i = 0; i < n; i++) cin >> a[i]; long long pAns,nAns; pAns = ans(n,a,1); nAns = ans(n,a,-1); printf("%d\n",min(pAns,nAns)); } int ans(int n, int *a, int change){ long long Ans = 0; long long sum = 0; for(int i = 0; i < n; i++){ sum += a[i]; switch (change) { case -1: if(sum > -1) Ans += 1 + sum, sum = -1; change *= -1; break; case 1: if(sum < 1) Ans += 1 - sum, sum = 1; change *= -1; break; } } return Ans; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) s, ans, i = 0,0,0 while a[i] == 0 and i < n: ans += 2 i += 1 ans = max(0, ans - 1) if i == n: print(ans) exit() if ans > 0: if abs(a[i]) == 1: ans += 1 s = a[i] // abs(a[i]) else: s = a[0] i += 1 for j in range(i,n): if abs(a[j]) > abs(s) and a[j] // abs(a[j]) != s // abs(s): s += a[j] else: s = -1 * s // abs(s) ans += abs(abs(a[j]) - 2) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
import java.io.InputStreamReader import java.io.BufferedReader fun main(args: Array<String>) { val br = BufferedReader(InputStreamReader(System.`in`)) br.readLine() val a = br.readLine().split(" ").map { it.toInt() } .toIntArray() fun solve(init: Int): Int { return a.foldIndexed(Pair(0,0)) { i, acc, e -> val v = acc.second + e // println("${i}: ${acc}, ${v}") if(i % 2 == init) { if(v > 0) Pair(acc.first, v) else Pair(acc.first - v + 1, 1) } else { if(v < 0) Pair(acc.first, v) else Pair(acc.first + v + 1, -1) } }.first } val ans = Math.min(solve(0), solve(1)) println(ans) }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) a_t = [0] for i in range(n): a_t.append(a[i] + a_t[i]) ans = 0 count = 0 for i in range(2, n + 1): if a_t[i - 1] + count > 0: if a_t[i] + count > 0: ans += a_t[i] + count + 1 count += -(a_t[i] + count) - 1 if a_t[i] + count == 0: ans += 1 count -= 1 elif a_t[i - 1] + count < 0: if a_t[i] + count < 0: ans += -(a_t[i] + count) + 1 count += -(a_t[i] + count) + 1 if a_t[i] + count == 0: ans += 1 count += 1 print(ans)