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
python3
n = int(input()) a = list(map(int,input().split())) def judge(count): idx = 0 wa = a[0] for i in range(2,n+1): if count == -1: if wa + a[i-1] < 0: wa += a[i-1] else: check = wa + a[i-1] + 1 j = a[i-1] a[i-1] = j - check idx += check wa = -1 elif count == 1: if wa +a[i-1] > 0: wa += a[i-1] else: check = 1 - wa - a[i-1] j = a[i-1] a[i-1] = check+ j idx += check wa = 1 count *= -1 return idx if a[0] > 0: b = judge(-1) print(b) elif a[0] < 0: c = judge(1) print(c) else: a[0] = 1 b = judge(-1) a[0] = -1 c = judge(1) print(min(b,c))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
// 要復習 #include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG int64_t main() { int64_t n; cin >> n; int64_t a[n], b[n]; for (int64_t i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; } //positive-negative-positive... case int64_t case1, total; if (a[0] > 0) { case1 = 0; total = a[0]; } else //if (a[0] <= 0) { case1 = 1 - a[0]; total = 1; } for (int64_t i = 1; i < n; i++) { int64_t ttmp = total + a[i]; int64_t atmp = a[i]; if ((ttmp * total) < 0 && ttmp != 0) { total = ttmp; } else { if (total > 0) { a[i] = -total - 1; } else { a[i] = -total + 1; } total += a[i]; } case1 += abs(a[i] - atmp); } //negative-positive... case int64_t case2; if (b[0] < 0) { case2 = 0; total = b[0]; } else //if (a[0] >= 0) { case2 = -1 - b[0]; total = -1; } for (int64_t i = 1; i < n; i++) { int64_t ttmp = total + b[i]; int64_t atmp = b[i]; if ((ttmp * total) < 0 && ttmp != 0) { total = ttmp; } else { if (total > 0) { b[i] = -total - 1; } else { b[i] = -total + 1; } total += b[i]; } case2 += abs(b[i] - atmp); } cout << min(case1, case2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <class T> int former(const vector<T> &v, T x) { return upper_bound(v.begin(), v.end(), x) - v.begin() - 1; } template <class T> int latter(const vector<T> &v, T x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } const long long LLINF = 1LL << 60; const int INTINF = 1 << 30; const int MAX = 510000; const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } struct UnionFind { vector<long long> par; UnionFind(long long n) : par(n, -1) {} void init(long long n) { par.assign(n, -1); } long long root(long long x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(long long x, long long y) { return root(x) == root(y); } bool merge(long long x, long long y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return true; } long long size(long long x) { return -par[root(x)]; } }; template <typename T> vector<T> dijkstra(int s, vector<vector<pair<int, T> > > &G) { const T INF = numeric_limits<T>::max(); using P = pair<T, int>; int n = G.size(); vector<T> d(n, INF); vector<int> b(n, -1); priority_queue<P, vector<P>, greater<P> > q; d[s] = 0; q.emplace(d[s], s); while (!q.empty()) { P p = q.top(); q.pop(); int v = p.second; if (d[v] < p.first) continue; for (auto &e : G[v]) { int u = e.first; T c = e.second; if (d[u] > d[v] + c) { d[u] = d[v] + c; b[u] = v; q.emplace(d[u], u); } } } return d; } vector<vector<int> > bfs(vector<string> &s, int sy, int sx, char wall, int dir) { int h = s.size(), w = s.front().size(); vector<vector<int> > dp(h, vector<int>(w, -1)); using P = pair<int, int>; queue<P> q; dp[sy][sx] = 0; q.emplace(sy, sx); int dy[] = {1, -1, 0, 0, 1, 1, -1, -1}; int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; auto in = [&](int y, int x) { return 0 <= y && y < h && 0 <= x && x < w; }; while (!q.empty()) { int y, x; tie(y, x) = q.front(); q.pop(); for (int k = 0; k < dir; k++) { int ny = y + dy[k], nx = x + dx[k]; if (!in(ny, nx) || s[ny][nx] == wall) continue; if (~dp[ny][nx]) continue; dp[ny][nx] = dp[y][x] + 1; q.emplace(ny, nx); } } return dp; } int64_t power(int64_t x, int64_t n, int64_t mod) { int64_t ret = 1; while (n > 0) { if (n & 1) (ret *= x) %= mod; (x *= x) %= mod; n >>= 1; } return ret; } vector<int> sieve_of_eratosthenes(int n) { vector<int> primes(n); for (int i = 2; i < n; ++i) primes[i] = i; for (int i = 2; i * i < n; ++i) if (primes[i]) for (int j = i * i; j < n; j += i) primes[j] = 0; return primes; } std::vector<long long> divisor(long long n) { std::vector<long long> ret; for (long long i = 1; i * i <= n; ++i) { if (n % i == 0) { ret.push_back(i); if (i != 1 && i * i != n) { ret.push_back(n / i); } } } return ret; } const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; int main(void) { long long n; cin >> n; vector<long long> a(n); for (long long i = 0, i_len = (n); i < i_len; ++i) cin >> a[i]; long long sum = 0; long long count = 0; long long ans = 0; for (long long i = 0, i_len = (n); i < i_len; ++i) { sum += a[i]; if (i % 2 == 0) if (sum < 0) ans += 1 - sum; if (i % 2 != 0) if (sum > 0) ans += sum + 1; } sum = 0; for (long long i = 0, i_len = (n); i < i_len; ++i) { sum += a[i]; if (i % 2 == 0) if (sum < 0) count += sum + 1; if (i % 2 != 0) if (sum > 0) count += 1 - sum; } chmin(ans, count); cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int ms = 1e5 + 9; int val; int main() { int f = 0; long long soma = 0, ans = 0; int n; cin >> n; cin >> soma; if (soma < 0) f = 1; for (int i = 0; i < n - 1; i++) { cin >> val; 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; } 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
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ABC059C { class Program { static void Main(string[] args) { int N = int.Parse(Console.ReadLine()); long[] x = new long[N]; var target = Console.ReadLine().Split(' '); for(int i=0;i<N;i++) x[i] = long.Parse(target[i]); long count = 0; if(x[0] != 0){ count = solve(x, x[0]); } else { count = solve(x, 1) + 1; long tmp = solve(x, -1) + 1; if (count > tmp) count = tmp; } Console.WriteLine(count); } static long solve(long[] x, long a){ long total = a; long count = 0; for(int i=1; i<x.Length;i++){ long tmp = 0; if(total > 0){ tmp = (long)Math.Min(0, -total-1-x[i]); } else { tmp = (long)Math.Max(0, -total+1-x[i]); } count += (long)Math.Abs(tmp); total += x[i] + 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> using namespace std; template <typename T> bool PN(T x) { if (x <= 1) return false; if (x == 2) return true; for (int i = 2; i < sqrt(x) + 1; i++) if (x % i == 0) return false; return true; } const long long MOD = 1e9 + 7; void solve() { int n; cin >> n; int a[n]; long long sum = 0; long long sum2 = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; } long long ans = 0; long long ans2 = 0; for (int i = 0; i < n; ++i) { if (i == 0) { sum += a[i]; continue; } if (sum > 0) { sum += a[i]; if (sum > 0) { ans += sum + 1; sum = -1; } else if (sum < 0) { continue; } else { ans++; sum = -1; } } else if (sum < 0) { sum += a[i]; if (sum > 0) { continue; } else if (sum < 0) { ans += abs(sum) + 1; sum = 1; } else { ans++; sum = 1; } } } for (int i = 0; i < n; ++i) { if (i == 0) { sum2 = -(a[i]) / abs(a[i]); ans2 += abs(a[i] + 1); continue; } if (sum2 > 0) { sum2 += a[i]; if (sum2 > 0) { ans2 += sum2 + 1; sum2 = -1; } else if (sum2 < 0) { continue; } else { ans2++; sum2 = -1; } } else if (sum2 < 0) { sum2 += a[i]; if (sum2 > 0) { continue; } else if (sum < 0) { ans2 += abs(sum2) + 1; sum2 = 1; } else { ans2++; sum2 = 1; } } } cout << min(ans, ans2) << 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
UNKNOWN
def rec(ary, n, i, sum, cnt) return cnt if i == n if sum < 0 sum += ary[i] if sum <= 0 diff = -sum+1 cnt += diff sum += diff end elsif sum > 0 sum += ary[i] if sum >= 0 diff = sum+1 cnt += diff sum -= diff end end return rec(ary, n, i+1, sum, cnt) end # main n = gets.to_i ary = gets.split(' ').map(&:to_i) puts rec(ary, n, 1, ary[0], 0)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 30; const long long LINF = 1LL << 50; const int NIL = -1; const int MAX = 10000; const int mod = 1000000007; const double pi = 3.141592653589; int main() { int N; cin >> N; vector<long long> a(N); for (int i = 0; i < N; i++) cin >> a[i]; vector<long long> sum(N); sum[0] = a[0]; for (int i = 1; i < N; i++) sum[i] = sum[i - 1] + a[i]; vector<int> cum(2), cnt(2); for (int i = 0; i < N; i++) { int i1 = i % 2, i2 = (i + 1) % 2; if (sum[i] + cum[i1] <= 0) { int d = abs(sum[i] + cum[i1]) + 1; cnt[i1] += d; cum[i1] += d; } if (sum[i] + cum[i2] >= 0) { int d = abs(sum[i] + cum[i2]) + 1; cnt[i2] += d; cum[i2] -= d; } } cout << min(cnt[0], cnt[1]) << '\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; long long a[n], cntplus = 0, cntplus1 = 0, cntminus = 0, cntminus1 = 0; for (int i = 0; i < n; i++) cin >> a[i]; if (a[0] > 0) { long long s = a[0]; for (int i = 1; i < n; i++) { if (i % 2) { if (0 <= s + a[i]) { cntplus += s + a[i] + 1; a[i] = -1 - s; } } else { if (s + a[i] <= 0) { cntplus += 1 - (s + a[i]); a[i] = 1 - s; } } s += a[i]; } s = -1; cntplus1 += a[0] + 1; for (int i = 1; i < n; i++) { if (i % 2) { if (s + a[i] <= 0) { cntplus1 += 1 - (s + a[i]); a[i] = 1 - s; } } else { if (0 <= s + a[i]) { cntplus1 += s + a[i] + 1; a[i] = -1 - s; } } s += a[i]; } cout << min(cntplus, cntplus1) << endl; } else { long long s = a[0]; for (int i = 1; i < n; i++) { if (i % 2) { if (s + a[i] <= 0) { cntminus += 1 - (s + a[i]); a[i] = 1 - s; } } else { if (0 <= s + a[i]) { cntminus += s + a[i] + 1; a[i] = -1 - s; } } s += a[i]; } s = 1; cntminus1 += -a[0] + 1; for (int i = 1; i < n; i++) { if (i % 2) { if (0 <= s + a[i]) { cntminus1 += s + a[i] + 1; a[i] = -1 - s; } } else { if (s + a[i] <= 0) { cntminus1 += 1 - (s + a[i]); a[i] = 1 - s; } } s += a[i]; } cout << min(cntminus, cntminus1) << endl; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const double eps = 1e-10; const int MAX = 200000; int n; int count(int flag, vector<long long> a) { int c = 0; long long sum = 0; for (int i = 0; i < n; i++) { sum = sum + a[i]; if (sum == 0) { if (flag) { c--; sum--; } else { c++; sum++; } } else if (sum > 0) { if (flag) { c += (sum + 1); sum = -1; } } else { if (!flag) { c += (-sum + 1); sum = 1; } } flag = !flag; } return c; } int main() { long long t; vector<long long> a; cin >> n; for (int i = 0; i < n; i++) { cin >> t; a.push_back(t); } cout << min(count(0, a), count(1, a)) << 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
using System; using System.Text; using System.Linq; using System.Collections; using System.Collections.Generic; using static System.Console; using static System.Math; namespace AtCorder { public class Program { public static void Main(string[] args) { new Program().Solve(new ConsoleInput(Console.In, ' ')); } public void Solve(ConsoleInput cin) { var n = cin.ReadInt; var a = cin.ReadLongArray(n); var ans = 0L; var pre = a[0]; var now = a[0]; for(int i = 1; i < n; i++) { now += a[i]; if(pre * now < 0) { pre += a[i]; continue; } if(now > 0) { ans += a[i] + (pre + 1); now -= a[i] + (pre + 1); a[i] = -1 * (pre + 1); } else { ans += -1 * (pre - 1) - a[i]; now += -1 * (pre - 1) - a[i]; a[i] = -1 * (pre - 1); } pre += a[i]; } WriteLine(ans); } public long C(int X, int Y) { if (Y == 0 || Y == X) { return 1; } if (X < Y) { return 0; } var Pascal = new long[X + 1, X + 1]; for (int i = 0; i <= X; i++) { Pascal[i, 0] = 1L; Pascal[i, i] = 1L; } for (int i = 2; i <= X; i++) { for (int j = 1; j < i; j++) { Pascal[i, j] = Pascal[i - 1, j] + Pascal[i - 1, j - 1]; } } return Pascal[X, Y]; } public class ConsoleInput { private readonly System.IO.TextReader _stream; private char _separator = ' '; private Queue<string> inputStream; public ConsoleInput(System.IO.TextReader stream, char separator = ' ') { this._separator = separator; this._stream = stream; inputStream = new Queue<string>(); } public string Read { get { if (inputStream.Count != 0) return inputStream.Dequeue(); string[] tmp = _stream.ReadLine().Split(_separator); for (int i = 0; i < tmp.Length; ++i) inputStream.Enqueue(tmp[i]); return inputStream.Dequeue(); } } public string ReadLine { get { return _stream.ReadLine(); } } public int ReadInt { get { return int.Parse(Read); } } public long ReadLong { get { return long.Parse(Read); } } public double ReadDouble { get { return double.Parse(Read); } } public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; } public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; } public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; } } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = [int(x) for x in input().split()] count=0 for i in range(1,N): if sum(A[0:i])*sum(A[0:i+1]) >=0: count += abs(sum(A[0:i])+A[i])+1 if sum(A[0:i])<0: A[i]=-sum(A[0:i])+1 else: A[i]=-sum(A[0:i])-1 print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long LINF = 1LL << 60; const int INF = 1 << 30; int main() { long long n, l; cin >> n; vector<long long> a(n); cin >> a[0]; for (long long i = 1; i < n; i++) { cin >> l; a[i] = a[i - 1] + l; } long long tmp = 0; long long count = 0; for (long long i = 0; i < n - 1; i++) { if (i == 0 && a[i] <= 0) { count += abs(a[i] - 1); tmp -= a[i] - 1; } if ((a[i] + tmp) * (a[i + 1] + tmp) >= 0) { if ((a[i + 1] + tmp) <= 0) { count += abs(a[i + 1] + tmp - 1); tmp -= (a[i + 1] + tmp - 1); } else { count += abs(a[i] + tmp + 1); tmp -= (a[i] + tmp + 1); } } } if (a[n - 1] + tmp == 0) { count++; } long long ans = count; count = 0; tmp = 0; for (long long i = 0; i < n - 1; i++) { if (i == 0 && a[i] >= 0) { count += abs(1 + a[i]); tmp -= 1 + a[i]; } if ((a[i] + tmp) * (a[i + 1] + tmp) >= 0) { if ((a[i + 1] + tmp) <= 0) { count += abs(a[i + 1] + tmp - 1); tmp -= (a[i + 1] + tmp - 1); } else { count += abs(a[i] + tmp + 1); tmp -= (a[i] + tmp + 1); } } } if (a[n - 1] + tmp == 0) { count++; } ans = min(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
python3
n = int(input()) A = list(map(int, input().split())) cnt = 0 w = A[0] for i in range(n - 1): nw = w + A[i + 1] if w > 0: if nw >= 0: cnt += nw + 1 chg = -(nw + 1) elif w < 0: if nw <= 0: cnt += 1 - nw chg = 1 - nw w = nw + chg chg = 0 print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; long long a[100001]; long long c[100001]; long long s[100001]; long long solve(bool plus, long long ans) { if (plus == 1) { if (a[0] <= 0) { a[0] = 1; ans = 1; s[0] = 1; } else s[0] = a[0]; } else { if (a[0] >= 0) { a[0] = -1; ans = 1; s[0] = 1; } else s[0] = a[0]; } for (int i = 0; i < n - 1; i++) { long long t = s[i] + a[i + 1]; if (s[i] * t >= 0) { if (s[i] < 0) { long long b = a[i + 1]; s[i + 1] = 1; a[i + 1] = s[i + 1] - s[i]; ans += (a[i + 1] - b); } else if (s[i] > 0) { long long b = a[i + 1]; s[i + 1] = -1; a[i + 1] = s[i + 1] - s[i]; ans += (b - a[i + 1]); } } else { s[i + 1] = t; } } return ans; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; c[i] = a[i]; } long long ans1 = solve(1, 0); for (int i = 0; i < n; i++) { a[i] = c[i]; } long long ans2 = solve(0, 0); cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; 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; ll x, sum = 0, count = 0; cin >> n; for (int(i) = 0; (i) < (n); (i)++) { cin >> x; if (i > 0) { ll temp; if (sum < 0) { if (sum + x < 0) { temp = (sum + x) * (-1) + 1; x += temp; count += temp; } else if (sum + x == 0) { x++; count++; } } else { if (sum + x > 0) { temp = (sum + x) * (-1) - 1; x += temp; count += abs(temp); } else if (sum + x == 0) { x--; count++; } } } sum += x; } cout << count << 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; long long _set(long long N, long long pos) { return N = N | (1 << pos); } long long _reset(long long N, long long pos) { return N = N & ~(1 << pos); } bool _check(long long N, long long pos) { return (bool)(N & (1 << pos)); } bool _upper(char a) { return a >= 'A' && a <= 'Z'; } bool _lower(char a) { return a >= 'a' && a <= 'z'; } bool _digit(char a) { return a >= '0' && a <= '9'; } long long dx[] = {1, -1, 0, 0, -1, -1, 1, 1}; long long dy[] = {0, 0, 1, -1, -1, 1, -1, 1}; long long a[100010]; int main() { long long n; cin >> n; long long cnt = 0, ans = 0; for (int i = 0; i < n; i++) { cin >> a[i]; cnt += a[i]; if (i % 2) { if (cnt > 0) ans += cnt + 1, cnt = -1; } else { if (cnt < 0) ans -= cnt - 1, cnt = 1; } } long long a2 = 0; cnt = 0; for (int i = 0; i < n; i++) { cnt += a[i]; if (i % 2 == 0) { if (cnt > 0) a2 += cnt + 1, cnt = -1; } else { if (cnt < 0) a2 -= cnt - 1, cnt = 1; } } cout << min(ans, a2) << '\n'; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> a(N); for (int i = 0; i < N; i++) { cin >> a[i]; } int allsum = 0, sum = 0, count = 0; for (int i = 0; i < N; i++) { allsum += a[i]; } if (allsum == 0) { sum++; count++; } for (int i = 0; i < N - 1; i++) { sum += a[i]; if (a[i + 1] < 0 && sum < 0) { if (0 <= a[i + 1] - sum) { count += sum; sum *= -1; } else { count += a[i + 1]; a[i + 1] *= -1; } } else if (0 <= a[i + 1] && 0 <= sum) { if (0 <= a[i + 1] - sum) { count += sum; sum *= -1; } else { count += a[i + 1]; a[i + 1] *= -1; } } } cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long n; cin >> n; int i; long a[n], su, cnt; su = 0; cnt = 0; for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 0; i < n; i++) { su += a[i]; if (a[0] >= 0) { if (i % 2 == 0) { if (su <= 0) { cnt += 1 - su; su = 1; } } else { if (su >= 0) { cnt += su + 1; su = -1; } } } else { if (i % 2 == 0) { if (su >= 0) { cnt += su + 1; su = -1; } } else { if (su >= 0) { cnt += su + 1; su = -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 解(): iN = int(input()) aA = [int(_) for _ in input().split()] iL = len(aA) iStart = 0 if sum(aA[0::2]) < sum(aA[1::2]): iStart = 1 iC = 0 aD = [0]*iL if 0 % 2 == iStart : if aA[0] < 0: aA[0] = 1 iC += -1 * aA[0] + 1 else: if 0 < aA[0] : aA[0] = -1 iC += aA[0] + 1 aD[0] = aA[0] for i in range(1,iL): aD[i] = aD[i-1]+aA[i] if i % 2 == iStart: if aD[i] <= 0: iC += -1*aD[i] +1 aD[i] = 1 else: if aD[i] >= 0: iC += aD[i] +1 aD[i] = -1 print(iC) 解()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double PI = 3.1415926535897932384626433832795; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool isDiffer(long long a, long long b) { if (b == 0) return false; if (((a > 0) && (b < 0)) || ((a < 0) && (b > 0))) return true; else return false; } int main() { ios::sync_with_stdio(false); long long n; cin >> n; vector<long long> v[2]; for (int i = 0; i < n; i++) { long long t; cin >> t; v[0].push_back(t); v[1].push_back(t); } long long ans[2] = {0}; for (int j = 0; j < 2; j++) { long long ob = (j == 0) ? -1 : 1; if (isDiffer(ob, v[j][0])) { ans[j] += llabs(ob - v[j][0]); v[j][0] = ob; } long long os = v[j][0]; for (int i = 1; i < n; i++) { if (!isDiffer(os, v[j][i] + os)) { long long ob = (os >= 0) ? -1 : 1; ans[j] += llabs(ob - os - v[j][i]); v[j][i] = ob - os; } os += v[j][i]; } } cout << min(ans[0], ans[1]) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5, Inf = 0x3f3f3f3f; int a[N], n; long long sum[N]; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; int cost = 0, ans = Inf; for (int i = 1; i <= n; ++i) { sum[i] = sum[i - 1] + a[i]; if (i & 1) { if (sum[i] >= 0) cost += sum[i] + 1, sum[i] = -1; } else { if (sum[i] <= 0) cost += -sum[i] + 1, sum[i] = 1; } } ans = min(ans, cost); cost = 0; for (int i = 1; i <= n; ++i) { sum[i] = sum[i - 1] + a[i]; if (!(i & 1)) { if (sum[i] >= 0) cost += sum[i] + 1, sum[i] = -1; } else { if (sum[i] <= 0) cost += -sum[i] + 1, sum[i] = 1; } } ans = min(ans, cost); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) cumulative=[0] for i in range(n): cumulative.append(cumulative[i] + a[i]) cumulative.pop(0) #print(cumulative) def plus(): change = 0 cnt = 0 for i, val in enumerate(cumulative): #print(i, val+change, change) if i % 2 == 0: if val + change > 0: pass else: temp = change change += 1 - (val + change) cnt += abs(1 - (val + temp)) else: if val + change < 0: pass else: temp = change change += -1 - (val + change) cnt += abs(-1 - (val + temp)) #print(cnt) #print(change) return cnt def minus(): change = 0 cnt = 0 for i, val in enumerate(cumulative): #print(i, val+change, change) if i % 2 == 0: if val + change < 0: pass else: temp = change change += -1 - (val + change) cnt += abs(-1 - (val + temp)) #print('+', abs(-1 - (val + temp))) else: if val + change > 0: pass else: temp = change change += 1 - (val + change) cnt += abs(1 - (val + temp)) #print('+', abs(1 - (val + temp))) #print(cnt) #print(change) return cnt if a[0] > 0: print(plus()) elif a[0] < 0: print(minus()) else: print(min(plus(), minus()))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; public class Main{ public static void main(String[]args){ Scanner sc = new Scanner(System.in); int N = Integer.parseInt(sc.nextLine()); long ans = 0; long now = sc.nextInt(); for(int i = 1; i < N; i++){ int A = sc.nextInt(); if(now > 0 && now+A >= 0){ ans += now+A+1; now = -1; }else if(now < 0 && now+A <= 0){ ans += Math.abs(now+A)+1; now = 1; }else{ now = now+A; } } System.out.println(ans); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AtCoder { class Program { static void Main(string[] args) { int n = int.Parse(Console.ReadLine()); int[] a = new int[n]; int[] sum = new int[n]; string[] lines = Console.ReadLine().Split(' '); for (int i = 0; i < n; i++) { a[i] = int.Parse(lines[i]); } int ans = 0; int diff = 0; int sign = (a[0] == 0 ? 0 : (a[0] > 0 ? 1 : -1)); sum[0] = a[0]; for (int i = 1; i < n; i++) { if (sum[i-1] + a[i] > 0) { if (sign > 0) { diff = +(sum[i-1] + 1); ans += diff; sum[i] = sum[i-1] - diff; } else { sum[i] = a[i] + sum[i-1]; } } else if (sum[i-1] + a[i] < 0) { if (sign < 0) { diff = -(sum[i - 1] - 1); ans += diff; sum[i] = sum[i - 1] + diff; } else { sum[i] = a[i] + sum[i - 1]; } } else if (sum[i-1] + a[i] == 0) { if (sign > 0) { diff = +(sum[i - 1]); ans += diff; sum[i] = sum[i - 1] - diff; } else { diff = -(sum[i - 1] ); ans += diff; sum[i] = sum[i - 1] + diff; } } if (sign == 0) { sign = (a[i] > 0 ? 1 : -1); } else { sign = -sign; } } 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() { 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 - total0; } total0 = -total0; } long total1 = 0; long ops1 = 0; for (int i = 0; i < N; i++) { total1 += a[i]; if (total1 > -1) { total1 = -1; ops1 += (total1 + 1); } total1 = -total1; } 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
n = int(input()) a = list(map(int,input().split())) ans = 0 o = 0 for i in range(n): if i == 0: if a[i] == 0: f = "+" a[i] = 1 elif a[0] > 0: f = "+" elif a[0] < 0: f = "-" else: o += a[i-1] if f == "+": if a[i] + o > 0: c = -1 - o ans += abs(c - a[i]) a[i] = c f = "-" else: if a[i] + o == 0: a[i] -= 1 ans += 1 f = "-" elif f == "-": if a[i] + o < 0: c = 1 - o ans += abs(c - a[i]) a[i] = c f = "+" else: if a[i] + o == 0: a[i] += 1 ans += 1 f = "+" #print(a) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n, i, j, sum, res, sumprev; cin >> n; sum = res = 0; long long arr[n]; long long sumprevarr[n]; for (i = 0; i < n; i++) cin >> arr[i]; sum = res = 0; sumprev = 0; for (i = 0; i < n; i++) { sum += arr[i]; if ((sum == 0) && (sumprev > 0)) { arr[i]--; sum--; res++; } else if ((sum == 0) && (sumprev < 0)) { arr[i]++; sum++; res++; } else if ((sumprev > 0) && (sum > 0)) { long long d = sum - 0; arr[i] = arr[i] - d - 1; if ((d + 1) < sumprevarr[i - 1]) { sum = sum - d - 1; res = res + d + 1; } else { res = res + sumprevarr[i - 1]; } } else if ((sumprev < 0) && (sum < 0)) { long long d = 0 - sum; if ((d + 1) < sumprevarr[i - 1]) { arr[i] += (d + 1); sum = sum + d + 1; res = res + d + 1; } else { res = res + sumprevarr[i - 1]; } } sumprev = sum; sumprevarr[i] += (abs(sum) + 1); } cout << res; 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 cnt1 = 0; int acu1[n]; for (int i = 0; i < n; i++) { if (i < 1) acu1[i] = a[i]; else acu1[i] = a[i] + acu1[i - 1]; if (i & 1 && acu1[i] >= 0) { cnt1 += acu1[i] + 1; acu1[i] = -1; } else if (!(i & 1) && acu1[i] <= 0) { cnt1 += -acu1[i] + 1; acu1[i] = 1; } } int cnt2 = 0; int acu2[n]; for (int i = 0; i < n; i++) { if (i < 1) acu2[i] = a[i]; else acu2[i] = a[i] + acu2[i - 1]; if (!(i & 1) && acu2[i] >= 0) { cnt2 += acu2[i] + 1; acu2[i] = -1; } else if (i & 1 && acu2[i] <= 0) { cnt2 += -acu2[i] + 1; acu2[i] = 1; } } cout << (min(cnt1, cnt2)) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[n], b[n]; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; } long long sum = 0, cnt_first = 0, cnt_second = 0; for (int i = 0; i < n; i += 2) { while (sum + a[i] <= 0) { a[i]++; cnt_first++; } sum += a[i]; if (i != n - 1) { while (sum + a[i + 1] >= 0) { a[i + 1]--; cnt_first++; } sum += a[i + 1]; } } sum = 0; for (int i = 0; i < n; i += 2) { while (sum + b[i] >= 0) { b[i]--; cnt_second++; } sum += b[i]; if (i != n - 1) { while (sum + b[i + 1] <= 0) { b[i + 1]++; cnt_second++; } sum += b[i + 1]; } } cout << ((cnt_first < cnt_second) ? cnt_first : cnt_second) << 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
nums = int(input()) ary = list(map(int,input().split())) #puls no toki if ary[0] < 0 : ary[0] = ary[0] + ary[0] +1 elif ary[0] == 0: ary[0] += 1 sum = ary[0] count = 0 for i in range (1,nums): sum += ary[i] if i % 2 == 1: if sum > 0 : count += abs(sum)+1 sum = -1 elif sum == 0: sum = -1 count += 1 elif i % 2 == 0: if sum < 0: count += abs(sum)+1 sum = 1 elif sum == 0: sum = 1 count += 1 result_plus = count #minus no toki sum = ary[0] if ary[0] > 0: ary[0] = ary[0] - ary[0] -1 elif ary[0] == 0: ary[0] -= 1 count = 0 for i in range(1,nums): sum += ary[i] if i % 2 == 1: if sum < 0: count += abs(sum)+1 sum = 1 elif sum == 0: count += 1 sum = 1 if i % 2 == 0: if sum > 0: count += abs(sum)+1 sum = -1 elif sum == 0: count += 1 sum = -1 result_minus = count if result_plus > result_minus: print(result_minus) else: print(result_plus)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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 = 1e9 for ini in (A[0], -A[0]): acc = ini res = 0 for a in A[1:]: if acc + a == 0: acc = 1 if acc < 0 else -1 res += 1 elif (acc < 0) != (acc + a > 0): ope = -(acc + a + (1 if acc + a > 0 else -1)) acc = acc + a + ope res += abs(ope) else: acc += a ans = min(ans, res) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# # Written by NoKnowledgeGG @YlePhan # ('ω') # #import math #mod = 10**9+7 #import itertools #import fractions #import numpy as np #mod = 10**4 + 7 """def kiri(n,m): r_ = n / m if (r_ - (n // m)) > 0: return (n//m) + 1 else: return (n//m)""" """ n! mod m 階乗 mod = 1e9 + 7 N = 10000000 fac = [0] * N def ini(): fac[0] = 1 % mod for i in range(1,N): fac[i] = fac[i-1] * i % mod""" """mod = 1e9+7 N = 10000000 pw = [0] * N def ini(c): pw[0] = 1 % mod for i in range(1,N): pw[i] = pw[i-1] * c % mod""" """ def YEILD(): yield 'one' yield 'two' yield 'three' generator = YEILD() print(next(generator)) print(next(generator)) print(next(generator)) """ """def gcd_(a,b): if b == 0:#結局はc,0の最大公約数はcなのに return a return gcd_(a,a % b) # a = p * b + q""" """def extgcd(a,b,x,y): d = a if b!=0: d = extgcd(b,a%b,y,x) y -= (a//b) * x print(x,y) else: x = 1 y = 0 return d""" def readInts(): return list(map(int,input().split())) mod = 10**9 + 7 def main(): n = int(input()) A = readInts() Cost = 0 # 符号 positive? #po_ = True # 変わったか変わってないか if A[0] > 0: # if positive po_ = True elif A[0] == 0: # negative po_ = True A[0] += 1 Cost += 1 else: po_ = False ANS = [0] * (n+1) ANS[0] = A[0] for i in range(1,n): #print(ANS[i-1],po_,ANS[i-1] + A[i],Cost) if ANS[i-1]+A[i] > 0 and not po_: # sumがpositiveで前がnegativeだった po_ = True ANS[i] = ANS[i-1] + A[i] # これで終わり elif ANS[i-1]+A[i] > 0 and po_: # posi : posi ? # 負にしなければならない Cost += abs(-1 - (ANS[i-1]+A[i])) # 先にこれやれ A[i] += -1 - (ANS[i-1] + A[i]) # -4 ANS[i] = ANS[i-1] + A[i] po_ = False elif ANS[i-1]+A[i] < 0 and not po_: #nega : nega # -1 はここ # print(A[i]) Cost += abs(1 - (ANS[i-1]+A[i])) # 先にこれやれ A[i] += 1 - (ANS[i-1] + A[i]) ANS[i] = ANS[i-1] + A[i] po_ = True elif ANS[i-1]+A[i] == 0 and po_: # nega: pos po_ = False A[i] -= 1 Cost += 1 ANS[i] = ANS[i-1] + A[i] elif ANS[i-1]+A[i] < 0 and po_: po_ = False ANS[i] = ANS[i-1] + A[i] elif ANS[i-1]+A[i] == 0 and not po_: po_ = True A[i] += 1 Cost += 1 ANS[i] = ANS[i-1] + A[i] print(Cost) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) arr=list(map(int,input().split())) ans=0 lsum=arr[0] if arr[0]>0: for i in range(1,n): if i%2==1: if arr[i]<-lsum: lsum+=arr[i] else: ans+=(abs(lsum)+1)+arr[i] lsum=-1 else: if abs(lsum)<arr[i]: lsum+=arr[i] else: ans+=(abs(lsum)+1)-arr[i] lsum=1 elif arr[0]<0: for i in range(1,n): if i%2==0: if arr[i]<-lsum: lsum+=arr[i] else: ans+=(abs(lsum)+1)+arr[i] lsum=-1 else: if abs(lsum)<arr[i]: lsum+=arr[i] else: ans+=(abs(lsum)+1)-arr[i] lsum=1 else: ans1=1 ans2=1 lsum=1 for i in range(1,n): if i%2==1: if arr[i]<-lsum: lsum+=arr[i] else: ans1+=(abs(lsum)+1)+arr[i] lsum=-1 else: if abs(lsum)<arr[i]: lsum+=arr[i] else: ans1+=(abs(lsum)+1)-arr[i] lsum=1 lsum=-1 for i in range(1,n): if i%2==1: if arr[i]<-lsum: lsum+=arr[i] else: ans2+=(abs(lsum)+1)+arr[i] lsum=-1 else: if abs(lsum)<arr[i]: lsum+=arr[i] else: ans2+=(abs(lsum)+1)-arr[i] lsum=1 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; unsigned long long n, a; unsigned long long ans; unsigned long long bef; signed main() { cin >> n; cin >> a; bef = a; for (unsigned long long i = 1; i < n; i++) { cin >> a; if (bef > 0) { if (bef + a < 0) { bef += a; continue; } ans += bef + a + 1; bef = -1; } else { if (bef + a > 0) { bef += a; continue; } ans -= bef + a - 1; bef = 1; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(int,input().split())) if a[0]>0: P=0 SUMP=a[0] else: P=1-a[0] SUMP=1 for i in range(1,n): if i%2==0: if SUMP+a[i]>=1: P+=0 SUMP=SUMP+a[i] else: P+=1-(SUMP+a[i]) SUMP=1 elif i%2==1: if SUMP+a[i]<=(-1): P+=0 SUMP=SUMP+a[i] else: P+=SUMP+a[i]+1 SUMP=(-1) if a[0]<0: N=0 SUMN=a[0] else: N=1-a[0] SUMN=(-1) for i in range(1,n): if i%2==1: if SUMN+a[i]>=1: N+=0 SUMN=SUMN+a[i] else: N+=1-(SUMN+a[i]) SUMN=1 elif i%2==0: if SUMN+a[i]<=(-1): N+=0 SUMN=SUMN+a[i] else: N+=SUMN+a[i]+1 SUMN=(-1) print(min(P,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<long long> a(n), b(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n - 1; i++) a[i + 1] += a[i]; b = a; long long sum1, sum2; sum1 = sum2 = 0; int i = 0; while (1) { int t; if (a[i] <= 0) { t = -a[i] + 1; sum1 += t; for (int j = i; j < n; j++) a[j] += t; } i++; if (i == n) break; if (a[i] >= 0) { t = a[i] + 1; sum1 += t; for (int j = i; j < n; j++) a[j] -= t; } i++; if (i == n) break; } i = 0; while (1) { int t; if (b[i] >= 0) { t = b[i] + 1; sum2 += t; for (int j = i; j < n; j++) b[j] -= t; } i++; if (i == n) break; if (b[i] <= 0) { t = -b[i] + 1; sum2 += t; for (int j = i; j < n; j++) b[j] += t; } i++; if (i == n) break; } cout << (min(sum1, sum2)) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using vl = vector<ll>; using vvl = vector<vector<ll>>; using pl = pair<ll, ll>; using vpl = vector<pl>; using vvpl = vector<vpl>; const ll MOD = 1000000007; const ll MOD9 = 998244353; const int inf = 1e9 + 10; const ll INF = 4e18; const ll dy[8] = {1, 0, -1, 0, 1, 1, -1, -1}; const ll dx[8] = {0, -1, 0, 1, 1, -1, 1, -1}; template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main() { ll n; cin >> n; ll res = inf; ll ans = 0; ll tmp = 0; vl v(n); for (ll i = 0; i < n; i++) cin >> v[i]; for (ll i = 0; i < n; i++) { tmp += v[i]; if (i % 2 == 0) { if (tmp >= 0) { ans += tmp + 1; tmp = -1; } } else { if (tmp <= 0) { ans += 1 - tmp; tmp = 1; } } } chmin(res, ans); ans = 0; tmp = 0; for (ll i = 0; i < n; i++) { tmp += v[i]; if (i % 2 == 1) { if (tmp >= 0) { ans += tmp + 1; tmp = -1; } } else { if (tmp <= 0) { ans += 1 - tmp; tmp = 1; } } } chmin(res, ans); cout << res << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int posi(long long x) { if (x > 0LL) return 1; if (x < 0LL) return -1; return 0; } int main() { int N; cin >> N; vector<long long> a(N); for (auto &i : a) cin >> i; long long ans = 0, tmp = 0; long long sum = a[0]; for (int i = 1; i < N; i++) { if (posi(sum + a[i]) * posi(sum) != -1) { tmp += abs(sum + a[i]) + 1LL; sum = (sum > 0LL) ? -1LL : 1LL; } else sum += a[i]; } ans = tmp; tmp = abs(a[0]) + 1LL; sum = (a[0] > 0) ? -1LL : 1LL; for (int i = 1; i < N; i++) { if (posi(sum + a[i]) * posi(sum) != -1) { tmp += abs(sum + a[i]) + 1LL; sum = (sum > 0LL) ? -1LL : 1LL; } else sum += a[i]; } ans = min(ans, tmp); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const constexpr int INF = 1e9; int N; string s; void solve() { vector<int> v(N); long long minV = INF; for (int i = 0; i < N; ++i) cin >> v[i]; long long cnt = 0; int tmp = v[0]; if (v[0] < 0) { cnt += -v[0] + 1; v[0] = 1; } int sum = v[0]; v[0] = tmp; for (int i = 1; i < N; ++i) { sum += v[i]; if (i % 2 != 0 && sum > 0) { cnt += sum + 1; sum = -1; } if (i % 2 == 0 && sum < 0) { cnt += -sum + 1; sum = 1; } } minV = min(minV, cnt); cnt = 0; sum = 0; if (v[0] > 0) { cnt += v[0] + 1; v[0] = -1; } sum = v[0]; for (int i = 1; i < N; ++i) { sum += v[i]; if (i % 2 != 0 && sum < 0) { cnt += -sum + 1; sum = 1; } if (i % 2 == 0 && sum > 0) { cnt += sum + 1; sum = -1; } if (sum == 0) { if (i % 2 == 0) sum = 1; else sum = -1; cnt++; } } cout << min(minV, cnt) << endl; } int main() { cin >> N; 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 INF = 300000000; const long long MOD = 1000000007; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int n; cin >> n; long long a[100100]; for (int i = 0; i < n; ++i) { cin >> a[i]; } long long ans = INF; for (int i = 0; i < 2; ++i) { long long count = 0; int su = 0; for (int j = 0; j < n; ++j) { su += a[j]; if (i == 0) { if (j % 2 == 0 && su <= 0) { count += abs(-su + 1); su = 1; } else if (j % 2 == 1 && su >= 0) { count += abs(-su - 1); su = -1; } } if (i == 1) { if (j % 2 == 0 && su >= 0) { count += abs(-su + 1); su = -1; } else if (j % 2 == 1 && su <= 0) { count += abs(-su - 1); su = 1; } } } ans = min(ans, count); } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
package main import ( "bufio" "fmt" "os" "strings" "strconv" ) func main() { sc := bufio.NewScanner(os.Stdin) sc.Buffer(make([]byte, 64*1024*1024), 64*1024*1024) sc.Scan() n, _ := strconv.Atoi(sc.Text()) sc.Scan() aArr := strings.Split(sc.Text(), " ") a := make([]int, n) for i := 0; i < n; i++ { a[i], _ = strconv.Atoi(aArr[i]) } cnt := 0 sum := a[0] for i := 1; i < n; i++ { if (sum+a[i])*sum < 0 { sum += a[i] continue } else if sum == 0{ cnt += 1 }else if sum+a[i] < 0 { cnt += 1-(sum+a[i]) sum = 1 } else if sum+a[i] > 0{ cnt += 1+sum+a[i] sum = -1 } else { cnt += 1 if sum > 0 { sum = -1 } else { sum = 1 } } } fmt.Println(cnt) }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using LL = long long; template <typename T1, typename T2> using P = pair<T1, T2>; using Pii = P<int, int>; using Pdd = P<double, double>; template <typename T> using V = vector<T>; using Vi = V<int>; using Vll = V<LL>; using Vs = V<string>; template <typename T1, typename T2> using M = map<T1, T2>; using Mii = M<int, int>; using Msi = M<string, int>; const int MOD = 1000000007; const int INF = 1999999999; const LL INFLL = 999999999999999LL; const double EPS = 1e-10; const int DX[8] = {-1, 0, 1, 0, -1, -1, 1, 1}; const int DY[8] = {0, -1, 0, 1, -1, 1, 1, -1}; const double PI = 3.141592653589793; void SCAN(int *a) { scanf("%d", a); } void SCAN(int *a, int n) { for (int(i) = 0; (i) < (n); (i)++) { scanf("%d", &a[i]); } } void SCAN(Pii *a) { scanf("%d", &a->first); scanf("%d", &a->second); } void SCAN(LL *a) { scanf("%lld", a); } void SCAN(LL *a, int n) { for (int(i) = 0; (i) < (n); (i)++) { scanf("%lld", &a[i]); } } void SCAN(char *c) { scanf(" %c", c); } void SCAN(char *c, int n) { for (int(i) = 0; (i) < (n); (i)++) { scanf(" %c", &c[i]); } } void PRINT(int a) { printf("%d\n", a); } void PRINT(int *a, int s, char c = '\n') { for (int(i) = 0; (i) < (s); (i)++) { if (i == s - 1) { c = '\n'; } printf("%d%c", a[i], c); } } void PRINT(Vi a, char c = '\n') { for (int(i) = 0; (i) < (a.size()); (i)++) { if (i == a.size() - 1) { c = '\n'; } printf("%d%c", a[i], c); } } void PRINT(LL a) { printf("%lld\n", a); } void PRINT(LL *a, int s, char c = '\n') { for (int(i) = 0; (i) < (s); (i)++) { if (i == s - 1) { c = '\n'; } printf("%lld%c", a[i], c); } } void PRINT(double a) { printf("%.15f\n", a); } void PRINT(double *a, int s, char c = '\n') { for (int(i) = 0; (i) < (s); (i)++) { if (i == s - 1) { c = '\n'; } printf("%f%c", a[i], c); } } void PRINT(char a) { printf("%c\n", a); } void PRINT(string a) { printf("%s\n", a.c_str()); } template <typename A> void UNIQUE(vector<A> &a, int mode = 0) { if (mode == 0) { sort((a).begin(), (a).end(), greater<A>()); } else { sort((a).begin(), (a).end()); } a.erase(unique((a).begin(), (a).end()), a.end()); } template <typename A, size_t N, typename T> void FILL(A (&array)[N], const T &val) { fill((T *)array, (T *)(array + N), val); } template <typename T> int sgn(T val) { return (T(0) < val) - (val < T(0)); } long double pascalTri(int n, int r) { long double tri[n + 1][n + 1]; for (int(i) = 0; (i) < (n + 1); (i)++) { for (int(j) = 0; (j) < (n + 1); (j)++) { tri[i][j] = 0; } } for (int(i) = 0; (i) < (n + 1); (i)++) { for (int(j) = 0; (j) < (n + 1); (j)++) { if (j > i) { break; } if (j == 0 || j == i) { tri[i][j] = 1; } else { tri[i][j] = (tri[i - 1][j - 1] + tri[i - 1][j]); } } } return tri[n][r]; } LL GCD(LL a, LL b) { LL t; LL r; if (a < b) { t = a; a = b; b = t; } if (b == 0) { return a; } while (a % b != 0) { r = a % b; a = b; b = r; } return b; } LL LCM(LL a, LL b) { LL ab = (a * b) % MOD; return ab / GCD(a % b, b); } LL BMPow(int x, int n, int m = 0) { LL ans = 1; LL p = x; if (m == 0) { while (n > 0) { if (n & 1 == 1) { ans *= p; } p *= p; n >>= 1; } } else { while (n > 0) { if (n & 1 == 1) { ans = (ans * p) % m; } p = (p * p) % m; n >>= 1; } } return ans; } LL modInv(LL x, int m) { return BMPow(x, m - 2, m); } LL factorial(int x, int m = 0) { LL a = 1; if (m == 0) { for (int(i) = (x); (i) >= (1); (i)--) { a *= i; } } else { for (int(i) = (x); (i) >= (1); (i)--) { a = (a * i) % m; } } return a; } P<Vll, Vll> primeFactor(LL n) { Vll p, e; LL m = n; int c; for (LL i = 2; i * i <= n; i++) { if (m % i != 0) { continue; } c = 0; while (m % i == 0) { c++; m /= i; } p.push_back(i); e.push_back(c); } if (m > 1) { p.push_back(m); e.push_back(1); } return make_pair(p, e); } template <typename T> using coordinate = P<T, T>; template <typename T> using coordinateSet = V<coordinate<T>>; template <typename T> coordinate<double> centroidPolygon(coordinateSet<T> &a) { coordinate<double> G; G.first = 0.; G.second = 0.; int n = a.size(); for (auto &(i) : a) { G.first += i.first; G.second += i.second; } G.first /= n; G.second /= n; return G; } double area(coordinate<int> a, coordinate<int> b, coordinate<int> c) { return ((b.first - a.first) * (c.second - a.second) - (b.second - a.second) * (c.first - a.first)) / 2.; } int checkCross(coordinate<int> v1, coordinate<int> v2, coordinate<int> a, coordinate<int> b) { if (area(a, b, v1) * area(a, b, v2) >= 0) { return 0; } if (area(v1, v2, a) * area(v1, v2, b) >= 0) { return 0; } return 1; } struct edge { int src; int dst; int weight; edge() : src(0), dst(0), weight(0) {} edge(int s, int d, int w) : src(s), dst(d), weight(w) {} }; using edges = V<edge>; using graph = V<edges>; void add_edge(edges &g, int s, int d, int w = 1) { g.push_back(edge(s, d, w)); } void add_edge(graph &g, int s, int d, int w = 1) { g[s].push_back(edge(s, d, w)); } V<Vi> floyd(const graph &g) { int i, j, k; int n = g.size(); V<Vi> dist(n, Vi(n, INF / 2)); for (int(i) = 0; (i) < (n); (i)++) { dist[i][i] = 0; } for (int(i) = 0; (i) < (n); (i)++) { for (auto &e : g[i]) { dist[e.src][e.dst] = e.weight; } } for (int(k) = 0; (k) < (n); (k)++) { for (int(i) = 0; (i) < (n); (i)++) { for (int(j) = 0; (j) < (n); (j)++) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } return dist; } Vi dijkstra(graph &g, int s) { int i, j, k; int n = g.size(); int visit[n]; Vi dist(n); priority_queue<Pii, V<Pii>, greater<Pii>> q; for (int(i) = 0; (i) < (n); (i)++) { visit[i] = 0; dist[i] = INF / 2; } dist[s] = 0; q.push(make_pair(0, s)); int nv; int min_cost = 0; while (!q.empty()) { int d, t; tie(d, t) = q.top(); q.pop(); if (visit[t] == 1) { continue; } visit[t] = 1; dist[t] = d; for (auto &e : g[t]) { if (dist[e.dst] <= d + e.weight) { continue; } q.push(make_pair(d + e.weight, e.dst)); } } return dist; } template <typename T> struct binaryIndexedTree { private: int n; V<T> x; public: binaryIndexedTree(int num = 0) : n(num), x(n, 0) {} void add(int a, T w) { for (int i = a; i < n; i |= i + 1) { if (x[i] < w) { x[i] = w; } } } T maximum(int a) { T m = -1; for (int k = a - 1; k >= 0; k = (k & (k + 1)) - 1) { m = max(m, x[k]); } return m; } }; struct unionFind { private: Vi data; public: unionFind(int size) : data(size, -1) {} bool unionSet(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) { swap(x, y); } data[x] += data[y]; data[y] = x; } return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return ((data[x] < 0) ? x : (data[x] = root(data[x]))); } int size(int x) { return -data[root(x)]; } }; int sums[100005]; int main() { int i, j; int n; SCAN(&n); int a[n], b[n]; SCAN(a, n); for (int(i) = 0; (i) < (n); (i)++) { b[i] = a[i]; } FILL(sums, 0); sums[0] = a[0]; for (int(i) = (1); (i) < (n); (i)++) { sums[i] = (sums[i - 1] + a[i]); } int cnt1 = 0; int temp; if (a[0] == 0) { cnt1++; a[0]++; sums[0] = a[0]; for (int(i) = (1); (i) < (n); (i)++) { sums[i] = (sums[i - 1] + a[i]); } } for (int(i) = (1); (i) < (n); (i)++) { if (sums[i] * sums[i - 1] >= 0) { if (sums[i - 1] < 0) { temp = abs(sums[i] - 1); cnt1 += temp; a[i] += temp; } else { temp = abs(sums[i] + 1); cnt1 += temp; a[i] -= temp; } for (int(j) = (i); (j) < (n); (j)++) { sums[j] = (sums[j - 1] + a[j]); } } } int cnt2 = 0; if (b[0] > 0) { temp = abs(sums[i] + 1); cnt2 += temp; b[i] -= temp; } else if (b[0] < 0) { temp = abs(sums[i] - 1); cnt2 += temp; b[i] += temp; } else { cnt2++; b[0]--; } sums[0] = b[0]; for (int(i) = (1); (i) < (n); (i)++) { sums[i] = (sums[i - 1] + b[i]); } for (int(i) = (1); (i) < (n); (i)++) { if (sums[i] * sums[i - 1] >= 0) { if (sums[i - 1] < 0) { temp = abs(sums[i] - 1); cnt2 += temp; b[i] += temp; } else { temp = abs(sums[i] + 1); cnt2 += temp; b[i] -= temp; } for (int(j) = (i); (j) < (n); (j)++) { sums[j] = (sums[j - 1] + b[j]); } } } PRINT((cnt1 < cnt2) ? cnt1 : cnt2); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long dy[4] = {1, 0, -1, 0}; long long dx[4] = {0, 1, 0, -1}; bool check(long long a, long long b) { if ((a <= 0 && b > 0) || (a >= 0 && b < 0)) return true; return false; } int32_t main() { long long n; cin >> n; vector<long long> v(n); long long sum = 0, cnt = 0; for (long long i = 0; i < n; i++) { cin >> v[i]; long long t = sum; sum += v[i]; if (i > 0) { if (!check(sum, t)) { if (sum > 0) { cnt += (sum + 1); sum -= (sum + 1); } else if (sum < 0) { cnt += (1 - sum); sum += (1 - sum); } } else if (sum == 0) { if (t > 0) { sum--; } else { sum++; } cnt++; } } } cout << cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; if (scanf("%d", &n) < 1) return 0; long long int tmp; long long int sm = 0; long long int cnt = 0; for (int i = 0; i < n; i++) { if (scanf("%lld", &tmp) < 1) return 0; if ((0 <= sm + tmp) && (0 < sm)) { cnt = cnt + (1 + sm + tmp); sm = -1; } else if ((sm + tmp <= 0) && (sm < 0)) { cnt = cnt + (1 - sm - tmp); sm = 1; } else sm = sm + tmp; } printf("%lld\n", cnt); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# # Written by NoKnowledgeGG @YlePhan # ('ω') # #import math #mod = 10**9+7 #import itertools #import fractions #import numpy as np #mod = 10**4 + 7 """def kiri(n,m): r_ = n / m if (r_ - (n // m)) > 0: return (n//m) + 1 else: return (n//m)""" """ n! mod m 階乗 mod = 1e9 + 7 N = 10000000 fac = [0] * N def ini(): fac[0] = 1 % mod for i in range(1,N): fac[i] = fac[i-1] * i % mod""" """mod = 1e9+7 N = 10000000 pw = [0] * N def ini(c): pw[0] = 1 % mod for i in range(1,N): pw[i] = pw[i-1] * c % mod""" """ def YEILD(): yield 'one' yield 'two' yield 'three' generator = YEILD() print(next(generator)) print(next(generator)) print(next(generator)) """ """def gcd_(a,b): if b == 0:#結局はc,0の最大公約数はcなのに return a return gcd_(a,a % b) # a = p * b + q""" """def extgcd(a,b,x,y): d = a if b!=0: d = extgcd(b,a%b,y,x) y -= (a//b) * x print(x,y) else: x = 1 y = 0 return d""" def readInts(): return list(map(int,input().split())) mod = 10**9 + 7 def main(): n = int(input()) A = readInts() # 符号 positive? #po_ = True # 変わったか変わってないか if A[0] >= 0: # if positive po_ = True else: # negative po_ = False Cost = 0 ANS = [0] * (n+1) ANS[0] = A[0] for i in range(1,n): #print(ANS[i-1],po_,ANS[i-1] + A[i],Cost) if ANS[i-1]+A[i] > 0 and not po_: # sumがpositiveで前がnegativeだった po_ = True ANS[i] = ANS[i-1] + A[i] # これで終わり elif ANS[i-1]+A[i] > 0 and po_: # posi : posi ? # 負にしなければならない Cost += abs(-1 - (ANS[i-1]+A[i])) # 先にこれやれ A[i] += -1 - (ANS[i-1] + A[i]) # -4 ANS[i] = ANS[i-1] + A[i] po_ = False elif ANS[i-1]+A[i] < 0 and not po_: #nega : nega # -1 はここ # print(A[i]) Cost += abs(1 - (ANS[i-1]+A[i])) # 先にこれやれ A[i] += 1 - (ANS[i-1] + A[i]) ANS[i] = ANS[i-1] + A[i] po_ = True elif ANS[i-1]+A[i] == 0 and po_: # nega: pos po_ = False A[i] -= 1 Cost += 1 ANS[i] = ANS[i-1] + A[i] elif ANS[i-1]+A[i] < 0 and po_: po_ = False ANS[i] = ANS[i-1] + A[i] elif ANS[i-1]+A[i] == 0 and not po_: po_ = True A[i] += 1 Cost += 1 ANS[i] = ANS[i-1] + A[i] print(Cost) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxn = 200010; long long n, m, md, ans; long long a[maxn], pre[maxn]; ; long long read() { long long s = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { s = s * 10 + ch - '0'; ch = getchar(); } return s * f; } int main() { md = 0, ans = 0; memset(pre, 0, sizeof(pre)); n = read(); for (int i = 1; i <= n; i++) { a[i] = read(); pre[i] = a[i]; pre[i] += pre[i - 1]; } if (pre[1] != 0) { for (int i = 1; i < n; i++) { long long tmp = md; if (((pre[i] + tmp) * (pre[i + 1] + tmp) >= 0)) { if ((pre[i] + tmp) < 0) { md += (1ll - (pre[i + 1] + tmp)); ans += (1ll - (pre[i + 1] + tmp)); } else { md -= ((pre[i + 1] + tmp) + 1ll); ans += (1ll + (pre[i + 1] + tmp)); } } } } else { long long ans1 = 0, ans2 = 0; md = 0, ans = 0; pre[0] = -1; for (int i = 0; i < n; i++) { long long tmp = md; if (((pre[i] + tmp) * (pre[i + 1] + tmp) >= 0)) { if ((pre[i] + tmp) < 0) { md += (1ll - (pre[i + 1] + tmp)); ans1 += (1ll - (pre[i + 1] + tmp)); } else { md -= ((pre[i + 1] + tmp) + 1ll); ans1 += (1ll + (pre[i + 1] + tmp)); } } } md = 0, ans = 0; pre[0] = 1ll; for (int i = 0; i < n; i++) { int tmp = md; if (((pre[i] + tmp) * (pre[i + 1] + tmp) >= 0)) { if ((pre[i] + tmp) < 0) { md += (1ll - (pre[i + 1] + tmp)); ans2 += (1ll - (pre[i + 1] + tmp)); } else { md -= ((pre[i + 1] + tmp) + 1ll); ans2 += (1ll + (pre[i + 1] + tmp)); } } } ans = min(ans1, ans2); } printf("%lld\n", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < (int)(n); i++) { cin >> a[i]; } long long s = a[0]; long long ss = a[0]; long long ans = 0; for (int i = 1; i < n; i++) { s = ss; ss = s + a[i]; if (ss * s < 0) continue; if (ss == 0) { ans += 1; ss = -s / abs(s); } else { ans += abs(ss - (-ss / abs(ss))); ss = -ss / abs(ss); } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> a(n); for (int i = 0; i < (int)(n); i++) { cin >> a[i]; } vector<long long> copy_a = a; long long count = 0; long long sum = 0; if (a[0] != 0) { for (int i = 0; i < n - 1; i++) { sum += a[i]; if (sum < 0 && sum + a[i + 1] <= 0) { long long na = 1 - sum; count += abs(na - a[i + 1]); a[i + 1] = na; } if (sum > 0 && sum + a[i + 1] >= 0) { long long na = -1 - sum; count += abs(na - a[i + 1]); a[i + 1] = na; } } } else if (a[0] == 0) { a[0] = 1; long long count1 = 1; for (int i = 0; i < n - 1; i++) { sum += a[i]; if (sum < 0 && sum + a[i + 1] <= 0) { long long na = 1 - sum; count1 += abs(na - a[i + 1]); a[i + 1] = na; } if (sum > 0 && sum + a[i + 1] >= 0) { long long na = -1 - sum; count1 += abs(na - a[i + 1]); a[i + 1] = na; } } copy_a[0] = -1; long long count2 = 1; long long sum2 = 0; for (int i = 0; i < n - 1; i++) { sum2 += copy_a[i]; if (sum2 < 0 && sum2 + copy_a[i + 1] <= 0) { long long na = 1 - sum2; count2 += abs(na - copy_a[i + 1]); copy_a[i + 1] = na; } if (sum2 > 0 && sum2 + copy_a[i + 1] >= 0) { long long na = -1 - sum2; count2 += abs(na - copy_a[i + 1]); copy_a[i + 1] = na; } } count = min(count1, count2); } cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; map<int, int> mp; vector<int> V; list<int> L; stack<int> S; queue<int> Q; deque<int> dq; static const int MAX = 1e5; static const int NMAX = 50; static const int MMAX = 50; int N; long long a, cunt, sum; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> N; cin >> a; sum += a; for (int i = 1; i < N; i++) { cin >> a; if (sum * a >= 0) { cunt += sum + a + 1; sum = (sum > 0 ? -1 : 1); } else if (sum * a < 0 && abs(sum) >= abs(a)) { cunt += abs(sum) - abs(a) + 1; sum = (sum > 0 ? -1 : 1); } else sum += a; } cout << cunt << "\n"; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
structure LI = LargeInt structure SC = StringCvt structure T = TextIO fun nextInt () = valOf (T.scanStream (LI.scan SC.DEC) T.stdIn) fun solve ([] : LI.int list) _ cnt = cnt | solve (x :: xs) sum cnt = let val sum' = sum + x in if sum > 0 andalso sum' < 0 orelse sum < 0 andalso sum' > 0 then solve xs sum' cnt else if sum >= 0 andalso sum' >= 0 then solve xs ~1 (cnt + sum' + 1) else solve xs 1 (cnt - sum' + 1) end val () = let val n = LI.toInt (nextInt ()) val x1 = nextInt () val xs = List.tabulate (n - 1, fn _ => nextInt ()) val cnt = if x1 <> 0 then solve xs x1 0 else LI.min (solve xs 1 1, solve xs ~1 1) in print (LI.toString cnt ^ "\n") end
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import numpy as np n=int(input()) a=list(map(int,input().split())) r=[0] for i in range(n): r.append(r[i]+a[i]) r.pop(0) pm=[1-2*(i%2) for i in range(n)] mp=[1-2*((i+1)%2) for i in range(n)] sum1,sum2=0,0 sousa1,sousa2=0,0 for i in range(n): if np.sign(r[i]+sousa1) != pm[i]: sum1+=abs(pm[i]-r[i]-sousa1) sousa1+=1-2*(i%2)-r[i] for i in range(n): if np.sign(r[i]+sousa2) != mp[i]: sum2+=abs(mp[i]-r[i]-sousa2) sousa2+=1-2*((i+1)%2)-r[i] print(min(sum1,sum2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<long long int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; vector<long long int> B(N); B[0] = A[0]; for (int i = 1; i < N; i++) B[i] = B[i - 1] + A[i]; long long int ans = 0; long long int base = 0; for (int i = 1; i < N; i++) { if ((B[i] + base) * (B[i - 1] + base) > 0) { if (B[i] + base > 0) { if (B[i] + base > B[i - 1] + base) { ans += abs(B[i - 1] + base) + 1; base -= abs(B[i - 1] + base) + 1; } else { ans += abs(B[i] + base) + 1; base -= abs(B[i] + base) + 1; } continue; } else if (B[i] + base < 0) { if (B[i] + base < B[i - 1] + base) { ans += abs(B[i - 1] + base) + 1; base += abs(B[i - 1] + base) + 1; } else { ans += abs(B[i] + base) + 1; base += abs(B[i] + base) + 1; } continue; } } if (B[i - 1] + base == 0) { if (B[i] + base > 0) { ans += 1; base -= 1; continue; } else if (B[i] + base < 0) { ans += 1; base += 1; continue; } } if (i == N - 1 && B[i] + base == 0) ans++; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < (n); i++) cin >> a[i]; int cnt = 0; int s = 0; for (int i = 1; i < n; i++) { s += a[i - 1]; int t = 0, u; if (s > 0) { u = (-1) * s - 1; if (u < a[i]) { t = a[i] - u; a[i] = u; } } else { u = (-1) * s + 1; if (u > a[i]) { t = u - a[i]; a[i] = u; } } cnt += t; } cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double PI = 3.1415926535897932384626433832795; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool isDiffer(long long a, long long b) { if (b == 0) return false; if (((a > 0) && (b < 0)) || ((a < 0) && (b > 0))) return true; else return false; } int main() { ios::sync_with_stdio(false); long long n; cin >> n; vector<long long> v; for (int i = 0; i < n; i++) { long long t; cin >> t; v.push_back(t); } long long ans = 0; if (v[0] == 0) { v[0] = 1; ans += 1; } long long os = v[0]; for (int i = 1; i < n; i++) { if (!isDiffer(os, v[i] + os)) { long long ob = (os >= 0) ? -1 : 1; ans += abs(ob - os - v[i]); v[i] = ob - os; } os += v[i]; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (auto& e : a) cin >> e; int can1 = 0, can2 = 0; for (int i = 0, acc1 = 0, acc2 = 0; i < n; i++) { acc1 += a[i], acc2 += a[i]; if (i % 2 == 0) { if (acc1 <= 0) { can1 += -acc1 + 1; acc1 = 1; } if (acc2 >= 0) { can2 += acc2 + 1; acc2 = -1; } } else { if (acc1 >= 0) { can1 += acc1 + 1; acc1 = -1; } if (acc2 <= 0) { can2 += -acc2 + 1; acc2 = 1; } } } cout << min(can1, can2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } long long sum = a.at(0); long long op = 0; for (int j = 1; j < n; j++) { if (sum > 0) { sum += a.at(j); while (sum >= 0) { op++; sum--; } } else { sum += a.at(j); while (sum <= 0) { op++; sum++; } } } cout << op << 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())) count=0 if abs(A[1]-A[0])!=0: tmp=A[1]+A[0] else: count+=abs(-1-(A[1]+A[0])) tmp=-1 for i in range(2,n): if tmp<0: if tmp+A[i]<=0: count+= abs(1-(A[i]+tmp)) tmp=1 else: tmp+=A[i] continue else: if tmp+A[i]>=0: count+=abs(-1-(A[i]+tmp)) tmp=-1 else: tmp+=A[i] continue print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); long[] a = new long[N]; long[] aNegative = new long[N]; for (int i = 0; i < N; i++) { a[i] = sc.nextLong(); aNegative[i] -= a[i]; } long cnt = Math.min(cntCal(a), cntCal(aNegative)); System.out.println(cnt); } public static long cntCal(long[] a){ long sum = 0; int cnt = 0; for (int i = 0; i < a.length; i++) { long tmpSum = sum + a[i]; if (sum > 0) { while (tmpSum >= 0) { tmpSum = sum + --a[i]; cnt++; } } else { while (tmpSum <= 0) { tmpSum = sum + ++a[i]; cnt++; } } sum = sum + a[i]; } return cnt; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
n = gets.to_i as = gets.chomp.split.map(&:to_i) ans_o = ans_e = 0 sum_o = sum_e = as[0] 1.upto(n-1) do |i| sum_e += as[i] sum_o += as[i] if i.even? until sum_e > 0 ans_e += 1 sum_e += 1 end until sum_o < 0 ans_o += 1 sum_o -= 1 end else until sum_e < 0 ans_e += 1 sum_e -= 1 end until sum_o > 0 ans_o += 1 sum_o += 1 end end end puts [ans_e,ans_o].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; int calc(vector<int>& t, bool topPlus) { int sum = 0; int cnt = 0; for (int i = 0; i < t.size(); ++i) { sum += t.at(i); if ((i % 2 == 0 && topPlus == true) || (i % 2 == 1 && topPlus == false)) { if (sum > 0) continue; cnt += abs(sum) + 1; sum = 1; } else { if (sum < 0) continue; cnt += abs(sum) + 1; sum = -1; } } return cnt; } int main() { int N; cin >> N; vector<int> t(N); for (long unsigned i = 0; i < N; ++i) { cin >> t.at(i); } int cnt1 = calc(t, true); int cnt2 = calc(t, false); int cnt = min(cnt1, cnt2); 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, count = 0; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; int su = A[0]; bool plus = A[0] > 0; for (int i = 1; i < N; i++) { plus = !plus; su += A[i]; if (plus) { if (su <= 0) { count += abs(su) + 1; su = 1; } } else { if (su >= 0) { count += abs(su) + 1; su = -1; } } } cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) now_a = a[0] count = 0 ''' True = positive False = negative ''' # sign = True # if now_a < 0: # sign = False # # for i in range(1, n): # next_a = now_a + a[i] # if sign: # if next_a >= 0: # count += next_a + 1 # now_a = -1 # else: # now_a = next_a # sign = False # else: # if next_a <= 0: # count += abs(next_a) + 1 # now_a = 1 # else: # now_a = next_a # sign = True # print(count) sa = a[0] sign = True if sa < 0: sign = False for i in range(1, n): na = sum(a[:i+1]) if sign: if na >= 0: a[i] += -1 * (na + 1) count += na + 1 sign = False elif not sign: if na <= 0: a[i] += 1 - na count += 1 - na sign = True print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N_MAX = 100000; int N; int a[N_MAX]; int All; int sum[N_MAX]; int sum2[N_MAX]; int dif = 0; int main() { All = 0; cin >> N; for (int i = 0; i < N; i++) { cin >> a[i]; All += a[i]; sum[i] = All; sum2[i] = All; } int sigh = 1; int ans1 = 0; dif = 0; for (int i = 0; i < N; i++) { if (sum[i] + dif == 0) { dif += sigh; ans1 += 1; } else if (((sum[i] + dif) / abs((sum[i] + dif))) != sigh) { ans1 += (abs(sum[i] + dif) + 1); int temp = sum[i] + dif; dif += (abs(temp) + 1) * sigh; } sigh *= -1; } sigh = -1; int ans2 = 0; dif = 0; for (int i = 0; i < N; i++) { if (sum2[i] + dif == 0) { dif += sigh; ans2 += 1; } else if (((sum2[i] + dif) / abs((sum2[i] + dif))) != sigh) { ans2 += (abs(sum2[i] + dif) + 1); int temp = sum2[i] + dif; dif += (abs(temp) + 1) * sigh; } sigh *= -1; } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import math n = int(input()) a = list(map(int, input().split())) cnt = 0 x = [] x.append(a[0]) if a[0] == 0: cnt += 1 if a[1] > 0: x[0] = -1 else: x[0] = 1 for i in range(n-1): if x[i] > 0: if x[i]+a[i+1] > -1: tmp = int(math.fabs(x[i] + a[i+1]) + 1) a[i+1] = a[i+1] - tmp cnt += tmp x += [x[i] + a[i+1]] else: if x[i]+a[i+1] < +1: tmp = int(math.fabs(x[i] + a[i+1]) + 1) a[i+1] = a[i+1] + tmp cnt += tmp x += [x[i] + a[i+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
java
import java.util.Scanner; public class Main { public static void main(String[] args) { long[] a = null; try(Scanner sc = new Scanner(System.in)){ // N入力 aスペース区切り入力 a = new long[sc.nextInt()]; for(int i = 0; i < a.length; i ++) { a[i] = sc.nextLong(); } } long start = a[0]; boolean isNaturalNum = true; if(start < 0) { isNaturalNum = false; } long ret = 0L; for(int i = 1; i < a.length; i++) { long temp2 = start + a[i]; if(isNaturalNum) { if(temp2 >= 0) { ret += Math.abs(start + a[i]) + 1 ; start = -1; } else { //OK start = temp2; } isNaturalNum = false; } else { if(temp2 <= 0) { ret += Math.abs(start + a[i]) + 1; start = 1; } else { start = temp2; } isNaturalNum = true; } } System.out.println(ret); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < (n); ++i) cin >> a[i]; long long now = (a[0] == 0) ? 1LL : a[0]; long long tmp_ans1 = (a[0] == 0) ? abs(a[0]) + 1LL : 0; for (int i = 1; i < n; ++i) { if (now >= 0 && now + a[i] >= 0) { tmp_ans1 += now + a[i] + 1LL; now = -1LL; } else if (now < 0 && now + a[i] <= 0) { tmp_ans1 += abs(now + a[i]) + 1LL; now = 1LL; } else { now += a[i]; } } now = (a[0] < 0) ? 1LL : -1LL; long long tmp_ans2 = abs(a[0]) + 1LL; for (int i = 1; i <= n; ++i) { if (now >= 0 && now + a[i] >= 0) { tmp_ans2 += now + a[i] + 1LL; now = -1LL; } else if (now < 0 && now + a[i] <= 0) { tmp_ans2 += abs(now + a[i]) + 1LL; now = 1LL; } else { now += a[i]; } } cout << min(tmp_ans1, tmp_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
python3
n = int(input()) a = list(map(int, input().split())) ruisekiwa = [] tmp = 0 for x in a: tmp += x ruisekiwa.append(tmp) ans = tmp = cnt = 0 for x, y in zip(ruisekiwa, ruisekiwa[1:]): x += tmp y += tmp if (x == abs(x) and y != abs(y)) or (x != abs(x) and y == abs(y)) and y != 0: cnt += 1 else: hoge = 0 if y > 0: hoge += -1-y elif y < 0: hoge += 1-y else: if x > 0: hoge = -1 else: hoge = 1 tmp += hoge ans += abs(hoge) if len(ruisekiwa) - 1 == cnt: print('0') else: 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; signed main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; long long sum = 0; long long ans = 0; for (int i = 0; i < n; i++) { int x; cin >> x; if (i == 0) { sum += x; continue; } long long tmp = sum; sum += x; if (tmp * sum < 0) continue; ans += 1 + abs(sum); if (tmp < 0) sum = 1; else sum = -1; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; int a[100001]; int sumo[100001]; int sume[100001]; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i + 1]; } int anso = 0; int anse = 0; sumo[0] = 0; sume[0] = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 0) { if (sume[i - 1] + a[i] > 0) sume[i] = sume[i - 1] + a[i]; if (sume[i - 1] + a[i] <= 0) { sume[i] = 1; anse += (1 - (sume[i - 1] + a[i])); } if (sumo[i - 1] + a[i] < 0) sumo[i] = sumo[i - 1] + a[i]; if (sumo[i - 1] + a[i] >= 0) { sumo[i] = -1; anso += sumo[i - 1] + a[i] + 1; } } else if (i % 2 == 1) { if (sumo[i - 1] + a[i] > 0) sumo[i] = sumo[i - 1] + a[i]; if (sumo[i - 1] + a[i] <= 0) { sumo[i] = 1; anso += (1 - (sumo[i - 1] + a[i])); } if (sume[i - 1] + a[i] < 0) sume[i] = sume[i - 1] + a[i]; if (sume[i - 1] + a[i] >= 0) { sume[i] = -1; anse += (sume[i - 1] + a[i] + 1); } } } int ans; ans = min(anso, anse); 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; using ll = long long; template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main() { int n; cin >> n; vector<ll> a(n); for (int i = (0); i < (n); ++i) cin >> a[i]; ll sum = a[0]; ll ans = 0; for (int i = (1); i < (n); ++i) { sum += a[i]; if (sum * (sum - a[i]) < 0) ; else { if (sum == 0) { if (sum - a[i] < 0) { sum = 1; ans++; } else { ans++; sum = -1; } } else if (sum > 0) { ans += (sum - (-1)); sum = -1; } else { ans += (1 - sum); sum = 1; } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) *a, = map(int, input().split()) if a[0]: ans = 0 e0 = 1 if a[0] > 0 else -1 S = a[0] for i in range(1, n): e = e0 * int((i % 2-0.5)*2) n = max(e*(S+a[i])+1, 0) S += a[i]+(-e*n) ans += n print(ans) else: ans = [0, 0] for e0 in [1, -1]: a[0] = e0*1 S = a[0] for i in range(1, n): e = e0 * int((i % 2-0.5)*2) n = max(e*(S+a[i])+1, 0) S += a[i]+(-e*n) ans[int(e0/2+0.5)] += n print(min(ans))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; long[] s1 = new long[n]; long[] s2 = new long[n]; int sum1 = 0; int sum2 = 0; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } s1[0] = a[0]; for (int i = 1; i < n; i++) { s1[i] = s1[i-1] + a[i]; } s2 = s1.clone(); int count = 0; // - + - + となる場合 while (s1[0] >= 0) { s1[0]--; count++; sum1++; } for (int i = 1; i < n; i++) { s1[i] -= count; } for (int i = 1; i < n; i++) { count = 0; // iが奇数の場合は正にする if (i % 2 != 0) { while (s1[i-1]*s1[i] >= 0) { s1[i]++; count++; sum1++; } for (int j = i+1; j < n; j++) { s1[j] += count; } } // iが偶数の場合は負にする else { while (s1[i-1]*s1[i] >= 0) { s1[i]--; count++; sum1++; } for (int j = i+1; j < n; j++) { s1[j] -= count; } } } count = 0; // + - + - となる場合 while (s2[0] <= 0) { s2[0]++; count++; sum2++; } for (int i = 1; i < n; i++) { s2[i] += count; } for (int i = 1; i < n; i++) { count = 0; if (i % 2 != 0) { while (s2[i-1]*s2[i] >= 0) { s2[i]--; count++; sum2++; } for (int j = i+1; j < n; j++) { s2[j] -= count; } } else { while (s2[i-1]*s2[i] >= 0) { s2[i]++; count++; sum2++; } for (int j = i+1; j < n; j++) { s2[j] += count; } } } System.out.println(Math.min(sum1, sum2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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())) b=0 count=0 x=0 if A[0]>0: x=-1 else: x=1 for a in A: b+=a if x==1: if b<0: x=-1 else: count+=1+b b=-1 x=-1 else: if b>0: x=1 else: count+=1-b b=1 x=1 print(count if b!=0 else count+1)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] nums = new long[n]; for(int i = 0; i < n; i++){ nums[i] = sc.nextLong(); } long result = 0; long sum = 0; // + - + - for(int i = 0; i < n; i++){ if(i % 2 == 0 && sum + nums[i] <= 0){ result += Math.abs(1 - (sum + nums[i])); sum = 1; } else if(i % 2 == 1 && sum + nums[i] >= 0){ result += Math.abs(-1 - (sum + nums[i])); sum = -1; } else{ sum += nums[i]; } } int result2 = 0; sum = 0; // - + - + for(int i = 0; i < n; i++){ if(i % 2 == 1 && sum + nums[i] <= 0){ result2 += Math.abs(1 - (sum + nums[i])); sum = 1; } else if(i % 2 == 0 && sum + nums[i] >= 0){ result2 += Math.abs(-1 - (sum + nums[i])); sum = -1; } else{ sum += nums[i]; } } System.out.println(Math.min(result, result2)); sc.close(); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> int abs(int x) { if (x < 0) return -x; else return x; } int main(void) { int n; int a[100000]; int i, j; int sum = 0; int ans = 0; char sign; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &a[i]); for (i = 0; i < n; i++) { if (a[i] > 0) { sign = 'p'; break; } else if (a[i] < 0) { sign = 'm'; break; } else continue; } for (; i < n; i++) { sum += a[i]; if (sum == 0) { for (j = i + 1; j < n; j++) { if (sum == a[j]) continue; else if (sign == 'p') { sum++; ans++; i = j; break; } else if (sign == 'm') { sum--; ans++; i = j; break; } } i = j; ans++; } else if (sum > 0 && sign == 'p') sign = 'm'; else if (sum < 0 && sign == 'm') sign = 'p'; else { if (sign == 'p') { ans += abs(sum) + 1; sum += abs(sum) + 1; sign = 'm'; } else if (sign == 'm') { ans += abs(sum) + 1; sum -= abs(sum) + 1; sign = 'p'; } } } printf("%d\n", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INTMAX = 2147483647; const int64_t LLMAX = 9223372036854775807; const int MOD = 1000000007; template <class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } inline void swap(int64_t& a, int64_t& b) { a ^= b; b ^= a; a ^= b; } inline void swap(int& a, int& b) { a ^= b; b ^= a; a ^= b; } int main() { int n; int64_t ans = 0; int64_t s, last, a; cin >> n; cin >> a; s = a; for (int i{1}; i < (int)(n); i++) { cin >> a; if ((s ^ (s + a)) >= 0 || !(s + a)) { if (s < 0) { ans += 1LL - (s + a); s = 1; } else { ans += abs((-1LL) - (s + a)); s = -1; } } else { s += a; } } 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()) arr = [int(x) for x in input().split()] def exec(sign): a = [x for x in arr] res = 0 if a[0] == 0: a[0] = 1 res += 1 x = 0 for i in range(n-1): x += a[i] tmp = sign - (x + a[i+1]) if sign < 0: tmp = min(tmp, 0) else: tmp = max(tmp, 0) res += abs(tmp) a[i+1] += tmp sign *= (-1) return res print(min(exec(1), exec(-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[i]; int ans = 0; int sum = a[0]; for (int i = 1; i < n; i++) { int sum_t = sum + a[i]; if (sum > 0 && sum_t >= 0) { while (sum_t >= 0) { a[i]--; sum_t--; ans++; } } else if (sum < 0 && sum_t <= 0) { while (sum_t <= 0) { a[i]++; sum_t++; ans++; } } 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
python3
n = int(input()) al = list(map(int, input().split())) m = n//2 mm = n % 2 temp = al[0] res = 0 def ddd(temp,al,m,mm,res): if temp > 0 and mm ==1: for i in range(1,m+1): temp +=al[i*2-1] if temp <0: pass else: res += temp+1 temp = -1 temp +=al[i*2] if temp >0: pass else: res +=1-temp temp = 1 return(res) if temp > 0 and mm ==0: for i in range(1,m): temp +=al[i*2-1] if temp <0: pass else: res += temp+1 temp = -1 temp +=al[i*2] if temp >0: pass else: res +=1-temp temp = 1 temp += al[n-1] if temp <0: pass else: res += temp+1 temp = -1 return(res) if temp < 0 and mm ==1: for i in range(1,m+1): temp +=al[i*2-1] if temp >0: pass else: res += 1-temp temp = 1 temp +=al[i*2] if temp <0: pass else: res +=temp+1 temp = -1 return(res) if temp < 0 and mm ==0: for i in range(1,m): temp +=al[i*2-1] if temp >0: pass else: res += 1-temp temp = 1 temp +=al[i*2] if temp <0: pass else: res +=temp+1 temp = -1 temp += al[n-1] if temp >0: pass else: res += 1-temp temp = 1 return(res) print(ddd(temp,al,m,mm,res))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) import numpy as np na = np.array(a).cumsum() cnt = 0 hoge = 0 if(na[0] > 0): for i in range(n): delta = abs(na[i]) + 1 na[i] += hoge if(i % 2 == 0 and na[i] <= 0): cnt = cnt + delta hoge += delta elif(i % 2 == 1 and na[i] >= 0): cnt = cnt + delta hoge -= delta else: na[i] else: for i in range(n): delta = abs(na[i]) + 1 na[i] += hoge if(i % 2 == 1 and na[i] <= 0): cnt = cnt + delta hoge += delta elif(i % 2 == 0 and na[i] >= 0): cnt = cnt + delta hoge -= delta else: na[i] print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long> a(n); long long wa = 0; int now_sign = 0; int pre_sign = 0; long long count = 0; long long min_count = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } pre_sign = 1; wa = a[0]; if (wa != 0) now_sign = wa / abs(wa); else now_sign = 0; if (now_sign != pre_sign) { count += abs(a[0]) + 1; } for (int i = 1; i < n; i++) { wa += a[i]; if (wa != 0) now_sign = wa / abs(wa); else now_sign = 0; if (now_sign == pre_sign || now_sign == 0) { count += abs(wa) + 1; wa = -1 * pre_sign; now_sign = -1 * pre_sign; } pre_sign = now_sign; } min_count = count; count = 0; pre_sign = -1; wa = a[0]; if (wa != 0) now_sign = wa / abs(wa); else now_sign = 0; if (now_sign != pre_sign) { count += abs(a[0]) + 1; } for (int i = 1; i < n; i++) { wa += a[i]; if (wa != 0) now_sign = wa / abs(wa); else now_sign = 0; if (now_sign == pre_sign || now_sign == 0) { count += abs(wa) + 1; wa = -1 * pre_sign; now_sign = -1 * pre_sign; } pre_sign = now_sign; } min_count = min(min_count, count); cout << min_count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> template <class T, class S> void cmin(T &a, const S &b) { if (a > b) a = b; } template <class T, class S> void cmax(T &a, const S &b) { if (a < b) a = b; } using namespace std; signed main() { long long int n; cin >> n; vector<long long int> v(n); bool flag = false; for (long long int i = 0; i < n; i++) cin >> v[i]; vector<long long int> sum(n); long long int ans = 0; for (long long int i = 0; i < n; i++) sum[i] = v[i]; for (long long int i = 0; i < n; i++) { if (!i) { if (sum[0] > 0) flag = true; else if (sum[0] < 0) flag = false; else { sum[0] = 1; ans++; flag = true; } continue; } sum[i] += sum[i - 1]; if (flag) { if (sum[i] < 0) flag = false; else { ans += (abs(sum[i]) + 1); sum[i] = -1; flag = false; } } else { if (sum[i] > 0) flag = true; else { ans += (abs(sum[i]) + 1); sum[i] = 1; flag = true; } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; const int INF = 1000000007; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int64_t N; cin >> N; int64_t A[N], sum[N] = {}; cin >> A[0]; sum[0] = A[0]; for(int i=1;i<N;i++){ cin >> A[i]; sum[i] = sum[i-1] + A[i]; } int64_t num = INF; for(int i=1;i<N;i++){ if(sum[i-1]*sum[i] >= 0){ num = i; break; } } if(num == INF){ cout << 0 << endl; return 0; } int64_t ans1 = 0, ans2 = 0; int64_t sumt = 1; int64_t sa = 0; for(int i=0;i<N;i++){ sumt *= -1; sum[i] += sa; if(sumt*sum[i] <= 0){ sa += sumt-sum[i]; ans1 += abs(sumt-sum[i]); } } sumt = -1; sa = 0; for(int i=0;i<N;i++){ sumt *= -1; sum[i] += sa; if(sumt*sum[i] <= 0){ sa += sumt-sum[i]; ans2 += abs(sumt-sum[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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n + 1); for (int i = 1; i <= n; i++) cin >> a[i]; vector<int> a1; a1 = a; int sum1 = 0, ans1 = 0; for (int i = 1; i <= n; i++) { sum1 += a1[i]; if (i % 2 == 1 && sum1 <= 0) { int plus = 1 - sum1; sum1 = 1; ans1 += plus; continue; } if (i % 2 == 0 && sum1 >= 0) { int minus = 1 + sum1; sum1 = -1; ans1 += minus; } } vector<int> a2; a2 = a; int sum2 = 0, ans2 = 0; for (int i = 1; i <= n; i++) { sum2 += a2[i]; if (i % 2 == 1 && sum2 >= 0) { int minus = 1 + sum2; sum2 = -1; ans2 += minus; continue; } else if (i % 2 == 0 && sum2 <= 0) { int plus = 1 - sum2; sum2 = 1; ans2 += plus; } } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> long long power(long long a, long long b) { long long r = 1; for (long long i = 0; i < (b); i++) { r *= a; } return r; } int main(void) { long long N, a[100100], ans = 0; scanf("%lld", &N); for (long long i = 0; i < (N); i++) scanf("%lld", a + i); for (long long i = 1; i < N; i++) { a[i] = a[i] + a[i - 1]; if (a[i] == 0) { if (a[i - 1] < 0) a[i]++; else a[i]--; ans++; } else if (a[i - 1] * a[i] > 0) { ans += ((a[i]) >= (0) ? (a[i]) : (-(a[i]))) + 1; if (a[i - 1] < 0) a[i] = 1; else a[i] = -1; } } printf("%lld\n", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<long long int> A(N); long long int s = 0LL; vector<long long int> V(N); for (int i = 0; i < N; i++) { cin >> A[i]; s += A[i]; V[i] = s; } long long int tmp1 = 0LL; long long int tmp2 = 0LL; for (int i = 0; i < N - 1; i++) { if (i % 2 == 0) { if (V[i] > 0) continue; else { tmp1 += abs(1LL - V[i]); V[i] = 1LL; V[i + 1] = 1LL + A[i + 1]; } } else { if (V[i] < 0) continue; else { tmp1 += abs(-1LL - V[i]); V[i] = -1LL; V[i + 1] = -1LL + A[i + 1]; } } } for (int i = 0; i < N - 1; i++) { if (i % 2 == 1) { if (V[i] > 0) continue; else { tmp2 += abs(1LL - V[i]); V[i] = 1LL; V[i + 1] = 1LL + A[i + 1]; } } else { if (V[i] < 0) continue; else { tmp2 += abs(-1LL - V[i]); V[i] = -1LL; V[i + 1] = -1LL + A[i + 1]; } } } cout << min(tmp1, tmp2); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static int32_t n; static vector<int32_t> a; static int32_t calcCost(bool isPlus) { int32_t sum = 0, cost = 0; for (int32_t i = 0; i < n; i++) { if (isPlus && sum + a[i] < 1) { cost += abs(a[i] - (1 - sum)); sum += 1 - sum; } else if (!isPlus && sum + a[i] > -1) { cost += abs(a[i] - (-1 - sum)); sum += -1 - sum; } else { sum += a[i]; } isPlus = !isPlus; } return cost; } int main() { cin >> n; a.resize(n); for (int32_t i = 0; i < n; i++) cin >> a[i]; cout << min(calcCost(true), calcCost(false)) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int signs[2] = {-1, 1}, cnt[2] = {0, 0}; for (int i = 0; i < 2; i++) { long long sum = 0; int sign = signs[i]; for (int j = 0; j < n; j++) { sum += a[j]; if (sum == 0) { sum += sign; cnt[i]++; } else if (sum * sign < 0) { cnt[i] = cnt[i] + abs(sum) + 1; sum = sum + sum * (-1) + sign; } sign *= -1; } } cout << (cnt[0] < cnt[1] ? cnt[0] : cnt[1]) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a; for (int i = 0; i < n; i++) { int ai; cin >> ai; a.push_back(ai); } int count = 0; if (a.at(0) == 0) { a.at(0) = 1; count = 1; } for (int i = 0; i < n - 1; i++) { int sum = accumulate(a.begin(), a.begin() + i + 1, 0); int sum_next = accumulate(a.begin(), a.begin() + i + 2, 0); if (sum > 0 && sum_next >= 0) { int diff = sum_next + 1; count += diff; a.at(i + 1) -= diff; } else if (sum < 0 && sum_next <= 0) { int diff = -sum_next + 1; count += diff; a.at(i + 1) += diff; } } cout << count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# sys.stdin.readline import sys input = sys.stdin.readline class AtCoder: def main(self): n = int(input()) a = list(map(int, input().split())) even_plus_ans = 0 before = a[0] if a[0] < 0: even_plus_ans -= a[0] - 1 before = 1 for i in range(1, n): if i % 2 == 0: v = a[i] + before if v > 0: before = v elif v <= 0: before = 1 even_plus_ans -= v - 1 else: v = a[i] + before if v < 0: before = v elif v >= 0: before = -1 even_plus_ans += v + 1 odd_plus_ans = 0 before = a[0] if a[0] >= 0: odd_plus_ans += a[0] + 1 before = -1 for i in range(1, n): if i % 2 != 0: v = a[i] + before if v > 0: before = v elif v <= 0: before = 1 odd_plus_ans -= v - 1 else: v = a[i] + before if v < 0: before = v elif v >= 0: before = -1 odd_plus_ans += v + 1 print(min(even_plus_ans, odd_plus_ans)) # Run main if __name__ == '__main__': AtCoder().main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools from collections import deque sys.setrecursionlimit(10**7) inf = 10**20 mod = 10**9 + 7 DR = [1, -1, 0, 0] DC = [0, 0, 1, -1] def LI(): return [int(x) for x in sys.stdin.readline().split()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def main(): N = I() A = LI() cumsum = [0] for i in range(N): cumsum.append(A[i] + cumsum[i]) cumsum = cumsum[1:] prev_plus = cumsum[0] > 0 cnt = 0 total_sum = 0 for i, c in enumerate(cumsum): if i == 0: continue c = c + total_sum if prev_plus: # if minus, ignore if c < 0: pass else: cnt += (c - (-1)) total_sum -= (c - (-1)) else: if c > 0: pass else: cnt += (1 - c) total_sum += (1 - c) prev_plus = not prev_plus print(cnt) main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def solve(): ans = 0 N = int(input()) A = list(map(int, input().split())) total = A[0] if total==0: for i in range(1,N): if A[i]!=0: total = pow(-1,i)*A[i]//abs(A[i]) break else: return N for i in range(1,N): new_total = total+A[i] if total*(new_total)>=0: new_total = (-1)*total//abs(total) ans += abs(new_total-(total+A[i])) total = new_total return ans print(solve())
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; /** * https://abc059.contest.atcoder.jp/tasks/arc072_a */ public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = Integer.parseInt(sc.next()); long[] a = new long[N]; for(int i=0; i<N; i++) a[i] = sc.nextLong(); sc.close(); long sum = a[0]; long ans = 0; for(int i=1; i<N; i++){ if(sum>0 && sum+a[i]>=0){ ans += Math.abs(-1-sum-a[i]); sum = -1; }else if(sum<0 && sum+a[i]<=0){ ans += Math.abs(1-sum-a[i]); sum = 1; }else{ sum = sum + a[i]; } } System.out.println(ans); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <algorithm> #include <iostream> #include <iomanip> #include <cstring> #include <cstdlib> #include <utility> #include <cstdio> #include <vector> #include <string> #include <queue> #include <stack> #include <cmath> #include <set> #include <map> using ll = long long; using itn = int; using namespace std; int GCD(int a, int b){ return b ? GCD(b, a%b) : a; } int main() { int n; cin >> n; ll a[n]; for(int i=0; i<n; i++){ cin >> a[i]; } ll asum[n+1]={}; for(int i=0; i<n; i++){ asum[i+1] = asum[i]+a[i]; } ll cnt=0; ll accSum=0; for(int i=1; i<n; i++){ asum[i+1]+=accSum; if(asum[i+1]*asum[i]>0){ ll s=abs(asum[i+1])+1; cnt+=s; asum[i+1]<0 ? accSum+=s : accSum+=-1*s; asum[i+1]<0 ? asum[i+1]=1 : asum[i+1]=-1; }else if(asum[i+1]*asum[i]==0){ cnt+=1; asum[i]<0 ? asum[i+1]=1,accSum+=1 : asum[i+1]=-1,accSum+=-1; } } cout << cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; long long a[100005], dp[100005]; cin >> n; long long sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; dp[i] = sum; } long long diff = 0, ans = 0; if (dp[0] == 0) { if (dp[1] < 0) diff++, ans++; else diff--, ans++; } for (int i = 1; i < n; i++) { if (dp[i] + diff == 0) { if (dp[i - 1] + diff < 0) diff++, ans++; if (dp[i - 1] + diff > 0) diff--, ans++; continue; } if ((dp[i - 1] + diff) / llabs(dp[i - 1] + diff) == (dp[i] + diff) / llabs(dp[i] + diff)) { if (dp[i] + diff >= 0) { ans += llabs(dp[i] + diff) + 1; diff -= llabs(dp[i] + diff) + 1; } else { ans += llabs(dp[i] + diff) + 1; diff += llabs(dp[i] + diff) + 1; } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=[int(i) for i in input().split()] check=a[0] ans=0 if check==0: for i in range(1,n): if check+a[i]==0: continue elif check+a[i]>0: check=-1 ans=1 break else: check=1 ans=1 break for i in range(1,n): check2=check+a[i] if check<0: if check2>0: check=check2 else: ans+=(abs(check2)+1) check=1 else: if check2<0: check=check2 else: ans+=(abs(check2)+1) check=-1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import numpy as np n = int(input()) a = list(map(int, input().split())) s = [] ss = a[0] cnt = 0 c = 1 if(a[0] != 0): s.append(a[0]) else: while (a[c] == 0): c += 1 cnt += 2 cnt -= 1 if(np.sign(a[c]) == 1): ss = -1 for i in range(c): if(i%2 != c%2): s.append(-1) else: s.append(1) else: ss = 1 for i in range(c): if(i%2 != c%2): s.append(1) else: s.append(-1) for i in range(c, n): ss += a[i] if(ss == 0): if(np.sign(s[i-1]) == -1): ss += 1 cnt += 1 s.append(1) else: ss -= 1 cnt += 1 s.append(-1) elif(np.sign(s[i-1]) == np.sign(ss)): if(np.sign(s[i-1]) == -1): cnt += 1 - ss ss = 1 s.append(1) else: cnt += abs(-1-ss) ss = -1 s.append(-1) else: s.append(ss) print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long a[1000000]; int min(long long a, long long b) { int t = a; if (b <= t) t = b; return t; } int main() { int n; cin >> n; for (int t = 0; t < n; t++) cin >> a[t]; int sum = 0; long long x = 0; for (int t = 0; t < n; t++) { sum += a[t]; if (t % 2 == 1 && sum >= 0) { long long s = sum + 1; sum = -1; x += s; } else if (t % 2 == 0 && sum <= 0) { long long s = 1 - sum; sum = 1; x += s; } } long long positive_x = x; x = 0; sum = 0; for (int t = 0; t < n; t++) { sum += a[t]; if (t % 2 == 0 && sum >= 0) { long long s = sum + 1; sum = -1; x += s; } else if (t % 2 == 1 && sum <= 0) { long long s = 1 - sum; sum = 1; x += s; } } long long negative_x = x; long long result = min(positive_x, negative_x); cout << result << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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.at(i); } long long sum = a.at(0); long long op_1 = 0; for (int j = 1; j < n; j++) { if (sum > 0) { sum += a.at(j); if (sum >= 0) { op_1 += (sum + 1); sum = -1; } } else { sum += a.at(j); if (sum <= 0) { op_1 += (-1 * sum + 1); sum = 1; } } } sum = a.at(0); long long op_2 = 0; if (sum > 0) { sum = -1; op_2 += (sum + 1); } else { sum = 1; op_2 += (sum * -1 + 1); } for (int j = 1; j < n; j++) { if (sum > 0) { sum += a.at(j); if (sum >= 0) { op_2 += sum + 1; sum = -1; } } else { sum += a.at(j); if (sum <= 0) { op_2 += -1 * sum + 1; sum = 1; } } } cout << (op_1 > op_2 ? op_2 : op_1) << endl; }