Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long> a(n); for (int i = (0); i < (int)(n); i++) cin >> a[i]; long long sum = a[0]; bool f; long ans = 0; if (a[0] > 0) f = 1; else f = 0; for (int i = (1); i < (int)(n); i++) { if (a[i] == 0) { if (f) a[i] = -1; else a[i] = 1; ans++; } sum += a[i]; if (f) { if (sum >= 0) { ans += abs(-1 - sum); a[i] -= abs(-1 - sum); sum = -1; } f = 0; } else { if (sum <= 0) { ans += abs(1 - sum); a[i] += abs(1 - sum); sum = 1; } f = 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 N; long long a[100001]; int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> a[i]; } long long ans = 0; long long ans2 = 0; long long R = a[0]; if (a[0] <= 0) { ans = 1 - a[0]; R = 1; } for (int i = 1; i < N; i++) { if (R < 0) { R += a[i]; if (R <= 0) { ans += 1 - R; R = 1; } } else { R += a[i]; if (R >= 0) { ans += R + 1; R = -1; } } } R = a[0]; if (a[0] >= 0) { ans2 = a[0] - 1; R = -1; } for (int i = 1; i < N; i++) { if (R < 0) { R += a[i]; if (R <= 0) { ans2 += 1 - R; R = 1; } } else { R += a[i]; if (R >= 0) { ans2 += R + 1; R = -1; } } } long long ans3 = min(ans, ans2); cout << ans3 << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) ans=10**13 for i in [1,-1]: b=0 c=0 for j in a: b+=j if b*i<=0: c+=abs(b-i) b=i i*=-1 ans=min(ans,c) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#!/usr/bin/env ruby # n = STDIN.gets.to_i a = STDIN.gets.split(' ').map{|x| x.to_i} s_pos = Array.new(n,0) s_neg = Array.new(n,0) output = 0 output_n = 0 output_p = 0 if a[0] < 0 output_p += a[0].abs+1 output_n += 0 s_pos[0] = 1 s_neg[0] = a[0] else output_n += a[0].abs+1 output_p += 0 s_neg[0] = -1 s_pos[0] = a[0] end s = s_neg 1.upto(n-1) do |i| s[i] = s[i-1] + a[i] if s[i] == 0 change = 1 s[i] = (s[i-1] > 0) ? -1 : 1 else if (s[i-1] > 0) ^ (s[i] > 0) change = 0 else change = s[i].abs+1 s[i] = (s[i-1] > 0) ? -1: 1 end end output += change end output_n = output output = 0 s = s_pos 1.upto(n-1) do |i| s[i] = s[i-1] + a[i] if s[i] == 0 change = 1 s[i] = (s[i-1] > 0) ? -1 : 1 else if (s[i-1] > 0) ^ (s[i] > 0) change = 0 else change = s[i].abs+1 s[i] = (s[i-1] > 0) ? -1: 1 end end output += change end output_p = output puts [output_p,output_n].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; const long MOD = 1e9 + 7; const long LINF = 1e13; const long LLINF = 1e18; template <class T> void Swap(T& r, T& l) { T tmp = r; r = l; l = tmp; } int main() { long n; cin >> n; vector<long> a(n); vector<long> accum(n, 0); for (int i = 0; i < n; ++i) { cin >> a[i]; accum[i] = a[i]; if (i > 0) accum[i] += accum[i - 1]; } vector<long> accumtmp(n, 0); copy(accum.begin(), accum.end(), accumtmp.begin()); for (int i = 0; i < n; ++i) { cerr << accum[i] << endl; } long ans = 0; long count = 0; for (int i = 1; i < n; ++i) { if (i % 2 == 1) { if (accumtmp[i] >= 0) { cerr << "a[i] " << a[i] << endl; long tmpc = -(-1 - accumtmp[i]); count += tmpc; for (int k = i; k < n; ++k) { accumtmp[k] += -tmpc; } accumtmp[i] = -1; } } else { if (accumtmp[i] <= 0) { cerr << "a[i] " << a[i] << endl; long tmpc = 1 - accumtmp[i]; count += tmpc; for (int k = i; k < n; ++k) { accumtmp[k] += tmpc; } } } } for (int i = 0; i < n; ++i) { cerr << accumtmp[i] << endl; } ans = count; cerr << "count:" << count << endl; count = 0; cerr << endl; for (int i = 0; i < n; ++i) { cerr << accum[i] << endl; } copy(accum.begin(), accum.end(), accumtmp.begin()); for (int i = 1; i < n; ++i) { if (i % 2 == 0) { if (accumtmp[i] >= 0) { cerr << "a[i] " << a[i] << endl; long tmpc = -(-1 - accumtmp[i]); count += tmpc; for (int k = i; k < n; ++k) { accumtmp[k] += -tmpc; } accumtmp[i] = -1; } } else { if (accumtmp[i] <= 0) { cerr << "a[i] " << a[i] << endl; long tmpc = 1 - accumtmp[i]; count += tmpc; for (int k = i; k < n; ++k) { accumtmp[k] += tmpc; } } } } for (int i = 0; i < n; ++i) { cerr << accumtmp[i] << endl; } cerr << "count:" << count << endl; ans = min(ans, count); cout << ans; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int, input().split())) B = A[:] # evenがプラスの時 sum_even = 0 ans_even = 0 for i in range(N): sum_even += A[i] if i % 2 == 0: if sum_even <= 0: ans_even += abs(sum_even) + 1 sum_even += abs(sum_even) + 1 else: if sum_even >= 0: ans_even += abs(sum_even) + 1 sum_even += abs(sum_even) + 1 # oddがプラスの時 sum_odd = 0 ans_odd = 0 for i in range(N): sum_odd += A[i] if i % 2 == 1: if sum_odd <= 0: ans_odd += abs(sum_odd) + 1 sum_odd += abs(sum_odd) + 1 else: if sum_odd >= 0: ans_odd += abs(sum_odd) + 1 sum_odd += abs(sum_odd) + 1 answer = min(ans_even, ans_odd) print(str(answer))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MX = 1e5 + 5, INF = 5 << 28, MOD = 1e9 + 7; long long N; vector<long long> A; void input() { cin >> N; A.resize(N); for (long long i = (long long)(0); i <= (long long)(N - 1); ++i) { cin >> A[i]; } } void solve() { long long ans = INF; long long fugo = 1; for (long long fg = (long long)(0); fg <= (long long)(1); ++fg) { if (fg == 0) { fugo = 1; } else fugo = 0; long long prev = 0; long long s = 0; long long ans1 = 0; for (long long i = (long long)(0); i <= (long long)(N - 1); ++i) { s += A[i]; if (fugo) { if (s > 0) { ans1 += 0; } else if (s == 0) { ans1 += 1; s += 1; } else { ans1 += abs(s) + 1; s += abs(s) + 1; } } else { if (s > 0) { ans1 += (abs(s) + 1); s -= (abs(s) + 1); } else if (s == 0) { ans1 += 1; s -= 1; } else { ans1 += 0; } } prev = s; fugo ^= 1; } ans = min(ans1, ans); } cout << ans << endl; } signed main() { input(); solve(); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int sum; long input; cin >> sum; int ans = 0; int reigai = 0; int ans1, ans2, sum1, sum2; if (sum == 0) { ans1 = 1; ans2 = 1; sum1 = 1; sum2 = -1; reigai = 1; } for (int i = 1; i < n; ++i) { cin >> input; if (reigai == 0) { if (sum * (sum + input) >= 0) { ans += abs(sum + input) + 1; if (sum < 0) sum = 1; else sum = -1; } else sum += input; } else { if (sum1 * (sum1 + input) >= 0) { ans1 += abs(sum1 + input) + 1; if (sum1 < 0) sum1 = 1; else sum1 = -1; } else sum1 += input; if (sum2 * (sum2 + input) >= 0) { ans2 += abs(sum2 + input) + 1; if (sum2 < 0) sum2 = 1; else sum2 = -1; } else sum2 += input; } } if (reigai == 0) cout << ans << endl; else 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; signed main() { cin.tie(0); ios::sync_with_stdio(false); int64_t n; cin >> n; vector<int64_t> a(n); for (int64_t i = 0; i < n; i++) cin >> a[i]; int64_t sum1 = 0, cost1 = 0; for (int64_t i = 0; i < n; i++) { sum1 += a[i]; int64_t diff = abs(sum1) + 1; if (i % 2 == 0 && sum1 < 0) { sum1 += diff; cost1 += diff; } if (i % 2 == 1 && sum1 > 0) { sum1 -= diff; cost1 += diff; } if (sum1 == 0) { if (i % 2 == 0) { sum1--; cost1++; } if (i % 2 == 1) { sum1++; cost1++; } } } int64_t sum2 = 0, cost2 = 0; for (int64_t i = 0; i < n; i++) { sum2 += a[i]; int64_t diff = abs(sum2) + 1; if (i % 2 == 0 && sum2 > 0) { sum2 -= diff; cost2 += diff; } if (i % 2 == 1 && sum2 < 0) { sum2 += diff; cost2 += diff; } if (sum2 == 0) { if (i % 2 == 0) { sum2++; cost2++; } if (i % 2 == 1) { sum2--; cost2++; } } } cout << min(cost1, cost2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long int; template <class T> ostream &operator<<(ostream &os, vector<T> &v) { for (auto i = v.begin(); i != v.end(); i++) { os << *i << " "; } return os; } ll gcd(ll a, ll b) { ll tmp; if (b > a) { tmp = a; a = b; b = tmp; } while (a % b != 0) { tmp = b; b = a % b; a = tmp; } return b; } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } int main(void) { ll n; vector<ll> v; cin >> n; for (int i = 0; i < n; i++) { ll x; cin >> x; v.push_back(x); } ll cnt = 0; if (v[0] == 0) { if (v[1] >= 0) { v[0] = -1; } else if (v[1] < 0) { v[0] = 1; } cnt++; } ll sum = v[0]; ll prev = v[0]; for (int i = 1; i < n; i++) { sum += v[i]; if (prev >= 0 and sum >= 0) { cnt += abs(-1 - sum); sum += -1 - sum; } else if (prev <= 0 and sum <= 0) { cnt += abs(1 - sum); sum += abs(1 - sum); } prev = sum; } std::cout << cnt << std::endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MaxN = 1e5; bool flag, ok; long long sum, ans, anv; int n; int a[MaxN + 5], b[MaxN + 5]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); b[i] = a[i]; } sum = a[1]; if (a[1] < 0) flag = 1, ok = 1; if (a[1] != 0) { for (int i = 2; i <= n; i++) { if (flag == 1) { if (sum + a[i] <= 0) { long long ant = sum + a[i]; int t = a[i]; a[i] = 1 - sum; ans += (a[i] - t); sum += a[i]; } else sum += a[i]; flag = 0; } else { if (sum + a[i] >= 0) { long long ant = sum + a[i]; int t = a[i]; a[i] = -1 - sum; ans += (t - a[i]); sum += a[i]; } else sum += a[i]; flag = 1; } } } if (a[1] == 0) ans = 1 << 30; int tr = b[1]; if (ok) b[1] = 1, flag = 0; else b[1] = -1, flag = 1; anv += (abs(b[1] - tr)); sum = b[1]; for (int i = 2; i <= n; i++) { if (flag == 1) { if (sum + b[i] <= 0) { long long ant = sum + b[i]; int t = b[i]; b[i] = 1 - sum; anv += (b[i] - t); sum += b[i]; } else sum += b[i]; flag = 0; } else { if (sum + b[i] >= 0) { long long ant = sum + b[i]; int t = b[i]; b[i] = -1 - sum; anv += (t - b[i]); sum += b[i]; } else sum += b[i]; flag = 1; } } printf("%lld\n", min(ans, anv)); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
.
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import numpy as np N = int(input()) a_s = input().split() for i in range(N): a_s[i] = int(a_s[i]) a_s = np.array(a_s) def get_sign(x): if x>0: return +1 elif x<0: return -1 else: return 0 ans = 0 S0 = 0 for i,a in enumerate(a_s): if i==0: S = a if S == 0: ans += 1 if np.all(a_s[1:])==0: S = +1 else: for i in range(1,N): if a_s[i]!=0: S = get_sign(a_s[i])*((-1)**i) break else: S = S0 + a if (get_sign(S0) == get_sign(S))|(get_sign(S)==0): if abs(S0)==1: ans += 2 + abs(a) a = get_sign(S0)*(-2) S = S0 + a else: ans += 1 + abs(a) a = get_sign(S0)*(-1) S = S0 + a else: pass S0 = S 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
# -*- coding: utf-8 -*- import bisect import heapq import math import random import sys from collections import Counter, defaultdict, deque from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal from functools import lru_cache, reduce from itertools import combinations, combinations_with_replacement, product, permutations from operator import add, mul, sub sys.setrecursionlimit(100000) input = sys.stdin.readline INF = 2**62-1 def read_int(): return int(input()) def read_int_n(): return list(map(int, input().split())) def read_float(): return float(input()) def read_float_n(): return list(map(float, input().split())) def read_str(): return input().strip() def read_str_n(): return list(map(str, input().split())) def error_print(*args): print(*args, file=sys.stderr) def mt(f): import time def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() error_print(e - s, 'sec') return ret return wrap @mt def slv(N, A): S = [A[0]] for a in A[1:]: S.append(S[-1]+a) sig = -1 if S[0] > 0 else 1 o = 0 ans = 0 for s in S: s += o if s == 0: ans += 1 o -= sig elif sig * s > 0: ans += abs(s) + 1 o -= sig*(abs(s) + 1) sig *= -1 return ans def main(): N = read_int() A = read_int_n() print(slv(N, A)) 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()) a = list(map(int, input().split())) count = 0 sum_ = 0 for i in range(n): if sum_ * (sum_+a[i]) <0 or i == 0: sum_ += a[i] continue elif sum_ > 0: count += sum_+a[i]+1 a[i] = -sum_-1 elif sum_ < 0: count += abs(sum_+a[i])+1 a[i] = -sum_+1 sum_ += a[i] print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long linf = 1001002003004005006ll; const int inf = 1001001001; const int mod = 1000000007; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<long long> a(n); for (int i = 0; i < (n); ++i) cin >> a[i]; long long tot = 0; long long res1 = 0; { for (int i = 0; i < (n); ++i) { if (i % 2 == 0) { if (tot + a[i] > 0) tot += a[i]; else { res1 += abs(tot + a[i]); tot = 1; } } else { if (tot + a[i] < 0) tot += a[i]; else { res1 += abs(tot + a[i]) + 1; tot = -1; } } } } tot = 0; long long res2 = 0; { for (int i = 0; i < (n); ++i) { if (i % 2 != 0) { if (tot + a[i] > 0) tot += a[i]; else { res2 += abs(tot + a[i]) + 1; tot = 1; } } else { if (tot + a[i] < 0) tot += a[i]; else { res2 += abs(tot + a[i]) + 1; tot = -1; } } } } long long ans = min(res1, res2); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int N = 1000000; int main() { int n; cin >> n; vector<int> a(n); for (long long int i = 0; i < n; i++) cin >> a[i]; int ans_plus = 0; vector<int> sum_plus(n, 0); if (a[0] > 0) { sum_plus[0] = a[0]; } else { ans_plus += 1 - a[0]; sum_plus[0] = 1; } for (int i = 1; i < n; ++i) { sum_plus[i] = sum_plus[i - 1] + a[i]; if (sum_plus[i] * sum_plus[i - 1] > 0) { if (sum_plus[i - 1] > 0) { ans_plus += 1 + sum_plus[i]; sum_plus[i] = -1; } else if (sum_plus[i - 1] < 0) { ans_plus += 1 - sum_plus[i]; sum_plus[i] = 1; } } else if (sum_plus[i] == 0) { if (sum_plus[i - 1] > 0) { sum_plus[i] = -1; } else if (sum_plus[i - 1] < 0) { sum_plus[i] = 1; } ans_plus++; } } int ans_minus = 0; vector<int> sum_minus(n); if (a[0] < 0) { sum_minus[0] = a[0]; } else { ans_minus += 1 + a[0]; sum_minus[0] = -1; } for (int i = 1; i < n; ++i) { sum_minus[i] = sum_minus[i - 1] + a[i]; if (sum_minus[i] * sum_minus[i - 1] > 0) { if (sum_minus[i - 1] > 0) { ans_minus += 1 + sum_minus[i]; sum_minus[i] = -1; } else if (sum_minus[i - 1] < 0) { ans_minus += 1 - sum_minus[i]; sum_minus[i] = 1; } } else if (sum_minus[i] == 0) { if (sum_minus[i - 1] > 0) { sum_minus[i] = -1; } else if (sum_minus[i - 1] < 0) { sum_minus[i] = 1; } ans_minus++; } } std::cout << min(ans_minus, ans_plus) << std::endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using vvvll = vector<vvll>; using vb = vector<bool>; using vvb = vector<vb>; using mii = map<int, int>; using pqls = priority_queue<long long>; using pqlg = priority_queue<long long, vector<long long>, greater<long long>>; using mll = map<long long, long long>; using pll = pair<long long, long long>; using sll = set<long long>; long long divup(long long a, long long b); long long kaijou(long long i); long long P(long long n, long long k); long long C(long long n, long long k); long long GCD(long long a, long long b); long long LCM(long long a, long long b); bool prime(long long N); double distance(vector<long long> p, vector<long long> q, long long n); void press(vector<long long> &v); void ranking(vector<long long> &v); void erase(vector<long long> &v, long long i); void unique(vector<long long> &v); void printv(vector<long long> v); vector<ll> keta(ll x); long long modpow(long long a, long long n, long long mod); long long modinv(long long a, long long mod); vector<long long> inputv(long long n); vector<long long> yakusuu(int n); map<long long, long long> soinsuu(long long n); vector<vector<long long>> maze(long long i, long long j, vector<string> &s); vector<long long> eratos(long long n); set<long long> eraset(long long n); long long divup(long long a, long long b) { long long x = abs(a); long long y = abs(b); long long z = (x + y - 1) / y; if ((a < 0 && b > 0) || (a > 0 && b < 0)) return -z; else if (a == 0) return 0; else return z; } long long kaijou(long long i) { if (i == 0) return 1; long long j = 1; for (long long k = 1; k <= i; k++) { j *= k; } return j; } long long P(long long n, long long k) { if (n < k) return 0; long long y = 1; for (long long i = 0; i < k; i++) { y *= (n - i); } return y; } long long C(long long n, long long k) { if (n < k) return 0; return P(n, k) / kaijou(k); } long long GCD(long long a, long long b) { if (a < b) swap(a, b); long long d = a % b; if (d == 0) { return b; } return GCD(b, d); } long long LCM(long long a, long long b) { return (a / GCD(a, b)) * b; } bool prime(long long N) { if (N == 1) { return false; } if (N < 0) return false; long long p = sqrt(N); for (long long i = 2; i <= p; i++) { if (N % i == 0) { return false; } } return true; } double distance(vector<long long> p, vector<long long> q, long long n) { double x = 0; for (long long i = 0; i < n; i++) { x += pow((p.at(i) - q.at(i)), 2); } return sqrt(x); } void press(vector<long long> &v) { long long n = v.size(); vector<long long> w(n); map<long long, long long> m; for (auto &p : v) { m[p] = 0; } long long i = 0; for (auto &p : m) { p.second = i; i++; } for (long long i = 0; i < n; i++) { w.at(i) = m[v.at(i)]; } v = w; return; } void ranking(vector<long long> &v) { long long n = v.size(); map<long long, long long> m; long long i; for (i = 0; i < n; i++) { m[v.at(i)] = i; } vector<long long> w(n); i = 0; for (auto &p : m) { v.at(i) = p.second; i++; } return; } void erase(vector<long long> &v, long long i) { long long n = v.size(); if (i > n - 1) return; for (long long j = i; j < n - 1; j++) { v.at(j) = v.at(j + 1); } v.pop_back(); return; } void unique(vector<long long> &v) { long long n = v.size(); set<long long> s; long long i = 0; while (i < n) { if (s.count(v.at(i))) { erase(v, i); n--; } else { s.insert(v.at(i)); i++; } } return; } void printv(vector<long long> v) { cout << "{ "; for (auto &p : v) { cout << p << ","; } cout << "}" << endl; } vector<ll> keta(ll x) { if (x == 0) return {0}; ll n = log10(x) + 1; vll w(n, 0); for (ll i = 0; i < n; i++) { ll p; p = x % 10; x = x / 10; w[n - 1 - i] = p; } return w; } long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); } vector<long long> inputv(long long n) { vector<long long> v(n); for (long long i = 0; i < n; i++) { cin >> v[i]; } return v; } vector<long long> yakusuu(long long n) { vector<long long> ret; for (long long i = 1; i <= sqrt(n); ++i) { if (n % i == 0) { ret.push_back(i); if (i * i != n) { ret.push_back(n / i); } } } sort(ret.begin(), ret.end()); return ret; } map<long long, long long> soinsuu(long long n) { map<long long, long long> m; long long p = sqrt(n); while (n % 2 == 0) { n /= 2; if (m.count(2)) { m[2]++; } else { m[2] = 1; } } for (long long i = 3; i * i <= n; i += 2) { while (n % i == 0) { n /= i; if (m.count(i)) { m[i]++; } else { m[i] = 1; } } } if (n != 1) m[n] = 1; return m; } vector<vector<long long>> maze(ll i, ll j, vector<string> &s) { ll h = s.size(); ll w = s[0].size(); queue<vector<long long>> q; vector<vector<long long>> dis(h, vll(w, -1)); q.push({i, j}); dis[i][j] = 0; while (!q.empty()) { auto v = q.front(); q.pop(); if (v[0] > 0 && s[v[0] - 1][v[1]] == '.' && dis[v[0] - 1][v[1]] == -1) { dis[v[0] - 1][v[1]] = dis[v[0]][v[1]] + 1; q.push({v[0] - 1, v[1]}); } if (v[1] > 0 && s[v[0]][v[1] - 1] == '.' && dis[v[0]][v[1] - 1] == -1) { dis[v[0]][v[1] - 1] = dis[v[0]][v[1]] + 1; q.push({v[0], v[1] - 1}); } if (v[0] < h - 1 && s[v[0] + 1][v[1]] == '.' && dis[v[0] + 1][v[1]] == -1) { dis[v[0] + 1][v[1]] = dis[v[0]][v[1]] + 1; q.push({v[0] + 1, v[1]}); } if (v[1] < w - 1 && s[v[0]][v[1] + 1] == '.' && dis[v[0]][v[1] + 1] == -1) { dis[v[0]][v[1] + 1] = dis[v[0]][v[1]] + 1; q.push({v[0], v[1] + 1}); } } return dis; } long long modC(long long n, long long k, long long mod) { if (n < k) return 0; long long p = 1, q = 1; for (long long i = 0; i < k; i++) { p = p * (n - i) % mod; q = q * (i + 1) % mod; } return p * modinv(q, mod) % mod; } long long POW(long long a, long long n) { long long res = 1; while (n > 0) { if (n & 1) res = res * a; a = a * a; n >>= 1; } return res; } vector<long long> eratos(long long n) { if (n < 2) return {}; vll v(n - 1); for (long long i = 0; i < n - 1; i++) { v[i] = i + 2; } ll i = 0; while (i < n - 1) { ll p = v[i]; for (ll j = i + 1; j < n - 1; j++) { if (v[j] % p == 0) { v.erase(v.begin() + j); n--; } } i++; } v.resize(n - 1); return v; } set<long long> eraset(long long n) { set<long long> s; vll v = eratos(n); for (auto &t : v) { s.insert(t); } return s; } vll line(ll x1, ll y1, ll x2, ll y2) { vector<ll> v(3); v[0] = y1 - y2; v[1] = x2 - x1; v[2] = -x1 * (y1 - y2) + y1 * (x1 - x2); return v; } double dis(vll v, ll x, ll y) { double s = sqrt(v[0] * v[0] + v[1] * v[1]); return (double)abs(v[0] * x + v[1] * y + v[2]) / s; } ll const mod = 1e9 + 7; int main() { ll n; cin >> n; auto a = inputv(n); auto b = a; ll l = 0; ll res = 0; for (long long i = 0; i < n; i++) { if (l == 0) { if (a[0] == 0) { for (long long j = 0; j < n; j++) { if (a[j] != 0) { a[0] = a[j] / abs(a[j]); if (j % 1) a[0] *= (-1); res++; break; } } } if (!a[0]) { a[0] = 1; res++; } l += a[0]; } else if (l < 0) { if (a[i] < -l + 1) { res += -l + 1 - a[i]; a[i] = -l + 1; l = 1; } else { l += a[i]; } } else if (l > 0) { if (a[i] > -l - 1) { res += abs(a[i] - (-l - 1)); a[i] = -l - 1; l = -1; } else { l += a[i]; } } } a = b; ll L = 0; ll res2 = 0; for (long long i = 0; i < n; i++) { if (L == 0) { if (a[0] == 0) { for (long long j = 0; j < n; j++) { if (a[j] != 0) { a[0] = a[j] / abs(a[j]); if (j % 1) a[0] *= (-1); res2++; break; } } } if (!a[0]) { a[0] = 1; res2++; } L += a[0]; L *= (-1); } else if (L < 0) { if (a[i] < -L + 1) { res2 += -L + 1 - a[i]; a[i] = -L + 1; L = 1; } else { L += a[i]; } } else if (L > 0) { if (a[i] > -L - 1) { res2 += abs(a[i] - (-L - 1)); a[i] = -L - 1; L = -1; } else { L += a[i]; } } } cout << min(res, res2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; vector<int> a(N); for (auto& ai : a) cin >> ai; int c = 0; int s = a[0]; for (int i = (1); i < (N); ++i) { if (s > 0) { s += a[i]; if (s >= 0) { c += s + 1; s = -1; } } else { s += a[i]; if (s <= 0) { c += -s + 1; s = 1; } } } cout << c << 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 ll = long long; using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < (int)(n); i++) cin >> a.at(i); ll ans = 0, sum = 0; bool f = false; for (int i = 0; i < (int)(n); i++) { int now = a.at(i); if (f == false) { if (now == 0) { if (i + 1 < n) { if (a.at(i + 1) != 0) f = true; if (a.at(i + 1) > 0) sum--; else if (a.at(i + 1) < 0) sum++; } ans++; continue; } else { f = true; } } if ((sum < 0 && sum + now > 0) || (sum > 0 && sum + now < 0)) { sum += now; } else { ll add = abs(sum + now) + 1; if (sum < 0) sum = 1; else sum = -1; ans += add; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
package main import ( "bufio" "fmt" "os" "strconv" ) func abs(x int64) int64 { if x < 0 { return -x } return x } func min(a, b int64) int64 { if a < b { return a } return b } func main() { var n int fmt.Scan(&n) sc := bufio.NewScanner(os.Stdin) sc.Split(bufio.ScanWords) a := make([]int64, n) for i := 0; i < n; i++ { sc.Scan() x, _ := strconv.Atoi(sc.Text()) a[i] = int64(x) } ans := int64(0) if a[0] == 0 { a[0] = -1 ans = solve(a, 0) a[0] = 1 ans = min(ans, solve(a, 1)) } else if a[0] < 0 { ans = solve(a, 0) } else { // a[0] > 0 ans = solve(a, 1) } fmt.Println(ans) } func solve(a []int64, ra int) int64 { s := int64(0) // S_i = \sum_i a[i] ans := int64(0) for i, e := range a { if i%2 == ra { if s+e >= 0 { ans += abs(-1 - s - e) s = -1 continue } } else { if s+e <= 0 { ans += abs(1 - s - e) s = 1 continue } } s += int64(e) } return ans }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys fastinput=sys.stdin.readline n=int(fastinput()) ai=[int(i) for i in fastinput().split()] #プラス始まり goukei=0 sousa=0 for k,a in enumerate(ai): goukei+=a if k==0: continue elif not k%2:#even:plus if goukei<=0: sousa+=1-goukei goukei=1 else:#odd:minus if goukei>=0: sousa+=goukei+1 goukei=-1 ans1=sousa #マイナス始まり goukei=0 sousa=0 for k,a in enumerate(ai): goukei+=a if k==0: continue if k%2:#odd:plus if goukei<=0: sousa+=1-goukei goukei=1 else:#even:minus if goukei>=0: sousa+=goukei+1 goukei=-1 ans2=sousa print(min(ans1,ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int s = a[0]; int cnt = 0; if (s == 0) { if (a[1] > 0) { s = -1; cnt++; } else { s = 1; cnt++; } } for (int i = 1; i < n; i++) { if (s > 0) { if (s + a[i] < 0) s += a[i]; else { cnt += abs(s + a[i]) + 1; s = -1; } } else { if (s + a[i] > 0) s += a[i]; else { cnt += abs(s + a[i]) + 1; s = 1; } } } cout << cnt; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; const int MOD = 1000000007; const int mod = 1000000007; const int INF = 1000000000; const long long LINF = 1e18; const int MAX = 510000; int code(int n) { if (n < 0) return 1; else if (n > 0) return 0; else return 2; } int main() { int n; int sum = 0; int ans = 0; int ans2 = 0; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } sum = a.at(0); if (sum != 0) { for (int i = 1; i < n; i++) { if (sum + a.at(i) == 0) { ans++; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else if (code(sum + a.at(i)) == code(sum)) { ans += abs(sum + a.at(i)) + 1; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else { sum = a.at(i) + sum; } } cout << ans << endl; return 0; } else if (sum == 0) { sum = -1; ans = 1; for (int i = 1; i < n; i++) { if (sum + a.at(i) == 0) { ans++; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else if (code(sum + a.at(i)) == code(sum)) { ans += abs(sum + a.at(i)) + 1; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else { sum = a.at(i) + sum; } } sum = 1; ans2 = 1; for (int i = 1; i < n; i++) { if (sum + a.at(i) == 0) { ans2++; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else if (code(sum + a.at(i)) == code(sum)) { ans2 += abs(sum + a.at(i)) + 1; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else { sum = a.at(i) + sum; } } } cout << min(ans, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) s = list(map(int,input().split())) temp = s[0] ans = 0 #第一項が正ならば if temp > 0: for i in range(1,n): temp += s[i] if i%2 == 0:#正になってほしい if temp <= 0: ans += abs(temp-1) temp = 1 else:#負になって欲しい if temp >= 0: ans += abs(temp+1) temp = -1 else: for i in range(1,n): temp += s[i] if i%2 == 0: if temp >= 0: ans += abs(temp+1) temp = -1 else: if temp <= 0: ans += abs(temp-1) temp = 1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = std::vector<int>; using vc = std::vector<char>; using vll = std::vector<long long>; using vs = std::vector<string>; using Mi = map<int, int>; using Mll = map<ll, ll>; using UMi = unordered_map<int, int>; using UMll = unordered_map<ll, ll>; using Pi = pair<int, int>; using Pll = pair<ll, ll>; using vPi = vector<Pi>; using vPll = vector<Pll>; using vvi = vector<vector<int>>; using vvll = vector<vector<ll>>; using vvc = vector<vector<char>>; using vvs = vector<vector<string>>; using pqgi = priority_queue<int, vector<int>, greater<int>>; using pqsi = priority_queue<int, vector<int>, less<int>>; using pqgll = priority_queue<int, vector<int>, greater<int>>; using pssll = priority_queue<int, vector<int>, less<int>>; template <class T> using vec = vector<T>; class Cpmath { public: template <typename T> static T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } template <typename T> static T findGCD(vector<T>& arr, size_t n) { T result = arr[0]; for (size_t i = 1; i < n; i++) result = gcd(arr[i], result); return result; } template <typename T> static T findLCM(vector<T>& arr, size_t n) { T lcm = arr[0]; for (size_t i = 1; i < n; i++) { lcm = (lcm / gcd(arr[i], lcm)) * arr[i]; } return lcm; } template <typename T> static bool is_prime(T n) { if (n == 1) { return false; } for (size_t i = 2; i <= pow(n, 0.5); i++) { if (n % i == 0) { return false; } } return true; } static ll fact(ll n) { if (n == 0) { return 1LL; } else { return n * fact(n - 1); } } static ll permutation(int n, int r) { assert(n >= r); ll ret = 1; for (int i = n; i > n - r; i--) { ret *= i; } return ret; } }; class NCR { private: static const int MAX = 210000; static const int MOD = 998244353; ll fac[MAX], finv[MAX], inv[MAX]; public: 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; } } ll 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 BipartiteMatching { vector<vector<int>> E; int n, m; vector<int> match, dist; void init(int _n, int _m) { n = _n, m = _m; E.resize(n + m + 2); match.resize(n + m + 2); dist.resize(n + m + 2); } bool bfs() { queue<int> que; for (int i = (1); i < (n + 1); i++) { if (!match[i]) dist[i] = 0, que.push(i); else dist[i] = 1e9; } dist[0] = 1e9; while (!que.empty()) { int u = que.front(); que.pop(); if (u) for (auto& v : E[u]) if (dist[match[v]] == 1e9) { dist[match[v]] = dist[u] + 1; que.push(match[v]); } } return (dist[0] != 1e9); } bool dfs(int u) { if (u) { for (auto& v : E[u]) if (dist[match[v]] == dist[u] + 1) if (dfs(match[v])) { match[v] = u; match[u] = v; return true; } dist[u] = 1e9; return false; } return true; } void add(int a, int b) { b += n; E[a + 1].push_back(b + 1); E[b + 1].push_back(a + 1); } int whois(int x) { return match[x + 1] - 1; } int solve() { for (int i = (0); i < (n + m + 1); i++) match[i] = 0; int res = 0; while (bfs()) for (int i = (1); i < (n + 1); i++) if (!match[i] && dfs(i)) res++; return res; } }; struct SegmentTree { private: int n; vector<int> node; public: SegmentTree(vector<int> v) { int sz = v.size(); n = 1; while (n < sz) n *= 2; node.resize(2 * n - 1, 1e9); for (int i = 0; i < sz; i++) { node[i + n - 1] = v[i]; } for (int i = n - 2; i >= 0; i--) { node[i] = min(node[2 * i + 1], node[2 * i + 2]); } } void update(int x, int val) { x += (n - 1); node[x] = val; while (x > 0) { x = (x - 1) / 2; node[x] = min(node[2 * x + 1], node[2 * x + 2]); } } int getmin(int a, int b, int k = 0, int l = 0, int r = -1) { if (r < 0) { r = n; } if (r <= a || b <= l) { return 1e9; } if (a <= l && r <= b) { return node[k]; } int vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2); int vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r); return min(vl, vr); } }; template <class Abel> struct WUnionFind { vector<int> par; vector<int> rank; vector<Abel> diff_weight; WUnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); } void init(int n = 1, Abel SUM_UNITY = 0) { par.resize(n); rank.resize(n); diff_weight.resize(n); for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY; } int root(int x) { if (par[x] == x) { return x; } else { int r = root(par[x]); diff_weight[x] += diff_weight[par[x]]; return par[x] = r; } } Abel weight(int x) { root(x); return diff_weight[x]; } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y, Abel w) { w += weight(x); w -= weight(y); x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y), w = -w; if (rank[x] == rank[y]) ++rank[x]; par[y] = x; diff_weight[y] = w; return true; } Abel diff(int x, int y) { return weight(y) - weight(x); } }; struct UnionFind { vector<int> par; UnionFind(int n) : par(n, -1) {} void init(int n) { par.assign(n, -1); } int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int 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; } int size(int x) { return -par[root(x)]; } }; void YN(bool a) { cout << (a ? "YES" : "NO") << "\n"; } void Yn(bool a) { cout << (a ? "Yes" : "No") << "\n"; } void yn(bool a) { cout << (a ? "yes" : "no") << "\n"; } 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; } int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; template <typename T> inline int digitsum(T num) { int ret = 0; while (num) { ret += num % 10; num /= 10; } return ret; } template <typename InputIt, typename T> inline bool argexist(InputIt first, InputIt last, const T& x) { if (std::find(first, last, x) != last) { return true; } else { return false; } } template <typename InputIt, typename T> inline int argfind(InputIt first, InputIt last, const T& x) { auto it = find(first, last, x); return distance(first, it); } template <typename InputIt> inline int argmax(InputIt first, InputIt last) { auto it = max_element(first, last); return distance(first, it); } template <typename InputIt> inline int argmin(InputIt first, InputIt last) { auto it = min_element(first, last); return distance(first, it); } template <typename T> inline void erasebv(vector<T>& c, T v) { c.erase(remove(begin(c), end(c), v), end(c)); } template <typename T> inline void uniq(T& c) { c.erase(unique(begin(c), end(c)), end(c)); } template <typename T> inline T POP_BACK(vector<T>& que) { T x = que.back(); que.pop_back(); return x; } template <typename T> inline T POP_BACK(deque<T>& que) { T x = que.back(); que.pop_back(); return x; } template <typename T> inline T POP_FRONT(deque<T>& que) { T x = que.front(); que.pop_front(); return x; } template <typename T, typename C> inline T POP(stack<T, C>& stk) { T x = stk.top(); stk.pop(); return x; } template <typename T, typename C> inline T POP(queue<T, C>& que) { T x = que.front(); que.pop(); return x; } template <typename T, typename Cont, typename Cmp> inline T POP(priority_queue<T, Cont, Cmp>& que) { T x = que.top(); que.pop(); return x; } template <typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p) { return s << "(" << p.first << ", " << p.second << ")"; } template <typename T> ostream& operator<<(ostream& s, const vector<T>& v) { int len = v.size(); for (int i = 0; i < len; ++i) { s << v[i]; if (i < len - 1) s << "\t"; } return s; } template <typename T> ostream& operator<<(ostream& s, const vector<vector<T>>& vv) { int len = vv.size(); for (int i = 0; i < len; ++i) { s << vv[i] << "\n"; } return s; } namespace GraphLib { using Weight = int; struct Edge { int src, dst; Weight weight; Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {} }; bool operator<(const Edge& e, const Edge& f) { return e.weight != f.weight ? e.weight > f.weight : e.src != f.src ? e.src < f.src : e.dst < f.dst; } using Edges = vector<Edge>; using Graph = vector<Edges>; using Array = vector<Weight>; using Matrix = vector<Array>; void DijkstraShortestPath(const Graph& g, int s, vector<Weight>& dist, vector<int>& prev) { int n = g.size(); dist.assign(n, 1e9); dist[s] = 0; prev.assign(n, -1); priority_queue<Edge> Q; for (Q.push(Edge(-2, s, 0)); !Q.empty();) { Edge e = Q.top(); Q.pop(); if (prev[e.dst] != -1) continue; prev[e.dst] = e.src; for (auto& f : g[e.dst]) { if (dist[f.dst] > e.weight + f.weight) { dist[f.dst] = e.weight + f.weight; Q.push(Edge(f.src, f.dst, e.weight + f.weight)); } } } } vector<int> DijkstraBuildPath(const vector<int>& prev, int t) { vector<int> path; for (int u = t; u >= 0; u = prev[u]) path.push_back(u); reverse(path.begin(), path.end()); return path; } bool BellmandFordshortestPath(const Graph g, int s, vector<Weight>& dist, vector<int>& prev) { int n = g.size(); dist.assign(n, 1e9 + 1e9); dist[s] = 0; prev.assign(n, -2); bool negative_cycle = false; for (int k = 0; k < (int)(n); k++) for (int i = 0; i < (int)(n); i++) for (auto& e : g[i]) { if (dist[e.dst] > dist[e.src] + e.weight) { dist[e.dst] = dist[e.src] + e.weight; prev[e.dst] = e.src; if (k == n - 1) { dist[e.dst] = -1e9; negative_cycle = true; } } } return !negative_cycle; } vector<int> BellmanFordbuildPath(const vector<int>& prev, int t) { vector<int> path; for (int u = t; u >= 0; u = prev[u]) path.push_back(u); reverse(path.begin(), path.end()); return path; } void visit(const Graph& g, int v, int u, Edges& brdg, vector<vector<int>>& tecomp, stack<int>& roots, stack<int>& S, vector<bool>& inS, vector<int>& num, int& time) { num[v] = ++time; S.push(v); inS[v] = true; roots.push(v); for (auto& e : g[v]) { int w = e.dst; if (num[w] == 0) visit(g, w, v, brdg, tecomp, roots, S, inS, num, time); else if (u != w && inS[w]) while (num[roots.top()] > num[w]) roots.pop(); } if (v == roots.top()) { brdg.push_back(Edge(u, v, 1)); tecomp.push_back(vector<int>()); while (1) { int w = S.top(); S.pop(); inS[w] = false; tecomp.back().push_back(w); if (v == w) break; } roots.pop(); } } void bridge(const Graph& g, Edges& brdg, vector<vector<int>>& tecomp) { const int n = g.size(); vector<int> num(n); vector<bool> inS(n); stack<int> roots, S; int time = 0; for (int u = 0; u < (int)(n); u++) if (num[u] == 0) { visit(g, u, n, brdg, tecomp, roots, S, inS, num, time); brdg.pop_back(); } } pair<Weight, Edges> minimumSpanningTree(const Graph& g, int r = 0) { int n = g.size(); Edges T; Weight total = 0; vector<bool> visited(n); priority_queue<Edge> Q; Q.push(Edge(-1, r, 0)); while (!Q.empty()) { Edge e = Q.top(); Q.pop(); if (visited[e.dst]) continue; T.push_back(e); total += e.weight; visited[e.dst] = true; for (auto& f : g[e.dst]) if (!visited[f.dst]) Q.push(f); } return pair<Weight, Edges>(total, T); } } // namespace GraphLib ll N; vll a; vll s; signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); cin >> N; a.resize(N); vll s(N + 1, 0); for (int i = 0; i < (int)(N); i++) { cin >> a[i]; } s[0] = 0; s[1] = a[0]; bool prev_sign; if (a[0] > 0) { prev_sign = 1; } else { prev_sign = 0; } ll ret = 0; for (int i = (1); i < (N); i++) { if (prev_sign) { ll cur_s = s[i] + a[i]; if (cur_s < 0) { s[i + 1] = s[i] + a[i]; } else { s[i + 1] = -1; ret += abs(cur_s + 1); } prev_sign = 0; } else { ll cur_s = s[i] + a[i]; if (cur_s > 0) { s[i + 1] = s[i] + a[i]; } else { s[i + 1] = 1; ret += abs(cur_s - 1); } prev_sign = 1; } } cout << ret << "\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 ll = long long; using itn = int; using namespace std; int GCD(int a, int b) { return b ? GCD(b, a % b) : a; } int main() { string s; cin >> s; for (int i = 0; i < s.size(); i++) { for (int j = 0; j < s.size() - i; j++) { string t; if (j != 0) { t = s.substr(0, j - 1); } t += s.substr(j + i); if (t == "keyence") { cout << "YES" << endl; return 0; } } } cout << "NO" << 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 double eps = 1e-8; const double pi = acos(-1.0); const int K = 1e6 + 7; const int mod = 1e9 + 7; long long ans, sum, ls, n, x; int main(void) { std::ios::sync_with_stdio(false); std::cin.tie(0); cin >> n >> sum; ls = sum; while (n-- != 1) { cin >> x; sum += x; if (sum == 0 && ls > 0) sum = ls = -1, ans += 1; else if (sum == 0 && ls < 0) sum = ls = 1, ans += 1; else if (sum > 0 && ls > 0) ans += sum + 1, sum = ls = -1; else if (sum < 0 && ls < 0) ans += 1 - sum, ls = sum = 1; else ls = sum; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.io.*; import java.math.BigInteger; public class Main implements Runnable { static ContestScanner in; static Writer out; public static void main(String[] args) { new Thread(null, new Main(), "", 16 * 1024 * 1024).start(); } public void run() { Main main = new Main(); try { in = new ContestScanner(); out = new Writer(); main.solve(); out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } void solve() throws IOException { int n = in.nextInt(); long ans = 0; long sum = in.nextLong(); for (int i = 0; i < n - 1; i++) { long a = in.nextLong(); long target = -Long.signum(sum); if ((a + sum) * target > 0) sum += a; else { ans += Math.abs(a + sum - target); sum = target; } } System.out.println(ans); } } class Writer extends PrintWriter{ public Writer(String filename)throws IOException {super(new BufferedWriter(new FileWriter(filename)));} public Writer()throws IOException {super(System.out);} } class ContestScanner implements Closeable { private BufferedReader in;private int c=-2; public ContestScanner()throws IOException {in=new BufferedReader(new InputStreamReader(System.in));} public ContestScanner(String filename)throws IOException {in=new BufferedReader(new InputStreamReader(new FileInputStream(filename)));} public String nextToken()throws IOException { StringBuilder sb=new StringBuilder(); while((c=in.read())!=-1&&Character.isWhitespace(c)); while(c!=-1&&!Character.isWhitespace(c)){sb.append((char)c);c=in.read();} return sb.toString(); } public String readLine()throws IOException{ StringBuilder sb=new StringBuilder();if(c==-2)c=in.read(); while(c!=-1&&c!='\n'&&c!='\r'){sb.append((char)c);c=in.read();} return sb.toString(); } public long nextLong()throws IOException,NumberFormatException {return Long.parseLong(nextToken());} public int nextInt()throws NumberFormatException,IOException {return(int)nextLong();} public double nextDouble()throws NumberFormatException,IOException {return Double.parseDouble(nextToken());} public void close() throws IOException {in.close();} }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = (long long)1e9; const long long MOD = (long long)1e9 + 7; const double EPS = (double)1e-10; struct Accelerate_Cin { Accelerate_Cin() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); }; }; signed main() { long long n; cin >> n; long long sum = 0, prev = 0, cont = 0; for (long long t = 0; t < n; t++) { long long a; cin >> a; if (t == 0) sum = a; if (t != 0) sum = prev + a; if (sum * prev >= 0 && t != 0) { cont += abs(sum) + 1; if (prev < 0) sum = 1; if (prev > 0) sum = -1; } prev = sum; } cout << cont << "\n"; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) s = [sum(a[0:i+1]) for i in range(n)] count = 0 total_delta = 0 if s[0] == 0: delta = -1 * (s[1] // abs(s[1])) total_delta += delta count += 1 for i in range(1, n): sign = (s[i-1] + total_delta) // abs(s[i-1] + total_delta) if (s[i] + total_delta) * sign > 0 : delta = (sign * -1) - (s[i] + total_delta) total_delta += delta count += abs(delta) elif (s[i] + total_delta) == 0: total_delta += sign * -1 count += 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
python3
# coding: utf-8 import sys #from operator import itemgetter sysread = sys.stdin.buffer.readline read = sys.stdin.buffer.read #from heapq import heappop, heappush #from collections import defaultdict sys.setrecursionlimit(10**7) #import math #from itertools import product, accumulate, combinations, product #import bisect import numpy as np #from copy import deepcopy #from collections import deque #from decimal import Decimal #from numba import jit INF = 1 << 50 EPS = 1e-8 def run(): n, *A = map(int, read().split()) v = 0 acum = [] for a in A: v += a acum.append(v) # greedy cums = 0 count = 0 if not A[0]: V = 1 else: V = A[0] // abs(A[0]) for a in acum[1:]: #print(a, '---------') V *= -1 if (a + cums) * V > 0: continue else: update = abs(a + cums) + 1 cums += (update) * V count += update #print(V, cums, count) print(count) if __name__ == "__main__": run()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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 cal(int b0, int n, long long* a) { long long b[n], ans = 0; b[0] = b0; for (int i = 1; i < n; i++) { b[i] = b[i - 1] + a[i]; if (b[i] == 0) { ans++; b[i] = -1 * b[i - 1] / b[i - 1]; } if (a[i] * b[i - 1] > 0 || (abs(a[i]) - abs(b[i - 1])) < 0) { ans += abs(a[i] + b[i - 1]) + 1; b[i] = -1 * b[i - 1] / b[i - 1]; } } return ans; } int main() { int n; cin >> n; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; if (a[0] != 0) { cout << cal(a[0], n, a) << endl; } else { cout << (cal(1, n, a) > cal(-1, n, a) ? cal(1, n, a) : cal(-1, n, a)) << endl; return 0; } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int ans = INT_MAX, res, standard, n, a[100000], sum; int get_sign(int x) { if (0 < x) return 1; else if (x < 0) return -1; else return 0; } int main() { cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; res = 0, standard = 1, sum = 0; for (int i = 0; i < n; ++i) { sum += a[i]; if (standard != get_sign(sum)) { int add = abs(sum) + 1; res += add; if (sum > 0) sum -= add; else if (sum < 0) sum += add; } standard *= (-1); } ans = min(ans, res); res = 0, standard = -1, sum = 0; for (int i = 0; i < n; ++i) { sum += a[i]; if (standard != get_sign(sum)) { int add = abs(sum) + 1; res += add; if (sum > 0) sum -= add; else if (sum < 0) sum += add; } standard *= (-1); } ans = min(ans, res); cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import functools n = int(input()) a = list(map(int, input().split())) r = a[0] @functools.lru_cache() def memo(n): global r cnt = 0 for i in range(1,n): if r>0: if r+a[i]<0: r+=a[i] else: cnt += a[i] +r +1 r = -1 elif r<0: if r+a[i]>0: r+=a[i] else: cnt += -a[i] - r+1 r = 1 return cnt print(memo(n))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def check1(a): sum = 0 for i in range(len(a)): if(i % 2 == 0): sum += a[i] if(sum >= 0): return (i, -abs(sum)-1) else: sum += a[i] if(sum <= 0): return (i, abs(sum)+1) return True def check2(a): sum = 0 for i in range(len(a)): if(i % 2 == 0): sum += a[i] if(sum <= 0): return (i, abs(sum)+1) else: sum += a[i] if(sum >= 0): return (i, -abs(sum)-1) return True n = input() b = input().split() a = [int(b[i]) for i in range(len(b))] a2 = list(a) ans1 = 0 ans2 = 0 while(True): c = check1(a) if(c == True): break a[c[0]] += c[1] ans1 += abs(c[1]) while(True): c = check2(a2) if(check2(a2) == True): break a2[c[0]] += c[1] ans2 += abs(c[1]) print(ans1) if(ans1<ans2) else print(ans2)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(){ int N; cin>>N; vector<int> data(N); for(int i=0;i<N;i++)cin>>data[i]; int count=0; int ans=data[0]; int saisyo; for(int i=1;i<N;i++){ ans+=data[i]; if(i%2==0){ while(ans<=0){ ans++; count++; } }else{ while(ans>=0){ ans--; count++; } } } saisyo=count; count=0; ans=data[0]; while(ans>=0){ ans--; count++ } for(int i=0;i<N;i++){ ans+=data[i]; if(i%2!=0){ while(ans<=0){ ans++; count++; } }else{ while(ans>=0){ ans--; count++; } } } saisyo=min(saisyo,count); cout<<saisyo<<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
#![allow(unused_macros)] #![allow(unused_mut)] #![allow(non_snake_case)] #![allow(unused_variables)] #![allow(unused_imports)] // // https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 // macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); let mut next = || { iter.next().unwrap() }; input_inner!{next, $($r)*} }; ($($r:tt)*) => { let stdin = std::io::stdin(); let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); let mut next = move || -> String{ bytes .by_ref() .map(|r|r.unwrap() as char) .skip_while(|c|c.is_whitespace()) .take_while(|c|!c.is_whitespace()) .collect() }; input_inner!{next, $($r)*} }; } macro_rules! input_inner { ($next:expr) => {}; ($next:expr, ) => {}; ($next:expr, $var:ident : $t:tt $($r:tt)*) => { let mut $var = read_value!($next, $t); input_inner!{$next $($r)*} }; } macro_rules! read_value { ($next:expr, ( $($t:tt),* )) => { ( $(read_value!($next, $t)),* ) }; ($next:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() }; ($next:expr, chars) => { read_value!($next, String).chars().collect::<Vec<char>>() }; ($next:expr, usize1) => { read_value!($next, usize) - 1 }; ($next:expr, $t:ty) => { $next().parse::<$t>().expect("Parse error") }; } macro_rules! max { ($x: expr) => ($x); ($x: expr, $($y: expr),+) => (::std::cmp::max($x, max!( $($y),+))); } macro_rules! min { ($x: expr) => ($x); ($x: expr, $($y: expr),+) => (::std::cmp::min($x, min!( $($y),+))); } // solution use std::cmp; use std::collections::{HashMap, HashSet, BTreeMap, BTreeSet}; fn main() { input! { n: usize, a: [i64; n], } let mut sum1 = 0; let mut sum2 = 0; let mut count1 = 0; let mut count2 = 0; let mut sign1: i64 = 1; let mut sign2: i64 = -1; for aa in a.iter() { sum1 += aa; sum2 += aa; if sign1*sum1 <= 0 { count1 += sum1.abs() + sign1.abs(); sum1 = sign1; } if sign2*sum2 <= 0 { count2 += sum2.abs() + sign2.abs(); sum2 = sign2; } sign1 = sign1 - 2*sign1; sign2 = sign2 - 2*sign2; } println!("{}", cmp::min(count1, count2)); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) s = [ sum(a[0:i+1]) for i in range(n)] s2 = [ s[i]*(-1) for i in range(n)] al = [(-1)**i for i in range(n)] c1 = 0 for i in range(n): if s[i]*al[i] <= 0: c1 = c1 - s[i]*al[i] + 1 s = [s[j] if j <= i-1 else s[j] + (s[i])*al[i] + al[i] for j in range(n)] c2 = 0 for i in range(n): if s2[i]*al[i] <= 0: c2 = c2 - s2[i]*al[i] + 1 s2 = [s2[j] if j <= i-1 else s2[j] - (s2[i])*al[i] + al[i] for j in range(n)] print(min(c1,c2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int a[100050]; int main() { int n; scanf("%d", &n); int cnt1 = 0, cnt2 = 0; for (int i = 0; i < n; i++) scanf("%d", &a[i]); int lazy1 = 0, lazy2 = 0; for (int i = 0; i < n; i++) { int sum1 = 0, sum2 = 0; for (int j = 0; j <= i; j++) { sum1 += a[j]; sum2 = sum1; } sum1 += lazy1; sum2 += lazy2; if (i % 2 == 0 && sum1 <= 0) { lazy1 += 1 - sum1; cnt1 += 1 - sum1; } if (i % 2 == 0 && sum2 >= 0) { lazy2 -= 1 + sum2; cnt2 += sum2 + 1; } if (i % 2 == 1 && sum1 >= 0) { lazy1 -= 1 + sum1; cnt1 += sum1 + 1; } if (i % 2 == 1 && sum2 <= 0) { lazy2 += 1 - sum2; cnt2 += 1 - sum2; } } printf("%d\n", min(cnt1, cnt2)); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long input[n]; for (long long i = 0; i < n; i++) cin >> input[i]; long long pre, aft, result = 0; pre = input[0]; for (long long i = 1; i < n; i++) { aft = pre + input[i]; if (aft == 0) { if (pre < 0) { aft = 1; } else aft = -1; result += 1; } if (aft > 0) { if (pre > 0) { aft = -1; result += (2 * abs(input[i]) + 1); } } if (aft < 0) { if (pre < 0) { aft = 1; result += (2 * abs(input[i]) + 1); } } pre = aft; } cout << result << 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 maxn = 1e5 + 10; long long s[maxn]; long long ans[maxn]; int main() { int n, j; cin >> n; long long sum = 0; for (int i = 1; i <= n; i++) { cin >> s[i]; } if (s[1]) { for (int i = 1; i < n; i++) { ans[i] = ans[i - 1] + s[i]; if (ans[i] > 0) { if (s[i + 1] >= 0) { sum += (s[i + 1] + ans[i] + 1); s[i + 1] = -(ans[i] + 1); } else { if (abs(s[i + 1]) > ans[i]) { } else { sum += (s[i + 1] + ans[i] + 1); s[i + 1] = -(ans[i] + 1); } } } else if (ans[i] < 0) { if (s[i + 1] > 0) { if (abs(ans[i]) < s[i + 1]) { } else { sum += (1 - ans[i] - s[i + 1]); s[i + 1] = -ans[i] + 1; } } else { sum += (1 - ans[i] - s[i + 1]); s[i + 1] = -ans[i] + 1; } } } cout << sum << endl; } else if (!s[1]) { sum = 0; long long anss = 0; ans[1] = 1; s[2] = -2; sum += 3; for (int i = 2; i < n; i++) { ans[i] = ans[i - 1] + s[i]; if (ans[i] > 0) { if (s[i + 1] >= 0) { sum += (s[i + 1] + ans[i] + 1); s[i + 1] = -(ans[i] + 1); } else { if (abs(s[i + 1]) > ans[i]) { } else { sum += (s[i + 1] + ans[i] + 1); s[i + 1] = -(ans[i] + 1); } } } else if (ans[i] < 0) { if (s[i + 1] > 0) { if (abs(ans[i]) < s[i + 1]) { } else { sum += (1 - ans[i] - s[i + 1]); s[i + 1] = -ans[i] + 1; } } else { sum += (1 - ans[i] - s[i + 1]); s[i + 1] = -ans[i] + 1; } } } ans[1] = -1; s[2] = 2; anss += 3; for (int i = 2; i < n; i++) { ans[i] = ans[i - 1] + s[i]; if (ans[i] > 0) { if (s[i + 1] >= 0) { anss += (s[i + 1] + ans[i] + 1); s[i + 1] = -(ans[i] + 1); } else { if (abs(s[i + 1]) > ans[i]) { } else { anss += (s[i + 1] + ans[i] + 1); s[i + 1] = -(ans[i] + 1); } } } else if (ans[i] < 0) { if (s[i + 1] > 0) { if (abs(ans[i]) < s[i + 1]) { } else { anss += (1 - ans[i] - s[i + 1]); s[i + 1] = -ans[i] + 1; } } else { anss += (1 - ans[i] - s[i + 1]); s[i + 1] = -ans[i] + 1; } } } cout << min(anss, sum) << endl; } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> static std::uint64_t solve(const std::vector<int>& va, int initSum, std::uint64_t initCnt = 0) { int sum = initSum; std::uint64_t cnt = initCnt; for (std::remove_reference<decltype(va)>::type::size_type i = 1; i < va.size(); i++) { auto nextSum = sum + va[i]; if (nextSum >= 0 && sum > 0) { cnt += nextSum + 1; sum = -1; } else if (nextSum <= 0 && sum < 0) { cnt += -nextSum + 1; sum = 1; } else { sum = nextSum; } } return cnt; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); int n; std::cin >> n; std::vector<int> va(n); for (auto&& e : va) { std::cin >> e; } std::uint64_t initCnt = 0; if (va[0] == 0) { va[0] = 1; initCnt++; } std::cout << std::min( solve(va, va[0], initCnt), solve(va, va[0] > 0 ? -1 : 1, std::abs(va[0]) + 1 + initCnt)) << std::endl; return EXIT_SUCCESS; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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 io = new Scanner(System.in); int n = io.nextInt(); int[] a = new int[n+1]; for(int i=1;i<=n;i++){ a[i] = io.nextInt(); } // + - int sum = 0; int now=0; int border = 1; int end = 0; int ans_p=0; for(int i=1;i<=n;i++){ sum += a[i]; end = border-sum; if(border>0){ if(now<end){ ans_p += Math.abs(now-end); now = end; } }else{ if(now>end){ ans_p += Math.abs(now-end); now = end; } } border = -border; } //- + sum=0; now=0; border = -1; end = 0; int ans_m=0; for(int i=1;i<=n;i++){ sum += a[i]; end = border-sum; if(border>0){ if(now<end){ ans_m += Math.abs(now-end); now = end; } }else{ if(now>end){ ans_m += Math.abs(now-end); now = end; } } border = -border; } System.out.println(Math.min(ans_p,ans_m)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# coding: utf-8 # Here your code N = int(input()) a = [int(i) for i in input().split()] result_1 = 0 before_sum =a[0] if a[0] == 0: before_sum = 1 after_sum =a[0] for i in range(1,N): before_sum = after_sum after_sum = before_sum + a[i] if before_sum * after_sum > 0: if after_sum < 0: result_1 += 1 - after_sum after_sum = 1 elif after_sum > 0: result_1 += 1 + after_sum after_sum = -1 elif before_sum * after_sum == 0: result_1 += 1 if before_sum < 0: after_sum = 1 else: after_sum = -1 if a[0] < 0: before_sum = 1 elif a[0] >= 0: before_sum = -1 after_sum =a[0] result_2 = 1 + abs(before_sum) for i in range(1,N): before_sum = after_sum after_sum = before_sum + a[i] if before_sum * after_sum > 0: if after_sum < 0: result_2 += 1 - after_sum after_sum = 1 elif after_sum > 0: result_2 += 1 + after_sum after_sum = -1 elif before_sum * after_sum == 0: result_2 += 1 if before_sum < 0: after_sum = 1 else: after_sum = -1 print(min(result_1,result_2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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()) sa = input().split() a = [0 for i in range(n)] sumeven = 0 sumodd = 0 for i,sai in enumerate(sa): a[i] = int(sai) if i % 2 == 0: sumeven += a[i] else: sumodd += a[i] ret = 0 sm = 0 for i,ai in enumerate(a): if i % 2 == 1: ret += max(-sm+1-ai,0) sm += max(ai, -sm+1) else: ret += max(sm+1 + ai, 0) sm += min(ai, -(sm+1)) ret2 = 0 sm = 0 for i,ai in enumerate(a): if i % 2 == 0: ret2 += max(-sm+1-ai,0) sm += max(ai, -sm+1) else: ret2 += max(sm+1 + ai, 0) sm += min(ai, -(sm+1)) print(max(ret,ret2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, s; int count = 0; vector<int> a; cin >> n; for (int i = 0; i < n; ++i) { cin >> s; a.emplace_back(s); } if (a[0] == 0) { count = count + 1; } int sum = a[0]; for (int i = 0; i < n - 1; ++i) { if (i > 0) { sum = sum + a[i]; } if (sum * (sum + a[i + 1]) >= 0 && abs(sum) >= abs(sum + a[i + 1])) { count = count + abs(sum + a[i + 1]) + 1; if (sum + a[i + 1] < 0) { sum = sum + abs(sum + a[i + 1]) + 1; } else { sum = sum - abs(sum + a[i + 1]) - 1; } } if (sum * (sum + a[i + 1]) >= 0 && abs(sum) < abs(sum + a[i + 1])) { count = count + abs(sum) + 1; if (sum < 0) { sum = sum + abs(sum) + 1; } else { sum = sum - abs(sum) - 1; } } } cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
p :: Int -> Int -> [Int] -> Int p n x (y:[]) | x + y >= 0 = n+x+y+1 | otherwise = n p n x (y:ys) | x + y >= 0 = m (n+x+y+1) (-1) ys | otherwise = m n (x+y) ys m :: Int -> Int -> [Int] -> Int m n x (y:[]) | x + y <= 0 = n-(x+y)+1 | otherwise = n m n x (y:ys) | x + y <= 0 = p (n-(x+y)+1) 1 ys | otherwise = p n (x+y) ys solve :: [Int] -> Int solve (x:xs) | x /= 0 = min (m 0 x xs) (p 0 x xs) | x == 0 = min (p 1 (x+1) xs) (m 1 (x-1) xs) main = getContents >>= print . solve . map read . words . last . lines
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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() { unsigned long long n; cin >> n; vector<long long> a(n); for (unsigned long long i = 0; i < n; ++i) cin >> a[i]; unsigned long long op = 0; long long sum = a[0]; for (unsigned long long i = 1; i < n; ++i) { if (sum > 0) { sum += a[i]; while (sum >= 0) { ++op; --sum; } } else { sum += a[i]; while (sum <= 0) { ++op; ++sum; } } } cout << op << endl; return EXIT_SUCCESS; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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_lst = [int(x) for x in input().split()] b_lst = a_lst def my_sign(num): return (num > 0) - (num < 0) sum = 0 cnt_p = 0 cnt_n = 0 sum_lst = [] sum2_lst = [] for i in range(N): if i == 0: if a_lst[i] == 0: a_lst[i] = 1 cnt_p += 1 sum_lst.append(a_lst[i]) else: sum_lst.append(a_lst[i] + sum_lst[i - 1]) if my_sign(sum_lst[i]) == my_sign(sum_lst[i - 1]) or my_sign(sum_lst[i]) == 0: cnt_p += max(-my_sign(sum_lst[i - 1]), sum_lst[i]) - min(-my_sign(sum_lst[i - 1]), sum_lst[i]) a_lst[i] += -my_sign(sum_lst[i - 1]) - sum_lst[i] sum_lst[i] = -my_sign(sum_lst[i - 1]) for i in range(N): if i == 0: if b_lst[i] == 0: b_lst[i] = -1 cnt_n += 1 sum2_lst.append(b_lst[i]) else: sum2_lst.append(b_lst[i] + sum2_lst[i - 1]) if my_sign(sum2_lst[i]) == my_sign(sum2_lst[i - 1]) or my_sign(sum2_lst[i]) == 0: cnt_n += max(-my_sign(sum2_lst[i - 1]), sum2_lst[i]) - min(-my_sign(sum2_lst[i - 1]), sum2_lst[i]) b_lst[i] += -my_sign(sum2_lst[i - 1]) - sum2_lst[i] sum2_lst[i] = -my_sign(sum2_lst[i - 1]) print(min(cnt_p, cnt_n))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> template <typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; } using namespace std; std::mt19937 mt((long long)time(0)); long long dx[4] = {0, 1, 0, -1}; long long dy[4] = {1, 0, -1, 0}; using Weight = long long; using Flow = long long; struct Edge { long long src, dst; Weight weight; Flow cap; Edge() : src(0), dst(0), weight(0) {} Edge(long long s, long long d, Weight w) : src(s), dst(d), weight(w) {} }; using Edges = std::vector<Edge>; using Graph = std::vector<Edges>; using Array = std::vector<Weight>; using Matrix = std::vector<Array>; void add_edge(Graph& g, long long a, long long b, Weight w = 1) { g[a].emplace_back(a, b, w); g[b].emplace_back(b, a, w); } void add_arc(Graph& g, long long a, long long b, Weight w = 1) { g[a].emplace_back(a, b, w); } struct uf_tree { std::vector<long long> parent; long long __size; uf_tree(long long size_) : parent(size_, -1), __size(size_) {} void unite(long long x, long long y) { if ((x = find(x)) != (y = find(y))) { if (parent[y] < parent[x]) std::swap(x, y); parent[x] += parent[y]; parent[y] = x; __size--; } } bool is_same(long long x, long long y) { return find(x) == find(y); } long long find(long long x) { return parent[x] < 0 ? x : parent[x] = find(parent[x]); } long long size(long long x) { return -parent[find(x)]; } long long size() { return __size; } }; template <signed M, unsigned T> struct mod_int { constexpr static signed MODULO = M; constexpr static unsigned TABLE_SIZE = T; signed x; mod_int() : x(0) {} mod_int(long long y) : x(static_cast<signed>(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO)) {} mod_int(signed y) : x(y >= 0 ? y % MODULO : MODULO - (-y) % MODULO) {} mod_int& operator+=(const mod_int& rhs) { if ((x += rhs.x) >= MODULO) x -= MODULO; return *this; } mod_int& operator-=(const mod_int& rhs) { if ((x += MODULO - rhs.x) >= MODULO) x -= MODULO; return *this; } mod_int& operator*=(const mod_int& rhs) { x = static_cast<signed>(1LL * x * rhs.x % MODULO); return *this; } mod_int& operator/=(const mod_int& rhs) { x = static_cast<signed>((1LL * x * rhs.inv().x) % MODULO); return *this; } mod_int operator-() const { return mod_int(-x); } mod_int operator+(const mod_int& rhs) const { return mod_int(*this) += rhs; } mod_int operator-(const mod_int& rhs) const { return mod_int(*this) -= rhs; } mod_int operator*(const mod_int& rhs) const { return mod_int(*this) *= rhs; } mod_int operator/(const mod_int& rhs) const { return mod_int(*this) /= rhs; } bool operator<(const mod_int& rhs) const { return x < rhs.x; } mod_int inv() const { assert(x != 0); if (x <= static_cast<signed>(TABLE_SIZE)) { if (_inv[1].x == 0) prepare(); return _inv[x]; } else { signed a = x, b = MODULO, u = 1, v = 0, t; while (b) { t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return mod_int(u); } } mod_int pow(long long t) const { assert(!(x == 0 && t == 0)); mod_int e = *this, res = mod_int(1); for (; t; e *= e, t >>= 1) if (t & 1) res *= e; return res; } mod_int fact() { if (_fact[0].x == 0) prepare(); return _fact[x]; } mod_int inv_fact() { if (_fact[0].x == 0) prepare(); return _inv_fact[x]; } mod_int choose(mod_int y) { assert(y.x <= x); return this->fact() * y.inv_fact() * mod_int(x - y.x).inv_fact(); } static mod_int _inv[TABLE_SIZE + 1]; static mod_int _fact[TABLE_SIZE + 1]; static mod_int _inv_fact[TABLE_SIZE + 1]; static void prepare() { _inv[1] = 1; for (long long i = 2; i <= (long long)TABLE_SIZE; ++i) { _inv[i] = 1LL * _inv[MODULO % i].x * (MODULO - MODULO / i) % MODULO; } _fact[0] = 1; for (unsigned i = 1; i <= TABLE_SIZE; ++i) { _fact[i] = _fact[i - 1] * signed(i); } _inv_fact[TABLE_SIZE] = _fact[TABLE_SIZE].inv(); for (long long i = (long long)TABLE_SIZE - 1; i >= 0; --i) { _inv_fact[i] = _inv_fact[i + 1] * (i + 1); } } }; template <signed M, unsigned F> std::ostream& operator<<(std::ostream& os, const mod_int<M, F>& rhs) { return os << rhs.x; } template <signed M, unsigned F> std::istream& operator>>(std::istream& is, mod_int<M, F>& rhs) { long long s; is >> s; rhs = mod_int<M, F>(s); return is; } template <signed M, unsigned F> mod_int<M, F> mod_int<M, F>::_inv[TABLE_SIZE + 1]; template <signed M, unsigned F> mod_int<M, F> mod_int<M, F>::_fact[TABLE_SIZE + 1]; template <signed M, unsigned F> mod_int<M, F> mod_int<M, F>::_inv_fact[TABLE_SIZE + 1]; template <signed M, unsigned F> bool operator==(const mod_int<M, F>& lhs, const mod_int<M, F>& rhs) { return lhs.x == rhs.x; } template <long long M, unsigned F> bool operator!=(const mod_int<M, F>& lhs, const mod_int<M, F>& rhs) { return !(lhs == rhs); } const signed MF = 1000010; const signed MOD = 1000000007; using mint = mod_int<MOD, MF>; mint binom(long long n, long long r) { return (r < 0 || r > n || n < 0) ? 0 : mint(n).choose(r); } mint fact(long long n) { return mint(n).fact(); } mint inv_fact(long long n) { return mint(n).inv_fact(); } template <typename T, typename E> struct SegmentTree { typedef function<T(T, T)> F; typedef function<T(T, E)> G; typedef function<E(E, E)> H; typedef function<E(E, long long)> P; long long n; F f; G g; H h; P p; T d1; E d0; vector<T> dat; vector<E> laz; SegmentTree( long long n_, F f, G g, H h, T d1, E d0, vector<T> v = vector<T>(), P p = [](E a, long long b) { return a; }) : f(f), g(g), h(h), d1(d1), d0(d0), p(p) { init(n_); if (n_ == (long long)v.size()) build(n_, v); } void init(long long n_) { n = 1; while (n < n_) n *= 2; dat.clear(); dat.resize(2 * n - 1, d1); laz.clear(); laz.resize(2 * n - 1, d0); } void build(long long n_, vector<T> v) { for (long long i = 0; i < n_; i++) dat[i + n - 1] = v[i]; for (long long i = n - 2; i >= 0; i--) dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]); } inline void eval(long long len, long long k) { if (laz[k] == d0) return; if (k * 2 + 1 < n * 2 - 1) { laz[k * 2 + 1] = h(laz[k * 2 + 1], laz[k]); laz[k * 2 + 2] = h(laz[k * 2 + 2], laz[k]); } dat[k] = g(dat[k], p(laz[k], len)); laz[k] = d0; } T update(long long a, long long b, E x, long long k, long long l, long long r) { eval(r - l, k); if (r <= a || b <= l) return dat[k]; if (a <= l && r <= b) { laz[k] = h(laz[k], x); return g(dat[k], p(laz[k], r - l)); } return dat[k] = f(update(a, b, x, k * 2 + 1, l, (l + r) / 2), update(a, b, x, k * 2 + 2, (l + r) / 2, r)); } T update(long long a, long long b, E x) { return update(a, b, x, 0, 0, n); } T query(long long a, long long b, long long k, long long l, long long r) { eval(r - l, k); if (r <= a || b <= l) return d1; if (a <= l && r <= b) return dat[k]; T vl = query(a, b, k * 2 + 1, l, (l + r) / 2); T vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return f(vl, vr); } T query(long long a, long long b) { return query(a, b, 0, 0, n); } }; class compress { public: static const long long MAP = 10000000; map<long long, long long> zip; long long unzip[MAP]; compress(vector<long long>& x) { sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end()); for (long long i = 0; i < x.size(); i++) { zip[x[i]] = i; unzip[i] = x[i]; } } }; unsigned euclidean_gcd(unsigned a, unsigned b) { while (1) { if (a < b) swap(a, b); if (!b) break; a %= b; } return a; } template <class T> struct CumulativeSum2D { vector<vector<T>> data; CumulativeSum2D(long long W, long long H) : data(W + 1, vector<long long>(H + 1, 0)) {} void add(long long x, long long y, T z) { ++x, ++y; if (x >= data.size() || y >= data[0].size()) return; data[x][y] += z; } void build() { for (long long i = 1; i < data.size(); i++) { for (long long j = 1; j < data[i].size(); j++) { data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1]; } } } T query(long long sx, long long sy, long long gx, long long gy) { return (data[gx][gy] - data[sx][gy] - data[gx][sy] + data[sx][sy]); } }; long long nC2(long long n) { return n * (n - 1) / 2; } class node { public: long long depth; long long num; node(long long d, long long n) { depth = d; num = n; } }; CumulativeSum2D<long long> sumB(4001, 4001); template <class T> struct CumulativeSum { vector<T> data; CumulativeSum(long long sz) : data(sz, 0){}; void add(long long k, T x) { data[k] += x; } void build() { for (long long i = 1; i < data.size(); i++) { data[i] += data[i - 1]; } } T query(long long k) { if (k < 0) return (0); return (data[min(k, (long long)data.size() - 1)]); } T query(long long left, long long right) { return query(right) - query(left - 1); } }; std::vector<bool> IsPrime; void sieve(size_t max) { if (max + 1 > IsPrime.size()) { IsPrime.resize(max + 1, true); } IsPrime[0] = false; IsPrime[1] = false; for (size_t i = 2; i * i <= max; ++i) if (IsPrime[i]) for (size_t j = 2; i * j <= max; ++j) IsPrime[i * j] = false; } vector<int64_t> divisor(int64_t n) { vector<int64_t> ret; for (int64_t i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(begin(ret), end(ret)); return (ret); } long long binary_search(function<bool(long long)> isOk, long long ng, long long ok) { while (abs(ok - ng) > 1) { long long mid = (ok + ng) / 2; if (isOk(mid)) ok = mid; else ng = mid; } return ok; } std::pair<std::vector<Weight>, bool> bellmanFord(const Graph& g, long long s) { long long n = g.size(); const Weight inf = std::numeric_limits<Weight>::max() / 8; Edges es; for (long long i = 0; i < n; i++) for (auto& e : g[i]) es.emplace_back(e); std::vector<Weight> dist(n, inf); dist[s] = 0; bool negCycle = false; for (long long i = 0;; i++) { bool update = false; for (auto& e : es) { if (dist[e.src] != inf && dist[e.dst] > dist[e.src] + e.weight) { dist[e.dst] = dist[e.src] + e.weight; update = true; } } if (!update) break; if (i > n) { negCycle = true; break; } } return std::make_pair(dist, !negCycle); } std::pair<std::vector<Weight>, bool> bellmanFord(const Graph& g, long long s, long long d) { long long n = g.size(); const Weight inf = std::numeric_limits<Weight>::max() / 8; Edges es; for (long long i = 0; i < n; i++) for (auto& e : g[i]) es.emplace_back(e); std::vector<Weight> dist(n, inf); dist[s] = 0; bool negCycle = false; for (long long i = 0; i < n * 2; i++) { bool update = false; for (auto& e : es) { if (dist[e.src] != inf && dist[e.dst] > dist[e.src] + e.weight) { dist[e.dst] = dist[e.src] + e.weight; update = true; if (e.dst == d && i == n * 2 - 1) negCycle = true; } } if (!update) break; } return std::make_pair(dist, !negCycle); } vector<long long> Manachar(string S) { long long len = S.length(); vector<long long> R(len); long long i = 0, j = 0; while (i < S.size()) { while (i - j >= 0 && i + j < S.size() && S[i - j] == S[i + j]) ++j; R[i] = j; long long k = 1; while (i - k >= 0 && i + k < S.size() && k + R[i - k] < j) R[i + k] = R[i - k], ++k; i += k; j -= k; } return R; } std::vector<long long> tsort(const Graph& g) { long long n = g.size(), k = 0; std::vector<long long> ord(n), in(n); for (auto& es : g) for (auto& e : es) in[e.dst]++; std::queue<long long> q; for (long long i = 0; i < n; ++i) if (in[i] == 0) q.push(i); while (q.size()) { long long v = q.front(); q.pop(); ord[k++] = v; for (auto& e : g[v]) { if (--in[e.dst] == 0) { q.push(e.dst); } } } return *std::max_element(in.begin(), in.end()) == 0 ? ord : std::vector<long long>(); } std::vector<Weight> dijkstra(const Graph& g, long long s) { const Weight INF = std::numeric_limits<Weight>::max() / 8; using state = std::tuple<Weight, long long>; std::priority_queue<state> q; std::vector<Weight> dist(g.size(), INF); dist[s] = 0; q.emplace(0, s); while (q.size()) { Weight d; long long v; std::tie(d, v) = q.top(); q.pop(); d *= -1; if (dist[v] < d) continue; for (auto& e : g[v]) { if (dist[e.dst] > dist[v] + e.weight) { dist[e.dst] = dist[v] + e.weight; q.emplace(-dist[e.dst], e.dst); } } } return dist; } Matrix WarshallFloyd(const Graph& g) { auto const INF = std::numeric_limits<Weight>::max() / 8; long long n = g.size(); Matrix d(n, Array(n, INF)); for (long long i = (0); i < (long long)(n); i++) d[i][i] = 0; for (long long i = (0); i < (long long)(n); i++) for (auto& e : g[i]) d[e.src][e.dst] = std::min(d[e.src][e.dst], e.weight); for (long long k = (0); k < (long long)(n); k++) for (long long i = (0); i < (long long)(n); i++) for (long long j = (0); j < (long long)(n); j++) { if (d[i][k] != INF && d[k][j] != INF) { d[i][j] = std::min(d[i][j], d[i][k] + d[k][j]); } } return d; } const long long BLACK = 1, WHITE = 0; bool isValid(vector<vector<long long>>& mapData, long long gyo, long long retu) { bool f = true; for (long long i = (0); i < (long long)(gyo); i++) { for (long long j = (0); j < (long long)(retu); j++) { long long colorCnt = 0; if (j > 0 && mapData[i][j] == mapData[i][j - 1]) { colorCnt++; } if (i > 0 && mapData[i][j] == mapData[i - 1][j]) { colorCnt++; } if (i < gyo - 1 && mapData[i][j] == mapData[i + 1][j]) { colorCnt++; } if (j < retu - 1 && mapData[i][j] == mapData[i][j + 1]) { colorCnt++; } if (colorCnt > 1) { f = false; } } } return f; } void getNext(long long nowX, long long nowY, long long* pOutX, long long* pOutY, long long gyo, long long retu) { if (nowX == retu - 1) { *pOutY = nowY + 1; *pOutX = 0; return; } *pOutX = nowX + 1; *pOutY = nowY; } void dfs(vector<vector<long long>> mapData, long long nowX, long long nowY, long long gyo, long long retu, long long* outCnt) { if (nowX == retu - 1 && nowY == gyo - 1) { mapData[nowY][nowX] = BLACK; if (isValid(mapData, gyo, retu)) { *outCnt = *outCnt + 1; } mapData[nowY][nowX] = WHITE; if (isValid(mapData, gyo, retu)) { *outCnt = *outCnt + 1; } return; } mapData[nowY][nowX] = BLACK; long long nextX, nextY; getNext(nowX, nowY, &nextX, &nextY, gyo, retu); dfs(mapData, nextX, nextY, gyo, retu, outCnt); mapData[nowY][nowX] = WHITE; getNext(nowX, nowY, &nextX, &nextY, gyo, retu); dfs(mapData, nextX, nextY, gyo, retu, outCnt); } void dec(map<long long, long long>& ma, long long a) { ma[a]--; if (ma[a] == 0) { ma.erase(a); } } long long N; long long solve(long long ans, vector<long long> A, vector<long long> cu) { for (long long i = (0); i < (long long)(N); i++) { if (cu[i] == 0) { ans++; if (i == 0) { if (cu[i + 1] < 0) { cu[i] = 1; } else { cu[i] = -1; } } else { if (cu[i - 1] < 0) { cu[i] = 1; } else { cu[i] = -1; } } } if (i == N - 1) { break; } if (cu[i] < 0 == cu[i + 1] < 0) { if (cu[i + 1] > 0) { ans += cu[i + 1] + 1; cu[i + 1] -= cu[i + 1] + 1; } else { ans += -cu[i + 1] + 1; cu[i + 1] += -cu[i + 1] + 1; } } cu[i + 2] = cu[i + 1] + A[i + 2]; } return ans; } signed main() { cin >> N; vector<long long> A(N + 2), A2; vector<long long> cu(N + 2); long long su = 0; for (long long i = (0); i < (long long)(N); i++) { cin >> A[i]; su += A[i]; cu[i] = su; } A2 = A; long long ans1 = 0, ans2 = 0; ans1 = solve(ans1, A, cu); if (A[0] < 0) { ans2 = -A[0] + 1; A[0] = 1; } else { ans2 = A[0] + 1; A[0] = -1; } su = 0; for (long long i = (0); i < (long long)(N); i++) { su += A2[i]; cu[i] = su; } ans2 = solve(ans2, A2, cu); cout << min(ans1, ans2) << "\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); vector<int> a1(n); vector<int> a2(n); vector<long long> sum1(n); vector<long long> sum2(n); for (int i = 0; i < n; i++) { cin >> a[i]; } a1 = a; a2 = a; int ans1 = 0; int ans2 = 0; for (int i = 0; i < n; i++) { if (i == 0) sum1[i] = a1[i]; else sum1[i] = sum1[i - 1] + a1[i]; if (i % 2 == 0 && sum1[i] <= 0) { a1[i] += (1 - sum1[i]); ans1 += (1 - sum1[i]); sum1[i] += (1 - sum1[i]); } else if (i % 2 == 1 && sum1[i] >= 0) { a1[i] -= (sum1[i] + 1); ans1 += (sum1[i] + 1); sum1[i] -= (1 + sum1[i]); } } if (sum1[n - 1] == 0) ans1++; for (int i = 0; i < n; i++) { if (i == 0) sum2[i] = a2[i]; else sum2[i] = sum2[i - 1] + a2[i]; if (i % 2 == 0 && sum2[i] >= 0) { a2[i] -= (1 + sum2[i]); ans2 += (sum2[i] + 1); sum2[i] -= (1 + sum2[i]); } else if (i % 2 == 1 && sum2[i] <= 0) { a2[i] += (1 - sum2[i]); ans2 += (1 - sum2[i]); sum2[i] += (1 - sum2[i]); } } if (sum2[n - 1] == 0) ans2++; if (ans1 >= ans2) cout << ans2 << endl; else cout << ans1 << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll N = 0; cin >> N; vector<ll> A(N, 0); for (ll i = 0; i < N; i++) { cin >> A.at(i); } ll ans = 0; ll ansa; vector<ll> sum(N, 0); vector<ll> suma(N, 0); if (A.at(0)) { sum.at(0) = A.at(0); ansa = abs(A.at(0) + (abs(A.at(0)) / A.at(0))); suma.at(0) = -1 * (abs(A.at(0)) / A.at(0)); } else { ans = 1; ansa = -1; sum.at(0) = 1; suma.at(0) = -1; } for (size_t i = 1; i < N; i++) { sum.at(i) = sum.at(i - 1) + A.at(i); if (sum.at(i) * sum.at(i - 1) < 0) { continue; } else { ans += abs(sum.at(i) + (abs(sum.at(i - 1)) / sum.at(i - 1))); sum.at(i) = -1 * (abs(sum.at(i - 1)) / sum.at(i - 1)); } } for (size_t i = 1; i < N; i++) { suma.at(i) = suma.at(i - 1) + A.at(i); if (suma.at(i) * suma.at(i - 1) < 0) { continue; } else { ansa += abs(suma.at(i) + (abs(suma.at(i - 1)) / suma.at(i - 1))); suma.at(i) = -1 * (abs(suma.at(i - 1)) / suma.at(i - 1)); } } cout << min(ans, ansa) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long mod2 = 998244353; const long long INF = 1e18; const long double EPS = 1e-10; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < (n); ++i) cin >> a[i]; long long ans = 0; if (a[0] == 0) { a[0] = 1; for (int i = 0; i < (n); ++i) { if (a[i] != 0) { if (a[i] > 0 && i % 2 == 1) a[i] = -1; if (a[i] < 0 && i % 2 == 0) a[i] = -1; break; } } ans++; } for (int i = 0; i < (n - 1); ++i) { a[i + 1] += a[i]; if (a[i] * a[i + 1] >= 0) { ans += abs(a[i + 1]) + 1; if (a[i] > 0) a[i + 1] = -1; else a[i + 1] = 1; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; set<string> c; map<ll, ll> mp; const ll inf = 100000000000000000; const ll mod = 1000000007; const ll mod2 = 998244353; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll c, ll d) { return c / gcd(c, d) * d; } int main() { ll n; cin >> n; vector<ll> a(n), sum(n + 1, 0); for (int i = 0; i < n; i++) cin >> a.at(i); for (int i = 0; i < n; i++) sum.at(i + 1) = a.at(i) + sum.at(i); ll ans = 0, res = 0, tmp = 0; for (int i = 2; i < n + 1; i++) { if ((sum.at(i) + tmp > 0 && sum.at(i - 1) < 0) || (sum.at(i) + tmp < 0 && sum.at(i - 1) > 0)) continue; if (sum.at(i) == 0) { if (sum.at(i - 1) > 0) { sum.at(i) = -1; tmp--; } else { sum.at(i) = 1; tmp++; } ans++; } else if (sum.at(i - 1) < 0) { sum.at(i) += abs(sum.at(i)) + 1; ans += abs(sum.at(i)) + 1; tmp += abs(sum.at(i)) + 1; } else { sum.at(i) -= sum.at(i) - 1; ans += sum.at(i) + 1; tmp -= sum.at(i) - 1; } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
n = gets.to_i ary = gets.split.map(&:to_i) #even > 0 sumE = ary[0] sumO = ary[0] > 0 ? -1 : 1 ansE = 0 ansO = ary[0].abs + 1 for i in 1 .. n - 1 if sumE * (sumE + ary[i]) >= 0 if sumE > 0 ansE += (- sumE - 1 - ary[i]).abs sumE = -1 else ansE += (- sumE + 1 - ary[i]).abs sumE = 1 end else sumE += ary[i] end if sumO * (sumO + ary[i]) >= 0 if sumO > 0 ansO += (- sumO - 1 - ary[i]).abs sumO = -1 else ansO += (- sumO + 1 - ary[i]).abs sumO = 1 end else sumO += ary[i] end end puts [ansE, ansO].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; #define mod 1000000007 #define ll long long #define mp make_pair #define pb push_back #define ff first #define ss second #define set0(a) memset ((a), 0 , sizeof(a)) #define set1(a) memset((a),-1,sizeof (a)) #define pi pair<int, int> #define ps pair<string, string> #define pl pair<long, long> #define pll pair<long long, long long> #define vll vector<long long> #define vl vector<long> #define vi vector<int> #define vs vector<string> #define vps vector< ps > #define vpi vector< pi > #define vpl vector< pl > #define vpll vector< pll > #define flash ios_base::sync_with_stdio(false); cin.tie(NULL); #define tc(t,T) for(long long t=0;t<T;t++) #define rep(i,s,n,d) for(long long i=s;i<n;i=i+d) bool sortbysec(const pll &a, const pll &b) { return (a.second < b.second); } void func(void) { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); } int main(){ ll n; cin>>n; ll a[n]; rep(i,0,n,1){ cin>>a[i]; } ll count1=0; if(a[0]==0){ if(a[1]>=0){ a[0]=-1; } else a[0]=1; count1++; } ll sum[n]={}; sum[0]=a[0]; rep(i,1,n,1){ sum[i]=sum[i-1]+a[i]; } ll sum1=a[0]; rep(i,1,n,1){ ll d=0; ll dif=0; if(sum1>0){ if(a[i]+sum1>=0){ d=-1; ll s=d-sum1; dif=abs(a[i]-s); count1=count1+dif; sum1=d; } else{ sum1=sum1+a[i]; } } else{ if(a[i]+sum1<=0){ d=1; ll s=d-sum1; dif=abs(a[i]-s); count1=count1+dif; sum1=d; } else{ sum1=sum1+a[i]; } } } cout<<count1<<endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <iostream> #include <string> #include <vector> #include <numeric> #include <queue> #include <unordered_map> #include <algorithm> #include <cmath> #include <iomanip> #include <sstream> #include <stack> #include <map> #include <set> #include <ios> #include <cctype> #include <cstdio> #include <functional> #include <cassert> #define REP(i,a) for(int i = 0;i < (a);++i) #define FOR(i,a,b) for(int i = (a);i < (b); ++i) #define FORR(i,a,b) for(int i = (a) - 1;i >=(b);--i) #define ALL(obj) (obj).begin(),(obj).end() #define SIZE(obj) (int)(obj).sizeT() #define YESNO(cond,yes,no){cout <<((cond)?(yes):(no))<<endl; } #define SORT(list) sort(ALL((list))); #define RSORT(list) sort((list).rbegin(),(list).rend()) #define ASSERT(cond,mes) assert(cond && mes) using namespace std; using ll = long long; constexpr int MOD = 1'000'000'007; constexpr int INF = 1'050'000'000; template<typename T> T round_up(const T& a, const T& b) { return (a + (b - 1)) / b; } template <typename T1, typename T2> istream& operator>>(istream& is, pair<T1, T2>& p) { is >> p.first >> p.second; return is; } template <typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1, T2>& p) { os << p.first << p.second; return os; } template <typename T> istream& operator>>(istream& is, vector<T>& v) { REP(i, (int)v.size())is >> v[i]; return is; } template <typename T> ostream& operator<<(ostream& os, vector<T>& v) { REP(i, (int)v.size())os << v[i] << endl; return os; } template <typename T> T clamp(T& n, T a, T b) { if (n < a)n = a; if (n > b)n = b; return n; } template <typename T> static T GCD(T u, T v) { T r; while (v != 0) { r = u % v; u = v; v = r; } return u; } template <typename T> static T LCM(T u, T v) { return u / GCD(u, v) * v; } template <typename T> static int sign(T t) { if (t > 0)return 1; else if (t < 0)return -1; else return 0; } int main() { cin.tie(0); ios::sync_with_stdio(false); std::cout << fixed << setprecision(20); int N; cin >> N; vector<int>A(N); cin >> A; ll s = A[0]; ll ans = 0; //先頭が0だった時の処理 if (s == 0) { auto it = find_if(ALL(A), [](int a) {return a != 0; }); int index = it - A.begin(); if (index % 2 == 0)s = 1; else s = -1; ans++; } FOR(i, 1, N) { ll next = s + A[i]; if (sign(next) == 0) { next = -sign(s); ans += 1; } else if (sign(s) == sign(next)) { ans += abs(next - (-sign(s))); next = -sign(s); } s = next; } 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); vector<long long> A2(N); vector<long long> A1(N); for (long long i = 0; i < N; i++) { cin >> A1[i]; A2[i] = A1[i]; A[i] = A1[i]; } long long cnt1 = 0; long long cnt2 = 0; long long S1 = 0; long long S2 = 0; for (long long i = 0; i < N; i++) { S1 += A1[i]; if (i % 2) { if (S1 <= 0) { A1[i] += 1 - S1; S1 = 1; } } else { if (S1 >= 0) { A1[i] += -1 - S1; S1 = -1; } } } for (long long i = 0; i < N; i++) { S2 += A2[i]; if (!i % 2) { if (S2 >= 0) { A2[i] += 1 - S2; S2 = 1; } } else { if (S2 >= 0) { A2[i] += -1 - S2; S2 = -1; } } } for (long long i = 0; i < N; i++) { cnt1 += abs(A[i] - A1[i]); cnt2 += abs(A[i] - A2[i]); } cout << min(cnt1, cnt2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int sign, n, a, cnt = 0, acm = 0; cin >> n >> a; acm = a; if (a >= 0) sign = 1; else sign = -1; for (int i = 1; i < n; i++) { cin >> a; if ((acm + a) * sign < 0) { acm += a; sign *= -1; } else { cnt += abs(acm + a) + 1; acm = -1 * sign; sign *= -1; } } cout << cnt << "\n"; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; const ll linf = 1LL << 62; const int inf = 999999; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; ll gcd(ll a, ll b) { if (a % b == 0) return b; else gcd(b, a % b); } ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; } int main() { int n; bool jud; ll ans = 0; vector<ll> v; cin >> n; for (int i = 0; i < n; i++) { ll a; cin >> a; v.push_back(a); } ll a = v[0]; if (a > 0) jud = true; else jud = false; for (int i = 1; i < n; i++) { if (jud) { a += v[i]; if (a < 0) jud = false; else { ans += abs(a) + 1; a -= abs(a) + 1; jud = false; } } else { a += v[i]; if (a > 0) jud = true; else { ans += abs(a) + 1; a += abs(a) + 1; jud = true; } } cout << a << endl; } 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())) #####segfunc###### def segfunc(x,y): return x+y def init(init_val): #set_val for i in range(len(init_val)): seg[i+num-1]=init_val[i] #built for i in range(num-2,-1,-1) : seg[i]=segfunc(seg[2*i+1],seg[2*i+2]) def update(k,x): k += num-1 seg[k] = x while k: k = (k-1)//2 seg[k] = segfunc(seg[k*2+1],seg[k*2+2]) def query(p,q): if q<=p: return ide_ele p += num-1 q += num-2 res=ide_ele while q-p>1: if p&1 == 0: res = segfunc(res,seg[p]) if q&1 == 1: res = segfunc(res,seg[q]) q -= 1 p = p//2 q = (q-1)//2 if p == q: res = segfunc(res,seg[p]) else: res = segfunc(segfunc(res,seg[p]),seg[q]) return res #####単位元###### ide_ele = 0 num =2**(n-1).bit_length() seg=[ide_ele]*(2*num - 1) init(a) ans_1 = 0 pre_sum = (-1) * a[0] for i in range(n): q_sum = query(0,i+1) if q_sum * pre_sum >= 0: if pre_sum < 0: update(i, abs(pre_sum) + 1) else: update(i, (-1) * (pre_sum+1)) pre_sum = query(0,i+1) for i in range(n): ans_1 += abs(a[i] - seg[i + num - 1]) if a[0] == 0: a[0] = 1 ans_2 = 1 else: ans_2 = abs(a[0] * 2) a[0] *= -1 pre_sum = (-1) * a[0] init(a) for i in range(n): q_sum = query(0,i+1) if q_sum * pre_sum >= 0: if pre_sum < 0: update(i, abs(pre_sum) + 1) else: update(i, (-1) * (pre_sum+1)) pre_sum = query(0,i+1) for i in range(n): ans_2 += abs(a[i] - seg[i + num - 1]) print(min(ans_1,ans_2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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 s1, s2, c1, c2; int main() { int n, a; scanf("%d", n); for (int i = 1; i <= n; i++) { scanf("%d", a); s1 += a; s2 += a; if (i % 2) { if (s1 <= 0) c1 += 1 - s1, s1 = 1; if (s2 >= 0) c2 += 1 + s2, s2 = -1; } else { if (s1 >= 0) c1 += 1 + s1, s1 = -1; if (s2 <= 0) c2 += 1 - s2, s2 = 1; } } cout << min(c1, c2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) ans = 10**10 # 1 -1 1 -1... cost1 = 0 s = 0 for i in range(n): t = 1 if i % 2 == 0 else -1 c = 0 if i % 2 == 0: if s + a[i] > 0: s += a[i] else: c = abs(s - t) s += c cost1 += abs(c - a[i]) else: if s + a[i] < 0: s += a[i] else: c = abs(s - t) s -= c cost1 += abs(c - a[i]) cost2 = 0 s = 0 for i in range(n): t = -1 if i % 2 == 0 else 1 c = 0 if i % 2 == 1: if s + a[i] > 0: s += a[i] else: c = abs(s - t) s += c cost2 += abs(c - a[i]) else: if s + a[i] < 0: s += a[i] else: c = abs(s - t) s -= c cost2 += abs(- c - a[i]) print(min(cost1, cost2)) # -1 1 -1 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; constexpr long long inf = 1e9 + 7; int main() { cin.tie(0); ios::sync_with_stdio(false); long long N; cin >> N; long long sum; cin >> sum; long long ans = 0; for (long long n = 1; n < N; n++) { long long A; cin >> A; if (sum < 0 && sum + A <= 0) { ans += 1 - sum - A; A = 1 - sum; } else if (sum > 0 && sum + A >= 0) { ans += A + 1 + sum; A = -1 - sum; } sum += A; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const long long maxLL = (long long)1 << 62; long long a[100001] = {}; long long s[100001] = {}; int main() { long long n; cin >> n; long long cnt = 0; for (long long i = 1; i <= n; i++) { cin >> a[i]; } for (long long i = 1; i <= n; i++) { s[i] = s[i - 1] + a[i]; if (i > 1) { if (s[i] == 0) { if (s[i - 1] > 0) s[i] = -1; else if (s[i - 1] < 0) s[i] = 1; cnt += abs(s[i]) + 1; } else if (i > 1 && s[i - 1] * s[i] > 0) { cnt += abs(s[i]) + 1; if (s[i] > 0) s[i] -= abs(s[i]) + 1; else if (s[i] < 0) s[i] += abs(s[i]) + 1; } } } cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; bool debug = false; int main() { int n; long long a[100005]; long long cnt = 0; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; long long sum = a[0]; bool plus; if (sum >= 0) plus = true; else plus = false; for (int i = 1; i < n; i++) { sum += a[i]; if (debug) cout << "sum:" << sum << endl; if (plus) { if (sum >= 0) { cnt += sum + 1; sum = -1; } plus = false; } else { if (sum <= 0) { cnt += abs(sum) + 1; sum = 1; } plus = true; } } cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; long long a[100006]; long long ans = 0; long long sum = 0; int i; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%lld", &a[i]); sum = a[0]; for (i = 1; i < n; i++) { if (sum > 0) { if (sum + a[i] >= 0) { ans += a[i] + sum + 1; sum = -1; } else { sum += a[i]; } } else { if (sum + a[i] <= 0) { ans += -sum + 1 - a[i]; sum = 1; } else { sum += a[i]; } } } 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> #pragma GCC optimize("Ofast") using namespace std; using ll = long long; signed main() { int n; cin >> n; ll a[n]; int count = 0; for (ll i = 0; i < ll(n); i++) { cin >> a[i]; } ll total = 0; for (ll i = 0; i <= ll(n - 2); i++) { total += a[i]; ll total_1 = total; if (total > 0) { while (true) { if (total_1 + a[i + 1] < 0) { break; } a[i + 1]--; count++; } } else { while (true) { if (total_1 + a[i + 1] > 0) { break; } a[i + 1]++; count++; } } } cout << count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; vector<int> a(n); for (int i=0; i<n; i++) cin >> a[i]; int odd = 0; even = 0; sum = 0; for (int i=0; i<n; i++){ sum += a[i]; if(i%2==0&&sum<=0){ odd += abs(sum)+1; sum = +1; } else if(i%2==1&&sum>=0){ odd += abs(sum)+1; sum = -1; } } sum = 0; for (int i=0; i<n; i++){ sum += a[i]; if (i%2==1&&sum<=0){ even += abs(sum)+1; sum = +1; } else if(i%2==0&&sum>=0){ even+=abs(sum)+1; sum=-1; } } cout << min(odd,even) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
from sys import stdout printn = lambda x: stdout.write(str(x)) inn = lambda : int(input()) inl = lambda: list(map(int, input().split())) inm = lambda: map(int, input().split()) ins = lambda : input().strip() DBG = True # and False BIG = 999999999 R = 10**9 + 7 def ddprint(x): if DBG: print(x) n = inn() a = inl() ne = sum(a[::2]) no = sum(a[1::2]) p0 = (ne>no) cnt = 0 acc = a[0] for i in range(1,n): if (p0 if i%2==0 else not p0): x = max(0, 1-a[i]-acc) cnt += x acc += a[i]+x else: x = max(0, a[i]+acc+1) cnt += x acc += a[i]-x print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
from sys import stdin import sys import math n = int(input()) a = list(map(int, stdin.readline().rstrip().split())) pair = 0 odd = 0 for i in range(0, len(a), 2): pair += a[i] for i in range(1, len(a), 2): odd += a[i] #print(odd) #print(pair) count = 0 current_sum = 0 for i in range(len(a)): ## odd if i % 2 == 1 and odd > pair: while current_sum + a[i] < 1: a[i] += 1 count += 1 ## odd elif i % 2 == 1 and odd < pair: while current_sum + a[i] > -1: a[i] -= 1 count += 1 ## pair elif i % 2 == 0 and odd > pair: while current_sum + a[i] > -1: a[i] -= 1 count += 1 elif i % 2 == 0 and odd < pair: while current_sum + a[i] < 1: a[i] += 1 count += 1 else: print("error") current_sum += a[i] print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } const int inf = INT_MAX / 2; const long long infl = 1LL << 60; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } enum PosiNega { POSITIVE = 0, NEGATIVE = 1 }; int solve(int N, long long *a, PosiNega odd_posinega) { long long ans = 0; long long sum = 0; PosiNega posi_nega = odd_posinega; for (int i = 0; i < N; i++) { sum += a[i]; if (POSITIVE == posi_nega) { if (0 >= sum) { ans += 1 - sum; sum = 1; } posi_nega = NEGATIVE; } else { if (0 <= sum) { ans += 1 + sum; sum = -1; } posi_nega = POSITIVE; } } return ans; } void _main() { int N; cin >> N; long long a[N]; for (int i = 0; i < N; i++) cin >> a[i]; long long ans = min(solve(N, a, POSITIVE), solve(N, a, NEGATIVE)); cout << ans << "\n"; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> a(N); for (int i = (0); i < (N); ++i) { cin >> a.at(i); } int ans = 0; int S = a.at(0); int start = 0; while (start < N && a.at(start) == 0) { start++; } if (start != 0) { ans = start * 2 - 1; if (start < N) { if (a.at(start) > 0) S = -1; else S = 1; } } for (int i = (start + 1); i < (N); ++i) { if (S > 0) { S += a.at(i); if (S >= 0) { ans += (S + 1); S = -1; } } else { S += a.at(i); if (S <= 0) { ans += (-S + 1); S = 1; } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using i8 = int8_t; using u8 = uint8_t; using i16 = int16_t; using u16 = uint16_t; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; using f32 = float; using f64 = double; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } template <typename C> i64 SIZE(const C &c) { return static_cast<i64>(c.size()); } template <typename T, size_t N> i64 SIZE(const T (&)[N]) { return static_cast<i64>(N); } struct ProconInit { static constexpr int IOS_PREC = 15; static constexpr bool AUTOFLUSH = false; ProconInit() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(IOS_PREC); if (AUTOFLUSH) cout << unitbuf; } } PROCON_INIT; int main() { i64 N; cin >> N; vector<i64> A(N); for (i64 i = (0), ixxxx_end = (N); i < ixxxx_end; ++i) cin >> A[i]; auto solve = [&](bool pos) { i64 cnt = 0; i64 a = A[0]; if (pos && A[0] <= 0) a = 1 - A[0]; if (!pos && A[0] >= 0) a = A[0] + 1; cnt += abs(A[0] - a); i64 cum = a; for (i64 i = 1; i <= N - 1; ++i) { i64 nc = cum + A[i]; i64 d = 0; if (cum > 0 && nc >= 0) d = nc + 1; if (cum < 0 && nc <= 0) d = 1 - nc; nc -= d; cnt += d; cum = nc; } return cnt; }; cout << min(solve(true), solve(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; const 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 sum = a[0]; long long ans1 = 0; for (int i = 0; i < n - 1; ++i) { if (sum < 0) { if (a[i + 1] + sum > 0) sum += a[i + 1]; else { ans1 += abs(1 - (a[i + 1] + sum)); sum = 1; } } else { if (sum == 0) { ans1++; sum += 1; } if (a[i + 1] + sum < 0) sum += a[i + 1]; else { ans1 += abs(-1 - (a[i + 1] + sum)); sum = -1; } } } sum = a[0]; long long ans2 = 0; for (int i = 0; i < n - 1; ++i) { if (sum == 0) { ans2++; sum = -1; continue; } if (sum < 0) { if (a[i + 1] + sum > 0) sum += a[i + 1]; else { ans2 += abs(1 - (a[i + 1] + sum)); sum = 1; } } else { if (a[i + 1] + sum < 0) sum += a[i + 1]; else { ans2 += abs(-1 - (a[i + 1] + sum)); sum = -1; } } } cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.io.File; import java.io.IOException; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Deque; import java.util.List; import java.util.Scanner; public class Main { //ABC059 public static void main(String[] args) throws IOException { //File file = new File("input.txt"); //Scanner in = new Scanner(file); Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; int[] sum = new int[n]; int ans = 0; for(int i = 0; i < n; i++){ a[i] = in.nextInt(); if(i == 0) sum[0] = a[0]; else sum[i] = sum[i-1] + a[i]; } /* for(int j = 0; j < n; j++) System.out.print(sum[j] + " "); System.out.println(); */ if(sum[0] == 0){ if(sum[1] > 0){ for(int i = 0; i < n; i++) sum[i]--; ans++; }else{ for(int i = 0; i < n; i++) sum[i]++; ans++; } } int diff_0 = 0; for(int i = 1; i < n; i++){ sum[i] += diff_0; if(sum[i] == 0){ if(sum[i-1] < 0){ sum[i]++; diff_0++; ans++; }else{ sum[i]--; diff_0--; ans++; } } } /* for(int j = 0; j < n; j++) System.out.print(sum[j] + " "); System.out.println(); */ int diff = 0; for(int i = 1; i < n; i++){ sum[i] += diff; /* System.out.print(i + ":"); for(int j = 0; j < n; j++) System.out.print(sum[j] + " "); System.out.println(); */ if(sum[i-1] < 0 && sum[i] <= 0){ int d = - sum[i] + 1; sum[i] += d; diff += d; ans += Math.abs(d); }else if(sum[i-1] > 0 && sum[i] >= 0){ int d = - sum[i] - 1; sum[i] += d; diff += d; ans += Math.abs(d); } } /* System.out.println(); for(int i = 0; i < n; i++) System.out.print(sum[i] + " "); System.out.println(); */ 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 <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v(n); cin >> v[0]; for (int i = (1); (i) < (n); (i)++) { cin >> v[i]; } int result = 0; for (int i = (1); (i) < (n); (i)++) { v[i] += v[i - 1]; if (v[i] * v[i - 1] >= 0) { result += abs(v[i]) + 1; v[i] = ((v[i - 1] > 0) ? -1 : 1); } } cout << result << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 0x7fffffff; const int maxn = 1e5 + 10; int a[maxn]; int n; long long cal() { long long t = a[0], ans = 0; for (int i = 1; i < n; ++i) { if (t < 0) { t += a[i]; if (t <= 0) { ans += 1 - t; t = 1; } continue; } t += a[i]; if (t >= 0) { ans += t + 1; t = -1; } } return ans; } int main() { scanf("%d", &n); for (int i = 0; i < (n); ++i) { scanf("%d", &a[i]); } long long ans1 = 0, ans2 = 0, ans3 = 0, ans = 0; int t = a[0]; if (t == 0) { a[0] = 1; ans1 = cal(); a[0] = -1; ans2 = cal(); ans = min(ans1, ans2) + 1; } else { ans = cal(); } 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; long int arr[n]; for(int i=0;i<n;i++) cin>>arr[i]; long int sum=arr[0]; long int ans=0; for(int i=1;i<n;i++) { if(sum<0) { sum=sum+arr[i]; if(sum>0) continue; else { ans+=abs(sum)+1; sum=1; } } else if { sum+=arr[i]; if(sum<0) continue; else { ans+=sum+1; sum=-1; } } } cout<<ans; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
import core.bitop, std.algorithm, std.ascii, std.bigint, std.conv, std.math, std.functional, std.numeric, std.range, std.stdio, std.string, std.random, std.typecons, std.container, std.format; // dfmt off T lread(T = long)(){return readln.chomp.to!T();} T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();} T[] aryread(T = long)(){return readln.split.to!(T[])();} void scan(TList...)(ref TList Args){auto line = readln.split(); foreach (i, T; TList){T val = line[i].to!(T);Args[i] = val;}} alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7; alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less); // dfmt on void main() { long N = lread(); auto A = aryread(); long ans = long.max; { auto B = A.dup; long sum = B[0]; long cost; if (sum == 0) { sum = -1; cost++; } foreach (i; 1 .. N) { if (0 < sum) { if (sum + B[i] < 0) { sum += B[i]; continue; } long x = -1 - B[i] - sum; cost += abs(x); B[i] += x; sum += B[i]; } else { if (0 < sum + B[i]) { sum += B[i]; continue; } long x = 1 - B[i] - sum; cost += abs(x); B[i] += x; sum += B[i]; } } // writeln(cost); // writeln(B); ans = ans.min(cost); } { auto B = A.dup; long cost; if (0 < B[0]) { cost += 1 + B[0]; B[0] -= 1 + B[0]; } else { cost += abs(1 - B[0]); B[0] += 1 - B[0]; } long sum = B[0]; foreach (i; 1 .. N) { if (0 < sum) { if (sum + B[i] < 0) { sum += B[i]; continue; } long x = -1 - B[i] - sum; cost += abs(x); B[i] += x; sum += B[i]; } else { if (0 < sum + B[i]) { sum += B[i]; continue; } long x = 1 - B[i] - sum; cost += abs(x); B[i] += x; sum += B[i]; } } // writeln(cost); // writeln(B); ans = ans.min(cost); } writeln(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(void) { int n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; i++) cin >> a[i]; long long sum = a[0]; long long ans1 = 0; for (long long i = 0; i < n; i++) { if (i % 2 == 0) { if (sum > 0) { ans1 += sum + 1; sum = -1; } } else { if (sum < 0) { ans1 += -sum + 1; sum = 1; } } if (i != n - 2) { sum += a[i + 1]; } } sum = a[0]; long long ans2 = 0; for (long long i = 0; i < n; i++) { if (i % 2 == 0) { if (sum < 0) { ans2 += -sum + 1; sum = 1; } } else { if (sum > 0) { ans2 += sum + 1; sum = -1; } } if (i != n - 2) { sum += a[i + 1]; } } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int main(int argc, char const *argv[]) { int n; ll a[100010], cnt = 0; std::cin >> n; for (int i = 0; i < n; i++) std::cin >> a[i]; if (a[0] > 0) { ll sum = a[0]; for (int i = 1; i < n; i++) { sum += a[i]; if (i % 2 == 1 && sum >= 0) { while (sum >= 0) { sum--; cnt++; } } else if (i % 2 == 0 && sum <= 0) { while (sum <= 0) { sum++; cnt++; } } } } else if (a[0] <= 0) { ll sum = a[0]; for (int i = 1; i < n; i++) { sum += a[i]; if (i % 2 == 1 && sum <= 0) { while (sum <= 0) { sum++; cnt++; } } else if (i % 2 == 0 && sum >= 0) { while (sum >= 0) { sum--; cnt++; } } } } std::cout << cnt << '\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> v(n); for (auto &i : v) cin >> i; long long ansq = 1e17; for (int x = 0; x < 2; x++) { int h = x; long long int sum = 0, ans = 0; for (int i = 0; i < n; i++) { sum += v[i]; if (h) { if (sum <= 0) ans += abs(1 - sum); } else { if (sum >= 0) { ans += abs(sum + 1); } } h = h ^ 1; } ansq = min(ansq, ans); } cout << ansq << "\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; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } template <class T> inline T sqr(T x) { return x * x; } const double EPS = 1e-10; const double PI = acos(-1.0); const long long mod = 1000000007; int main() { int N; cin >> N; vector<long long> A(N), B; for (int i = (0); i < (N); ++i) { cin >> A[i]; } B = A; long long sum = A[0]; long long num = 0; for (int i = 1; i < N; i++) { if (sum > 0) { if (sum + A[i] >= 0) { num += abs(A[i] + sum) + 1; sum = -1; } else { sum += A[i]; } } else if (sum < 0) { if (sum + A[i] <= 0) { num += abs(A[i] + sum) + 1; sum = 1; } else { sum += A[i]; } } } cout << num << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def main(): n=II() aa=LI() cs=aa[0] ans=0 for a in aa[1:]: if cs>0: if a>=-cs: ans+=abs(-cs-1-a) cs=-1 else:cs+=a else: if a <= -cs: ans += abs(-cs + 1 - a) cs = 1 else: cs += a print(ans) main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18 + 18; const long long MAX = 100005; const long long MOD = 1000000007; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } template <typename T1, typename T2> ostream &operator<<(ostream &s, const pair<T1, T2> &p) { return s << "(" << p.first << ", " << p.second << ")"; } template <typename T> istream &operator>>(istream &i, vector<T> &v) { for (long long j = (0); j < (v.size()); j++) i >> v[j]; return i; } template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) { int len = v.size(); for (int i = 0; i < len; ++i) { s << v[i]; if (i < len - 1) s << " "; } return s; } template <typename T> ostream &operator<<(ostream &s, const vector<vector<T>> &vv) { int len = vv.size(); for (int i = 0; i < len; ++i) { s << vv[i] << endl; } return s; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << std::setprecision(10); long long n; cin >> n; vector<long long> a(n); cin >> a; vector<long long> s(n + 1); for (long long i = (1); i < (n + 1); i++) { s[i] = s[i - 1] + a[i - 1]; } long long x = 0; long long cnt = 0; long long ans = INF; for (long long i = (1); i < (n + 1); i++) { if (i % 2 == 0) { if (s[i] + x <= 0) { cnt += 1 - (s[i] + x); x += 1 - (s[i] + x); } } else { if (s[i + x] >= 0) { cnt += s[i] + x + 1; x -= s[i] + x + 1; } } } chmin(ans, cnt); x = 0; cnt = 0; for (long long i = (1); i < (n + 1); i++) { if (i % 2 == 1) { if (s[i] + x <= 0) { cnt += 1 - (s[i] + x); x += 1 - (s[i] + x); } } else { if (s[i] + x >= 0) { cnt += s[i] + x + 1; x -= s[i] + x + 1; } } } chmin(ans, cnt); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.io.*; import java.util.*; public class Main { static StringBuilder sb = new StringBuilder(); static FastScanner sc = new FastScanner(System.in); static int INF = 12345678; static long MOD = 1000000007; static int[] y4 = {0, 1, 0, -1}; static int[] x4 = {1, 0, -1, 0}; static int[] y8 = {0, 1, 0, -1, -1, 1, 1, -1}; static int[] x8 = {1, 0, -1, 0, 1, -1, 1, -1}; static long[] F;//factorial static boolean[] isPrime; static int[] primes; static char[][] map; static int N, M; static long T; static int[] A; public static void main(String[] args) { int n = sc.nextInt(); long[] a = sc.nextLongArray(n); long ans_1 = 0;//初期値正 long ans_2 = 0; long sum_1 = a[0]; long sum_2 = a[0]; for (int i = 1; i < n; i++) { if(i%2==1){ if(sum_1 + a[i] >= 0){ ans_1 += abs(sum_1 + a[i]) + 1; sum_1 = -1; }else{ sum_1 += a[i]; } if(sum_2 + a[i] <= 0){ ans_2 += abs(sum_2 + a[i]) + 1; sum_2 = 1; }else{ sum_2 += a[i]; } }else{ if(sum_2 + a[i] >= 0){ ans_2 += abs(sum_2 + a[i]) + 1; sum_2 = -1; }else{ sum_2 += a[i]; } if(sum_1 + a[i] <= 0){ ans_1 += abs(sum_1 + a[i]) + 1; sum_1 = 1; }else{ sum_1 += a[i]; } } } System.out.println(min(ans_1,ans_2)); } static class Dijkstra { long initValue = -1; Node[] nodes; int n; long[] d; Dijkstra(int n) { this.n = n; nodes = new Node[n]; for (int i = 0; i < n; i++) nodes[i] = new Node(i); d = new long[n]; Arrays.fill(d, initValue); } Dijkstra(int n, int edge, boolean isDirectedGraph) { this.n = n; nodes = new Node[n]; for (int i = 0; i < n; i++) nodes[i] = new Node(i); d = new long[n]; Arrays.fill(d, initValue); if (isDirectedGraph) { for (int ei = 0; ei < edge; ei++) { int f = sc.nextInt() - 1; int t = sc.nextInt() - 1; long c = sc.nextLong(); addEdge(f, t, c); } } else { for (int ei = 0; ei < edge; ei++) { int f = sc.nextInt() - 1; int t = sc.nextInt() - 1; long c = sc.nextLong(); addEdge(f, t, c); addEdge(t, f, c); } } } void addEdge(int f, int t, long c) { nodes[f].edges.add(new Edge(t, c)); } long[] solve(int s) { d[s] = 0; //最短距離と頂点を持つ PriorityQueue<Dis> q = new PriorityQueue<>(); q.add(new Dis(s, 0)); while (!q.isEmpty()) { Dis now = q.poll(); int nowId = now.p; long nowC = now.cos; for (Edge edge : nodes[nowId].edges) { int to = edge.toId; long needsCost = edge.toCost + nowC; if (d[to] == initValue || needsCost < d[to]) { d[to] = needsCost; q.add(new Dis(to, needsCost)); } } } return d; } //O( E ^ 2) 辺が密の時用 long[] solve2(int s) { boolean[] used = new boolean[n]; long[][] cost = new long[n][n]; Main.fill(cost, initValue); Arrays.fill(d, initValue); d[s] = 0; for (Node node : nodes) { for (Edge edge : node.edges) { int fromId = node.id; int toId = edge.toId; long toCost = edge.toCost; cost[fromId][toId] = toCost; } } while (true) { int v = -1; //まだ使われていない頂点のうち、距離が最小のものを探す。 for (int u = 0; u < n; u++) if (!used[u] && (v == -1 || d[u] < d[v])) v = u; if (v == -1) break; used[v] = true; for (int u = 0; u < n; u++) d[u] = Math.min(d[u], d[v] + cost[v][u]); } return d; } static class Dis implements Comparable<Dis> { //現在地点 最短距離 int p; long cos; Dis(int p, long cost) { this.p = p; cos = cost; } public int compareTo(Dis d) { if (cos != d.cos) { if (cos > d.cos) return 1; else if (cos == d.cos) return 0; else return -1; } else { return p - d.p; } } } static class Node { int id; List<Edge> edges; Node(int id) { edges = new ArrayList<>(); this.id = id; } } static class Edge { int toId; long toCost; Edge(int id, long cost) { toId = id; toCost = cost; } } } public static long toLong(int[] ar) { long res = 0; for (int i : ar) { res *= 10; res += i; } return res; } public static int toInt(int[] ar) { int res = 0; for (int i : ar) { res *= 10; res += i; } return res; } //k個の次の組み合わせをビットで返す 大きさに上限はない 110110 -> 111001 public static int nextCombSizeK(int comb, int k) { int x = comb & -comb; //最下位の1 int y = comb + x; //連続した下の1を繰り上がらせる return ((comb & ~y) / x >> 1) | y; } public static int keta(long num) { int res = 0; while (num > 0) { num /= 10; res++; } return res; } public static long getHashKey(int a, int b) { return (long) a << 32 | b; } public static boolean isOutofIndex(int x, int y) { if (x < 0 || y < 0) return true; if (map[0].length <= x || map.length <= y) return true; return false; } public static void setPrimes() { int n = 100001; isPrime = new boolean[n]; List<Integer> prs = new ArrayList<>(); Arrays.fill(isPrime, true); isPrime[0] = isPrime[1] = false; for (int i = 2; i * i <= n; i++) { if (!isPrime[i]) continue; prs.add(i); for (int j = i * 2; j < n; j += i) { isPrime[j] = false; } } primes = new int[prs.size()]; for (int i = 0; i < prs.size(); i++) primes[i] = prs.get(i); } public static void revSort(int[] a) { Arrays.sort(a); reverse(a); } public static void revSort(long[] a) { Arrays.sort(a); reverse(a); } public static int[][] copy(int[][] ar) { int[][] nr = new int[ar.length][ar[0].length]; for (int i = 0; i < ar.length; i++) for (int j = 0; j < ar[0].length; j++) nr[i][j] = ar[i][j]; return nr; } /** * <h1>指定した値以上の先頭のインデクスを返す</h1> * <p>配列要素が0のときは、0が返る。</p> * * @return<b>int</b> : 探索した値以上で、先頭になるインデクス * 値が無ければ、挿入できる最小のインデックス */ public static int lowerBound(final int[] arr, final int value) { int low = 0; int high = arr.length; int mid; while (low < high) { mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策) if (arr[mid] < value) { low = mid + 1; } else { high = mid; } } return low; } /** * <h1>指定した値より大きい先頭のインデクスを返す</h1> * <p>配列要素が0のときは、0が返る。</p> * * @return<b>int</b> : 探索した値より上で、先頭になるインデクス * 値が無ければ、挿入できる最小のインデックス */ public static int upperBound(final int[] arr, final int value) { int low = 0; int high = arr.length; int mid; while (low < high) { mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策) if (arr[mid] <= value) { low = mid + 1; } else { high = mid; } } return low; } /** * <h1>指定した値以上の先頭のインデクスを返す</h1> * <p>配列要素が0のときは、0が返る。</p> * * @return<b>int</b> : 探索した値以上で、先頭になるインデクス * 値がなければ挿入できる最小のインデックス */ public static long lowerBound(final long[] arr, final long value) { int low = 0; int high = arr.length; int mid; while (low < high) { mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策) if (arr[mid] < value) { low = mid + 1; } else { high = mid; } } return low; } /** * <h1>指定した値より大きい先頭のインデクスを返す</h1> * <p>配列要素が0のときは、0が返る。</p> * * @return<b>int</b> : 探索した値より上で、先頭になるインデクス * 値がなければ挿入できる最小のインデックス */ public static long upperBound(final long[] arr, final long value) { int low = 0; int high = arr.length; int mid; while (low < high) { mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策) if (arr[mid] <= value) { low = mid + 1; } else { high = mid; } } return low; } //次の順列に書き換える、最大値ならfalseを返す public static boolean nextPermutation(int A[]) { int len = A.length; int pos = len - 2; for (; pos >= 0; pos--) { if (A[pos] < A[pos + 1]) break; } if (pos == -1) return false; //posより大きい最小の数を二分探索 int ok = pos + 1; int ng = len; while (Math.abs(ng - ok) > 1) { int mid = (ok + ng) / 2; if (A[mid] > A[pos]) ok = mid; else ng = mid; } swap(A, pos, ok); reverse(A, pos + 1, len - 1); return true; } //次の順列に書き換える、最小値ならfalseを返す public static boolean prevPermutation(int A[]) { int len = A.length; int pos = len - 2; for (; pos >= 0; pos--) { if (A[pos] > A[pos + 1]) break; } if (pos == -1) return false; //posより小さい最大の数を二分探索 int ok = pos + 1; int ng = len; while (Math.abs(ng - ok) > 1) { int mid = (ok + ng) / 2; if (A[mid] < A[pos]) ok = mid; else ng = mid; } swap(A, pos, ok); reverse(A, pos + 1, len - 1); return true; } //↓nCrをmod計算するために必要。 ***factorial(N)を呼ぶ必要がある*** static long ncr(int n, int r) { if (n < r) return 0; else if (r == 0) return 1; factorial(n); return F[n] / (F[n - r] * F[r]); } static long ncr2(int a, int b) { if (b == 0) return 1; else if (a < b) return 0; long res = 1; for (int i = 0; i < b; i++) { res *= a - i; res /= i + 1; } return res; } static long ncrdp(int n, int r) { if (n < r) return 0; long[][] dp = new long[n + 1][r + 1]; for (int ni = 0; ni < n + 1; ni++) { dp[ni][0] = dp[ni][ni] = 1; for (int ri = 1; ri < ni; ri++) { dp[ni][ri] = dp[ni - 1][ri - 1] + dp[ni - 1][ri]; } } return dp[n][r]; } static long modNcr(int n, int r) { if (n < r) return 0; long result = F[n]; result = result * modInv(F[n - r]) % MOD; result = result * modInv(F[r]) % MOD; return result; } public static long modSum(long... lar) { long res = 0; for (long l : lar) res = (res + l % MOD) % MOD; return res; } public static long modDiff(long a, long b) { long res = a - b; if (res < 0) res += MOD; res %= MOD; return res; } public static long modMul(long... lar) { long res = 1; for (long l : lar) res = (res * l) % MOD; if (res < 0) res += MOD; res %= MOD; return res; } public static long modDiv(long a, long b) { long x = a % MOD; long y = b % MOD; long res = (x * modInv(y)) % MOD; return res; } static long modInv(long n) { return modPow(n, MOD - 2); } static void factorial(int n) { F = new long[n + 1]; F[0] = F[1] = 1; // for (int i = 2; i <= n; i++) // { // F[i] = (F[i - 1] * i) % MOD; // } // for (int i = 2; i <= 100000; i++) { F[i] = (F[i - 1] * i) % MOD; } for (int i = 100001; i <= n; i++) { F[i] = (F[i - 1] * i) % MOD; } } static long modPow(long x, long n) { long res = 1L; while (n > 0) { if ((n & 1) == 1) { res = res * x % MOD; } x = x * x % MOD; n >>= 1; } return res; } //↑nCrをmod計算するために必要 static int gcd(int n, int r) { return r == 0 ? n : gcd(r, n % r); } static long gcd(long n, long r) { return r == 0 ? n : gcd(r, n % r); } static <T> void swap(T[] x, int i, int j) { T t = x[i]; x[i] = x[j]; x[j] = t; } static void swap(int[] x, int i, int j) { int t = x[i]; x[i] = x[j]; x[j] = t; } public static void reverse(int[] x) { int l = 0; int r = x.length - 1; while (l < r) { int temp = x[l]; x[l] = x[r]; x[r] = temp; l++; r--; } } public static void reverse(long[] x) { int l = 0; int r = x.length - 1; while (l < r) { long temp = x[l]; x[l] = x[r]; x[r] = temp; l++; r--; } } public static void reverse(int[] x, int s, int e) { int l = s; int r = e; while (l < r) { int temp = x[l]; x[l] = x[r]; x[r] = temp; l++; r--; } } static int length(int a) { int cou = 0; while (a != 0) { a /= 10; cou++; } return cou; } static int length(long a) { int cou = 0; while (a != 0) { a /= 10; cou++; } return cou; } static int cou(boolean[] a) { int res = 0; for (boolean b : a) { if (b) res++; } return res; } static int cou(String s, char c) { int res = 0; for (char ci : s.toCharArray()) { if (ci == c) res++; } return res; } static int countC2(char[][] a, char c) { int co = 0; for (int i = 0; i < a.length; i++) for (int j = 0; j < a[0].length; j++) if (a[i][j] == c) co++; return co; } static int countI(int[] a, int key) { int co = 0; for (int i = 0; i < a.length; i++) if (a[i] == key) co++; return co; } static int countI(int[][] a, int key) { int co = 0; for (int i = 0; i < a.length; i++) for (int j = 0; j < a[0].length; j++) if (a[i][j] == key) co++; return co; } static void fill(int[][] a, int v) { for (int i = 0; i < a.length; i++) for (int j = 0; j < a[0].length; j++) a[i][j] = v; } static void fill(long[][] a, long v) { for (int i = 0; i < a.length; i++) for (int j = 0; j < a[0].length; j++) a[i][j] = v; } static void fill(int[][][] a, int v) { for (int i = 0; i < a.length; i++) for (int j = 0; j < a[0].length; j++) for (int k = 0; k < a[0][0].length; k++) a[i][j][k] = v; } static int max(int... a) { int res = Integer.MIN_VALUE; for (int i : a) { res = Math.max(res, i); } return res; } static long max(long... a) { long res = Long.MIN_VALUE; for (long i : a) { res = Math.max(res, i); } return res; } static int max(int[][] ar) { int res = Integer.MIN_VALUE; for (int i[] : ar) res = Math.max(res, max(i)); return res; } static int min(int... a) { int res = Integer.MAX_VALUE; for (int i : a) { res = Math.min(res, i); } return res; } static long min(long... a) { long res = Long.MAX_VALUE; for (long i : a) { res = Math.min(res, i); } return res; } static int min(int[][] ar) { int res = Integer.MAX_VALUE; for (int i[] : ar) res = Math.min(res, min(i)); return res; } static int sum(int[] a) { int cou = 0; for (int i : a) cou += i; return cou; } static int abs(int a) { return Math.abs(a); } static long abs(long a) { return Math.abs(a); } static class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } /*public String nextChar(){ return (char)next()[0]; }*/ public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public int[] nextIntArrayDec(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt() - 1; } return a; } public int[][] nextIntArray2(int h, int w) { int[][] a = new int[h][w]; for (int hi = 0; hi < h; hi++) { for (int wi = 0; wi < w; wi++) { a[hi][wi] = nextInt(); } } return a; } public int[][] nextIntArray2Dec(int h, int w) { int[][] a = new int[h][w]; for (int hi = 0; hi < h; hi++) { for (int wi = 0; wi < w; wi++) { a[hi][wi] = nextInt() - 1; } } return a; } //複数の配列を受け取る public void nextIntArrays2ar(int[] a, int[] b) { for (int i = 0; i < a.length; i++) { a[i] = sc.nextInt(); b[i] = sc.nextInt(); } } public void nextIntArrays2arDec(int[] a, int[] b) { for (int i = 0; i < a.length; i++) { a[i] = sc.nextInt() - 1; b[i] = sc.nextInt() - 1; } } //複数の配列を受け取る public void nextIntArrays3ar(int[] a, int[] b, int[] c) { for (int i = 0; i < a.length; i++) { a[i] = sc.nextInt(); b[i] = sc.nextInt(); c[i] = sc.nextInt(); } } //複数の配列を受け取る public void nextIntArrays3arDecLeft2(int[] a, int[] b, int[] c) { for (int i = 0; i < a.length; i++) { a[i] = sc.nextInt() - 1; b[i] = sc.nextInt() - 1; c[i] = sc.nextInt(); } } public Integer[] nextIntegerArray(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public char[] nextCharArray(int n) { char[] a = next().toCharArray(); return a; } public char[][] nextCharArray2(int h, int w) { char[][] a = new char[h][w]; for (int i = 0; i < h; i++) { a[i] = next().toCharArray(); } return a; } //スペースが入っている場合 public char[][] nextCharArray2s(int h, int w) { char[][] a = new char[h][w]; for (int i = 0; i < h; i++) { a[i] = nextLine().replace(" ", "").toCharArray(); } return a; } public char[][] nextWrapCharArray2(int h, int w, char c) { char[][] a = new char[h + 2][w + 2]; //char c = '*'; int i; for (i = 0; i < w + 2; i++) a[0][i] = c; for (i = 1; i < h + 1; i++) { a[i] = (c + next() + c).toCharArray(); } for (i = 0; i < w + 2; i++) a[h + 1][i] = c; return a; } //スペースが入ってる時用 public char[][] nextWrapCharArray2s(int h, int w, char c) { char[][] a = new char[h + 2][w + 2]; //char c = '*'; int i; for (i = 0; i < w + 2; i++) a[0][i] = c; for (i = 1; i < h + 1; i++) { a[i] = (c + nextLine().replace(" ", "") + c).toCharArray(); } for (i = 0; i < w + 2; i++) a[h + 1][i] = c; return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public long[][] nextLongArray2(int h, int w) { long[][] a = new long[h][w]; for (int hi = 0; hi < h; hi++) { for (int wi = 0; wi < w; wi++) { a[hi][wi] = nextLong(); } } return a; } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long a[10000]; int getsign(long long int n) { if (n > 0) { return 1; } if (n < 0) { return -1; } return -1; } long long int count(int sign0, long long a[], int n) { long long int sum = 0; long long int sign = sign0; long long int count = 0; for (int i = 0; i < n; ++i) { sum += a[i]; if (getsign(sum) != sign) { count += abs(sign - sum); sum = sign; } sign = (sign * -1); } return count; } int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } cout << min(count(1, a, n), count(-1, a, n)) << 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
parseInt(x) = parse(Int, x) parseMap(x::Array{SubString{String},1}) = map(parseInt, x) function main() n = readline() |> parseInt a = readline() |> split |> parseMap b = zeros(Int,n) sb = 0 b[1] = a[1] for i in 2:n b[i]=b[i-1]+a[i] if b[i]<=0&&b[i-1]<=0 sb += 1-b[i] b[i] = 1 elseif b[i]>=0&&b[i-1]>=0 sb += b[i]+1 b[i] = -1 end end c = zeros(Int,n) sc = a[1]>0?a[1]+1:1-a[1] c[1] = a[1]>0?-1:1 for i in 2:n c[i]=c[i-1]+a[i] if c[i]<=0&&c[i-1]<=0 sc += 1-c[i] c[i] = 1 elseif c[i]>=0&&c[i-1]>=0 sc += c[i]+1 c[i] = -1 end end println(min(sb,sc)) end main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; bool is_odd(int n) { return (n % 2 == 1); } bool is_even(int n) { return (n % 2 == 0); } int main() { int n; cin >> n; vector<ll> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } ll ans = 0; bool is_positive = (a.at(0) > 0); bool is_negative = (a.at(0) < 0); ll sum = a.at(0); for (int i = 1; i < n; i++) { sum = sum + a.at(i); if (is_positive && is_odd(i) && sum >= 0) { ll num_of_operations = sum - (-1); ans += num_of_operations; sum -= num_of_operations; } if (is_positive && is_even(i) && sum <= 0) { ll num_of_operations = (1) - sum; ans += num_of_operations; sum += num_of_operations; } if (is_negative && is_odd(i) && sum <= 0) { ll num_of_operations = (1) - sum; ans += num_of_operations; sum += num_of_operations; } if (is_negative && is_even(i) && sum >= 0) { ll num_of_operations = sum - (-1); ans += num_of_operations; sum -= num_of_operations; } } 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; long long buf; cin >> N; vector<long long> aaa = vector<long long>(N + 1, 0); for (int i = 1; i < N + 1; i++) { cin >> buf; aaa.at(i) = buf + aaa.at(i - 1); } bool be = true; bool af; vector<long long> ans = vector<long long>(2, 0); long long change = 0; for (int l = 0; l < 2; l++) { vector<long long> aa = aaa; if (l == 0) { be = true; } else { be = false; } change = 0; for (int i = 1; i < N + 1; i++) { if (ans.at(1) > ans.at(0)) { break; } aa.at(i) += change; if (aa.at(i) == 0) { if (af) { change += 1 - aa.at(i); ans.at(l) += 1 - aa.at(i); be = false; } else { change += -(aa.at(i) + 1); ans.at(l) += 1 + aa.at(i); be = true; } } else { if (aa.at(i) < 0) { af = true; } else { af = false; } if (af == be) { if (af) { change += 1 - aa.at(i); ans.at(l) += 1 - aa.at(i); be = false; } else { change += -(aa.at(i) + 1); ans.at(l) += 1 + aa.at(i); be = true; } } else { be = af; } } } } long long tt = min(ans.at(0), ans.at(1)); std::cout << tt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(int,input().split())) import sys sum=a[0] cnt=0 if a[0]==0: sum+=1 cnt+=1 for i in range(1,n): if sum<0: z=sum+a[i] if z>0: sum=z elif z<=0: cnt+=(1-z) sum=1 elif sum>0: z=sum+a[i] if z>=0: cnt+=(z+1) sum=-1 elif z<0: sum=z cnt_plus=cnt sum=-1 cnt=1 for i in range(1,n): if sum<0: z=sum+a[i] if z>0: sum=z elif z<=0: cnt+=(1-z) sum=1 elif sum>0: z=sum+a[i] if z>=0: cnt+=(z+1) sum=-1 elif z<0: sum=z cnt_sbst=cnt print(min(cnt_plus,cnt_sbst)) sys.exit() for i in range(1,n): if sum<0: z=sum+a[i] if z>0: sum=z elif z<=0: cnt+=(1-z) sum=1 elif sum>0: z=sum+a[i] if z>=0: cnt+=(z+1) sum=-1 elif z<0: sum=z print(cnt) # 6 # 0 0 -1 3 5 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()] s0 = a[0] count=0 flag = False if a.count(0)==n: print(2*n+1) exit() i=0 while a[i]==0 and i<n: i+=1 flag=True if flag: if a[i]>0: if i%2==0:s0+=1 else:s0-=1 count+=1 else: if i%2==0:s0-=1 else:s0+=1 count+=1 for i in range(1,n): s1 = s0+a[i] if s0*s1>=0: if s1>0: a[i]-=(abs(s1)+1) count+=(abs(s1)+1) elif s1<0: a[i]+=(abs(s1)+1) count+=(abs(s1)+1) elif s1==0: if s0>0: a[i]-=1 count+=1 elif s0<0: a[i]+=1 count+=1 s0 += a[i] print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> unsigned long long N; unsigned long long M; std::vector<long long> Ai; std::vector<std::string> Bi; unsigned long long answer; void getInput(); unsigned long long calcAnswer(); int main() { getInput(); unsigned long long answer = calcAnswer(); std::cout << answer << std::endl; return EXIT_SUCCESS; } void getInput() { std::cin >> N; for (unsigned long long i = 0; i < N; ++i) { long long A; std::cin >> A; Ai.push_back(A); } } unsigned long long calcAnswer() { unsigned long long answer = 0; bool plus = false; bool minus = false; long long temp = Ai.at(0); if (temp < 0) { minus = true; } else { plus = true; } for (unsigned long long i = 1; i < N; ++i) { temp += Ai.at(i); if (plus) { plus = false; minus = true; while (temp >= 0) { --temp; ++answer; } continue; } if (minus) { plus = true; minus = false; while (temp <= 0) { ++temp; ++answer; } continue; } } return answer; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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 ans=0; int sum1 = sc.nextInt(); int sum2 = sum1; for(int i=0;i<n-1;i++) { sum2 += sc.nextInt(); if(sum1*sum2 > 0) { ans += Math.abs(sum2)+1; if(sum2 >0) sum2 = -1; else sum2 = 1; }else if(sum2 == 0) { ans += 1; if(sum1 > 0) sum2 = -1; else sum2 = 1; } sum1 = sum2; } 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 <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[200010] = {}; for (long long i = (0); i < (n); i++) cin >> a[i]; bool flag = true; for (long long i = (0); i < (n - 1); i++) { a[i + 1] += a[i]; if (a[i + 1] * a[i] >= 0) flag = false; } if (flag) { cout << 0 << endl; return 0; } long long tasu = 0, aa = 0, cnt = 0, minari = 1000000007; for (long long i = (0); i < (n); i++) { aa = 0; if (i % 2 == 0) { if (a[i] + tasu < 0) { minari = min(minari, abs(a[i] + tasu)); continue; } else if (a[i] + tasu > 0) { aa = abs(a[i]) + 1; } else { aa = 1; } cnt += aa; tasu -= aa; } else { if (a[i] + tasu > 0) { minari = min(minari, abs(a[i] + tasu)); continue; } else if (a[i] + tasu < 0) { aa = abs(a[i]) + 1; } else { aa = 1; } cnt += aa; tasu += aa; } minari = min(minari, abs(a[i] + tasu)); } cnt += minari - 1; long long cnt2 = 0; tasu = 0; minari = 1000000007; for (long long i = (0); i < (n); i++) { aa = 0; if (i % 2 != 0) { if (a[i] + tasu < 0) { minari = min(minari, abs(a[i] + tasu)); continue; } else if (a[i] + tasu > 0) { aa = abs(a[i]) + 1; } else { aa = 1; } cnt2 += aa; tasu -= aa; } else { if (a[i] + tasu > 0) { minari = min(minari, abs(a[i] + tasu)); continue; } else if (a[i] + tasu < 0) { aa = abs(a[i]) + 1; } else { aa = 1; } cnt2 += aa; tasu += aa; } minari = min(minari, abs(a[i] + tasu)); } cnt2 += minari - 1; cout << min(cnt, cnt2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.io.IOException; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException{ Sequence solver = new Sequence(); solver.readInput(); solver.solve(); solver.writeOutput(); } static class Sequence { private int n; private int a[]; private int output; private Scanner scanner; public Sequence() { this.scanner = new Scanner(System.in); } public void readInput() { n = Integer.parseInt(scanner.next()); a = new int[n]; for(int i=0; i<n; i++) { a[i] = Integer.parseInt(scanner.next()); } } private int count(boolean sign) { int count=0; long sum=0; for(int i=0; i<n; i++) { sum += a[i]; if((i%2==0) == sign) { // a[i]までの合計を正にするとき if(sum<=0) { count += Math.abs(sum)+1; sum = 1; } } else if((i%2==0) != sign){ // a[i]までの合計を負にするとき if(0<=sum) { count += Math.abs(sum)+1; sum = -1; } } } return count; } public void solve() { output = Math.min(this.count(true), this.count(false)); } public void writeOutput() { System.out.println(output); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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 total = 0; int count = 0; for (int i = 0; i < N; i++) { int current; cin >> current; if (total == 0) { total += current; } else if ((total > 0 && total + current < 0) || (total < 0 && total + current > 0)) { total += current; } else { if (total > 0) { count += total + 1 + current; total = -1; } else { count += abs(total) + 1 - current; total = 1; } } } cout << count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); long long a[n]; for (int i = 0; i < n; i++) scanf("%lld", a + i); long long initial_plus = ((-1 - a[0]) > (0) ? (-1 - a[0]) : (0)); long long initial_minus = ((1 + a[0]) > (0) ? (1 + a[0]) : (0)); long long sum = 0; sum = initial_plus + a[0]; for (int i = 1; i < n; i++) { if ((sum + a[i]) * sum >= 0ll) { if (sum < 0) { initial_plus += 1 - sum - a[i]; sum = 1; } else { initial_plus += sum + a[i] + 1; sum = -1; } } else { sum += a[i]; } } sum = a[0] - initial_minus; for (int i = 1; i < n; i++) { if ((sum + a[i]) * sum >= 0ll) { if (sum < 0) { initial_minus += 1 - sum - a[i]; sum = 1; } else { initial_minus += sum + a[i] + 1; sum = -1; } } else { sum += a[i]; } } printf("%lld\n", ((initial_plus) > (initial_minus) ? (initial_minus) : (initial_plus))); return 0; }