Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> data(N); for (int i = 0; i < N; i++) cin >> data[i]; int count = 0; int ans = data[0]; int saisyo; for (int i = 1; i < N; i++) { ans += data[i]; if (i % 2 == 0) { while (ans <= 0) { ans++; count++; } } else { while (ans >= 0) { ans--; count++; } } } saisyo = count; count = 0; ans = data[0]; while (ans >= 0) { ans--; count++; } for (int i = 0; i < N; i++) { ans += data[i]; if (i % 2 != 0) { while (ans <= 0) { ans++; count++; } } else { while (ans >= 0) { ans--; count++; } } } saisyo = min(saisyo, count); cout << saisyo << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = (long long)1e9; const long long MOD = (long long)1e9 + 7; const long long MAX = 510000; vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1}; template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } int main() { long long N, sum, ans = 0; cin >> N; long long A[N], B[N]; bool f = false; for (long long i = 0; i < N; i++) { cin >> A[i]; B[i] = A[i]; } if (A[0] == 0) { A[0] = 1; B[0] = -1; f = true; } sum = A[0]; for (long long i = 1; i < N; i++) { if (sum * (sum + A[i]) >= 0) { ans += abs(A[i] - sum); if (sum > 0) sum = -1; else sum = 1; } else sum += A[i]; } if (f) { long long sumb = B[0], ansb = 0; for (long long i = 1; i < N; i++) { if (sumb * (sumb + B[i]) >= 0) { ans += abs(B[i] - sumb); if (sumb > 0) sumb = -1; else sumb = 1; } else sumb += B[i]; } ans = min(ans, ansb); } 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 P = pair<int, int>; using ll = long long; int main() { int n; cin >> n; int a[114514]; int cnt1 = 0, sum1 = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (i % 2 == 0) { if (sum1 == 0) { if (a[i] > 0) { sum1 += a[i]; } else if (a[i] < 0) { cnt1 += 1 - a[i]; sum1 += 1; } else { sum1 += 1; cnt1 += 1; } } else { int plus = 1 - sum1; if (a[i] >= plus) { sum1 += a[i]; } else { cnt1 += plus - a[i]; sum1 = 1; } } } else { if (sum1 == 0) { if (a[i] < 0) { sum1 += a[i]; } else if (a[i] > 0) { cnt1 += a[i] + 1; sum1--; } else { sum1--; cnt1 += 1; } } else { int minus = -1 - sum1; if (a[i] <= minus) { sum1 += a[i]; } else { cnt1 += a[i] - minus; sum1 = -1; } } } } int ans1 = cnt1; cnt1 = 0; sum1 = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (sum1 == 0) { if (a[i] < 0) { sum1 += a[i]; } else if (a[i] > 0) { cnt1 += a[i] + 1; sum1--; } else { sum1--; cnt1 += 1; } } else { int minus = -1 - sum1; if (a[i] <= minus) { sum1 += a[i]; } else { cnt1 += a[i] - minus; sum1 = -1; } } } else { if (sum1 == 0) { if (a[i] > 0) { sum1 += a[i]; } else if (a[i] < 0) { cnt1 += 1 - a[i]; sum1 += 1; } else { sum1 += 1; cnt1 += 1; } } else { int plus = 1 - sum1; if (a[i] >= plus) { sum1 += a[i]; } else { cnt1 += plus - a[i]; sum1 = 1; } } } } cout << min(ans1, cnt1) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long sum = 0, num = 0; for (int i = 0; i < n; i++) { long long a; cin >> a; if (sum > 0 && sum + a >= 0) { num += sum + a + 1; a -= sum + a + 1; } else if (sum < 0 && sum + a <= 0) { num += abs(sum + a) + 1; a += abs(sum + a) + 1; } sum += a; } 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 <bits/stdc++.h> using namespace std; int a[100000]; int getTotal(int n, int dir) { int total{}, sum{}; for (int i{0}; i < n; ++i) { int diff{}; if (dir > 0) { while (sum + a[i] + diff <= 0) { ++diff; ++total; } } else { while (sum + a[i] + diff >= 0) { --diff; ++total; } } sum += a[i] + diff; dir *= -1; } return total; } int main() { int n; scanf("%d", &n); for (int i{0}; i < n; ++i) scanf("%d", &a[i]); int try1 = getTotal(n, 1); int try2 = getTotal(n, -1); printf("%d\n", ((try1) < (try2) ? (try1) : (try2))); 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
fn read<T: std::str::FromStr>() -> T { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().parse().ok().unwrap() } fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>() .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect() } use std::cmp::min; fn main() { let n: usize = read(); let a: Vec<i64> = read_vec(); //start with plus let mut cnt1: i64 = 0; let mut sum = a[0]; if sum < 0 { cnt1 += 1 - sum; sum = 1; } for i in 1..n { if sum > 0 { if a[i] + sum < 0 { sum += a[i]; } else { cnt1 += a[i] + sum + 1; sum = -1; } } else { if a[i] + sum > 0 { sum += a[i]; } else { cnt1 += 1 - sum - a[i]; sum = 1; } } } //start with minus let mut cnt2: i64 = 0; let mut sum = a[0]; if sum > 0 { cnt2 += 1 + sum; sum = -1; } for i in 1..n { if sum > 0 { if a[i] + sum < 0 { sum += a[i]; } else { cnt2 += a[i] + sum + 1; sum = -1; } } else { if a[i] + sum > 0 { sum += a[i]; } else { cnt2 += 1 - sum - a[i]; sum = 1; } } } println!("{}", min(cnt1, cnt2)); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<iostream> #include<string> #include<algorithm> #include<vector> #include<iomanip> #include<math.h> #include<complex> #include<queue> #include<deque> #include<stack> #include<map> #include<set> #include<bitset> #include<functional> #include<assert.h> #include<numeric> using namespace std; #define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i ) #define rep(i,n) REP(i,0,n) #define pint pair<int,int> #define pll pair<ll,ll> using ll = long long; const int inf=1e9+7; const ll longinf=1LL<<60 ; const ll mod=1e9+7 ; int main(){ int n; cin >> n; ll a[n]; rep(i,n)cin >> a[i]; ll sum[n]={},sum2[n]={}; ll temp=0,temp2=0; rep(i,n){ if(i==0){ sum[i]=a[i]; if(a[i]<=0){ temp+=-a[i]+1; sum[i]=1; } } else{ sum[i]=a[i]+sum[i-1]; if(sum[i]*sum[i-1]>=0){ if(i%2==0){ temp+=-sum[i]+1; sum[i]=1; }else{ temp+=sum[i]+1; sum[i]=-1; } } } } rep(i,n){ if(i==0){ sum2[i]=a[i]; if(sum2[i]>=0){ temp2+=a[i]+1; sum2[i]=-1; } } else{ sum2[i]=a[i]+sum2[i-1]; if(sum2[i]*sum2[i-1]>=0){ if(i%2==0){ temp2+=sum2[i]+1; sum2[i]=-1; }else{ temp2+=-sum2[i]+1; sum[i]=1; } } } } // cout << temp << ' ' << temp2 << endl; cout << min(temp,temp2) << 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> x(n), y, z, cnt(4, 0); for (int i = 0; i < n; i++) cin >> x[i]; auto f1 = [&n](vector<int>& a) { int tmp_sum; int cnt = 0; if (a[0] > 0) { tmp_sum = a[0]; } else { tmp_sum = 1; cnt += abs(a[0]) + 1; } for (int i = 1; i < n; i++) { tmp_sum += a[i]; if (i % 2 == 1 && tmp_sum >= 0) { cnt += (abs(tmp_sum) + 1); tmp_sum -= (abs(tmp_sum) + 1); } if (i % 2 == 0 && tmp_sum <= 0) { cnt += (abs(tmp_sum) + 1); tmp_sum += (abs(tmp_sum) + 1); } } return cnt; }; auto f2 = [&n](vector<int>& a) { int tmp_sum; int cnt = 0; if (a[0] < 0) { tmp_sum = a[0]; } else { tmp_sum = -1; cnt += abs(a[0]) + 1; } for (int i = 1; i < n; i++) { tmp_sum += a[i]; if (i % 2 == 0 && tmp_sum >= 0) { cnt += (abs(tmp_sum) + 1); tmp_sum -= (abs(tmp_sum) + 1); } if (i % 2 == 1 && tmp_sum <= 0) { cnt += (abs(tmp_sum) + 1); tmp_sum += (abs(tmp_sum) + 1); } } return cnt; }; if (x[0] == 0) { y = x; z = x; y[0] = 1; z[0] = -1; cnt[0] = f1(y); cnt[1] = f2(y); cnt[2] = f1(z); cnt[3] = f2(z); auto s = min_element(cnt.begin(), cnt.end()); cout << *s + 1 << endl; } else { cnt[0] = f1(x); cnt[1] = f2(x); auto s = min_element(cnt.begin(), cnt.begin() + 2); cout << *s << endl; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int solve(int *a, int n) { int count = 0; int calc = 0; int state, pstate; if (a[0] < 0) state = -1; if (a[0] > 0) state = 1; for (int i = 1; i < n; i++) { pstate = state; int tmp = a[i] + calc; if (tmp < 0) state = -1; if (tmp == 0) state = 0; if (tmp > 0) state = 1; if (pstate == state) { if (state == -1) { count += 1 - tmp; calc += 1 - tmp; state = 1; } else if (state == 1) { count += tmp + 1; calc += -1 - tmp; state = -1; } } if (state == 0) { if (pstate == -1) { count += 1; calc += 1; state = 1; } else if (pstate == 1) { count += 1; calc += -1; state = -1; } } } return count; } int main() { int n; int ans; int *a; cin >> n; a = new int[n]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) a[i] = a[i - 1] + a[i]; if (a[0] == 0) { int bs, cs; int *b = new int[n]; int *c = new int[n]; for (int i = 0; i < n; i++) b[i] = a[i] + 1; for (int i = 0; i < n; i++) c[i] = a[i] - 1; bs = solve(b, n); cs = solve(c, n); ans = bs < cs ? bs : cs; } else ans = solve(a, n); 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 = 0, suma = 0, sumb = 0; cin >> n; long long counta = 0, countb = 0; int* a = new int[n]; int* b = new int[n]; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; } if (a[0] >= 0) { counta += (a[0] + 1); a[0] = -1; } suma += a[0]; for (int i = 1; i < n; i++) { if (suma <= -1) { if (suma + a[i] <= 0) { counta += (1 - suma - a[i]); suma = 1; } } else if (suma >= 1) { if (suma + a[i] >= 0) { counta += (suma + a[i] + 1); suma = -1; } } } if (b[0] <= 0) { countb += (1 - b[0]); b[0] = 1; } sumb += b[0]; for (int i = 1; i < n; i++) { if (sumb <= -1) { if (sumb + b[i] <= 0) { countb += (1 - sumb - b[i]); sumb = 1; } } else if (sumb >= 1) { if (sumb + b[i] >= 0) { countb += (sumb + b[i] + 1); sumb = -1; } } } if (counta < countb) cout << counta; else cout << countb; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; bool diff(long long a, long long b) { if (a < 0 && b > 0) return true; if (a > 0 && b < 0) return true; return false; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector<int> a(n); for (int i = (0); i < (n); ++i) cin >> a[i]; long long ans = 0, sum = a[0]; for (int i = (1); i < (n); ++i) { if (diff(sum + a[i], sum)) { sum += a[i]; } else { long long need = (sum > 0 ? -1 : 1); long long now = need - sum; ans += abs(now - a[i]); sum = need; } } long long tmp = abs(a[0]) + 1; sum = (a[0] > 0 ? -1 : 1); for (int i = (1); i < (n); ++i) { if (diff(sum + a[i], sum)) { sum += a[i]; } else { long long need = (sum > 0 ? -1 : 1); long long now = need - sum; tmp += abs(now - a[i]); sum = need; } } cout << min(ans, tmp) << '\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
java
import java.util.*; public class Main { private static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int n = sc.nextInt(); long sum = sc.nextLong(); long ret = 0; long tmp = 0; for (int i = 1;i < n;i++) { long a = sc.nextInt(); tmp = sum; sum += a; if ((tmp<0&&sum>=0)||(tmp>=0&&sum<0)) continue; long l = Math.abs(sum)+1; if (sum>=0) { sum -= l; } else { sum += l; } ret += l; } if (sum==0) ret++; System.out.println(ret); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int resp = 0; long long s = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { if (s + a[i] > 0) { s += a[i]; } else { resp += 1 - s - a[i]; s = 1; } } else { if (s + a[i] < 0) { s += a[i]; } else { resp += s + a[i] + 1; s = -1; } } } int resm = 0; s = 0; for (int i = 0; i < n; i++) { if (i % 2 == 1) { if (s + a[i] > 0) { s += a[i]; } else { resm += 1 - s - a[i]; s = 1; } } else { if (s + a[i] < 0) { s += a[i]; } else { resm += s + a[i] + 1; s = -1; } } } int res = min(resp, resm); cout << res << "\n"; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def sequence(N: int, A: list) -> int: s = A[0] op = 0 for a in A[1:]: if s < 0: if s + a > 0: # OK s = s + a continue else: op += 1 - (s + a) s = 1 else: # s > 0 if s + a < 0: # OK s = s + a continue else: op += (s + a) - (-1) s = -1 return op if __name__ == "__main__": N = int(input()) A = [int(s) for s in input().split()] ans = sequence(N, A) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(int,input().split())) #waszero=0 #cnt=0 #total=a[0] def calc(n,a,total): cnt=0 for i in range(1,n): if total*(total+a[i])<0: total+=a[i] else: if total>0: shouldbe=-total-1 cnt+=abs(shouldbe-a[i]) total=-1 elif total<0: shouldbe=-total+1 cnt+=abs(shouldbe-a[i]) total=1 else: pass return cnt cntlsit=[] cntlsit.append(calc(n,a,a[0])) cntlsit.append(calc(n,a,1)) cntlsit.append(calc(n,a,-1)) print(min(cntlsit))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<bits/stdc++.h> using namespace std; #define mod 1000000007 #define ll long long #define mp make_pair #define pb push_back #define ff first #define ss second #define set0(a) memset ((a), 0 , sizeof(a)) #define set1(a) memset((a),-1,sizeof (a)) #define pi pair<int, int> #define ps pair<string, string> #define pl pair<long, long> #define pll pair<long long, long long> #define vll vector<long long> #define vl vector<long> #define vi vector<int> #define vs vector<string> #define vps vector< ps > #define vpi vector< pi > #define vpl vector< pl > #define vpll vector< pll > #define flash ios_base::sync_with_stdio(false); cin.tie(NULL); #define tc(t,T) for(long long t=0;t<T;t++) #define rep(i,s,n,d) for(long long i=s;i<n;i=i+d) bool sortbysec(const pll &a, const pll &b) { return (a.second < b.second); } void func(void) { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); } int main(){ ll n; cin>>n; ll a[n]; rep(i,0,n,1){ cin>>a[i]; } ll count1=0; if(a[0]==0){ if(a[1]>=0){ a[0]=-1; } else a[0]=1; count1++; } ll sum[n]={}; sum[0]=a[0]; rep(i,1,n,1){ sum[i]=sum[i-1]+a[i]; } ll sum1=a[0]; rep(i,1,n,1){ ll d=0; ll dif=0; if(sum1>0){ if(a[i]+sum1>=0){ d=-1; dif=abs(a[i]+sum1-d); count1=count1+dif; sum1=d; } else{ sum1=sum1+a[i]; } } else{ if(a[i]+sum1<=0){ d=1; dif=abs(a[i]+sum1-d); count1=count1+dif; sum1=d; } else{ sum1=sum1+a[i]; } } } cout<<count1<<endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# https://atcoder.jp/contests/abc059/tasks/arc072_a # C - Sequence import copy N = int(input().split()[0]) a_list = list(map(int, input().split())) c = 0 w_list = copy.copy(a_list) c_list = [] for mode in [0, 1]: s_list = [] w_list = copy.copy(a_list) c = 0 for i in range(N): s = sum(w_list[:i+1]) if i % 2 == mode and s < 0: w_list[i] += abs(s) c += abs(s) elif i % 2 != mode and s >= 0: w_list[i] -= abs(s+1) c += abs(s+1) s = sum(w_list[:i+1]) if s == 0: a = w_list[i] w_list[i] = a + 1 if a >= 0 else a - 1 c += 1 s = sum(w_list[:i+1]) s_list.append(s) c_list.append(c) ans = min(c_list) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
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() # pattern 1 s = alist[0] sign = get_sign(s) edit = 0 if sign != 1: edit += 1 - s s = 1 sign = get_sign(s) 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) edit1 = edit # pattern 2 s = alist[0] sign = get_sign(s) edit = 0 if sign != -1: edit += s + 1 s = -1 get_sign(s) 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) edit2 = edit print(min(edit1, edit2)) 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<long long> a(n); for (int i = 0; i < (int)(n); i++) cin >> a.at(i); long long cnt = 0; long long wa = 0; long long wa2 = 0; wa = a.at(0); for (int i = 0; i < n - 1;) { wa2 = wa + a.at(i + 1); if (wa > 0) { if (wa2 < 0) { i++; wa = wa2; } else if (wa2 > 0) { cnt++; a.at(i + 1) -= 1; } else if (wa2 == 0) { cnt++; a.at(i + 1) -= 1; } } else if (wa < 0) { if (wa2 < 0) { cnt++; a.at(i + 1) += 1; } else if (wa2 > 0) { i++; wa = wa2; } else if (wa2 == 0) { cnt++; a.at(i + 1) += 1; } } } cout << cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n, i, j, ans = 0, sum = 0, flag; cin >> n; vector<long long> a(n); for (i = 0; i < n; i++) { cin >> a[i]; } sum += a[0]; if (sum == 0) { sum++; for (i = 0; i < n; i++) { if (a[i] != 0) { if (i % 2 == 0) { sum = 1; } else { sum = -1; } break; } } } for (i = 1; i < n; i++) { if (sum > 0) { flag = 1; } else { flag = 0; } if (flag == 1) { sum += a[i]; if (sum >= 0) { ans += (sum + 1); sum = -1; } } else { sum += a[i]; if (sum <= 0) { ans += 1 - sum; sum = 1; } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long cnt1 = 0, cnt2 = 0; long long sumv = 0; for (int i = 0; i < n; i++) { sumv += a[i]; if (i % 2 == 1 && sumv < 0) { sumv = 1; cnt1 += abs(sumv) + 1; } else if (i % 2 == 1 && sumv > 0) { sumv = -1; cnt1 += abs(sumv) + 1; } } sumv = 0; for (int i = 0; i < n; i++) { sumv += a[i]; if (i % 2 == 0 && sumv > 0) { sumv = -1; cnt2 += abs(sumv) + 1; } else if (i % 2 == 1 && sumv < 0) { sumv = 1; cnt2 += abs(sumv) + 1; } } cout << min(cnt1, cnt2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int A[100005], N; long long ch(int init) { int t = A[0]; long long res = init; for (int i = 1; i < N; i++) { if (t * (t + A[i]) >= 0) { if (t > 0) { res += abs(t + 1 + A[i]); t = -1; } else { res += abs(t) + 1 - A[i]; t = 1; } } else { t += A[i]; } } return res; } int main() { scanf("%d", &N); for (int i = 0; i < N; i++) scanf("%d", &A[i]); long long t = ch(0), a, p; if (A[0] == 0) t = 15; a = -A[1]; a > 0 ? a++ : a--; p = abs(a - A[0]); t = min(t, ch(p)); printf("%lld\n", t); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> template <class T, class S> void cmin(T &a, const S &b) { if (a > b) a = b; } template <class T, class S> void cmax(T &a, const S &b) { if (a < b) a = b; } using namespace std; signed main() { long long int n; cin >> n; vector<long long int> v(n), sum(n); for (long long int i = 0; i < n; i++) cin >> v[i]; long long int ans = 0; bool used = true, flag = false; for (long long int i = 0; i < n; i++) { if (i) sum[i] = v[i] + sum[i - 1]; else sum[i] = v[i]; if (used) { if (sum[i] > 0) { flag = true; used = false; continue; } if (sum[i] < 0) { flag = false; used = false; continue; } ans++; continue; } if (flag) { if (sum[i] < 0) { flag = false; continue; } if (sum[i] >= 0) { flag = false; ans += abs(sum[i]) + 1; sum[i] = -1; continue; } } else { if (sum[i] > 0) { flag = true; continue; } if (sum[i] <= 0) { flag = true; ans += abs(sum[i]) + 1; sum[i] = 1; continue; } } } 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
#include <bits/stdc++.h> int main() { int n; int a, sum[10000000], cnt[2] = {}, b; scanf("%d", &n); scanf("%ld", &a); sum[0] = a; for (int i = 1; i < n; i++) { scanf("%ld", &a); sum[i] = sum[i - 1] + a; } b = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && sum[i] + b >= 0) { cnt[0] += sum[i] + b + 1; b -= sum[i] + b + 1; } else if (i % 2 == 1 && sum[i] + b <= 0) { cnt[0] -= sum[i] + b; cnt[0]++; b += 1 - (sum[i] + b); } } b = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && sum[i] + b <= 0) { cnt[1] -= sum[i] + b; cnt[1]++; b += 1 - (sum[i] + b); } else if (i % 2 == 1 && sum[i] + b >= 0) { cnt[1] += sum[i] + b + 1; b -= sum[i] + b + 1; } } if (cnt[0] < cnt[1]) printf("%ld\n", cnt[0]); else printf("%ld\n", cnt[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
cpp
#include <bits/stdc++.h> using namespace std; using Graph = vector<vector<long long>>; const long long mod = 1000000007; long long digitsum(long long n, long long b) { if (b < 2) return -1; if (n < b) return n; return digitsum(n / b, b) + n % b; } long long mpow(long long a, long long x); long long m_inv(long long n); vector<long long> split(long long n, long long a); string xal_number(long long n, long long x); long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; } long long lcm(long long x, long long y) { return x * y / gcd(x, y); } class Factorial { private: vector<long long> fac; public: Factorial(long long N) { fac.push_back(1); for (long long i = (0); i < (N); ++i) fac.push_back(fac[i] * (i + 1) % mod); } long long fact(long long a) { return fac[a]; } long long ifac(long long a) { return m_inv(fac[a]); } long long cmb(long long n, long long r); }; struct UnionFind { vector<long long> par; UnionFind(long long n = 1) { init(n); } void init(long long n = 1) { par.resize(n); for (long long i = (0); i < (n); ++i) par[i] = -1; } long long root(long long x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } long long size(long long x) { return -par[root(x)]; } bool issame(long long x, long long y) { return root(x) == root(y); } bool connect(long long x, long long y); }; signed main() { long long n; cin >> n; vector<long long> a(n); for (long long i = (0); i < (n); ++i) cin >> a[i]; long long ans = 0; vector<long long> S(n + 1); S[0] = 0; for (long long i = (0); i < (n); ++i) { S[i + 1] = S[i] + a[i]; if (S[i + 1] * S[i] > 0) { ans += abs(S[i + 1]) + 1; S[i + 1] = (S[i] > 0) ? -1 : 1; } if (S[i + 1] == 0) { ans += 1; S[i + 1] += (S[i] > 0) ? -1 : 1; } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long int> v(n); for (int i = 0; (i) < (n); i++) { cin >> v[i]; } long long int result_a = 0, result_b = abs(v[0]) + 1; vector<long long int> tmp(n); tmp[0] = v[0]; for (int i = (1); (i) < (n); (i)++) { tmp[i] = v[i] + tmp[i - 1]; if (tmp[i] * tmp[i - 1] >= 0) { result_a += abs(tmp[i]) + 1; tmp[i] = ((tmp[i - 1] > 0) ? -1 : 1); } } tmp[0] = v[0] > 0 ? -1 : 1; for (int i = (1); (i) < (n); (i)++) { tmp[i] = v[i] + tmp[i - 1]; if (tmp[i] * tmp[i - 1] >= 0) { result_b += abs(tmp[i]) + 1; tmp[i] = ((tmp[i - 1] > 0) ? -1 : 1); } } cout << min(result_a, result_b) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int N; long long a[100001]; int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> a[i]; } long long ans = 0; long long ans2 = 0; long long R = a[0]; if (a[0] <= 0) { ans = 1 - a[0]; R = 1; } for (int i = 1; i < N; i++) { if (R < 0) { R += a[i]; if (R >= 0) { ans += R + 1; R = -1; } } else { R += a[i]; if (R <= 0) { ans += 1 - R; R = 1; } } } R = a[0]; if (a[0] >= 0) { ans2 = a[0] - 1; R = -1; } for (int i = 1; i < N; i++) { if (R < 0) { R += a[i]; if (R <= 0) { ans2 += 1 - R; R = 1; } } else { R += a[i]; if (R >= 0) { ans2 += R + 1; R = -1; } } } long long ans3 = min(ans, ans2); cout << ans3 << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) count = 0 sum_ = 0 for i in range(n): if sum_ * (sum_+a[i]) >=0 and i!=0: if sum_ > 0: count += sum_+a[i]+1 a[i] = -sum_-1 elif sum_ < 0: count += -sum_-a[i]+1 a[i] = -sum_+1 sum_ += a[i] print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int, input().split())) def sol(S): ret = 0 B = [S] for a in A[1:]: b = a if S * (S + b) > 0: b = (abs(S) + 1) * (1 if S < 0 else -1) if S + b == 0: b = b - 1 if b < 0 else b + 1 ret += abs(b - a) S += b B.append(b) return ret ans = min( sol(A[0]), sol(-A[0] // abs(A[0])) + abs(A[0]) + 1 if A[0] != 0 else 10**18 ) 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; class C { public: template <typename T> int sgn(T val) { return (T(0) < val) - (val < T(0)); } void solve(std::istream& in, std::ostream& out) { ios::sync_with_stdio(false); int n, prevSign; in >> n; vector<int> a(n), p(n); for (int i = 0; i < n; ++i) { in >> a[i]; } int steps = 0; int steps2 = 0; p[0] = a[0]; if (a[0] != 0) { for (int i = 0; i < n - 1; ++i) { p[i + 1] = p[i] + a[i + 1]; if (sgn(p[i]) == -1) { if (p[i + 1] == 0) { ++p[i + 1]; ++steps; } else if (sgn(p[i + 1]) == -1) { steps += -p[i + 1] + 1; p[i + 1] = 1; } } else if (sgn(p[i]) == 1) { if (p[i + 1] == 0) { --p[i + 1]; ++steps; } else if (sgn(p[i + 1]) == 1) { steps += p[i + 1] + 1; p[i + 1] = -1; } } } } else { p[0] = 1; for (int i = 0; i < n - 1; ++i) { p[i + 1] = p[i] + a[i + 1]; if (sgn(p[i]) == -1) { if (p[i + 1] == 0) { ++p[i + 1]; ++steps; } else if (sgn(p[i + 1]) == -1) { steps += -p[i + 1] + 1; p[i + 1] = 1; } } else if (sgn(p[i]) == 1) { if (p[i + 1] == 0) { --p[i + 1]; ++steps; } else if (sgn(p[i + 1]) == 1) { steps += p[i + 1] + 1; p[i + 1] = -1; } } } p[0] = -1; for (int i = 0; i < n - 1; ++i) { p[i + 1] = p[i] + a[i + 1]; if (sgn(p[i]) == -1) { if (p[i + 1] == 0) { ++p[i + 1]; ++steps2; } else if (sgn(p[i + 1]) == -1) { steps2 += -p[i + 1] + 1; p[i + 1] = 1; } } else if (sgn(p[i]) == 1) { if (p[i + 1] == 0) { --p[i + 1]; ++steps2; } else if (sgn(p[i + 1]) == 1) { steps2 += p[i + 1] + 1; p[i + 1] = -1; } } } steps = min(steps, steps2); } out << steps << endl; } }; int main() { C solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using LL = long long int; using LD = long double; using pii = pair<int, int>; using pll = pair<LL, LL>; using pdd = pair<double, double>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vl = vector<LL>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vd = vector<double>; using vvd = vector<vd>; using vs = vector<string>; using vb = vector<bool>; using vvb = vector<vb>; const int INF = (1 << 30) - 1; const LL INF64 = ((LL)1 << 62) - 1; const double PI = 3.1415926535897932384626433832795; const int dy[] = {0, 1, 0, -1}; const int dx[] = {1, 0, -1, 0}; int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } LL gcd(LL x, LL y) { return y ? gcd(y, x % y) : x; } int n; vi a; int solve(int num) { int res = 0, sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (sum * num <= 0) { res += abs(sum - num); sum = num; } num *= -1; } return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; a.resize(n); for (int i = 0; i < n; i++) { cin >> a[i]; } cout << min(solve(1), solve(-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; long long change_num(long long p[], int N) { long long res = 0; long long sum = p[0]; for (int i = 1; i < N; i++) { if ((sum < 0 && (sum + p[i]) > 0) || (sum > 0 && (sum + p[i]) < 0)) { sum += p[i]; continue; } if (sum > 0 && sum + p[i] >= 0) { sum += p[i]; while (sum >= 0) { res++; sum--; } continue; } if (sum < 0 && sum + p[i] <= 0) { sum += p[i]; while (sum <= 0) { res++; sum++; } continue; } } return res; } int main() { int N; cin >> N; long long a[N]; for (int i = 0; i < N; i++) cin >> a[i]; long long ans = 0; long long sum = a[0]; if (a[0] == 0) { long long plus_ans = 0; a[0] = 1; plus_ans = change_num(a, N) + 1; long long minus_ans = 0; a[0] = -1; minus_ans = change_num(a, N) + 1; if (plus_ans < minus_ans) { ans = plus_ans; } else { ans = minus_ans; } cout << ans << endl; return 0; } else { ans = change_num(a, N); 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]; int res1 = 0; int sum1 = 0; for (int i = 0; i < n; ++i) { sum1 += a[i]; if (i % 2 == 0 && sum1 <= 0) { res1 += (1 - sum1), sum1 = 1; } if (i % 2 != 0 && sum1 >= 0) { res1 += (sum1 + 1), sum1 = -1; } } int res2 = 0; int sum2 = 0; for (int i = 0; i < n; ++i) { sum2 += a[i]; if (i % 2 == 0 && sum2 >= 0) { res2 += (sum2 + 1), sum2 = -1; } if (i % 2 != 0 && sum2 <= 0) { res2 += (1 - sum2), sum2 = 1; } } cout << min(res1, res2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, ans = 0; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } long sum = v[0]; for (int i = 1; i < n; i++) { if (sum < 0) { if (v[i] + sum <= 0) { ans += abs(1 - (v[i] + sum)); sum = 1; } else { sum += v[i]; } } else { if (v[i] + sum >= 0) { ans += abs(-1 - v[i] - sum); sum = -1; } else { sum += v[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; const int INF = 999999999; const int MOD = (int)1e9 + 7; const int EPS = 1e-9; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, a; cin >> n; vector<int> A; for (int i = (0); i < (n); ++i) { cin >> a; A.push_back(a); } int mn = INF; for (int j = (0); j < (2); ++j) { int ans = 0; int sum = A[0]; if (j == 1) { if (sum > 0) { ans += sum + 1; sum = -1; } else { ans += (-sum + 1); sum = 1; } } for (int i = (1); i < (n); ++i) { a = A[i]; if (sum > 0) { sum += a; if (sum >= 0) { ans += (sum + 1); sum = -1; } } else { sum += a; if (sum <= 0) { ans += (-sum + 1); sum = 1; } } } mn = min(mn, ans); } cout << mn << 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; int N[100000], E[100000]; cin >> n; for (int i = 0; i < n; i++) { cin >> N[i]; } for (int i = 0; i < n; i++) { E[i] = N[i]; } int sumA = 0, sumB = 0; int ansA = 0, ansB = 0; for (int i = 0; i < n; i++) { sumA = sumA + N[i]; if (i % 2 == 0 && sumA <= 0) { N[i] = N[i] - sumA + 1; ansA = ansA - sumA + 1; sumA = 1; } if (i % 2 == 1 && sumA >= 0) { N[i] = N[i] - sumA - 1; ansA = ansA + sumA + 1; sumA = -1; } } for (int i = 0; i < n; i++) { sumB = sumB + E[i]; if (i % 2 == 0 && sumB >= 0) { E[i] = E[i] - sumB - 1; ansB = ansB + sumB + 1; sumB = -1; } if (i % 2 == 1 && sumB <= 0) { E[i] = E[i] - sumB + 1; ansB = ansB - sumB + 1; sumB = 1; } } int ans; ans = min(ansA, ansB); 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
// -*- coding:utf-8-unix -*- #![allow(dead_code)] #![allow(unused_imports)] use std::cmp::*; use std::collections::*; use std::fs::File; use std::io::prelude::*; use std::io::*; use std::mem; use std::str; use std::vec; const INF: i64 = 1223372036854775807; const MEM_SIZE: usize = 202020; const MOD: i64 = 1000000007; // const MOD: i64 = 998244353; use std::cmp::*; use std::collections::*; use std::io::stdin; use std::io::stdout; use std::io::Write; #[allow(dead_code)] fn read<T: std::str::FromStr>() -> T { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().parse().ok().unwrap() } #[allow(dead_code)] fn readi() -> (i64) { let mut str = String::new(); let _ = stdin().read_line(&mut str).unwrap(); let mut iter = str.split_whitespace(); iter.next().unwrap().parse::<i64>().unwrap() } #[allow(dead_code)] fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>() .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect() } #[allow(dead_code)] fn read_vec2<T: std::str::FromStr>(n: u32) -> Vec<Vec<T>> { (0..n).map(|_| read_vec()).collect() } #[allow(dead_code)] fn readii() -> (i64, i64) { let mut str = String::new(); let _ = stdin().read_line(&mut str).unwrap(); let mut iter = str.split_whitespace(); ( iter.next().unwrap().parse::<i64>().unwrap(), iter.next().unwrap().parse::<i64>().unwrap(), ) } #[allow(dead_code)] fn readiii() -> (i64, i64, i64) { let mut str = String::new(); let _ = stdin().read_line(&mut str).unwrap(); let mut iter = str.split_whitespace(); ( iter.next().unwrap().parse::<i64>().unwrap(), iter.next().unwrap().parse::<i64>().unwrap(), iter.next().unwrap().parse::<i64>().unwrap(), ) } #[allow(dead_code)] fn readuu() -> (usize, usize) { let mut str = String::new(); let _ = stdin().read_line(&mut str).unwrap(); let mut iter = str.split_whitespace(); ( iter.next().unwrap().parse::<usize>().unwrap(), iter.next().unwrap().parse::<usize>().unwrap(), ) } #[allow(dead_code)] fn readuuu() -> (usize, usize, usize) { let mut str = String::new(); let _ = stdin().read_line(&mut str).unwrap(); let mut iter = str.split_whitespace(); ( iter.next().unwrap().parse::<usize>().unwrap(), iter.next().unwrap().parse::<usize>().unwrap(), iter.next().unwrap().parse::<usize>().unwrap(), ) } #[allow(dead_code)] fn readuuuu() -> (usize, usize, usize, usize) { let mut str = String::new(); let _ = stdin().read_line(&mut str).unwrap(); let mut iter = str.split_whitespace(); ( iter.next().unwrap().parse::<usize>().unwrap(), iter.next().unwrap().parse::<usize>().unwrap(), iter.next().unwrap().parse::<usize>().unwrap(), iter.next().unwrap().parse::<usize>().unwrap(), ) } /// Equivalent to std::lowerbound and std::upperbound in c++ pub trait BinarySearch<T> { fn lower_bound(&self, x: &T) -> usize; fn upper_bound(&self, x: &T) -> usize; } impl<T: Ord> BinarySearch<T> for [T] { fn lower_bound(&self, x: &T) -> usize { let mut low = 0; let mut high = self.len(); while low != high { let mid = (low + high) / 2; match self[mid].cmp(x) { Ordering::Less => { low = mid + 1; } Ordering::Equal | Ordering::Greater => { high = mid; } } } low } fn upper_bound(&self, x: &T) -> usize { let mut low = 0; let mut high = self.len(); while low != high { let mid = (low + high) / 2; match self[mid].cmp(x) { Ordering::Less | Ordering::Equal => { low = mid + 1; } Ordering::Greater => { high = mid; } } } low } } fn solve() { let n: usize = read(); let vec: Vec<i64> = read_vec(); let mut s: i64 = 0; let mut res: i64 = 0; let mut flg = true; if vec[0] < 0 { flg = false; } for i in 0..n { s += vec[i]; if flg { if s < 1 { res += 1 - s; s = 1; } } else { if s > -1 { res += 1 + s; s = -1; } } // println!("{:?}", (s, res, flg)); flg ^= true; } println!("{:?}", res); } fn 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int count = 0; int sum = 0; vector<int> a(n); for (int i = 0; i < (int)(n); i++) cin >> a.at(i); int Ah = 0; int Bh = 0; int sumA = 0; int sumB = 0; if (a.at(0) == 0) { if (a.at(1) > 0) a.at(0) = -1; else a.at(0) = 1; count++; } for (int i = 0; i < n - 1; i++) { sumA += a.at(i); Ah = 0; Bh = 0; for (;;) { sumB = sumA + a.at(i + 1); if (sumA > 0) Ah = 1; else Ah = -1; if (sumB > 0) Bh = 1; else if (sumB < 0) Bh = -1; else Bh = 0; if ((Ah == 1 && Bh == -1) || (Ah == -1 && Bh == 1)) break; else if (Ah == 1 && Bh != -1) { a.at(i + 1) -= abs(sumB) + 1; count += abs(sumB) + 1; break; } else { a.at(i + 1) += abs(sumB) + 1; count += abs(sumB) + 1; break; } } } cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
fun main(args: Array<String>) { val n = readLine()?.toInt() ?: return val aList = readLine()?.split(" ")?.map { it.toLong() } ?: return val v1 = calc(aList, true) val v2 = calc(aList, false) println(Math.min(v1, v2)) } private fun calc(list: List<Long>, type: Boolean): Long { var count = 0L var sum = if (type) Math.abs(list[0]) else -Math.abs(list[0]) if (sum == 0L) { sum += if (type) 1 else -1 count++ } else { count += Math.abs(sum - list[0]) } for (i in 1 until list.size) { val sign = sum < 0 if (sign == (sum + list[i] > 0)) { sum += list[i] continue } val expected = if (sign) Math.abs(sum) + 1 else -Math.abs(sum) - 1 val diff = Math.abs(expected - list[i]) sum += expected count += diff } return count }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); int n; cin >> n; vector<long long> a(n); for (int i = 0; i < (int)(n); i++) cin >> a[i]; vector<long long> cusum(n); cusum[0] = a[0]; for (int i = 1; i < n; i++) { cusum[i] = cusum[i - 1] + a[i]; } int tc = 2; long long ans = 1e18; while (tc--) { long long sum = 0; long long tmp = 0; for (int i = 0; i < n; i++) { long long x = cusum[i] + sum; if (x > 0) { if ((tc && i % 2 == 0) || (tc == 0 && i % 2 == 1)) { continue; } else { tmp += x + 1; sum -= (x + 1); } } else { if ((tc && i % 2 == 0) || (tc == 0 && i % 2 == 1)) { tmp += ((-1) * x + 1); sum += ((-1) * x + 1); } else { continue; } } } if (cusum[n - 1] + sum == 0) tmp++; ans = min(ans, tmp); } 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
java
import java.io.IOException; import java.io.InputStream; import java.util.*; import java.util.function.IntFunction; import java.util.function.Supplier; import java.util.stream.IntStream; import java.util.stream.Stream; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(); int n=scanner.nextInt(); long[] a=new long[n+1]; for(int i=1;i<=n;i++){ a[i]=scanner.nextInt(); } Arrays.parallelPrefix(a,(c,b)->c+b); //put(Arrays.toString(a)); long ans=0; long ruiseki=0; for(int i=1;i<=n;i++){ //put(format("i=%d",i)); //put(format("ruiseki=%d",ruiseki)); long val=a[i]+ruiseki; long val_=a[i-1]+ruiseki; //put(format("val=%d",val)); //put(format("val_=%d",val_)); if(val==0){ int bit=Long.signum(val_); ruiseki+=-bit; ans+=Math.abs(bit); }else if(val>0&&val_>0){ ruiseki-=(val+1); ans+=Math.abs(val+1); }else if(val<0&&val_<0){ ruiseki+=Math.abs(val)+1; ans+=Math.abs(val)+1; } //put(ans); //put(); } put(ans); } public static void print(Object object){ System.out.print(object); } public static void put(Object object) { System.out.println(object); } public static void put(){ System.out.println(); } public static String format(String string, Object... args) { return String.format(string, args); } } final class Scanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } } final class Pair { final public int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int hashCode() { return x+y; } @Override public boolean equals(Object obj) { boolean result=super.equals(obj); if(obj.getClass()!=this.getClass()){ return false; } Pair pair=(Pair)obj; if(this.x==pair.x&&this.y==pair.y) return true; return false; } @Override public String toString() { return String.format("(%d,%d)", x, y); } } final class Tuple<T,V>{ //immutabl1でないことに注意(T,Vがmutableの場合変更可能) final public T t; final public V v; Tuple(T t,V v){ this.t=t; this.v=v; } @Override public int hashCode() { return (t.hashCode()+v.hashCode()); } @Override public boolean equals(Object obj) { if(obj.getClass()!=this.getClass()){ return false; } Tuple<T,V> tuple=(Tuple)obj; return tuple.t.equals(this.t)&&tuple.v.equals(this.v); } @Override public String toString() { return String.format("<Tuple>=<%s,%s>",t,v); } } final class LowerBoundComparator<T extends Comparable<? super T>> implements Comparator<T> { public int compare(T x, T y) { return (x.compareTo(y) >= 0) ? 1 : -1; } } final class UpperBoundComparator<T extends Comparable<? super T>> implements Comparator<T> { public int compare(T x, T y) { return (x.compareTo(y) > 0) ? 1 : -1; } } final class Util { static long gcd(long a,long b){ if(a%b==0)return b; return gcd(b,a%b); } static long lcm(long a,long b){ long gcd=gcd(a,b); long result=b/gcd; return a*result; } static long kaijoMod(int n,int mod){ if(n<1) return -1; long result=1; for(int i=n;i>1;i--){ result*=i; result%=mod; } return result; } static <T extends Comparable> Map<T,Integer> count(List<T> list){ //副作用 Collections.sort(list); Map<T,Integer> result=new HashMap<>(); int l=0,r=0; while(l<list.size()){ while(r<list.size()-1&&list.get(r).equals(r+1)){ r++; } result.put(list.get(r),r-l+1); r++; l=r; } return result; } static Map<Integer,Integer> count(int[] array){ //副作用 Arrays.sort(array); Map<Integer,Integer> result=new HashMap<>(); int l=0,r=0; while(l<array.length){ while(r<array.length-1&&array[r]==array[r+1]){ r++; } result.put(array[l],r-l+1); r++; l=r; } return result; } static String toStringBWS(Iterable iterable){ Iterator ite=iterable.iterator(); return toStringBWS(ite); } static String toStringBWS(Iterator ite){ StringBuilder sb=new StringBuilder(); sb.append(ite.next()); while(ite.hasNext()){ sb.append(" "); sb.append(ite.next()); } return sb.toString(); } static String toStringBWS(int[] array){ StringBuilder sb=new StringBuilder(); for(int i=0;i<array.length-1;i++){ sb.append(array[i]); sb.append(" "); } sb.append(array[array.length-1]); return sb.toString(); } static String toStringBWS(long[] array){ StringBuilder sb=new StringBuilder(); for(int i=0;i<array.length-1;i++){ sb.append(array[i]); sb.append(" "); } sb.append(array[array.length-1]); return sb.toString(); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(int,input().split())) s=[0]*n s[0]=a[0] cnt=0 for i in range(1,n): s[i]=s[i-1]+a[i] if 0<s[i] and 0<s[i-1]: cnt+=abs(-1-s[i]) s[i]=-1 elif s[i]<0 and s[i-1]<0: cnt+=abs(1-s[i]) s[i]=1 elif s[i]==0: if s[i-1]<0: cnt+=1 s[i]=1 elif 0<s[i-1]: cnt+=1 s[i]=-1 print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using i_i = pair<int, int>; using ll_ll = pair<ll, ll>; using d_ll = pair<double, ll>; using ll_d = pair<ll, double>; using d_d = pair<double, double>; template <class T> using vec = vector<T>; static constexpr ll LL_INF = 1LL << 60; static constexpr int I_INF = 1 << 28; static constexpr double PI = static_cast<double>(3.14159265358979323846264338327950288); static constexpr double EPS = numeric_limits<double>::epsilon(); static map<type_index, const char* const> scanType = {{typeid(int), "%d"}, {typeid(ll), "%lld"}, {typeid(double), "%lf"}, {typeid(char), "%c"}}; template <class T> static void scan(vector<T>& v); [[maybe_unused]] static void scan(vector<string>& v, bool isWord = true); template <class T> static inline bool chmax(T& a, T b); template <class T> static inline bool chmin(T& a, T b); template <class T> static inline T gcd(T a, T b); template <class T> static inline T lcm(T a, T b); template <class A, size_t N, class T> static void Fill(A (&arr)[N], const T& val); template <class T> T mod(T a, T m); template <class Monoid> struct SegmentTree { using F = function<Monoid(Monoid, Monoid)>; int sz; vector<Monoid> seg; const F f; const Monoid M1; SegmentTree(int n, const F f, const Monoid& M1) : f(f), M1(M1) { sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); } void set(int k, const Monoid& x) { seg[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } void update(int k, const Monoid& x) { k += sz; seg[k] = x; while (k >>= 1) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int& k) const { return seg[k + sz]; } template <class C> int find_subtree(int a, const C& check, Monoid& M, bool type) { while (a < sz) { Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]); if (check(nxt)) a = 2 * a + type; else M = nxt, a = 2 * a + 1 - type; } return a - sz; } template <class C> int find_first(int a, const C& check) { Monoid L = M1; if (a <= 0) { if (check(f(L, seg[1]))) return find_subtree(1, check, L, false); return -1; } int b = sz; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) { Monoid nxt = f(L, seg[a]); if (check(nxt)) return find_subtree(a, check, L, false); L = nxt; ++a; } } return -1; } template <class C> int find_last(int b, const C& check) { Monoid R = M1; if (b >= sz) { if (check(f(seg[1], R))) return find_subtree(1, check, R, true); return -1; } int a = sz; for (b += sz; a < b; a >>= 1, b >>= 1) { if (b & 1) { Monoid nxt = f(seg[--b], R); if (check(nxt)) return find_subtree(b, check, R, true); R = nxt; } } return -1; } }; int main(int argc, char* argv[]) { ll n; cin >> n; vec<ll> a(n); scan(a); SegmentTree<ll> seg( n, [](ll x, ll y) { return x + y; }, 0LL); for (int i = (0); i < (n); i++) { seg.set(i, a[i]); } seg.build(); ll ans = 0; bool next_sign = (a[0] < 0) ? true : false; if (a[0] == 0) seg.update(0, 1LL); for (int i = (2); i < (n + 1); i++) { ll sum = seg.query(0, i); if ((next_sign && sum > 0) || (!next_sign && sum < 0)) { next_sign = !next_sign; continue; } ll to = (next_sign) ? 1 : -1; ll diff = abs(sum - to); ans += diff; seg.update(i - 1, seg[i - 1] + (to - sum)); next_sign = !next_sign; } ll ans2 = 0; for (int i = (0); i < (n); i++) { seg.update(i, a[i]); } next_sign = (a[0] < 0) ? true : false; if (a[0] == 0) seg.update(0, -1LL); for (int i = (2); i < (n + 1); i++) { ll sum = seg.query(0, i); if ((next_sign && sum > 0) || (!next_sign && sum < 0)) { next_sign = !next_sign; continue; } ll to = (next_sign) ? 1 : -1; ll diff = abs(sum - to); ans2 += diff; seg.update(i - 1, seg[i - 1] + (to - sum)); next_sign = !next_sign; } ((cout) << (min(ans, ans2)) << (endl)); return 0; } template <class T> static void scan(vector<T>& v) { auto tFormat = scanType[typeid(T)]; for (T& n : v) { scanf(tFormat, &n); } } static void scan(vector<string>& v, bool isWord) { if (isWord) { for (auto& n : v) { cin >> n; } return; } int i = 0, size = v.size(); string s; getline(cin, s); if (s.size() != 0) { i++; v[0] = s; } for (; i < size; ++i) { getline(cin, v[i]); } } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> inline T gcd(T a, T b) { return __gcd(a, b); } template <class T> inline T lcm(T a, T b) { T c = min(a, b), d = max(a, b); return c * (d / gcd(c, d)); } template <class A, size_t N, class T> void Fill(A (&arr)[N], const T& val) { std::fill((T*)arr, (T*)(arr + N), val); } template <class T> T mod(T a, T m) { return (a % m + m) % m; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; long long a[100000]; long long c[2]; long long csum; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } c[0] = 0; c[1] = 0; for (int l = 0; l < 2; l++) { csum = 0; long long sign; if (l == 0) { sign = 1; } else { sign = -1; } csum += a[0]; if (csum == 0) { c[l] += 1; csum += sign; } else if (csum * sign < 0) { csum += -csum + sign; } for (int i = 1; i < n; i++) { long long bsum = csum; bsum += a[i]; if (csum * bsum >= 0) { if (csum > 0) { c[l] += (bsum + 1); bsum -= (bsum + 1); } else { c[l] += (-bsum + 1); bsum += (-bsum + 1); } } csum = bsum; } } cout << min(c[0], c[1]) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void Main() { int n; cin >> n; long long ct = 0; long long now = 0; bool rev = false; bool allzero = true; long long a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; if (allzero == true) { if (a[i] > 0) { rev = false; allzero = false; } else if (a[i] < 0) { rev = true; allzero = false; } } } for (int i = 0; i < n; i++) { now += a[i]; if ((i % 2) xor rev == 0) { if (now <= 0) { ct += 1 - now; now = 1; } } else { if (now >= 0) { ct += now + 1; now = (-1); } } } cout << ct << "\n"; } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(15); Main(); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1LL << 60; signed main() { long long n; cin >> n; vector<long long> a(n); long long count = 0; for (long long i = 0; i < n; i++) { cin >> a[i]; } long long nowand = 0; long long count1 = 0; for (long long i = 0; i < n; i++) { nowand += a[i]; if (nowand >= 0) { count1 += nowand + 1; nowand = -1; } } long long count2 = 0; nowand = 0; for (long long i = 0; i < n; i++) { nowand += a[i]; if (nowand >= 0) { count2 += nowand + 1; nowand = -1; } } cout << min(count1, count2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int,input().split())) #初期化 dp = [[0,0]]*N if A[0] == 0: for i in range(1,N): if A[i] > 0: dp[i-1][0] = 2*i-1 dp[i-1][1] = -1 break elif A[i] < 0: dp[i-1][0] = 2*i-1 dp[i-1][1] = 1 break else: pass else: dp[N-1][0] = 2*N-1 else: dp[0][1] = A[0] #dp for j in range(N-1): S_j = dp[j][1] if (S_j > 0 and A[j+1] < -S_j) or (S_j < 0 and A[j+1] > -S_j): dp[j+1][0] = dp[j][0] dp[j+1][1] = dp[j][1]+A[j+1] elif S_j > 0: dp[j+1][0] = dp[j][0]+S_j+A[j+1]+1 dp[j+1][1] = -1 elif S_j < 0: dp[j+1][0] = dp[j][0]-S_j-A[j+1]+1 dp[j+1][1] = -1 print(dp[N-1][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 Int = long long; Int INF = 1 << 30; Int large0(std::vector<Int> a, Int n) { Int ans = 0; Int sum = a[0]; for (Int i = 1; i < n; i++) { sum += a[i]; if (i % 2 == 1 && sum >= 0) { ans += sum + 1; sum = -1; } if (i % 2 == 0 && sum <= 0) { ans += 1 - sum; sum = 1; } } return ans; } Int small0(std::vector<Int> a, Int n) { Int ans = 0; Int sum = a[0]; for (Int i = 1; i < n; i++) { sum += a[i]; if (i % 2 == 1 && sum <= 0) { ans += 1 - sum; sum = 1; } if (i % 2 == 0 && sum >= 0) { ans += sum + 1; sum = -1; } } return ans; } int main() { Int n; std::cin >> n; std::vector<Int> a(n); for (Int i = 0; i < n; i++) std::cin >> a[i]; Int ans = 0; Int sum = a[0]; if (a[0] > 0) { std::cout << large0(a, n) << std::endl; } if (a[0] < 0) { std::cout << small0(a, n) << std::endl; } if (a[0] == 0) { Int res1 = 0; Int res2 = 0; a[0] = 1LL; res1 = large0(a, n); a[0] = -1LL; res2 = small0(a, n); std::cout << std::min(res1, res2) + 1 << std::endl; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> long long a[100010]; int main() { int n, i, j, k; long long s, x, y; while (scanf("%d", &n) != EOF) { for (i = 0; i < n; i++) scanf("%lld", &a[i]); s = 0; x = a[0]; for (i = 1; i < n; i++) { y = x; x = x + a[i]; if (x < 0 && y > 0) continue; if (y < 0 && x > 0) continue; if (y < 0) { s = s - x + 1; x = 1; } else if (y > 0) { s = s + x + 1; x = -1; } } printf("%lld\n", s); } 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(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = scan.nextLong(); } long sum1 = 0; long sum2 = 0; long ans1 = 0; long ans2 = 0; for (int i = 0; i < n-1; i++) {//偶数添字が正 sum1 += a[i]; if (i%2 == 0) { if (sum1 > 0) continue; else { ans1 += (1 + Math.abs(sum1)); sum1 = 1; } } else if (i%2 == 1) { if (sum1 < 0) continue; else { ans1 += (sum1 + 1); sum1 = -1; } } } for (int i = 0; i < n-1; i++) {//奇数添字が正 sum2 += a[i]; if (i%2 == 1) { if (sum2 > 0) continue; else { ans2 += (1 + Math.abs(sum2)); sum2 = 1; } } else if (i%2 == 0) { if (sum2 < 0) continue; else { ans2 += (sum2 + 1); sum2 = -1; } } } if (ans1 == 0) { ans1++; } if (ans2 == 0) { ans2++; } System.out.println(Math.min(ans1, ans2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) l = map(int, input().split()) ll = [] for q in range(len(l)): ll.append(l[q]) print(ll) sums1 = [0] * n count1 = 0 sums1[0] = ll[0] for i in range(1, len(ll)): sums1[i] = sums1[i-1] + ll[i] if l[0] == 0: for v in range(len(sums1)): sums1 += [1] * len(sums1) count1 += 1 for k in range(1, len(sums1)): while sums1[k] == 0 or sums1[k] * sums1[k-1] > 0: if sums1[k] == 0: for p in range(k, len(sums1)): sums1[p] += -(sums1[k-1]/abs(sums1[k-1])) count1 += 1 if sums1[k] * sums1[k-1] > 0: for p in range(k, len(sums1)): sums1[p] += (abs(sums1[k])+1) * (-(sums1[k])/abs(sums1[k])) count1 += abs(sums1[k])+1 sums2 = [0] * n count2 = 0 sums2[0] = ll[0] for i in range(1, len(ll)): sums2[i] = sums2[i-1] + ll[i] if l[0] == 0: for v in range(len(sums2)): sums2 -= [1] * len(sums2) count2 += 1 for k in range(1, len(sums2)): while sums2[k] == 0 or sums2[k] * sums2[k-1] > 0: if sums2[k] == 0: for p in range(k, len(sums2)): sums2[p] += -sums2[k-1]/abs(sums2[k-1]) count2 += 1 if sums2[k] * sums2[k-1] > 0: for p in range(k, len(sums1)): sums2[p] += -(abs(sums2[k])+1) * ((sums2[k])/abs(sums2[k])) count2 += abs(sums1[k])+1 print(min(count2, count2) * 2)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; } int p = 0, sum = 0; for (int i = 0; i < n; i++) { if (i == 0) { if (a.at(i) <= 0) { p += 1 - a.at(i); a.at(i) = 1; } } else { if (sum > 0 && sum + a.at(i) >= 0) { p += sum + a.at(i) + 1; a.at(i) = -sum - 1; } else if (sum < 0 && sum + a.at(i) <= 0) { p -= sum + a.at(i) - 1; a.at(i) = -sum + 1; } } sum += a.at(i); } int ne = 0; sum = 0; for (int i = 0; i < n; i++) { if (i == 0) { if (b.at(i) >= 0) { ne += 1 + b.at(i); b.at(i) = -1; } } else { if (sum > 0 && sum + b.at(i) >= 0) { ne += sum + b.at(i) + 1; b.at(i) = -sum - 1; } else if (sum < 0 && sum + b.at(i) <= 0) { ne -= sum + b.at(i) - 1; b.at(i) = -sum + 1; } } sum += b.at(i); } cout << min(p, ne) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#![allow(unused_imports)] #![allow(unused_variables)] #![allow(non_snake_case)] #![allow(dead_code)] #![allow(deprecated)] use std::cell::{Cell, Ref, RefCell, RefMut}; use std::cmp::{max, min, Ordering}; use std::collections::*; use std::fmt::{Debug, Formatter, Write as FmtWrite}; use std::io::{stderr, stdin, BufRead, Write}; use std::mem::{replace, swap}; use std::ops::*; use std::rc::Rc; use std::usize; const MOD_10_9_7: u64 = 1_000_000_007; const INF: i64 = 1_000_000_000_000; const MIN_INF: i64 = -1_000_000_000_000; /// FYI: https://github.com/vain0x/scan-bench #[allow(unused_macros)] macro_rules! read { ([$t:ty] ; $n:expr) => ((0..$n).map(|_| read!([$t])).collect::<Vec<_>>()); ($($t:ty),+ ; $n:expr) => ((0..$n).map(|_| read!($($t),+)).collect::<Vec<_>>()); ([$t:ty]) => (rl().split_whitespace().map(|w| w.parse().unwrap()).collect::<Vec<$t>>()); ($t:ty) => (rl().parse::<$t>().unwrap()); ($($t:ty),*) => {{ let buf = rl(); let mut w = buf.split_whitespace(); ($(w.next().unwrap().parse::<$t>().unwrap()),*) }}; } fn rl() -> String { let mut buf = String::new(); stdin().read_line(&mut buf).unwrap(); buf.trim_right().to_owned() } #[allow(unused_macros)] macro_rules! debug { ($($a:expr),*) => { eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*); } } fn main() { let n = read!(usize); let a: Vec<i64> = read![[i64]]; let mut ans = 0; let mut s = a[0]; for &ai in &a[1..] { if s > 0 { if s + ai < 0 { s = s + ai; continue; } else { // ai を s + ai = -1 になるように操作する ans += ai + 1 + s; s = -1; continue; } } else { if s + ai > 0 { s = s + ai; continue; } else { // ai を s + ai = 1 になるように操作する ans += 1 - s - ai; s = 1; continue; } } } println!("{}", ans); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; int n; int a[100000]; int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int total = 0, ans1 = 0, ans2 = 0; for (int i = 0; i < n; i++) { total += a[i]; if (i % 2 == 0 && total <= 0) { while (total != 1) { total++; ans1++; } } else if (i % 2 != 0 && total >= 0) { while (total != -1) { total--; ans1++; } } } total = 0; for (int i = 0; i < n; i++) { total += a[i]; if (i % 2 != 0 && total <= 0) { while (total != 1) { total++; ans2++; } } else if (i % 2 == 0 && total >= 0) { while (total != -1) { total--; ans2++; } } } cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<iostream> #include<string> #include<vector> #include<stdio.h> #include<algorithm> #include<math.h> #include<numeric> #include<iomanip> #include<deque> #include<tuple> #include<queue> #include<map> #include <cstdint> #include <boost/multiprecision/cpp_int.hpp> #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define vi vector<int> #define all(x) (x).begin(),(x).end() #define Endl endl #define F first #define S second namespace mp = boost::multiprecision; using cpp_int = mp::cpp_int; using ll = long long; using namespace std; int main() { int n; cin >> n; vector<ll>sum(n),a(n); rep(i, n) { cin >> a[i]; } ll count = 0; sum[0] = a[0]; FOR(i,1, n) { sum[i] = sum[i - 1] + a[i]; if (sum[i - 1] > 0) { if (sum[i] >= 0) { count += sum[i] + 1; sum[i] = -1; } } else if (sum[i - 1] < 0) { if (sum[i] <= 0) { count += abs(sum[i]) + 1; sum[i] = 1; } } else if (sum[i - 1] == 0) { break; } } 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 <iostream> #include <utility> #include <vector> #include <tuple> #include <cstdint> using namespace std; // Type aliases using i8 = int8_t; using u8 = uint8_t; using i16 = int16_t; using u16 = uint16_t; using i32 = int32_t; using u32 = uint32_t; using i64 = int64_t; using u64 = uint64_t; template <class T> using V = vector<T>; // Loops #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = (n) - 1; i >= 0; --i) #define FOR(i, n, m) for (int i = (n); i < (m); ++i) #define FORR(i, n, m) for (int i = (m) - 1; i >= (n); --i) #define FORE(x, xs) for (auto &x: (xs)) // Utils for Tuple namespace tuple_utils { template<size_t...> struct seq{}; template<size_t N, size_t... Is> struct gen_seq : gen_seq<N - 1, N - 1, Is...>{}; template<size_t... Is> struct gen_seq<0, Is...> : seq<Is...>{}; template <class Tuple, size_t... Is> void read(istream &stream, Tuple &t, seq<Is...>) { static_cast<void>((int[]){0, (void(stream >> get<Is>(t)), 0)...}); } template<class Tuple, size_t... Is> void print(ostream& stream, Tuple const& t, seq<Is...>) { static_cast<void>((int[]){0, (void(stream << (Is == 0 ? "" : ", ") << get<Is>(t)), 0)...}); } } // Input template <class F, class S> istream &operator>>(istream &stream, pair<F, S> &pair) { stream >> pair.first; stream >> pair.second; return stream; } template <class ...Args> istream &operator>>(istream &stream, tuple<Args...> &tuple) { tuple_utils::read(stream, tuple, tuple_utils::gen_seq<sizeof...(Args)>()); return stream; } template <class T> T read() { T t; cin >> t; return t; } template <class F, class S> pair<F, S> read() { pair<F, S> p; cin >> p; return p; } template <class T1, class T2, class T3, class ...Args> tuple<T1, T2, T3, Args...> read() { tuple<T1, T2, T3, Args...> t; cin >> t; return t; } template <class T> V<T> read(const int length) { V<T> ts(length); for (auto& t: ts) { cin >> t; } return ts; } template <class F, class S> V<pair<F, S>> read(const int length) { V<pair<F, S>> ps(length); for (auto& p: ps) { cin >> p; } return ps; } template <class T1, class T2, class T3, class ...Args> V<tuple<T1, T2, T3, Args...>> read(const int length) { V<tuple<T1, T2, T3, Args...>> ts(length); for (auto& t: ts) { cin >> t; } return ts; } // Output namespace debug { template <class F, class S> ostream &operator<<(ostream& stream, const pair<F, S> &pair) { stream << "{" << pair.first << ", " << pair.second << "}"; return stream; } template <class ...Args> ostream &operator<<(ostream& stream, const tuple<Args...> &tuple) { stream << "{"; tuple_utils::print(stream, tuple, tuple_utils::gen_seq<sizeof...(Args)>()); stream << "}"; return stream; } template <class T, class Alloc> ostream &operator<<(ostream& stream, const vector<T, Alloc> &vector) { stream << "["; for (auto i = 0; i < vector.size(); i++) { stream << vector[i]; if (i != vector.size() - 1) { stream << "," << ((i % 10 == 9) ? "\n " : "\t"); } } stream << "]"; return stream; } } // Body void body() { auto n = read<i32>(); auto as = read<i64>(n); auto solve = [](auto as) { i64 total = 0; u64 retval = 0; FORE (a, as) { if (total != 0 && total * (total + a) >= 0) { // total -> (total + a)で符号が変わらないまたは0になる retval += abs(total + a) + 1; a -= (total + a); // total + a = 0にする if (total > 0) { a -= 1; } else { a += 1; } } total += a; } return retval; }; if (as[0] == 0) { auto as2 = as; as2[0] = 1; auto r1 = solve(as2); as[0] = -1; auto r2 = solve(as); using namespace debug; cout << min(r1, r2) + 1 << endl; } else { cout << solve(as) << endl; } } // main function (DO NOT EDIT) int main () { ios_base::sync_with_stdio(false); body(); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int INF = 1e7; int main() { int n; cin >> n; vector<int> v(n); vector<int> s(n, 0); for (int i = 0; i < (n); ++i) { cin >> v[i]; } int ans = 0; if (v[0] == 0) { for (int i = 0; i < (n); ++i) { if (v[i] != 0) { if (i % 2 == 0) { if (v[i] > 0) { v[0]++; ans++; } else { v[0]--; ans++; } } else { if (v[i] > 0) { v[0]--; ans++; } else { v[0]++; ans++; } } } } } s[0] = v[0]; for (int i = (1); i < (int)(n); ++i) { if (s[i - 1] > 0) { while ((s[i - 1] + v[i]) >= 0) { v[i]--; ans++; } } if (s[i - 1] < 0) { while ((s[i - 1] + v[i]) <= 0) { v[i]++; ans++; } } s[i] = s[i - 1] + v[i]; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 100000000000000LL; long long a[100001]; long long t[2][100001] = {}; long long r[2] = {}; int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } t[0][0] = a[0]; if (t[0][0] == 0) { t[0][0] = -1; t[0][0] = 1; r[0] = 1; r[1] = 1; } if (t[0][0] > 0) { t[1][0] = -1; r[1] = t[0][0] + 1; } if (t[0][0] < 0) { t[1][0] = 1; r[1] = -t[0][0] + 1; } for (int i = 0; i < 2; ++i) { for (int j = 1; j < n; ++j) { t[i][j] = t[i][j - 1] + a[j]; if (t[i][j - 1] < 0 && t[i][j] < 0) { r[i] += 1 - t[i][j]; t[i][j] = 1; } if (t[i][j - 1] > 0 && t[i][j] > 0) { r[i] += 1 + t[i][j]; t[i][j] = -1; } } } cout << min(r[0], r[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; using ll = long long; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int dy[] = {0, 0, 1, -1}; int dx[] = {1, -1, 0, 0}; int main() { ll n; cin >> n; vector<ll> a(n); (i, a, b) for (ll(i) = a; (i) < (b); ++(i))(i, 0, n) cin >> a[i]; vector<ll> sum(n); sum[0] = a[0]; (i, a, b) for (ll(i) = a; (i) < (b); ++(i))(i, 0, n - 1) sum[i + 1] = sum[i] + a[i + 1]; ll c = 0; ll d = 0; if (sum[0] == 0) { (i, a, b) for (ll(i) = a; (i) < (b); ++(i))(i, 1, n) { if (sum[i] == 0) continue; if (sum[i] < 0) d++; if (sum[i] > 0) d--; c++; break; } } (i, a, b) for (ll(i) = a; (i) < (b); ++(i))(i, 0, n - 1) { if ((d + sum[i]) * (d + sum[i + 1]) < 0) continue; if (d + sum[i] >= 0) { c += d + sum[i + 1] + 1; d -= d + sum[i + 1] + 1; } else { c += abs(d + sum[i + 1]) + 1; d += abs(d + sum[i + 1]) + 1; } } cout << c << 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 ll = long long; using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<ll> v(n, 0); for (int i = (int)(0); i < (int)(n); i++) cin >> v[i]; vector<int> p1(n + 1, 1); for (int i = (int)(0); i < (int)(n + 1); i++) { if (i % 2 == 0) p1[i] *= -1; } cerr << "v" ":[ "; for (auto macro_vi : v) { cerr << macro_vi << " "; } cerr << "]" << endl; ll c[2]; vector<ll> sum_until(n + 1, 0); ll cnt = 0; cerr << "p1" ":[ "; for (auto macro_vi : p1) { cerr << macro_vi << " "; } cerr << "]" << endl; for (int i = 1; i <= n; i++) { sum_until[i] = 0ll + sum_until[i - 1] + v[i - 1]; if (not(sum_until[i] * p1[i] < 0)) { int plus = abs(sum_until[i]); sum_until[i] += plus * p1[i] + p1[i]; cnt += abs(plus * p1[i]) + 1; } else if (sum_until[i] == 0) { sum_until[i] = p1[i]; cnt += 1; } } cerr << "sum_until" ":[ "; for (auto macro_vi : sum_until) { cerr << macro_vi << " "; } cerr << "]" << endl; c[0] = cnt; cerr << "\"next\"" ":" << "next" << endl; fill(sum_until.begin(), sum_until.end(), 0ll); cnt = 0; for (int i = (int)(0); i < (int)(n + 1); i++) { if (i % 2 == 1) p1[i] = -1; else p1[i] = 1; } cerr << "p1" ":[ "; for (auto macro_vi : p1) { cerr << macro_vi << " "; } cerr << "]" << endl; for (int i = 1; i <= n; i++) { sum_until[i] = sum_until[i - 1] + v[i - 1]; if (sum_until[i] * p1[i] < 0) { int plus = abs(sum_until[i]); sum_until[i] += plus * p1[i] + p1[i]; cnt += abs(plus * p1[i]) + 1; } else if (sum_until[i] == 0) { sum_until[i] = p1[i]; cnt += 1; } } cerr << "sum_until" ":[ "; for (auto macro_vi : sum_until) { cerr << macro_vi << " "; } cerr << "]" << endl; c[1] = cnt; cerr << "(" "c[0]" "," "c[1]" "):(" << c[0] << "," << c[1] << ")" << endl; cout << min(c[1], c[0]) << 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(void) { int n; long long sum1 = 0; long long sum2 = 0; long long tmp; long long lcount = 0; long long rcount = 0; long long a[100000]; char input[1000000]; int i = 0, j = 0; int cp = 0, tcp = 0; char tp[12]; tp[12] = '\0'; fgets(input, 1000000, stdin); n = atoi(input); fgets(input, 1000000, stdin); for (i = 0; i < n; i++) { while (input[cp] != ' ' && input[cp] != '\n') { tp[tcp] = input[cp]; tcp++; cp++; } tp[tcp] = '\0'; tcp = 0; cp++; a[i] = atoi(tp); } tmp = a[0]; for (i = 1; i < n; i++) { if (i % 2 == 0) { tmp += a[i]; while (tmp > -1) { lcount++; tmp--; } } else { tmp += a[i]; while (tmp < 1) { lcount++; tmp++; } } } tmp = a[0]; for (i = 1; i < n; i++) { if (i % 2 == 1) { tmp += a[i]; while (tmp > -1) { rcount++; tmp--; } } else { tmp += a[i]; while (tmp < 1) { rcount++; tmp++; } } } printf("%ld\n", lcount > rcount ? rcount : lcount); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int check(int sum, int ans, vector<int> T, int N, bool pre_pm) { for (int i = 1; i < N; i++) { if (pre_pm) { sum += T.at(i); while (0 <= sum) { sum--; ans++; } pre_pm = false; } else { sum += T.at(i); while (sum <= 0) { sum++; ans++; } pre_pm = true; } } return ans; } int main() { int N; vector<int> T; cin >> N; for (int i = 0; i < N; i++) { int tmp; cin >> tmp; T.push_back(tmp); } int ans = 0; int sum = 0; bool pre_pm; sum = T.at(0); if (0 <= sum) { pre_pm = true; int tmp1 = check(sum, ans, T, N, pre_pm); pre_pm = false; int tmp2 = check(-1, 1 + sum, T, N, pre_pm); cout << min(tmp1, tmp2) << endl; } else { pre_pm = false; int tmp1 = check(sum, ans, T, N, pre_pm); pre_pm = true; int tmp2 = check(1, 1 + sum, T, N, pre_pm); cout << min(tmp1, tmp2) << endl; } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = [int(a) for a in input().split()] previous = a[0] for i in range(1, n): if previous < 0: if previous + a[i] > 0: previous = previous + a[i] else: break elif previous > 0: if previous + a[i] < 0: previous = previous + a[i] else: break else: print(0) exit() times1 = 0 times2 = 0 if a[0] < 0: times1 += abs(1 + abs(a[0]) - a[1]) times2 += 1 - a[0] + abs(-2 - a[1]) for i in range(3, n+1): if i % 2 != 0: times1 += abs(-2 - a[i-1]) times2 += abs(2 - a[i-1]) else: times1 += abs(2 - a[i-1]) times2 += abs(-2 - a[i-1]) else: times1 += abs(-(1 + a[0]) - a[1]) times2 += abs(-1 - a[0]) + abs(2 - a[1]) for i in range(3, n+1): if i % 2 != 0: times1 += abs(2 - a[i-1]) times2 += abs(-2 - a[i-1]) else: times1 += abs(-2 - a[i-1]) times2 += abs(2 - a[i-1]) print(min(times1, times2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; long long sum; cin >> sum; if (sum == 0) { long long delta1 = 1; sum = 1; long long ar[N]; for (int i = 1; i < N; ++i) { cin >> ar[i]; long long temp = ar[i]; if (sum > 0 && sum + temp >= 0) { delta1 += sum + temp + 1; sum = -1; } else if (sum < 0 && sum + temp <= 0) { delta1 += 1 - (sum + temp); sum = 1; } else { sum += temp; } } long long delta2 = 1; sum = -1; for (int i = 1; i < N; ++i) { long long temp = ar[i]; if (sum > 0 && sum + temp >= 0) { sum = -1; delta2 += sum + temp + 1; } else if (sum < 0 && sum + temp <= 0) { sum = 1; delta2 += 1 - (sum + temp); } else { sum += temp; } } if (delta1 < delta2) cout << delta1; else cout << delta2; } else { long long delta = 0; for (int i = 1; i < N; ++i) { long long temp; cin >> temp; if (sum > 0 && sum + temp >= 0) { delta += sum + temp + 1; sum = -1; } else if (sum < 0 && sum + temp <= 0) { delta += 1 - (sum + temp); sum = 1; } else { sum += temp; } } cout << delta; } 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]; } bool plus = false; long long ans = 0; long long sum = a[0]; if (sum > 0) { plus = false; } else if (sum < 0) { plus = true; } else { sum = 1; ans = 1; plus = false; } for (int i = 1; i < n; ++i) { sum += a[i]; if (plus == false) { if (sum >= 0) { ans += (abs(sum) + 1); sum = -1; } plus = true; } else { if (sum <= 0) { ans += (abs(sum) + 1); sum = 1; } plus = false; } } if (sum == 0) ans++; 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; long long gcd(long long x, long long y) { if (y == 0) return x; return gcd(y, x % y); } long long lcm(long long x, long long y) { return x * y / gcd(x, y); } long long n, a[100001], d[100001] = {0}; bool b[100001] = {0}; int up() { int cnt = 0, ud = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 1) { if (d[i] + ud <= 0) { cnt -= d[i] + ud - 1; ud = -(d[i] + ud - 1); } } else { if (d[i] + ud >= 0) { cnt += d[i] + ud + 1; ud = -(d[i] + ud + 1); } } } return cnt; } int down() { int cnt = 0, ud = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 0) { if (d[i] + ud <= 0) { cnt -= d[i] + ud - 1; ud = -(d[i] + ud - 1); } } else { if (d[i] + ud >= 0) { cnt += d[i] + ud + 1; ud = -(d[i] + ud + 1); } } } return cnt; } int main() { cin >> n; b[0] = 1; for (int i = 0; i < n; i++) { cin >> a[i]; d[i + 1] = d[i] + a[i]; } cout << min(up(), down()) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll LINF = 1LL << 60; const int INF = 1e9 + 7; vector<vector<ll>> g(100010); vector<ll> dist(100010); int main() { ll n; cin >> n; ll a[n]; for (ll i = 0; i < n; ++i) cin >> a[i]; ll ans = 0; ll sum; if (a[0] >= 0) { sum = -1; ans += a[0] + 1; } else { sum = a[0]; } for (ll i = 1; i < n; ++i) { if ((i & 1 && sum + a[i] <= 0) || (!(i & 1) && sum + a[i] >= 0)) { ans += llabs(sum + a[i]) + 1; sum = -1 * (sum / llabs(sum)); } else { sum += a[i]; } } ll res = 0; if (a[0] <= 0) { sum = 1; res += a[0] + 1; } else { sum = a[0]; } for (ll i = 1; i < n; ++i) { if ((!(i & 1) && sum + a[i] <= 0) || (i & 1 && sum + a[i] >= 0)) { res += llabs(sum + a[i]) + 1; sum = -1 * (sum / llabs(sum)); } else { sum += a[i]; } } 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; int solve(); int solve1(vector<long> adder); int solve2(vector<long> adder); int adds(int i, int num, vector<long> *adder); int show(vector<long> adder); vector<long> adde; int N; int main() { cin >> N; adde = vector<long>(N + 1); adde.at(0) = 0; long buf; for (int i = 0; i < N; i++) { cin >> buf; adde.at(i + 1) = adde.at(i) + buf; } solve(); } int solve1(vector<long> adder) { int buf; int score = 0; for (int i = 0; i < N; i++) { show(adder); buf = abs(adder.at(i + 1)) + 1; if (i % 2 == 0) { if (adder.at(i + 1) > 0) { } else { adds(i + 1, buf, &adder); score += buf; } } else { if (adder.at(i + 1) > 0) { adds(i + 1, -buf, &adder); score += buf; } else { } } } return score; } int solve2(vector<long> adder) { int buf; int score = 0; for (int i = 0; i < N; i++) { show(adder); buf = abs(adder.at(i + 1)) + 1; if (i % 2 != 0) { if (adder.at(i + 1) > 0) { } else { adds(i + 1, buf, &adder); score += buf; } } else { if (adder.at(i + 1) > 0) { adds(i + 1, -buf, &adder); score += buf; } else { } } } return score; } int solve() { cout << min(solve1(adde), solve2(adde)) << endl; return 0; } int adds(int i, int num, vector<long> *adder) { for (int j = i; j < N + 1; j++) { (*adder).at(j) += num; } return 0; } int show(vector<long> adder) { return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; long long chk1, chk2, ans1 = 0, ans2 = 0; scanf("%d", &n); vector<int> a(n); for (auto& e : a) scanf("%d", &e); chk1 = a[0]; chk2 = a[0]; for (int i = 1; i < n; i++) { if (i % 2) { chk1 += a[i]; chk2 += a[i]; if (chk1 >= 0) { ans1 += chk1 + 1; chk1 = -1; } if (chk2 <= 0) { ans2 += -1 * chk2 + 1; chk2 = 1; } } else { chk1 += a[i]; chk2 += a[i]; if (chk1 <= 0) { ans1 += -1 * chk1 + 1; chk1 = 1; } if (chk2 >= 0) { ans2 += chk2 + 1; chk2 - 1; } } } printf("%lld\n", min(ans1, ans2)); 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) { long long n; cin >> n; long long a[n]; long long sum[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } long long count = 0; for (int i = 0; i < n; i++) { if (i == 0) { sum[i] = a[i]; } else { sum[i] = sum[i - 1] + a[i]; if (sum[i] != 0 && sum[i - 1] == abs(sum[i - 1]) && sum[i] == abs(sum[i])) { count += sum[i] + 1; sum[i] = -1; } else if (sum[i] != 0 && sum[i - 1] != abs(sum[i - 1]) && sum[i] != abs(sum[i])) { count += 1 - sum[i]; sum[i] = 1; } } if (sum[i] == 0) { count++; if (i == 0) { if (a[i + 1] == abs(a[i + 1])) sum[i] = -1; else sum[i] = 1; } else { if (sum[i - 1] == abs(sum[i - 1])) sum[i] = -1; else sum[i] = 1; } } } cout << count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; long long a[100005], dp[100005]; cin >> n; long long sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; dp[i] = sum; } long long diff = 0, ans = 0; for (int i = 1; i < n; i++) { if (dp[i] + diff == 0) { if (dp[i - 1] + diff < 0) diff++, ans++; if (dp[i - 1] + diff > 0) diff--, ans++; continue; } if ((dp[i - 1] + diff) / abs(dp[i - 1] + diff) == (dp[i] + diff) / abs(dp[i] + diff)) { if (dp[i] + diff >= 0) { ans += abs(dp[i] + diff) + 1; diff -= abs(dp[i] + diff) + 1; } else { ans += abs(dp[i] + diff) + 1; diff += abs(dp[i] + diff) + 1; } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; int odd = 0, even = 0, sum, sign; sum = 0; sign = 1; for (int i = 0; i < n; i++) { sum += a[i]; if (sum == 0 || (sum > 0 && sign == 1) || (sum < 0 && sign == -1)) { odd += abs(sum) + 1; sum = -1 * sign; } sign *= -1; } sum = 0; sign = -1; for (int i = 0; i < n; i++) { sum += a[i]; if (sum == 0 || (sum > 0 && sign == 1) || (sum < 0 && sign == -1)) { even += abs(sum) + 1; sum = -1 * sign; } sign *= -1; } cout << min(odd, even) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import itertools from collections import Counter from collections import defaultdict import bisect from heapq import heappush, heappop def main(): n = int(input()) a = list(map(int, input().split())) ans = 10**10 cumulative = 0 count = 0 for i in range(len(a)): cumulative += a[i] if i % 2 == 0: # positive if cumulative <= 0: count += abs(cumulative) + 1 cumulative += (abs(cumulative) + 1) else: # negative if cumulative >= 0: count += abs(cumulative) + 1 cumulative -= (abs(cumulative) + 1) # print('count = {} c = {}'.format(count, cumulative)) ans = min(ans, count) cumulative = 0 count = 0 for i in range(len(a)): cumulative += a[i] if i % 2 == 0: # negative if cumulative >= 0: count += abs(cumulative) + 1 cumulative -= (abs(cumulative) + 1) else: # positive if cumulative <= 0: count += abs(cumulative) + 1 cumulative += (abs(cumulative) + 1) # print('count = {} c = {}'.format(count, cumulative)) ans = min(ans, count) print(ans) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("O3,no-stack-protector") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx") using namespace std; using Graph = vector<vector<int64_t>>; const double pi = M_PI; const int64_t MOD = 1000000007; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int64_t n; cin >> n; vector<int64_t> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int64_t ans = 0, tem = a[0]; if (tem == 0) { if (0 <= a[1]) { tem = -1; ans++; } else { tem = 1; ans++; } } for (int i = 1; i < n; i++) { if ((0 < tem + a[i] && tem < 0) || (tem + a[i] < 0 && 0 < tem)) { tem += a[i]; } else { if (0 <= tem + a[i] && 0 <= tem) { ans += abs(-1 - (tem + a[i])); tem = -1; } else { ans += abs(1 - (tem + a[i])); tem = 1; } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int f(int isum, vector<int>& a, int n, int ans) { for (int i = 1; i < n; i++) { int temp = isum; isum += a[i]; if (temp > 0) { if (isum >= 0) { ans += (1 + isum); isum = -1; } } else if (temp < 0) { if (isum <= 0) { ans += (1 + abs(isum)); isum = 1; } } } return ans; } void solve() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int p = 1; int ne = -1; int pans = 1; int nans = 1; if (a[0] > 0) { p = a[0]; nans = a[0] + 1; pans--; } else if (a[0] < 0) { ne = a[0]; nans--; pans = a[0] + 1; } cout << min(f(p, a, n, pans), f(ne, a, n, nans)); } int main() { ios::sync_with_stdio(false); cin.tie(0); solve(); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int64_t sum_from1tok(vector<int> vec, int k) { int64_t sum = 0; for (int i = 0; i < (int)(k); i++) { sum += vec.at(i); } return sum; } int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < (int)(n); i++) { int num; cin >> num; a.at(i) = num; } int count = 0; int64_t sum = a.at(0); if (a.at(0) > 0) { for (int i = 1; i < n; i++) { sum += a.at(i); if (i % 2 != 0) { if (sum >= 0) { count += abs(sum) + 1; sum = -1; } } else { if (sum <= 0) { count += abs(sum) + 1; sum = 1; } } } cout << count << endl; } else if (a.at(0) < 0) { for (int i = 1; i < n; i++) { sum += a.at(i); if (i % 2 != 0) { if (sum <= 0) { count += abs(sum) + 1; sum = 1; } } else { if (sum >= 0) { count += abs(sum) + 1; sum = -1; } } } cout << count << endl; } else { a.at(0) = 1; int64_t seisum = 1; int seicount = 1; for (int i = 1; i < n; i++) { seisum += a.at(i); if (i % 2 != 0) { if (seisum >= 0) { seicount += abs(seisum) + 1; seisum = -1; } } else { if (seisum <= 0) { seicount += abs(seisum) + 1; seisum = 1; } } cout << count << endl; } a.at(0) = -1; int64_t fusum = -1; int fucount = 1; for (int i = 1; i < n; i++) { fusum += a.at(i); if (i % 2 != 0) { if (fusum <= 0) { fucount += abs(fusum) + 1; fusum = 1; } } else { if (fusum >= 0) { fucount += abs(fusum) + 1; fusum = -1; } } } cout << min(seisum, fusum) << 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; const ll mod = 1e9 + 7; int n; vector<ll> a; ll cost(bool sign) { ll cst = 0, rs = 0; for (int i = 0; i < n; i++) { rs += a[i]; if (rs == 0) { ll x = sign ? 1 : -1; cst += x; rs += x; } else { if ((rs > 0) == true && !sign) { cst += rs + 1; rs = -1; } else if ((rs > 0) == false && sign) { cst += abs(rs) + 1; rs = 1; } } sign = !sign; } return cst; } int main() { cin >> n; a.resize(n); for (ll &i : a) cin >> i; ll ans = cost(true); ans = min(ans, cost(false)); 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; template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long modinv(long long a, long long m) { long long b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } vector<pair<long long, long long>> prim; void pf(long long n) { long long s = sqrt(n); long long r = 0; for (long long i = 2; i <= s; i++) { if ((n % i) == 0) { r = 0; do { r++; n = n / i; } while ((n % i) == 0); prim.push_back({i, r}); } } if (n > s) { prim.push_back({n, 1}); } } void solve() { long long N; cin >> N; vector<long long> a(N); for (long long i = 0; i < (N); i++) cin >> a[i]; long long asum = 0; long long ans = 0; if (a[0] == 0) { if (a[0] + a[1] > 0) { a[0] = -1; ans++; } else { a[0] = 1; ans++; } } asum = a[0]; cerr << a[0] << " "; for (long long i = 1; i < N; i++) { if (asum * (asum + a[i]) >= 0) { long long nas = (asum > 0) ? -1 : 1; long long na = nas - (asum); ans += abs(a[i] - na); a[i] = na; } asum += a[i]; cerr << a[i] << "(" << asum << ") "; } cerr << "\n"; cout << ans << endl; } int main(void) { 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
cpp
#include <bits/stdc++.h> using namespace std; using i64 = long long; using f80 = long double; using vi32 = vector<int>; using vi64 = vector<i64>; using vf80 = vector<f80>; using vstr = vector<string>; inline void yes() { cout << "Yes" << endl; exit(0); } inline void no() { cout << "No" << endl; exit(0); } inline i64 gcd(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); if (a % b == 0) return b; return gcd(b, a % b); } inline i64 lcm(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); return a / gcd(a, b) * b; } template <typename T> class pqasc : public priority_queue<T, vector<T>, greater<T>> {}; template <typename T> class pqdesc : public priority_queue<T, vector<T>, less<T>> {}; template <typename T> inline void amax(T &x, T y) { x = max(x, y); } template <typename T> inline void amin(T &x, T y) { x = min(x, y); } template <typename T> inline T exp(T x, i64 n, T e = 1) { T r = e; while (n > 0) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &x : v) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { for (int i = 0; i < int(v.size()); i++) { if (i) os << ' '; os << v[i]; } return os; } void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(16); solve(); return 0; } void solve() { int n; cin >> n; vi32 a(n); cin >> a; i64 ans = 1e18; auto f = [&](int sum) -> i64 { i64 cnt = 0; for (int i = 1; i <= int(n - 1); i++) { if (sum > 0) { if (sum + a[i] >= 0) { cnt += abs(sum + a[i]) + 1; sum = -1; } else { sum += a[i]; } } else { if (sum + a[i] <= 0) { cnt += abs(sum + a[i]) + 1; sum = 1; } else { sum += a[i]; } } } return cnt; }; if (a[0] == 0) { amin(ans, f(1)); amin(ans, f(-1)); } else { amin(ans, f(a[0])); amin(ans, f(a[0] > 0 ? -1 : 1) + abs(a[0]) + 1); } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, i, count = 0; double a[100001], sum; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%lf", &a[i]); } if (a[0] == 0) { if (a[1] >= 0) a[0]--; else a[0]++; count++; } sum = a[0]; for (i = 1; i < n; i++) { if (a[i] == 0) { if (sum > 0) { while (sum + a[i] >= 0) { a[i]--; count++; } } else { while (sum + a[i] <= 0) { a[i]++; count++; } } } else if (sum > 0) { while (sum + a[i] >= 0) { a[i]--; count++; } } else if (sum < 0) { while (sum + a[i] <= 0) { a[i]++; count++; } } sum += a[i]; } printf("%d\n", 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
python3
n = int(input()) a = [int(x) for x in input().split()] def check(a, t): ans = 0 x = 0 for i in a: x += i if t == True and x < 1: ans += 1 - x x = 1 elif t == False and x > -1: ans += x + 1 x = -1 t = not t return ans print(min(chk(a, True), chk(a, False)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Main { public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] input = new int[n]; int[] result = new int[n]; int even = 0; int odd = 0; boolean sign = true; //正=true, 負=false for(int i = 0; i < n; i++) { input[i] = sc.nextInt(); if(i % 2 == 0) { even += input[i]; } else { odd += input[i]; } } if(even > 0 && odd < 0) { //正負 sign = true; } else if(even < 0 && odd > 0) { //負正 sign = false; } else if(even > 0 && odd > 0) { //正正 if(even > odd) { sign = true; } else { sign = false; } } else if(even < 0 && odd < 0) { //負負 if(even > odd) { sign = false; } else { sign = true; } } else if(even == 0) { if(odd < 0) { sign = true; } else { sign = false; } } else if(odd == 0){ if(even > 0) { sign = true; } else { sign = false; } } //System.out.println(Arrays.toString(input)); //System.out.println(sign + ""); //System.out.println(counting(input, result, 0, 0, sign)); counting(input, result, 0, 0, sign); } public static void counting(int[] input, int[] result, int count, int index, boolean sign) { if(index > 0) { result[index] = result[index - 1] + input[index]; } else { result[index] = input[index]; } if(sign) { if(result[index] <= 0) { count += Math.abs(result[index]) + 1; result[index] = result[index] + Math.abs(result[index]) + 1; } sign = false; } else { if(result[index] >= 0) { count += Math.abs(result[index]) + 1; result[index] = result[index] - Math.abs(result[index]) - 1; } sign = true; } if(index < result.length - 1) { counting(input, result, count, index+1, sign); } else { System.out.println(count); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = list(map(int, input().split())) R = [0] * n R[0] = A[0] for i in range(1, n): R[i] = R[i-1] + A[i] # search def solve(r, inc=0): ans = 0 is_plus = (r[0] > 0) for i in range(1, n): if (is_plus and r[i]+inc >= 0) or (not is_plus and r[i]+inc <= 0): ans += abs(r[i]+inc) + 1 if (r[i]+inc) > 0: inc -= abs(r[i]+inc) + 1 else: inc += abs(r[i]+inc) + 1 is_plus = (r[i]+inc > 0) return ans # normal ret = solve(R, 0) #print(R, ret) # modify is_plus = (R[0] > 0) ans0 = abs(R[0]) + 1 if is_plus: for i in range(n): R[i] -= abs(R[0]) + 1 else: for i in range(n): R[i] += abs(R[0]) + 1 ret2 = ans0 + solve(R, 0) #print(R, ret2) print(min(ret, ret2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = [int(i) for i in input().split()] sum = a[0] cnt = 0 for i in range(1,n): if sum>0: if sum+a[i]>=0: cnt+=abs(a[i]-1-sum) sum = -1 else: sum +=a[i] else: if sum+a[i]<=0: cnt+=abs(a[i]-1-sum) sum = 1 else: sum+=a[i] print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = list(map(int, input().split())) acc = [0] * n acc[0] = A[0] for i in range(1, n): acc[i] = acc[i - 1] + A[i] ans = 0 cur = acc[0] x = 0 for i in range(1, n): acc[i] += x if cur > 0: if acc[i] > 0: ans += acc[i] + 1 x -= acc[i] + 1 acc[i] = -1 else: if acc[i] < 0: ans += abs(acc[i]) + 1 x += abs(acc[i]) + 1 acc[i] = 1 cur = acc[i] if acc[n - 1] == 0: 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
UNKNOWN
# main n = gets.to_i ary = gets.split(' ').map(&:to_i) sum = ary[0] cnt = 0 (1...n).each{ |i| if sum < 0 sum += ary[i] if sum <= 0 diff = -sum+1 cnt += diff sum += diff end elsif sum > 0 sum += ary[i] if sum >= 0 diff = sum+1 cnt += diff sum -= diff end end } puts 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, t = 0, sum = 0, count = 0, count2 = 0; cin >> n; int a[n]; cin >> a[0]; if (a[0] > 0) { t = 1; } else if (a[0] < 0) { t = -1; } sum += a[0]; for (int i = 1; i < n; i++) { cin >> a[i]; sum += a[i]; if (t == -1) { while (sum <= 0) { sum++; count++; } t = 1; } else if (t == 1) { while (sum >= 0) { sum--; count++; } t = -1; } } sum = 0; if (a[0] > 0) { t = 1; } else { t = -1; } for (int i = 0; i < n; i++) { sum += a[i]; if (t == -1) { while (sum <= 0) { sum++; count2++; } t = 1; } else if (t == 1) { while (sum >= 0) { sum--; count2++; } t = -1; } } cout << min(count, count2) << 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); unsigned long op = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } if (a[0] == 0) { for (int i = 1; i < n; i++) { if (a[i] == 0) { continue; } else if (a[i] > 0) { if (i % 2 == 0) { a[0] = 1; } else { a[0] = -1; } op = 1; break; } else { if (i % 2 == 0) { a[0] = -1; } else { a[0] = 1; } op = 1; break; } } if (op == 1) { a[0] = 1; op = 1; } } long sum = a[0]; for (int i = 1; i < n; i++) { if (sum > 0) { if (sum + a[i] >= 0) { op += abs(-1 - sum - a[i]); sum = -1; } else { sum += a[i]; } } else { if (sum + a[i] <= 0) { op += abs(1 - sum - a[i]); sum = 1; } else { sum += a[i]; } } } cout << op << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long INF = 1e18; const double pi = acos(-1.0); int main(void) { long long n; cin >> n; vector<long long> a(n); for (int i = 0; i < (n); ++i) cin >> a[i]; long long ans, sum = 0, res1 = 0, res2 = 0; for (int sign = 0; sign < (2); ++sign) { for (int i = 0; i < (n); ++i) { sum += a[i]; if ((i % 2 ^ sign) && sum >= 0) { res1 += sum + 1; sum = -1; } if (!(i % 2 ^ sign) && sum <= 0) { res2 += abs(sum - 1); sum = 1; } } } ans = min(res1, res2); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int MOD = 1000000007; using namespace std; int main() { int n, a[100000], sum, ans = 0, m = 1; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } if (a[0] < 0) m = -1; sum = a[0]; for (int i = 1; i < n; i++) { m *= -1; sum += a[i]; if (m > 0 && sum <= 0) { ans += 1 - sum; sum = 1; } if (m < 0 && sum >= 0) { ans += 1 + sum; sum = -1; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define VS vector<string> #define ll long long int #define debug(x) cout << x << " :" <<#x << endl int main(void) { int n; cin >> n; vector<ll> a(100001); ll s; REP(i,n) { cin >{ s; a[i] = s; a[i] = s; } ll oddsum = 0, evensum = 0; bool odd = true, even = false; REP(i,n) { oddsum += a[i]; evensum += a[i]; if(odd && oddsum <= 0) { oddcount += 1 - oddsum; oddsum = 1; } if(even && oddsum >= 0) { oddcount += 1 + oddsum; oddsum = -1; } if(even && evensum <= 0) { evencount += 1 - evensum; evensum = 1; } if(odd && evensum >= 0) { evencount += 1 + evensum; evensum = -1; } odd = !odd; even = !even; //debug(a[i]); //debug(oddsum); //debug(oddcount); //debug(evensum); //debug(evencount); //cout << endl; } cout << fmin(oddcount, evencount) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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(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
python3
import numpy as np n = int(input()) a = list(map(int, input().split())) c1 = 0 a0 = a[0] if a0 == 0: c1 += 1 a[0] = 1 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: c1 += a[i] + sum + 1 sum = -1 else: c1 += -a[i] - sum + 1 sum = 1 else: sum += a[i] c2 = abs(a[0]) + 1 if a0 == 0: c2 = 1 a[0] = 1 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: c2 += a[i] + sum + 1 sum = -1 else: c2 += -a[i] - sum + 1 sum = 1 else: sum += a[i] print(min(c1, c2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; 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 - (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; long check(vector<long> a) { long time = 0; long pre_sum = a.at(0); int n = a.size(); if (a.at(0) < 0) { for (int i = 1; i < n; i++) { long sum = pre_sum + a.at(i); if (i % 2 == 1 && sum <= 0) { time += abs(sum - 1); sum = 1; } else if (i % 2 == 0 && sum >= 0) { time += abs(sum + 1); sum = -1; } pre_sum = sum; } } else if (a.at(0) > 0) { for (int i = 1; i < n; i++) { long sum = pre_sum + a.at(i); if (i % 2 == 0 && sum <= 0) { time += abs(sum - 1); sum = 1; } else if (i % 2 == 1 && sum >= 0) { time += abs(sum + 1); sum = -1; } pre_sum = sum; } } return time; } long zerocheck(vector<long> a) { a.at(0) = 1; long time1 = check(a) + 1; a.at(0) = -1; long time2 = check(a) + 1; long time = min(time1, time2); return time; } int main() { int n; cin >> n; long time = 0; vector<long> a(n); for (auto& x : a) { cin >> x; } if (a.at(0) == 0) { time = zerocheck(a); } else { time = check(a); } cout << time << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MaxN = 1e5; bool flag, ok; long long sum, ans, anv, anw; int n; int a[MaxN + 5], b[MaxN + 5], c[MaxN + 5]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); b[i] = a[i]; c[i] = a[i]; } sum = a[1]; if (a[1] < 0) flag = 1, ok = 1; for (int i = 2; i <= n; i++) { if (flag == 1) { if (sum + a[i] <= 0) { long long ant = sum + a[i]; int t = a[i]; a[i] = 1 - sum; ans += (a[i] - t); sum += a[i]; } else sum += a[i]; flag = 0; } else { if (sum + a[i] >= 0) { long long ant = sum + a[i]; int t = a[i]; a[i] = -1 - sum; ans += (t - a[i]); sum += a[i]; } else sum += a[i]; flag = 1; } } int tr = b[1]; if (ok) b[1] = 1, flag = 0; else b[1] = -1, flag = 1; anv += (abs(b[1] - tr)); sum = b[1]; for (int i = 2; i <= n; i++) { if (flag == 1) { if (sum + b[i] <= 0) { long long ant = sum + b[i]; int t = b[i]; b[i] = 1 - sum; anv += (b[i] - t); sum += b[i]; } else sum += b[i]; flag = 0; } else { if (sum + b[i] >= 0) { long long ant = sum + b[i]; int t = b[i]; b[i] = -1 - sum; anv += (t - b[i]); sum += b[i]; } else sum += b[i]; flag = 1; } } int te = c[1]; if (!ok) c[1] = 1, flag = 0; else c[1] = -1, flag = 1; anw += (abs(c[1] - te)); sum = c[1]; for (int i = 2; i <= n; i++) { if (flag == 1) { if (sum + c[i] <= 0) { long long ant = sum + c[i]; int t = c[i]; c[i] = 1 - sum; anw += (c[i] - t); sum += c[i]; } else sum += c[i]; flag = 0; } else { if (sum + c[i] >= 0) { long long ant = sum + c[i]; int t = c[i]; c[i] = -1 - sum; anw += (t - c[i]); sum += c[i]; } else sum += c[i]; flag = 1; } } printf("%lld\n", min(ans, min(anv, anw))); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1LL << 60; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<int> A(n); vector<int> B(n + 1); vector<int> B2(n + 1); B[0] = 0; B2[0] = 0; for (long long i = 0; i < n; i++) { cin >> A[i]; B[i + 1] = A[i] + B[i]; B2[i + 1] = B[i + 1]; } int sum_p = 0; for (long long i = 1; i < n + 1; i++) { int del = 0; if (i % 2 && B[i] < 0) del = abs(B[i]) + 1; if (i % 2 == 0 && B[i] > 0) del = -(B[i] + 1); for (long long j = i; j < n + 1; j++) { B[j] += del; } sum_p += abs(del); } int sum_m = 0; for (long long i = 1; i < n + 1; i++) { int del = 0; if (i % 2 == 0 && B2[i] <= 0) del = abs(B2[i]) + 1; if (i % 2 && B2[i] >= 0) del = -(B2[i] + 1); for (long long j = i; j < n + 1; j++) { B2[j] += del; } sum_m += abs(del); } cout << min(sum_p, sum_m) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long *a = new long[n]; for (int i = 0; i < n; i++) cin >> a[i]; int sum1 = a[0]; int sum2 = a[0]; int count1 = 0; int count2 = 0; if (a[0] >= 0) { count2 = 1 + a[0]; sum2 = -1; } if (a[0] <= 0) { count1 = 1 - a[0]; sum1 = 1; } for (int i = 1; i < n; i++) { if (sum1 * (sum1 + a[i]) < 0) { sum1 = sum1 + a[i]; } else { if (sum1 > 0) { count1 += abs(sum1 + 1 + a[i]); sum1 = -1; } else { count1 += abs(a[i] + sum1 - 1); sum1 = 1; } } if (sum2 * (sum2 + a[i]) < 0) { sum2 = sum2 + a[i]; } else { if (sum2 > 0) { count2 += abs(sum2 + 1 + a[i]); sum2 = -1; } else { count2 += abs(a[i] + sum2 - 1); sum2 = 1; } } } if (count1 < count2) { cout << count1 << endl; } else { cout << count2 << endl; } cin >> n; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = [int(a) for a in input().split()] times = 0 previous = a[0] for i in range(1, n): if previous < 0: if previous + a[i] > 0: previous = previous + a[i] else: times += 1 - (previous + a[i]) previous = 1 elif previous > 0: if previous + a[i] < 0: previous = previous + a[i] else: times += abs(-1 - (previous + a[i])) previous = -1 times2 = 0 if a[0] > 0: times2 += abs(-1 - a[0]) previous = -1 else: times2 += 1 - a[0] previous = 1 for i in range(1, n): if previous < 0: if previous + a[i] > 0: previous = previous + a[i] else: times2 += 1 - (previous + a[i]) previous = 1 elif previous > 0: if previous + a[i] < 0: previous = previous + a[i] else: times2 += abs(-1 - (previous + a[i])) previous = -1 times3 = 0 if a[0] > 0: times3 += abs(1 - a[0]) previous = 1 else: times3 += -1 - a[0] previous = -1 for i in range(1, n): if previous < 0: if previous + a[i] > 0: previous = previous + a[i] else: times3 += 1 - (previous + a[i]) previous = 1 elif previous > 0: if previous + a[i] < 0: previous = previous + a[i] else: times3 += abs(-1 - (previous + a[i])) previous = -1 print(min(times, times2, times3))