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; template <class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first - r.first, l.second - r.second); } const long long int MOD = 1e9 + 7, INF = 1e18; long long int N, arr[100000], sums[100000]; int main() { cin.tie(0); ios_base::sync_with_stdio(false); cin >> N; for (long long int i = (0), i_end_ = (N); i < i_end_; i++) { cin >> arr[i]; } bool flag; long long int sum = 0; long long int ans = 0; sums[0] = arr[0]; for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) { sums[i + 1] = arr[i + 1] + arr[i]; } if (sums[0] > 0) flag = true; else flag = false; for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) { sums[i + 1] += sum; if (flag ^ ((i % 2) == 1)) { if (sums[i + 1] >= 0) { sum -= (sums[i + 1] + 1); ans += abs(sums[i + 1] + 1); sums[i + 1] -= (sums[i + 1] + 1); } } else { if (sums[i + 1] <= 0) { sum -= (sums[i + 1] - 1); ans += abs(sums[i + 1] - 1); sums[i + 1] -= (sums[i + 1] - 1); } } } long long int tmp = ans; sum = 0; ans = 0; sums[0] = arr[0]; for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) { sums[i + 1] = arr[i + 1] + arr[i]; } if (sums[0] >= 0) flag = true; else flag = false; for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) { sums[i + 1] += sum; if (flag ^ ((i % 2) == 1)) { if (sums[i + 1] >= 0) { sum -= (sums[i + 1] + 1); ans += abs(sums[i + 1] + 1); sums[i + 1] -= (sums[i + 1] + 1); } } else { if (sums[i + 1] <= 0) { sum -= (sums[i + 1] - 1); ans += abs(sums[i + 1] - 1); sums[i + 1] -= (sums[i + 1] - 1); } } } cout << min(tmp, ans) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = list(map(int, input().split())) ans = 0 acc = A[0] for a in A[1:]: if acc + a == 0: acc = 1 if acc < 0 else -1 ans += 1 elif (acc < 0) != (acc + a > 0): ope = -(acc + a + (1 if acc + a > 0 else -1)) acc = acc + a + ope ans += abs(ope) else: acc += a print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = [int(i) for i in input().split()] S = A[0] total = 0 for i in range(1, N): if S > 0: if S + A[i] < 0: S += A[i] else: total += abs(-1 - (S + A[i])) S = -1 elif S < 0: if S + A[i] > 0: S += A[i] else: total += 1 - (S + A[i]) S = 1 print(total)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } long long ans = 0; long long sum = 0; for (int i = 0; i < n; i++) { if (i == 0) { sum = a[i]; continue; } if (sum > 0) { if (sum + a[i] >= 0) { ans += sum + a[i] + 1; a[i] -= sum + a[i] + 1; } } else { if (sum + a[i] <= 0) { ans -= sum + a[i] - 1; a[i] -= sum + a[i] - 1; } } sum += a[i]; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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 = sc.nextInt(); int a[] = new int[n]; for(int i = 0;i<a.length;i++) { a[i] = sc.nextInt(); } int b[] = new int[n]; b[0] = a[0]; int count = 0; for(int i = 0;i<a.length-1;i++) { b[i+1] = b[i] + a[i+1]; if(b[i+1]*b[i]>0) { count += Math.abs(b[i+1])+1; if(b[i+1]>0) b[i+1]=-1; else b[i+1] = 1; } else if(b[i+1] == 0) { if(b[i]>0) b[i+1]=-1; else b[i+1] = 1; count++; } } System.out.println(count); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[100010]; long long sum1 = 0; long long sum2 = 0; long long res1 = 0; long long res2 = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; sum1 += a[i]; sum2 += a[i]; if (a[1] >= 0) { if (i % 2 == 0) { if (sum1 < 0) continue; else if (sum1 >= 0) { res1 += sum1 + 1; sum1 = -1; } } else if (i % 2 == 1) { if (sum1 > 0) continue; else if (sum1 <= 0) { res1 += -sum1 + 1; sum1 = 1; } } } else if (a[1] <= 0) { if (i % 2 == 0) { if (sum2 > 0) continue; else if (sum2 <= 0) { res2 += -sum2 + 1; sum2 = 1; } } else if (i % 2 == 1) { if (sum2 < 0) continue; else if (sum2 >= 0) { res2 += sum2 + 1; sum2 = -1; } } } } if (a[1] == 0) cout << min(res1, res2) << endl; else cout << max(res1, res2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) a = [int(i) for i in input().split()] def pura(a): A = a[:] ans = 0 check = 0 if A[0] <= 0: ans += 1 + abs(A[0]) A[0] = 1 check += A[0] for i in range(1, N): if i % 2 != 0: if check + A[i] >= 0: ans += abs(A[i] + 1 + check) A[i] = -1 + check else: if check + A[i] <= 0: ans += abs(A[i] - (1 + check)) A[i] = 1 - check check += A[i] return ans def mai(a): A = a[:] ans = 0 check = 0 if A[0] >= 1: ans +=1+abs(A[0]) A[0] = -1 check += A[0] for i in range(1, N): if i % 2 != 0: if check + A[i] <= 0: ans += abs(A[i] - (1 - check)) A[i] = 1 - check else: if check + A[i] >= 0: ans += abs(A[i] +( 1 + check)) A[i] = -1 - check check += A[i] return ans print(min(pura(a), mai(a)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long calc(bool plus, vector<long long> a) { long long ret = 0; int n = a.size(); if (a[0] == 0) { if (plus) { a[0] = 1; } else { a[0] = -1; } ret++; } for (int i = 1; i <= n - 1; i++) { a[i] += a[i - 1]; if (a[i] == 0) { if (a[i - 1] < 0) a[i] = 1; if (a[i - 1] > 0) a[i] = -1; ret++; } else if (a[i] > 0 && a[i - 1] > 0) { ret += a[i] + 1; a[i] = -1; } else if (a[i] < 0 && a[i - 1] < 0) { ret += 1 - a[i]; a[i] = 1; } } return ret; } int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; cout << min(calc(true, a), calc(false, a)) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ull = uint64_t; using ll = int64_t; using PII = pair<int, int>; using VI = vector<int>; string to_string(string s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto& x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } int main() { ios::sync_with_stdio(false), cin.tie(0); int N; cin >> N; vector<ll> V(N); for (int _n = N, i = 0; i < _n; ++i) cin >> V[i]; if (V[0]) { ll sum = V[0]; ull ans = 0; for (int i = (1), _b = (N - 1); i <= _b; ++i) { ll nsum = sum + V[i]; ll target = (ll)-1 * (sum / abs(sum)); if (nsum == 0) { ans += abs(target - nsum); sum = target; } else { ll nsign = nsum / abs(nsum); if (nsign == target) { sum = nsum; continue; } else { ans += abs(target - nsum); sum = target; } } } cout << ans << endl; } else { ull ans1 = 1; ll sum = 1; for (int i = (1), _b = (N - 1); i <= _b; ++i) { ll nsum = sum + V[i]; ll target = (ll)-1 * (sum / abs(sum)); if (nsum == 0) { ans1 += abs(target - nsum); sum = target; } else { ll nsign = nsum / abs(nsum); if (nsign == target) { sum = nsum; continue; } else { ans1 += abs(target - nsum); sum = target; } } } ull ans2 = 1; sum = -1; for (int i = (1), _b = (N - 1); i <= _b; ++i) { ll nsum = sum + V[i]; ll target = (ll)-1 * (sum / abs(sum)); if (nsum == 0) { ans2 += abs(target - nsum); sum = target; } else { ll nsign = nsum / abs(nsum); if (nsign == target) { sum = nsum; continue; } else { ans2 += abs(target - nsum); sum = target; } } } 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; using ll = int64_t; int main() { ll n; cin >> n; ll a[n]; for (ll i = 0; i < n; i++) cin >> a[i]; ll sum = a[0]; ll cost = 0; for (ll j = 1; j < n; j++) { if (sum + a[j] != 0 && (sum + a[j]) / (ll)abs(sum + a[j]) * sum / (ll)abs(sum) == -1) { sum += a[j]; } else { ll sign = sum / (ll)abs(sum); ll aim = sign * -1; ll tmp = abs(aim - sum) * aim; cost += abs(tmp - a[j]); sum = aim; } } cout << cost << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> 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 = 0; long long wa = a[0]; 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]; } } } 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
java
import java.util.Arrays; 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()); int[] a1 = new int[n]; int[] a2 = new int[n]; for (int i = 0; i < n; i++) { int temp = Integer.parseInt(sc.next()); a1[i] = temp; a2[i] = temp; } int ans1 = 0; int ans2 = 0; long temp = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && temp + a1[i] >= 0) { ans1 += temp + a1[i] + 1; a1[i] -= temp + a1[i] + 1; } if (i % 2 != 0 && temp + a1[i] <= 0) { ans1 += Math.abs(temp + a1[i] - 1); a1[i] += Math.abs(temp + a1[i] - 1); } temp += a1[i]; } if (Arrays.stream(a1).sum() == 0) { ans1++; } temp = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && temp + a2[i] <= 0) { ans2 += Math.abs(temp + a2[i] - 1); a2[i] += Math.abs(temp + a2[i] - 1); } if (i % 2 != 0 && temp + a2[i] >= 0) { ans2 += temp + a2[i] + 1; a1[i] -= temp + a2[i] + 1; } temp += a2[i]; } if (Arrays.stream(a2).sum() == 0) { ans2++; } 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
#include <bits/stdc++.h> using namespace std; int main() { int n, ans; cin >> n; int A[n]; for (int i = 0; i<n; i++) cin >> A[i]; int cnt=0, acm=0, ans=0; for(int i = 0; i<n; i++) { if(i%2) { if(acm+A[i]>0) acm+=A[i]; else { cnt += abs(acm+A[i]) + 1; acm = 1; } } else { if(acm+A[i]<0) acm+=A[i]; else { cnt += abs(acm+A[i]) + 1; acm = -1; } } } ans = cnt; cnt=0; acm=0; for(int i = 0; i<n; i++) { if((i+1)%2) { if(acm+A[i]>0) acm+=A[i]; else { cnt += abs(acm+A[i]) + 1; acm = 1; } } else { if(acm+A[i]<0) acm+=A[i]; else { cnt += abs(acm+A[i]) + 1; acm = -1; } } } ans = min(ans,cnt); cout << ans << "\n"; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# https://atcoder.jp/contests/abc059/tasks/arc072_a # 2020/02/21 import sys input = sys.stdin.readline def main(): n = int(input()) a_list = list(map(int, input().split())) a_sum = a_list[0] if a_list[0] > 0: sign = "plus" else: sign = "minus" ans = 0 for i in range(1, n): if sign == "plus": sign = "minus" if a_sum + a_list[i] == 0: a_sum -= 1 ans += 1 elif a_sum + a_list[i] > 0: ans += a_sum + 1 + a_list[i] a_sum = -1 else: a_sum += a_list[i] elif sign == "minus": sign = "plus" if a_sum + a_list[i] == 0: a_sum += 1 ans += 1 elif a_sum + a_list[i] < 0: ans += (-1 * a_sum + 1 + -1 * a_list[i]) a_sum = 1 else: a_sum += a_list[i] 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int num[100005]; int tot = 0; int fir = 0; int sec = 0; for (int i = 0; i < n; i++) { cin >> num[i]; } int jud = 1; for (int i = 0; i < n; i++) { tot += num[i]; if (tot > 0 && jud == 1) { jud *= -1; } else if (tot < 0 && jud == -1) { jud *= -1; } else if (tot >= 0 && jud == -1) { fir += abs(-1 - tot); tot += (-1 - tot); jud *= -1; } else if (tot <= 0 && jud == 1) { fir += abs(1 - tot); tot += (1 - tot); jud *= -1; } } jud = -1; tot = 0; for (int i = 0; i < n; i++) { tot += num[i]; cout << tot << " " << jud << endl; if (tot > 0 && jud == 1) { jud *= -1; } else if (tot < 0 && jud == 1) { jud *= -1; } else if (tot >= 0 && jud == -1) { sec += abs(-1 - tot); tot += -1 - num[i]; jud *= -1; cout << tot << " " << jud << endl; } else if (tot <= 0 && jud == 1) { sec += abs(1 - tot); tot += 1 - num[i]; jud *= -1; cout << tot << " " << jud << endl; } } cout << min(fir, sec) << " " << fir << " " << sec << 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())) a2 = a.copy() ans1 = 0 #偶数インデックスが正 for i in range(n): s1 = sum(a[:i+1]) if i % 2 == 0: if s1 <= 0: x = abs(s1) + 1 ans1 += x a[i] += x else: continue else: if s1 >= 0: x = abs(s1) + 1 ans1 += x a[i] -= x ans2 = 0 #偶数インデックスが負 for i in range(n): s2 = sum(a2[:i+1]) if i % 2 == 1: if s2 <= 0: x = abs(s2) + 1 ans2 += x a2[i] += x else: continue else: if s2 >= 0: x = abs(s2) + 1 ans2 += x a2[i] -= x print(min(ans1, ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
function Main(s) { var s = s.split("\n"); var n = parseInt(s[0], 10); var a = s[1].split(" ").map(e => parseInt(e, 10)); var acc = 0, cnt = 0, arr = []; for (var i = 0; i < n; i++) { acc += a[i]; if (i === 0) { if (acc === 0) { if (a[i + 1] >= 0) { acc -= (a[i + 1] - 1); if (acc === 0) acc--; cnt += Math.abs(acc); } else { acc += (Math.abs(a[i + 1]) - 1); if (acc === 0) acc++; cnt += acc; } } } else { if (arr[i - 1] > 0) { if (acc >= 0) { cnt += (acc + 1); acc -= (acc + 1); } } else { if (acc <= 0) { cnt += (Math.abs(acc) + 1); acc += (Math.abs(acc) + 1); } } } arr.push(acc); } console.log(cnt); } Main(require("fs").readFileSync("/dev/stdin", "utf8"));
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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())) cnt1 = 0 cnt2 = 0 total = 0 for i in range(len(a)): total += a[i] if i % 2 == 0: if total >= 0: cnt1 += 1 + total total -= 1 + total else: if total <= 0: cnt1 += 1 - total total += 1 + total total = 0 for i in range(len(a)): total += a[i] if i % 2 != 0: if total >= 0: cnt2 += 1 + total total -= 1 + total else: if total <= 0: cnt2 += 1 - total total += 1 + total 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 ll = long long; using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < (int)(n); i++) cin >> a.at(i); ll ans = 0, sum = 0; bool f = false; for (int i = 0; i < (int)(n); i++) { int now = a.at(i); if (f == false) { if (now == 0) { if (i + 1 < n) { if (a.at(i + 1) != 0) f = true; if (a.at(i + 1) > 0) sum--; else if (a.at(i + 1) < 0) sum++; } ans++; } else { f = true; sum += now; } continue; } if ((sum < 0 && sum + now > 0) || (sum > 0 && sum + now < 0)) { sum += now; } else { ll add = abs(sum + now) + 1; if (sum < 0) sum = 1; else sum = -1; ans += add; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys def input(): return sys.stdin.readline().strip() sys.setrecursionlimit(20000000) def main(): N = int(input()) A = list(map(int, input().split())) if A[0] == 0: answer = [] for a in (1, -1): cnt = 1 S = a for i in range(1, N): s = S + A[i] if A[i] == 0: if S > 0: cnt += S + 1 S = -1 else: cnt += abs(S) + 1 S = 1 else: if s == 0: if S < 0: cnt += 1 S = 1 else: cnt += 1 S = -1 else: if S * s > 0: if S < 0: cnt += abs(s) + 1 S = 1 else: cnt += s + 1 S = -1 else: S = s answer.append(cnt) print(min(answer)) else: if A[0] >0: cnt1 = 0 S1 = A[0] cnt2 = A[0]+1 S1 = -1 else: cnt1 = 0 S1= A[0] cnt2 = abs(A[0])+1 S2 = 1 answer = [] for cnt,S in ((cnt1,S1),(cnt2,S2)): for i in range(1, N): s = S + A[i] if A[i] == 0: if S > 0: cnt += S + 1 S = -1 else: cnt += abs(S) + 1 S = 1 else: if s == 0: if S < 0: cnt += 1 S = 1 else: cnt += 1 S = -1 else: if S * s > 0: if S < 0: cnt += abs(s) + 1 S = 1 else: cnt += s + 1 S = -1 else: S = s answer.append(cnt) print(min(answer)) 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; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } template <class T> inline T sqr(T x) { return x * x; } const double EPS = 1e-10; const double PI = acos(-1.0); const int INF = (int)1000000007; const long long MOD = (long long)1000000007; const long long INF2 = (long long)100000000000000000; int main() { long long n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } long long sum = 0; long long tmp1 = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { sum += a[i]; if (sum < 0) { tmp1 += (1 - sum); sum = 1; } } else { sum += a[i]; if (sum > 0) { tmp1 += (sum + 1); sum = -1; } } } sum = 0; long long tmp2 = 0; for (int i = 0; i < n; i++) { if (i % 2 == 1) { sum += a[i]; if (sum < 0) { tmp2 += (1 - sum); sum = 1; } } else { sum += a[i]; if (sum > 0) { tmp2 += (sum + 1); sum = -1; } } } cout << min(tmp1, tmp2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int top = a[0], cnt = 0; bool sign = (top >= 0 ? true : false); for (int i = 1; i < n; i++) { if (sign && top + a[i] < 0) { top += a[i]; sign = false; } else if (!sign && top + a[i] > 0) { top += a[i]; sign = true; } else if (top + a[i] == 0) { cnt++; if (sign) { top = -1; sign = false; } else { top = 1; sign = false; } } else { if (sign) { cnt += (top + a[i]) + 1; top = -1; sign = false; } else { cnt += 1 - (top + a[i]); top = 1; sign = true; } } } int t = cnt; top = a[0], cnt = a[0] + 1; sign = (top <= 0 ? true : false); for (int i = 1; i < n; i++) { if (sign && top + a[i] < 0) { top += a[i]; sign = false; } else if (!sign && top + a[i] > 0) { top += a[i]; sign = true; } else if (top + a[i] == 0) { cnt++; if (sign) { top = -1; sign = false; } else { top = 1; sign = false; } } else { if (sign) { cnt += (top + a[i]) + 1; top = -1; sign = false; } else { cnt += 1 - (top + a[i]); top = 1; sign = true; } } } cnt = min(cnt, t); cout << cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; int flag[100005], k[100005]; long long a[100005], sum[100005], ans, b[100005], tot[100005], ant; int main() { int m = 0; scanf("%d", &n); scanf("%lld", &a[1]); b[1] = a[1]; sum[1] = a[1]; tot[1] = sum[1]; if (sum[1] > 0) flag[1] = 1; if (sum[1] < 0) flag[1] = 0; if (sum[1] == 0) m = 1; if (m == 0) { for (int i = 2; i <= n; i++) { scanf("%lld", &a[i]); sum[i] = a[i] + sum[i - 1]; if (sum[i] > 0) flag[i] = 1; if (sum[i] < 0) flag[i] = 0; if (flag[i - 1] == 1) { if (sum[i] >= 0) { ans += sum[i] + 1; sum[i] = -1; flag[i] = 0; } } else { if (sum[i] <= 0) { ans += 1 - sum[i]; sum[i] = 1; flag[i] = 1; } } } printf("%lld\n", ans); } else { for (int i = 2; i <= n; i++) { scanf("%lld", &a[i]); flag[1] = 0; b[i] = a[i]; sum[i] = a[i] + sum[i - 1]; if (sum[i] > 0) flag[i] = 1; if (sum[i] < 0) flag[i] = 0; if (flag[i - 1] == 1) { if (sum[i] >= 0) { ans += sum[i] + 1; sum[i] = -1; flag[i] = 0; } } else { if (sum[i] <= 0) { ans += 1 - sum[i]; sum[i] = 1; flag[i] = 1; } } } k[1] = 1; for (int i = 2; i <= n; i++) { tot[i] = b[i] + tot[i - 1]; if (tot[i] > 0) k[i] = 1; if (tot[i] < 0) k[i] = 0; if (k[i - 1]) { if (tot[i] >= 0) ant += tot[i] + 1; tot[i] = -1; k[i] = 0; } else { if (tot[i] <= 0) { ant += 1 - tot[i]; tot[i] = 1; k[i] = 1; } } } printf("%lld\n", min(ant, ans)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; int main() { int n; cin >> n; vector<long long> v(n); for (int i = 0; i < n; ++i) { cin >> v[i]; } long long cnt_a = 0; long long sum_a = 0; for (int i = 0; i < n; ++i) { sum_a += v[i]; if (i % 2 == 0) { if (sum_a >= 0) { sum_a = -1; cnt_a += sum_a + 1; } } else { if (sum_a <= 0) { sum_a = 1; cnt_a += abs(sum_a) + 1; } } } long long cnt_b = 0; long long sum_b = 0; for (int i = 0; i < n; ++i) { sum_b += v[i]; if (i % 2 == 0) { if (sum_b <= 0) { sum_b = 1; cnt_b += abs(sum_b) + 1; } } else { if (sum_b >= 0) { sum_b = -1; cnt_b += abs(sum_b) + 1; } } } cout << min(cnt_a, cnt_b) << 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 z(s,l): for i in range(n-1): r=l+a[i+1] if r*l>=0: if l<=0: s+=1-r r=1 else: s+=1+r r=-1 print(a[i+1],r,s) l=r return s n=int(input()) a=list(map(int,input().split())) s1=0 l1=a[0] if a[0]<=0: s1=1-a[0] l1=1 s2=0 l2=a[0] if a[0]>=0: s2=a[0]+1 l2=-1 print(min(z(s1,l1),z(s2,l2)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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 i from 0 to (1- n) do (incf sum (aref numv i)) (loop (cond ((and (zerop sum) (zerop presum) (plusp (aref numv (1+ i)))) (incf count) (decf sum)) ((and (zerop sum) (zerop presum) (minusp (aref numv (1+ i)))) (incf count) (incf sum)) ((and (zerop sum) (plusp presum)) (incf count) (decf sum)) ((and (zerop sum) (minusp presum)) (incf count) (incf sum)) ((and (plusp sum) (plusp presum)) (incf count (1+ sum)) (decf sum (1+ sum))) ((and (plusp sum) (minusp presum)) (return)) ((and (minusp sum) (plusp presum)) (return)) ((and (minusp sum) (minusp presum)) (incf count (1+ (abs sum))) (incf sum (1+ (abs sum)))) (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; constexpr long long mod = 1e9 + 7; signed main() { int N; cin >> N; vector<long long> a(N), A; for (int i = 0, i_len = (N); i < i_len; ++i) cin >> a[i]; int ans = INT_MAX; int sig[2] = {1, -1}; for (int j = 0, j_len = (2); j < j_len; ++j) { A = a; long long sum = 0; int count = 0; for (int i = 0, i_len = (N); i < i_len; ++i) { sum += A[i]; if (i % 2 == 0 && sig[j] * sum <= 0) { A[i] -= sum - sig[j] * 1; count += llabs(sum - sig[j] * 1); sum = sig[j] * 1; } if (i % 2 == 1 && sig[j] * sum >= 0) { A[i] -= sum + sig[j] * 1; count += llabs(sum + sig[j] * 1); sum = -1 * sig[j]; } } 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
python3
n = int(input()) a = list(map(int, input().split())) even_is_negative = 0 odd_is_nagative = 0 for i in range(n): if i % 2 == 0 and a[i] < 0: even_is_negative += 1 elif i % 2 == 1 and a[i] < 0: odd_is_nagative += 1 # print(even_is_negative) # print(odd_is_nagative) sum = a[0] change = 0 if even_is_negative >= odd_is_nagative: for i in range(1, n): if i % 2 == 1 and sum + a[i] <= 0: change += abs(sum + a[i]) + 1 sum = 1 # print("a " + str(sum)) elif i % 2 == 0 and sum + a[i] >= 0: change += sum + abs(a[i]) + 1 sum = -1 # print("b " + str(sum)) else: sum += a[i] # print("c " + str(sum)) if even_is_negative < odd_is_nagative: for i in range(1, n): if i % 2 == 1 and sum + a[i] >= 0: change += sum + abs(a[i]) + 1 sum = -1 # print("a " + str(sum) + str(change)) elif i % 2 == 0 and sum + a[i] <= 0: change += abs(sum + a[i]) + 1 sum = 1 # print("b " + str(sum)+ str(change)) else: sum += a[i] # print("c " + str(sum)+ str(change)) if sum == 0: print(change + 1) else: print(change)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int zerocount(vector<int>& a, int i, int n) { cout << "strart" << endl; if (a.at(i) != 0) { return 0; } if (i == n - 1) { a.at(i) = 1; return 1; } int count = 0; count += zerocount(a, i + 1, n); if (a.at(i + 1) > 0) { a.at(i) = -1; } else { a.at(i) = 1; } count++; return count; } int main() { int n; cin >> n; vector<int> a(n); for (auto& x : a) { cin >> x; } bool hugou; int sum = 0; int count = 0; if (a.at(0) < 0) { hugou = false; sum += a.at(0); } if (a.at(0) > 0) { hugou = true; sum += a.at(0); } if (a.at(0) == 0) { count += zerocount(a, 0, n); } for (int i = 0; i < n - 1; i++) { int i_sum; i_sum = sum + a.at(i + 1); cout << i_sum << endl; if (i_sum >= 0) { if (hugou) { sum = -1; count += (i_sum + 1); hugou = false; } else { hugou = true; sum = i_sum; } } if (i_sum <= 0) { if (!hugou) { sum = 1; count += (-1) * (i_sum - 1); hugou = true; } else { hugou = false; sum = i_sum; } } } if (sum == 0) count++; cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(int argc, const char* argv[]) { long long int n, x; long long int cnt; vector<long long int> v, sum; cin >> n; for (int i = 0; i < n; i++) { cin >> x; v.push_back(x); sum.push_back(0); } sum[0] = v[0]; cnt = 0; for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + v[i]; if (sum[i - 1] >= 0 && sum[i] >= 0) { cnt += sum[i - 1] + 1 + v[i]; v[i] = sum[i - 1] * (-1) - 1; sum[i] = -1; } else if (sum[i - 1] < 0 && sum[i] < 0) { cnt += sum[i - 1] * (-1) + 1 - v[i]; v[i] = sum[i - 1] * (-1) + 1; sum[i] = 1; } else if (sum[i] == 0 && sum[i - 1] > 0) { cnt++; v[i]--; sum[i] = -1; } else if (sum[i] == 0 && sum[i - 1] < 0) { cnt++; v[i]++; sum[i] = 1; } } cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def main(): N = int(input()) A = [int(i) for i in input().split()] ans = 0 S = [0] * N S[0] = A[0] for i in range(1, N): S[i] = S[i-1] + A[i] if S[i]*S[i-1] < 0: continue if S[i-1] > 0: S[i] = -1 ans += abs((-1) - (S[i-1]+A[i])) else: S[i] = 1 ans += abs(1 - (S[i-1]+A[i])) 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
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 - a < 0) != (sum > 0)) { ll need = max(abs(sum - 1), abs(sum + 1)); sum += (sum > 0 ? -need : need); ans += need; } else if (sum == 0) { sum += (sum - a > 0 ? -1 : 1); ans += 1; } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { long int n; cin >> n; vector<long int> a(n); for (int i = 0; i < (n); i++) { cin >> a[i]; } long int oddcount = 0, evencount = 0; long int oddsum = 0, evensum = 0; bool oddplus = true, evenplus = false; for (int i = 0; i < (n); i++) { oddsum += a[i]; evensum += a[i]; if (oddplus && oddsum <= 0) { oddcount += 1 - oddsum; oddsum = 1; } if (!oddplus && oddsum >= 0) { oddcount += 1 + oddsum; oddsum = -1; } if (evenplus && evensum <= 0) { evencount += 1 - evensum; evensum = 1; } if (!evenplus && evensum >= 0) { evencount += 1 + evensum; evensum = -1; } oddplus = !oddplus; evenplus = !evenplus; } cout << fmin(oddcount, evencount) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, 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
cpp
#include <bits/stdc++.h> using namespace std; static const int MOD = 10000007; static const int MAX = 100005; int N; int A[MAX]; long long int S[MAX]; int change(bool pos) { int k = -1, diff = 0, cnt = 0, counter; if (pos) { for (int i = (0); i < (N); i++) { if (S[i] + diff > 0 && i % 2 == 0) continue; else if (S[i] + diff < 0 && i % 2 == 1) continue; else { k = i; if (k % 2 == 0) { counter = abs(1 - (S[k] + diff)); diff += 1 - (S[k] + diff); cnt += counter; } else { counter = abs((S[k] + diff) + 1); diff += -1 - (S[k] + diff); cnt += counter; } } } } else { for (int i = (0); i < (N); i++) { if (S[i] + diff > 0 && i % 2 == 1) continue; else if (S[i] + diff < 0 && i % 2 == 0) continue; else { k = i; if (k % 2 == 1) { counter = abs(1 - (S[k] + diff)); diff += 1 - (S[k] + diff); cnt += counter; } else { counter = abs(1 + (S[k] + diff)); diff += -1 - (S[k] + diff); cnt += counter; } } } } return cnt; } int main() { cin >> N; long long int sum = 0; for (int i = (0); i < (N); i++) { int a; cin >> a; A[i] = a; sum += a; S[i] = sum; } bool pos = true; int plus, minus; plus = change(pos); pos = false; minus = change(pos); cout << min(plus, 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
cpp
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; int main() { int n; cin >> n; long long cnt_a = 0; long long cnt_b = 0; long long sum_a = 0; long long sum_b = 0; for (int i = 0; i < n; ++i) { long long t; cin >> t; long long sign_t = (t > 0) ? 1 : -1; if (i == 0) { sum_a += t; sum_b += -1 * sign_t; cnt_b += abs(t) + 1; } else { long long tsum_a = sum_a + t; long long tsum_b = sum_b + t; long long sign_a = (sum_a > 0) ? 1 : -1; long long sign_b = (sum_b > 0) ? 1 : -1; if (tsum_a * sum_a > 0) { cnt_a += abs(tsum_a) + 1; sum_a = -1 * sign_a; } else { sum_a = tsum_a; if (sum_a == 0) { sum_a = -1 * sign_a; cnt_a += 1; } } if (tsum_b * sum_b > 0) { cnt_b += abs(tsum_b) + 1; sum_b = -1 * sign_b; } else { sum_b = tsum_b; if (sum_b == 0) { sum_b = -1 * sign_b; cnt_b += 1; } } } } cout << min(cnt_a, cnt_b) << 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 int mod = 1000000007; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; long long int arr[n]; long long int pref[n]; cin >> arr[0]; pref[0] = arr[0]; long long int cnt = 0; for (long long int i = 1; i < n; i++) { cin >> arr[i]; pref[i] = arr[i] + pref[i - 1]; if (pref[i] * pref[i - 1] > 0 || pref[i] == 0) { long long int rep = -1 - pref[i - 1]; cnt = cnt + abs(arr[i] - rep); arr[i] = rep; pref[i] = pref[i - 1] + rep; } } cout << cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int store[200007]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) scanf("%d", &store[i]); long long cnt = 0; long long sum = store[1]; if (sum == 0) sum++, store[1]++, cnt++; for (int i = 2; i <= n; i++) { if (sum < 0 && sum + store[i] <= 0) { while (sum + store[i] <= 0) cnt++, store[i]++; cnt += 1 - (sum + store[i]); sum = 1; } else if (sum > 0 && sum + store[i] > 0) { cnt += (sum + store[i]) + 1; sum = -1; } else sum += store[i]; } cout << cnt << "\n"; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> a(N); for (int i = 0; i < (int)(N); i++) { cin >> a[i]; } int ans = 0; int sum = 0; for (int i = 0; i < (int)(N - 1); i++) { sum += a[i]; int next_sum = sum + a[i + 1]; if (sum > 0) { if (next_sum < 0) { ; } else { int op = abs(-1 - next_sum); ans += op; sum += op; } } else { if (next_sum > 0) { ; } else { int op = abs(1 - next_sum); ans += op; sum -= op; } } } int ans1 = ans; ans = 0; sum = 0; for (int i = 0; i < (int)(N - 1); i++) { sum += a[i]; int next_sum = sum + a[i + 1]; if (sum < 0) { if (next_sum > 0) { ; } else { int op = abs(1 - next_sum); ans += op; sum += op; } } else { if (next_sum < 0) { ; } else { int op = abs(-1 - next_sum); ans += op; sum -= op; } } } ans = min(ans, ans1); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long N; cin >> N; long a[100001]; for (long i = 0; i < N; i++) { cin >> a[i]; } long total0 = 0; long ops0 = 0; for (int i = 0; i < N; i++) { total0 += a[i]; if (total0 < 1) { total0 = 1; ops0 += 1 - a[i]; } } long total1 = 0; long ops1 = 0; for (int i = 0; i < N; i++) { total1 += a[i]; if (total1 > -1) { total1 = -1; ops1 += (a[i] + 1); } } printf("%d\n", min(ops0, ops1)); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
#!/usr/bin/env python3 from itertools import accumulate def main(): n = int(input()) a = list(map(int, input().split())) a = list(accumulate(a)) if a[0] != 0: ans = 0 diff = 0 for i in range(1,n): p = a[i] + diff q = a[i-1] + diff if p * q >= 0: tmp = -q//abs(q) - p ans += abs(tmp) diff += tmp print(ans) else: ans = 10**18 for d in [-1,1]: ans2 = 1 for i in range(1,n): p = a[i] + d q = a[i-1] + d if p * q >= 0: tmp = -q//abs(q) - p ans2 += abs(tmp) diff += tmp ans = min(ans, ans2) 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
cpp
#include <bits/stdc++.h> using namespace std; int n, a[100000]; int even() { int res = 0; int sum[n]; for (int i = 0; i < n; i++) { if (i == 0) { sum[0] = a[0]; } else { sum[i] = sum[i - 1] + a[i]; } } for (int i = 0; i < n; i++) { if (i % 2 == 0 && sum[i] < 0) { res += -sum[i] + 1; for (int j = i + 1; j < n; j++) { sum[j] += -sum[i] + 1; } } else if (i % 2 == 1 && sum[i] > 0) { res += sum[i] + 1; for (int j = i + 1; j < n; j++) { sum[j] -= sum[i] + 1; } } else if (sum[i] == 0) { if (i % 2 == 0) { res += 1; for (int j = i + 1; j < n; j++) { sum[j] += 1; } } else { res += 1; for (int j = i + 1; j < n; j++) { sum[j] -= 1; } } } } return res; } int odd() { int res = 0; int sum[n]; for (int i = 0; i < n; i++) { if (i == 0) { sum[0] = a[0]; } else { sum[i] = sum[i - 1] + a[i]; } } for (int i = 0; i < n; i++) { if (i % 2 == 0 && sum[i] > 0) { res += sum[i] + 1; for (int j = i + 1; j < n; j++) { sum[j] -= sum[i] + 1; } } else if (i % 2 == 1 && sum[i] < 0) { res += -sum[i] + 1; for (int j = i + 1; j < n; j++) { sum[j] += -sum[i] + 1; } } else if (sum[i] == 0) { if (i % 2 == 0) { res += 1; for (int j = i + 1; j < n; j++) { sum[j] -= 1; } } else { res += 1; for (int j = i + 1; j < n; j++) { sum[j] += 1; } } } } return res; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } cout << min(even(), odd()) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; // ABC 6-C // http://abc006.contest.atcoder.jp/tasks/abc006_3 public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; i++) { nums[i] = in.nextInt(); } long answer = 0; if (nums[0] == 0) { answer = solve(nums, 0, 0); } else { answer = solve(nums, nums[0], 1); } System.out.println(answer); // // long sum = 0; // long answer = 0; // // for (int i = 0; i < n; i++) { // int a = in.nextInt(); // // if (sum < 0 && sum + a < 0) { // answer += 1 + Math.abs(sum + a); // sum = 1; // } else if (sum > 0 && sum + a > 0) { // answer += 1 + sum + a; // sum = -1; // } else if (sum + a == 0) { // answer++; // if (sum < 0) { // sum = 1; // } else { // sum = -1; // } // } else { // sum += a; // } // } // System.out.println(answer); } public static long solve(int[] nums, long sum, int index) { if (index == nums.length) { return 0; } if (sum < 0 && sum + nums[index] < 0) { return 1 + Math.abs(sum + nums[index]) + solve(nums, 1, index + 1); } else if (sum > 0 && sum + nums[index] > 0) { return 1 + sum + nums[index] + solve(nums, -1, index + 1); } else if (sum + nums[index] == 0) { if (sum < 0) { return Math.abs(sum) + 1 + solve(nums, 1, index + 1); } else if (sum > 0) { return Math.abs(sum) + 1 + solve(nums, -1, index + 1); } else { return 1 + Math.min(solve(nums, 1, index + 1), solve(nums, -1, index + 1)); } } else if (sum == 0) { return 1 + Math.min(solve(nums, 1, index + 1), solve(nums, -1, index + 1)); } { return solve(nums, sum + nums[index], index + 1); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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 and sum <= 0) { ans1 += -sum + 1; sum = 1; } else if (i % 2 == 1 and sum >= 0) { ans1 += sum + 1; sum = -1; } } sum = 0; for (int i = 0; i < N; i++) { ans2 += a.at(i); if (i % 2 == 0 and sum >= 0) { ans2 += sum + 1; sum = -1; } else if (i % 2 == 1 and sum <= 0) { ans2 += -sum + 1; sum = 1; } } int ans = min(ans1, ans2); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(int, input().split())) cnt =0 def check(a,i,cnt): sum1 = sum(a[:i]) sum2 = sum(a[:i+1]) if sum1<0 and sum2 <=0: a[i] += abs(sum2)+1 cnt += abs(sum2)+1 elif sum1>0 and sum2 >=0: a[i] -= abs(sum2)+1 cnt += abs(sum2)+1 return cnt for i in range(n): cnt=check(a,i,cnt) print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# encoding:utf-8 import copy import random import bisect #bisect_left これで二部探索の大小検索が行える import fractions #最小公倍数などはこっち import math mod = 10**9+7 n = int(input()) a = [int(i) for i in input().split()] def solve(a): if a[0] > 0: status_pos = True else: status_pos = False tmp = 0 ans = 0 for i in range(n): tmp += a[i] if tmp == 0: ans += 1 if status_pos: tmp = -1 else: tmp = 1 elif status_pos and tmp < 0: ans += 1+abs(tmp) tmp = 1 elif status_pos == False and tmp > 0: ans += 1+abs(tmp) tmp = -1 status_pos = not(status_pos) return ans ans = solve(a) ans2 = abs(a[0])+1 if a[0] > 0: a[0] = -1 else: a[0] = 1 ans2 += solve(a) print(min(ans,ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) ans=10**10 s = 0 cnt=0 for i in range(n): s+=a[i] if i%2==1: if s<=0: cnt += -s + 1 s = 1 else: if s>=0: cnt += s + 1 s = -1 ans=min(ans,cnt) s = 0 cnt=0 for i in range(n): s+=a[i] if i%2==1: if s<=0: cnt += -s + 1 s = 1 else: if s>=0: cnt += s + 1 s = -1 ans=min(ans,cnt) 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 = list(map(int,input().split())) i = 0 k = 0 count = 0 ans = list_a[0] if list_a[0] > 0: for i in range(1,n): ans += list_a[i] if ans / ((-1) ** i) <= 0: count += abs(ans - (-1) ** i) ans = (-1) ** i else: for i in range(1,n): ans += list_a[i] if ans / ((-1) ** (i+1)) <= 0: count += abs(ans - (-1) ** (i+1)) ans = (-1) ** (i+1) count1 = count count = 0 if list_a[0] <= 0: for i in range(1,n): ans += list_a[i] if ans / ((-1) ** i) <= 0: count += abs(ans - (-1) ** i) ans = (-1) ** i else: for i in range(1,n): ans += list_a[i] if ans / ((-1) ** (i+1)) <= 0: count += abs(ans - (-1) ** (i+1)) ans = (-1) ** (i+1) print(min(count1, count2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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 ((a.at(i) > 0 && sum + a.at(i + 1) >= 0) || (a.at(i) < 0 && sum + a.at(i + 1) <= 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
UNKNOWN
n = gets.to_i a = gets.strip.split.map(&:to_i) b = Marshal.load(Marshal.dump(a)) c = Marshal.load(Marshal.dump(a)) answer1 = 0 answer2 = 0 for i in 0..n-1 if i % 2 == 0 && b.slice(0..i).inject(:+) <= 0 answer1 += 1 - b.slice(0..i).inject(:+) b[i] = 1 - b.slice(0..i).inject(:+) + b[i] elsif i % 2 == 1 && b.slice(0..i).inject(:+) >= 0 answer1 += b.slice(0..i).inject(:+) + 1 b[i] = - b.slice(0..i).inject(:+) - 1 + b[i] end end for i in 0..n-1 if i % 2 == 0 && c.slice(0..i).inject(:+) >= 0 answer2 += c.slice(0..i).inject(:+) + 1 c[i] = -1 - c.slice(0..i).inject(:+) + c[i] elsif i % 2 == 1 && c.slice(0..i).inject(:+) <= 0 answer2 += 1 - c.slice(0..i).inject(:+) c[i] = - c.slice(0..i).inject(:+) + 1 + c[i] end end puts [answer1,answer2].min
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; char ch, last; int n, a[100002], s[100002], tmp, ans; inline void read(int &x) { x = ch = 0; do { last = ch; ch = getchar(); } while (ch < '0' || '9' < ch); do { x = x * 10 + ch - '0'; ch = getchar(); } while ('0' <= ch && ch <= '9'); if (last == '-') x = -x; } int main(void) { read(n); for (int i = (1); i <= (n); i++) read(a[i]); for (int i = (1); i <= (n); i++) { s[i] = s[i - 1] + a[i]; if (i & 1) { if (s[i] <= 0) tmp += 1 - s[i], s[i] = 1; } else { if (s[i] >= 0) tmp += s[i] + 1, s[i] = -1; } } ans = tmp; tmp = 0; for (int i = (1); i <= (n); i++) { s[i] = s[i - 1] + a[i]; if (i & 1) { if (s[i] >= 0) tmp += s[i] + 1, s[i] = -1; } else { if (s[i] <= 0) tmp += 1 - s[i], s[i] = 1; } } ans = min(ans, tmp); printf("%d", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, m; int i, j; cin >> n; vector<int> a(n); for (i = 0; i < n; ++i) { cin >> a.at(i); } int s = 0, h1 = 0, h2 = 0; for (i = 0; i < n; ++i) { s += a[i]; if (i % 2 && s >= 0) { h1 += s + 1; s = -1; } else if (i % 2 == 0 && s <= 0) { h1 += 1 - s; s = 1; } } s = 0; for (i = 0; i < n; ++i) { s += a[i]; if (i % 2 == 0 && s >= 0) { h2 += s + 1; s = -1; } else if (i % 2 && s <= 0) { h2 += 1 - s; s = 1; } } cout << min(h1, h2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> inline void chmax(T& a, T b) { if (a < b) { a = b; } } template <class T> inline void chmin(T& a, T b) { if (a > b) { a = b; } } template <class T> inline T gcd(T x, T y) { if (y == 0) { return x; } else if (x == 0) { return y; } return gcd(y, x % y); } template <class T> inline T lcm(T x, T y) { return (x * y) / gcd(x, y); } template <class T> inline void print_vector(vector<T> vec) { for (int i = 0; i < vec.size(); i++) { cout << vec[i] << " "; } cout << endl; } const long long MOD = 1e9 + 7; const long long LLINF = 1LL << 60; const int INF = 1 << 30; int main(void) { long long N; cin >> N; vector<long long> A(N); vector<long long> B(N); for (int i = 0; i < N; i++) { cin >> A[i]; B[i] = A[i]; } long long sum = A[0] > 0 ? A[0] : 1; long long count = sum - A[0]; for (int i = 1; i < N; i++) { long long tmp = A[i] + sum; if (i % 2 == 1 and tmp >= 0) { A[i] += (-1 - tmp); count += abs(-1 - tmp); } else if (i % 2 == 0 and tmp <= 0) { A[i] += (1 - tmp); count += (1 - tmp); } sum += A[i]; } long long ans = count; sum = B[0] < 0 ? B[0] : -1; count = sum - B[0]; for (int i = 1; i < N; i++) { long long tmp = B[i] + sum; if (i % 2 == 0 and tmp >= 0) { B[i] += (-1 - tmp); count += abs(-1 - tmp); } else if (i % 2 == 1 and tmp <= 0) { B[i] += (1 - tmp); count += (1 - tmp); } sum += B[i]; } chmin(ans, count); 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 CalcSumOfDigit(int n); string upper(string str); string lower(string str); const int INF = 1e9; using ull = unsigned long long; using ll = long long; int main() { ll n; cin >> n; vector<ll> a(n); for (int(i) = 0; (i) < (n); (i)++) cin >> a[i]; ll m = 0; ll p = 0; ll m_cnt = 0; ll p_cnt = 0; for (int(i) = 0; (i) < (n); (i)++) { if ((i % 2 ? 1 : -1) * (m + a[i]) < 0) { ll b_m = m + a[i]; m_cnt += 1 + abs(b_m); m = i % 2 ? 1 : -1; } else if (m + a[i] == 0) { m = i % 2 ? 1 : -1; m_cnt++; } else { m += a[i]; } if ((i % 2 ? -1 : 1) * (p + a[i]) < 0) { ll b_p = p + a[i]; p_cnt += 1 + abs(b_p); p = i % 2 ? -1 : 1; } else if (p + a[i] == 0) { p = i % 2 ? 1 : -1; p_cnt++; } else { p += a[i]; } } cout << min(m_cnt, p_cnt) << endl; return 0; } int CalcSumOfDigit(int n) { int s = 0; while (n) { s += n % 10; n = n / 10; } return s; } string upper(string str) { for (auto itr = str.begin(); itr != str.end(); itr++) { if (97 <= *itr && *itr <= 122) { *itr = *itr - 32; } } return str; } string lower(string str) { for (auto itr = str.begin(); itr != str.end(); itr++) { if (65 <= *itr && *itr <= 90) { *itr = *itr + 32; } } return str; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) a = [int(i) for i in input().split()] sam = a[0] old = sam num = 0 for i in range(1, len(a)): sam += a[i] if sam >= 0 and old > 0: num += (abs(sam) + 1) sam -= (-sam + 1) elif sam <= 0 and old < 0: num += (abs(sam) + 1) sam += (-sam + 1) old = sam print(num)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < (int)(n); i++) cin >> a[i]; int s = a[0]; long long ans1 = 0, ans2 = 0; if (s <= 0) ans1 += -s + 1; for (int i = (1); i < (int)(n); i++) { if (i % 2 && s + a[i] >= 0) { ans1 += s + a[i] + 1; s = -1; } else if (i % 2 == 0 && s + a[i] <= 0) { ans1 += -(s + a[i]) + 1; s = 1; } else s += a[i]; } s = a[0]; if (s >= 0) ans2 += s + 1; for (int i = (1); i < (int)(n); i++) { if (i % 2 == 0 && s + a[i] >= 0) { ans2 += s + a[i] + 1; s = -1; } else if (i % 2 && s + a[i] <= 0) { ans2 += -(s + a[i]) + 1; s = 1; } else s += a[i]; } 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
UNKNOWN
using System; class Program { static void Main() { int n = int.Parse(Console.ReadLine()); var a = Array.ConvertAll(Console.ReadLine().Split(), int.Parse); int sum = a[0]; long ans = 0; for (int i = 1; i < n; i++) { int temp = a[i]; if (sum * a[i] > 0 || Math.Abs(sum) >= Math.Abs(a[i])) a[i] = sum > 0 ? -(sum + 1) : -(sum - 1); ans += Math.Abs(a[i] - temp); sum += a[i]; } 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
cpp
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const long long int linf = 1LL << 50; int main(int argc, char const* argv[]) { int n; cin >> n; vector<int> a; for (int i = 0; i < n; i++) { int x; cin >> x; a.push_back(x); } long long int res = 0; long long int sum = a[0]; for (int i = 1; i < n; i++) { long long int tmp = sum + a[i]; if ((sum > 0 && tmp < 0) || (sum < 0 && tmp > 0)) { sum = tmp; } else { int target; if (sum > 0) target = -1; else target = 1; res += abs(target - tmp); sum = target; } } cout << res << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(1001); for (int i = 0; i < n; i++) { cin >> a[i]; } int c1 = 0, s1 = 0; int c2 = 0, s2 = 0; for (int i = 0; i < n; i++) { s1 += a[i]; s2 += a[i]; if (i % 2 == 0) { if (s1 < 0) { c1 += 1 - s1; s1 = 1; } if (s2 > 0) { c2 += s2 + 1; s2 = -1; } } else { if (s2 < 0) { c2 += 1 - s2; s2 = 1; } if (s1 > 0) { c1 += s1 + 1; s1 = -1; } } } printf("%d\n", min(c1, c2)); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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[] sum = new int[n]; sum[0] = a[0]; int count = 0; if(sum[0] == 0){ count++; } count = solve(sum,a,count); System.out.println(count); } public static int solve(int[] sum,int[] a,int count){ for(int i = 0;i < sum.length-1;i++){ sum[i+1] = sum[i] + a[i+1]; if(sum[0] > 0){ if((i+1) % 2 == 1){ while(sum[i+1] >= 0){ sum[i+1]--; count++; } } if((i+1) % 2 == 0){ while(sum[i+1] <= 0){ sum[i+1]++; count++; } } } if(sum[0] < 0){ if((i+1) % 2 == 1){ while(sum[i+1] <= 0){ sum[i+1]++; count++; } } if((i+1) % 2 == 0){ while(sum[i+1] >= 0){ sum[i+1]--; count++; } } } } return count; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> v(n); for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); i += 1 - 2 * ((0) > (n))) cin >> v[i]; long long res1 = 0, res2 = 0, res3, res4; long long som = v[0]; if (som >= 0) { for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 1) if (som < 0) continue; else { res1 += som + 1; som = -1; } else if (som > 0) continue; else { res1 += 1 - som; som = 1; } } res2 = v[0] + 1; som = -1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 0) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } } else { for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 0) if (som < 0) continue; else { res1 += som + 1; som = -1; } else if (som > 0) continue; else { res1 += 1 - som; som = 1; } } res2 = 1 - v[0]; som = 1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 1) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } } if (v[0] == 0) { res1 = 1; som = 1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 1) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } res2 = 1; som = -1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 0) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } } cout << min(res1, res2); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; enum State { Plus, Minus, Zero }; State GetState(int sum) { State state; if (sum > 0) state = Plus; else if (sum == 0) state = Zero; else state = Minus; return state; } int main() { int n; cin >> n; vector<int> a(n); cin >> a[0]; int count = 0; State state = GetState(a[0]); if (state == Zero) { a[0] = 1; state = Plus; count++; } int sum = a[0]; for (int i = 1; i < n; i++) { cin >> a[i]; State nextState = GetState(sum + a[i]); switch (nextState) { case Plus: if (state == Plus) { int bf_a = a[i]; a[i] = -1 - sum; count += abs(a[i] - bf_a); nextState = Minus; } break; case Minus: if (state == Minus) { int bf_a = a[i]; a[i] = 1 - sum; count += abs(a[i] - bf_a); nextState = Plus; } break; default: break; } sum += a[i]; state = nextState; } if (sum == 0) count++; cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long zero = 0; int N; cin >> N; vector<int> sum(N,0); int now; cin >> now; sum[0] = now; for (int i=1; i<N; i++) { cin >> now; sum[i] = sum[i-1] + now; } long change = 0; long ansp = 0; int i = 0; while (i<N) { ansp += max(1-(sum[i]+change),0); change += max(1-(sum[i]+change),0); i++; if (i==N) { break; } ansp += max((sum[i]+change)+1,0); change -= max((sum[i]+change)+1,0); i++; } change = 0; long ansm = 0; i = 0; while (i<N) { ansm += max((sum[i]+change)+1,zero); change -= max((sum[i]+change)+1,zero); i++; if (i==N) { break; } ansm += max(1-(sum[i]+change),zero); change += max(1-(sum[i]+change),zero) i++; } cout << min(ansp,ansm) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long MOD = 1e9 + 7; const long LINF = 1e13; const long LLINF = 1e18; template <class T> void Swap(T& r, T& l) { T tmp = r; r = l; l = tmp; } int main() { long n; cin >> n; vector<long> a(n); vector<long> accum(n, 0); for (int i = 0; i < n; ++i) { cin >> a[i]; accum[i] = a[i]; if (i > 0) accum[i] += accum[i - 1]; } vector<long> accumtmp(n, 0); copy(accum.begin(), accum.end(), accumtmp.begin()); long ans = 0; long count = 0; long tmpcount = 0; for (int i = 1; i < n; ++i) { long accump = accumtmp[i] + tmpcount; if (i % 2 == 1) { if (accump >= 0) { long tmpc = -(-1 - accump); count += tmpc; accumtmp[i] = -1; tmpcount -= tmpc; } } else { if (accump <= 0) { long tmpc = 1 - accump; count += tmpc; tmpcount += tmpc; } } } ans = count; count = 0; copy(accum.begin(), accum.end(), accumtmp.begin()); tmpcount = 0; for (int i = 1; i < n; ++i) { long accump = accumtmp[i] + tmpcount; if (i % 2 == 0) { if (accump >= 0) { long tmpc = -(-1 - accump); count += tmpc; accumtmp[i] = -1; tmpcount -= tmpc; } } else { if (accump <= 0) { long tmpc = 1 - accump; count += tmpc; tmpcount += tmpc; } } } ans = min(ans, count); 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
UNKNOWN
macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut iter = $ s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; ( $ ( $ r : tt ) * ) => { let s = { use std :: io :: Read ; let mut s = String :: new ( ) ; std :: io :: stdin ( ) . read_to_string ( & mut s ) . unwrap ( ) ; s } ; let mut iter = s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; } macro_rules ! input_inner { ( $ iter : expr ) => { } ; ( $ iter : expr , ) => { } ; ( $ iter : expr , mut $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let mut $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , mut $ var : ident $ ( $ r : tt ) * ) => { let mut $ var = $ iter . next ( ) . unwrap ( ) . parse ::< usize > ( ) . unwrap ( ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident $ ( $ r : tt ) * ) => { let $ var = $ iter . next ( ) . unwrap ( ) . parse ::< usize > ( ) . unwrap ( ) ; input_inner ! { $ iter $ ( $ r ) * } } ; } macro_rules ! read_value { ( $ iter : expr , ( $ ( $ t : tt ) ,* ) ) => { ( $ ( read_value ! ( $ iter , $ t ) ) ,* ) } ; ( $ iter : expr , [ $ t : tt ; $ len : expr ] ) => { ( 0 ..$ len ) . map ( | _ | read_value ! ( $ iter , $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ iter : expr , chars ) => { read_value ! ( $ iter , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ iter : expr , usize1 ) => { read_value ! ( $ iter , usize ) - 1 } ; ( $ iter : expr , $ t : ty ) => { $ iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) } ; } fn main() { input! { n: usize, a: [i64; n], } let mut ans0 = 0; let mut sum = 0; for (i, &v) in a.iter().enumerate() { sum += v; if i % 2 == 0 && sum <= 0 { ans0 += 1 - sum; sum = 1; } else if i % 2 == 1 && sum >= 0 { ans0 += sum + 1; sum = -1; } } let mut ans1 = 0; sum = 0; for (i, &v) in a.iter().enumerate() { sum += v; if i % 2 == 1 && sum <= 0 { ans1 += 1 - sum; sum = 1; } else if i % 2 == 1 && sum >= 0 { ans1 += sum + 1; sum = -1; } } println!("{}", std::cmp::min(ans1, ans0)); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; using System.Numerics; using static System.Console; using static System.Convert; using static System.Math; class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); int[] arr = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); long[] arrSum = new long[n]; long sum_Plus = 0; long sum_minus = 0; bool isChange = false; bool isChange_minus = false; long nPreSum_Plus = 0; long nPreSum_minus = 0; for (int i = 0; i < n; i++) { if (i == 0) { arrSum[0] = arr[0]; nPreSum_minus = arr[0]; nPreSum_Plus = arr[0]; } else { arrSum[i] = (arrSum[i - 1] + arr[i]); } #region //plusStart if (i % 2 == 0) { if (!isChange) { //一回も変更していない if (arrSum[i] > 0) { //isChange = false; } else if (arrSum[i] < 0) { long tmpcnt = (1 - arrSum[i]); sum_Plus += tmpcnt; nPreSum_Plus = 1; isChange = true; } else { sum_Plus += 1; nPreSum_Plus = 1; isChange = true; } } else { //一回でも変更したとき arrSum[i-1]=-1 long tmpsum = (nPreSum_Plus) + arr[i]; if (0<tmpsum) { //isChange = false; nPreSum_Plus = tmpsum; } else if (tmpsum < 0) { long tmpcnt = Math.Abs(1 - (nPreSum_Plus)); sum_Plus += tmpcnt; nPreSum_Plus = nPreSum_Plus + arr[i]; } else { sum_Plus += 1; nPreSum_Plus = 1; } } } else { //奇数インデックス⇒マイナスにする if (!isChange) { //一回も変更していない if (arrSum[i] < 0) { //isChange = false; } else if (arrSum[i] > 0) { long tmpcnt = (arrSum[i] - (-1)); sum_Plus += tmpcnt; nPreSum_Plus = -1; isChange = true; } else { sum_Plus += 1; nPreSum_Plus = -1; isChange = true; } } else { //一回でも変更したとき⇒arrSum[i-1]=1; long tmpSum = nPreSum_Plus + arr[i]; if (tmpSum < 0) { //isChange = false; nPreSum_Plus= tmpSum; } else if (0<tmpSum) { long tmpcnt = (Math.Abs((-1)-nPreSum_Plus)); sum_Plus += tmpcnt; nPreSum_Plus = nPreSum_Plus + arr[i]; } else { sum_Plus += 1; nPreSum_Plus = -1; } } } #endregion #region //minusStart if (i % 2 == 1) { if (!isChange_minus) { //一回も変更していない if (arrSum[i] > 0) { //isChange_minus = false; } else if (arrSum[i] < 0) { long tmpcnt = (1 - arrSum[i]); sum_minus += tmpcnt; nPreSum_minus = 1; isChange_minus = true; } else { sum_minus += 1; nPreSum_minus = 1; isChange_minus = true; } } else { //一回でも変更したとき arrSum[i-1]=-1 long tmpsum = (nPreSum_minus) + arr[i]; if (0 < tmpsum) { //isChange_minus = false; nPreSum_minus= tmpsum; } else if (tmpsum < 0) { long tmpcnt = Math.Abs(1 - (nPreSum_minus)); sum_minus += tmpcnt; nPreSum_minus = nPreSum_minus + arr[i]; } else { sum_minus += 1; nPreSum_minus = 1; } } } else { //奇数インデックス⇒マイナスにする if (!isChange_minus) { //一回も変更していない if (arrSum[i] < 0) { //isChange_minus = false; } else if (arrSum[i] > 0) { long tmpcnt = (arrSum[i] - (-1)); sum_minus += tmpcnt; nPreSum_minus = -1; isChange_minus = true; } else { sum_minus += 1; nPreSum_minus = -1; isChange_minus = true; } } else { //一回でも変更したとき⇒arrSum[i-1]=1; long tmpSum = nPreSum_minus + arr[i]; if (tmpSum < 0) { //isChange_minus = false; nPreSum_minus = tmpSum; } else if (0 < tmpSum) { long tmpcnt = (Math.Abs((-1) - nPreSum_minus)); sum_minus += tmpcnt; nPreSum_minus = nPreSum_minus + arr[i]; } else { sum_Plus += 1; nPreSum_minus = -1; } } } #endregion } var ans = sum_minus <= sum_Plus ? sum_minus : sum_Plus; 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n][2]; for (int i = 0; i < n; i++) { cin >> a[i][0]; a[i][1] = a[i][0]; } int sum = 0; int res[2]; for (int check = 0; check < 2; check++) { sum = 0; for (int i = 0; i < n - 1; i++) { sum += a[i][check]; if (sum * (sum + a[i + 1][check]) >= 0) { if (sum > 0) { int temp = -1 - sum - a[i + 1][check]; a[i + 1][check] += temp; res[check] += temp * -1; } else { int temp = 1 - sum - a[i + 1][check]; a[i + 1][check] += temp; res[check] += temp; } } } } cout << min(res[0], res[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
def ops_needed(x, target): if abs(target - x) < abs(x): return 0 else: return target - x def count_ops(a): total_ops = 0 accum = a[0] for i in range(1, len(a)): new_accum = accum + a[i] if new_accum == 0: ops = -1 * (accum / abs(accum)) else: ops = ops_needed(new_accum, -1 * (accum / abs(accum))) total_ops += abs(ops) accum = new_accum + ops return int(total_ops) if __name__ == '__main__': _ = int(input()) a = list(map(int, input().split())) print(count_ops(a))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int store[200007]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) scanf("%d", &store[i]); long long cnt = 0; long long sum = store[1]; if (sum == 0) sum++, store[1]++, cnt++; for (int i = 2; i <= n; i++) { if (sum < 0 && sum + store[i] <= 0) { while (sum + store[i] <= 0) cnt++, store[i]++; sum += store[i]; } else if (sum > 0 && sum + store[i] > 0) { while (sum + store[i] >= 0) cnt++, store[i]--; sum += store[i]; } else sum += store[i]; } cout << cnt << "\n"; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } int min_ans = INT_MAX; for (int mod = 0; mod < 2; ++mod) { int ans = 0; int sum = 0; for (int i = 0; i < n; ++i) { int sign = ((i % 2) == mod) * -2 + 1; sum += a[i]; cout << i << " : target is " << sign << " sum: " << sum; if (sign * sum <= 0) { int diff = sign - sum; cout << " move: " << diff; sum = sign; ans += abs(diff); } cout << "\n"; } min_ans = min(min_ans, ans); } cout << min_ans << "\n"; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] A = new int[N + 1]; A[0] = 0; for (int i = 1; i <= N; ++i) { A[i] = sc.nextInt(); } sc.close(); int sum1 = 0; int sum2 = 0; int ans1 = 0; int ans2 = 0; for (int i = 1; i <= N; ++i) { sum1 += A[i]; if (i % 2 == 0 && sum1 >= 0) { ans1 += sum1 +1; sum1 = -1; } else if (i % 2 != 0 && sum1 <= 0) { ans1 += Math.abs(sum1) + 1; sum1 = 1; } } for (int i = 1; i <= N; ++i) { sum2 += A[i]; if (i % 2 == 0 && sum2 <= 0) { ans2 += sum1 +1; sum2 = 1; } else if (i % 2 != 0 && sum2 >= 0) { ans2 += Math.abs(sum2) + 1; sum2 = -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
#include <bits/stdc++.h> using namespace std; int n; long long Num[1111111]; long long Sum[1111111]; long long Out; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &Num[i]); if (Num[1] == 0) { long long Out1 = 0; long long Out2 = 0; Num[1] = 1; for (int i = 1; i <= n; i++) { Sum[i] = Sum[i - 1] + Num[i]; if (i % 2 == 0) { if (Sum[i] >= 0) { Out1 += (Sum[i] + 1); Sum[i] = -1; } } else { if (Sum[i] <= 0) { Out1 += (1 - Sum[i]); Sum[i] = 1; } } } Num[1] = -1; for (int i = 1; i <= n; i++) { Sum[i] = Sum[i - 1] + Num[i]; if (i % 2 == 1) { if (Sum[i] >= 0) { Out2 += (Sum[i] + 1); Sum[i] = -1; } } else { if (Sum[i] <= 0) { Out2 += (1 - Sum[i]); Sum[i] = 1; } } } printf("%lld", min(Out1, Out2)); return 0; } if (Num[1] > 0) { for (int i = 1; i <= n; i++) { Sum[i] = Sum[i - 1] + Num[i]; if (i % 2 == 0) { if (Sum[i] >= 0) { Out += (Sum[i] + 1); Sum[i] = -1; } } else { if (Sum[i] <= 0) { Out += (1 - Sum[i]); Sum[i] = 1; } } } } else { for (int i = 1; i <= n; i++) { Sum[i] = Sum[i - 1] + Num[i]; if (i % 2 == 1) { if (Sum[i] >= 0) { Out += (Sum[i] + 1); Sum[i] = -1; } } else { if (Sum[i] <= 0) { Out += (1 - Sum[i]); Sum[i] = 1; } } } } printf("%lld\n", Out); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = int64_t; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int DX[] = {1, 1, 0, -1, -1, -1, 0, 1}; int DY[] = {0, -1, -1, -1, 0, 1, 1, 1}; void solve() { int n; cin >> n; ll a[n], ans = 0; for (int(i) = 0; (i) < (n); (i)++) cin >> a[i]; int temp = 0; for (int(i) = 0; (i) < (n); (i)++) { if (temp > 0 && temp + a[i] > 0) { ans += abs(-1 - temp - a[i]); a[i] = -1 - temp; } else if (temp < 0 && temp + a[i] < 0) { ans += abs(1 - temp - a[i]); a[i] = 1 - temp; } else if (temp + a[i] == 0) { if (temp > 0) { a[i] -= 1; } else { a[i] += 1; } ans += 1; } temp += a[i]; } cout << ans << endl; } int main() { solve(); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long ms = 1e5 + 9; long long vet[ms], n; long long resp(long long soma, long long ans, int f) { long long val; for (long long i = 1; i < n; i++) { val = vet[i]; soma += val; if (f) { if (soma == 0) { ans += 1; soma++; } else if (soma < 0) { ans += ((-soma) + 1); soma = 1; } } else { if (soma == 0) { ans++; soma--; } else if (soma > 0) { ans += (soma + 1); soma = -1; } } f = 1 - f; } return ans; } int main() { long long f = 0; cin >> n; for (long long i = 0; i < n; i++) cin >> vet[i]; int soma = vet[0]; long long ans = 0x3f3f3f3f3f3f3f3f; if (soma < 0) { ans = min(ans, resp(soma, 0, 1)); ans = min(ans, resp(1, 0 - soma + 1, 0)); } else if (soma > 0) { ans = min(ans, resp(soma, 0, 0)); cout << ans << "\n"; ans = min(ans, resp(-1, 0 + soma + 1, 1)); cout << ans << "\n"; } else if (soma == 0) { ans = min(resp(-1, 1, 1), ans); ans = min(resp(1, 1, 0), ans); } cout << ans << "\n"; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (auto& x : a) { cin >> x; } int sum1 = 0; int sum2 = 0; int time1 = 0; int time2 = 0; for (int i = 0; i < n; i++) { sum1 += a.at(i); sum2 += a.at(i); if (i % 2 == 0) { if (sum1 <= 0) { time1 += sum1 * (-1) + 1; sum1 = 1; } if (sum2 >= 0) { time2 += sum2 + 1; sum2 = -1; } } if (i % 2 == 1) { if (sum1 >= 0) { time1 += sum1 + 1; sum1 = -1; } if (sum2 <= 0) { time2 += sum2 * (-1) + 1; sum2 = 1; } } } cout << min(time1, time2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int solve(); int adds(int i, int num); int show(); vector<long> adder; int N; int main() { cin >> N; adder = vector<long>(N + 1); adder.at(0) = 0; long buf; for (int i = 0; i < N; i++) { cin >> buf; adder.at(i + 1) = adder.at(i) + buf; } solve(); } int solve() { int flag = 0; int count = 0; for (int i = 0; i < N;) { show(); if (adder.at(i + 1) > 0) { if (flag != 1) { flag = 1; i++; } else { adds(i + 1, -1); count++; } } else if (adder.at(i + 1) < 0) { if (flag != -1) { flag = -1; i++; } else { adds(i + 1, 1); count++; } } else { if (flag == -1) { adds(i + 1, 1); count++; } else if (flag == 1) { adds(i + 1, -1); count++; } else { if (i == 0) { adds(i + 1, 1); } } } } cout << count << endl; return 0; } int adds(int i, int num) { for (int j = i; j < N + 1; j++) { adder.at(j) += num; } return 0; } int show() { return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long dptemp[100010]; long long s1[100010], dp[100010]; int main() { long long mi = 0x3f3f3f3f, n, a, sum, pri1, pri2, all; scanf("%lld", &n); dp[0] = 0; for (a = 1; a <= n; a++) { scanf("%lld", &s1[a]); dp[a] = s1[a] + dp[a - 1]; dptemp[a] = dp[a]; } if (dp[1] == 0) { dp[1]++; all = 1; for (a = 2; a <= n; a++) { dp[a] = (dp[a - 1] + s1[a]); if (dp[a - 1] > 0) { if (dp[a] >= 0) { all += (dp[a] + 1); dp[a] = -1; } } else { if (dp[a] <= 0) { all += (-dp[a] + 1); dp[a] = 1; } } } if (all < mi) mi = all; for (a = 1; a <= n; a++) dp[a] = dptemp[a]; dp[1]--; all = 1; for (a = 2; a <= n; a++) { dp[a] = (dp[a - 1] + s1[a]); if (dp[a - 1] > 0) { if (dp[a] >= 0) { all += (dp[a] + 1); dp[a] = -1; } } else { if (dp[a] <= 0) { all += (-dp[a] + 1); dp[a] = 1; } } } if (all < mi) mi = all; } else if (dp[1] > 0) { all = 0; for (a = 1; a <= n; a++) dp[a] = dptemp[a]; for (a = 2; a <= n; a++) { dp[a] = (dp[a - 1] + s1[a]); if (dp[a - 1] > 0) { if (dp[a] >= 0) { all += (dp[a] + 1); dp[a] = -1; } } else { if (dp[a] <= 0) { all += (-dp[a] + 1); dp[a] = 1; } } } if (all < mi) mi = all; } else { sum = 0; all = 0; for (a = 1; a <= n; a++) dp[a] = dptemp[a]; for (a = 2; a <= n; a++) { dp[a] = (dp[a - 1] + s1[a]); if (dp[a - 1] > 0) { if (dp[a] >= 0) { all += (dp[a] + 1); dp[a] = -1; } } else { if (dp[a] <= 0) { all += (-dp[a] + 1); dp[a] = 1; } } } if (all < mi) mi = all; } printf("%lld\n", mi); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; ll mod = 1e9 + 7; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int main() { ll n; cin >> n; ll a[n]; ll sum = 0; ll cnt = 0; cin >> a[0]; sum += a[0]; bool flg; if (sum > 0) { flg = true; } else if (sum < 0) { flg = false; } for (ll i = 1; i < n; i++) { cin >> a[i]; sum += a[i]; if (flg) { if (sum >= 0) { cnt += (sum + 1); sum = -1; } flg = false; } else if (!flg) { if (sum <= 0) { cnt += (1 + abs(sum)); sum = 1; } flg = true; } } cout << cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; long N[100000]; cin >> n; for (int i = 0; i < n; i++) { cin >> N[i]; } for (int i = 1; i < n; i++) { N[i] = N[i] + N[i - 1]; } int ans = 0; if (N[0] == 0) { if (N[1] <= 0) { for (int i = 0; i < n; i++) { N[i]++; ans = 1; } } else { for (int i = 0; i < n; i++) { N[i] = N[i] - 1; } ans = 1; } } for (int i = 1; i < n; i++) { if (N[i - 1] < 0) { while (N[i] <= 0) { for (int j = i; j < n; j++) { N[j]++; } ans++; } } if (N[i - 1] > 0) { while (N[i] >= 0) { for (int j = i; j < n; j++) { N[j] = N[j] - 1; } ans++; } } } 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; using llong = long long; const int MOD = 1000000007; int main(int argc, char** argv) { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int sum = 0; int c1 = 0, c2 = 0; int sign = 1; for (int i = 0; i < n; i++) { sum += a[i]; if (sum * sign <= 0) { c1 += abs(sum) + 1; sum = sign; } sign *= -1; } sign = -1; sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (sum * sign <= 0) { c2 += abs(sum) + 1; sum = sign; } sign *= -1; } cout << min(c1, c2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long max(long long a, long long b) { return (a > b) ? a : b; } long long min(long long a, long long b) { return (a < b) ? a : b; } long long abss(long long a) { return (a < 0) ? -a : a; } long long gcd(long long a, long long b) { if (b > a) { long long tmp = b; b = a; a = tmp; } if (b == 0) return a; else return gcd(b, a % b); } long long lcm(long long a, long long b) { long long gcdi = gcd(a, b); return a / gcdi * (b); } int a[100001]; int sum[100001]; int main() { long long N; scanf("%lld", &N); for (int i = 0; i < N; i++) { scanf("%d", a + i); } long long cnt = 0; char sign; long long sum = 0; sign = 1; sum = a[0]; for (int i = 1; i < N; i++) { sum += a[i]; if (sign * sum > 0) { } else { cnt += abss(sum - sign); sum = sign; } sign = -sign; } long long ans = cnt; sign = -1; sum = a[0]; for (int i = 1; i < N; i++) { sum += a[i]; if (sign * sum > 0) { } else { cnt += abss(sum - sign); sum = sign; } sign = -sign; } printf("%lld\n", min(ans, cnt)); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; public class Main { public static void main(String[] args) throws Exception { // Your code here! Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = sc.nextInt(); } int countA = 0; int sum = 0; for (int i = 0; i < n; i++) { sum += array[i]; if (i % 2 == 0) { if (sum >= 0) { countA += sum + 1; sum = -1; } } else { if (sum <= 0) { countA += sum * (-1) + 1; sum = 1; } } } int countB = 0; sum = 0; for (int i = 0; i < n; i++) { sum += array[i]; if (i % 2 == 0) { if (sum <= 0) { countB += sum * (-1) + 1; sum = 1; } } else { if (sum >= 0) { countB += sum + 1; sum = -1; } } } System.out.println(Math.min(countA, countB)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -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 iy[] = {0, 0, 1, -1}; int ix[] = {1, -1, 0, 0}; int n, a[10001], sum[10001], ans; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } sum[0] = a[0]; for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + a[i]; if (sum[i - 1] > 0 && sum[i] > 0) { ans += sum[i] + 1; sum[i] = -1; for (int j = i + 1; j < n; j++) { a[i] -= sum[i] + 1; } } else if (sum[i - 1] < 0 && sum[i] < 0) { ans += -sum[i] + 1; sum[i] = 1; for (int j = i + 1; j < n; j++) { a[i] += -sum[i] + 1; } } else if (sum[i] == 0) { if (sum[i - 1] > 0) { ans++; sum[i] = -1; for (int j = i + 1; j < n; j++) { a[i]--; } } else { ans++; sum[i] = 1; for (int j = i + 1; j < n; j++) { a[i]++; } } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) A = [] cnt = 0 for i in range(n-1): A.append(a[i]) if sum(A) > 0 and sum(A) + a[i+1] > 0: cnt += abs(sum(A) + a[i+1])+1 a[i+1] -= abs(sum(A) + a[i+1])+1 elif sum(A) < 0 and sum(A) + a[i+1] < 0: cnt += abs(sum(A) - a[i+1])-1 a[i+1] += abs(sum(A) - a[i+1])-1 if sum(a) == 0: cnt += 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
UNKNOWN
import java.util.* fun main(args: Array<String>) { val sc = Scanner(System.`in`) val n = sc.nextInt() val a = (0 until n).map { sc.next().toLong() } println(problem059c(n, a)) } fun problem059c(n: Int, a: List<Long>): Long { val count1 = compute(n, a) val a = a.toMutableList() val a0 = a[0] var count = 0L if (a0 > 0) { val tmp = a0 + 1 a[0] = a0 - tmp count += tmp } else { val tmp = a0 - 1 a[0] = a0 - tmp count -= tmp } val count2 = compute(n, a) + count return Math.min(count1, count2) } fun compute(n: Int, a: List<Long>): Long { val a = a.toMutableList() var count = 0L var sum = 0L for (i in 0 until n) { if (sum + a[i] == 0L) { count++ if (a[i] < 0) { a[i] = a[i] - 1 } else { a[i] = a[i] + 1 } } sum += a[i] if (i >= n - 1) { continue } val sum2 = sum + a[i + 1] if (sum * sum2 < 0) { continue } else { if (sum > 0) { val tmp = sum2 + 1 a[i + 1] = sum2 - tmp count += tmp } else { val tmp = sum2 - 1 a[i + 1] = sum2 - tmp count -= tmp } } } return count }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int main(void) { int n; std::cin >> n; std::vector<int> a(n); for (int i = 0; i < n; ++i) std::cin >> a[i]; int cost = 0; std::vector<int> s(n, 0); s[0] = a[0]; for (int i = 1; i < n; ++i) { s[i] = s[i - 1] + a[i]; if (s[i] * s[i - 1] < 0) continue; else { if (s[i - 1] < 0) { cost += std::abs(1 - s[i - 1] - a[i]); a[i] = 1 - s[i - 1]; } else { cost += std::abs(-1 - s[i - 1] - a[i]); a[i] = -1 - s[i - 1]; } } s[i] = s[i - 1] + a[i]; } std::cout << cost << std::endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
from copy import copy n = int(input()) a = [int(x) for x in input().split()] ans1=[(-1)**i for i in range(n)] b=copy(a) res_b=0 sb=0 c=copy(a) res_c=0 sc=0 for i in range(n): sb+=a[i] print("sb:",sb) if ans1[i]*sb>0: pass else: b[i]=ans1[i]-(sb-b[i]) sb=sb-a[i]+b[i] res_b+=abs(b[i]-a[i]) sc+=a[i] if -1*ans1[i]*sc>0: pass else: c[i]=-1*ans1[i]-(sc-c[i]) sc=sc-a[i]+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
cpp
#include <bits/stdc++.h> template <typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; } using namespace std; std::mt19937 mt((long long)time(0)); long long dx[4] = {0, 1, 0, -1}; long long dy[4] = {1, 0, -1, 0}; using Weight = long long; using Flow = long long; struct Edge { long long src, dst; Weight weight; Flow cap; Edge() : src(0), dst(0), weight(0) {} Edge(long long s, long long d, Weight w) : src(s), dst(d), weight(w) {} }; using Edges = std::vector<Edge>; using Graph = std::vector<Edges>; using Array = std::vector<Weight>; using Matrix = std::vector<Array>; void add_edge(Graph& g, long long a, long long b, Weight w = 1) { g[a].emplace_back(a, b, w); g[b].emplace_back(b, a, w); } void add_arc(Graph& g, long long a, long long b, Weight w = 1) { g[a].emplace_back(a, b, w); } struct uf_tree { std::vector<long long> parent; long long __size; uf_tree(long long size_) : parent(size_, -1), __size(size_) {} void unite(long long x, long long y) { if ((x = find(x)) != (y = find(y))) { if (parent[y] < parent[x]) std::swap(x, y); parent[x] += parent[y]; parent[y] = x; __size--; } } bool is_same(long long x, long long y) { return find(x) == find(y); } long long find(long long x) { return parent[x] < 0 ? x : parent[x] = find(parent[x]); } long long size(long long x) { return -parent[find(x)]; } long long size() { return __size; } }; template <signed M, unsigned T> struct mod_int { constexpr static signed MODULO = M; constexpr static unsigned TABLE_SIZE = T; signed x; mod_int() : x(0) {} mod_int(long long y) : x(static_cast<signed>(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO)) {} mod_int(signed y) : x(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO) {} mod_int& operator+=(const mod_int& rhs) { if ((x += rhs.x) >= MODULO) x -= MODULO; return *this; } mod_int& operator-=(const mod_int& rhs) { if ((x += MODULO - rhs.x) >= MODULO) x -= MODULO; return *this; } mod_int& operator*=(const mod_int& rhs) { x = static_cast<signed>(1LL * x * rhs.x % MODULO); return *this; } mod_int& operator/=(const mod_int& rhs) { x = static_cast<signed>((1LL * x * rhs.inv().x) % MODULO); return *this; } mod_int operator-() const { return mod_int(-x); } mod_int operator+(const mod_int& rhs) const { return mod_int(*this) += rhs; } mod_int operator-(const mod_int& rhs) const { return mod_int(*this) -= rhs; } mod_int operator*(const mod_int& rhs) const { return mod_int(*this) *= rhs; } mod_int operator/(const mod_int& rhs) const { return mod_int(*this) /= rhs; } bool operator<(const mod_int& rhs) const { return x < rhs.x; } mod_int inv() const { assert(x != 0); if (x <= static_cast<signed>(TABLE_SIZE)) { if (_inv[1].x == 0) prepare(); return _inv[x]; } else { signed a = x, b = MODULO, u = 1, v = 0, t; while (b) { t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return mod_int(u); } } mod_int pow(long long t) const { assert(!(x == 0 && t == 0)); mod_int e = *this, res = mod_int(1); for (; t; e *= e, t >>= 1) if (t & 1) res *= e; return res; } mod_int fact() { if (_fact[0].x == 0) prepare(); return _fact[x]; } mod_int inv_fact() { if (_fact[0].x == 0) prepare(); return _inv_fact[x]; } mod_int choose(mod_int y) { assert(y.x <= x); return this->fact() * y.inv_fact() * mod_int(x - y.x).inv_fact(); } static mod_int _inv[TABLE_SIZE + 1]; static mod_int _fact[TABLE_SIZE + 1]; static mod_int _inv_fact[TABLE_SIZE + 1]; static void prepare() { _inv[1] = 1; for (long long i = 2; i <= (long long)TABLE_SIZE; ++i) { _inv[i] = 1LL * _inv[MODULO % i].x * (MODULO - MODULO / i) % MODULO; } _fact[0] = 1; for (unsigned i = 1; i <= TABLE_SIZE; ++i) { _fact[i] = _fact[i - 1] * signed(i); } _inv_fact[TABLE_SIZE] = _fact[TABLE_SIZE].inv(); for (long long i = (long long)TABLE_SIZE - 1; i >= 0; --i) { _inv_fact[i] = _inv_fact[i + 1] * (i + 1); } } }; template <signed M, unsigned F> std::ostream& operator<<(std::ostream& os, const mod_int<M, F>& rhs) { return os << rhs.x; } template <signed M, unsigned F> std::istream& operator>>(std::istream& is, mod_int<M, F>& rhs) { long long s; is >> s; rhs = mod_int<M, F>(s); return is; } template <signed M, unsigned F> mod_int<M, F> mod_int<M, F>::_inv[TABLE_SIZE + 1]; template <signed M, unsigned F> mod_int<M, F> mod_int<M, F>::_fact[TABLE_SIZE + 1]; template <signed M, unsigned F> mod_int<M, F> mod_int<M, F>::_inv_fact[TABLE_SIZE + 1]; template <signed M, unsigned F> bool operator==(const mod_int<M, F>& lhs, const mod_int<M, F>& rhs) { return lhs.x == rhs.x; } template <long long M, unsigned F> bool operator!=(const mod_int<M, F>& lhs, const mod_int<M, F>& rhs) { return !(lhs == rhs); } const signed MF = 1000010; const signed MOD = 1000000007; using mint = mod_int<MOD, MF>; mint binom(long long n, long long r) { return (r < 0 || r > n || n < 0) ? 0 : mint(n).choose(r); } mint fact(long long n) { return mint(n).fact(); } mint inv_fact(long long n) { return mint(n).inv_fact(); } template <typename T, typename E> struct SegmentTree { typedef function<T(T, T)> F; typedef function<T(T, E)> G; typedef function<E(E, E)> H; typedef function<E(E, long long)> P; long long n; F f; G g; H h; P p; T d1; E d0; vector<T> dat; vector<E> laz; SegmentTree( long long n_, F f, G g, H h, T d1, E d0, vector<T> v = vector<T>(), P p = [](E a, long long b) { return a; }) : f(f), g(g), h(h), d1(d1), d0(d0), p(p) { init(n_); if (n_ == (long long)v.size()) build(n_, v); } void init(long long n_) { n = 1; while (n < n_) n *= 2; dat.clear(); dat.resize(2 * n - 1, d1); laz.clear(); laz.resize(2 * n - 1, d0); } void build(long long n_, vector<T> v) { for (long long i = 0; i < n_; i++) dat[i + n - 1] = v[i]; for (long long i = n - 2; i >= 0; i--) dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]); } inline void eval(long long len, long long k) { if (laz[k] == d0) return; if (k * 2 + 1 < n * 2 - 1) { laz[k * 2 + 1] = h(laz[k * 2 + 1], laz[k]); laz[k * 2 + 2] = h(laz[k * 2 + 2], laz[k]); } dat[k] = g(dat[k], p(laz[k], len)); laz[k] = d0; } T update(long long a, long long b, E x, long long k, long long l, long long r) { eval(r - l, k); if (r <= a || b <= l) return dat[k]; if (a <= l && r <= b) { laz[k] = h(laz[k], x); return g(dat[k], p(laz[k], r - l)); } return dat[k] = f(update(a, b, x, k * 2 + 1, l, (l + r) / 2), update(a, b, x, k * 2 + 2, (l + r) / 2, r)); } T update(long long a, long long b, E x) { return update(a, b, x, 0, 0, n); } T query(long long a, long long b, long long k, long long l, long long r) { eval(r - l, k); if (r <= a || b <= l) return d1; if (a <= l && r <= b) return dat[k]; T vl = query(a, b, k * 2 + 1, l, (l + r) / 2); T vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return f(vl, vr); } T query(long long a, long long b) { return query(a, b, 0, 0, n); } }; class compress { public: static const long long MAP = 10000000; map<long long, long long> zip; long long unzip[MAP]; compress(vector<long long>& x) { sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end()); for (long long i = 0; i < x.size(); i++) { zip[x[i]] = i; unzip[i] = x[i]; } } }; unsigned euclidean_gcd(unsigned a, unsigned b) { while (1) { if (a < b) swap(a, b); if (!b) break; a %= b; } return a; } template <class T> struct CumulativeSum2D { vector<vector<T>> data; CumulativeSum2D(long long W, long long H) : data(W + 1, vector<long long>(H + 1, 0)) {} void add(long long x, long long y, T z) { ++x, ++y; if (x >= data.size() || y >= data[0].size()) return; data[x][y] += z; } void build() { for (long long i = 1; i < data.size(); i++) { for (long long j = 1; j < data[i].size(); j++) { data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1]; } } } T query(long long sx, long long sy, long long gx, long long gy) { return (data[gx][gy] - data[sx][gy] - data[gx][sy] + data[sx][sy]); } }; long long nC2(long long n) { return n * (n - 1) / 2; } class node { public: long long depth; long long num; node(long long d, long long n) { depth = d; num = n; } }; CumulativeSum2D<long long> sumB(4001, 4001); template <class T> struct CumulativeSum { vector<T> data; CumulativeSum(long long sz) : data(sz, 0){}; void add(long long k, T x) { data[k] += x; } void build() { for (long long i = 1; i < data.size(); i++) { data[i] += data[i - 1]; } } T query(long long k) { if (k < 0) return (0); return (data[min(k, (long long)data.size() - 1)]); } T query(long long left, long long right) { return query(right) - query(left - 1); } }; std::vector<bool> IsPrime; void sieve(size_t max) { if (max + 1 > IsPrime.size()) { IsPrime.resize(max + 1, true); } IsPrime[0] = false; IsPrime[1] = false; for (size_t i = 2; i * i <= max; ++i) if (IsPrime[i]) for (size_t j = 2; i * j <= max; ++j) IsPrime[i * j] = false; } vector<int64_t> divisor(int64_t n) { vector<int64_t> ret; for (int64_t i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(begin(ret), end(ret)); return (ret); } long long binary_search(function<bool(long long)> isOk, long long ng, long long ok) { while (abs(ok - ng) > 1) { long long mid = (ok + ng) / 2; if (isOk(mid)) ok = mid; else ng = mid; } return ok; } std::pair<std::vector<Weight>, bool> bellmanFord(const Graph& g, long long s) { long long n = g.size(); const Weight inf = std::numeric_limits<Weight>::max() / 8; Edges es; for (long long i = 0; i < n; i++) for (auto& e : g[i]) es.emplace_back(e); std::vector<Weight> dist(n, inf); dist[s] = 0; bool negCycle = false; for (long long i = 0;; i++) { bool update = false; for (auto& e : es) { if (dist[e.src] != inf && dist[e.dst] > dist[e.src] + e.weight) { dist[e.dst] = dist[e.src] + e.weight; update = true; } } if (!update) break; if (i > n) { negCycle = true; break; } } return std::make_pair(dist, !negCycle); } std::pair<std::vector<Weight>, bool> bellmanFord(const Graph& g, long long s, long long d) { long long n = g.size(); const Weight inf = std::numeric_limits<Weight>::max() / 8; Edges es; for (long long i = 0; i < n; i++) for (auto& e : g[i]) es.emplace_back(e); std::vector<Weight> dist(n, inf); dist[s] = 0; bool negCycle = false; for (long long i = 0; i < n * 2; i++) { bool update = false; for (auto& e : es) { if (dist[e.src] != inf && dist[e.dst] > dist[e.src] + e.weight) { dist[e.dst] = dist[e.src] + e.weight; update = true; if (e.dst == d && i == n * 2 - 1) negCycle = true; } } if (!update) break; } return std::make_pair(dist, !negCycle); } vector<long long> Manachar(string S) { long long len = S.length(); vector<long long> R(len); long long i = 0, j = 0; while (i < S.size()) { while (i - j >= 0 && i + j < S.size() && S[i - j] == S[i + j]) ++j; R[i] = j; long long k = 1; while (i - k >= 0 && i + k < S.size() && k + R[i - k] < j) R[i + k] = R[i - k], ++k; i += k; j -= k; } return R; } std::vector<long long> tsort(const Graph& g) { long long n = g.size(), k = 0; std::vector<long long> ord(n), in(n); for (auto& es : g) for (auto& e : es) in[e.dst]++; std::queue<long long> q; for (long long i = 0; i < n; ++i) if (in[i] == 0) q.push(i); while (q.size()) { long long v = q.front(); q.pop(); ord[k++] = v; for (auto& e : g[v]) { if (--in[e.dst] == 0) { q.push(e.dst); } } } return *std::max_element(in.begin(), in.end()) == 0 ? ord : std::vector<long long>(); } std::vector<Weight> dijkstra(const Graph& g, long long s) { const Weight INF = std::numeric_limits<Weight>::max() / 8; using state = std::tuple<Weight, long long>; std::priority_queue<state> q; std::vector<Weight> dist(g.size(), INF); dist[s] = 0; q.emplace(0, s); while (q.size()) { Weight d; long long v; std::tie(d, v) = q.top(); q.pop(); d *= -1; if (dist[v] < d) continue; for (auto& e : g[v]) { if (dist[e.dst] > dist[v] + e.weight) { dist[e.dst] = dist[v] + e.weight; q.emplace(-dist[e.dst], e.dst); } } } return dist; } Matrix WarshallFloyd(const Graph& g) { auto const INF = std::numeric_limits<Weight>::max() / 8; long long n = g.size(); Matrix d(n, Array(n, INF)); for (long long i = (0); i < (long long)(n); i++) d[i][i] = 0; for (long long i = (0); i < (long long)(n); i++) for (auto& e : g[i]) d[e.src][e.dst] = std::min(d[e.src][e.dst], e.weight); for (long long k = (0); k < (long long)(n); k++) for (long long i = (0); i < (long long)(n); i++) for (long long j = (0); j < (long long)(n); j++) { if (d[i][k] != INF && d[k][j] != INF) { d[i][j] = std::min(d[i][j], d[i][k] + d[k][j]); } } return d; } void solve() { long long n, m; cin >> n; vector<long long> p(n); for (long long i = (0); i < (long long)(n); i++) { cin >> p[i]; } cin >> m; vector<long long> q(m); for (long long i = (0); i < (long long)(m); i++) { cin >> q[i]; } long long pEven = 0, pOdd = 0, qEven = 0, qOdd = 0; for (long long i : p) { if (i % 2 == 0) { pEven++; } else { pOdd++; } } for (long long i : q) { if (i % 2 == 0) { qEven++; } else { qOdd++; } } long long ans = 0; ans += pEven * qEven; ans += pOdd * qOdd; cout << ans << "\n"; } const long long BLACK = 1, WHITE = 0; bool isValid(vector<vector<long long>>& mapData, long long gyo, long long retu) { bool f = true; for (long long i = (0); i < (long long)(gyo); i++) { for (long long j = (0); j < (long long)(retu); j++) { long long colorCnt = 0; if (j > 0 && mapData[i][j] == mapData[i][j - 1]) { colorCnt++; } if (i > 0 && mapData[i][j] == mapData[i - 1][j]) { colorCnt++; } if (i < gyo - 1 && mapData[i][j] == mapData[i + 1][j]) { colorCnt++; } if (j < retu - 1 && mapData[i][j] == mapData[i][j + 1]) { colorCnt++; } if (colorCnt > 1) { f = false; } } } return f; } void getNext(long long nowX, long long nowY, long long* pOutX, long long* pOutY, long long gyo, long long retu) { if (nowX == retu - 1) { *pOutY = nowY + 1; *pOutX = 0; return; } *pOutX = nowX + 1; *pOutY = nowY; } void dfs(vector<vector<long long>> mapData, long long nowX, long long nowY, long long gyo, long long retu, long long* outCnt) { if (nowX == retu - 1 && nowY == gyo - 1) { mapData[nowY][nowX] = BLACK; if (isValid(mapData, gyo, retu)) { *outCnt = *outCnt + 1; } mapData[nowY][nowX] = WHITE; if (isValid(mapData, gyo, retu)) { *outCnt = *outCnt + 1; } return; } mapData[nowY][nowX] = BLACK; long long nextX, nextY; getNext(nowX, nowY, &nextX, &nextY, gyo, retu); dfs(mapData, nextX, nextY, gyo, retu, outCnt); mapData[nowY][nowX] = WHITE; getNext(nowX, nowY, &nextX, &nextY, gyo, retu); dfs(mapData, nextX, nextY, gyo, retu, outCnt); } void dec(map<long long, long long>& ma, long long a) { ma[a]--; if (ma[a] == 0) { ma.erase(a); } } signed main() { long long N; cin >> N; vector<long long> A(N + 2); vector<long long> cu(N + 2); long long su = 0; for (long long i = (0); i < (long long)(N); i++) { cin >> A[i]; su += A[i]; cu[i] = su; } long long ans = 0; for (long long i = (0); i < (long long)(N); i++) { if (cu[i] == 0) { ans++; if (i == 0) { if (cu[i + 1] < 0) { cu[i] = 1; } else { cu[i] = -1; } } else { if (cu[i - 1] < 0) { cu[i] = 1; } else { cu[i] = -1; } } } if (i == N - 1) { break; } if (cu[i] < 0 == cu[i + 1] < 0) { if (cu[i + 1] > 0) { ans += cu[i + 1] + 1; cu[i + 1] -= cu[i + 1] + 1; } else { ans += -cu[i + 1] + 1; cu[i + 1] += -cu[i + 1] + 1; } } cu[i + 2] = cu[i + 1] + A[i + 2]; } cout << ans << "\n"; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import numpy as np N = int(input()) a_s = input().split() for i in range(N): a_s[i] = int(a_s[i]) a_s = np.array(a_s) #sum = 1,-1,1, ... ans1 = 0 sum0 = 0 for i ,a in enumerate(a_s): sum1 = sum0 + a if i%2==0: if sum1*sum0<0: pass else: ans1 += abs(1 - sum1) sum1 = 1 else: if sum1*sum0<0: pass else: ans1 += abs(-1 - sum1) sum1 = -1 sum0 = sum1 #sum = -1,1,-1, ... ans2 = 0 sum0 = 0 for i ,a in enumerate(a_s): sum2 = sum0 + a if i%2==0: if sum2*sum0<0: pass else: ans2 += abs(-1 - sum2) sum2 = -1 else: if sum2*sum0<0: pass else: ans2 += abs(1 - sum2) sum2 = 1 sum0 = sum2 ans = min(ans1,ans2) 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; string to_hex(int x) { stringstream ss; ss << hex << x; return ss.str(); } inline int get_int() { int ret; scanf("%d", &ret); return ret; } inline vector<int> get_ints(int n) { vector<int> ret(n); for (int i = 0; i < (int)n; i++) { scanf("%d", &ret[i]); } return ret; } inline string get_str() { string ret; cin >> ret; return ret; } bool is_prime(int n) { int s = sqrt(n) + 1; for (int i = 2; i <= s; ++i) { if (n % i == 0) { return 0; } } return 1; } vector<pair<int, int> > prime_division(int n) { vector<pair<int, int> > ret; int s = sqrt(n) + 1; int c = 0; for (int i = 2; i <= n; ++i) { if (n % i == 0) { c = 0; do { ++c; n /= i; } while (n % i == 0); ret.push_back(pair<int, int>(i, c)); } } return ret; } string to_string(string s) { return s; } template <class T> string to_string(vector<T> v) { string ret = "{"; for (int i = 0; i < (int)v.size() - 1; i++) { ret += to_string(v[i]) + ","; } if (v.size() > 0) { ret += to_string((v)[(v).size() - 1]); } ret += "}"; return ret; } void debug_print() { cerr << endl; } template <class Head, class... Tail> void debug_print(Head head, Tail... tail) { cerr << to_string(head) << " "; debug_print(tail...); } template <class... T> void debug(T... args) { cerr << "[" << 85 << "]: "; debug_print(args...); } void print() { cout << endl; } template <class Head, class... Tail> void print(Head head, Tail... tail) { cout << to_string(head); print(tail...); } int main() { int(n); scanf("%d", &(n)); long long ans = 0, sum = 0; int(a); scanf("%d", &(a)); sum = a; for (int i = 0; i < (int)n - 1; i++) { int(a); scanf("%d", &(a)); if (sum < 0 && sum + a <= 0) { ans += 1 - (sum + a); sum = 1; } else if (sum > 0 && sum + a >= 0) { ans += (sum + a) + 1; sum = -1; } else { sum += a; } } print(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 ll = long long; using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<ll> v(n, 0); for (int i = (int)(0); i < (int)(n); i++) cin >> v[i]; vector<int> p1(n + 1, 1), p2(n + 1, 1); for (int i = (int)(0); i < (int)(n + 1); i++) { if (i % 2 == 0) p1[i] *= -1; } for (int i = (int)(0); i < (int)(n + 1); i++) { if (i % 2 == 1) p2[i] *= -1; } cerr << "p1" ":[ "; for (auto macro_vi : p1) { cerr << macro_vi << " "; } cerr << "]" << endl; cerr << "p2" ":[ "; for (auto macro_vi : p2) { cerr << macro_vi << " "; } cerr << "]" << endl; priority_queue<int, vector<int>, greater<int> > pq; cerr << "v" ":[ "; for (auto macro_vi : v) { cerr << macro_vi << " "; } cerr << "]" << endl; vector<ll> sum_until(n + 1, 0); int cnt; cnt = 0; for (int i = 1; i <= n; i++) { sum_until[i] = sum_until[i - 1] + v[i - 1]; cerr << "sum_until" ":[ "; for (auto macro_vi : sum_until) { cerr << macro_vi << " "; } cerr << "]" << endl; if (sum_until[i] * p1[i] < 0) { int plus = abs(sum_until[i]); cerr << "(" "i" "," "plus * p1[i]" "):(" << i << "," << plus * p1[i] << ")" << endl; cerr << "sum_until[i]" ":" << sum_until[i] << endl; sum_until[i] += plus * p1[i] + p1[i]; cerr << "sum_until[i]" ":" << sum_until[i] << endl; cnt += abs(plus * p1[i]) + 1; } } cerr << "cnt" ":" << cnt << endl; pq.push(cnt); p1 = p2; cerr << "sum_until" ":[ "; for (auto macro_vi : sum_until) { cerr << macro_vi << " "; } cerr << "]" << endl; cnt = 0; for (int i = 1; i <= n; i++) { cerr << "i" ":" << i << endl; sum_until[i] = sum_until[i - 1] + v[i - 1]; cerr << "sum_until" ":[ "; for (auto macro_vi : sum_until) { cerr << macro_vi << " "; } cerr << "]" << endl; if (sum_until[i] * p1[i] < 0) { int plus = abs(sum_until[i]); cerr << "(" "i" "," "plus * p1[i]" "):(" << i << "," << plus * p1[i] << ")" << endl; cerr << "sum_until[i]" ":" << sum_until[i] << endl; sum_until[i] += plus * p1[i] + p1[i]; cerr << "sum_until[i]" ":" << sum_until[i] << endl; cnt += abs(plus * p1[i]) + 1; } else if (sum_until[i] == 0) { sum_until[i] = p1[i]; cnt += 1; } } pq.push(cnt); cerr << "cnt" ":" << cnt << endl; cout << pq.top() << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long MOD = 1000000007; const int MAX_N = 100000005; int n; int a[MAX_N]; int check(long long sum, long long ans) { for (int i = 1; i < n; i++) { long long t = sum + a[i]; if ((sum >= 0 && t < 0) || (sum < 0 && t >= 0)) { sum = t; if (sum == 0) { sum = 1; ans++; } continue; } long long at; if (sum >= 0) at = -1 - sum; else at = 1 - sum; ans = ans + abs(a[i] - at); sum = sum + at; } return ans; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } long long ta, s; ta = a[0]; s = 0; if (ta == 0) { ta = 1; s = 1; } long long another; if (a[0] >= 0) another = -1; else another = 1; long long a1 = check(ta, s); long long a2 = check(another, abs(a[0] - another)); long long ans = min(a1, a2); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = list(map(int,input().split())) cur = 1 ans1 = abs(A[0] - 1) ans2 = abs(A[0] + 1) for a in A[1:]: if cur > 0: if a + cur >= 0: ans1 += abs(-1 - (a + cur)) cur = -1 else: cur = a + cur elif cur < 0: if a + cur <= 0: ans1 += abs(1 - (a + cur)) cur = 1 else: cur = a + cur for a in A[1:]: if cur > 0: if a + cur >= 0: ans2 += abs(-1 - (a + cur)) cur = -1 else: cur = a + cur elif cur < 0: if a + cur <= 0: ans2 += abs(1 - (a + cur)) cur = 1 else: cur = a + cur print(min(ans1,ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; int sum1 = 0, res1 = 0; for (int i = 0; i < N; i++) { sum1 += A[i]; if (i % 2 == 0 && sum1 <= 0) { res1 += (1 - sum1); sum1 = 1; } if (i % 2 == 1 && sum1 >= 0) { res1 += (sum1 + 1); sum1 = -1; } } int sum2 = 0, res2 = 0; for (int i = 0; i < N; i++) { sum2 += A[i]; if (i % 2 == 0 && sum2 >= 0) { res2 += (1 + sum2); sum2 = -1; } if (i % 2 == 1 && sum2 <= 0) { res2 += (1 - sum2); sum2 = 1; } } cout << min(res1, res2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, ansa = 0, ansb = 0, suma = 0, sumb = 0; cin >> n; for (int i = 0; i < (n); i++) { int a, b; cin >> b; a = b; if (i % 2 == 0) { if (suma + a <= 0) { ansa = 1 - a - suma; a = 1 - suma; } if (sumb + b >= 0) { ansb = sumb + b + 1; b = -1 - sumb; } } else { if (suma + a >= 0) { ansa = suma + a + 1; a = -1 - suma; } if (sumb + b <= 0) { ansb = 1 - b - sumb; b = 1 - sumb; } } suma += a; sumb += b; } cout << min(ansa, ansb) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines sys.setrecursionlimit(10 ** 7) n = int(readline()) a = list(map(int, readline().split())) if a[0] > 0: cnt2 = a[0] + 1 now2 = -1 cnt1 = 0 now1 = a[0] else: cnt2 = 0 now2 = a[0] cnt1 = -a[0] + 1 now1 = 1 for i, aa in enumerate(a[1:]): if i % 2 == 0: if aa + now2 > 0: now2 += aa else: cnt2 += abs(now2 + aa) + 1 now2 = 1 if now1 + aa < 0: now1 += aa else: cnt1 += now1 + aa + 1 now1 = -1 else: if aa + now1 > 0: now1 += aa else: cnt1 += abs(now1 + aa) + 1 now1 = 1 if now2 + aa < 0: now2 += aa else: cnt2 += now2 + aa + 1 now2 = -1 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; void answer1() { cin.tie(0); ios_base::sync_with_stdio(false); int n; cin >> n; vector<int> a(n); for (int& a_i : a) { cin >> a_i; } long long count = 0; long long sum = 0; long long count2 = 0; long long sum2 = 0; bool is_positive = a.at(0) > 0; for (int i = 0; i < a.size(); i++) { sum += a.at(i); if (is_positive) { if (sum <= 0) { long long diff = 1 - sum; count += diff; sum += diff; } if (sum2 >= 0) { long long diff = 1 + sum2; count2 += diff; sum2 -= diff; } } else { if (sum >= 0) { long long diff = 1 + sum; count += diff; sum -= diff; } if (sum2 <= 0) { long long diff = 1 - sum2; count2 += diff; sum2 += diff; } } is_positive = !is_positive; } cout << min(count, count2) << endl; } int main() { answer1(); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.io.PrintWriter; import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); final int MOD = 1000000007; final int INF = 1100000000; //入力 int n = sc.nextInt(); int[] a = new int[n]; for(int i = 0; i < n; i++){ a[i] = sc.nextInt(); } sc.close(); //処理 long ans = -1; boolean bool = true; for(int count = 0; count < 2; count++){ long temp = 0; bool ^= true; boolean f = bool; long sum = 0; for(int i = 0; i < n; i++){ sum += a[i]; if(sum > 0 == f){ //nothing }else{ temp += Math.abs(sum) + 1; if(f){ sum = 1; }else{ sum = -1; } } f ^= true; } if(ans == -1){ ans = temp; }else{ ans = Math.min(ans, temp); } } //出力 out.println(ans); out.flush(); } static class Pair{ int w,v; public Pair(int a, int b){ this.w = a; this.v = b; } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = list(map(int, input().split())) R = [0] * n R[0] = A[0] for i in range(1, n): R[i] = R[i-1] + A[i] def solve(r, inc=0): ans = 0 # search is_plus = (r[0] > 0) for i in range(1, n): if (is_plus and r[i]+inc < 0) or (not is_plus and r[i]+inc > 0): pass else: ans += abs(r[i]+inc) + 1 if is_plus: inc -= abs(r[i]+inc) + 1 else: inc += abs(r[i]+inc) + 1 is_plus = (r[i]+inc > 0) return ans ret = solve(R, 0) # modify is_plus = (R[0] > 0) ans0 = abs(R[0]) + 1 if is_plus: for i in range(n): R[i] -= abs(R[0]) + 1 else: for i in range(n): R[i] += abs(R[0]) + 1 print(min(ret, ans0 + solve(R, 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 check(vector<long> a) { long time = 0; long pre_sum = a.at(0); int n = a.size(); if (a.at(0) < 0) { for (int i = 1; i < n; i++) { long sum = pre_sum + a.at(i); if (i % 2 == 1 && sum <= 0) { time += abs(sum - 1); sum = 1; } else if (i % 2 == 0 && sum >= 0) { time += abs(sum + 1); sum = -1; } pre_sum = sum; } } else if (a.at(0) > 0) { for (int i = 1; i < n; i++) { long sum = pre_sum + a.at(i); if (i % 2 == 0 && sum <= 0) { time += abs(sum - 1); sum = 1; } else if (i % 2 == 1 && sum >= 0) { time += abs(sum + 1); sum = -1; } pre_sum = sum; } } return time; } int zerocheck(vector<long> a) { a.at(0) = 1; long time1 = check(a) + 1; a.at(0) = -1; long time2 = check(a) + 1; long time = min(time1, time2); return time; } int main() { int n; cin >> n; int time = 0; vector<long> a(n); for (auto& x : a) { cin >> x; } if (a.at(0) == 0) { time = zerocheck(a); } else { time = check(a); } cout << time << endl; }