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
UNKNOWN
open Batteries let () = let n = Scanf.scanf "%d " (fun a -> a) in let a_lst = Array.to_list @@ Array.init n (fun _ -> Scanf.scanf "%d " (fun a -> a)) in let rev_cumsum l = let rec aux last res = function | [] -> res | hd :: tl -> let last = last + hd in aux last (last::res) tl in aux 0 [] l in let l = List.rev (rev_cumsum a_lst) in let sign = if List.hd a_lst > 0 then `Plus else `Minus in let _,ans,_ = List.fold_left (fun (cum, cnt, sign) x -> match sign with | `Plus -> if x + cum <= 0 then let x = x+cum in ((cum+1-x), (cnt+1-x), `Minus) else (cum,cnt,`Minus) | `Minus -> if x + cum >= 0 then let x = x+cum in ((cum+(-1-x)), (cnt-(-1-x)), `Plus) else (cum,cnt,`Plus) ) (0,0,sign) l in Printf.printf "%d\n" 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; long long chk, ans = 0, ans2 = 0; scanf("%d", &n); vector<int> a(n); for (auto& e : a) scanf("%d", &e); chk = a[0]; for (int i = 1; i < n; i++) { if (i % 2) { chk += a[i]; if (chk >= 0) { ans += chk + 1; chk = -1; } } else { chk += a[i]; if (chk <= 0) { ans += -1 * chk + 1; chk = 1; } } } chk = a[0]; for (int i = 1; i < n; i++) { if (i % 2 == 0) { chk += a[i]; if (chk >= 0) { ans2 += chk + 1; chk = -1; } } else { chk += a[i]; if (chk <= 0) { ans2 += -1 * chk + 1; chk = 1; } } } if (a[0] > 0) printf("%lld\n", ans); else printf("%lld\n", ans2); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first - r.first, l.second - r.second); } const long long int MOD = 1e9 + 7, INF = 1e18; long long int N, arr[100000]; int main() { cin.tie(0); ios_base::sync_with_stdio(false); cin >> N; for (long long int i = (0), i_end_ = (N); i < i_end_; i++) { cin >> arr[i]; } for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) { arr[i + 1] += arr[i]; } bool flag; if (arr[0] >= 0) flag = true; else flag = false; long long int sum = 0; long long int ans = 0; for (long long int i = (0), i_end_ = (N - 1); i < i_end_; i++) { arr[i + 1] += sum; if (flag ^ ((i % 2) == 1)) { if (arr[i + 1] >= 0) { sum -= (arr[i + 1] + 1); ans += abs(arr[i + 1] + 1); arr[i + 1] -= (arr[i + 1] + 1); } } else { if (arr[i + 1] <= 0) { sum -= (arr[i + 1] - 1); ans += abs(arr[i + 1] - 1); arr[i + 1] -= (arr[i + 1] - 1); } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; int count = 0; vector<int> a(100000); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } if (a[0] == 0) { if (a[1] > 0) { a[0]--; count++; } else { a[0]++; count++; } } vector<long long int> sum(100000); sum[0] = a[0]; for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + a[i]; if (sum[i] * sum[i - 1] >= 0) { if (sum[i - 1] < 0) { while (sum[i] * sum[i - 1] >= 0) { sum[i]++; count++; } } else { while (sum[i] * sum[i - 1] >= 0) { sum[i]--; count++; } } } } cout << count; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> inline void chmax(T& a, T b) { if (a < b) { a = b; } } template <class T> inline void chmin(T& a, T b) { if (a > b) { a = b; } } template <class T> inline T gcd(T x, T y) { if (y == 0) { return x; } else if (x == 0) { return y; } return gcd(y, x % y); } template <class T> inline T lcm(T x, T y) { return (x * y) / gcd(x, y); } template <class T> inline void print_vector(vector<T> vec) { for (int i = 0; i < vec.size(); i++) { cout << vec[i] << " "; } cout << endl; } const long long MOD = 1e9 + 7; const long long LLINF = 1LL << 60; const int INF = 1 << 30; int main(void) { long long N; cin >> N; vector<long long> A(N); vector<long long> B(N); for (int i = 0; i < N; i++) { cin >> A[i]; B[i] = A[i]; } long long sum = A[0] > 0 ? A[0] : 1; long long count = abs(sum - A[0]); for (int i = 1; i < N; i++) { long long tmp = A[i] + sum; if (i % 2 == 1 and tmp >= 0) { A[i] += (-1 - tmp); count += abs(-1 - tmp); } else if (i % 2 == 0 and tmp <= 0) { A[i] += (1 - tmp); count += (1 - tmp); } sum += A[i]; } long long ans = count; sum = B[0] < 0 ? B[0] : -1; count = sum - B[0]; for (int i = 1; i < N; i++) { long long tmp = B[i] + sum; if (i % 2 == 0 and tmp >= 0) { B[i] += (-1 - tmp); count += abs(-1 - tmp); } else if (i % 2 == 1 and tmp <= 0) { B[i] += (1 - tmp); count += (1 - tmp); } sum += B[i]; } chmin(ans, count); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #define int long long #define r(i,n) for(int i=0;i<n;i++) using namespace std; int a[100009],n; int d1(){ int sum=0,ans=0; for(int i=0;i<n;i++){ if(i%2==0){ if(sum+a[i]>=0)ans+=sum-a[i]+1,sum=-1; else sum+=a[i]; } else{ if(sum+a[i]<=0)ans+=1-sum+a[i],sum=1; else sum+=a[i]; } } return ans; } int d2(){ int sum=0,ans=0; for(int i=0;i<n;i++){ if(i%2==1){ if(sum+a[i]>=0)ans+=sum-a[i]+1,sum=-1; else sum+=a[i]; } else{ if(sum+a[i]<=0)ans+=1-sum+a[i],sum=1; else sum+=a[i]; } } return ans; } main(){ cin>>n; r(i,n)cin>>a[i]; cout<<min(d1(),d2())<<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; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } template <class T> inline T sqr(T x) { return x * x; } const double EPS = 1e-10; const double PI = acos(-1.0); int main() { int N; scanf("%d", &N); vector<int> arr(N); for (int i = (0); i < (N); ++i) { scanf("%d", &arr[i]); } int count = 0; int sum = 0; bool positive = true; if (arr[0] < 0) { positive = false; } sum += arr[0]; for (int i = 1; i < N; i++) { if (positive) { if (sum + arr[i] < 0) { sum = sum + arr[i]; positive = false; } else { int must = -sum - 1; int diff = arr[i] - must; count += diff; sum += must; positive = false; } } else { if (sum + arr[i] > 0) { sum = sum + arr[i]; positive = true; } else { int must = -sum + 1; int diff = must - arr[i]; count += diff; sum += must; positive = true; } } } printf("%d", count); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; const int MOD = 1000000007; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } ll t = 0; ll sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { if (sum <= 0) { t += 1 - sum; sum += t; } } else { if (sum >= 0) { t += sum + 1; sum -= t; } } } ll u = 0; sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 1) { if (sum <= 0) { u += 1 - sum; sum += u; } } else { if (sum >= 0) { u += sum + 1; sum -= u; } } } cout << min(t, u) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
package main import ( "bufio" "fmt" "math" "os" "strconv" ) const pi = math.Pi var mod int = pow(10, 9) + 7 var Umod uint64 = 1000000007 var ans int64 func main() { reader.Split(bufio.ScanWords) n, _ := strconv.Atoi(read()) a := make([]int, n) for i := 0; i < n; i++ { a[i], _ = strconv.Atoi(read()) } sum := make([]int64, n) sum[0] = int64(a[0]) for i := 1; i < n; i++ { sum[i] += int64(a[i]) + sum[i-1] fmt.Println(sum[i-1], sum[i], ans) if (0 <= sum[i-1] && 0 <= sum[i]) || (sum[i-1] <= 0 && sum[i] <= 0) { // NGパターン if sum[i] < 0 { ans += 1 - sum[i] sum[i] = 1 } else { ans += sum[i] + 1 sum[i] = -1 } } } fmt.Println(ans) } /* ---------------------------------------- */ var reader = bufio.NewScanner(os.Stdin) func read() string { reader.Scan() return reader.Text() } func lcm(x, y int) int { return (x / gcd(x, y)) * y } func gcd(x, y int) int { if x%y == 0 { return y } else { r := x % y return gcd(y, r) } } var fac [1000000]int var finv [1000000]int var inv [1000000]int func combination_init() { fac[0], fac[1] = 1, 1 finv[0], finv[1] = 1, 1 inv[1] = 1 // invは a^(-1) mod p // pをaで割ることを考える // p/a*(a) + p%a = p // p/a*(a) + p%a = 0 (mod p) // -p%a = p/a*(a) (mod p) // -p%a *a^(-1)= p/a (mod p) // a^(-1)= p/a * (-p%a)^(-1) (mod p) // a^(-1) = for i := 2; i < 1000000; i++ { fac[i] = fac[i-1] * i % mod inv[i] = mod - inv[mod%i]*(mod/i)%mod finv[i] = finv[i-1] * inv[i] % mod } } func combination(x, y int) int { if x < y { return 0 } if fac[0] != 1 { combination_init() } return fac[x] * (finv[y] * finv[x-y] % mod) % mod //return fac[x] / (fac[y] * fac[x-y]) } func permutation(x, y int) int { if x < y { return 0 } if fac[0] != 1 { combination_init() } return fac[x] * (finv[x-y] % mod) % mod //return fac[x] / fac[x-y] } func max(x ...int) int { var res int = x[0] for i := 1; i < len(x); i++ { res = int(math.Max(float64(x[i]), float64(res))) } return res } func min(x ...int) int { var res int = x[0] for i := 1; i < len(x); i++ { res = int(math.Min(float64(x[i]), float64(res))) } return res } func pow(x, y int) int { return int(math.Pow(float64(x), float64(y))) } func abs(x int) int { return int(math.Abs(float64(x))) } func floor(x int) int { return int(math.Floor(float64(x))) } func ceil(x int) int { return int(math.Ceil(float64(x))) } type SortBy [][]int func (a SortBy) Len() int { return len(a) } func (a SortBy) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a SortBy) Less(i, j int) bool { return a[i][0] < a[j][0] } type PriorityQueue []int func (h PriorityQueue) Len() int { return len(h) } func (h PriorityQueue) Less(i, j int) bool { return h[i] < h[j] } func (h PriorityQueue) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *PriorityQueue) Push(x interface{}) { *h = append(*h, x.(int)) } func (h *PriorityQueue) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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<int64_t> a(n + 10, 0); for (int i = 1; i <= n; ++i) { cin >> a[i]; } int pn = 0; int mn = 0; const int64_t first = a[1]; { int num = 0; int64_t total = 0; if (first <= 0) { num = 1 - first; total = 1; } else { total = first; } for (int i = 2; i <= n; ++i) { int64_t ai = a[i]; if (i % 2 == 0) { if (ai >= 0) { num += -(-1 - ai); ai = -1; } if (total + ai >= 0) { const int64_t back = ai; ai = -1 - total; num += abs(ai - back); } total += ai; } else { if (ai <= 0) { num += 1 - ai; ai = 1; } if (total + ai <= 0) { const int64_t back = ai; ai = 1 - total; num += abs(ai - back); } total += ai; } } pn = num; } { int num = 0; int64_t total = 0; if (first >= 0) { num = -(-1 - first); total = -1; } else { total = first; } for (int i = 2; i <= n; ++i) { int64_t ai = a[i]; if (i % 2 == 0) { if (ai <= 0) { num += 1 - ai; ai = 1; } if (total + ai <= 0) { const int64_t back = ai; ai = 1 - total; num += abs(ai - back); } total += ai; } else { if (ai >= 0) { num += -(-1 - ai); ai = -1; } if (total + ai >= 0) { const int64_t back = ai; ai = -1 - total; num += abs(ai - back); } total += ai; } } mn = num; } cout << min(pn, mn) << 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 long long = long long; 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; } const long long INF = 1e9; long long n; vector<long long> a; int main() { cin >> n; a.resize(n); for (long long i = 0; i < (n); ++i) cin >> a[i]; long long sum; long long ans = 0; sum = a[0]; if (sum == 0) { if (a[1] < 0) sum = 1; else sum = -1; ans++; } for (long long i = 0; i < (n - 1); ++i) { if (sum < 0) { if (sum + a[i + 1] > 0) { sum += a[i + 1]; continue; } else { ans += 1 - sum - a[i + 1]; sum = 1; } } else { if (sum + a[i + 1] < 0) { sum += a[i + 1]; continue; } else { ans += a[i + 1] + sum + 1; sum = -1; } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long dptemp[100010]; long long s1[100010], dp[100010]; long long mi = 9223372036854775807, n, a, sum, pri1, pri2, all; void cir() { for (a = 2; a <= n; a++) { dp[a] = (dp[a - 1] + s1[a]); if (dp[a - 1] > 0) { if (dp[a] >= 0) { all += (dp[a] + 1); dp[a] = -1; } } else { if (dp[a] <= 0) { all += (-dp[a] + 1); dp[a] = 1; } } } if (all < mi) mi = all; } void copyy() { for (long long a = 1; a <= n; a++) dptemp[a] = dp[a]; } int main() { scanf("%lld", &n); dp[0] = 0; for (a = 1; a <= n; a++) { scanf("%lld", &s1[a]); dp[a] = s1[a] + dp[a - 1]; dptemp[a] = dp[a]; } if (dp[1] > 0) { copyy(); all = 0; cir(); copyy(); all = dp[1] + 1; dp[1] = -1; cir(); } else if (dp[1] < 0) { copyy(); all = 0; cir(); copyy(); all = -dp[1] + 1; dp[1] = 1; cir(); } else { copyy(); all = 1; dp[1] = 1; cir(); copyy(); all = -1; dp[1] = -1; cir(); } printf("%lld\n", mi); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } template <class T> inline T sqr(T x) { return x * x; } const double EPS = 1e-10; const double PI = acos(-1.0); int main(void) { int n; cin >> n; int array[100000]; for (int i = (0); i < (100000); ++i) { int a; cin >> array[i]; } int ans = 0; bool flag = false; int sum = array[0]; if (array[0] == 0) { if (array[1] > 0) { ans++; sum = -1; flag = false; } else if (array[1] < 0) { ans++; sum = 1; flag = true; } else { sum = 1; ans++; } } else if (array[0] > 0) flag = true; else flag = false; for (int i = (1); i < (n); ++i) { sum += array[i]; if (flag) { if (sum >= 0) { ans += (sum + 1); sum -= (sum + 1); } flag = false; } else { if (sum <= 0) { ans += -1 * (sum - 1); sum += -1 * (sum - 1); } flag = true; } } 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
x = input().split(" ") a = int(x[0]) b = int(x[1]) if (a>b): print("GREATER") elif(a==b): print("EQUAL") else: print("LESS")
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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 solve(int *a, int n) { long long count = 0; long long calc = 0; int state, pstate; if (a[0] < 0) state = -1; if (a[0] > 0) state = 1; for (int i = 1; i < n; i++) { pstate = state; int tmp = a[i] + calc; if (tmp < 0) state = -1; if (tmp == 0) state = 0; if (tmp > 0) state = 1; if (pstate == state) { if (state == -1) { count += 1 - tmp; calc += 1 - tmp; state = 1; } else if (state == 1) { count += tmp + 1; calc += -1 - tmp; state = -1; } } if (state == 0) { if (pstate == -1) { count += 1; calc += 1; state = 1; } else if (pstate == 1) { count += 1; calc += -1; state = -1; } } } return count; } int main() { int n; long long ans; int *a; cin >> n; a = new int[n]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) a[i] = a[i - 1] + a[i]; if (a[0] == 0) { long long bs, cs; int *b = new int[n]; int *c = new int[n]; for (int i = 0; i < n; i++) b[i] = a[i] + 1; for (int i = 0; i < n; i++) c[i] = a[i] - 1; bs = solve(b, n); cs = solve(c, n); ans = bs < cs ? bs : cs; } else ans = solve(a, n); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long fcount(long long arr[], long long n, int b, long long val[]) { long long c = 0; long long diff = 0; for (long long i = 0; i < n; i++) { if (arr[i] + diff == 0) { if (b == 1) { diff += 1; c += 1; } else { diff -= 1; c += 1; } } else if (arr[i] + diff > 0 && b == 0) { long long temp = diff; diff -= (arr[i] + diff + 1); c += arr[i] + temp + 1; } else if (arr[i] + diff < 0 && b == 1) { long long temp = diff; diff += -(arr[i] + diff) + 1; c += -(arr[i] + temp) + 1; } b = (b + 1) % 2; } return c; } int main() { long long n; cin >> n; long long arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; long long c = 0; long long diff = 0, b = -1; long long prefix[n]; prefix[0] = arr[0]; for (int i = 1; i < n; i++) { prefix[i] = prefix[i - 1] + arr[i]; } for (int i = 0; i < n; i++) cout << prefix[i] << " "; cout << "\n"; cout << min(fcount(prefix, n, 1, arr), fcount(prefix, n, 0, arr)) << "\n"; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import copy N=int(input()) l=list(map(int, input().split())) #リスト入力 cp = copy.copy(l) #c=0 for k in range(N-1): if sum(l[:k+1])==0: #c=c+1 if l[k+1]>0: l[k+1]=l[k+1]+1 else: l[k+1]=l[k+1]-1 if sum(l[:k])*sum(l[:k+1])>0: print(k,sum(l[:k]),sum(l[:k+1]),l[k+1]) if sum(l[:k+1])>0: l[k]=l[k]-(sum(l[:k+1])-(-1)) #c=c+abs(sum(l[:k+1])-(-1)) else: l[k]=l[k]+(1-sum(l[:k+1])) #c=c+abs(1-sum(l[:k+1])) print(l[k]) if sum(l)==0: c=c+1 l[-1]=l[-1]+1 #print(l) #print(c) print(sum([abs(l[n]-cp[n]) for n in range(N)]))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long ns[] = new long[n]; for (int i = 0; i < n; i++) { ns[i] = sc.nextLong(); } long sum = ns[0]; long ans = 0; boolean isNegative = ns[0] < 0; for (int i = 1; i < n; i++) { sum += ns[i]; if (isNegative && sum < 0) { ans -= sum - 1; sum = 1; } else if (!isNegative && sum > 0) { ans += sum + 1; sum = -1; } else if (sum == 0) { ans++; } isNegative = !isNegative; } System.out.println(ans); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> signed main() { long long n; std::cin >> n; std::vector<long long> a(n); for (long long i = 0; i < (n); i++) std::cin >> a[i]; const long long INF = 1e18; long long mincount = INF; for (long long x = 0; x < (2); x++) { long long sum = a[0]; long long count = 0; if (x == 1) { if (sum > 0) { count += sum + 1; sum = -1; } else if (sum < 0) { count += 1 - sum; sum = 1; } } if (sum == 0) continue; for (long long i = 1; i < n; i++) { if ((sum + a[i]) * sum >= 0) { if (sum > 0) { count += a[i] + sum + 1; sum = -1; } else if (sum < 0) { count += 1 - a[i] - sum; sum = 1; } } else sum += a[i]; } mincount = std::min(count, mincount); } std::cout << (mincount) << '\n'; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def c(ints): for i in range(len(ints)): if ints[i] != 0: sig = 1 if ints[i] > 0 else -1 total = ints[i] mov = i + 1 j = i break if i == len(ints) - 1: return i + 1 for i_ in ints[j+1:]: tmp = total + i_ if tmp == 0: mov +=1 tmp = -sig elif sig * tmp > 0: mov += abs(tmp) + 1 tmp = -sig sig *= -1 total = tmp return mov _ = input() inp = input() inp = inp.split(' ') inp = [int(i_) for i_ in inp] print(c(inp))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long N; long long buf; cin >> N; vector<long long> aaa = vector<long long>(N + 1, 0); for (int i = 1; i < N + 1; i++) { cin >> buf; aaa.at(i) = buf + aaa.at(i - 1); } bool be = true; bool af; vector<long long> ans = vector<long long>(2, 0); long long change = 0; for (int l = 0; l < 2; l++) { vector<long long> aa = aaa; if (l == 0) { be = true; } else { be = false; } for (int i = 1; i < N + 1; i++) { if (ans.at(1) > ans.at(0)) { break; } if (aa.at(i) == 0) { if (af) { change = 1 - aa.at(i); ans.at(l) += 1 - aa.at(i); for (int j = i; j < N + 1; j++) { aa.at(j) += change; } be = false; } else { change = -(aa.at(i) + 1); ans.at(l) += 1 + aa.at(i); for (int j = i; j < N + 1; j++) { aa.at(j) += change; } be = true; } } else { if (aa.at(i) < 0) { af = true; } else { af = false; } if (af == be) { if (af) { change = 1 - aa.at(i); ans.at(l) += 1 - aa.at(i); for (int j = i; j < N + 1; j++) { aa.at(j) += change; } be = false; } else { change = -(aa.at(i) + 1); ans.at(l) += 1 + aa.at(i); for (int j = i; j < N + 1; j++) { aa.at(j) += change; } be = true; } } else { be = af; } } } } long long tt = min(ans.at(0), ans.at(1)); std::cout << tt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) l = list(map(int,input().split())) count = l[0] ans=0 for i in range(1,n): if count < 0: if count + l[i] <= 0: ans += 1-(count+l[i]) count = 1 else: count = count + l[i] else: if count + l[i] >= 0: ans += count + l[i] + 1 count = -1 else: count = count + l[i] print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxx = 1e5 + 7; long long n, ans = 1ll << 60; long long a[maxx]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; int flag = 0; long long sum = 0; long long res = 0; for (int i = 1; i <= n; i++) { sum += a[i]; if (flag == 0) { if (sum <= 0) { res = res + 1 - sum; sum = 1; } } else { if (sum >= 0) { res = res + sum + 1; sum = -1; } } flag ^= 1; } ans = min(ans, res); res = 0; sum = 0; flag = 1; for (int i = 1; i <= n; i++) { sum += a[i]; if (flag == 1) { if (sum <= 0) { res = res + 1 - sum; sum = 1; } } else { if (sum >= 0) { res = res + sum + 1; sum = -1; } } flag ^= 1; } ans = min(ans, res); cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; int C[200000]; cin >> N; for (int i = 0; i < N; i++) { cin >> C[i]; } int cnt_a = 0; int cnt_b = 0; int sum_a = 0; int sum_b = 0; for (int i = 0; i < N; i++) { sum_a += C[i]; if ((i % 2 == 0) && (sum_a <= 0)) { cnt_a += 1 - sum_a; sum_a = 1; } if ((i % 2 == 1) && (sum_a >= 0)) { cnt_a += sum_a + 1; sum_a = -1; } } for (int i = 0; i < N; i++) { sum_b += C[i]; if ((i % 2 == 0) && (sum_b >= 0)) { cnt_b += 1 + sum_b; sum_b = -1; } if ((i % 2 == 1) && (sum_b <= 0)) { cnt_b += 1 - sum_b; sum_b = 1; } } cout << min(cnt_a, cnt_b) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> int main() { long long int n, a, i, S[2] = {}, C[2] = {}; scanf("%lld", &n); for (i = 0; i < n; i++) { scanf("%lld", &a); S[0] += a; S[1] += a; if (i == 0) ; else { 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; } } } } printf("%lld\n", C[0] < C[1] ? C[0] : C[1]); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
require 'prime' include Math def max(a,b); a > b ? a : b end def min(a,b); a < b ? a : b end def swap(a,b); a, b = b, a end def gif; gets.to_i end def gff; gets.to_f end def gsf; gets.chomp end def gi; gets.split.map(&:to_i) end def gf; gets.split.map(&:to_f) end def gs; gets.chomp.split.map(&:to_s) end def gc; gets.chomp.split('') end def pr(num); num.prime_division end def digit(num); num.to_s.length end def array(s,ini=nil); Array.new(s){ini} end def darray(s1,s2,ini=nil); Array.new(s1){Array.new(s2){ini}} end def rep(num); num.times{|i|yield(i)} end def repl(st,en,n=1); st.step(en,n){|i|yield(i)} end n = gif a = gi sum = [] count = 0 sum << a[0] repl 1,a.size-1 do |i| sum << a[i]+sum[i-1] if sum[i-1] > 0 if sum[i] >= 0 count += sum[i]+1 sum[i] = -1 end else if sum[i] <= 0 count += 1-sum[i] sum[i] = 1 end end end puts count
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int count = 0, sum = a[0]; for (int i = 1; i < n; i++) { if (a[i - 1] * a[i] > 0) { count += abs(a[i]) + abs(sum) + 1; a[i] = abs(sum) + 1; if (a[i - 1] > 0) a[i] *= -1; sum += a[i]; } else { if (sum + a[i] == 0) { if (a[i] > 0) a[i]++; else a[i]--; count++; } 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
cpp
#include <bits/stdc++.h> using namespace std; int n; long long a[100000]; long long c; long long csum; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } c = 0; csum = 0; for (int i = 0; i < n; i++) { long long bsum = csum; bsum += a[i]; if (i == 0 && bsum == 0) { c += 1; int nonzero; for (int j = 0; j < n; j++) { if (a[j] != 0) { nonzero = j; break; } } if (nonzero % 2 == 0) { if (a[nonzero] > 0) { bsum = 1; } else { bsum = -1; } } else { if (a[nonzero] < 0) { bsum = 1; } else { bsum = -1; } } } if (i != 0 && csum * bsum >= 0) { if (csum > 0) { c += (bsum + 1); bsum -= (bsum + 1); } else { c += (-bsum + 1); bsum += (-bsum + 1); } } csum = bsum; } cout << c << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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()]; int x[] = new int[l.length]; int y[] = new int[l.length]; for (int i = 0;i < l.length;++i){ l[i] = Integer.valueOf(scanner.next()); y[i] = Integer.valueOf(l[i]); if(i > 0){ x[i] = l[i] + x[i - 1]; } else{ x[i] = l[i]; } } boolean flag = true; while (true){ for (int i = 1;i < l.length;++i){ int p = x[i - 1]; int q = x[i]; if(q == 0||(q < 0&&p < 0)||(q > 0&&p > 0)){ flag = false; int d = (p < 0&&q <= 0) ? 1 : -1; l[i] += d; for (int j = i;j < l.length;++j){ x[j] += d; } } } if(flag){ break; } flag = true; } for (int i = 1;i < l.length;++i){ count += Math.abs(l[i] - y[i]); } 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; const int MaxN = 1e5; bool flag; long long sum, ans; int n; int a[MaxN + 5], b[MaxN + 5]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); b[i] = a[i]; } sum = a[1]; if (a[1] < 0) flag = 1; else flag = 0; for (int i = 2; i <= n; i++) { if (flag == 1) { if (sum + a[i] <= 0) { long long ant = sum + a[i]; int t = a[i]; a[i] = 1 - sum; ans += (a[i] - t); sum += a[i]; } else sum += a[i]; flag = 0; } else { if (sum + a[i] >= 0) { long long ant = sum + a[i]; int t = a[i]; a[i] = -1 - sum; ans += (t - a[i]); sum += a[i]; } else sum += a[i]; flag = 1; } } sum = a[n]; printf("%lld\n", 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())) a1 = [a[0]] * n a2 = [-a[0]] * n b = a[0] ans = 0 ans1 = 0 def f(x): if x == 0: return 0 else: return x // abs(x) for i in range(1, n): if a1[i - 1] * a[i] >= 0: a1[i] = -a[i] else: a1[i] = a[i] if b * (b + a[i]) >= 0: a1[i] = -f(a1[i - 1]) - b if b + a1[i] == 0: a1[i] += f(a1[i]) ans += abs(a1[i] - a[i]) b += a1[i] print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# -*- coding: utf-8 -*- """ Created on Sat Sep 8 15:51:53 2018 @author: maezawa """ def f(n, a0, cnt, sa, sign): a = a0[:] if sign == -1: a[0] = -a[0] cnt += 2*a[0] for i in range(n-1): sa += a[i] na = -sa//abs(sa)*(abs(sa)+1) if abs(a[i+1]) > abs(na) and a[i+1]*na > 0: continue else: cnt += abs(na-a[i+1]) a[i+1] = na return cnt n = int(input()) a = list(map(int, input().split())) sa = 0 cnt = 0 cnt0 = f(n, a, cnt, sa, -1) cnt1 = f(n, a, cnt, sa, 1) cnt = min([cnt0,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
cpp
#include <bits/stdc++.h> using namespace std; const long long INFF = 0x3f3f3f3f3f3f3f3f; long long a[1000010]; int n; long long solve() { long long sum = 0; long long oo = a[0]; for (int i = 1; i < n; i++) { if (oo < 0) { oo += a[i]; if (oo <= 0) { sum += 1 - oo; oo = 1; } } else { oo += a[i]; if (oo >= 0) { sum += oo + 1; oo = -1; } } } return sum; } int main() { while (scanf("%d", &n) != EOF) { long long sum = 0; for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); } if (a[0] == 0) { a[0] = 1; long long sum1 = solve(); a[0] = -1; long long sum2 = solve(); sum = min(sum1, sum2) + 1; } else { long long sum0 = solve(); a[0] = 1; long long sum1 = solve() + abs(a[0] - 1); a[0] = -1; long long sum2 = solve() + abs(a[0] + 1); sum = min(sum0, min(sum1, sum2)); } printf("%lld\n", sum); } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int x = 0, sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { if (sum < 1) { x += 1 - sum; sum = 1; } } else { if (sum > -1) { x += 1 + sum; sum = -1; } } } int y = 0; sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { if (sum > -1) { y += (1 + sum); sum = -1; } } else { if (sum < 1) { y += (1 - sum); sum = 1; } } } cout << min(x, y) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys input = sys.stdin.readline n = int(input()) a = [int(x) for x in input().split()] A = a[0] if A != 0: ans = 0 for i in range(1, n): nextA = A + a[i] if (A > 0 and nextA < 0) or (A < 0 and nextA > 0): A = nextA elif nextA == 0 and A > 0: ans += 1 A = -1 elif nextA == 0 and A < 0: ans += 1 A = 1 elif A > 0: ans += abs(nextA) + 1 A = -1 else: ans += abs(nextA) + 1 A = 1 print(ans) sys.exit() ans1 = 1 A1 = 1 for i in range(1, n): nextA = A1 + a[i] if (A1 > 0 and nextA < 0) or (A1 < 0 and nextA > 0): A1 = nextA elif nextA == 0 and A1 > 0: ans1 += 1 A1 = -1 elif nextA == 0 and A1 < 0: ans1 += 1 A1 = 1 elif A1 > 0: ans1 += abs(nextA) + 1 A1 = -1 else: ans1 += abs(nextA) + 1 A1 = 1 ans2 = 1 A2 = -1 for i in range(1, n): nextA = A2 + a[i] if (A2 > 0 and nextA < 0) or (A2 < 0 and nextA > 0): A2 = nextA elif nextA == 0 and A2 > 0: ans2 += 1 A2 = -1 elif nextA == 0 and A2 < 0: ans2 += 1 A2 = 1 elif A2 > 0: ans2 += abs(nextA) + 1 A2 = -1 else: ans2 += abs(nextA) + 1 A2 = 1 print(min(ans1, ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) if a[0] == 0: flag = 1 a[0] = 1 elif a[0] > 0: flag = 1 else: flag = -1 ans = 0 dp = [0 for i in range(n)] dp[0] = a[0] for i in range(1, n): dp[i] = dp[i - 1] + a[i] if dp[i] == 0: dp[i] = flag * -1 ans += 1 elif flag == 1 and dp[i] > 0: ans += abs(-1 - dp[i]) dp[i] = -1 elif flag == -1 and dp[i] < 0: ans += abs(1 - dp[i]) dp[i] = 1 flag *= -1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long cnt1 = 0, sum; if (a[0] > 0) sum = a[0]; else sum = 1, cnt1 += abs(a[0]) + 1; for (int i = 1; i < n; i++) { long long t = sum + a[i]; if (sum > 0 && t > 0) { if (a[i] >= 0) cnt1 += a[i] + sum + 1; else cnt1 += abs(sum - a[i]) + 1; sum = -1; } else if (sum < 0 && t < 0) { if (a[i] < 0) cnt1 += a[i] + sum + 1; else cnt1 += abs(sum - a[i]) + 1; sum = 1; } else if (t == 0) { cnt1++; if (sum > 0) sum = -1; else sum = 1; } else sum += a[i]; } long long cnt2 = 0; if (a[0] < 0) sum = a[0]; else sum = -1, cnt2 += abs(a[0]) + 1; for (int i = 1; i < n; i++) { long long t = sum + a[i]; if (sum > 0 && t > 0) { if (a[i] >= 0) cnt2 += a[i] + sum + 1; else cnt2 += abs(sum - a[i]) + 1; sum = -1; } else if (sum < 0 && t < 0) { if (a[i] < 0) cnt2 += a[i] + sum + 1; else cnt2 += abs(sum - a[i]) + 1; sum = 1; } else if (t == 0) { cnt2++; if (sum > 0) sum = -1; else sum = 1; } else sum += a[i]; } cout << min(cnt1, cnt2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long int a(int arr[], int n) { long long int sum = arr[0]; long long int c = 0; for (int i = 1; i < n; i++) { if (sum > 0) { if (sum + arr[i] < 0) sum = sum + arr[i]; else { c += (sum + arr[i]) + 1; sum = -1; } } else { if (sum + arr[i] > 0) sum = sum + arr[i]; else { c += abs(sum + arr[i]) + 1; sum = 1; } } } return c; } int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); ; int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; long long int ans1 = a(arr, n); long long int ans2 = 1 + abs(arr[0]); arr[0] = -arr[0]; ans2 += a(arr, n); cout << min(ans1, ans2); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int64_t min(int64_t a, int64_t b) { if (a > b) { return b; } else { return a; } } int64_t solve(vector<int> a, bool next) { bool nextposi = next; int ans = 0; int sum = 0; for (int i = 0; i < a.size(); i++) { sum += a.at(i); if (nextposi != (sum > 0)) { if (nextposi == 1) { ans += abs(sum - 1); sum = 1; } else { ans += abs(sum + 1); sum = -1; } } nextposi = !nextposi; } return ans; } int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } int64_t ans = 0; { ans = min(solve(a, 0), solve(a, 1)); } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) ans = 0 sumL = a[0] for i in range(1,n): sumL += a[i] if (sumL-a[i])*(sumL) > 0: if sumL > 0: ans += abs(sumL)+1 sumL = -1 else: ans += abs(sumL)+1 sumL = 1 elif (sumL-a[i])*(sumL) == 0: ans += 1 if sumL-a[i] < 0: sumL = 1 else: sumL = -1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { long long n; cin >> n; long long a[n]; long long sum[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } long long count = 0; for (int i = 0; i < n; i++) { if (i == 0) { sum[i] = a[i]; } else { sum[i] = sum[i - 1] + a[i]; if (sum[i] != 0 && sum[i - 1] == abs(sum[i - 1]) && sum[i] == abs(sum[i])) { count += sum[i] + 1; sum[i] = -1; } else if (sum[i] != 0 && sum[i - 1] != abs(sum[i - 1]) && sum[i] != abs(sum[i])) { count += 1 - sum[i]; sum[i] = 1; } } if (sum[i] == 0) { count++; if (i == 0) { if (a[i + 1] == abs(a[i + 1])) sum[i] = -1; else sum[i] = 1; } else { if (sum[i - 1] == abs(sum[i - 1])) sum[i] = 1; else sum[i] = -1; } } } cout << count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
after [] = 1 after as | head as == 0 = after (tail as) | head as < 0 = -1 | head as > 0 = 1 solve [] v acc = acc solve as v acc | v == 0 = if after as < 0 then solve as 1 (1 + acc) else solve as (-1) (1 + acc) | v < 0 = let w = v + (head as) in if w <= 0 then solve (tail as) 1 (1 - w + acc) else solve (tail as) w acc | v > 0 = let w = v + (head as) in if w >= 0 then solve (tail as) (-1) (1 + w + acc) else solve (tail as) w acc main = do n <- read <$> getLine :: IO Int l <- getLine let as = fmap read (words l) :: [Int] in putStrLn (show (solve (tail as) (head as) 0))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } template <class T> inline T sqr(T x) { return x * x; } int main(void) { int n; cin >> n; long long a[n + 1]; for (int i = 1; i <= n; ++i) { cin >> a[i]; } long long S1, S2; long long ans[2]; if (a[1] == 0) { S1 = 1; ans[0] = 1; for (int i = (2); i < (n + 1); ++i) { S2 = S1 + a[i]; if ((S1 < 0 && S2 > 0) || (S1 > 0 && S2 < 0)) { S1 = S2; } else { ans[0] += llabs(S2) + 1; if (S1 < 0) S2 = 1; else S2 = -1; S1 = S2; } } S1 = -1; ans[1] = 1; for (int i = (2); i < (n + 1); ++i) { S2 = S1 + a[i]; if ((S1 < 0 && S2 > 0) || (S1 > 0 && S2 < 0)) { S1 = S2; } else { ans[1] += llabs(S2) + 1; if (S1 < 0) S2 = 1; else S2 = -1; S1 = S2; } } ans[0] = min(ans[0], ans[1]); printf("%lld\n", ans[0]); } else { S1 = a[1]; ans[0] = 0; for (int i = (2); i < (n + 1); ++i) { S2 = S1 + a[i]; if ((S1 < 0 && S2 > 0) || (S1 > 0 && S2 < 0)) { S1 = S2; } else { ans[0] += llabs(S2) + 1; if (S1 < 0) S2 = 1; else S2 = -1; S1 = S2; } } printf("%lld\n", ans[0]); } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; bool flag = true; int sum = 0; int plus = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (flag && sum <= 0) { plus += abs(sum) + 1; sum = 1; } if (!flag && sum >= 0) { plus += abs(sum) + 1; sum = -1; } flag = !flag; } flag = false; sum = 0; int minus = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (flag && sum <= 0) { minus += abs(sum) + 1; sum = 1; } if (!flag && sum >= 0) { minus += abs(sum) + 1; sum = -1; } flag = !flag; } cout << min(minus, plus) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int sign(long long n) { return ((n > 0) - (n < 0)); } int main() { int n; scanf("%d", &n); long long sum = 0; long long ans = 0; long long prev_sum = 0; for (int i = 0; i < n; i++) { int ra; scanf("%d", &ra); long long a = ra; if (sum == 0) { if (a == 0) { a -= sign(prev_sum); ans++; } } else if (sum + a == 0) { a - sign(sum); ans++; } else if (sign(sum + a) + sign(sum) != 0) { long long tmp = a; if (sum + a > 0) { a = a - (sum + a) - 1; } else if (sum + a < 0) { a = a - (sum + a) + 1; } else { a -= sign(sum); } ans += abs(tmp - a); } prev_sum = sum; sum += a; } printf("%llu\n", 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.IOException; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException{ Sequence solver = new Sequence(); solver.readInput(); solver.solve(); solver.writeOutput(); } static class Sequence { private int n; private long a[]; private int output; private Scanner scanner; public Sequence() { this.scanner = new Scanner(System.in); } public void readInput() { n = Integer.parseInt(scanner.next()); a = new long[n]; for(int i=0; i<n; i++) { a[i] = Integer.parseInt(scanner.next()); } } private int count(boolean sign) { int count=0; long sum=0; for(int i=0; i<n; i++) { sum += a[i]; if((i%2==0) == sign) { // a[i]までの合計を正にするとき if(sum<=0) { count += 1-sum; sum = 1; } } else { // a[i]までの合計を負にするとき if(0<=sum) { count += 1+sum; sum = -1; } } } return count; } public void solve() { int plus = count(true); int minus = count(false); output = Math.min(plus,minus); } public void writeOutput() { System.out.println(output); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) if a[0] != 0: print(0) else: print(a[n+4])
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int calc(vector<int>& a) { int sum = a[0]; int count = 0; for (int i = (1); i < (a.size()); ++i) { if (sum < 0) { sum += a[i]; if (sum <= 0) { count += 1 - sum; sum = 1; } continue; } sum += a[i]; if (sum >= 0) { count += sum + 1; sum = -1; } } return count; } int main() { ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; vector<int> a(N); for (auto& ai : a) cin >> ai; int count = 0; int sum = a[0]; if (sum == 0) { a[0] = 1; auto count1 = calc(a); a[0] = -1; auto count2 = calc(a); count = min(count1, count2); } else { auto count1 = calc(a); a[0] = 1; auto count2 = calc(a) + abs(1 - sum); a[0] = -1; auto count3 = calc(a) + abs(-1 - sum); count = min(count1, min(count2, count3)); } cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<iostream> #include<algorithm> #include<math.h> #include<string> #include<tuple> #include<vector> #include<cstdlib> #include<cstdint> #include<stdio.h> #include<cmath> #include<limits> #include<iomanip> #include<ctime> #include<climits> #include<random> #include<queue> #include<map> using namespace std; template <class T> using V = vector<T>; 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; } const long long INF = 1LL << 60; const double pi=acos(-1); using ll = long long; using db = long double; using st = string; using ch = char; using vll = V<ll>; using vpll =V<pair<ll,ll>>; using vst = V<st>; using vdb = V<db>; using vch = V<ch>; using graph = V<V<int>>; using pq = priority_queue<ll>; #define FOR(i,a,b) for(ll i=(a);i<(b);i++) #define bgn begin() #define en end() #define SORT(a) sort((a).bgn,(a).en) #define REV(a) reverse((a).bgn,(a).en) #define fi first #define se second #define sz size() #define gcd(a,b) __gcd(a,b) #define pb(a) push_back(a); #define ALL(a) (a).begin(),(a).end() ll Sum(ll n) { ll m=0; while(n){ m+=n%10; n/=10; } return m; } const int MAX = 510000; // change const int MOD = 1000000007; long long fac[MAX], finv[MAX], inv[MAX]; void Comuse() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++){ fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } #define comuse Comuse() ll combi(int n, int k){ if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } ll perm(int n,int k){ if(n < k) return 0; if(n < 0 || k < 0) return 0; return fac[n] * (finv[k] % MOD) % MOD; } ll modpow(ll a,ll n,ll mod){ ll ans=1; while(n>0){ if(n&1){ ans=ans*a%mod; } a=a*a%mod; n>>=1; } return ans; } ll modinv(ll a, ll mod) { return modpow(a, mod - 2, mod); } ll modcombi(int n,int k,int mod){ ll ans=1; for(ll i=n;i>n-k;i--){ ans*=i; ans%=mod; } for(ll i=1;i<=k;i++){ ans*=modinv(i,mod); ans%=mod; } return ans; } ll lcm(ll a,ll b){ ll n; n=a/gcd(a,b)*b; return n; } vll div(ll n){ vll ret; for(ll i=1;i*i<=n;i++){ if(n%i==0){ ret.push_back(i); if(i*i!=n){ ret.push_back(n/i); } } } SORT(ret); return (ret); } vector<bool> isprime(MAX+100,true); void primeuse(){ isprime[0]=false; isprime[1]=false; for(int i=2;i<MAX+50;i++){ int up=sqrt(i)+1; for(int j=2;j<up;j++){ if(i%j==0){ isprime[i]=false; } } } } void bf(ll n,string s){ for(ll i=0;i<n;i++){ cout<<s; } cout<<"\n"; } void Solve(); const int MAX_N = 131072; //segment tree int NN; int seg[MAX_N*2-1]; void seguse(){ for(int i=0;i<2*NN-1;i++){ seg[i]=INT_MAX; } } signed main(){ cin.tie(0); ios::sync_with_stdio(false); cout<<setprecision(20)<<fixed; Solve(); } /****************************************\ | Thank you for viewing my code:) | | Written by RedSpica a.k.a. RanseMirage | | Twitter:@asakaakasaka | \****************************************/ //segtreeの葉の先頭の添え字はN-1 void Solve(){ ll n; cin>>n; vll A(n); vll B(n); FOR(i,0,n){ cin>>A[i]; } ll ans=0; ll all=A[0]; bool can=true; FOR(i,1,n){ if((all+A[i])*all>=0){ can=false; break; } all+=A[i]; } if(can){ cout<<"0\n"; return; } B[0]=A[0]; if(A[0]==0){ ans++; if(A[1]>0){ B[0]=-1; } else if(A[1]<0){ B[0]=1; } } FOR(i,1,n){ B[i]=B[i-1]+A[i]; if(B[i]*B[i-1]<0){ continue; } ans+=abs(B[i])+1; if(B[i-1]<0){ B[i]=1; } else{ B[i]=-1; } } cout<<ans<<"\n"; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long int a[100000]; for (int i = 0; i < n; i++) { cin >> a[i]; } long long int Mp = 0; long long int Sp = 0; long long int dif = 0; for (int i = 0; i < n; i++) { Sp += a[i]; if (i % 2 == 0) { if (Sp <= 0) { dif = 1 - Sp; Mp += dif; Sp += dif; } } else { if (Sp >= 0) { dif = Sp + 1; Mp += dif; Sp += -dif; } } } long long int Mn = 0; long long int Sn = 0; long long int di = 0; for (int i = 0; i < n; i++) { Sn += a[i]; if (i % 2 == 1) { if (Sn <= 0) { di = 1 - Sp; Mn += di; Sn += di; } } else { if (Sn >= 0) { di = Sn + 1; Mn += di; Sn += -di; } } } if (Mp < Mn) { cout << Mp; } else { cout << Mn; } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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 itertools import accumulate def main(): n = int(input()) a = list(map(int, input().split())) a = list(accumulate(a)) ans = 10**18 diff = [None, None]# a[0]<0, a[0]>0それぞれの初期コスト for i in range(2): if a[0] * [-1,1][i] < 0: diff[i] = 0 else: diff[i] = [-1,1][i] * (abs(a[0])+1) for d in diff: ans2 = abs(d) for i in range(1,n): p = a[i] + d q = a[i-1] + d if p * q >= 0: tmp = -q//abs(q) - p ans2 += abs(tmp) d += tmp ans = min(ans, ans2) print(ans) if __name__ == "__main__": main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n; long long ans, ans2; long long a[100001]; long long solve(long long sum) { for (int i = 1; i < n; i++) { if (sum > 0 && sum + a[i] >= 0) { ans += sum + a[i] + 1; sum = -1; } else if (sum < 0 && sum + a[i] <= 0) { ans += 1 - sum - a[i]; sum = 1; } else sum += a[i]; } return ans; } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; if (a[0] == 0) ans = min(solve(1), solve(-1)) + 1; else ans = solve(a[0]); 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
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long ns[] = new long[n]; for (int i = 0; i < n; i++) { ns[i] = sc.nextLong(); } long sum = ns[0]; long ans = 0; boolean isNegative = ns[0] < 0; int j; for (j = 0; j < n -1; j++) { if (ns[j] != ns[j+1]) { isNegative = ns[j] < ns[j+1]; break; } } if (ns[0] == 0) { if (j % 2 != 0) isNegative = !isNegative; if (isNegative) { sum = -1; } else sum = 1; ans++; } for (int i = 1; i < n; i++) { sum += ns[i]; if (isNegative && sum < 0) { ans -= sum - 1; sum = 1; } else if (!isNegative && sum > 0) { ans += sum + 1; sum = -1; } else if (sum == 0) { ans++; if (isNegative) sum = 1; else sum = -1; } isNegative = !isNegative; } System.out.println(ans); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> v; long long sum = 0; long long ans1 = 0; long long op; for (int i = 0; i < n; i++) { long long k; cin >> k; sum += k; if (i == 0) op = k / max(k, -k); else { op *= -1; if (sum / op <= 0) { ans1 += max(sum, -sum) + 1; sum = op; } } v.push_back(k); } sum = 0; long long ans2 = 0; for (int i = 0; i < n; i++) { sum += v[i]; if (i == 0) op = v[i] / max(v[i], -v[i]); op *= -1; if (sum / op <= 0) { ans2 += max(sum, -sum) + 1; sum = op; } } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(int,input().split())) sum=a[0] op=0 for i in a[1:]: if(sum*(sum+i)>=0): op+=abs(sum+i)+1 if(sum<0):sum=1 else:sum=-1 else:sum+=i 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, count = 0; cin >> N; vector<int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; int su = A[0]; for (int i = 1; i < N; i++) { while (((su > 0) == (su + A[i] > 0)) || su + A[i] == 0) { if (su + A[i] == 0) { if (su > 0) A[i]--; else A[i]++; } else if (su + A[i] > 0) { A[i]--; } else { A[i]++; } count++; } su += A[i]; cout << su << ' '; } cout << endl << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; int main() { int n; cin >> n; vector<long long> v(n); for (int i = 0; i < n; ++i) { cin >> v[i]; } long long cnt_a = 0; long long sum_a = 0; for (int i = 0; i < n; ++i) { sum_a += v[i]; if (i % 2 == 0) { if (sum_a >= 0) { sum_a = -1; cnt_a += sum_a + 1; } } else { if (sum_a <= 0) { sum_a = 1; cnt_a += abs(sum_a) + 1; } } } long long cnt_b = 0; long long sum_b = 0; for (int i = 0; i < n; ++i) { sum_b += v[i]; if (i % 2 == 0) { if (sum_b <= 0) { sum_b = 1; cnt_b += abs(sum_b) + 1; } } else { if (sum_b >= 0) { sum_b = 1; cnt_b += abs(sum_b) + 1; } } } cout << min(cnt_a, cnt_b) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n), b(n + 1, 0); for (int i = 0; i < n; i++) { cin >> a[i]; } int sum = 0; int ans1 = 0; for (int i = 0, s = 1; i < n; i++, s *= -1) { sum += a[i]; if (sum * s <= 0) { ans1 += abs(sum - s); sum = s; } } sum = 0; int ans2 = 0; for (int i = 0, s = -1; i < n; i++, s *= -1) { sum += a[i]; if (sum * s <= 0) { ans2 += abs(sum - s); sum = s; } } 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; const long long INF = 10000000000; const long long MOD = 1000000007; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int n; cin >> n; long long a[100100]; for (int i = 0; i < n; ++i) { cin >> a[i]; } long long ans = INF; for (int i = 0; i < 2; ++i) { long long count = 0; long long su = 0; for (int j = 0; j < n; ++j) { su += a[j]; if (i == 0) { if (j % 2 == 1 && su <= 0) { count += -su + 1; su = 1; } else if (j % 2 == 0 && su >= 0) { count += su + 1; su = -1; } } else { if (j % 2 == 1 && su >= 0) { count += su + 1; su = -1; } else if (j % 2 == 0 && su <= 0) { count += -su + 1; su = 1; } } } ans = min(ans, count); } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; long long sum = 0; long long ans = 0; for (int i = 0; i < n; i++) { int x; cin >> x; if (i == 0) { sum += x; continue; } long long tmp = sum; sum += x; if (tmp * sum < 0) continue; if (tmp < 0) { ans += 1 - sum; sum = 1; } else { ans += sum + 1; sum = -1; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const double pie = acos(-1.0); template <typename T> T Max(T x, T y) { return (x > y) ? x : y; } template <typename T> T Min(T x, T y) { return (x > y) ? y : x; } int gcd(int n1, int n2) { if (n2 != 0) return gcd(n2, n1 % n2); else return n1; } template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } clock_t time_p = clock(); void rtime() { time_p = clock() - time_p; cerr << "Time Taken : " << (float)1000 * (time_p) / CLOCKS_PER_SEC << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; int arr[n]; for (int i = 0; i < int(n); i++) cin >> arr[i]; long long int ans = 0; long long int sum = arr[0]; for (int i = 1; i < n; i++) { if (sum < 0) { sum = sum + arr[i]; if (sum > 0) continue; else { ans = ans + abs(sum) + 1; sum = 1; } } else { sum = sum + arr[i]; if (sum < 0) continue; else { ans = ans + sum + 1; sum = -1; } } } cout << ans; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long n; scanf("%ld", &n); vector<long> a(n); for (long i = 0; i < n; i++) scanf(" %ld", &a[i]); long sum = a[0]; long j = 0; for (long i = 1; i < n; i++) { if (sum * (sum + a[i]) < 0) sum += a[i]; else { j += abs(sum + a[i]) + 1; if (sum < 0) sum = 1; else if (sum > 0) sum = -1; } } printf("%ld\n", sum); 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())) def judge_pm(a,b): if a*b<0: return True else: return False tmp_sum = a[0] operate_num = 0 for i in range(1, n): if judge_pm(tmp_sum, tmp_sum+a[i]): pass elif tmp_sum<0: tmp_operate_num = - tmp_sum + 1 - a[i] operate_num += tmp_operate_num a[i] += tmp_operate_num else: tmp_operate_num = tmp_sum + 1 + a[i] operate_num += tmp_operate_num a[i] -= tmp_operate_num tmp_sum += a[i] print(operate_num)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> 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; } int main() { cin.tie(0); ios::sync_with_stdio(false); long long N; cin >> N; long long a[N]; long long sum = 0, cnt = 0; for (long long i = 0; i < N; i++) { cin >> a[i]; if (i == 0) { sum += a[i]; continue; } if (sum > 0 && sum + a[i] > 0) { cnt += sum + a[i] + 1; sum = -1; } else if (sum < 0 && sum + a[i] < 0) { cnt += abs(sum + a[i]) + 1; sum = 1; } else if (sum + a[i] == 0) { if (a[i] >= 0) { sum++; cnt++; } else { sum--; cnt++; } } else sum += a[i]; } cout << cnt << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
use std::io::prelude::*; fn input<T>() -> T where T: std::str::FromStr, { let stdin = std::io::stdin(); let token: String = stdin .lock() .bytes() .map(|c| c.unwrap() as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect(); token.parse().ok().unwrap() } fn main() { let n: usize = input(); let a: Vec<i64> = (0..n).map(|_| input()).collect(); let sum: Vec<i64> = a .iter() .scan(0, |acc, a| { *acc += a; Some(*acc) }) .collect(); let mut x = 0; let mut ans = 0; for i in 1..n { if (sum[i - 1] + x > 0 && sum[i] + x < 0) || (sum[i - 1] + x < 0 && sum[i] + x > 0) { continue; } if sum[i - 1] + x > 0 { ans += sum[i] + x + 1; x -= sum[i] + x + 1; } else { ans += -sum[i] - x + 1; x -= sum[i] + x - 1; } } println!("{}", ans); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::<Vec<char>>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } fn solve(as_: Vec<i64>) -> i64 { let mut sum = 0; let mut count = 0; if as_[0] == 0 { for i in 1..as_.len() as i64{ if as_[i as usize].is_negative() { if i % 2 == 0 { sum = -1; count += 1; break; } else { sum = 1; count += 1; break; } } if as_[i as usize].is_positive() { if i % 2 == 0 { sum = 1; count += 1; break; } else { sum = -1; count += 1; break; } } } } else { sum = as_[0]; } if sum == 0 { sum += 1; count += 1; } for i in 1..as_.len() { let cur = as_[i]; if sum > 0 { if cur + sum < 0 { sum += cur; } else { let exp = -1 - sum; count += cur - exp; sum = -1; } } else if sum < 0 { if cur + sum > 0 { sum += cur; } else { let exp = 1 - sum; count += exp - cur; sum = 1; } } } count } fn main() { input!{ n: usize, as_: [i64; n], } println!("{}", solve(as_)); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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 afify() { ios::sync_with_stdio(0); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const long long oo = (long long)1e13; const double EPS = 1e-4; vector<long long> v; int arr[100005]; int main() { afify(); int n; cin >> n; v.resize(n); for (int i = int(0); i < n; i++) cin >> v[i]; long long sum = v[0], res = 0, cnt = 0; arr[0] = sum; for (int i = int(1); i < n; i++) { if (sum + v[i] == 0) { v[i]++; res++; } if (i + 1 < n && sum + v[i] == sum + v[i] + v[i + 1]) { v[i + 1]++; res++; } sum += v[i]; arr[i] = sum; cnt++; if (i == n - 1) { i = 0; sum = v[0]; } if (cnt == 100000) { break; } } for (int i = int(0); i < n - 1; i++) { if (arr[i] == arr[i + 1]) { res++; arr[i + 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() { int n; cin >> n; vector<int> an(n); for (int i = 0; i < n; ++i) { cin >> an[i]; } int cnt_min = INT_MAX; for (int j = 0; j < 2; ++j) { int sign = j == 0 ? -1 : 1; int accum = an[0]; int cnt = 0; if (accum * sign <= 0) { auto x = sign - accum; accum += x; cnt += abs(x); } for (int i = 1; i < n; ++i) { auto new_accum = accum + an[i]; if (new_accum * accum >= 0) { int x = -sign - new_accum; new_accum += x; cnt += abs(x); } int new_sign = new_accum > 0 ? 1 : -1; accum = new_accum; assert(new_sign == -sign); sign = new_sign; } if (cnt < cnt_min) { cout << endl; cnt_min = cnt; } } cout << cnt_min << 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; struct fastio { fastio() { ios::sync_with_stdio(false); cout << setprecision(10) << fixed; cin.tie(0); } }; fastio _fast_io; const int N = 1e5 + 5; int n; int a[N]; long long int add[N]; long long int sum, ans; int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } sum = a[0]; for (int i = 1; i < n; ++i) { long long int nsum = sum + a[i]; if (sum < 0) { if (nsum <= 0) { ans += 1 - nsum; nsum = 1; } } else { if (nsum >= 0) { ans += nsum + 1; nsum = -1; } } sum = nsum; } 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()) time = list(map(int, input().split())) ans = 0 sumTime = time[0] ''' if (time[0] < 0): before = False else: before = True ''' for i in range(1, n): temp = time[i] + sumTime check = temp * sumTime if (check < 0): sumTime = temp else: dif = abs(temp) + 1 ans += dif if (sumTime < 0): sumTime = 1 else: sumTime = -1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long linf = 1001002003004005006ll; const int inf = 1001001001; const int mod = 1000000007; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<long long> a(n); for (int i = 0; i < (n); ++i) cin >> a[i]; long long tot = 0; long long res1 = 0; { for (int i = 0; i < (n); ++i) { if (i % 2 == 0) { if (tot + a[i] > 0) tot += a[i]; else { res1 += 1 - tot - a[i]; tot = 1; } } else { if (tot + a[i] < 0) tot += a[i]; else { res1 += 1 + tot + a[i]; tot = -1; } } } } tot = 0; long long res2 = 0; { for (int i = 0; i < (n); ++i) { if (i % 2 != 0) { if (tot + a[i] > 0) tot += a[i]; else { res2 += 1 - tot - a[i]; tot = 1; } } else { if (tot + a[i] < 0) tot += a[i]; else { res2 += 1 + tot + a[i]; tot = -1; } } } } int ans = min(res1, res2); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int signs[2] = {-1, 1}; long long cnt[2] = {0, 0}; for (int i = 0; i < 2; i++) { long long sum = 0; int sign = signs[i]; for (int j = 0; j < n; j++) { sum += a[j]; if (sum == 0) { sum += sign; cnt[i]++; } else if (sum * sign < 0) { sum = sum + sum * (-1) + sign; } sign *= -1; } } cout << (cnt[0] < cnt[1] ? cnt[0] : cnt[1]) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) prv_total =0 cnt = 0 for i in range(n-1): total = prv_total + a[i] nxt_total = total+a[i+1] if total > 0 and nxt_total >= 0: a[i+1] -= nxt_total+1 cnt += nxt_total+1 nxt_total -= nxt_total+1 elif total < 0 and nxt_total <=0: a[i+1] += abs(nxt_total)+1 cnt += abs(nxt_total)+1 nxt_total += abs(nxt_total)+1 prv_total = total total = prv_total + a[-1] if total == 0: cnt += 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 = list(map(int,input().split())) a = [A,A] res = [0,0] sum = 0 for check in range(2): sum = 0 if check: if a[check][0] > 0: temp = -1 - a[check][0] a[check][0] += temp res[check] += temp * -1 elif a[check][0] < 0: temp = 1 - a[check][0] a[check][0] += temp res[check] += temp if a[check][0] == 0: if check == 0: a[check][0] += 1 else: a[check][0] -= 1 res[check] += 1 for i in range(n-1): sum += a[check][i] if sum * (sum + a[check][i+1]) >= 0: if sum > 0: temp = -1 - sum - a[check][i+1] a[check][i+1] += temp res[check] += temp * -1 else: temp = 1 - sum - a[check][i+1] a[check][i+1] += temp res[check] += temp print(min(res[0],res[1]))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(NULL); ios::sync_with_stdio(false); int n; cin >> n; long long a[n]; long long sum = 0; long long count = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (i == 0) { sum = a[i]; if ((sum == 0) && (i < n - 1)) { if (a[i + 1] < 0) { sum = 1; } else { sum = -1; } count += 1; } } else { if (sum < 0) { if (sum + a[i] <= 0) { count += 1 - (sum + a[i]); sum = 1; } else { sum = sum + a[i]; } } else if (sum > 0) { if (sum + a[i] >= 0) { count += sum + a[i] - (-1); sum = -1; } else { sum = sum + a[i]; } } } } cout << count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 0x7fffffff; const int maxn = 1e5 + 10; int a[maxn]; int n; long long cal() { long long t = a[0], ans = 0; for (int i = 1; i < n; ++i) { if (t < 0) { t += a[i]; if (t <= 0) { ans += 1 - t; t = 1; } continue; } t += a[i]; if (t >= 0) { ans += t + 1; t = -1; } } return ans; } int main() { scanf("%d", &n); for (int i = 0; i < (n); ++i) { scanf("%d", &a[i]); } long long ans1 = 0, ans2 = 0, ans3 = 0, ans = 0; int t = a[0]; if (t == 0) { a[0] = 1; ++ans1; ans1 = cal(); a[0] = -1; ++ans2; ans2 = cal(); ans = min(ans1, ans2); } else { ans1 = cal(); a[0] = 1; ans2 += abs(1 - t); ans2 = cal(); a[0] = -1; ans3 += abs(-1 - t); ans3 = cal(); ans = min(ans1, min(ans2, ans3)); } 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() { long long n; cin >> n; vector<long long> a(n); vector<long long> as(n + 1); vector<long long> asb(n + 1); for (int i = 0; i < n; i++) { cin >> a[i]; } as[0] = a[0]; for (int i = 0; i < n - 1; i++) { as[i + 1] = as[i] + a[i + 1]; } long long ans = 99999999999999; long long op = 0; long long bal = 0; long long diff = 0; for (int i = 0; i < n; i++) { diff = as[i] + bal; if (i % 2 == 0 && diff >= 0) { op += diff + 1; bal -= diff + 1; } else if (i % 2 == 1 && diff <= 0) { op += diff + 1; bal += diff + 1; } } if (op > 0) { ans = min(ans, op); } for (int i = 0; i < n; i++) { diff = as[i] + bal; if (i % 2 == 1 && diff >= 0) { op += diff + 1; bal -= diff + 1; } else if (i % 2 == 0 && diff <= 0) { op += diff + 1; bal += diff + 1; } } if (op > 0) { ans = min(ans, op); } 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; bool dif(int a, int b) { if (a < 0 && b > 0) return true; if (a > 0 && b < 0) return true; return false; } int odd(vector<int> v, vector<int> &w) { int ans = 0; if (v[0] <= 0) while (++v[0] != 1) ; int sum = v[0]; ans = abs(v[0] - w[0]); for (int i = 1; i < v.size(); i++) { if (dif(sum, sum + v[i])) { sum += v[i]; } else { if (sum > 0) { v[i] = -1 - sum; } else if (sum < 0) { v[i] = 1 - sum; } sum += v[i]; } ans += abs(v[i] - w[i]); } return ans; } int even(vector<int> v, vector<int> &w) { int ans = 0; if (v[0] >= 0) while (--v[0] != -1) ; int sum = v[0]; ans = abs(v[0] - w[0]); for (int i = 1; i < v.size(); i++) { if (dif(sum, sum + v[i])) { sum += v[i]; } else { if (sum > 0) { v[i] = -1 - sum; } else if (sum < 0) { v[i] = 1 - sum; } sum += v[i]; } ans += abs(v[i] - w[i]); } return ans; } int main() { int n; cin >> n; vector<int> v(n), cpy; for (int &i : v) cin >> i; cpy = v; int ans = min(odd(v, cpy), even(v, cpy)); 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
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 cnt, tmp = 0, sum = a.at(0), x; for (int j = 0; j < 2; j++) { cnt = 0; if (j == 1) { cnt = abs(a.at(0)) + 1; if (a.at(0) > 0) a.at(0) = -1; else a.at(0) = 1; sum = a.at(0); } if (a.at(0) >= 0) for (int i = 1; i < n; i++) { x = 0; if (i % 2 == 1) { if (a.at(i) >= 0 || sum + a.at(i) >= 0) x = -1 - a.at(i) - sum; } else { if (a.at(i) < 0 || sum + a.at(i) <= 0) x = 1 - a.at(i) - sum; } cnt += abs(x); sum += a.at(i) + x; } else { for (int i = 1; i < n; i++) { x = 0; if (i % 2 == 1) { if (a.at(i) <= 0 || sum + a.at(i) <= 0) x = 1 - a.at(i) - sum; } else { if (a.at(i) > 0 || sum + a.at(i) >= 0) x = -1 - a.at(i) - sum; } cnt += abs(x); sum += a.at(i) + x; } } if (j == 0) tmp = cnt; if (tmp > cnt) tmp = cnt; } cout << tmp << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[100010]; long long sum = 0, cnt = 0, sum2 = 0, cnt2 = 0; for (int i = 0; i < n; i++) cin >> a[i]; bool nextpo = false, nextpo2 = false; bool nextne = false, nextne2 = false; sum = a[0]; sum2 = a[0]; if (sum < 0) { nextpo = true; } else if (sum > 0) { nextne = true; } if (sum2 < 0) { nextne2 = true; cnt2 += abs(sum2) + 1; sum2 += abs(sum2) + 1; } else if (sum2 > 0) { nextpo2 = true; cnt2 += sum2 + 1; sum2 -= sum2 + 1; } for (int i = 1; i < n; i++) { if (nextpo) { nextpo = false; nextne = true; sum += a[i]; if (sum == 0) { sum++; cnt++; } else if (sum < 0) { cnt += abs(sum) + 1; sum += abs(sum) + 1; } } else if (nextne) { nextpo = true; nextne = false; sum += a[i]; if (sum == 0) { sum--; cnt++; } else if (sum > 0) { cnt += sum + 1; sum -= sum + 1; } } } for (int i = 1; i < n; i++) { if (nextpo2) { nextpo2 = false; nextne2 = true; sum2 += a[i]; if (sum2 == 0) { sum2++; cnt2++; } else if (sum2 < 0) { cnt2 += abs(sum2) + 1; sum2 += abs(sum2) + 1; } } else if (nextne2) { nextpo2 = true; nextne2 = false; sum2 += a[i]; if (sum2 == 0) { sum2--; cnt2++; } else if (sum2 > 0) { cnt2 += sum2 + 1; sum2 -= sum2 + 1; } } } cout << min(cnt, cnt2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int solve(vector<int> vec) { long long int n = vec.size(); long long int sum = vec[0] + vec[1]; int ans = 0; for (long long int i = 2; i < n; i++) { if (sum > 0) { if (sum + vec[i] >= 0) { ans += 1 + (sum + vec[i]); sum = -1; } else { sum += vec[i]; } } else if (sum <= 0) { if (sum + vec[i] <= 0) { ans += 1 - (sum + vec[i]); sum = 1; } else { sum += vec[i]; } } } return ans; } int main() { int n, Ans; cin >> n; vector<int> as; for (int i = 0; i < n; i++) { int t; cin >> t; as.push_back(t); } vector<int> as1, as2; copy(as.begin(), as.end(), back_inserter(as1)); copy(as.begin(), as.end(), back_inserter(as2)); as1[0] = 1; as2[0] = -1; Ans = min(solve(as), min(solve(as1) + abs(1 - as[0]), solve(as2) + abs(-1 - as[0]))); cout << Ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int a[100005]; int main() { int n, sum, num = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } sum = a[0]; bool flag; if (a[0] < 0) flag = true; else flag = false; for (int i = 1; i < n; i++) { sum += a[i]; if (flag) { flag = !flag; if (sum > 0) continue; else if (sum == 0) num += 1, sum = 1; else num += 1 - sum, sum = 1; } else { flag = !flag; if (sum < 0) continue; else if (sum == 0) num += 1, sum = -1; else num += sum + 1, sum = -1; } } cout << num << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T, template <typename ELEM, typename ALLOC = std::allocator<ELEM> > class Container> std::ostream& operator<<(std::ostream& o, const Container<T>& container) { typename Container<T>::const_iterator beg = container.begin(); while (beg != container.end()) o << " " << *beg++; return o; } int n, ans; long long int d; vector<long long int> v, a; int main(int argc, char const* argv[]) { ios_base::sync_with_stdio(false); cin >> n; v.push_back(0); a.push_back(0); for (int i = 1; i <= n; i++) { cin >> d; a.push_back(d); v.push_back(v[i - 1] + d); } a.push_back(0); a.push_back(0); if (a[1] != 0) { int ans = 0; for (int i = 2; i <= n; i++) { if (v[i - 1] > 0 && (v[i - 1] + a[i]) >= 0) { ans = ans + v[i - 1] + a[i] + 1; v[i] = -1; } else if (v[i - 1] < 0 && (v[i - 1] + a[i]) <= 0) { ans = ans + 1 - (v[i - 1] + a[i]); v[i] = 1; } else if (i < n && v[i - 1] > 0 && (v[i - 1] + a[i] < 0) && (v[i - 1] + a[i] + a[i + 1] < 0)) { ans = ans - (v[i - 1] + a[i]) - 1; v[i] = -1; } else if (i < n && v[i - 1] < 0 && (v[i - 1] + a[i] > 0) && (v[i - 1] + a[i] + a[i + 1] > 0)) { ans = ans + v[i - 1] + a[i] - 1; v[i] = 1; } else v[i] = v[i - 1] + a[i]; } cout << ans; } else { int ans1 = 0; int ans2 = 0; a[1] = 1; for (int i = 2; i <= n; i++) { if (v[i - 1] > 0 && (v[i - 1] + a[i]) >= 0) { ans1 = ans1 + v[i - 1] + a[i] + 1; v[i] = -1; } else if (v[i - 1] < 0 && (v[i - 1] + a[i]) <= 0) { ans1 = ans1 + 1 - (v[i - 1] + a[i]); v[i] = 1; } else if (i < n && v[i - 1] > 0 && (v[i - 1] + a[i] < 0) && (v[i - 1] + a[i] + a[i + 1] < 0)) { ans1 = ans1 - (v[i - 1] + a[i]) - 1; v[i] = -1; } else if (i < n && v[i - 1] < 0 && (v[i - 1] + a[i] > 0) && (v[i - 1] + a[i] + a[i + 1] > 0)) { ans1 = ans1 + v[i - 1] + a[i] - 1; v[i] = 1; } else v[i] = v[i - 1] + a[i]; } a[1] = -1; for (int i = 2; i <= n; i++) { if (v[i - 1] > 0 && (v[i - 1] + a[i]) >= 0) { ans2 = ans2 + v[i - 1] + a[i] + 1; v[i] = -1; } else if (v[i - 1] < 0 && (v[i - 1] + a[i]) <= 0) { ans2 = ans2 + 1 - (v[i - 1] + a[i]); v[i] = 1; } else if (i < n && v[i - 1] > 0 && (v[i - 1] + a[i] < 0) && (v[i - 1] + a[i] + a[i + 1] < 0)) { ans2 = ans2 - (v[i - 1] + a[i]) - 1; v[i] = -1; } else if (i < n && v[i - 1] < 0 && (v[i - 1] + a[i] > 0) && (v[i - 1] + a[i] + a[i + 1] > 0)) { ans2 = ans2 + v[i - 1] + a[i] - 1; v[i] = 1; } else v[i] = v[i - 1] + a[i]; } cout << min(ans1, ans2); } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
import std.stdio, std.conv, std.algorithm, std.range, std.array, std.string, std.uni, std.bigint, std.math; void main() { auto n = readln.chomp.to!uint; auto an = readln.split.to!(int[]); auto sum = 0; auto cnt = 0; foreach (a; an) { if (sum != 0 && (sum + a) * sum >= 0) { auto na = -sum - sgn(sum); cnt += abs(na - a); a = na; } sum += a; } writeln(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; unsigned int manipulation(vector<int>& a) { unsigned int m = 0; if (a[0] == 0) { if (a[1] <= 0) { a[0] = 1; m++; } else { a[0] = -1; m++; } } int sum = a[0]; bool is_sum_above_0 = sum > 0; for (unsigned int ii = 1; ii < a.size(); ++ii) { sum += a[ii]; if (is_sum_above_0) { while (sum >= 0) { m++; sum--; } } else { while (sum <= 0) { m++; sum++; } } is_sum_above_0 = !is_sum_above_0; } return m; } int main() { unsigned int n; cin >> n; vector<int> a(n); for (unsigned int ii = 0; ii < n; ++ii) cin >> a[ii]; cout << manipulation(a) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) s_pos = 0 s_neg = 0 # start neg val cum = 0 for i in range(n): cum += a[i] if i % 2 == 0 and cum >= 0: s_neg += abs(cum) + 1 cum = -1 if i % 2 != 0 and cum <= 0: s_neg += abs(cum) + 1 cum = 1 # start pos val cum = 0 for i in range(n): cum += a[i] if i % 2 == 0 and cum <= 0: s_pos += abs(cum) + 1 cum = -1 if i % 2 != 0 and cum >= 0: s_pos += abs(cum) + 1 cum = 1 ans = min(s_pos, s_neg) 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 = [int(i) for i in input().split()] c = 10**15 for i in range(2): A = [-a for a in A] if A[0] != 0: ans = 0 S = A[0] f = A[0]//abs(A[0]) else: ans = 1 S = 1 f = 1 for a in A[1:]: S += a if S == 0: ans += 1 S = -f else: if S/abs(S) != f*(-1): ans += abs(S)+1 S = -f f *= -1 c = min(ans, c) 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()) a = list(map(int, input().split())) count = 0 sum_ = 0 for i in range(n): if sum_ * (sum_+a[i]) <0 or i == 0: sum_ += a[i] elif sum_ > 0: count += sum_+a[i]+1 a[i] = -sum_-1 sum_ += a[i] elif sum_ < 0: count += abs(sum_+a[i])+1 a[i] = -sum_-1 sum_ += a[i] print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> a(n), dp1(n), dp2(n); for (long long i = (0); i < (long long)(n); i++) cin >> a[i]; long long ans1 = 0, ans2 = 0; if (a[0] > 0) { ans2 = a[0] - (-1); dp1[0] = a[0]; dp2[0] = -1; } else if (a[0] == 0) { ans1 = 1; ans2 = 1; dp1[0] = 1; dp1[0] = -1; } else { ans1 = 1 - a[0]; dp1[0] = 1; dp2[0] = a[0]; } for (long long i = (1); i < (long long)(n); i++) { if (dp1[i - 1] < 0) { if (dp1[i - 1] + a[i] > 0) { dp1[i] = dp1[i - 1] + a[i]; } else if (dp1[i - 1] + a[i] == 0) { ans1 += 1; dp1[i] = 1; } else { dp1[i] = 1; ans1 += 1 - (dp1[i - 1] + a[i]); } } else { if (dp1[i - 1] + a[i] < 0) { dp1[i] = dp1[i - 1] + a[i]; } else if (dp1[i - 1] + a[i] == 0) { ans1 += 1; dp1[i] = -1; } else { dp1[i] = -1; ans1 += (dp1[i - 1] + a[i]) - (-1); } } if (dp2[i - 1] < 0) { if (dp2[i - 1] + a[i] > 0) { dp2[i] = dp2[i - 1] + a[i]; } else if (dp2[i - 1] + a[i] == 0) { ans2 += 1; dp2[i] = 1; } else { dp2[i] = 1; ans2 += 1 - (dp2[i - 1] + a[i]); } } else { if (dp2[i - 1] + a[i] < 0) { dp2[i] = dp2[i - 1] + a[i]; } else if (dp2[i - 1] + a[i] == 0) { ans2 += 1; dp2[i] = -1; } else { dp2[i] = -1; ans2 += (dp2[i - 1] + a[i]) - (-1); } } } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; long long ans = 0; long long ans2 = 0; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } long long dp[10010]; dp[0] = 0; bool flag = true; for (int i = 0; i < n; i++) { if (flag == true && dp[i] + a[i] >= 0) { dp[i + 1] = -1; ans += abs(a[i] - (-1 - dp[i])); } else if (flag == false && dp[i] + a[i] <= 0) { dp[i + 1] = 1; ans += abs(a[i] - (1 - dp[i])); } else { dp[i + 1] = dp[i] + a[i]; } flag = !flag; } flag = false; for (int i = 0; i < n; i++) { if (flag == true && dp[i] + a[i] >= 0) { dp[i + 1] = -1; ans2 += abs(a[i] - (-1 - dp[i])); } else if (flag == false && dp[i] + a[i] <= 0) { dp[i + 1] = 1; ans2 += abs(a[i] - (1 - dp[i])); } else { dp[i + 1] = dp[i] + a[i]; } flag = !flag; } cout << min(ans, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def main(): n = int(input()) A = list(map(int, input().split())) res = 0 sums = [] for i in range(n): sums.append(sum(A[:i]) + A[i]) if i == 0: if A[i] == 0: index = -1 for num in A: if num != 0: index = A.index(num) break if index == -1: res += 1 A[i] = 1 sums[i] = 1 elif (index % 2 and A[index] > 0) or (index % 2 == 0 and A[index] < 0): A[i] = -1 sums[i] = -1 res += 1 else: A[i] = 1 sums[i] = 1 res += 1 else: if sums[i] == 0: if sums[i-1] > 0: sums[i] -= 1 A[i] -= 1 res += 1 else: sums[i] += 1 A[i] += 1 res += 1 elif (sums[i-1] > 0) and (sums[i] > 0): res += sums[i] + 1 A[i] -= sums[i] + 1 sums[i] -= sums[i] + 1 elif (sums[i-1] < 0) and (sums[i] < 0): res += abs(sums[i]) + 1 A[i] += abs(sums[i]) + 1 sums[i] += abs(sums[i]) + 1 print(res) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int n; cin >> n; vector<int> a(n); for (int i = (int)(0); i < (int)(n); i++) cin >> a[i]; int ans = 0; int sum = a[0]; for (int i = (int)(0); i < (int)(n - 1); i++) { if (sum < 0) { if (a[i + 1] < abs(sum)) { ans += abs(sum) - a[i + 1] + 1; a[i + 1] += abs(sum) - a[i + 1] + 1; } sum += a[i + 1]; } else if (sum > 0) { if (a[i + 1] > -sum) { ans += a[i + 1] + sum + 1; a[i + 1] -= a[i + 1] + sum + 1; } sum += a[i + 1]; } cout << ans << endl; } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, a; int ans; int bef; int main() { cin >> n; cin >> a; bef = a; for (int i = 1; i < n; i++) { cin >> a; if (bef > 0) { if (bef + a < 0) { bef += a; continue; } ans += bef + a + 1; bef = -1; } else { if (bef + a > 0) { bef += a; continue; } ans -= bef + a - 1; bef = 1; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) A = list(map(int,input().split())) def calc(A): ans = 0 s = A[0] if s > 0: flag = 1 elif s < 0: flag = -1 for i in range(1,N): s += A[i] if flag == 1 and s >= 0: ans += s + 1 s = -1 elif flag == -1 and s <= 0: ans += 1 - s s = 1 flag *= -1 return ans if A[0] != 0: print(calc(A)) else: print(min(calc([1]+A[1:]),calc([-1]+A[1:]))+1)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<bits/stdc++.h> using namespace std; #define ll long long; LL ans1,ans2,sum; int n; int a[100010]; int main(){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); sum=0; for(int i=1,s=1;i<=n;i++,s*=-1){ sum+=a[i]; if(sum*s<=0) ans1+=abs(sum-s),sum=s; } sum=0; for(int i=1,s=-1;i<=n;i++,s*=-1){ sum+=a[i]; if(sum*s<=0) ans2+=abs(sum-s),sum=s; } printf("%lld\n",min(ans1,ans2)); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int checkSign(int A) { return (int)(A > 0) - (int)(A < 0); } int main() { int N; cin >> N; vector<int> a(N); for (int i = 0; i < N; i++) { cin >> a.at(i); } int res = 0; if (a.at(0) == 0) { a.at(0)++; res++; } long long sum = a.at(0); int sign = -a.at(0) / abs(a.at(0)); for (int i = 1; i < N; i++) { if (checkSign(sum + a.at(i)) == 0 || checkSign(sum + a.at(i)) == checkSign(sum)) { int tmp = sign * (abs(sum) + 1); res += abs(tmp - a.at(i)); a.at(i) = tmp; } sum += a.at(i); sign *= -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; long long a[1000000]; int min(int a, int b) { int t = a; if (b <= t) t = b; return t; } int main() { int n; cin >> n; for (int t = 0; t < n; t++) cin >> a[t]; int sum = 0; long long x = 0; for (int t = 0; t < n; t++) { sum += a[t]; if (t % 2 == 1 && sum >= 0) { long long s = sum + 1; sum = -1; x += s; } else if (t % 2 == 0 && sum <= 0) { long s = 1 - sum; sum = 1; x += s; } } int positive_x = x; x = 0; sum = 0; for (int t = 0; t < n; t++) { sum += a[t]; if (t % 2 == 0 && sum >= 0) { int s = sum + 1; sum = -1; x += s; } else if (t % 2 == 1 && sum <= 0) { int s = 1 - sum; sum = 1; x += s; } } int negative_x = x; int result = min(positive_x, negative_x); cout << result << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
from collections import Counter N = int(input()) A = list(map(int, input().split()))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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) { vector<int> v; int res = 0; int sign = 0; int n, t; int sum = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> t; v.push_back(t); } sign = 0; for (int i = 0; i < v.size(); i++) { sum += v[i]; if (sign == 0) { if (sum >= 0) { res += (sum + 1); sum = -1; } } else { if (sum <= 0) { res += (abs(sum) + 1); sum = 1; } } sign = 1 - sign; } t = 0; sign = 1; sum = 0; for (int i = 0; i < v.size(); i++) { sum += v[i]; if (sign == 0) { if (sum >= 0) { t += (sum + 1); sum = -1; } } else { if (sum <= 0) { t += (abs(sum) + 1); sum = 1; } } sign = 1 - sign; } res = min(res, t); 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
python3
n=int(input()) a=list(map(int,input().split())) b=[] for i in range(n): b.append(a[i]) ct1=0 if a[0]<=0: a[0]=1 ct1+=1-a[0] x=a[0] for i in range(1,n): y=x+a[i] if i%2==1: if y>=0: ct1+=y+1 a[i]=-x-1 else: if y<=0: ct1+=1-y a[i]=-x+1 x+=a[i] ct2=0 if b[0]>=0: b[0]=-1 ct2+=b[0]-1 z=b[0] for i in range(1,n): w=z+b[i] if i%2==0: if w>=0: ct2+=w+1 b[i]=-z-1 else: if w<=0: ct2+=1-w b[i]=-z+1 z+=b[i] print(min(ct1,ct2))