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() { long long n; cin >> n; vector<long long> a_sum(n); long long a; long long kiyu = 0; long long sum_m = 0; long long sum_p = 0; long long ans_m = 0; long long ans_p = 0; for (long long i = 0; i < n; i++) { cin >> a; kiyu += a; a_sum[i] = kiyu; } for (long long i = 0; i < n; i++) { if (i % 2 == 0) { if (a_sum[i] + sum_m >= 0) { ans_m = ans_m + (a_sum[i] + sum_m + 1); sum_m = sum_m - (a_sum[i] + sum_m + 1); } } else { if (a_sum[i] + sum_m <= 0) { ans_m = ans_m - (a_sum[i] + sum_m - 1); sum_m = sum_m - (a_sum[i] + sum_m - 1); } } } for (long long i = 0; i < n; i++) { if (i % 2 == 0) { if (a_sum[i] + sum_p <= 0) { ans_p = ans_p - (a_sum[i] + sum_p - 1); sum_p = sum_p - (a_sum[i] + sum_p - 1); } } else { if (a_sum[i] + sum_p >= 0) { ans_p = ans_p + (a_sum[i] + sum_p + 1); sum_p = sum_p + (a_sum[i] + sum_p + 1); } } } cout << min(ans_p, ans_m) << 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.at(i); } int sum = a.at(0); bool p = false; bool n = false; int num = 0; if (a.at(0) > 0) { p = true; for (int i = 1; i < N; i++) { sum += a.at(i); if (p) { while (sum >= 0) { sum--; num++; } p = false; n = true; continue; } if (n) { while (sum <= 0) { sum++; num++; } n = false; p = true; continue; } } } else if (a.at(0) == 0) { int num_1 = 0; int num_2 = 0; p = true; sum++; for (int i = 1; i < N; i++) { sum += a.at(i); if (p) { while (sum >= 0) { sum--; num_1++; } p = false; n = true; continue; } if (n) { while (sum <= 0) { sum++; num_1++; } n = false; p = true; continue; } } p = false; n = true; sum--; for (int i = 1; i < N; i++) { sum += a.at(i); if (p) { while (sum >= 0) { sum--; num_2++; } p = false; n = true; continue; } if (n) { while (sum <= 0) { sum++; num_2++; } n = false; p = true; continue; } } num = min(num_1, num_2); } else if (a.at(0) < 0) { n = true; for (int i = 1; i < N; i++) { sum += a.at(i); if (p) { while (sum >= 0) { sum--; num++; } p = false; n = true; continue; } if (n) { while (sum <= 0) { sum++; num++; } n = false; p = true; continue; } } } cout << num << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <iostream> #include <vector> #include <string> #include <cstring> #include <math.h> #include <limits.h> #include <map> #include <algorithm> #include <functional> using namespace std; int main() { int n; vector<long long> A; int j; bool is_plus; long long ans = 0; long long sum = 0; cin >> n; for ( int i = 0; i < n; i++ ) { long long a; cin >> a; A.push_back(a); } // for ( j = 0; j < n; j++ ) { // if ( abs(A[j]) ) { break; } // } // if ( j == n ) { // cout << A.size()*2-1 << endl; // return 0; // } // if ( j ) { // ans += ( j+1 )*2 - 1; // sum = ( A[j] > 0 ) ? -1: 1; // } // else { // sum = 0; // ans = 0; // } for ( int i = 0; i < n; i++ ) { if ( !i ) { if ( A[i] == 0 ) { if ( A[i+1] >= 0 ) { sum = -1; } else { sum = 1; } ans+;; } else { sum = A[i]; } continue; } bool is_plus = sum > 0; sum += A[i]; if ( sum == 0 ) { ans += 1; sum = is_plus ? -1 : 1; } else if ( is_plus == (sum > 0) ) { ans += abs(sum)+1; sum = is_plus ? -1 : 1; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; constexpr long long MOD = 1e9 + 7; int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; long long A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z; string S, T; long long ans = 0; template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; } signed main() { cin >> N; vector<int> a(N); cin >> a; for (int i = 0; i < (int)(N - 1); i++) { a[i + 1] = a[i] + a[i + 1]; } if (a[0] < 0) { for (int i = 0; i < (int)(N); i++) a[i] *= -1; } int base = 0; for (int i = 0; i < (int)(N); i++) { if (i == 0) continue; if (i & 1) { int tmp = (a[i] + base) - (-1); if (tmp > 0) { ans += tmp; base -= tmp; } } else { int tmp = 1 - (a[i] + base); if (tmp > 0) { ans += tmp; base += tmp; } } } int tmp = ans; ans = 0; for (int i = 0; i < (int)(N); i++) a[i] *= -1; base = 1 - a[0]; for (int i = 0; i < (int)(N); i++) { if (i == 0) continue; if (i & 1) { int tmp = -1 - (a[i] + base); if (tmp > 0) { ans += tmp; base -= tmp; } } else { int tmp = 1 - (a[i] + base); if (tmp > 0) { ans += tmp; base += tmp; } } } ans = max((long long)tmp, ans); cout << ans << "\n"; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; int d[n]; for(int i=0;i<n;i++) { cin >> d[i]; } int count=0; int sum=d[0]; int f =0; if(d[0]>0){ f=-1; } if(d[0]<0){ f=1; } for(int i=1;i<n;i++){ sum+=d[i]; if(sum>0){ if(f==1){ f=-1; continue; } if(f==-1){ count+=sum+1; sum=-1; f=1; continue; } } if(sum<0){ if(f==-1){ f=1; continue; } if(f==1){ count+=1-sum; sum=1; f=-1; continue; } } cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; int a[n]; long long int s = 0; long long int ans = INT_MAX; int i; for (i = 0; i < n; i++) cin >> a[i]; s = a[0]; long long int p = 0; if (s > 0) { for (i = 1; i < n; i++) { if (i % 2) { if (s + a[i] < 0) { s += a[i]; } else { p += 1 + s + a[i]; s = -1; } } else { if (s + a[i] > 0) s += a[i]; else { p += 1 - s - a[i]; s = 1; } } } s = -1; ans = min(ans, p); p = a[0] + 1; for (i = 1; i < n; i++) { if (i % 2 == 0) { if (s + a[i] < 0) { s += a[i]; } else { p += abs(1 + s + a[i]); s = -1; } } else { if (s + a[i] > 0) s += a[i]; else { p += abs(1 - s - a[i]); s = 1; } } } ans = min(ans, p); cout << ans << endl; } else if (s < 0) { for (i = 1; i < n; i++) { if (i % 2 == 0) { if (s + a[i] < 0) { s += a[i]; } else { p += abs(1 + s + a[i]); s = -1; } } else { if (s + a[i] > 0) s += a[i]; else { p += abs(1 - s - a[i]); s = 1; } } } s = 1; ans = min(ans, p); p = (-1) * a[0] + 1; for (i = 1; i < n; i++) { if (i % 2 == 1) { if (s + a[i] < 0) { s += a[i]; } else { p += abs(1 + s + a[i]); s = -1; } } else { if (s + a[i] > 0) s += a[i]; else { p += abs(1 - s - a[i]); s = 1; } } } ans = min(ans, p); cout << ans << endl; } else { p = 1; s = 1; for (i = 1; i < n; i++) { if (i % 2) { if (s + a[i] < 0) { s += a[i]; } else { p += abs(1 + s + a[i]); s = -1; } } else { if (s + a[i] > 0) s += a[i]; else { p += abs(1 - s - a[i]); s = 1; } } } s = -1; ans = min(ans, p); p = 1; for (i = 1; i < n; i++) { if (i % 2 == 0) { if (s + a[i] < 0) { s += a[i]; } else { p += abs(1 + s + a[i]); s = -1; } } else { if (s + a[i] > 0) s += a[i]; else { p += abs(1 - s - a[i]); s = 1; } } } ans = min(ans, p); cout << ans << endl; } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
if(a[0]==0){ ans1++; sum0=1; }else{ sum0=a[0]; } REP(i,1,n){ sum1=sum0+a[i]; if(sum1*sum0<0){ }else if(sum1*sum0>0){ ans1+=abs(sum1)+1; sum1=-1*sum0/abs(sum0); }else{ ans1++; sum1=-1*sum0/abs(sum0); } sum0=sum1; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> signed main() { long long n; std::cin >> n; std::vector<long long> a(n); for (long long i = 0; i < (n); i++) std::cin >> a[i]; const long long INF = 1e18; long long mincount = INF; for (long long x = 0; x < (2); x++) { long long sum = a[0]; long long count = 0; if (sum == 0) continue; if (x == 1) { if (sum > 0) { count += sum + 1; sum = -1; } else if (sum < 0) { count += 1 - sum; sum = 1; } } for (long long i = 1; i < n; i++) { if ((sum + a[i]) * sum >= 0) { if (sum > 0) { count += a[i] + sum + 1; sum = -1; } else if (sum < 0) { count += 1 - a[i] - sum; sum = 1; } } else sum += a[i]; } mincount = std::min(count, mincount); } std::cout << (mincount) << '\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
python2
n = input() a = map(int ,raw_input().split()) if a[0] > 0: sign = 1 if a[0] < 0: sign = -1 c = 0 sum = a[0] for i in xrange(1,n): temp = a[i]+sum if temp == 0 and sum > 0: c += 1 sum = -1 continue if temp == 0 and sum < 0: c += 1 sum = 1 continue if temp < 0 and sum > 0 : sum = temp continue if temp > 0 and sum < 0: sum = temp continue if temp > 0 and sum > 0: c += abs(sum + a[i]) + 1 sum = -1 continue if temp < 0 and sum <0 : c += abs(sum + a[i]) + 1 sum = 1 continue print c
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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 ans_a = 0, sum_a = 0; for (int i = 0; i < n; i++) { sum_a += a[i]; if ((i % 2 != 0) && (sum_a >= 0)) { ans_a += abs(sum_a) + 1; sum_a -= abs(sum_a) + 1; } else if ((i % 2 == 0) && (sum_a <= 0)) { ans_a += abs(sum_a) + 1; sum_a += abs(sum_a) + 1; } } int ans_b = 0, sum_b = 0; for (int i = 0; i < n; i++) { sum_b += a[i]; if ((i % 2 != 0) && (sum_b <= 0)) { ans_b += abs(sum_b) + 1; sum_b += abs(sum_b) + 1; } else if ((i % 2 == 0) && (sum_b >= 0)) { ans_b += abs(sum_b) + 1; sum_b -= abs(sum_b) + 1; } } int ans = ans_a < ans_b ? ans_a : ans_b; cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; typedef vector<vector<int> > vii; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); long long n; cin >> n; vector<long long> a(n); for (int i = 0; i < (int)n; i++) cin >> a[i]; long long sum = a[0], op_cnt = 0; for (int i = (int)1; i < (int)n; i++) { if (sum < 0 && sum + a[i] <= 0) { op_cnt += (0 - sum - a[i]) + 1; sum = 1; } else if (sum >= 0 && sum + a[i] >= 0) { op_cnt += sum + a[i] + 1; sum = -1; } else sum += a[i]; } cout << op_cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# coding: utf-8 # Here your code N = int(input()) a = [int(i) for i in input().split()] result = 0 before_sum =a[0] after_sum =a[0] for i in range(1,N): before_sum = after_sum after_sum = before_sum + a[i] if before_sum * after_sum > 0: if after_sum < 0: result += 1 - after_sum after_sum = 1 elif after_sum > 0: result += 1 + after_sum after_sum = -1 elif before_sum * after_sum == 0: result += 1 if before_sum < 0: after_sum = 1 else: after_sum = -1 print(result)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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); int ju = 0; for (int i = 0; i < (int)(n); i++) { cin >> a[i]; if (i % 2 == 0 && a[i] >= 0) ju++; if (i % 2 == 1 && a[i] < 0) ju++; } long long int sum = 0, ans = 0; if (ju >= n / 2) { for (int i = 0; i < (int)(n); i++) { sum += a[i]; if (sum <= 0 && i % 2 == 0) { ans += abs(1 - sum); sum = 1; } else if (sum >= 0 && i % 2 == 1) { ans += abs(-1 - sum); sum = -1; } } } else { for (int i = 0; i < (int)(n); i++) { sum += a[i]; if (sum >= 0 && i % 2 == 0) { ans += abs(-1 - sum); sum = -1; } else if (sum <= 0 && i % 2 == 1) { ans += abs(1 - sum); sum = 1; } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) sum = a[0] change = 0 for i in range(1,n): val = 0 tempsum = sum+a[i] if sum < 0 and tempsum <=0: val = 1 - tempsum if sum > 0 and tempsum >=0: val = -1 - tempsum sum = tempsum + val change += abs(val) print(change)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; class Main { int n; int[] a; public static void main(String[] args) { Scanner sc = new Scanner(System.in); Main m = new Main(sc); m.solve(); sc.close(); } Main(Scanner sc) { n = sc.nextInt(); a = new int[n]; for(int i=0;i<n;i++){ a[i] = sc.nextInt(); } } void solve() { int sign = (a[0]>=0)?1:-1; int cnt = (a[0]==0)?1:0; long sum = (a[0]==0)?1:a[0]; //System.out.println(sum); for(int i=1;i<n;i++){ sum += a[i]; if(sum*sign>=0){ cnt += Math.abs(sum) + 1; sum = -sign; } //System.out.println(sum); sign *= -1; } System.out.println(cnt); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double PI = 3.1415926535897932384626433832795; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool isDiffer(long long a, long long b) { if (b == 0) return false; if (((a > 0) && (b < 0)) || ((a < 0) && (b > 0))) return true; else return false; } int main() { ios::sync_with_stdio(false); long long n; cin >> n; vector<long long> v; for (int i = 0; i < n; i++) { long long t; cin >> t; v.push_back(t); } long long ans = 0; if (v[0] == 0) { v[0] = -1; ans += 1; } long long os = v[0]; for (int i = 1; i < n; i++) { if (!isDiffer(os, v[i] + os)) { long long ob = (os >= 0) ? -1 : 1; ans += abs(ob - os - v[i]); v[i] = ob - os; } os += v[i]; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
require 'prime' include Math def max(a,b); a > b ? a : b end def min(a,b); a < b ? a : b end def swap(a,b); a, b = b, a end def gif; gets.to_i end def gff; gets.to_f end def gsf; gets.chomp end def gi; gets.split.map(&:to_i) end def gf; gets.split.map(&:to_f) end def gs; gets.chomp.split.map(&:to_s) end def gc; gets.chomp.split('') end def pr(num); num.prime_division end def digit(num); num.to_s.length end def array(s,ini=nil); Array.new(s){ini} end def darray(s1,s2,ini=nil); Array.new(s1){Array.new(s2){ini}} end def rep(num); num.times{|i|yield(i)} end def repl(st,en,n=1); st.step(en,n){|i|yield(i)} end def f(sum,a,count) repl 1,a.size-1 do |i| sum << a[i]+sum[i-1] if sum[i-1] > 0 if sum[i] >= 0 count += sum[i]+1 sum[i] = -1 end elsif sum[i-1] < 0 if sum[i] <= 0 count += 1-sum[i] sum[i] = 1 end end end return count end n = gif a = gi sum2 = [] sum3 = [] ans2 = nil ans3 = nil sum2 << 1 ans2 = f sum2,a,(1-a[0]).abs sum3 << -1 ans3 = f sum3,a,(1+a[0]).abs puts min ans2,ans3
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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()) x=list(map(int,input().split())) k=x[0] s="" if x[0]>0: s="True" else: s="Folus" l=0 for i in range(1,n): k+=x[i] if k>=0 and s=="True": l+=k+1 k=-1 s="Folus" elif k<=0 and s=="Folus": l+=k*(-1)+1 k=1 s="True" else: if s=="True": s="Folus" else: s="True" print(l)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import copy n=int(input()) a=list(map(int,input().split())) b=copy.copy(a) ct1=0 if a[0]<=0: a[0]=1 ct1+=1-a[0] for i in range(1,n): if i%2==1: if sum(a[0:i+1])>=0: ct1+=sum(a[0:i+1])+1 a[i]=-sum(a[0:i])-1 else: if sum(a[0:i+1])<=0: ct1+=1-sum(a[0:i+1]) a[i]=-sum(a[0:i])+1 ct2=0 if b[0]>=0: b[0]=-1 ct2+=b[0]-1 for i in range(1,n): if i%2==0: if sum(b[0:i+1])>=0: ct2+=sum(b[0:i+1])+1 b[i]=-sum(b[0:i])-1 else: if sum(b[0:i+1])<=0: ct2+=1-sum(b[0:i+1]) b[i]=-sum(b[0:i])+1 print(min(ct1,ct2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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 math if __name__ == "__main__": n = int(input()) nums = list(map(int, input().split())) acm = nums[0] counts = 0 for num in nums[1:]: if acm * (acm+num)>0: counts += abs(acm+num)+1 acm = -acm//abs(acm) elif acm * (acm+num)<0: acm = acm+num else: if acm == 0: counts += 1 acm = 1 else: counts += 1 acm = -acm//abs(acm) print(counts)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int32_t main() { uint64_t N; cin >> N; long long total, sign; unsigned long long count; cin >> total; if (total == 0) { total = 1; sign = 1; count = 1; } else { sign = total / abs(total); count = 0; } for (uint64_t i = 1; i < N; i++) { sign *= -1; long long val; cin >> val; total += val; if ((total == 0) || (sign * total < 0)) { count += abs(sign - total); total = sign; } } 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> #define rep(i,n) for(int i=0;i<(int)(n);i++) #define rrep(i,n) for(int i=(int)(n-1);i>=0;i--) #define FOR(i,n,m) for(int i=n;i<=(int)(m);i++) #define RFOR(i,n,m) for(int i=(int)(n);i>=m;i--) #define all(x) (x).begin(),(x).end() #define sz(x) int(x.size()) typedef long long ll; const int INF = 1e9; const int MOD = 1e9+7; const ll LINF = 1e18; const double PI=3.14159265358979323846; using namespace std; vector<int> dx={1,0,-1,0}; vector<int> dy={0,1,0,-1}; template<class T> vector<T> make_vec(size_t a){ return vector<T>(a); } template<class T, class... Ts> auto make_vec(size_t a, Ts... ts){ return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } ll gcd (ll x,ll y) { if (x < y) swap(x, y); if (y == 0) return x; return gcd(x % y, y); } int main() { int n; cin>>n; vector<ll> a(n); rep(i,n) cin>>a[i]; ll ans=LINF; { ll add=0; ll sum=0; int now=1; rep(i,n) { sum+=a[i]; if(now==1 && sum<=0) { add+=1-sum; sum+=add; } if(now==-1 && sum>=0) { add+=sum+1; sum-=add; } now*=-1; } ans=min(ans,add); } { ll add=0; ll sum=0; int now=-1; rep(i,n) { sum+=a[i]; if(now==1 && sum<=0) { add+=1-sum; sum+=add; } if(now==-1 && sum>=0) { add+=sum+1; sum-=add; } now*=-1; } ans=min(ans,add); } 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
function Main(s) { var s = s.split("\n"); var n = parseInt(s[0], 10); var a = s[1].split(" ").map(e => parseInt(e, 10)); var acc = 0, cnt = 0, arr = []; for (var i = 0; i < n; i++) { acc += a[i]; if (i === 0) { if (acc === 0) { if (a[i + 1] >= 0) { acc--; cnt++; } else { acc++; cnt++; } } } else { if (arr[i - 1] > 0) { if (acc >= 0) { cnt += (acc + 1); acc -= (acc + 1); } } else { if (acc <= 0) { cnt += (Math.abs(acc) + 1); acc += (Math.abs(acc) + 1); } } } arr.push(acc); } console.log(cnt); } Main(require("fs").readFileSync("/dev/stdin", "utf8"));
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long res, standard, n, a[100000], sum; long long ans = 1000000000000000LL; 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
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using vpii = vector<pair<int, int>>; using vpll = vector<pair<ll, ll>>; int main(void) { int N; cin >> N; vector<ll> A(N); for (int i = 0; i < N; i++) cin >> A[i]; ll ans = 0; ll cur = A[0]; if (cur == 0) { ans++; int i = 1; while (A[i] == 0 && i < N) i++; if (i == N) cur++; else if (A[i] > 0 && i % 2 == 0) cur++; else if (A[i] > 0 && i % 2 == 1) cur--; else if (A[i] < 0 && i % 2 == 0) cur--; else cur++; } for (int i = 1; i < N; i++) { if (cur > 0 && cur + A[i] > 0) { ans += abs(-1 - (cur + A[i])); cur = -1; } else if (cur > 0 && cur + A[i] == 0) { ans++; cur = -1; } else if (cur < 0 && cur + A[i] == 0) { ans++; cur = 1; } else if (cur < 0 && cur + A[i] < 0) { ans += abs(1 - abs(cur + A[i])); cur = 1; } else cur += A[i]; } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int f(vector<int> a, int sign) { int ans = 0; if (sign > 0 && a.at(0) <= 0) { ans += -a.at(0) + 1; a.at(0) += -a.at(0) + 1; } if (sign < 0 && a.at(0) >= 0) { ans += a.at(0) + 1; a.at(0) -= a.at(0) + 1; } int sum = a.at(0); for (int i = 1; i < a.size(); i++) { if (sum > 0 && a.at(i) + sum >= 0) { ans += a.at(i) + sum + 1; a.at(i) -= a.at(i) + sum + 1; } if (sum < 0 && a.at(i) + sum <= 0) { ans += -(a.at(i) + sum - 1); a.at(i) += -(a.at(i) + sum - 1); } sum += a.at(i); } return ans; } int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } cout << min(f(a, 1), f(a, -1)); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, a; cin >> n; int ans = 0; vector<int> vec(n); for (int i = 0; i < n; i++) { cin >> a; vec.at(i) = a; } int now1 = 0, now2 = 0, cnt1 = 0, cnt2 = 0; for (int i = 0; i < n; i++) { now1 += vec[i]; now2 += vec[i]; if (now1 <= 0) { cnt1 += abs(now1) + 1; now1 = 1; } if (now2 >= 0) { cnt2 += abs(now2) + 1; now2 = -1; } swap(now1, now2); swap(cnt1, cnt2); } cout << min(cnt1, cnt2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input().strip()) A = list(map(int, input().strip().split(" "))) def solver(_sign): s = A[0] count = 0 if _sign and s <= 0: # head is positive count += abs(s) + 1 s = 1 elif s >= 0: # head is negative count += abs(s) + 1 s = -1 for a in A[1:]: prev = s sign = prev > 0 s += a if s == 0: count += 1 if sign: # previous is positive s = -1 else: # prev is negative s = 1 elif sign == (s > 0): # previous and current have the same sign count += abs(s)+1 if s > 0: s = -1 else: s = 1 else: pass return count print(min(solver(True), solver(False)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long int n, sum = 0, f = 0, ans = 0; cin >> n; vector<long long int> a(10000000); for (int i = 0; i < n; i++) { cin >> a[i]; } sum += a[0]; if (sum > 0) { f = -1; } else if (sum < 0) { f = 1; } else { if (a[1] < 0) { ans++; f = -1; a[0] = 1; } else { ans++; f = 1; a[0] = -1; } } for (int i = 1; i < n; i++) { sum += a[i]; if (f * sum > 0) { } else { ans += abs(sum) + 1; sum = f; a[i] += f * (abs(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
UNKNOWN
#include <bits/stdc++.h> int main() { int n, a[100010]; long sgn, cont = 0, ans = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } cont += a[0]; if (cont > 0) sgn = 1; else if (cont < 0) sgn = -1; else { ans++; int i = 0; for (; a[i] == 0; i++) { sgn = -1 * sgn; } sgn = (a[i] > 0) ? sgn : -1 * sgn; } for (int i = 1; i < n; i++) { cont += a[i]; if (cont * sgn < 0) { sgn = -1 * sgn; } else if (cont * sgn >= 0) { ans = ans + 1 + cont * sgn; sgn = -1 * sgn; cont = sgn; } } printf("%ld", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = [int(i) for i in input().split()] ans1 = ans2 = 0 s = s2 = a[0] if a[0] < 0: s = 1 ans1 = -a[0] + 1 if a[0] > 0: s2 = -1 ans2 = a[0] + 1 for i in range(1, len(a)): s += a[i] if i % 2 == 1 and s >= 0: ans1 += s + 1 s = -1 elif i % 2 == 0 and s <= 0: ans1 += -s + 1 s = 1 s2 += a[i] if i % 2 == 0 and s2 >= 0: ans2 += s2 + 1 s2 = -1 elif i % 2 == 1 and s2 <= 0: ans2 += -s2 + 1 s2 = 1 print(min(ans1, ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> S(N + 1); for (int i = 1; i <= N; ++i) { cin >> S[i]; S[i] += S[i - 1]; } int ians = (1 << 30); for (int j = -1; j <= 1; j += 2) { vector<int> S_(S); int ans = 0; int add = 0; int sign = j; for (int i = 1; i <= N; ++i) { S_[i] += add; int sign_i = S_[i] / abs(S_[i]); if (sign_i == sign) { ans += abs(-sign_i - S_[i]); add += -sign_i - S_[i]; S_[i] = -sign_i; } else if (S_[i] == 0) { ans += 1; add += -sign; S_[i] += -sign; } sign = -sign; } ians = min(ans, ians); } cout << ians << 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
input() a = list(map(int, input().split())) s = a[0] ret = 0 for i in a[1:]: if s * i > 0: t = s + i k = 1 if t > 0 else -1 ret += k * t + 1 s = -k else: if s * (s+i) < 0: s = s+i else: ret += s+i+1 s = -1 if s > 0 else 1 print(ret)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long a[200005] = {0}; int main() { long long n, i; cin >> n; for (i = 1; i <= n; i++) { scanf("%lld", &a[i]); } long long sum1 = 0, sum2 = 0; long long ans1 = 0, ans2 = 0; sum1 += a[1], sum2 += a[1]; for (i = 2; i <= n; i++) { sum1 += a[i]; if (i % 2 == 0 && sum1 >= 0) { ans1 += sum1 + 1; sum1 = -1; } else if (i % 2 != 0 && sum1 <= 0) { ans1 += 1 - sum1; sum1 = 1; } } for (i = 2; i <= n; i++) { sum2 += a[i]; if (i % 2 == 0 && sum2 <= 0) { ans2 += 1 - sum2; sum2 = 1; } else if (i % 2 == 1 && sum2 >= 0) { ans2 += sum2 + 1; sum2 = -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
UNKNOWN
package main import "fmt" func main() { var n int fmt.Scan(&n) sum := 0 cnt := 0 plus := false a := make([]int, n) for i := 0; i < n; i++ { fmt.Scan(&a[i]) } var base int for i :=0; i <n; i++ { if a[i] != 0 { base = i break } } if base % 2 == 0 { plus = true } else { plus = false } if a[base] < 0 { plus = !plus } for i := 0; i < n; i++ { sum += a[i] if plus { for sum <= 0 { sum++ cnt++ } plus = !plus } else { for sum >= 0 { sum-- cnt++ } plus = !plus } fmt.Println(sum, cnt) } fmt.Println(cnt) }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int &x : a) { cin >> x; } bool plus; long sum = 0; int count_plus = 0; plus = false; for (int i = 0; i < n; i++) { plus = !plus; sum += a.at(i); if (plus) { if (sum > 0) { continue; } else { count_plus += 1 - sum; sum = 1; } } else { if (sum < 0) { continue; } else { count_plus += 1 + sum; sum = -1; } } } int count_minus = 0; plus = true; sum = 0; for (int i = 0; i < n; i++) { plus = !plus; sum += a.at(i); if (plus) { if (sum > 0) { continue; } else { count_minus += 1 - sum; sum = 1; } } else { if (sum < 0) { continue; } else { count_minus += 1 + sum; sum = -1; } } } cout << min(count_plus, count_minus) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) al = list(map(int, input().split())) m = n//2 mm = n % 2 temp = al[0] res = 0 def ddd(temp,al,m,mm,res): if temp > 0 and mm ==1: for i in range(1,m+1): temp +=al[i*2-1] if temp <0: pass else: res += temp+1 temp = -1 temp +=al[i*2] if temp >0: pass else: res +=1-temp temp = 1 return(res) if temp > 0 and mm ==0: for i in range(1,m): temp +=al[i*2-1] if temp <0: pass else: res += temp+1 temp = -1 temp +=al[i*2] if temp >0: pass else: res +=1-temp temp = 1 temp += al[n-1] if temp <0: pass else: res += temp+1 temp = -1 return(res) if temp < 0 and mm ==1: for i in range(1,m+1): temp +=al[i*2-1] if temp >0: pass else: res += 1-temp temp = 1 temp +=al[i*2] if temp <0: pass else: res +=temp+1 temp = -1 return(res) if temp < 0 and mm ==0: for i in range(1,m): temp +=al[i*2-1] if temp >0: pass else: res += 1-temp temp = 1 temp +=al[i*2] if temp <0: pass else: res +=temp+1 temp = -1 temp += al[n-1] if temp >0: pass else: res += 1-temp temp = 1 return(res) if al[0]==0: temp = 1 res = 1 dpl =ddd(temp,al,m,mm,res) temp = -1 res = 1 dmi =ddd(temp,al,m,mm,res) print(min(dpl,dmi)) else: print(ddd(temp,al,m,mm,res))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def z(s,l): for i in range(n-1): r=l+a[i+1] if r*l>=0: if l<=0: s+=1-r r=1 else: s+=1+r r=-1 l=r return s n=int(input()) a=list(map(int,input().split())) s1=0 l1=a[0] if a[0]<=0: s1=1-a[0] l1=1 s2=0 l2=a[0] if a[0]>=0: s1=a[0]-1 l1=-1 print(min(z(s1,l1),z(s2,l2)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> std::vector<int> seq, sum; int main() { int n; std::cin >> n; seq.resize(n); sum.resize(n); std::cin >> seq[0]; sum[0] = seq[0]; for (int i = 1; i < n; i++) { std::cin >> seq[i]; sum[i] = sum[i - 1] + seq[i]; } bool is_plus = true; long ans = 0; long dif = 0; if (sum[0] <= 0) { dif = 1 - sum[0]; ans = dif; } for (int i = 1; i < sum.size(); i++) { if (is_plus && sum[i] + dif >= 0) { long tmp = -(sum[i] + dif) - 1; dif += tmp; ans += (tmp < 0 ? -tmp : tmp); } else if (!is_plus && sum[i] + dif <= 0) { long tmp = 1 - (sum[i] + dif); dif += tmp; ans += (tmp < 0 ? -tmp : tmp); } is_plus = !is_plus; } long ya = ans; ans = 0; is_plus = false; if (sum[0] > 0) { dif = -1 - sum[0]; ans = -dif; } else { dif = 0; } for (int i = 1; i < sum.size(); i++) { if (is_plus && sum[i] + dif >= 0) { long tmp = -(sum[i] + dif) - 1; dif += tmp; ans += (tmp < 0 ? -tmp : tmp); } else if (!is_plus && sum[i] + dif <= 0) { long tmp = 1 - (sum[i] + dif); dif += tmp; ans += (tmp < 0 ? -tmp : tmp); } is_plus = !is_plus; } std::cout << (ya < ans ? ya : ans) << 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 <cstdio> #include <algorithm> using namespace std; int main(){ int n; scanf("%d", &n); int a[n]; for (int i = 0; i < n; i++) scanf(" %d", &a[i]); int S = a[0]; int j = 0; for (int i = 1; i < n; i++){ if (S * (S+a[i]) < 0){ S += a[i]; } else if (S+a[i] == 0){ j += 1; if (S > 0) S = -1; else (S < 0) S = 1; } else { j += abs(S + a[i]) + 1; if (S < 0) S = 1; else if (S > 0) S = -1; } } } printf("%d\n", j); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def main(): n = int(input()) A = list(map(int, input().split())) res = 0 sums = [] for i in range(n): if i == 0: sums.append(sum(A[:i]) + A[i]) if A[i] == 0: index = -1 for j in range(n): if A[j] != 0: index = A.index(A[j]) break if index == -1: res += 3 A[i] = 1 A[i+1] = -2 sums[i] = 1 elif (index % 2 and A[index] > 0) or (index % 2 == 0 and A[index] < 0): A[i] = -1 sums[i] = -1 res += 1 else: A[i] = 1 sums[i] = 1 res += 1 else: sums.append(sums[i-1] + A[i]) if sums[i] == 0: if sums[i-1] > 0: A[i] -= 1 sums[i] = sums[i-1] + A[i] res += 1 else: A[i] += 1 sums[i] = sums[i-1] + A[i] res += 1 elif (sums[i-1] > 0) and (sums[i] > 0): res += A[i] - (-sums[i-1] - 1) A[i] = -sums[i-1] - 1 sums[i] = sums[i-1] + A[i] elif (sums[i-1] < 0) and (sums[i] < 0): res += 1 - (sums[i-1] + A[i]) A[i] = abs(sums[i-1]) + 1 sums[i] = sums[i-1] + A[i] print(res) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<long long> A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } vector<long long> S1(N); vector<long long> S2(N); S1[0] = A[0]; S2[0] = 0; int cnt1 = 0; int cnt2 = 0; for (int i = 0; i < N; i++) { if (i) { S1[i] = S1[i - 1] + A[i]; S2[i] = S2[i] + A[i]; } if (!(i % 2)) { if (S1[i] < 0) { cnt1 += 1 - S1[i]; S1[i] = 1; } if (S2[i] > 0) { cnt2 += S2[i] + 1; S2[i] = -1; } } else { if (S1[i] > 0) { cnt1 += S1[i] + 1; S1[i] = -1; } if (S2[i] < 0) { cnt2 += 1 - S2[i]; S2[i] = 1; } } } cout << min(cnt1, cnt2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int,input().split())) ans1,ans2 = 0,0 res = A[0] for i in range(1,N): res += A[i] if i%2==1: if res >= 0: ans1 += res+1 res = -1 else: if res <= 0: ans1 -= res-1 res = 1 res = A[0] for i in range(1,N): res += A[i] if i%2==1: if res <= 0: ans2 -= res-1 res = 1 else: if res >= 0: ans2 += res+1 res = -1 print(min(ans1,ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; long long A[100010] = {}; bool flag = false; long long tmp, sum; long long ans[] = {0, 0}; cin >> n; for (int i = 0; i < n; i++) cin >> A[i]; if (A[0] == 0) { flag = true; A[0] = 1; ans[0]++; } tmp = 0, sum = 0; for (int i = 0; i < n; i++) { tmp = sum + A[i]; if (tmp == 0) { ans[0]++; if (A[i] > 0) tmp = 1; else tmp = -1; } else { if ((tmp > 0 && sum > 0) || (tmp < 0 && sum < 0)) { if (tmp > 0) tmp = -1; else tmp = 1; ans[0] += abs((tmp - sum) - A[i]); } } sum = tmp; } if (flag) { A[0] = -1; ans[1]++; } tmp = 0, sum = 0; for (int i = 0; i < n; i++) { tmp = sum + A[i]; if (tmp == 0) { ans[1]++; if (A[i] > 0) tmp = 1; else tmp = -1; } else { if ((tmp > 0 && sum > 0) || (tmp < 0 && sum < 0)) { if (tmp > 0) tmp = -1; else tmp = 1; ans[1] += abs((tmp - sum) - A[i]); } } sum = tmp; } cout << min(ans[0], ans[1]) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; int a[n], c[n]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) c[i] = a[i]; int count = 0, count2 = 0, sum = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (sum + a[i] <= 0) { int b = (abs(sum) + 1); count += abs(b - a[i]); a[i] = b; } sum += a[i]; } else { if (sum + a[i] >= 0) { int b = -1 * (abs(sum) + 1); count += abs(b - a[i]); a[i] = b; } sum += a[i]; } } sum = 0; for (int i = 0; i < n; i++) { if (i % 2 == 1) { if (sum + c[i] <= 0 || c[i] <= 0) { int b = (abs(sum) + 1); count2 += abs(b - c[i]); c[i] = b; } sum += c[i]; } else { if (sum + c[i] >= 0 || c[i] >= 0) { int b = -1 * (abs(sum) + 1); count2 += abs(b - c[i]); c[i] = b; } sum += c[i]; } } cout << min(count, count2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int MOD = 1e9 + 7; const long long LINF = 1e18; long long n; vector<long long> a; long long count(long long sum) { long long cnt = 0; for (long long i = 1; i < n; ++i) { if (sum > 0) { sum += a[i]; if (sum >= 0) { cnt += abs(sum) + 1; sum = -1; } } else { sum += a[i]; if (sum <= 0) { cnt += abs(sum) + 1; sum = 1; } } } return cnt; } int main() { cin >> n; long long ans = LINF; a.resize(n); for (long long i = 0; i < n; ++i) { cin >> a[i]; } if (a[0] == 0) { ans = min(ans, count(1) + 1); ans = min(ans, count(-1) + 1); } else { long long sum = a[0]; ans = min(ans, count(sum)); } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int N; vector<int> a; int solve(bool b) { long long count = 0; long long sum = a[0]; if (a[0] == 0) { sum = b ? 1 : -1; count++; } else if (b && a[0] < 0) { sum = 1; count = 1 - a[0]; } else if (!b && a[0] > 0) { sum = -1; count = a[0] + 1; } for (int i = 1; i < N; i++) { if (sum * a[i] < 0 && abs(sum) < abs(a[i])) { sum += a[i]; } else { if (sum > 0) { count += a[i] + sum + 1; sum = -1; } else { count += 1 - sum - a[i]; sum = 1; } } } return count; } int main() { cin >> N; a.resize(N); for (int i = 0; i < N; i++) cin >> a[i]; 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
python3
import numpy as np n = int(input()) L = np.array([int(i) for i in input().split()]) count = 0 s = L[0] #if L[0] == 0: # if L[1] > 0: # L[0] = -1 # else: # L[0] = 1 # count += 1 # print(L) loopnum = n//2 if n%2 == 0: loopnum -= 1 #+-+-... for i in range(loopnum): s = s + L[2*i+1] if s >= 0: subt = s + 1 count += subt s = s - subt s = s + L[2*i+2] if s <= 0: subt = s - 1 count -= subt s = s - subt if n%2 == 0: s = s + L[-1] if s >= 0: subt = s + 1 count += subt cand1 = count count = 0 s = L[0] #-+-+... for i in range(loopnum): s = s + L[2*i+1] if s <= 0: subt = s - 1 count -= subt s = s - subt s = s + L[2*i+2] if s >= 0: subt = s + 1 count += subt s = s - subt if n%2 == 0: s = s + L[-1] if s <= 0: subt = s - 1 count -= subt cand2 = count #print(cand1) #print(cand2) print(min(cand1,cand2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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; const long long INF = 1e18; signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); long long n, ans1 = 0, ans2 = 0, sum1 = 0, sum2 = 0; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; i++) { cin >> a[i]; } sum1 = a[0]; if (sum1 == 0) { sum1 = (a[1] > 0 ? -1 : 1); ans1++; } for (long long i = 1; i < n; i++) { if (sum1 > 0 && sum1 + a[i] < 0) { sum1 += a[i]; } else if (sum1 < 0 && sum1 + a[i] > 0) { sum1 += a[i]; } else if (sum1 > 0 && a[i] + sum1 > 0) { ans1 += sum1 + a[i] + 1; } else { ans1 += -a[i] - sum1 + 1; } } a[0] *= -1; if (sum2 == 0) { sum2 = (a[1] > 0 ? -1 : 1); ans2++; } for (long long i = 1; i < n; i++) { if (sum2 > 0 && sum2 + a[i] < 0) { sum2 += a[i]; } else if (sum2 < 0 && sum2 + a[i] > 0) { sum2 += a[i]; } else if (sum2 > 0 && a[i] + sum2 > 0) { ans2 += sum2 + a[i] + 1; } else { ans2 += -a[i] - sum2 + 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
python3
def solve(): n = int(input()) a = list(map(int, input().split())) i = 0 sum = 0 ans = 0 for i in range(n-1): sum += a[i] if sum > 0 and sum+a[i+1] > 0: tmp = -1 - sum ans += abs(tmp - a[i+1]) a[i+1] = tmp elif sum < 0 and sum+a[i+1] < 0: tmp = 1 - sum ans += abs(tmp - a[i+1]) a[i+1] = tmp print(ans) print(a) if __name__ == "__main__": solve()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import copy n = int(input()) a = [int(i) for i in input().split()] b=a.copy() s0p = a[0] s0n = b[0] countp = 0 countn = 0 if a.count(0)==n: print(2*n+1) exit() """ if s0p<=0: while s0p<=0: s0p+=1 countp+=1 if s0n>=0: while s0n>=0: s0n-=1 countn+=1 """ for i in range(1,n): s1 = s0p+a[i] if s0p*s1>=0: if s1>0: a[i]-=(abs(s1)+1) countp+=(abs(s1)+1) elif s1<0: a[i]+=(abs(s1)+1) countp+=(abs(s1)+1) elif s1==0: if s0p>0: a[i]-=1 countp+=1 elif s0p<0: a[i]+=1 countp+=1 s0p += a[i] """ for i in range(1,n): s1 = s0n+b[i] if s0n*s1>=0: if s1>0: b[i]-=(abs(s1)+1) countn+=(abs(s1)+1) elif s1<0: b[i]+=(abs(s1)+1) countn+=(abs(s1)+1) elif s1==0: if s0n>0: b[i]-=1 countn+=1 elif s0n<0: b[i]+=1 countn+=1 s0n += b[i] """ print(countp if countp<=countn else(countn))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<long long> A(N); for (int i = 0; i < N; i++) { cin >> A[i]; } long long sum = 0; long long cnt1 = 0; for (int i = 0; i < N; i++) { sum += A[i]; if (i % 2 == 0 && sum <= 0) { cnt1 += 1 - sum; sum = 1; } if (i % 2 == 1 && sum >= 0) { cnt1 += sum + 1; sum = -1; } } long long sum2 = 0; long long cnt2 = 0; for (int i = 0; i < N; i++) { sum += A[i]; if (i % 2 == 0 && sum2 >= 0) { cnt2 += sum2 + 1; sum2 = -1; } if (i % 2 == 1 && sum2 <= 0) { cnt2 += 1 - sum2; sum2 = 1; } } cout << min(cnt1, cnt2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int sign(int a) { if (a > 0) return 1; else if (a < 0) return -1; else return 0; } int main(void) { int n; cin >> n; int sum = 0, s = 1; int a; int num = 0; for (int i = 0; i < n; i++) { cin >> a; sum += a; if (i != 0 && (sign(s) == sign(sum) || sum == 0)) { num += abs(sum) + 1; sum = sign(s) * -1; s *= -1; } else { s = sign(sum); } } 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
def read_input(): n = int(input()) alist = list(map(int, input().split())) return n, alist def get_sign(x): if x > 0: return 1 elif x < 0: return -1 return 0 def submit(): n, alist = read_input() s = alist[0] sign = get_sign(s) edit = 0 for a in alist[1:]: temp = s + a temp_sign = get_sign(temp) if sign == temp_sign: edit += temp_sign * temp temp -= temp if temp == 0: edit += 1 temp -= sign s = temp sign = get_sign(s) print(edit) if __name__ == '__main__': submit()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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> s(n); for (int &x : a) { cin >> x; } s.at(0) = a.at(0); for (int i = 1; i < n; i++) { s.at(i) = s.at(i - 1) + a.at(i); } int count = 0; if (a.at(0) == 0) { a.at(0)++; count++; for (int x : s) { x++; } } bool is_next_plus; if (s.at(0) > 0) { is_next_plus = false; } else { is_next_plus = true; } for (int i = 1; i < n; i++) { if (is_next_plus) { is_next_plus = !is_next_plus; if (s.at(i) > 0) { continue; } else { int diff = 1 - s.at(i); count += diff; for (int j = i; j < n; j++) { s.at(j) += diff; } } } else { is_next_plus = !is_next_plus; if (s.at(i) < 0) { continue; } else { int diff = 1 + s.at(i); count += diff; for (int j = i; j < n; j++) { s.at(j) -= diff; } } } } cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> unsigned solve(unsigned N, const std::vector<long> &a, int flag) { unsigned c = 0; long s = 0; for (unsigned n = 0; n < N; ++n) { long b = s + a[n]; if (n % 2 == flag) { if (b <= 0) { c += std::abs((+1) - b); s = +1; } else { s = b; } } else { if (b >= 0) { c += std::abs((-1) - b); s = -1; } else { s = b; } } } return c; } int main() { unsigned N; std::cin >> N; std::vector<long> a(N); for (unsigned n = 0; n < N; ++n) { std::cin >> a[n]; } std::cout << std::min(solve(N, a, 0), solve(N, a, 1)) << 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; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int c1 = 0, s1 = 0, c2 = 0, s2 = 0; for (int i = 0; i < n; i++) { s1 += a[i]; s2 += a[i]; if (i % 2 == 0) { if (s1 <= 0) { c1 += 1 - s1; s1 = 1; } if (s2 >= 0) { c2 += s2 + 1; s2 = -1; } } else { if (s2 <= 0) { c2 += 1 - s2; s2 = 1; } if (s1 >= 0) { c1 += s1 + 1; s1 = -1; } } } cout << min(c1, c2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } long long ans = 0; if (a[0] == 0) { a[0] = 1; long long total = a[0]; long long temp; long long ans1 = 0; ans1++; for (int i = 1; i < n; i++) { temp = a[i]; if (total > 0) { if (total + a[i] >= 0) { a[i] = -(total + 1); } total += a[i]; ans1 += abs(a[i] - temp); a[i] = temp; } else if (total < 0) { if (total + a[i] <= 0) { a[i] = (-total + 1); } total += a[i]; ans1 += abs(a[i] - temp); a[i] = temp; } } a[0] = -1; total = a[0]; long long ans2 = 1; for (int i = 1; i < n; i++) { temp = a[i]; if (total > 0) { if (total + a[i] >= 0) { a[i] = -(total + 1); } total += a[i]; ans2 += abs(a[i] - temp); a[i] = temp; } else if (total < 0) { if (total + a[i] <= 0) { a[i] = (-total + 1); } total += a[i]; ans2 += abs(a[i] - temp); a[i] = temp; } } ans = min(ans1, ans2); } else { long long total = a[0]; long long temp; long long ans1 = 0; for (int i = 1; i < n; i++) { temp = a[i]; if (total > 0) { if (total + a[i] >= 0) { a[i] = -(total + 1); } total += a[i]; ans1 += abs(a[i] - temp); a[i] = temp; } else if (total < 0) { if (total + a[i] <= 0) { a[i] = (-total + 1); } total += a[i]; ans1 += abs(a[i] - temp); a[i] = temp; } } temp = a[0]; a[0] = -a[0]; total = a[0]; long long ans2 = abs(a[0] - temp); for (int i = 1; i < n; i++) { temp = a[i]; if (total > 0) { if (total + a[i] >= 0) { a[i] = -(total + 1); } total += a[i]; ans2 += abs(a[i] - temp); a[i] = temp; } else if (total < 0) { if (total + a[i] <= 0) { a[i] = (-total + 1); } total += a[i]; ans2 += abs(a[i] - temp); a[i] = temp; } } ans = min(ans1, ans2); } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; using vl = vector<long long>; using vvl = vector<vector<long long>>; using vs = vector<string>; const int mod = 1000000007; class mint { public: long long x; mint(long long x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint& a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint& a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint& a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint& a) const { mint res(*this); return res += a; } mint operator-(const mint& a) const { mint res(*this); return res -= a; } mint operator*(const mint& a) const { mint res(*this); return res *= a; } mint pow(long long t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } mint inv() const { return pow(mod - 2); } mint& operator/=(const mint& a) { return (*this) *= a.inv(); } mint operator/(const mint& a) const { mint res(*this); return res /= a; } friend ostream& operator<<(ostream& os, const mint& m) { os << m.x; return os; } }; long long modpow(long long x, long long n, long long p = 1000000007) { if (n == 0) return 1 % p; if (n % 2 == 0) return modpow(x * x % p, n / 2, p); else return x * modpow(x, n - 1, p) % p; } void Main() { long long N; cin >> N; vl v(N); for (long long i = 0; i < N; i++) cin >> v[i]; long long flg = (v[0] > 0); long long ans = 0; long long acc = v[0]; for (long long i = 1; i < N; i++) { acc += v[i]; if (flg && acc >= 0) { ans += acc + 1; acc = -1; } else if (!flg && acc <= 0) { ans += -acc + 1; acc = 1; } flg ^= 1; } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long t = 1; for (long long i = 0; i < t; i++) Main(); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; int a[100000]; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } int cnt = 0; int sum = 0; for (int i = 0; i < n; i++) { if (sum + a[i] == 0) { cnt++; if (i == 0) { sum = a[1] > 0 ? -1 : 1; } else { sum = sum > 0 ? -1 : 1; } } else if (sum > 0 && sum + a[i] > 0) { cnt += sum + a[i] + 1; sum = -1; } else if (sum < 0 && sum + a[i] < 0) { cnt += 1 - sum - a[i]; sum = 1; } else { sum += a[i]; } } cout << cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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 = None 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) break else: S = S0 + a if get_sign(S0) == get_sign(S): ans += abs(get_sign(S)*(-1) - S) S = get_sign(S)*(-1) elif get_sign(S)==0: ans += 1 S = get_sign(S0)*(-1) 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
UNKNOWN
N = gets.to_i a = gets.split.map(&:to_i) # 偶数番目を負に ans_even = 0 a_even = a.dup # 奇数番目を負に ans_odd = 0 a_odd = a.dup if a_even[0] >= 0 while a_even[0] > 0 a_even[0] -= 1 ans_even += 1 end elsif a_odd[0] <= 0 while a_odd[0] < 0 a_odd[0] += 1 ans_odd += 1 end end sum_even = 0 sum_odd = 0 a_even.each_with_index do |ai, i| if sum_even + ai == 0 if i % 2 == 0 a_even[i] -= 1 ans_even += 1 else a_even[i] += 1 ans_even += 1 end end if sum_even*(sum_even+a_even[i]) > 0 if i % 2 == 0 while sum_even + a_even[i] >= 0 a_even[i] -= 1 ans_even += 1 end else while sum_even + a_even[i] <= 0 a_even[i] += 1 ans_even += 1 end end end sum_even += a_even[i] end a_odd.each_with_index do |ai, i| if sum_odd + ai == 0 if i % 2 == 1 a_odd[i] -= 1 ans_odd += 1 else a_odd[i] += 1 ans_odd += 1 end end if sum_odd*(sum_odd+a_odd[i]) > 0 if i % 2 == 1 while sum_odd + a_odd[i] >= 0 a_odd[i] -= 1 ans_odd += 1 end else while sum_odd + a_odd[i] <= 0 a_odd[i] += 1 ans_odd += 1 end end end sum_odd += a_odd[i] end puts [ans_even, ans_odd].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
python3
n=int(input()) a=list(map(int,input().split())) now=a[0] flag=abs(a[0])//a[0] c=0 #print(c,flag) for i in range(1,n): tmp=now+a[i] if not tmp*flag<0: c+=abs(flag*-1-tmp) now=flag*-1 else: now=tmp flag*=-1 #print(c,flag) print(c)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double PI = 3.1415926535897932384626433832795; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; bool isDiffer(long long a, long long b) { if (b == 0) return false; if (((a > 0) && (b < 0)) || ((a < 0) && (b > 0))) return true; else return false; } int main() { ios::sync_with_stdio(false); long long n; cin >> n; vector<long long> v; vector<long long> vv; for (int i = 0; i < n; i++) { long long t; cin >> t; v.push_back(t); vv.push_back(t); } long long ans[2] = {0}; if (v[0] == 0) { v[0] = 1; ans[0] += 1; ans[1] += 1; } for (int j = 0; j < 2; j++) { v[0] = (j == 0) ? (v[0] * -1) : v[0]; long long os = v[0]; for (int i = 1; i < n; i++) { if (!isDiffer(os, v[i] + os)) { long long ob = (os >= 0) ? -1 : 1; ans[j] += llabs(ob - os - v[i]); v[i] = ob - os; } os += v[i]; } v = vv; } cout << min(ans[0], ans[1]) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
n = gets.to_i arr = gets.chomp.split(" ").map(&:to_i) count = 0 def check(i,arr) if arr[i] > 0 arr[1] -= 1 elsif arr[i] < 0 arr[1] += 1 else if i == arr.size - 1 arr[1] += 1 return end check(i+1,arr) end end num = arr[0] + arr[1] if num == 0 if arr[2] > 0 arr[1] -= 1 elsif arr[2] < 0 arr[1] += 1 else check(3,arr) end end num = arr[0] + arr[1] (2...arr.size).each do |i| diff = num + arr[i] # puts %(num : #{num}) # puts %(diff : #{diff}) if num > 0 if diff > 0 arr[i] -= diff.abs+1 count += diff.abs+1 end else if diff < 0 arr[i] += diff.abs+1 count += diff.abs+1 end end if diff == 0 if num > 0 arr[i] -= 1 else arr[i] += 1 end count += 1 end num += arr[i] end #p arr puts count
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include"bits/stdc++.h" using namespace std; int main(){ int n; cin>>n; vector<int> x; int temp,ans=0; for(int i=0;i!=n;++i){ cin>>temp; x.push_back(temp); } if(!x[0]){ x[0]=1; ++ans; int val,ind; for(int i=0;i!=n;++i){ if(!x[i]){ val=x[i]; ind=i; break; } } if((val>0 && ind%2) || (val<0 && !(ind%2)) x[0]=-1; } int sum=x[0]; for(int i=1;i!=n;++i){ int sum2=sum+x[i]; if(sum*sum2>=0){ ans+=abs(sum2)+1; if(sum<0) sum2=1; else sum2=-1; } sum=sum2; } 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
using System; using System.Collections.Generic; using System.Linq; using System.Numerics; using static System.Math; using static AtCoderTemplate.MyExtensions; using static AtCoderTemplate.MyInputOutputs; using static AtCoderTemplate.MyNumericFunctions; using static AtCoderTemplate.MyAlgorithm; namespace AtCoderTemplate { public class Program { public static void Main (string[] args) { var n = ReadInt (); var a = ReadLongs (); // evenが+ var evenCount = 0L; { var sum0 = a[0] > 0 ? a[0] : 1; var count = a[0] > 0 ? 0 : Abs (a[0]); foreach (var i in Enumerable.Range (1, n - 1)) { var sum1 = sum0 + a[i]; if (IsEven (i) && sum1 < 0) { count += Abs (sum1) + 1; sum0 = 1; } else if (IsOdd (i) && sum1 > 0) { count += Abs (sum1) + 1; sum0 = -1; } else { sum0 = sum1; } } if (sum0 == 0) count += 1; evenCount = count; } var oddCount = 0L; { var sum0 = a[0] < 0 ? a[0] : -1; var count = a[0] < 0 ? 0 : Abs (a[0]); foreach (var i in Enumerable.Range (1, n - 1)) { var sum1 = sum0 + a[i]; if (IsOdd (i) && sum1 < 0) { count += Abs (sum1) + 1; sum0 = 1; } else if (IsEven (i) && sum1 > 0) { count += Abs (sum1) + 1; sum0 = -1; } else { sum0 = sum1; } } if (sum0 == 0) count += 1; oddCount = count; } Print (Min (evenCount, oddCount)); } } public static class MyInputOutputs { /* Input & Output*/ public static int ReadInt () { return int.Parse (Console.ReadLine ()); } public static long ReadLong () { return long.Parse (Console.ReadLine ()); } public static List<int> ReadInts () { return Console.ReadLine ().Split (' ').Select (c => int.Parse (c)).ToList (); } public static List<long> ReadLongs () { return Console.ReadLine ().Split (' ').Select (c => long.Parse (c)).ToList (); } public static List<List<int>> ReadIntColumns (int n) { /* 入力例 A1 B1 A2 B2 ... An Bn 出力例 [[A1,A2,...,An], [B1,B2,...,Bn]] */ var rows = Enumerable.Range (0, n).Select (i => ReadInts ()).ToList (); var m = rows.FirstOrDefault ()?.Count () ?? 0; return Enumerable.Range (0, m).Select (i => rows.Select (items => items[i]).ToList ()).ToList (); } public static List<List<long>> ReadLongColumns (int n) { /* 入力例 A1 B1 A2 B2 ... An Bn 出力例 [[A1,A2,...,An], [B1,B2,...,Bn]] */ var rows = Enumerable.Range (0, n).Select (i => ReadLongs ()).ToList (); var m = rows.FirstOrDefault ()?.Count () ?? 0; return Enumerable.Range (0, m).Select (i => rows.Select (items => items[i]).ToList ()).ToList (); } public static void Print<T> (T item) { Console.WriteLine (item); } public static void PrintIf<T1, T2> (bool condition, T1 trueResult, T2 falseResult) { if (condition) { Console.WriteLine (trueResult); } else { Console.WriteLine (falseResult); } } public static void PrintRow<T> (IEnumerable<T> list) { /* 横ベクトルで表示 A B C D ... */ if (!list.IsEmpty ()) { Console.Write (list.First ()); foreach (var item in list.Skip (1)) { Console.Write ($" {item}"); } } Console.Write ("\n"); } public static void PrintColomn<T> (IEnumerable<T> list) { /* 縦ベクトルで表示 A B C D ... */ foreach (var item in list) { Console.WriteLine (item); } } public static void Print2DArray<T> (IEnumerable<IEnumerable<T>> sources) { foreach (var row in sources) { PrintRow (row); } } } public static class MyNumericFunctions { public static bool IsEven (int a) { return a % 2 == 0; } public static bool IsEven (long a) { return a % 2 == 0; } public static bool IsOdd (int a) { return !IsEven (a); } public static bool IsOdd (long a) { return !IsEven (a); } /// <summary> /// 順列の総数を得る /// O(N-K) /// </summary> /// <param name="n">全体の数</param> /// <param name="k">並べる数</param> /// <param name="divisor">返り値がlongを超えないようにdivisorで割った余りを得る</param> /// <returns>nPk (をdivisorで割った余り)</returns> public static long nPk (int n, int k, long divisor) { if (k > n) { return 0L; } else { return Enumerable.Range (n - k + 1, k).Aggregate (1L, ((i, m) => (i * m) % divisor)); } } public static long nPk (int n, int k) { if (k > n) { return 0L; } else { return Enumerable.Range (n - k + 1, k).Aggregate (1L, ((i, m) => (i * m))); } } /// <summary> /// 階乗を得る /// O(N) /// </summary> /// <param name="n"></param> /// <param name="divisor">返り値がlongを超えないようにdivisorで割った余りを得る</param> /// <returns>n! (をdivisorで割った余り)</returns> public static long Fact (int n, long divisor) { return nPk (n, n, divisor); } public static long Fact (int n) { return nPk (n, n); } /// <summary> /// 組み合わせの総数を得る /// </summary> /// <param name="n"></param> /// <param name="k"></param> /// <returns>nCk</returns> public static long nCk (int n, int k) { if (k > n) { return 0L; } else { return nPk (n, k) / Fact (k); } } /// <summary> /// 最大公約数を得る /// O(log N) /// </summary> /// <param name="m">自然数</param> /// <param name="n">自然数</param> /// <returns></returns> public static long GCD (long m, long n) { // GCD(m,n) = GCD(n, m%n)を利用 // m%n = 0のとき、mはnで割り切れるので、nが最大公約数 if (m <= 0L || n <= 0L) throw new ArgumentOutOfRangeException (); if (m < n) return GCD (n, m); while (m % n != 0L) { var n2 = m % n; m = n; n = n2; } return n; } /// <summary> /// 最小公倍数を得る /// O(log N) /// </summary> /// <param name="m"></param> /// <param name="n"></param> /// <returns></returns> public static long LCM (long m, long n) { var ans = checked ((long) (BigInteger.Multiply (m, n) / GCD (m, n))); return ans; } /// <summary> /// 約数列挙(非順序) /// O(√N) /// </summary> /// <param name="m">m > 0</param> /// <returns></returns> public static IEnumerable<long> Divisor (long m) { if (m == 0) throw new ArgumentOutOfRangeException (); var front = Enumerable.Range (1, (int) Sqrt (m)) .Select (i => (long) i) .Where (d => m % d == 0); return front.Concat (front.Where (x => x * x != m).Select (x => m / x)); } /// <summary> /// 公約数列挙(非順序) /// O(√N) /// </summary> /// <param name="m">m > 0</param> /// <param name="n">n > 0</param> /// <returns></returns> public static IEnumerable<long> CommonDivisor (long m, long n) { if (m < n) return CommonDivisor (n, m); return Divisor (m).Where (md => n % md == 0); } } public static class MyAlgorithm { /// <summary> /// 二分探索法 /// O(log N) /// </summary> /// <param name="list">探索するリスト</param> /// <param name="predicate">条件の述語関数</param> /// <param name="ng">条件を満たさない既知のindex</param> /// <param name="ok">条件を満たす既知のindex</param> /// <typeparam name="T">順序関係を持つ型(IComparableを実装する)</typeparam> /// <returns>条件を満たすindexの内、境界に最も近いものを返す</returns> public static int BinarySearch<T> (IList<T> list, Func<T, bool> predicate, int ng, int ok) where T : IComparable<T> { while (Abs (ok - ng) > 1) { int mid = (ok + ng) / 2; if (predicate (list[mid])) { ok = mid; } else { ng = mid; } } return ok; } /// <summary> /// 辺の集まりを操作するオブジェクト /// </summary> public class Edge { long[, ] edge; public int NodeNum { get; } public Edge (int nodeNum, long overDistance) { var edge = new long[nodeNum, nodeNum]; foreach (var i in Enumerable.Range (0, nodeNum)) { foreach (var j in Enumerable.Range (0, nodeNum)) { if (i != j) { edge[i, j] = overDistance; } else { edge[i, j] = 0; } } } this.edge = edge; this.NodeNum = nodeNum; } public Edge (Edge edge) { this.edge = new long[edge.NodeNum, edge.NodeNum]; foreach (var i in Enumerable.Range (0, edge.NodeNum)) { foreach (var j in Enumerable.Range (0, edge.NodeNum)) { this.edge[i, j] = edge.GetLength (i, j); } } this.NodeNum = edge.NodeNum; } public List<List<long>> ToList () { return Enumerable.Range (0, NodeNum).Select (i => Enumerable.Range (0, NodeNum).Select (j => edge[i, j] ).ToList () ).ToList (); } public void Add (int node1, int node2, long distance) { edge[node1, node2] = distance; } public long GetLength (int node1, int node2) { return edge[node1, node2]; } } /// <summary> /// ワーシャルフロイド法 /// O(N^3) /// </summary> /// <param name="edge">Edgeオブジェクト</param> /// <param name="nodeNum">ノードの数</param> /// <returns>各ノード間の最短距離を辺として持つEdgeオブジェクト</returns> public static Edge WarshallFloyd (Edge edge) { var res = new Edge (edge); foreach (var b in Enumerable.Range (0, edge.NodeNum)) { foreach (var a in Enumerable.Range (0, edge.NodeNum)) { foreach (var c in Enumerable.Range (0, edge.NodeNum)) { res.Add (a, c, Min (res.GetLength (a, c), res.GetLength (a, b) + res.GetLength (b, c))); } } } return res; } } public static class MyExtensions { // AppendとPrependが、.NET Standard 1.6からの追加で、Mono 4.6.2 はそれに対応して仕様はあるが、実装がない public static IEnumerable<T> Append<T> (this IEnumerable<T> source, T element) { return source.Concat (Enumerable.Repeat (element, 1)); } public static IEnumerable<T> Prepend<T> (this IEnumerable<T> source, T element) { return Enumerable.Repeat (element, 1).Concat (source); } // TakeLastとSkipLastが、.Net Standard 2.1からの追加で、Mono 4.6.2 はそれに対応していない public static IEnumerable<T> TakeLast<T> (this IEnumerable<T> source, int count) { return source.Skip (source.Count () - count); } public static IEnumerable<T> SkipLast<T> (this IEnumerable<T> source, int count) { return source.Take (source.Count () - count); } public static bool IsEmpty<T> (this IEnumerable<T> source) { return !source.Any (); } /// <summary> /// インデックスiの位置の要素からk個取り除く /// O(N) /// </summary> public static IEnumerable<T> TakeAwayRange<T> (this IEnumerable<T> source, int i, int count) { return source.Take (i).Concat (source.Skip (i + count)); } /// <summary> /// インデックスiの位置の要素を取り除く /// O(N) /// </summary> public static IEnumerable<T> TakeAwayAt<T> (this IEnumerable<T> source, int i) { return source.TakeAwayRange (i, 1); } /// <summary> /// インデックスiの位置にシーケンスを挿入する /// O(N + K) /// </summary> public static IEnumerable<T> InsertEnumAt<T> (this IEnumerable<T> source, int i, IEnumerable<T> inserted) { return source.Take (i).Concat (inserted).Concat (source.Skip (i)); } /// <summary> /// 順列を得る /// O(N!) /// </summary> public static IEnumerable<IEnumerable<T>> Perm<T> (this IEnumerable<T> source, int n) { if (n == 0 || source.IsEmpty () || source.Count () < n) { return Enumerable.Empty<IEnumerable<T>> (); } else if (n == 1) { return source.Select (i => new List<T> { i }); } else { var nexts = source.Select ((x, i) => new { next = source.Take (i).Concat (source.Skip (i + 1)), selected = source.Take (i + 1).Last () }); return nexts.SelectMany (next => Perm (next.next, n - 1).Select (item => item.Prepend (next.selected))); } } /// <summary> /// シーケンスの隣り合う要素を2引数の関数に適用したシーケンスを得る /// </summary> /// <para>O(N)</para> /// <param name="source">元のシーケンス</param> /// <param name="func">2引数関数</param> /// <example>[1,2,3,4].MapAdjacent(f) => [f(1,2), f(2,3), f(3,4)]</example> public static IEnumerable<TR> MapAdjacent<T1, TR> (this IEnumerable<T1> source, Func<T1, T1, TR> func) { var list = source.ToList (); return Enumerable.Range (1, list.Count - 1) .Select (i => func (list[i - 1], list[i])); } /// <summary> /// 累積項を要素にもつシーケンスを得る(初項は、first) /// <para>O(N)</para> /// </summary> /// <param name="source">元のシーケンス</param> /// <param name="func">2引数関数f</param> /// <param name="first">func(first, source[0])のための初項</param> /// <example> [1,2,3].Scanl1(f,0) => [0, f(0,1), f(f(0,1),2), f(f(f(0,1),2),3)]</example> public static IEnumerable<TR> Scanl<T, TR> (this IEnumerable<T> source, TR first, Func<TR, T, TR> func) { var list = source.ToList (); var result = new List<TR> { first }; foreach (var i in Enumerable.Range (0, source.Count ())) { result.Add (func (result[i], list[i])); } return result; } /// <summary> /// 累積項を要素にもつシーケンスを得る(初項は、source.First()) /// <para>O(N)</para> /// </summary> /// <param name="source">元のシーケンス</param> /// <param name="func">2引数関数f</param> /// <example> [1,2,3].Scanl1(f) => [1, f(1,2), f(f(1,2),3)]</example> public static IEnumerable<T> Scanl1<T> (this IEnumerable<T> source, Func<T, T, T> func) { var list = source.ToList (); var result = new List<T> { list[0] }; foreach (var i in Enumerable.Range (1, source.Count () - 1)) { result.Add (func (result[i - 1], list[i])); } return result; } /// <summary> /// 昇順にソートしたインデックスを得る /// </summary> /// <para>O(N * log N)</para> public static IEnumerable<int> SortIndex<T> (this IEnumerable<T> source) { return source .Select ((item, i) => new { Item = item, Index = i }) .OrderBy (x => x.Item) .Select (x => x.Index); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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()) original_A = [int(x) for x in input().split()] ans = [] for k in range(2): A = original_A.copy() count = 0 if k%2: if A[0] >= 0: A[0] = -1 count += (abs(A[0]) +1) else: if A[0] < 0: A[0] = 1 count += (abs(A[0]) +1) sum_before = A[0] #print('***', k, A, '***') for i in range(n): if i == 0: continue sum_for_i = sum_before + A[i] #print('[',i,']: before',sum_before,'after',sum_for_i, 'before', A) if sum_for_i == 0 and sum_before > 0: #print("case 1") A[i] -= 1 count += 1 elif sum_for_i == 0 and sum_before <0: #print("case 2") A[i] += 1 count += 1 elif sum_before >0 and sum_for_i>0: #print("case 3") count += (abs(sum_for_i)+1) A[i] -= (abs(sum_for_i)+1) elif sum_before <0 and sum_for_i<0: #print("case 4") count += (abs(sum_for_i)+1) A[i] += (abs(sum_for_i)+1) #print('[',i,']: ','modified', A, 'count', count) sum_before += A[i] ans.append(count) print(min(ans))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; 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); ll l = 0; ll res = 0; for (long long i = 0; i < n; i++) { if (l == 0) { for (long long j = 0; j < n; j++) { if (a[j] != 0) { a[0] = a[j] / abs(a[j]); if (j & 1 && j != 0) a[0] *= (-1); 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]; } } } cout << res << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
import System.IO import Control.Monad import Data.List main = do n <- getLine as <- fmap read . words <<= getLine putStrLn . show . head . [xs | xs <- iterate mani as, jouken n xs] mani [] = [] mani [a] = [a-1, a+1] mani (a:as) = [(a-1) : x | x <- mani as] ++ [(a+1) : x | x <- mani as] subsum xs j = sum . take j $ xs jouken n xs = length [i | i <- [1 .. n-1], (subsum xs i) * (subsum xs (i+1)) >= 0] == 0
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys n = int(input()) a = [int(n) for n in input().split()] sum = [0]*n sum[0] = a[0] ans = 0 for i in range(1,n): sum[i] = sum[i-1] while((sum[i]+a[i])*sum[i-1] >= 0): if(sum[i-1] > 0): ans+=sum[i-1] + a[i]+1 a[i]-=sum[i-1] + a[i]+1 else: ans+=1 - sum[i-1] - a[i] a[i]+=1 - sum[i-1] - a[i] print(a) sum[i] += a[i] print(ans) # print(a) # print(sum)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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())) d1 = [0] * (n + 1) d2 = [0] * (n + 1) ans1 = 0 ans2 = 0 for i in range(n): d1[i+1] += d1[i] + a[i] d2[i+1] += d2[i] + a[i] # - + - + - の順番 for i in range(n): d1[i+1] = d1[i] + a[i] if(i % 2 == 0 and d1[i+1] >= 0): ans1 += abs(d1[i+1])+1 d1[i + 1] = -1 if(i % 2 == 1 and d1[i+1] <= 0): ans1 += abs(d1[i+1])+1 d1[i + 1] = 1 #print(d1, ans1) #print(ans1) #print() # + - + - + の順番 for i in range(n): d2[i+1] = d2[i] + a[i] if(i % 2 == 1 and d2[i+1] >= 0): ans2 += abs(d1[i + 1]) + 1 d2[i+1] = -1 if(i % 2 == 0 and d2[i+1] <= 0): ans2 += abs(d2[i + 1]) + 1 d2[i+1] = 1 #print(d2, ans2) #print(ans2) #print() ans = min(ans1, ans2) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = int64_t; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int DX[] = {1, 1, 0, -1, -1, -1, 0, 1}; int DY[] = {0, -1, -1, -1, 0, 1, 1, 1}; int n; ll a[100010]; ll hoge() { ll ans = 0; ll temp = 0; for (int(i) = 0; (i) < (n); (i)++) { if (temp > 0 && temp + a[i] > 0) { ans += abs(-1 - temp - a[i]); temp = -1; } else if (temp < 0 && temp + a[i] < 0) { ans += abs(1 - temp - a[i]); temp = 1; } else if (temp + a[i] == 0) { if (temp > 0) { temp = -1; } else { temp = 1; } ans += 1; } else { temp += a[i]; } } return ans; } void solve() { cin >> n; for (int(i) = 0; (i) < (n); (i)++) cin >> a[i]; ll ans1 = hoge(); ll temp = 0; if (a[0] > 0) { temp += (a[0] * (-1) - 1); a[0] = -1; } else if (a[0] < 0) { temp = (a[0] * (-1) + 1); a[0] = 1; } else { temp = 1; a[0] = -1; } ll ans2 = hoge() + temp; cout << min(ans1, ans2) << endl; } int main() { solve(); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) res = 0 sum = 0 for i in range(n - 1): sum += a[i] if sum * (sum+a[i+1]) >= 0: if sum > 0: temp = -1 - sum-a[i+1] a[i+1] += temp res += abs(temp) else: temp = 1 - sum-a[i+1] a[i+1] += temp res += temp print(res)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def read_input(): n = int(input()) alist = list(map(int, input().split())) return n, alist def get_sign(x): if x > 0: return 1 elif x < 0: return -1 return 0 def submit(): n, alist = read_input() s = alist[0] sign = get_sign(s) edit = 0 for a in alist[1:]: temp = s + a temp_sign = get_sign(temp) if sign == temp_sign: edit += temp_sign * temp temp -= temp_sign * temp if temp == 0: edit += 1 temp -= sign s = temp sign = get_sign(s) print(edit) if __name__ == '__main__': submit()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) ttl = a[0] cst = 0 if a[0]>0: flg = 1 elif a[0] == 0: for i in range(1,n): if a[i]==0: continue elif a[i]>0: cst += 1 ttl -= 1 flg = -1 break elif a[i]<0: cst += 1 ttl += 1 flg = 1 break else: flg = -1 for i in range(1,n): ttl += a[i] if ttl*flg < 0: flg *= -1 else: if flg > 0: memo = abs(ttl)+abs(-1) ttl -= memo cst += memo elif flg < 0: memo = abs(ttl)+abs(-1) ttl += memo cst += memo flg *= -1 print(cst)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < (n); ++i) cin >> a[i]; int sum = 0; int sign = 1; int ans_p = 0; for (int i = 0; i < (n); ++i) { sum += a[i]; if ((sign > 0) && (sum <= 0)) { ans_p += (1 - sum); sum = 1; } else if ((sign < 0) && (sum >= 0)) { ans_p += abs(-1 - sum); sum = -1; } sign = sign == 1 ? -1 : 1; } sum = 0; sign = -1; int ans_n = 0; for (int i = 0; i < (n); ++i) { sum += a[i]; if ((sign > 0) && (sum <= 0)) { ans_n += (1 - sum); sum = 1; } else if ((sign < 0) && (sum >= 0)) { ans_n += abs(-1 - sum); sum = -1; } sign = sign == 1 ? -1 : 1; } if (ans_p > ans_n) { cout << ans_n << endl; } else { cout << ans_p << 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 = 0 prev_sm = A[0] # total to i - 1 for i in range(1, N): # if prev_sum is plus and a is more minus than prev_sum. if prev_sm > 0 and prev_sm + A[i] < 0: prev_sm += A[i] continue # if prev_sum is plus and a is larger than or equal to prev_sum. elif prev_sm > 0 and prev_sm + A[i] >= 0: ans += prev_sm + A[i] + 1 A[i] -= prev_sm + A[i] + 1 prev_sm += A[i] # if prev_sum is minus and a is more plus than prev_sum. elif prev_sm < 0 and prev_sm + A[i] > 0: prev_sm += A[i] continue # if prev_sum is minus and a is more smaller than or equal to prev_sum. elif prev_sm < 0 and prev_sm + A[i] <= 0: ans += -(prev_sm + A[i] - 1) A[i] += -(prev_sm + A[i] + 1) prev_sm += A[i] print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void answer1() { cin.tie(0); ios_base::sync_with_stdio(false); int n; cin >> n; vector<int> a(n); for (int& a_i : a) { cin >> a_i; } int count = 0; int sum = 0; int count2 = 0; int sum2 = 0; bool is_positive = a.at(0) > 0; for (int i = 0; i < a.size(); i++) { sum += a.at(i); if (is_positive) { if (sum <= 0) { int diff = 1 - sum; count += diff; sum += diff; } if (sum2 >= 0) { int diff = 1 + sum2; count2 += diff; sum2 -= diff; } } else { if (sum >= 0) { int diff = 1 + sum; count += diff; sum -= diff; } if (sum2 <= 0) { int diff = 1 - sum2; count2 += diff; sum2 += diff; } } is_positive = !is_positive; } cout << min(count, count2) << endl; } int main() { answer1(); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys n, *a = map(int, sys.stdin.read().split()) def main(): c1 = c2 = s1 = s2 = 0 for i in range(n): s1 += a[i] s2 += a[i] if i & 1: if s1 >= 0: c1 += s1 + 1 s1 = -1 if s2 <= 0: c2 += 1 - s2 s2 = 1 else: if s1 <= 0: c1 += 1 - s2 s1 = 1 if s2 >= 0: c2 += s2 + 1 s2 = -1 print(min(c1, c2)) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, a[100000]; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int preans, ans = 0, sum = 0; for (int i = 0, j = 0; j < 2; i++) { int presum = sum, cnt = 0; sum += a[i]; if (presum < 0) { if (sum <= 0) { while (sum <= 0) { sum++; cnt++; } } } else if (presum > 0) { if (sum >= 0) { while (sum >= 0) { sum--; cnt++; } } } else continue; ans += cnt; if (i == n - 1) { if (!j) preans = ans; else ans = min(preans, ans); j++; i = 0; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } long long wa = a[0]; long long ans = 0; for (int i = 1; i < n; i++) { if (wa > 0) { wa += a[i]; if (wa > 0) { ans += wa + 1; wa -= (wa + 1); } else if (wa == 0) { ans++; wa--; } } else if (wa < 0) { wa += a[i]; if (wa < 0) { ans += -wa + 1; wa += -wa + 1; } else if (wa == 0) { ans++; wa++; } } else { if (a[i] > 0) wa--; else wa++; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int sumi = 0; int sumi1 = 0; int countPlus = 0; int countMinus = 0; int[] a = new int[n]; for(int i = 0; i < n; i++) { a[i] = scan.nextInt(); } // 奇数がプラス for(int i = 0; i < n; i++) { sumi1 = sumi + a[i]; if((i + 1) % 2 == 1) { if(sumi1 <= 0) { countPlus += Math.abs(sumi1) + 1; sumi1 += Math.abs(sumi1) + 1; } } else if((i + 1) % 2 == 0) { if(sumi1 >= 0) { countPlus += Math.abs(sumi1) + 1; sumi1 -= Math.abs(sumi1) + 1; } } sumi = sumi1; } sumi = 0; sumi1 = 0; // 奇数がマイナス for(int i = 0; i < n; i++) { sumi1 = sumi + a[i]; if((i + 1) % 2 == 1) { if(sumi1 >= 0) { countMinus += Math.abs(sumi1) + 1; sumi1 -= Math.abs(sumi1) + 1; } } else if((i + 1) % 2 == 0) { if(sumi1 <= 0) { countMinus += Math.abs(sumi1) + 1; sumi1 += Math.abs(sumi1) + 1; } } sumi = sumi1; } if(countPlus < countMinus) { System.out.println(countPlus); } else { System.out.println(countMinus); } scan.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; 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(long long int n) { if (n < 0) return 1; else if (n > 0) return 0; else return 2; } int main() { int n; long long int sum = 0; long long int ans = 0; long long int ans2 = 0; cin >> n; vector<long long 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
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int N; cin >> N; vector<ll> a(N); for (int i = 0; i < N; i++) cin >> a.at(i); ll sumO = a.at(0), sumE = a.at(0), countO = 0, countE = 0; for (int i = 1; i < N; i++) { ll O = a.at(i), E = a.at(i); if (i % 2 == 0) { if (sumE + E <= 0) { countE += abs(1 - (sumE + E)); E = 1 - sumE; } if (sumO + O >= 0) { countO += abs(-1 - (sumO + O)); O = -1 - sumO; } } else { if (sumO + O <= 0) { countO += abs(1 - (sumO + O)); O = 1 - sumO; } if (sumE + E >= 0) { countE += abs(-1 - (sumE + E)); E = -1 - sumE; } } sumE += E; sumO += O; } cout << min(countE, countO) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; string divide[4] = {"dream", "dreamer", "erase", "eraser"}; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) cin >> a[i]; int sum = 0; int plus = 0; for (int i = 0; i < n; ++i) { if (i % 2 == 0) { if (sum + a[i] <= 1) { plus += 1 - (sum + a[i]); sum = 1; } else sum += a[i]; } else { if (sum + a[i] >= -1) { plus += 1 + (sum + a[i]); sum = -1; } else sum += a[i]; } } int minus = 0; for (int i = 0; i < n; ++i) { if (i % 2 == 1) { if (sum + a[i] <= 1) { minus += 1 - (sum + a[i]); sum = 1; } else sum += a[i]; } else { if (sum + a[i] >= -1) { minus += 1 + (sum + a[i]); sum = -1; } else sum += a[i]; } } cout << min(plus, minus) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int sign(int x) { if (x > 0) return 1; else if (x < 0) return 0; else return -1; } int f(int t, int pre, int s) { if (s == 0) return abs(-1 - pre - t); else return abs((1 - t) - pre); } int main() { int n, a[100005]; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; int ans1 = 0, ans2 = 0; int s = 1, x = 0, tmp; for (int i = 1; i <= n; i++, s ^= 1) { tmp = x; x += a[i]; if (sign(x) != s) ans1 += f(tmp, a[i], s); if (not s) x = min(-1, x); else x = max(1, x); } s = 0, x = 0; for (int i = 1; i <= n; i++, s ^= 1) { tmp = x; x += a[i]; if (sign(x) != s) ans2 += f(tmp, a[i], s); if (not s) x = min(-1, x); else x = max(1, x); } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) a = [int(i) for i in input().split()] sam = a[0] old = sam num = 0 for i in range(1, len(a)): sam += a[i] if sam >= 0 and old > 0: num += (abs(sam) + 1) sam -= (sam + 1) elif sam <= 0 and old < 0: num += (abs(sam) + 1) sam -= (sam - 1) old = sam print(num)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> 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::cout << std::min(solve(va, va[0]), solve(va, va[0] > 0 ? -1 : 1, std::abs(va[0]) + 1)) << 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
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); } long long sumi = 0, val1 = 0; int ne = 0; if (a.at(0) == 0) { while (a.at(ne) == 0) { if (ne == 0) val1++; else val1 += 2; ne++; if (ne == n) break; } } long long val2 = val1; for (int i = ne; i < n; i++) { if (a.at(0) == 0 && sumi == 0) { if (ne % 2 == 0) sumi = -1; else sumi = 1; } if (i == 0) { sumi = a.at(i); continue; } if (i % 2 == 1) { if (sumi + a.at(i) < 0) sumi += a.at(i); else { val1 += (sumi + a.at(i) + 1); sumi = -1; } } else { if (sumi + a.at(i) > 0) sumi += a.at(i); else { val1 += (abs(sumi + a.at(i)) + 1); sumi = 1; } } } for (int i = ne; i < n; i++) { if (a.at(0) == 0 && sumi == 0) { if (ne % 2 == 0) sumi = 1; else sumi = -1; } if (i == 0) { sumi = a.at(i); continue; } if (i % 2 == 1) { if (sumi + a.at(i) > 0) sumi += a.at(i); else { val2 += (abs(sumi + a.at(i)) + 1); sumi = 1; } } else { if (sumi + a.at(i) < 0) sumi += a.at(i); else { val2 += (sumi + a.at(i) + 1); sumi = -1; } } } cout << min(val1, val2) << 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
object Main { def main(args: Array[String]): Unit = { import scala.io.StdIn.readLine val _ = readLine val datA = readLine.split(" ").map(_.toInt) val work = datA.map(a => (a, 0)).foldLeft((0, 0)){ case ((sum, count),(a, _)) => if (sum > 0) { if (sum + a >= 0) (-1, count + math.abs(sum + a) + 1) else (sum + a, count) } else if (sum < 0) { if (sum + a <= 0) (1, count + math.abs(sum + a) + 1) else (sum + a, count) } else { (a, count) } } val ans = work._2 println(ans) } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import numpy as np n = int(input()) a = list(map(int, input().split())) c = 0 sum = a[0] for i in range(1, n): if not (np.sign(sum) != np.sign(sum + a[i]) and sum + a[i] != 0): if sum > 0: c += a[i] + sum + 1 sum = -1 else: c += -a[i] - sum + 1 sum = 1 else: sum += a[i] print(c)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python2
if __name__ == '__main__': N = input() array = raw_input().split() ans = 0 total = int(array[0]) totalZero = False if total == 0: totalZero = True flag = False if total >= 0: flag = True for a in array[1:]: if totalZero == True: ans += 1 if a > 0: total = -1 else: total = 1 totalZero = False total += int(a) if total > 0 and flag == True: while True: ans += 1 total -= 1 if total == -1: flag = False break elif total < 0 and flag ==False: while True: ans += 1 total += 1 if total == 1: flag == True break elif total == 0: totalZero = True if total > 0: flag = True elif total < 0: flag = False if totalZero == True: ans += 1 print ans
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #include <boost/range/irange.hpp> #include <boost/range/adaptors.hpp> using namespace std; using namespace boost; using namespace boost::adaptors; using uint = unsigned int; using ll = long long int; using ull = unsigned long long int; int main() { ll n; cin >> n; ll count{0}, s_now{0}, s_prev{0}; vector<ll> as(n, 0); for (auto &&i: irange(0LL, n)){ cin >> as.at(i); } bool flag{false}; bool direction; for (auto &&i: irange(0LL, n)){ if (as.at(i)!=0){ direction = (i%2 != 0); flag = true; break; } } if (!flag)return as.size(); if (as.at(0)==0){ count++; s_prev = direction ? 1 : -1; } for (auto &&i: irange(1LL, n)){ s_now += as.at(i); // cout << " new s: " << s_now << endl; if (s_prev * s_now >= 0){ // cout << "kakikae because: " << s_prev * s_now << endl; ll target = s_prev < 0 ? 1 : -1; // cout << "from: " << s_now << " to: " << target << endl; count += abs(target - s_now); // cout << "count: " << count << endl; s_now = target; } s_prev = s_now; // cout << "old s: " << s_prev; } cout << count; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for(int i = 0 ; i < n ; i++) a[i] = sc.nextInt(); int sum = 0, ans = 0, ans2 = 0; // 偶数位置までの和:正、奇数位置までの和:負 for(int i = 0 ; i < n ; i++) { if(i % 2 == 0) { if(sum + a[i] >= 0) { ans += sum + a[i] + 1; sum = -1; } else { sum += a[i]; } } else { if(sum + a[i] <= 0) { ans += 1 - (sum + a[i]); sum = 1; } else { sum += a[i]; } } } sum = 0; // 偶数位置までの和:正、奇数位置までの和:負 for(int i = 0 ; i < n ; i++) { if(i % 2 == 1) { if(sum + a[i] >= 0) { ans2 += sum + a[i] + 1; sum = -1; } else { sum += a[i]; } } else { if(sum + a[i] <= 0) { ans2 += 1 - (sum + a[i]); sum = 1; } else { sum += a[i]; } } } System.out.println(ans2); System.out.println(Math.min(ans, ans2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
program ec12; var a,s:array[0..100000] of longint; n,m,i,j,ans,sum1,sum2,ans1:longint; begin readln(n); ans:=0; ans1:=0; s[0]:=0; for i:=1 to n do read(a[i]); if a[1]>0 then begin sum1:=a[1]; ans:=0; ans1:=a[1]+1; sum2:=-1; end else begin if a[1]=0 then begin sum1:=1; sum2:=-1; ans:=1; ans1:=1; end else begin ans:=(-a[1])+1; sum1:=1; ans1:=0; end; end; for i:=2 to n do begin if sum1>0 then begin if sum1+a[i]>=0 then begin inc(ans,sum1+a[i]+1); sum1:=-1; end else sum1:=sum1+a[i]; end else begin if sum1+a[i]<=0 then begin inc(ans,abs(sum1+a[i])+1); sum1:=1; end else sum1:=sum1+a[i]; end; if sum2>0 then begin if sum2+a[i]>=0 then begin inc(ans1,sum2+a[i]+1); sum2:=-1; end else sum2:=sum2+a[i]; end else begin if sum2+a[i]<=0 then begin inc(ans1,abs(sum2+a[i])+1); sum2:=1; end else sum2:=sum2+a[i]; end; end; if ans<ans1 then writeln(ans) else writeln(ans1); end.
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int, input().split())) cntA, sumA = 0, 0 for i in range(N): sumA += A[i] if i % 2 == 0: if sumA < 0: cntA += abs(sumA) + 1 sumA += abs(sumA) + 1 else: if sumA > 0: cntA += abs(sumA) + 1 sumA -= abs(sumA) + 1 cntB, sumB = 0, 0 for i in range(N): sumB += A[i] if i % 2 != 0: if sumB < 0: cntB += abs(sumB) + 1 sumB += abs(sumB) + 1 else: if sumB > 0: cntB += abs(sumB) + 1 sumB -= abs(sumB) + 1 print(min(cntA, cntB))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
parseInt(x) = parse(Int, x) function main() n = readline() |> parseInt a = map(parseInt, split(readline())) b = Array{Int}(n) k = 0 if a[1] > 0 b[1] = 1 k += abs(a[1]-1) else b[1] = -1 k += abs(a[1]+1) end for i in 2:n b[i] = a[i]+b[i-1] if b[i]*b[i-1] >= 0 if b[i-1] < 0 k += abs(b[i]-1) b[i] = 1 else k += abs(b[i]+1) b[i] = -1 end end end c = Array{Int}(n) l = 0 if a[1] > 0 c[1] = -1 l += abs(a[1]+1) else c[1] = 1 l += abs(a[1]-1) end for i in 2:n c[i] = a[i]+c[i-1] if c[i]*c[i-1] >= 0 if c[i-1] < 0 l += abs(c[i]-1) c[i] = 1 else l += abs(c[i]+1) c[i] = -1 end end end print(min(k,l)) end main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; int a[100010]; int solve(int sign) { int ans = 0; int sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (sign * sum <= 0) { ans += 1 - sum * sign; sum = sign; } sign *= -1; } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long t = 1; while (t--) { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; cout << min(solve(1), solve(-1)); } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
n = gets.to_i as = gets.split.map(&:to_i) cnt1 = 0 sum = as[0] n.times.with_index(1) do |_,i| break if as[i].nil? prev = sum < 0 ? "n" : "p" tmp = sum sum += as[i] if prev == "n" if sum == 0 sum += 1 cnt1 += 1 elsif sum < 0 sum = 1 cnt1 += tmp.abs - as[i].abs + 1 end else if sum == 0 sum -= 1 cnt1 += 1 elsif sum > 0 sum = -1 cnt1 += as[i].abs + tmp + 1 end end end cnt2 = 0 sum = as[0] * -1 n.times.with_index(1) do |_,i| break if as[i].nil? prev = sum < 0 ? "n" : "p" tmp = sum sum += as[i] if prev == "n" if sum == 0 sum += 1 cnt2 += 1 elsif sum < 0 sum = 1 cnt2 += tmp.abs - as[i].abs + 1 end else if sum == 0 sum -= 1 cnt2 += 1 elsif sum > 0 sum = -1 cnt2 += as[i].abs + tmp + 1 end end end puts [cnt1, cnt2].min