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; typedef long long ll; //1回の操作で、a[i]のいずれかを+1or-1できる //以下になる最小の操作回数を求める //全てのiに対して第一項からi項までの和Siは0でない //SiとSi+1の符号が異なる //予めSiを全て計算し、Si>0,Si+1>=0なら-1-Si+1だけa[i+1]から引く //a[0] = 0 のとき、a[0]>0,a[0]<0とした時の操作回数の小さい方が答え //a[0] != 0のとき、Si <0,Si+1<=0ならSi+c = 1を満たすcとa[i]の差をカウント int main(){ ll n; cin >> n; vector<ll> a(n); for(int i= 0;i<n;i++{ cin >> a[i]; } vector<ll> copy_a = a; ll count = 0; ll sum = 0; if(a[0] != 0){ for(int i = 0;i<n-1;i++){ sum += a[i];//i項までの和 if(sum < 0 && sum + a[i+1] <=0){ ll na = 1 -sum;//sumに何を足せば1になるか これを新たなa[i+1]にする count += abs(na - a[i+1]); a[i+1] = na; } if(sum > 0 && sum + a[i+1] >=0){ ll na = -1 -sum;//sumに何を足せば-1になるか これを新たなa[i+1]にする count += abs(na - a[i+1]); a[i+1] = na; } } } else if(a[0] == 0){ a[0] = 1; ll count1 = 1; for(int i = 0;i<n-1;i++){ sum += a[i];//i項までの和 if(sum < 0 && sum + a[i+1] <=0){ ll na = 1 -sum;//sumに何を足せば1になるか これを新たなa[i+1]にする count1 += abs(na - a[i+1]); a[i+1] = na; } if(sum > 0 && sum + a[i+1] >=0){ ll na = -1 -sum;//sumに何を足せば-1になるか これを新たなa[i+1]にする count1 += abs(na - a[i+1]); a[i+1] = na; } } copy_a[0] = -1; ll count2 = 1; ll sum2 = 0; for(int i = 0;i<n-1;i++){ sum2 += copy_a[i];//i項までの和 if(sum2 < 0 && sum2 + copy_a[i+1] <=0){ ll na = 1 -sum2;//sumに何を足せば1になるか これを新たなa[i+1]にする count2 += abs(na - copy_a[i+1]); copy_a[i+1] = na; } if(sum2 > 0 && sum2 + copy_a[i+1] >=0){ ll na = -1 -sum2;//sumに何を足せば-1になるか これを新たなa[i+1]にする count2 += abs(na - copy_a[i+1]); copy_a[i+1] = na; } } count = min(count1,count2); } cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<n;i++) #define FOR(i,start,end) for(int i=start;i<=end;i++) const int INF = 1001001001; typedef long long ll; const ll MOD=1000000007; using namespace std; 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>auto MAX(const T& a) { return *max_element(a.begin(),a.end()); } template<class T>auto MIN(const T& a) { return *min_element(a.begin(),a.end()); } template<class T, class U>U SUM(const T& a, const U& v) { return accumulate(a.begin(),a.end(), v); } template<class T, class U>U COUNT(const T& a, const U& v) { return count(a.begin(),a.end(), v); } template<class T, class U>int LOWER(const T& a, const U& v) { return lower_bound(a.begin(),a.end(), v) - a.begin(); } template<class T, class U>int UPPER(const T& a, const U& v) { return upper_bound(a.begin(),a.end(), v) - a.begin(); } int GCD(int a, int b) { return b ? GCD(b, a%b) : a; } int LCM(int a, int b) { int g = GCD(a, b); return a / g * b; } //--------------------------------------------------------------------------------------------------- template<int MOD> struct ModInt { static const int Mod = MOD; unsigned x; ModInt() : x(0) { } ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } int get() const { return (int)x; } ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; } ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; } ModInt &operator/=(ModInt that) { return *this *= that.inverse(); } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return ModInt(u); } bool operator==(ModInt that) const { return x == that.x; } bool operator!=(ModInt that) const { return x != that.x; } ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; } }; template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; }; template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) { ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; } template<typename T, int FAC_MAX> struct Comb { vector<T> fac, ifac; Comb(){fac.resize(FAC_MAX,1);ifac.resize(FAC_MAX,1);FOR(i,1,FAC_MAX-1)fac[i]=fac[i-1]*i; ifac[FAC_MAX-1]=T(1)/fac[FAC_MAX-1];for(int i=FAC_MAX-2;i>=1;i--)ifac[i]=ifac[i+1]*T(i+1);} T aPb(int a, int b) { if (b < 0 || a < b) return T(0); return fac[a] * ifac[a - b]; } T aCb(int a, int b) { if (b < 0 || a < b) return T(0); return fac[a] * ifac[a - b] * ifac[b]; } T nHk(int n, int k) { if (n == 0 && k == 0) return T(1); if (n <= 0 || k < 0) return 0; return aCb(n + k - 1, k); } // nHk = (n+k-1)Ck : n is separator T pairCombination(int n) {if(n%2==1)return T(0);return fac[n]*ifac[n/2]/(T(2)^(n/2));} // combination of paris for n }; typedef ModInt<1000000007> mint; int main(void){ // Your code here! int n; cin >> n; vector<ll> a(n); rep(i,n) cin >> a[i]; // +-+-.... ll sh = 0; ll nw = 0; rep(i,n) { nw += a[i]; if(i%2 == 0) { if(nw<= 0) { sh += 1 - nw; nw = 1; } } if(i%2 != 0) { if(nw>=0){ sh += nw + 1; nw = -1; } } } // -+-+... ll hs = 0; nw = 0; rep(i,n) { if(i%2 == 0) if(nw>=0) hs += 1 + nw,nw = -1; if(i%2 != 0) if(nw <= 0) += 1 - nw,nw = 1; } cout << min(hs,sh) << endl; } // 基本的にはlong long を使いましょう
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; bool DifSign(int a, int b) { if (a == 0 || b == 0) return false; return ((a > 0 && 0 > b) || (b > 0 && 0 > a)); } int solver(vector<int> A, int dsum) { int ans = 0; for (int i = 1; i < A.size(); i++) { if (!DifSign(dsum, dsum + A[i])) { int addnum = abs(dsum + A[i]) + 1; ans += addnum; if (dsum + A[i] > 0) A[i] -= addnum; else if (dsum + A[i] < 0) A[i] += addnum; else { if (dsum > 0) A[i] -= addnum; else A[i] += addnum; } } dsum += A[i]; } return ans; } int main() { int N; int dsum = 0; int ans1 = 0; int ans2 = 0; cin >> N; vector<int> A(N); vector<int> B(N); for (int i = 0; i < N; i++) { cin >> A[i]; B[i] = A[i]; } if (A[0] < 0) { ans1 += abs(A[0]) + 1; A[0] = 1; } dsum += A[0]; ans1 += solver(A, dsum); dsum = 0; if (B[0] > 0) { ans2 += abs(B[0]) + 1; B[0] = -1; } dsum += B[0]; ans2 += solver(B, dsum); cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { bool ch = false; long long N, i; long long ans = 0, count = 0; cin >> N; long long a[N]; cin >> a[0]; ans += a[0]; if (ans > 0) ch = true; else ch = false; for (i = 1; i < N; i++) { cin >> a[i]; if (ch) { if (ans >= -a[i]) { count += ans + a[i] + 1; ans = -1; } else ans += a[i]; ch = false; } else { if (ans <= -a[i]) { count += -ans - a[i] + 1; ans = 1; } else ans += a[i]; ch = true; } } long long con = 0; if (a[0] > 0) { ans = -1; ch = false; } else { ans = 1; ch = true; } con = a[0] + 1; for (i = 1; i < N; i++) { if (ch) { if (ans >= -a[i]) { count += ans + a[i] + 1; ans = -1; } else ans += a[i]; ch = false; } else { if (ans <= -a[i]) { count += -ans - a[i] + 1; ans = 1; } else ans += a[i]; ch = true; } } cout << min(count, con) << 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; signed main() { cin.tie(0); ios::sync_with_stdio(false); int64_t n; cin >> n; vector<int64_t> a(n); for (int64_t i = 0; i < n; i++) cin >> a[i]; int64_t sum1 = 0, cost1 = 0; for (int64_t i = 0; i < n; i++) { sum1 += a[i]; if (i % 2 == 0 && sum1 < 0) sum1 += abs(sum1) + 1, cost1 += abs(sum1) + 1; if (i % 2 == 1 && sum1 > 0) sum1 -= abs(sum1) - 1, cost1 += abs(sum1) + 1; } int64_t sum2 = 0, cost2 = 0; for (int64_t i = 0; i < n; i++) { sum2 += a[i]; if (i % 2 == 0 && sum1 > 0) sum2 -= abs(sum2) - 1, cost2 += abs(sum2) + 1; if (i % 2 == 1 && sum1 < 0) sum2 += abs(sum2) + 1, cost1 += abs(sum2) + 1; } cout << min(cost1, cost2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int sum; long long int ans1 = 0, ans2 = 0; vector<long long int> a; for (int i = 1; i <= n; i++) { long long int tmp; cin >> tmp; a.push_back(tmp); } sum = 0; for (int i = 1; i <= n; i++) { sum += a[i - 1]; if (i % 2 == 0 && sum < 0) { ans1 += abs(sum - 1); sum = 1; } else if (i % 2 == 1 && sum > 0) { ans1 += abs(sum - (-1)); sum = -1; } } sum = 0; for (int i = 1; i <= n; i++) { sum += a[i - 1]; if (i % 2 == 0 && sum > 0) { ans2 += abs(sum - (-1)); sum = -1; } else if (i % 2 == 1 && sum < 0) { ans2 += abs(sum - 1); sum = 1; } } std::cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<long long> A(N); for (int i = 0; i < N; i++) { cin >> A.at(i); } long long C1 = 0, C2 = 0; long long sum = 0; for (int i = 0; i < N; i++) { sum += A.at(i); if (i % 2 == 0 && sum <= 0) { C1 += abs(sum) + 1; sum = 1; } if (i % 2 == 1 && sum >= 0) { C1 += abs(sum) + 1; sum = -1; } } for (int i = 0; i < N; i++) { sum += A.at(i); if (i % 2 == 1 && sum <= 0) { C2 += abs(sum) + 1; sum = 1; } if (i % 2 == 0 && sum >= 0) { C2 += abs(sum) + 1; sum = -1; } } cout << min(C1, C2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int getS(int val) { if (val < 0) return -1; if (val > 0) return 1; return 0; } int minNum(vector<int> &arr, int n, int sign) { int acu = 0, sol = 0; for (int i = 0; i < n; i++) { acu += arr[i]; if (getS(acu) != sign) { sol += abs(sign - acu); acu = sign; } sign = sign * -1; } return sol; } int main() { int n; cin >> n; vector<int> arr(n); for (int i = 0; i < n; i++) cin >> arr[i]; int a1 = minNum(arr, n, -1); int a2 = minNum(arr, n, 1); cout << (a1 < a2 ? a1 : a2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<iostream> #include<string> #include<iomanip> #include<cmath> #include<vector> #include<algorithm> using namespace std; #define int long long #define endl "\n" const long long INF = (long long)1e18; const long long MOD = 1'000'000'007; string yn(bool f){return f?"Yes":"No";} string YN(bool f){return f?"YES":"NO";} signed main(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout<<fixed<<setprecision(10); int n; int sum = 0, sum2 = 0; int ans = 0, ans2 = 0; vector<int> a; cin>>n; a.assign(n, 0); for(int i = 0; i < n; i++){ cin>>a[i]; if(i){ if(sum > 0){ sum += a[i]; if(sum >= 0){ // cout<<"Y"<<endl; ans += sum+1; sum = -1; } } else { sum += a[i]; if(sum <= 0){ // cout<<"Z"<<endl; ans += 1-sum; sum = 1; } } } else { sum = a[i]; if(sum == 0){ sum = 1; ans = 1; } } // cout<<ans<<" "<<sum<<endl; } for(int i = 0; i < n; i++){ if(i){ if(sum2 > 0){ sum2 += a[i]; if(sum2 >= 0){ ans2 += sum2+1; sum2 = -1; } } else { sum2 += a[i]; if(sum2 <= 0){ ans2 += 1-sum2; sum2 = 1; } } } else { sum2 = a[i]; if(sum2 == 0){ sum2 = -1; ans2 = 1; } } } cout<<min(ans,ans2)<<endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long zero = 0; int N; cin >> N; vector<int> sum(N, 0); int now; cin >> now; sum[0] = now; for (int i = 1; i < N; i++) { cin >> now; sum[i] = sum[i - 1] + now; } long change = 0; long ansp = 0; int i = 0; while (i < N) { ansp += max(1 - (sum[i] + change), zero); change += max(1 - (sum[i] + change), zero); i++; if (i == N) { break; } ansp += max((sum[i] + change) + 1, zero); change -= max((sum[i] + change) + 1, zero); i++; } change = 0; long ansm = 0; i = 0; while (i < N) { ansm += max((sum[i] + change) + 1, zero); change -= max((sum[i] + change) + 1, zero); i++; if (i == N) { break; } ansm += max(1 - (sum[i] + change), zero); change += max(1 - (sum[i] + change), zero); i++; } cout << min(ansp, ansm) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int Z = 0; int Y = 0; int positive(int X) { if (X <= 0) { while (X <= 0) { Z++; X++; } } return X; } int negative(int X) { if (X >= 0) { while (X >= 0) { Z++; X--; } } return X; } int main() { int U; int N; cin >> N; int A[100008]; for (int i = 0; i < N; i++) { cin >> A[i]; } for (int i = 0; i < N; i++) { Y += A[i]; if (i % 2 == 0) Y = positive(Y); else Y = negative(Y); } U = Z; Y = 0; Z = 0; for (int i = 0; i < N; i++) { Y += A[i]; if (i % 2 != 0) Y = positive(Y); else Y = negative(Y); } if (Z < U) { cout << Z << endl; } else { cout << U << 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()) arr1=list(map(int,input().split())) arr2=arr1[:] ans1=0 ans2=0 if arr1[0]<=0: ans1+=abs(arr1[0])+1 arr1[0]=1 if arr2[0]>=0: ans2+=arr2[0]+1 arr2[0]=-1 sum1=arr1[0] for i in range(1,n): tmp=sum1+arr1[i] if i%2==1: if tmp>=0: ans1+=tmp+1 sum1=-1 else: sum1=tmp else: if tmp<=0: ans1+=abs(tmp)+1 sum1=1 else: sum1=tmp sum2=arr2[0] for i in range(1,n): tmp=sum2+arr2[i] if i%2==0: if tmp>=0: ans2+=tmp+1 sum2=-1 else: sum2=tmp else: if tmp<=0: ans2+=abs(tmp)+1 sum2=1 else: sum2=tmp print(ans1,ans2)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; class Program { static string InputPattern = "InputX"; static List<string> GetInputList() { var WillReturn = new List<string>(); if (InputPattern == "Input1") { WillReturn.Add("4"); WillReturn.Add("1 -3 1 0"); //4 } else if (InputPattern == "Input2") { WillReturn.Add("5"); WillReturn.Add("3 -6 4 -5 7"); //0 } else if (InputPattern == "Input3") { WillReturn.Add("6"); WillReturn.Add("-1 4 3 2 -5 4"); //8 } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static void Main() { List<string> InputList = GetInputList(); int[] AArr = InputList[1].Split(' ').Select(X => int.Parse(X)).ToArray(); if (AArr[0] == 0) { AArr[0] = 1; long Cost1 = Solve(AArr) + 1; AArr[0] = -1; long Cost2 = Solve(AArr) + 1; Console.WriteLine(Math.Min(Cost1, Cost2)); } else { Console.WriteLine(Solve(AArr)); } } static long Solve(int[] pArr) { long Cost = 0; long RunSum = pArr[0]; for (int I = 1; I <= pArr.GetUpperBound(0); I++) { if (RunSum < 0) { RunSum += pArr[I]; if (RunSum > 0) continue; Cost += Math.Abs(RunSum) + 1; RunSum = 1; } else if (RunSum > 0) { RunSum += pArr[I]; if (RunSum < 0) continue; Cost += Math.Abs(RunSum) + 1; RunSum = -1; } } return Cost; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = [int(i) for i in input().split()] s0 = a[0] count=0 i=0 while a[i]!=0:i++ if a[i]>0: s0+=1 count+=1 else: s0-=1 count+=1 for i in range(1,n): s1 = s0+a[i] if s0*s1>=0: if s1>0: a[i]-=(abs(s1)+1) count+=(abs(s1)+1) elif s1<0: a[i]+=(abs(s1)+1) count+=(abs(s1)+1) elif s1==0: if s0>0: a[i]-=1 count+=1 elif s0<0: a[i]+=1 count+=1 s0 += a[i] print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[100010]; int ans = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (i != 0) { a[i] += a[i - 1]; if (a[i] == 0) { if (a[i - 1] > 0) { ans++; a[i]--; } else { ans++; a[i]++; } } else if (a[i - 1] > 0) { if (a[i] > 0) { ans += abs(-1 - a[i]); a[i] = -1; } } else if (a[i - 1] < 0) { if (a[i] < 0) { ans += 1 - a[i]; a[i] = 1; } } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#[allow(unused_imports)] use proconio::input; #[allow(unused_imports)] use proconio::marker::Chars; #[allow(unused_imports)] use std::cmp::{max, min}; #[allow(unused)] const ALPHA_SMALL: [char; 26] = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ]; #[allow(unused)] const ALPHA: [char; 26] = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ]; #[allow(unused)] 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(unused)] fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>() .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect() } #[allow(unused)] fn read_touple<T: std::str::FromStr + Copy>() -> (T, T) { let v: Vec<T> = read_vec(); assert_eq!(v.len(), 2); (v[0], v[1]) } fn main() { input!(N: usize); input!(A: [i64; N]); let mut ans: i64 = 1_000_000_000_000; for i in 0..2 { let mut st = SegmentTree::<i64>::new(&A); // i % 2 == 0 ==> 偶数番目が正 // i % 2 == 1 ==> 偶数番目が負 let mut tot: i64 = 0; for j in 0..N { let mut diff = 0; let now = st.get(0, j + 1); if i % 2 == 0 { if j % 2 == 0 && now <= 0 { // 1にする diff = 1 - now; } else if j % 2 == 1 && now >= 0 { // -1にする diff = -1 - now; } } else { if j % 2 == 0 && now >= 0 { // -1にする diff = -1 - now; } else if j % 2 == 1 && now <= 0 { // 1にする diff = 1 - now; } } //println!("j = {}, now = {}, diff = {}", j, now, diff); tot += diff.abs(); st.update(j, A[j] + diff); } ans = min(ans, tot); } println!("{}", ans); } impl Monoid for i64 { const E: i64 = 0; fn calc(&self, rhs: &i64) -> Self { self + rhs } } #[allow(clippy::declare_interior_mutable_const)] trait Monoid<Rhs = Self> { const E: Rhs; fn calc(&self, other: &Rhs) -> Self; } #[derive(Debug, PartialEq, Clone)] struct SegmentTree<T: Clone + Monoid> { n: usize, //元の配列に一番近い2の冪乗を格納する tree: Vec<T>, } // 区間の最小の値を求めるセグメント木 impl<T: Clone + Monoid> SegmentTree<T> { // 元の配列をセグメント木で表現 // 2^nが元の配列のサイズ以上になる最小のnをNとする // O(N) fn new(v: &[T]) -> SegmentTree<T> { let mut n: usize = 1; // セグメント木の葉の要素数 while n < v.len() { n *= 2; } // セグメント木を表現するベクター let mut tree = vec![T::E; 2 * n - 1]; // 区間における最小値を求めるセグメント木に注意 // 葉から順に値を格納していく // まず最下段に値を格納する for (i, e) in v.iter().enumerate() { tree[i + n - 1] = e.clone(); } // 最下段の次の下段から値を書くのしていく for i in (0..(n - 1)).rev() { tree[i] = T::calc(&tree[2 * i + 1], &tree[2 * i + 2]); // 区間における最小値を求めるセグメント木に注意 } SegmentTree { n, tree } } // 元の配列のi番目の値が変更された時にセグメント木を更新 // O(logN) Nは元の配列の長さを超える最小の2の冪乗 fn update(&mut self, i: usize, val: T) { // 最下段の葉を更新 let mut m = i + self.n - 1; self.tree[m] = val; while m > 0 { m = (m - 1) / 2; self.tree[m] = T::calc(&self.tree[2 * m + 1], &self.tree[2 * m + 2]); // 区間における最小値を求めるセグメント木に注意 } } // 指定された区間の値を求める // 区間は半開区間[i, j)での値を求める // O(logN) fn get(&self, i: usize, j: usize) -> T { fn get_sub<U: Clone + Monoid>( t: &SegmentTree<U>, a: usize, b: usize, k: usize, l: usize, r: usize, ) -> U { // 求めたい区間が今いるノードの対応する区間と交差していない if r <= a || b <= l { return U::E; } // 区間における最小値を求めるセグメント木に注意 // 求めたい区間が今いるノードの対応する区間を被覆してる if a <= l && r <= b { return t.tree[k].clone(); } // 求めたい区間と今いるノードと対応している区間が交差してるとき // 子を探索する // 左の子の値 let vl = get_sub(t, a, b, 2 * k + 1, l, (l + r) / 2); // 右のこの値 let vr = get_sub(t, a, b, 2 * k + 2, (l + r) / 2, r); U::calc(&vl, &vr) } get_sub(&self, i, j, 0, 0, self.n) } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int sgn(int a, int b) { if (a > 0 and b > 0) return 1; else if (a < 0 and b < 0) return 1; else return 0; } int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int sum_b = 0; int sum_n = 0; int ans = 0; for (int i = 0; i < n; i++) { if (i == 0) { sum_n += a[i]; } if (i != 0) { sum_b = sum_n; sum_n += a[i]; if (sgn(sum_b, sum_n)) { if (sum_n > 0) { ans += abs(1 + sum_n); sum_n = -1; } else if (sum_n < 0) { ans += abs(sum_n - 1); sum_n = 1; } else { if (sum_b < 0) { cout << "stp5" << endl; sum_n += 1; ans += 1; } else { cout << "stp6" << endl; sum_n += -1; ans += 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
python2
# -*- coding:utf-8 -*- def sign(x): if (x > 0): return 1 elif (x < 0): return -1 else: return 0 n = int(raw_input()) numlist = (raw_input()).split(' ') sumlist = [int(numlist[0])] count = 0 for i in range(1, n): sumlist.append(sumlist[i-1] + int(numlist[i])) while ((sign(sumlist[i-1]) == sign(sumlist[i])) or (sumlist[i] == 0)): if (sumlist[i] > 0): #i-1,i番目までのsumがともに正 numlist[i] = int(numlist[i]) - 1 sumlist[i] -= 1 count += 1 elif (sumlist[i] < 0): #i-1,i番目までのsumがともに負 numlist[i] = int(numlist[i]) + 1 sumlist[i] += 1 count += 1 else: if (sumlist[i-1] > 0): numlist[i] = int(numlist[i]) - 1 sumlist[i] -= 1 if (sumlist[i-1] < 0): numlist[i] = int(numlist[i]) + 1 sumlist[i] += 1 count += 1 print count
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int number[100001]; int N; int sum[100001]; int main() { int mul = 0; long long int answer = 0; int fugou = 0; scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d", &(number[i])); mul += number[i]; sum[i] = mul; if (i > 0) { if (sum[i - 1] * mul >= 0) { answer += (long long int)abs(mul) + 1; } } } printf("%d\n", answer); 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 <typename T> bool PN(T x) { if (x <= 1) return false; if (x == 2) return true; for (int i = 2; i < sqrt(x) + 1; i++) if (x % i == 0) return false; return true; } const long long MOD = 1e9 + 7; void solve() { int n; cin >> n; int a[n]; long long sum = 0; long long sum2 = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; } long long ans = 0; long long ans2 = 0; for (int i = 0; i < n; ++i) { if (i == 0) { sum += a[i]; continue; } if (sum > 0) { sum += a[i]; if (sum > 0) { ans += sum + 1; sum = -1; } else if (sum < 0) { continue; } else { ans++; sum = -1; } } else if (sum < 0) { sum += a[i]; if (sum > 0) { continue; } else if (sum < 0) { ans += abs(sum) + 1; sum = 1; } else { ans++; sum = 1; } } } for (int i = 0; i < n; ++i) { if (i == 0) { if (a[i] != 0) { sum2 = -(a[i]) / abs(a[i]); } ans2 += abs(a[i] + 1); continue; } if (sum2 > 0) { sum2 += a[i]; if (sum2 > 0) { ans2 += sum2 + 1; sum2 = -1; } else if (sum2 < 0) { continue; } else { ans2++; sum2 = -1; } } else if (sum2 < 0) { sum2 += a[i]; if (sum2 > 0) { continue; } else if (sum < 0) { ans2 += abs(sum2) + 1; sum2 = 1; } else { ans2++; sum2 = 1; } } } cout << min(ans, ans2) << endl; } int main() { solve(); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MAX = 1e+5; int n; int a[MAX + 1]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; bool flag; int sum; long long ans1 = 0; sum = a[0]; flag = true; for (int i = 1; i < n; i++) { sum += a[i]; if (flag && sum <= 0) { ans1 += -sum + 1; sum = 1; } else if (!flag && sum >= 0) { ans1 += sum + 1; sum = -1; } flag = !flag; } long long ans2 = 0; sum = a[0]; flag = false; for (int i = 1; i < n; i++) { sum += a[i]; if (flag && sum <= 0) { ans2 += -sum + 1; sum = 1; } else if (!flag && sum >= 0) { ans2 += sum + 1; sum = -1; } flag = !flag; } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { private static long[] add(long[] a, long add) { long[] res = new long[a.length]; for (int i = 0; i < a.length; i++) { res[i] = a[i]; } res[0] += add; return res; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { int n = sc.nextInt(); long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextLong(); } boolean isZero = (a[0] == 0); long ans = Long.MAX_VALUE; if ( isZero ) { long[] ta = add(a, 1); ans = Math.min(ans, solve(ta)); ta = add(a, -1); ans = Math.min(ans, solve(ta)); } else { ans = Math.min(ans, solve(a)); long[] ta = add(a, -a[0] + (a[0] < 0 ? 1 : -1)); ans = Math.min(ans, solve(ta)); } System.out.println(ans); } } private static long solve(long[] a) { long res = 0; long sum = a[0]; boolean isPositive = sum > 0; for (int i = 1; i < a.length; i++) { isPositive = !isPositive; sum += a[i]; if ( isPositive && sum <= 0 ) { res += Math.abs(sum) + 1; sum = 1; } else if ( !isPositive && sum >= 0 ) { res += Math.abs(sum) + 1; sum = -1; } } return res; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
object Main { def main(args: Array[String]): Unit = { import scala.io.StdIn.readLine val _ = readLine val datA = readLine.split(" ").map(_.toLong).dropWhile(_ == 0) def judgement(acc: (Long, Long), cur: (Long, Long)): (Long, Long) = { val sum = acc._1 val count = acc._2 val a = cur._1 if (sum > 0) { if (sum + a >= 0) (-1, count + math.abs(sum + a) + 1) else (sum + a, count) } else if (sum < 0) { if (sum + a <= 0) (1, count + math.abs(sum + a) + 1) else (sum + a, count) } else { (a, count) } } val posA = datA.map(a => (a, 0L)).foldLeft((0L, 0L)){ judgement(_, _) } val negA = datA.tail.map(a => (a, 0L)).foldLeft( if(datA.head > 0) -1L else 1L, math.abs(datA.head) + 1 ){ judgement(_, _) } val ans = math.min(posA._2, negA._2) println(ans) } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; long long Num[1111111]; long long Sum[1111111]; long long Out; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &Num[i]); if (Num[1] == 0) { long long Out1 = 0; long long Out2 = 0; Num[1] = 1; for (int i = 1; i <= n; i++) { Sum[i] = Sum[i - 1] + Num[i]; if (i % 2 == 0) { if (Sum[i] >= 0) { Out1 += (Sum[i] + 1); Sum[i] = -1; } } else { if (Sum[i] <= 0) { Out1 += (1 - Sum[i]); Sum[i] = 1; } } } Num[1] = -1; for (int i = 1; i <= n; i++) { Sum[i] = Sum[i - 1] + Num[i]; if (i % 2 == 1) { if (Sum[i] >= 0) { Out2 += (Sum[i] + 1); Sum[i] = -1; } } else { if (Sum[i] <= 0) { Out2 += (1 - Sum[i]); Sum[i] = 1; } } } printf("%lld", min(Out1, Out2)); } if (Num[1] > 0) { for (int i = 1; i <= n; i++) { Sum[i] = Sum[i - 1] + Num[i]; if (i % 2 == 0) { if (Sum[i] >= 0) { Out += (Sum[i] + 1); Sum[i] = -1; } } else { if (Sum[i] <= 0) { Out += (1 - Sum[i]); Sum[i] = 1; } } } } else { for (int i = 1; i <= n; i++) { Sum[i] = Sum[i - 1] + Num[i]; if (i % 2 == 1) { if (Sum[i] >= 0) { Out += (Sum[i] + 1); Sum[i] = -1; } } else { if (Sum[i] <= 0) { Out += (1 - Sum[i]); Sum[i] = 1; } } } } printf("%lld\n", Out); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } long long ans = 0; long long ans_1 = 100000000; long long sum = v[0]; long long sum_old; if (sum == 0) { ans = 0; sum = 1; ans += 1; sum_old = 1; for (int i = 1; i < n; i++) { sum += v[i]; if (sum_old < 0) { if (sum <= 0) { ans += 1 - sum; sum = 1; } } if (sum_old > 0) { if (sum >= 0) { ans += 1 + sum; sum = -1; } } sum_old = sum; } ans_1 = min(ans_1, ans); ans = 0; sum = -1; ans += 1; sum_old = -1; for (int i = 1; i < n; i++) { sum += v[i]; if (sum_old < 0) { if (sum <= 0) { ans += 1 - sum; sum = 1; } } if (sum_old > 0) { if (sum >= 0) { ans += 1 + sum; sum = -1; } } sum_old = sum; } ans_1 = min(ans_1, ans); } ans = 0; sum = v[0]; sum_old = sum; for (int i = 1; i < n; i++) { sum += v[i]; if (sum_old < 0) { if (sum <= 0) { ans += 1 - sum; sum = 1; } } if (sum_old > 0) { if (sum >= 0) { ans += 1 + sum; sum = -1; } } sum_old = sum; } ans_1 = min(ans_1, ans); cout << ans_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; const int INF = 1000000001; const double PI = 3.141592653589; const long long LMAX = 1000000000000001; long long gcd(long long a, long long b) { if (a < b) swap(a, b); while ((a % b) != 0) { a = b; b = a % b; } return b; } int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<long long> dp(n, 0); long long sum = a[0]; for (int i = 1; i < n; i++) { if (sum > 0) { if (sum + a[i] < 0) { dp[i] = dp[i - 1]; sum += a[i]; } else { dp[i] = dp[i - 1] + abs(sum + a[i]) + 1; sum = -1; } } else { if (sum + a[i] > 0) { dp[i] = dp[i - 1]; sum += a[i]; } else { dp[i] = dp[i - 1] + abs(sum + a[i]) + 1; sum = 1; } } } cout << dp[n - 1] << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long minA = 0; long sumA = 0; for (int i = 0; i < n; i++) { sumA += a[i]; if (i % 2 == 0) { if (sumA <= 0) { minA += abs(sumA) + 1; sumA += abs(sumA) + 1; } } else { if (sumA >= 0) { minA += abs(sumA) + 1; sumA -= abs(sumA) + 1; } } } long minB = 0; long sumB = 0; for (int i = 0; i < n; i++) { sumB += a[i]; if ((i + 1) % 2 == 0) { if (sumB <= 0) { minB += abs(sumB) + 1; sumB += abs(sumB) + 1; } } else { if (sumB >= 0) { minB += abs(sumB) + 1; sumB -= abs(sumB) + 1; } } } cout << min(minA, minB) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) import numpy as np na = np.array(a).cumsum() cnt = 0 hoge = 0 if(na[0] > 0): for i in range(n): delta = abs(na[i]) + 1 if(i % 2 == 0 and na[i] <= 0): cnt = cnt + delta hoge += delta na[i] += hoge elif(i % 2 == 1 and na[i] >= 0): cnt = cnt + delta hoge -= delta na[i] -= hoge else: na[i] else: for i in range(n): delta = abs(na[i]) + 1 if(i % 2 == 1 and na[i] <= 0): cnt = cnt + delta hoge += delta na[i] += hoge elif(i % 2 == 0 and na[i] >= 0): cnt = cnt + delta hoge -= delta na[i] -= hoge else: na[i] print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; int main() { int n; cin >> n; vector<long long> accum(n + 1); vector<long long> a(n); accum[0] = 0; for (int i = 0; i < (n); ++i) { cin >> a[i]; accum[i + 1] = a[i] + accum[i]; } long long now = (a[0] == 0) ? 1 : a[0]; long long tmp_ans1 = (a[0] == 0) ? abs(a[0]) + 1 : 0; for (int i = 1; i < n; ++i) { if (now >= 0 && now + a[i] >= 0) { tmp_ans1 += now + a[i] + 1; now = -1; } else if (now < 0 && now + a[i] <= 0) { tmp_ans1 += abs(now + a[i]) + 1; now = 1; } else { now += a[i]; } } now = (a[0] < 0) ? 1 : -1; long long tmp_ans2 = abs(a[0]) + 1; for (int i = 1; i <= n; ++i) { if (now >= 0 && now + a[i] >= 0) { tmp_ans2 += now + a[i] + 1; now = -1; } else if (now < 0 && now + a[i] <= 0) { tmp_ans2 += abs(now + a[i]) + 1; now = 1; } else { now += a[i]; } } cout << min(tmp_ans1, tmp_ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const bool debug = false; using namespace std; const long long MOD = 1000000007; template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main(void) { cin.tie(0); ios::sync_with_stdio(false); long long n; cin >> n; vector<long long> a(n); for (int(i) = 0; (i) < (n); (i)++) { cin >> a[i]; } function<long long()> solve = [&]() { long long sum = 0, cnt = 0; for (int(i) = 0; (i) < (n); (i)++) { if (i == 0) { sum = a[0]; continue; } if (sum > 0 && sum + a[i] >= 0) { cnt += sum + a[i] + 1; sum = -1; } else if (sum < 0 && sum + a[i] <= 0) { cnt += -(sum + a[i]) + 1; sum = 1; } else { sum += a[i]; } } return cnt; }; long long res; if (a[0] == 0) { a[0] = 1; res = solve(); a[0] = -1; chmin(res, solve()); res++; } else if (a[0] > 0) { res = solve(); a[0] = -1; res = chmin(res, solve() + a[0] + 1); } else { res = solve(); a[0] = 1; res = chmin(res, solve() - a[0] + 1); } cout << res << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n, sum, b, c; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } if (a[0] <= 0) { b = 1 - a[0]; sum = 1; } else { sum = a[0]; b = 0; } for (int i = 1; i < n; i++) { if (i % 2) { if (-sum - 1 < a[i]) { b += abs(a[i] + sum + 1); sum = -1; } else { sum += a[i]; } } else { if (1 - sum > a[i]) { b += abs(a[i] + sum - 1); sum = 1; } else { sum += a[i]; } } } if (a[0] >= 0) { c = 1 - a[0]; sum = -1; } else { sum = a[0]; c = 0; } for (int i = 1; i < n; i++) { if (i % 2) { if (1 - sum > a[i]) { c += abs(a[i] + sum - 1); sum = 1; } else { sum += a[i]; } } else { if (-sum - 1 < a[i]) { c += abs(a[i] + sum + 1); sum = -1; } else { sum += a[i]; } } } cout << min(b, c); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) ans = 0 while a[0] == 0: if a[1] > 0: a[0] = -1 elif a[1] < 0: a[0] = 1 else: a.pop(0) ans += 1 if a[0] < 0: a = list(map(lambda x: x * (-1), a)) product = 0 for i in range(len(a)): product += a[i] if i % 2 == 0: if product <= 0: ans += abs(product) + 1 product = 1 elif i % 2 == 1: if product >= 0: ans += abs(product) + 1 product = -1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) ans = 0 while a[0] == 0: if a[1] > 0: a[0] = -1 elif a[1] < 0: a[0] = 1 a.pop(0) ans += 1 _a = list(map(lambda x: x * (-1), a)) def func(l: list): _ans = 0 product = 0 for i in range(len(l)): product += l[i] if i % 2 == 0: if product <= 0: _ans += abs(product) + 1 product = 1 elif i % 2 == 1: if product >= 0: _ans += abs(product) + 1 product = -1 return _ans ans += min(func(a), func(_a)) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; #define forx(i,a,b) for(int i=(a);i<(b);i++) #define rep(j,n) for(int j=0;j<(n);j++) typedef long long ll; int main() { int n,ansa=0,ansb=0,suma=0;sumb=0; cin>>n; bool plus=true; vector<int>a(n); rep(i,n){ int a,b; cin>>t[i]; a=b=t[i]; while(plus&&suma+a<=0){ a++; ansa++; } while(!plus&&suma+a>=0){ a--; ansa++; } while(plus&&sumb+b>=0){ b++; ansb++; } while(!plus&&sumb+b<=0){ b--; ansb++; } suma+=a; sumb+=b; plus=!plus; } cout<<min(ansa,ansb)<<endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> S(N + 1); for (int i = 1; i <= N; ++i) { cin >> S[i]; S[i] += S[i - 1]; } int ians = (1 << 30); for (int j = -1; j <= 1; j += 2) { vector<int> S_(S); int ans = 0; int add = 0; int sign = j; for (int i = 1; i <= N; ++i) { S_[i] += add; int sign_i = ((S_[i] >> 31) << 1) + 1; if (S_[i] == 0) { ans += 1; add += -sign; S_[i] += -sign; } else if (sign_i == sign) { ans += abs(-sign_i - S_[i]); add += -sign_i - S_[i]; S_[i] = -sign_i; } sign = -sign; } ians = min(ans, ians); } cout << ians << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int, input().split())) def sol(S): ret = 0 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 return ret print(sol(A[0]))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import numpy as np import copy N=int(input()) l=list(map(int, input().split())) #リスト入力 cp = copy.copy(l) #even_c=0 #odd_c=0 if l[0]!=0: #l[0]=-1 #even_c=even_c+1 for k in range(1,N): #print(sum(l[:k]),sum(l[:k+1])) if sum(l[:k])*sum(l[:k+1])>0: #print(k) l[k]=-np.sign(sum(l[:k]))-sum(l[:k]) #print(l) if sum(l)==0: print(1+sum([abs(l[n]-cp[n]) for n in range(N)])) else: print(sum([abs(l[n]-cp[n]) for n in range(N)])) else: #1 sei_l=copy.copy(l) sei_l[0]=1 for k in range(1,N): #print(sum(l[:k]),sum(l[:k+1])) if sum(sei_l[:k])*sum(sei_l[:k+1])>0: #print(k) sei_l[k]=-np.sign(sum(sei_l[:k]))-sum(sei_l[:k]) #print(l) if sum(sei_l)==0: c1=1+sum([abs(sei_l[n]-cp[n]) for n in range(N)]) else: c1=sum([abs(sei_l[n]-cp[n]) for n in range(N)]) #-1 fu_l=copy.copy(l) sei_l[0]=-1 for k in range(1,N): #print(sum(l[:k]),sum(l[:k+1])) if sum(fu_l[:k])*sum(fu_l[:k+1])>0: #print(k) fu_l[k]=-np.sign(sum(fu_l[:k]))-sum(fu_l[:k]) #print(l) if sum(fu_l)==0: c2=1+sum([abs(fu_l[n]-cp[n]) for n in range(N)]) else: c2=sum([abs(fu_l[n]-cp[n]) for n in range(N)]) print(max(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
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 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 edit += sign * s s -= s edit += 1 s -= sign 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) 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
python3
n = int(input()) numbers = list(map(int, input().split())) counter = 0 sum_i_n = 0 sum_i_n_1 = numbers[0] for i in range(len(numbers) - 1): sum_i_n += numbers[i] sum_i_n_1 += numbers[i + 1] if sum_i_n == 0: numbers[i] += 1 sum_i_n += 1 sum_i_n_1 += 1 counter += 1 if sum_i_n * sum_i_n_1 > 0: sub_n = abs(sum_i_n) sub_n1 = abs(sum_i_n_1) if abs(sum_i_n) - abs(sum_i_n_1) > 0: if sum_i_n_1 > 0: numbers[i + 1] -= (sub_n1 + 1) sum_i_n_1 -= (sub_n1 + 1) counter += (sub_n1 + 1) else: numbers[i + 1] += (sub_n1 + 1) sum_i_n_1 += (sub_n1 + 1) counter += (sub_n1 + 1) else: if sum_i_n > 0: numbers[i] -= (sub_n + 1) sum_i_n -= (sub_n + 1) sum_i_n_1 -= (sub_n + 1) counter += (sub_n + 1) else: numbers[i] += (sub_n + 1) sum_i_n += (sub_n + 1) sum_i_n_1 += (sub_n + 1) counter += (sub_n + 1) # sub = abs(sum_i_n_1) + 1 # counter += sub # if sum_i_n_1 > 0: # numbers[i + 1] -= sub # sum_i_n_1 -= sub # else: # numbers[i + 1] += sub # sum_i_n_1 += sub if sum_i_n_1 == 0: counter += 1 print(counter)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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 MAX = 1e5 + 111; int n; int a[MAX]; long long calc(int id, long long sum) { long long ans = 0; for (int i = id; i < n; ++i) { long long cur = sum + a[i]; if (sum < 0 && cur > 0) { sum = cur; continue; } if (sum > 0 && cur < 0) { sum = cur; continue; } if (cur == 0) { if (sum < 0) cur = 1; else cur = -1; sum = cur; ans++; continue; } if (sum < 0) { ans += 1 - cur; cur = 1; } else { ans += cur + 1; cur = -1; } sum = cur; } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; int id = -1; for (int i = 0; i < n; ++i) { cin >> a[i]; if (a[i] != 0 && id == -1) id = i; } if (id == -1) { cout << 1 + (n - 1) * 2; return 0; } if (id != 0) { long long ans1 = calc(id, -1); long long ans2 = calc(id, 1); cout << min(ans1, ans2) + 1 + 2 * id; } else { long long ans1 = calc(1, a[0]); cout << ans1; } 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()) b = [int(x) for x in input().split()] a = list() temp = 0 count1 = 0 count2 = 0 a = b.copy() if a[0] == 0: a[0] = 1 count1 = 1 sum = a[0] for i in range(1, n): if abs(a[i]) <= abs(sum) or a[i] * sum >= 0: if sum > 0: temp = -1 * abs(sum) - 1 count1 += abs(temp - a[i]) else: temp = abs(sum) + 1 count1 += abs(temp - a[i]) a[i] = temp sum += a[i] a = b.copy() count2 = abs(a[0]) + 1 if a[0] == 0: a[0] = 1 count2 = 1 if a[0] > 0: a[0] = -1 else: a[0] = 1 sum = a[0] for i in range(1, n): if abs(a[i]) <= abs(sum) or a[i] * sum >= 0: count2 += abs(sum - a[i]) + 1 if sum > 0: temp = -1 * abs(sum) - 1 count2 += abs(temp - a[i]) else: temp = abs(sum) + 1 count2 += abs(temp - a[i]) a[i] = temp sum += a[i] print(min(count1, count2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> int main() { int n; int a, sum[1000000000], cnt[2] = {}, b; scanf("%d", &n); scanf("%d", &a); sum[0] = a; for (int i = 1; i < n; i++) { scanf("%d", &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("%d\n", cnt[0]); else printf("%d\n", cnt[1]); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) seq = [int(x) for x in input().split()] a_sum = seq[0] op = 0 for a in seq[1:]: tmp = a_sum + a if tmp * a_sum < 0: a_sum = tmp elif a_sum < 0: print(a_sum, a) diff = 1 - a_sum - a a_sum = 1 op += abs(diff) elif a_sum > 0: diff = -1 - a_sum - a a_sum = -1 op += abs(diff) print(op)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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> vector; long long temp; for (int i = 0; i < n; i++) { cin >> temp; vector.push_back(temp); } long long answer = 0; long long sum = 0; for (int i = 0; i < n; i++) { if (sum == 0) sum += vector[0]; else if (sum < 0) { if (sum + vector[i] > 0) { sum += vector[i]; } else { answer += abs((-1) * sum + 1 - vector[i]); sum = 1; } } else { if (sum + vector[i] < 0) { sum += vector[i]; } else { answer += abs((-1) * sum - 1 - vector[i]); sum = -1; } } } cout << answer << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int( input()) A = list( map( int, input().split())) ans = 10**15 for i in [1, -1]: ansi, sums = 0, 0 for a in A: sums += a if sums*i <= 0: ansi += abs(sums-s) sums = s s *= -1 ans = min( ans, ansi) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n + 1, 0); for (int i = 1; i <= n; i++) { cin >> a[i]; a[i] += a[i - 1]; } long long ans = 1LL << 50; for (int k = -1; k <= 1; k += 2) { int sign = k; int plus = 0, minus = 0; for (int i = 1; i <= n; i++) { if (a[i] + plus - minus > 0) { if (sign == -1) minus += a[i] + plus - minus + 1; } else if (a[i] + plus - minus < 0) { if (sign == 1) plus += -(a[i] + plus - minus) + 1; } else { if (sign == -1) minus += 1; else if (sign == 1) plus += 1; } sign *= -1; } if (ans > plus + minus) ans = plus + minus; } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int n, a; long long sum = 0, ans = 0; int s; cin >> n; cin >> sum; if (sum > 0) s = 1; else s = -1; for (int i = 1; i < n; i++) { cin >> a; sum += a; if (sum * s > 0) { ans += abs(sum) + 1; sum = -s; } if (sum == 0) { sum += -s; ans++; } if (sum > 0) s = 1; else s = -1; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
gets xs = gets.split(' ').map(&:to_i) def flip_sign(v) v > 0 ? -1 : 1 end def sign(v) v > 0 ? 1 : -1 end a = xs[1..-1].inject([xs.first,0]){|r,v| sum = r.first count = r.last s = sign(sum) sum += v fs = sign(sum) if s == fs count += sum.abs + 1 sum = flip_sign(s) end [sum, count] }.last b = xs[1..-1].inject([flip_sign(xs.first),xs.first.abs + 1]){|r,v| sum = r.first count = r.last s = sign(sum) sum += v fs = sign(sum) if s == fs count += sum.abs + 1 sum = flip_sign(s) end [sum, count] }.last puts [a,b].min
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) b = [int(x) for x in input().split()] a = list() temp = 0 count1 = 0 count2 = 0 a = b if a[0] == 0: a[0] = 1 count1 = 1 sum = a[0] for i in range(1, n): if abs(a[i]) <= abs(sum) or a[i] * sum >= 0: if sum > 0: temp = -1 * abs(sum) - 1 count1 += abs(temp - a[i]) else: temp = abs(sum) + 1 count1 += abs(temp - a[i]) a[i] = temp sum += a[i] count2 = abs(a[0]) + 1 a = b if a[0] > 0: a[0] = -1 else: a[0] = 1 sum = a[0] for i in range(1, n): if abs(a[i]) <= abs(sum) or a[i] * sum >= 0: count2 += abs(sum - a[i]) + 1 if sum > 0: temp = -1 * abs(sum) - 1 count2 += abs(temp - a[i]) else: temp = abs(sum) + 1 count2 += abs(temp - a[i]) a[i] = temp sum += a[i] print(min(count1, count2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; constexpr auto INF = 100000000000; constexpr auto mod = 1000000007; struct edge { int to, cost; }; long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } long long modinv(long long a, long long 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; } long long int c(long long int a, long long int b, long long int m) { long long int ans = 1; for (long long int i = 0; i < b; i++) { ans *= a - i; ans %= m; } for (long long int i = 1; i <= b; i++) { ans *= modinv(i, m); ans %= m; } return ans; } void dijkdtra(int s, int v, vector<int>& d, vector<vector<edge>>& G) { priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que; d[s] = 0; que.push(pair<int, int>(0, s)); while (!que.empty()) { pair<int, int> p = que.top(); que.pop(); int V = p.second; if (d[V] < p.first) continue; for (int i = 0; i < G[V].size(); i++) { edge e = G[V][i]; if (d[e.to] > d[V] + e.cost) { d[e.to] = d[V] + e.cost; que.push(pair<int, int>(d[e.to], e.to)); } } } } long long int binary_search(vector<int>& s, long long int a) { long long int l = -1; long long int r = (int)s.size(); while (r - l > 1) { long long int mid = l + (r - l) / 2; if (s[mid] >= a) r = mid; else l = mid; } return r; } int k(long long n) { int x = 0; while (n) { x += n % 10; n /= 10; } return x; } long long max(long long x, long long y) { if (x < y) return y; return x; } int main() { long long int n, ans = INF; cin >> n; vector<long long int> a(n), t(n), s(n); for (int i = (0); i < (n); i++) { cin >> a[i]; t[i] = a[i]; s[i] = a[i]; } long long int w = a[0]; if (w <= 0) { w = 1; } for (int i = (1); i < (n); i++) { if (i % 2 == 0) { if (abs(w) >= a[i]) { a[i] = abs(w) + 1; } w += a[i]; } else { if (w >= abs(a[i])) { a[i] = -1 * (w + 1); } w += a[i]; } } w = t[0]; if (w >= 0) { w = -1; } for (int i = (1); i < (n); i++) { if (i % 2 == 1) { if (abs(w) >= t[i]) { t[i] = abs(w) + 1; } w += t[i]; } else { if (w >= abs(t[i])) { t[i] = -1 * (w + 1); } w += t[i]; } } long long int cost1 = 0, cost2 = 0; for (int i = (0); i < (n); i++) { cost1 += abs(s[i] - a[i]); cost2 += abs(s[i] - t[i]); } ans = min(cost1, cost2); 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, t = 0, sum = 0, count = 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; } } cout << count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = [int(_) for _ in input().split()] even_p = 0 even_n = 0 odd_p = 0 odd_n = 0 for i in range(n): if i % 2 == 0: if a[i] > 0: odd_p += 1 elif a[i] < 0: odd_n += 1 else: if a[i] > 0: even_p += 1 elif a[i] < 0: even_n += 1 cnt = 0 if odd_p > even_p: if a[0] > 0: sum_i = a[0] else: cnt += abs(a[0]-1) sum_i = 1 else: if a[0] < 0: sum_i = a[0] else: cnt += abs(a[0] + 1) sum_i = -1 for i in range(1, n): if sum_i > 0: if sum_i + a[i] < 0: sum_i += a[i] else: cnt += abs(a[i]+sum_i+1) sum_i = -1 elif sum_i < 0: if sum_i + a[i] > 0: sum_i += a[i] else: cnt += abs(a[i]+sum_i-1) sum_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; int n; long long a[100004], temp[100004]; long long solve() { long long ans = 0; for (int i = (2); i <= (int)(n); ++i) { if (a[i - 1] > 0) { if (a[i] + a[i - 1] < 0) { a[i] += a[i - 1]; continue; } ans += abs(a[i] + 1 + a[i - 1]); a[i] = -1; } else { if (a[i] + a[i - 1] > 0) { a[i] += a[i - 1]; continue; } ans += abs(a[i] - 1 + a[i - 1]); a[i] = 1; } } return ans; } int main() { scanf("%d", &n); for (int i = (1); i <= (int)(n); ++i) scanf("%lld", &a[i]); long long ans = 0; if (!a[1]) { a[1] = 1; memcpy(temp, a, sizeof(temp)); ans = solve() + 1; memcpy(a, temp, sizeof(a)); a[1] = -1; ans = min(ans, solve() + 1); } else ans = solve(); printf("%lld\n", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { freopen("testcase", "r", stdin); int N, temp; vector<int> a; scanf("%d", &N); int start = 0; bool v = false; for (int i = 0; i < N; i++) { scanf("%d", &temp); if (temp == 0) { if (!v) { start += 1; } } else if (!v) v = true; a.push_back(temp); } long long sum = 0, cnt = 0; if (start != 0) { cnt = 2 * (start - 1) + 1; if (a[start] > 0) { if (a[start] > 1) { sum = a[start] - 1; } else { sum = 1; cnt += 1; } } else { if (a[start] < -1) { sum = a[start] + 1; } else { sum = -1; cnt += 1; } } } else { sum = a[start]; } start++; for (size_t i = start; i != a.size(); i++) { if (sum + a[i] >= 0 && sum > 0) { cnt += sum + a[i] + 1; sum = -1; } else if (sum + a[i] <= 0 && sum < 0) { cnt += 1 - sum - a[i]; sum = 1; } else { sum += a[i]; } } if (sum == 0) cnt += 1; printf("%ll\n", cnt); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < (int)(n); i++) cin >> a[i]; long long prevArraySum = a[0]; long long currentArraySum = a[0]; long long res = 0; if (a[0] == 0) { res = 1; prevArraySum = -1; currentArraySum = -1; for (int i = (1); i < (n); ++i) { if (prevArraySum > 0) { currentArraySum = prevArraySum + a[i]; if (currentArraySum >= 0) { res += abs(-1 - currentArraySum); prevArraySum = -1; } else { prevArraySum = currentArraySum; } } else { currentArraySum = prevArraySum + a[i]; if (currentArraySum <= 0) { res += abs(1 - currentArraySum); prevArraySum = 1; } else { prevArraySum = currentArraySum; } } } long long res1 = res; res = 1; prevArraySum = 1; currentArraySum = 1; for (int i = (1); i < (n); ++i) { if (prevArraySum > 0) { currentArraySum = prevArraySum + a[i]; if (currentArraySum >= 0) { res += abs(-1 - currentArraySum); prevArraySum = -1; } } else { currentArraySum = prevArraySum + a[i]; if (currentArraySum <= 0) { res += abs(1 - currentArraySum); prevArraySum = 1; } else { prevArraySum = currentArraySum; } } } res = min(res, res1); } else { for (int i = (1); i < (n); ++i) { if (prevArraySum > 0) { currentArraySum = prevArraySum + a[i]; if (currentArraySum >= 0) { res += abs(-1 - currentArraySum); prevArraySum = -1; } else { prevArraySum = currentArraySum; } } else { currentArraySum = prevArraySum + a[i]; if (currentArraySum <= 0) { res += abs(1 - currentArraySum); prevArraySum = 1; } else { prevArraySum = currentArraySum; } } } } cout << res << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void Main() { long long N; cin >> N; vector<long long> A(N); for (int i = 0; i < N; i++) cin >> A[i]; long long res = (1 << 30); int sum, tmp; sum = tmp = 0; for (int i = 0; i < N; i++) { sum += A[i]; if (i % 2) { if (sum >= 0) { tmp += abs(sum) + 1; sum = -1; } } else { if (sum <= 0) { tmp += abs(sum) + 1; sum = 1; } } } if (tmp < res) res = tmp; sum = tmp = 0; for (int i = 0; i < N; i++) { sum += A[i]; if (i % 2) { if (sum <= 0) { tmp += abs(sum) + 1; sum = 1; } } else { if (sum >= 0) { tmp += abs(sum) + 1; sum = -1; } } } if (tmp < res) res = tmp; cout << res << endl; } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); Main(); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) ans=10**10 now=cnt=0 for i in range(len(a)): now+=a[i] if i%2==0: if now>=0: cnt+=now+1 now=-1 else: if now<=0: cnt+=1-now now=1 ans=min(ans,cnt) now=cnt=0 for i in range(len(a)): now+=a[i] if i%2==1: if now>=0: cnt+=(now+1) now=-1 else: if now<=0: cnt+=1-now now=1 ans=min(ans,cnt) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> long long int n; long long int count(long long int a0) { long long int a, i, S[2], C[2] = {}; S[0] = a0; S[1] = a0; for (i = 1; i < n; i++) { scanf("%lld", &a); S[0] += a; S[1] += a; if (i % 2 == 1) { if (S[0] <= 0) { C[0] += -1 * S[0] + 1; S[0] = 1; } if (S[1] >= 0) { C[1] += S[1] + 1; S[1] = -1; } } else { if (S[0] >= 0) { C[0] += S[0] + 1; S[0] = -1; } if (S[1] <= 0) { C[1] += -1 * S[1] + 1; S[1] = 1; } } } return C[0] < C[1] ? C[0] : C[1]; } int main() { long long int a, ans; scanf("%lld %lld", &n, &a); if (a == 0) { ans = count(1) < count(-1) ? count(1) + 1 : count(-1) + 1; } else { ans = count(a); } printf("%lld\n", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; int a[100000]; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } int sum1, sum2; int cnt = 0; sum1 = 0; for (int i = 0; i < n; i++) { sum1 += a[i]; if (sum1 == 0) { if (a[i + 1] >= 0) a[i]++; else a[i]--; cnt++; } if (i < n - 1) { sum2 = sum1 + a[i + 1]; if (sum1 * sum2 > 0) { int a_p = a[i + 1]; if (sum2 > 0) a[i + 1] = -sum1 - 1; else if (sum2 < 0) a[i + 1] = -sum1 + 1; cnt += abs(a[i + 1] - a_p); } } } cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const long long maxLL = (long long)1 << 62; long long a[100001] = {}; long long s[100001] = {}; int main() { long long n; cin >> n; long long cnt = 0; for (long long i = 1; i <= n; i++) { cin >> a[i]; s[i] = s[i - 1] + a[i]; if (i > 1) { if (s[i] == 0) { s[i] = s[i - 1] * -1; cnt++; } else if (i > 1 && s[i - 1] * s[i] > 0) { cnt += abs(s[i]) + 1; if (s[i] > 0) s[i] -= cnt; else if (s[i] < 0) s[i] += cnt; } } } cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) num_list = list(map(int, input().split())) count = 0 sum_ = num_list[0] if sum_ > 0: for i in range(1, n): sum_ += num_list[i] if i%2 == 0: if sum_ <= 0: count += abs(sum_) + 1 sum_ = 1 else: if sum_ >= 0: count += abs(sum_) + 1 sum_ = -1 print(count) elif sum_ < 0: for i in range(1, n): sum_ += num_list[i] if i%2 == 1: if sum_ <= 0: count += abs(sum_) + 1 sum_ = 1 else: if sum_ >= 0: count += abs(sum_) + 1 sum_ = -1 print(count) else: sum_ = 1 for i in range(1, n): sum_ += num_list[i] if i%2 == 0: if sum_ <= 0: count += abs(sum_) + 1 sum_ = 1 else: if sum_ >= 0: count += abs(sum_) + 1 sum_ = -1 count1 = count sum_ = -1 for i in range(1, n): sum_ += num_list[i] if i%2 == 1: if sum_ <= 0: count += abs(sum_) + 1 sum_ = 1 else: if sum_ >= 0: count += abs(sum_) + 1 sum_ = -1 count2 = count print(min(count1+1, count2+1))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) S = 0 C = 0 S = a[0] if S > 0: pm = 1 else: pm = 0 for i in range(1, n): S += a[i] if pm == 1 and S >= 0: C += S + 1 S -= S + 1 elif pm == 0 and S <= 0: C += -S + 1 S += -S + 1 pm = 1 - pm print(C)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) list_a = list(map(int,input().split())) i = 0 k = 0 count = 0 ans = list_a[0] if list_a[0] > 0: for i in range(0,n): ans += list_a[i] if ans / ((-1) ** i) <= 0: count += abs(ans - (-1) ** i) ans = (-1) ** i else: for i in range(0,n): ans += list_a[i] if ans / ((-1) ** (i+1)) <= 0: count += abs(ans - (-1) ** (i+1)) ans = (-1) ** (i+1) count1 = count count = 0 if list_a[0] <= 0: for i in range(0,n): ans += list_a[i] if ans / ((-1) ** i) <= 0: count += abs(ans - (-1) ** i) ans = (-1) ** i else: for i in range(0,n): ans += list_a[i] if ans / ((-1) ** (i+1)) <= 0: count += abs(ans - (-1) ** (i+1)) ans = (-1) ** (i+1) count2 = count print(min(count1, count2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
#!usr/bin/env python3 from collections import defaultdict from collections import deque from heapq import heappush, heappop import sys import math import bisect import random import itertools sys.setrecursionlimit(10**5) stdin = sys.stdin bisect_left = bisect.bisect_left bisect_right = bisect.bisect_right def LI(): return list(map(int, stdin.readline().split())) def LF(): return list(map(float, stdin.readline().split())) def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split())) def II(): return int(stdin.readline()) def IF(): return float(stdin.readline()) def LS(): return list(map(list, stdin.readline().split())) def S(): return list(stdin.readline().rstrip()) def IR(n): return [II() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] def FR(n): return [IF() for _ in range(n)] def LFR(n): return [LI() for _ in range(n)] def LIR_(n): return [LI_() for _ in range(n)] def SR(n): return [S() for _ in range(n)] def LSR(n): return [LS() for _ in range(n)] mod = 1000000007 inf = float('INF') #A def A(): a = input().split() a = list(map(lambda x: x.capitalize(), a)) a,b,c = a print(a[0]+b[0]+c[0]) return #B def B(): a = II() b = II() if a > b: print("GREATER") if a < b: print("LESS") if a == b: print("EQUAL") return #C def C(): II() a = LI() def f(suma, b): for i in a[1:]: if suma * (suma + i) < 0: suma += i continue b += abs(suma + i) + 1 suma = -1 * (suma >= 0) or 1 return b if a[0] == 0: ans = min(f(1, 1), f(-1, 1)) else: ans = min(f(a[0], 0), f(-a[0], 2 * abs(a[0]))) print(ans) return #D def D(): s = S() for i in range(len(s) - 1): if s[i] == s[i+1]: print(i + 1, i + 2) return for i in range(len(s) - 2): if s[i] == s[i + 2]: print(i + 1, i + 3) return print(-1, -1) return #Solve if __name__ == '__main__': C()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using unsi = unsigned; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using pii = pair<int, int>; using db = double; using plex = complex<double>; using vs = vector<string>; template <class T> inline bool amax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool amin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); }; } aaaaaaa; const int INF = 1001001001; const ll LINF = 1001001001001001001ll; const int MOD = 1e9 + 7; const db EPS = 1e-9; const int dx[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy[] = {0, 1, 1, 1, 0, -1, -1, -1}; signed main() { int n; cin >> n; int odd{}; int ans{}; int even{}; vector<int> a(n); for (auto i = 0; i != n; ++i) { cin >> a.at(i); } if (a[0] < 0) { for (auto i = 0; odd < n; ++i) { odd = 2 * i + 1; while (a[odd] <= 0) { ++a[odd]; ++ans; } } for (auto i = 1; even < n; ++i) { even = 2 * i; while (a[even] >= 0) { --a[even]; ++ans; } } } else if (a[0] >= 0) { if (a[0] = 0) ++a[0]; for (auto i = 0; odd < n; ++i) { odd = 2 * i + 1; while (a[odd] >= 0) { --a[odd]; ++ans; } } for (auto i = 1; even < n; ++i) { even = 2 * i; while (a[even] <= 0) { ++a[even]; ++ans; } } } cout << ans; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = list(map(int, input().split())) ans = 0 sum = A[0] if sum == 0: ans = 1 for i in range(1, len(A)): a = A[i] if a != 0: if i % 2 == 0: if a > 0: sum = 1 else: sum = -1 else: if a > 0: sum = -1 else: sum = 1 break for i in range(1, len(A)): a = A[i] prev_sum = sum sum += a if sum * prev_sum >= 0: ans += abs(prev_sum+a)+1 if prev_sum > 0: sum = -1 else: sum = 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
java
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] inputString = br.readLine().split(" "); int[] input = new int[n]; for(int i = 0 ; i < n ; i++) { input[i] = Integer.parseInt(inputString[i]); } int result = -1, keyValue = result; while(keyValue == -1){ keyValue = result; result = 0; int count = 0; for(int i = 0 ; i < n ; i++){ if(i % 2 == 0 && (count + input[i]) * Math.signum(keyValue) >= 0){ result += Math.abs(count + input[i]) + 1; count = (int)Math.signum(keyValue) * -1; }else if(i % 2 != 0 && (count + input[i]) * Math.signum(keyValue) <= 0){ result += Math.abs(count + input[i]) + 1; count = (int)Math.signum(keyValue); }else{ count += input[i]; } } result = (keyValue != -1)? Math.min(result, keyValue): result; } System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const long long LINF = 1e18; const double EPS = 1e-9; const double PI = M_PI; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } void yes() { cout << "Yes" << endl; } void no() { cout << "No" << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<long long> a(n); for (long long(i) = 0; (i) < (long long)(n); i++) { cin >> a[i]; } vector<long long> sum(n); sum[0] = a[0]; long long ans = 0; if (sum[0] == 0) { ans++; } for (long long(i) = 1; (i) < (long long)n; i++) { if (sum[i - 1] == 0 && a[i] == 0) { ans++; } sum[i] = sum[i - 1] + a[i]; if (sum[i] * sum[i - 1] < 0) { continue; } else if (sum[i] * sum[i - 1] > 0) { if (sum[i] > 0 && sum[i - 1] > 0) { ans += sum[i] + 1; sum[i] = -1; } else { ans += -sum[i] + 1; sum[i] = 1; } } else { ans++; if (sum[i - 1] > 0) { sum[i] = -1; } else { sum[i] = 1; } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; 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; LL 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; 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, ans = 0; cin >> n; cin >> a; int sum = a; for (int i = (0); i < (n - 1); ++i) { cin >> a; if (sum > 0) { sum += a; if (sum >= 0) { ans += (sum + 1); sum = -1; } } else { sum += a; if (sum <= 0) { ans += (-sum + 1); 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 <stdio.h> #include <iostream> #include <vector> using namespace std; int main() { int N; cin >> N; std::vector<int> odd, even; for (int i = 0; i < N; i++) { int temp; cin >> temp; odd.push_back(temp); } even = odd; int oddNum = 0, evenNum = 0; while(odd[0] <= 0) odd[0]++, oddNum++; while(even[0] >= 0) even[0]--, evenNum++; int sum = odd[0]; int sum2 = even[0]; for(int i = 1; i < N; i += 2) { while(sum + odd[i] >= 0) odd[i]--, oddNum++; while(sum + even[i] <= 0) even[i]++, evenNum++; sum += odd[i]; sum2 += even[i]; if(i != N - 1) { while(sum + odd[i + 1] <= 0) odd[i + 1]++, oddNum++; while(sum + even[i + 1] >= 0) even[i + 1]--, evenNum++; sum += odd[i + 1]; sum += even[i + 1]; } } printf("%d\n", oddNum < evenNum ? oddNum : evenNum); 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
# coding: utf-8 # Here your code N = int(input()) a = [int(i) for i in input().split()] result_1 = 0 before_sum =a[0] if a[0] == 0: before_sum = 1 result_1 += 1 after_sum =before_sum for i in range(1,N): before_sum = after_sum after_sum = before_sum + a[i] if before_sum * after_sum > 0: if after_sum < 0: result_1 += 1 - after_sum after_sum = 1 elif after_sum > 0: result_1 += 1 + after_sum after_sum = -1 elif before_sum * after_sum == 0: result_1 += abs(before_sum) + 1 if before_sum < 0: after_sum = 1 else: after_sum = -1 if a[0] < 0: before_sum = 1 elif a[0] >= 0: before_sum = -1 after_sum =before_sum result_2 = 1 + abs(before_sum) for i in range(1,N): before_sum = after_sum after_sum = before_sum + a[i] if before_sum * after_sum > 0: if after_sum < 0: result_2 += 1 - after_sum after_sum = 1 elif after_sum > 0: result_2 += 1 + after_sum after_sum = -1 elif before_sum * after_sum == 0: result_2 += abs(before_sum) + 1 if before_sum < 0: after_sum = 1 else: after_sum = -1 print(min(result_1,result_2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = input() a = [int(i) for i in input.split()] X = 0 ans = 0 for i in a: X += i if X > 0: b = -1 - X ans += b - a(i+1) else X < 0: b = 1 - X ans += b - a(i+1) return ans print (ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, sum; vector<int> a(100000); int ans1 = 0, ans2 = 0; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; if (a[0] < 1) { ans1 += abs(1 - a[0]); sum = 1; } else sum = a[0]; for (int i = 1; i < n; i++) { if (i % 2 != 0 && sum + a[i] >= 0) { ans1 += abs(sum * (-1) - 1 - a[i]); sum = -1; } else if (i % 2 == 0 && sum + a[i] <= 0) { ans1 += abs(sum * (-1) + 1 - a[i]); sum = 1; } else sum += a[i]; } if (a[0] > -1) { ans2 += abs(-1 - a[0]); sum = -1; } else sum = a[0]; for (int i = 1; i < n; i++) { if (i % 2 == 0 && sum + a[i] >= 0) { ans2 += abs(sum * (-1) - 1 - a[i]); sum = -1; } else if (i % 2 != 0 && sum + a[i] <= 0) { ans2 += abs(sum * (-1) + 1 - a[i]); sum = 1; } else sum += a[i]; } cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) A = [n for n in a] cnt1 = 0 cnt2 = 0 S = 0 if a[0] > 0: for i in range(n-1): S += a[i] if a[i] > 0: while S + a[i+1] >= 0: a[i+1] -= 1 cnt1 += 1 elif a[i] < 0: while S + a[i+1] <= 0: a[i+1] += 1 cnt1 += 1 A[0] = -1 cnt2 = a[0] + 1 for i in range(n-1): S += A[i] if A[i] > 0: while S + A[i+1] >= 0: A[i+1] -= 1 cnt2 += 1 elif A[i] < 0: while S + A[i+1] <= 0: A[i+1] += 1 cnt2 += 1 elif a[0] < 0: for i in range(n-1): S += a[i] if a[i] > 0: while S + a[i+1] >= 0: a[i+1] -= 1 cnt1 += 1 elif a[i] < 0: while S + a[i+1] <= 0: a[i+1] += 1 cnt1 += 1 A[0] = 1 cnt2 = -a[0] + 1 for i in range(n-1): S += A[i] if A[i] > 0: while S + A[i+1] >= 0: A[i+1] -= 1 cnt2 += 1 elif A[i] < 0: while S + A[i+1] <= 0: A[i+1] += 1 cnt2 += 1 else: a[0] = 1 cnt1 = 1 for i in range(n-1): S += a[i] if a[i] > 0: while S + a[i+1] >= 0: a[i+1] -= 1 cnt1 += 1 elif a[i] < 0: while S + a[i+1] <= 0: a[i+1] += 1 cnt1 += 1 A[0] = -1 cnt2 = 1 for i in range(n-1): S += A[i] if A[i] > 0: while S + A[i+1] >= 0: A[i+1] -= 1 cnt2 += 1 elif A[i] < 0: while S + A[i+1] <= 0: A[i+1] += 1 cnt2 += 1 cnt = cnt2 if cnt1 >= cnt2 else cnt1 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())) def sol(S): ret = 0 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 S >= 0 else b + 1 ret += abs(b - a) S += b return ret if A[0] == 0: ans = min(sol(1), sol(-1)) + 1 else: ans = min(sol(A[0]), sol(-A[0] // A[0]) + abs(A[0]) + 1) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = input() num_list = list(map(int,input().split())) count = 0 numsum = 0 for i in range(len(num_list)): if i == 0: numsum += num_list[0] else: numsum_pre = numsum numsum += num_list[i] # print(numsum) if numsum == 0: count += 1 if numsum_pre < 0: numsum += 1 else: numsum-= 1 if numsum_pre*numsum > 0: if numsum_pre < 0: count += abs(numsum) +1 numsum += abs(numsum) +1 # print('count{}'.format(abs(numsum-numsum_pre) +1)) else: # print(numsum) # print('count{}'.format(abs(numsum-numsum_pre) +1)) count += abs(numsum) +1 numsum-= (abs(numsum) +1) print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
l = int(input()) n = [int(i) for i in input().split()] count = 0 nsum = n[0] for i in range(1, l): tmp = 0 tmp_1 = n[i-1] while(True): if (tmp_1 * tmp >= 0) or (nsum * (nsum + tmp) >= 0): if tmp_1 < 0: count += 1 tmp += 1 else: count += 1 tmp -= 1 else: n[i] = tmp nsum += tmp break print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } long long int count = 0; if (a[0] == 0) { for (int i = 0; i < n; ++i) { if (a[i] > 0) { a[0] = -1; ++count; break; } else if (a[i] < 0) { a[0] = 1; ++count; break; } } } long long int cal = a[0]; if (a[0] == 0) { cout << n * (n + 1) / 2; } else { for (int i = 1; i < n; ++i) { if (cal + a[i] == 0) { if (cal < 0) { ++count; ++a[i]; cal = 1; } else { ++count; --a[i]; cal = -1; } } else if (cal < 0 && cal + a[i] < 0) { count += -(cal + a[i] - 1); a[i] += -(cal + a[i] - 1); } else if (cal > 0 && cal + a[i] > 0) { count += (cal + a[i] + 1); a[i] -= (cal + a[i] + 1); } cal += a[i]; } } cout << count << "\n"; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; long long a[100005], dp[100005]; cin >> n; long long sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; dp[i] = sum; } long long diff = 0, ans = 0; if (dp[0] == 0) { if (dp[1] < 0) diff--, ans++; else diff++, ans++; } for (int i = 1; i < n; i++) { if (dp[i] + diff == 0) { if (dp[i - 1] + diff < 0) diff++, ans++; if (dp[i - 1] + diff > 0) diff--, ans++; continue; } if ((dp[i - 1] + diff) / llabs(dp[i - 1] + diff) == (dp[i] + diff) / llabs(dp[i] + diff)) { if (dp[i] + diff >= 0) { ans += llabs(dp[i] + diff) + 1; diff -= llabs(dp[i] + diff) + 1; } else { ans += llabs(dp[i] + diff) + 1; diff += llabs(dp[i] + diff) + 1; } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) s = a[0] count = 0 total_delta = 0 delta = 0 if a[0] == 0: i = 0 while i < len(a): if a[i] != 0: break i += 1 if i == len(a): delta = 1 total_delta += delta count += 1 else: delta = -1 * (a[i] // abs(a[i])) * ((1, -1)[(i + 1) % 2]) total_delta += delta count += 1 for i in range(1, n): print('loop : ', i ) print(s) sign = (s + total_delta) // abs(s + total_delta) if (s + a[i] + total_delta) * sign > 0 : delta = (sign * -1) - (s + a[i] + total_delta) total_delta += delta count += abs(delta) elif (s + a[i] + total_delta) == 0: total_delta += sign * -1 count += 1 s += a[i] print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; int ans = 0, ans2 = 0, sum = a[0]; for (int i = 0; i < n; i++) { while (sum <= 0 && i % 2 == 0) { sum++; a[i]++; ans++; if (sum == 1) break; } while (sum >= 0 && i % 2 == 1) { sum--; a[i]--; ans++; if (sum == -1) break; } if (i == n) break; sum += a[i + 1]; } for (int i = 0; i < n; i++) { while (sum <= 0 && i % 2 == 1) { sum++; a[i]++; ans2++; if (sum == 1) break; } while (sum >= 0 && i % 2 == 0) { sum--; a[i]--; ans2++; if (sum == -1) break; } if (i == n) break; sum += a[i + 1]; } cout << min(ans, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import itertools def sign(num): if num < 0: return -1 elif num > 0: return 1 else: return 0 N = input() a_i = list(map(int, input().split())) a_sum = [a_i[0]] for i, a in enumerate(a_i[1:]): i += 1 a_sum.append(a_sum[-1]+a) signs = [0, 0] changes = 0 for i, sum_i in enumerate(a_sum): if sum_i != 0: signs[i%2] = sign(sum_i) signs[i%2+1] = -sign(sum_i) break if signs == [0, 0]: signs = [1, -1] for i, sum_i in enumerate(a_sum): if i == 0: signs = [sign(sum_i), -sign(sum_i)] elif sign(sum_i) != signs[i%2]: a_sum[i:] = [num + (abs(sum_i) + 1) * signs[i%2] for num in a_sum[i:]] changes += abs(sum_i) + 1 # print(a_sum) print(changes)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, ansa = 0, ansb = 0, suma = 0, sumb = 0; cin >> n; bool plus = true; for (int i = 0; i < (n); i++) { int a, b; cin >> b; a = b; while (plus && suma + a <= 0) { a++; ansa++; } while (!plus && suma + a >= 0) { a--; ansa++; } while (plus && sumb + b >= 0) { b++; ansb++; } while (!plus && sumb + b <= 0) { b--; ansb++; } suma += a; sumb += b; plus = !plus; } cout << min(ansa, ansb) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String args[]){ Scanner scanner = new Scanner(System.in); int count = 0; int l[] = new int[scanner.nextInt()]; for (int i = 0;i < l.length;++i){ l[i] = scanner.nextInt(); } for (int i = 0;i < l.length;++i){ int p = 0; int q = 0; for (int j = 0;j <= i;++j){ if(j != i) { p += l[j]; } q += l[j]; } // System.out.println(q + ":" + p); if(q == 0||(q < 0&&p < 0)||(q > 0&&p > 0)){ int c = 1 + ((p > 0) ? 1 : -1) * q; count += c; l[i] += ((p > 0) ? -1 : 1) * c; // System.out.println("adf" + i + ":" + c); } } System.out.println(count); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; const long long MOD = 1e9 + 7; const int INF = 1e9 + 7; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<ll> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; ll cnt = 0; ll sub_sum = a[0]; if (sub_sum == 0) sub_sum = -1; for (int i = 1; i < n; ++i) { ll sum = sub_sum + a[i]; ll sgn = sub_sum / abs(sub_sum) * sum; if (sgn >= 0) { ll b = -1 * sub_sum / abs(sub_sum); sum = b; cnt += abs(b - sub_sum - a[i]); } sub_sum = sum; } ll ans = cnt; cnt = abs(a[0]) + 1; sub_sum = (a[0] > 0 ? -1 : 1); for (int i = 1; i < n; ++i) { ll sum = sub_sum + a[i]; ll sgn = sub_sum / abs(sub_sum) * sum; if (sgn >= 0) { ll b = -1 * sub_sum / abs(sub_sum); sum = b; cnt += abs(b - sub_sum - a[i]); } sub_sum = sum; } ans = min(ans, cnt); cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) A=list(map(int,input().split())) W=[] wa=0 for i in range(n): wa=A[i]+wa W.append(wa) counter=0 for i in range(n): if i==n-1: break elif W[i]<0 and W[i+1]<0: counter=abs(W[i])-abs(A[i+1])+1+counter elif W[i]>0 and W[i+1]>0: counter=abs(W[i])+1+counter-abs(A[i+1]) if A[n-1]==0: counter=counter+abs(A[n-2]) print(counter)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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 = [i for i in map(int,input().split())] ruiseki = [0] * N ans = 2 ** 30 + 1 for j in range(2): cnt= 0 ruiseki[0] = A[0] if j == 0: if A[0] <= 0: ruiseki[0] = 1 cnt += 1 - A[0] else: if A[0] >= 0: ruiseki[0] = -1 cnt += 1 + A[0] for i in range(1,N): ruiseki[i] = ruiseki[i-1]+A[i] totalp = 0 if ruiseki[i] * ruiseki[i-1] >0: totalp= (int(ruiseki[i])>0)*(-1)*(1 + ruiseki[i]) + (int(ruiseki[i])<0) * (1 - ruiseki[i]) elif ruiseki[i] == 0: if ruiseki[i-1] > 0: totalp = -1 else: totalp = 1 ruiseki[i] += totalp cnt += abs(totalp) ans = min(ans,cnt) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> int main(void) { int n; long int a[100000] = {0}, fugo, dif, ans = 0, min = 10000000; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%ld", &a[i]); } for (fugo = 0; fugo <= 1; fugo++) { ans = 0; int b[100001] = {0}; for (int i = 0; i < n; i++) { dif = 0; b[i + 1] = b[i] + a[i]; if ((i + 1) % 2 == fugo) { if (b[i + 1] <= 0) { dif += -1 - b[i]; b[i] += dif; dif += 1 - b[i + 1] - dif; b[i + 1] += dif; ans += dif; } } else { if (b[i + 1] >= 0) { dif += b[i] - 1; b[i] -= dif; dif += b[i + 1] + 1 - dif; b[i + 1] -= dif; ans += dif; } } } if (min > ans) min = ans; } printf("%d\n", min); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys input = sys.stdin.readline def main(): n = int(input()) a_list = list(map(int, input().split())) a_sum = a_list[0] if a_list[0] > 0: sign = "plus" else: sign = "minus" ans1 = 0 for i in range(1, n): if sign == "plus": sign = "minus" if a_sum + a_list[i] == 0: ans1 += 1 a_sum = -1 elif a_sum + a_list[i] > 0: ans1 += a_sum + 1 + a_list[i] a_sum = -1 else: a_sum += a_list[i] elif sign == "minus": sign = "plus" if a_sum + a_list[i] == 0: ans1 += 1 a_sum = 1 elif a_sum + a_list[i] < 0: ans1 += -1 * a_sum + 1 + -1 * a_list[i] a_sum = 1 else: a_sum += a_list[i] a_sum = 0 if a_list[0] > 0: sign = "plus" else: sign = "minus" ans2 = 0 for i in range(0, n): if sign == "plus": sign = "minus" if a_sum + a_list[i] == 0: ans2 += 1 a_sum = -1 elif a_sum + a_list[i] > 0: ans2 += a_sum + 1 + a_list[i] a_sum = -1 else: a_sum += a_list[i] elif sign == "minus": sign = "plus" if a_sum + a_list[i] == 0: ans2 += 1 a_sum = 1 elif a_sum + a_list[i] < 0: ans2 += -1 * a_sum + 1 + -1 * a_list[i] a_sum = 1 else: a_sum += a_list[i] print(min(ans1, ans2)) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) a_1 = a ans = 0 ans_2 = 0 o = 0 for i in range(n): if i == 0: if a[i] == 0: f = "+" a[i] = 1 elif a[0] > 0: f = "+" elif a[0] < 0: f = "-" else: o += a[i-1] if f == "+": if a[i] + o > 0: c = -1 - o ans += abs(c - a[i]) a[i] = c f = "-" else: if a[i] + o == 0: a[i] -= 1 ans += 1 f = "-" elif f == "-": if a[i] + o < 0: c = 1 - o ans += abs(c - a[i]) a[i] = c f = "+" else: if a[i] + o == 0: a[i] += 1 ans += 1 f = "+" o = 0 a = a_1 for i in range(n): if i == 0: if a[i] == 0: f = "+" a[i] = 1 elif a[0] > 0: f = "-" ans_2 += abs(-1 - a[0]) a[i] = -1 elif a[0] < 0: ans_2 += abs(1 - a[0]) a[i] = 1 f = "+" else: o += a[i-1] if f == "+": if a[i] + o > 0: c = -1 - o ans_2 += abs(c - a[i]) a[i] = c f = "-" else: if a[i] + o == 0: a[i] -= 1 ans_2 += 1 f = "-" elif f == "-": if a[i] + o < 0: c = 1 - o ans_2 += abs(c - a[i]) a[i] = c f = "+" else: if a[i] + o == 0: a[i] += 1 ans_2 += 1 f = "+" #print(a) print(min(ans,ans_2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } long long sum_ = a[0]; long long count = 0; if (a[0] > 0) { for (int i = 1; i < n; ++i) { if (i % 2 == 1) { if (sum_ + a[i] >= 0) { long long r = -(sum_ + a[i]) - 1; count += abs(r); a[i] += r; } } else { if (sum_ + a[i] <= 0) { long long r = -(sum_ + a[i]) + 1; count += abs(r); a[i] += r; } } sum_ += a[i]; } } else { for (int i = 1; i < n; ++i) { if (i % 2 == 1) { if (sum_ + a[i] <= 0) { long long r = -(sum_ + a[i]) + 1; count += abs(r); a[i] += r; } } else { if (sum_ + a[i] >= 0) { long long r = -(sum_ + a[i]) - 1; count += abs(r); a[i] += r; } } sum_ += a[i]; } } cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def op(pos, n, a): if pos: S = 1 if a[0] == 0 else a[0] else: S = -1 if a[0] == 0 else a[0] count = 1 if S == 0 else 0 for i in a[1:]: if S * (S + i) > 0: count += abs(S + i) + 1 S = -1 if S > 0 else 1 elif S + i == 0: count += 1 S = -1 if S > 0 else 1 else: S += i return count def main(): n = int(input()) a = list(map(int, input().split())) c1 = op(True, n, a) c2 = op(False, n, a) print(min(c1, c2)) if __name__ == "__main__": main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) import numpy as np ans = 0 sum0 = a[0] sum1 = a[0] for i in range(1, n): sum1 += a[i] if np.sign(sum0) != np.sign(sum1) and sum1 != 0: #合計の符号が逆となっており、0でない sum0 = sum1 pass elif sum1 == 0: #合計が0になった場合は、符号が逆になるよう1か-1を足す sum1 -= 1 * np.sign(sum0) ans += 1 sum0 = sum1 elif np.sign(sum0) == np.sign(sum1): #符号が同じ場合は、+1か-1になるまで足す if np.sign(sum1) == 1: #sum0もsum1もプラスの場合 ans = ans + sum0 + a[i] + 1 sum1 = -1 else: #sum0もsum1もマイナスの場合 ans = ans + abs(sum0 + a[i]) + 1 sum1 = 1 sum0 = sum1 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
java
import java.util.*; import static java.lang.Integer.*; import static java.lang.Long.*; import static java.lang.Math.*; import static java.lang.System.*; public class Main { public static void main(String[] args) { int i,j; Scanner sc = new Scanner(in); int n = parseInt(sc.next()); long[] a = new long[n]; for(i=0;i<n;i++) { a[i] = parseLong(sc.next()); } sc.close(); long cnt=0; int sign0 = signum(a[0]); long sum = a[0]; for (i = 1; i < a.length; i++) { sum += a[i]; int sign1 = signum(sum); if(sign1 == 0 ||sign0 == sign1) { long dif = (long)(-sign0) - sum; sum += dif; cnt += abs(dif); sign1 = -sign1; } sign0 = sign1; } if(sum==0)cnt++; out.println(cnt); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) cnt=0 for i in range(1,n): # 条件満たすまでループ for _ in range(3): #print(a) now_tmp = sum(a[:i]) next_tmp = sum(a[:i+1]) #print(i, now_tmp, next_tmp) # 符号が逆転していればOK かつ 現在までの総和が0でない # 異なる符号を掛けるとマイナスになる if now_tmp * next_tmp <0 and now_tmp !=0: break else: # 現在の合計がマイナスの場合 if now_tmp < 0: a[i] += -next_tmp+1 cnt +=abs(next_tmp+1) # 現在の合計がプラスの場合 elif now_tmp > 0 : a[i] += -next_tmp-1 cnt +=abs(next_tmp+1) # 現在の合計が0の場合 elif now_tmp == 0 : # 1個前がプラスの場合、 if sum(a[:i-1]) > 0: a[i] += -next_tmp+1 cnt +=abs(next_tmp+1) # 1個前がマイナスの場合 else: a[i] += -next_tmp+1 cnt +=abs(next_tmp+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
python3
n = int(input()) A = [int(i) for i in input().split()] def f(A, op, acc = 0, cnt = 0): for i in range(n): acc += A[i] if i % 2 == 0: if op * acc <= 0: cnt += - op * acc + 1 acc = 1 if i % 2 == 1: if op * acc >= 0: cnt += op * acc + 1 acc = -1 if acc == 0: cnt += 1 return cnt print(min(f(A, 1), f(A, -1)))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) b = [int(x) for x in input().split()] a = list() temp = 0 count1 = 0 count2 = 0 a = b.copy() if a[0] == 0: a[0] = 1 count1 = 1 sum = a[0] for i in range(1, n): if abs(a[i]) <= abs(sum) or a[i] * sum >= 0: count1 += abs(temp - a[i]) if sum > 0: temp = -1 * abs(sum) - 1 else: temp = abs(sum) + 1 a[i] = temp sum += a[i] a = b.copy() count2 = abs(a[0]) + 1 if a[0] >= 0: a[0] = -1 else: a[0] = 1 sum = a[0] for i in range(1, n): if abs(a[i]) <= abs(sum) or a[i] * sum >= 0: count2 += abs(temp - a[i]) if sum > 0: temp = -1 * abs(sum) - 1 else: temp = abs(sum) + 1 a[i] = temp sum += a[i] print(min(count1, count2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; long long a[100001]; long long sumo[100001]; long long sume[100001]; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i + 1]; } int anso = 0; int anse = 0; sumo[0] = 0; sume[0] = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 0) { if (sume[i - 1] + a[i] > 0) sume[i] = sume[i - 1] + a[i]; if (sume[i - 1] + a[i] <= 0) { sume[i] = 1; anse += (1 - (sume[i - 1] + a[i])); } if (sumo[i - 1] + a[i] < 0) sumo[i] = sumo[i - 1] + a[i]; if (sumo[i - 1] + a[i] >= 0) { sumo[i] = -1; anso += sumo[i - 1] + a[i] + 1; } } else if (i % 2 == 1) { if (sumo[i - 1] + a[i] > 0) sumo[i] = sumo[i - 1] + a[i]; if (sumo[i - 1] + a[i] <= 0) { sumo[i] = 1; anso += (1 - (sumo[i - 1] + a[i])); } if (sume[i - 1] + a[i] < 0) sume[i] = sume[i - 1] + a[i]; if (sume[i - 1] + a[i] >= 0) { sume[i] = -1; anse += (sume[i - 1] + a[i] + 1); } } } int ans; ans = min(anso, anse); 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> int body(std::vector<int>& a) { int ans = 0; std::vector<int> s(a.size()); s.at(0) = a.at(0); for (unsigned int i = 1; i < a.size(); i++) { s.at(i) = s.at(i - 1) + a.at(i); } for (unsigned int i = 1; i < s.size(); i++) { if (s.at(i - 1) > 0 && s.at(i) >= 0) { int n = s.at(i) + 1; ans += n; for (unsigned int j = i; j < s.size(); j++) { s.at(j) -= n; } } else if (s.at(i - 1) < 0 && s.at(i) <= 0) { int n = -1 * s.at(i) + 1; ans += n; for (unsigned int j = i; j < s.size(); j++) { s.at(j) += n; } } } return ans; } int main(int argc, char** argv) { int n; std::cin >> n; std::vector<int> a(n); for (int i = 0; i < n; i++) { std::cin >> a.at(i); } int ans; if (a.at(0) != 0) { ans = body(a); } else { a.at(0) = -1; int ans_a = body(a); a.at(0) = 1; int ans_b = body(a); ans = std::min(ans_a, ans_b); } std::cout << ans << std::endl; }