Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long N, i, sum, count; cin >> N; vector<long> input(N); for (i = 0; i < N; i++) { cin >> input.at(i); } sum = 0; count = 0; sum = input.at(0); if (input.at(0) >= 0) { for (i = 1; i < N; i++) { if (i % 2 == 1) { sum += input.at(i); if (sum >= 0) { while (sum >= 0) { sum--; count++; } } } if (i % 2 == 0) { sum += input.at(i); if (sum < 0) { while (sum <= 0) { sum++; count++; } } } } } if (input.at(0) < 0) { for (i = 1; i < N; i++) { if (i % 2 == 1) { sum += input.at(i); if (sum <= 0) { while (sum <= 0) { sum++; count++; } } } if (i % 2 == 0) { sum += input.at(i); if (sum >= 0) { while (sum >= 0) { sum--; count++; } } } } } cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <stdio.h> #include<math.h> int main(void) { // your code goes here long int a[100000]; long int n,count= 0,count1=0; scanf("%d",&n); int i; for(i = 0;i<n;i++) { scanf("%ld",&a[i]); }; long int new = a[0]+a[1]; if(new==0) { new = -1; count++;} int pos; if(new <0||new == 0) pos= 0; else if(new>0) pos = 1; for(i = 2;i<n;i++) { new = a[i]+new; // printf("%d",new); pos = !pos; if(new>0&& pos==0) {count+=abs(new)+1; new= -1;} else if(new<0 && pos ) {count+=abs(new)+1; new = 1; } else if(new ==0) { count++; if(pos==0) new = -1; else new = 1; } } count1+=abs(a[0]+a[1])+1; if(a[0]+a[1]<0 ||a[0]+a[1]==0) {new = 1; pos = 1;} else if(a[0]+a[1]>0) { new = -1; pos = 0; } for(i = 2;i<n;i++) { new = a[i]+new; // printf("%d",new); if(new>0&& pos==0) {count1+=abs(new)+1; new= -1;} else if(new<0 && pos ) {count1+=abs(new)+1; new = 1; } else if(new ==0) { count1++; if(pos==0) new = -1; else new = 1; } pos =!pos; } printf("%ld",count<count1?count:count1); 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, = map(int,input().split()) ans1 = 0 S = A[0] if S <= 0: S = 1 ans1 = -S+1 for a in A[1:]: S1 = S+a if S1*S >= 0: ans1 += abs(S1)+1 S1 = -S//abs(S) S = S1 ans2 = 0 S = A[0] if S >= 0: S = -1 ans1 = -S+1 for a in A[1:]: S1 = S+a if S1*S >= 0: ans2 += abs(S1)+1 S1 = -S//abs(S) S = S1 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
# -*- coding: utf-8 -*- N = int(input()) aN = list(map(int, input().split())) sum1 = aN[0] cnt = 0 if aN[0] > 0: sei = True elif aN[0] < 0: sei = False else: print("和が0なので、これはないはず") for i in range(1,N): sum1 += aN[i] if sei: if sum1 >= 0: tmp = abs(sum1) +1 cnt += tmp sum1 -= tmp sei = False else: if sum1 <= 0: tmp = abs(sum1) +1 cnt += tmp sum1 += tmp sei = True print(cnt)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> data(N); for (int i = 0; i < N; i++) cin >> data[i]; int count = 0; long int ans = data[0]; int saisyo; while (ans <= 0) { ans++; count++; } for (int i = 1; i < N; i++) { ans += data[i]; if (i % 2 == 0) { while (ans <= 0) { ans++; count++; } } else { while (ans >= 0) { ans--; count++; } } } saisyo = count; count = 0; ans = data[0]; while (ans >= 0) { ans--; count++; } for (int i = 1; i < N; i++) { ans += data[i]; if (i % 2 != 0) { while (ans <= 0) { ans++; count++; } } else { while (ans >= 0) { ans--; count++; } } } saisyo = min(saisyo, count); cout << saisyo << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MAXN = 100 * 1000 + 10; int main() { long long n, f = 0, z = 0, s = 0, sum = 0; cin >> n; long long b[n]; for (long long i = 0; i < n; i++) { cin >> b[i]; if (i % 2 == 0) { f += b[i]; } else { z += b[i]; } } if (f > z) { if (b[0] <= 0) { s += -1 * b[0] + 1; b[0] = 1; } } else { if (b[0] >= 0) { s += b[0] + 1; b[0] = -1; } } for (long long i = 0; i < n - 1; i++) { sum += b[i]; if (sum < 0 && sum + b[i + 1] < 0) { s += -1 * (sum + b[i + 1]) + 1; b[i + 1] += -1 * (sum + b[i + 1]) + 1; } else if (sum > 0 && sum + b[i + 1] >= 0) { s += sum + b[i + 1] + 1; b[i + 1] -= sum + b[i + 1] + 1; } else if (sum + b[i + 1] == 0) { if (sum < 0) { b[i + 1] += 1; } else { b[i + 1] -= 1; } s++; } } cout << s; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
using System; using System.Collections.Generic; using static Assistant.Input; using static Assistant.Debug; using System.Linq; using Assistant; namespace ABC059C { class Program { static void Main(string[] args) { var n = RInt; var a = RInts; long ans = 0; if (a[0] != 0) { ans = Math.Min(cand(a[0], a), cand(-a[0], a)); } else { ans = Math.Min(cand(1, a), cand(-1, a)) + 1; } Console.WriteLine(ans); } static long cand(long sum, int[] a) { long ret = 0; for (int i = 1; i < a.Length; i++) { if (sum < 0) { sum += a[i]; if (sum < 1) { ret += 1 - sum; sum = 1; } } else if (sum > 0) { sum += a[i]; if (sum > -1) { ret += sum + 1; sum = -1; } } } return ret; } } } namespace Assistant { static class Input { static List<string> line = new List<string>(); static int index = 0; static String RNext() { if (line.Count <= index) line.AddRange(Console.ReadLine().Split()); return line[index++]; } public static int RInt => int.Parse(RNext()); public static long RLong => long.Parse(RNext()); public static int[] RInts => Console.ReadLine().Split().Select(int.Parse).ToArray(); public static long[] RLongs => Console.ReadLine().Split().Select(long.Parse).ToArray(); public static string RString => RNext(); //以下未テスト public static int[] RIntsC(int c) => Enumerable.Repeat(0, c).Select(x => int.Parse(RNext())).ToArray(); public static long[] RLongsC(int c) => Enumerable.Repeat(0, c).Select(x => long.Parse(RNext())).ToArray(); public static char[][] RMap(int h) => Enumerable.Repeat(0, h).Select(x => Console.ReadLine().ToCharArray()).ToArray(); } public struct Mlong { long _v; const long mod = 1000000007; public Mlong(long n = 0) : this() { _v = n >= mod ? n % mod : n; } public static implicit operator Mlong(long _x) => new Mlong(_x); public static implicit operator long(Mlong _x) => _x._v; public static Mlong operator +(Mlong m1, Mlong m2) { long m = m1._v + m2._v; return m >= mod ? m - mod : m; } public static Mlong operator -(Mlong m1, Mlong m2) { long m = m1._v - m2._v; return m >= 0 ? m : m + mod; } public static Mlong operator *(Mlong m1, Mlong m2) => m1._v * m2._v % mod; public static Mlong operator /(Mlong m1, Mlong m2) => m1._v * ModPow(m2._v, mod - 2) % mod; public static long ModPow(long a, long n) { if (n == 0) return 1; else if (n % 2 == 1) return a * ModPow(a, n - 1) % mod; else return ModPow(a * a % mod, n / 2); } static Mlong[] fac, finv, inv; public static void nCkInit(int max) { fac = new Mlong[max]; finv = new Mlong[max]; inv = new Mlong[max]; 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; inv[i] = mod - inv[mod % i] * (mod / i); finv[i] = finv[i - 1] * inv[i]; } } public static Mlong nCk(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * finv[k] * finv[n - k]; } } static class Debug { static public void Draw2D<T>(T[,] map, int mode = 1) { #if DEBUG int W = map.GetLength(0); int H = map.GetLength(1); string[,] map2 = new string[W + 1, H + 1]; for (int i = 0; i < W + 1; i++) { for (int j = 0; j < H + 1; j++) { if (i == 0 && j == 0) map2[i, j] = 0.ToString(); else if (i == 0) map2[i, j] = (j - 1).ToString(); else if (j == 0) map2[i, j] = (i - 1).ToString(); else map2[i, j] = map[i - 1, j - 1].ToString(); } } for (int i = 0; i < W + 1; i++) { for (int j = 0; j < H + 1; j++) { if (mode == 0) Console.Write(map2[i, j].Last()); if (mode == 1) Console.Write(map2[i, j] + " "); } Console.WriteLine(); } Console.WriteLine(); #endif } public static void Draw1D<T>(T[] array, int mode = 0) { #if DEBUG Console.WriteLine(string.Join(" ", array)); #endif } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; use std::io::{BufWriter, Write}; // https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { ($($r:tt)*) => { let stdin = std::io::stdin(); let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); let mut next = move || -> String{ bytes .by_ref() .map(|r|r.unwrap() as char) .skip_while(|c|c.is_whitespace()) .take_while(|c|!c.is_whitespace()) .collect() }; input_inner!{next, $($r)*} }; } macro_rules! input_inner { ($next:expr) => {}; ($next:expr, ) => {}; ($next:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($next, $t); input_inner!{$next $($r)*} }; } macro_rules! read_value { ($next:expr, [graph1; $len:expr]) => {{ let mut g = vec![vec![]; $len]; let ab = read_value!($next, [(usize1, usize1)]); for (a, b) in ab { g[a].push(b); g[b].push(a); } g }}; ($next:expr, ( $($t:tt),* )) => { ( $(read_value!($next, $t)),* ) }; ($next:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>() }; ($next:expr, chars) => { read_value!($next, String).chars().collect::<Vec<char>>() }; ($next:expr, usize1) => (read_value!($next, usize) - 1); ($next:expr, [ $t:tt ]) => {{ let len = read_value!($next, usize); read_value!($next, [$t; len]) }}; ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); } #[allow(unused)] macro_rules! debug { ($($format:tt)*) => (write!(std::io::stderr(), $($format)*).unwrap()); } #[allow(unused)] macro_rules! debugln { ($($format:tt)*) => (writeln!(std::io::stderr(), $($format)*).unwrap()); } /* mod mod_int { use std::ops::*; pub trait Mod: Copy { fn m() -> i64; } #[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct ModInt<M> { pub x: i64, phantom: ::std::marker::PhantomData<M> } impl<M: Mod> ModInt<M> { // x >= 0 pub fn new(x: i64) -> Self { ModInt::new_internal(x % M::m()) } fn new_internal(x: i64) -> Self { ModInt { x: x, phantom: ::std::marker::PhantomData } } pub fn pow(self, mut e: i64) -> Self { debug_assert!(e >= 0); let mut sum = ModInt::new_internal(1); let mut cur = self; while e > 0 { if e % 2 != 0 { sum *= cur; } cur *= cur; e /= 2; } sum } #[allow(dead_code)] pub fn inv(self) -> Self { self.pow(M::m() - 2) } } impl<M: Mod, T: Into<ModInt<M>>> Add<T> for ModInt<M> { type Output = Self; fn add(self, other: T) -> Self { let other = other.into(); let mut sum = self.x + other.x; if sum >= M::m() { sum -= M::m(); } ModInt::new_internal(sum) } } impl<M: Mod, T: Into<ModInt<M>>> Sub<T> for ModInt<M> { type Output = Self; fn sub(self, other: T) -> Self { let other = other.into(); let mut sum = self.x - other.x; if sum < 0 { sum += M::m(); } ModInt::new_internal(sum) } } impl<M: Mod, T: Into<ModInt<M>>> Mul<T> for ModInt<M> { type Output = Self; fn mul(self, other: T) -> Self { ModInt::new(self.x * other.into().x % M::m()) } } impl<M: Mod, T: Into<ModInt<M>>> AddAssign<T> for ModInt<M> { fn add_assign(&mut self, other: T) { *self = *self + other; } } impl<M: Mod, T: Into<ModInt<M>>> SubAssign<T> for ModInt<M> { fn sub_assign(&mut self, other: T) { *self = *self - other; } } impl<M: Mod, T: Into<ModInt<M>>> MulAssign<T> for ModInt<M> { fn mul_assign(&mut self, other: T) { *self = *self * other; } } impl<M: Mod> Neg for ModInt<M> { type Output = Self; fn neg(self) -> Self { ModInt::new(0) - self } } impl<M> ::std::fmt::Display for ModInt<M> { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { self.x.fmt(f) } } impl<M: Mod> ::std::fmt::Debug for ModInt<M> { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { let (mut a, mut b, _) = red(self.x, M::m()); if b < 0 { a = -a; b = -b; } write!(f, "{}/{}", a, b) } } impl<M: Mod> From<i64> for ModInt<M> { fn from(x: i64) -> Self { Self::new(x) } } // Finds the simplest fraction x/y congruent to r mod p. // The return value (x, y, z) satisfies x = y * r + z * p. fn red(r: i64, p: i64) -> (i64, i64, i64) { if r.abs() <= 10000 { return (r, 1, 0); } let mut nxt_r = p % r; let mut q = p / r; if 2 * nxt_r >= r { nxt_r -= r; q += 1; } if 2 * nxt_r <= -r { nxt_r += r; q -= 1; } let (x, z, y) = red(nxt_r, r); (x, y - q * z, z) } } // mod mod_int macro_rules! define_mod { ($struct_name: ident, $modulo: expr) => { #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] struct $struct_name {} impl mod_int::Mod for $struct_name { fn m() -> i64 { $modulo } } } } const MOD: i64 = 1_000_000_007; define_mod!(P, MOD); type ModInt = mod_int::ModInt<P>; //n^p mod m fn repeat_square(n: i64, p: i64, m: i64) -> i64 { if p == 0 { 1 } else if p == 1 { n % m } else if p % 2 == 0 { repeat_square(n, p / 2, m).pow(2) % m } else { (n * repeat_square(n, p - 1, m)) % m } } fn ncr_mod(n: i64, r: i64, m: i64) -> i64 { let mut denominator = n; let mut numerator = 1; for i in 1..r { denominator = (denominator * (n - i)) % m; numerator = (numerator * (i + 1)) % m; } (denominator * repeat_square(numerator, m - 2, m)) % m } */ fn solve() { let out = std::io::stdout(); let mut out = BufWriter::new(out.lock()); macro_rules! puts { ($($format:tt)*) => (let _ = write!(out,$($format)*);); } input! { n: usize, a: [i32; n], } let mut cnt_odd = 0; let mut cnt_even = 0; let mut cum_1 = vec![0; n]; let mut cum_2 = vec![0; n]; //cum_1[even] < 0,cum_2[odd] < 0 if a[0] >= 0 { cnt_even = a[0].abs() + 1; cum_1[0] = a[0]; cum_2[0] += -cnt_even; } else { cnt_odd = a[0].abs() + 1; cum_1[0] += cnt_odd; cum_2[0] = a[0]; } //+ - + - for i in 1..n { cum_1[i] = cum_1[i-1] + a[i]; if i % 2 != 0 { if cum_1[i] >= 0 { cnt_odd += cum_1[i].abs() + 1; cum_1[i] = -1; } } else { if cum_1[i] <= 0 { cnt_odd += cum_1[i].abs() + 1; cum_1[i] = 1; } } } //- + - + for i in 1..n { cum_2[i] = cum_2[i-1] + a[i]; if i % 2 == 0 { if cum_2[i] >= 0 { cnt_even += cum_2[i].abs() + 1; cum_2[i] = -1; } } else { if cum_2[i] <= 0 { cnt_even += cum_2[i].abs() + 1; cum_2[i] = 1; } } } puts!("{}\n",min(cnt_odd, cnt_even)); } fn main() { // In order to avoid potential stack overflow, spawn a new thread. let stack_size = 104_857_600; // 100 MB let thd = std::thread::Builder::new().stack_size(stack_size); thd.spawn(|| solve()).unwrap().join().unwrap(); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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
"""ABC059 C Sequence diff: """ N=int(input()) A=list(map(int,input().split())) Acop = A.copy() cum = [0]*N cum[0] = A[0] for i in range(1, N): cum[i] = cum[i-1] + A[i] cumcop = cum.copy() # + - + - ... start = N for i in range(N): if i % 2 == 0: if cum[i] <= 0: start = i break else: if cum[i] >= 0: start = i break if start == N: print(0) exit() ans1 = 0 for i in range(start, N): cum[i] = cum[i-1] + A[i] if i % 2 == 0: if cum[i] <= 0: ans1 += abs(cum[i]) + 1 A[i] += abs(cum[i]) + 1 cum[i] = 1 else: if cum[i] >= 0: ans1 += abs(cum[i]) + 1 A[i] -= abs(cum[i]) + 1 cum[i] = -1 # cum[i] = cum[i-1] + A[i] # - + - + A = Acop.copy() cum = cumcop.copy() start = N for i in range(N): if i % 2 == 1: if cum[i] <= 0: start = i break else: if cum[i] >= 0: start = i break if start == N: print(0) exit() ans2 = 0 for i in range(start, N): cum[i] = cum[i-1] + A[i] if i % 2 == 1: if cum[i] <= 0: ans2 += abs(cum[i]) + 1 A[i] += abs(cum[i]) + 1 cum[i] = 1 else: if cum[i] >= 0: ans2 += abs(cum[i]) + 1 A[i] -= abs(cum[i]) + 1 cum[i] = -1 # cum[i] = cum[i-1] + A[i] 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
def c(ints): for i in range(len(ints)): if ints[i] != 0: sig = 1 if ints[i] > 0 else -1 sig_ = -sig total = ints[i] total_ = -sig mov = i mov_ = abs(total) + 1 if i > 0: mov += 1 mov_ += 1 j = i break if i == len(ints) - 1: return i + 1 for i_ in ints[j+1:]: tmp = total + i_ tmp_ = total_ + i_ if tmp == 0: mov +=1 tmp = -sig elif sig * tmp > 0: mov += abs(tmp) + 1 tmp = -sig if tmp_ == 0: mov_ +=1 tmp_ = -sig_ elif sig_ * tmp_ > 0: mov_ += abs(tmp_) + 1 tmp_ = -sig_ sig *= -1 total = tmp sig_ *= -1 total_ = tmp_ print(mov, mov_) return min(mov, 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; 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; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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 scala.io.{StdIn => in} object Main extends App { val n = in.readInt val a = in.readLine.split(" ").map(_.toLong) def solve(isEven: Boolean) = { val even_odd = if(isEven) 0 else 1 a./:((0L, 0L, 0)) { (acc, e) => val (count, sum, i) = acc val v = sum + e if(i % 2 == even_odd) { if(v > 0) (count, v, i+1) else (count + v.abs + 1L, 1, i+1) } else { if(v < 0) (count, v, i+1) else (count + v.abs + 1L, -1, i+1) } }._1 } val ans = solve(true) min solve(false) 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; int a[n], sum[n]; cin >> a[0]; sum[0] = a[0]; int ans = 0; for (int i = 1; i < n; i++) cin >> a[i]; if (sum[0] > 0) { for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + a[i]; if (i & 1) { if (sum[i] >= 0) { ans += sum[i] + 1; sum[i] = -1; } } else { if (sum[i] <= 0) { ans += 1 - sum[i]; sum[i] = 1; } } } } else if (sum[0] < 0) { for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + a[i]; if (i & 1) { if (sum[i] <= 0) { ans += 1 - sum[i]; sum[i] = 1; } } else { if (sum[i] >= 0) { ans += sum[i] + 1; sum[i] = -1; } } } } else { int ans2 = 1, ans3 = 1; sum[0] = 1; for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + a[i]; if (i & 1) { if (sum[i] >= 0) { ans2 += sum[i] + 1; sum[i] = -1; } } else { if (sum[i] <= 0) { ans2 += 1 - sum[i]; sum[i] = 1; } } } sum[0] = -1; for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + a[i]; if (i & 1) { if (sum[i] <= 0) { ans3 += 1 - sum[i]; sum[i] = 1; } } else { if (sum[i] >= 0) { ans3 += sum[i] + 1; sum[i] = -1; } } } ans = min(ans2, ans3); } 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())) count = 0 sum_i = a[0] if a[0] != 0: for i in range(1, n): if sum_i < 0: sum_i += a[i] if sum_i <= 0: count += 1 - sum_i sum_i = 1 elif sum_i > 0: sum_i += a[i] if sum_i >= 0: count += sum_i + 1 sum_i = -1 else: sum_i = 1 pos_count = 0 for i in range(1, n): if sum_i < 0: sum_i += a[i] if sum_i <= 0: pos_count += 1 - sum_i sum_i = 1 elif sum_i > 0: sum_i += a[i] if sum_i >= 0: pos_count += sum_i + 1 sum_i = -1 sum_i = -1 neg_count = 0 for i in range(1, n): if sum_i < 0: sum_i += a[i] if sum_i <= 0: neg_count += 1 - sum_i sum_i = 1 elif sum_i > 0: sum_i += a[i] if sum_i >= 0: neg_count += sum_i + 1 sum_i = -1 if neg_count < pos_count: count = neg_count else: count = pos_count 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
UNKNOWN
parseInt(x) = parse(Int, x) function main() n = readline() |> parseInt a = map(parseInt, split(readline())) b = Array{Int}(n) b[1] = a[1] k = 0 for i in 2:n b[i] = a[i]+b[i-1] if b[i]*b[i-1] >= 0 if b[i-1] < 0 k += abs(b[i]-1) b[i] = 1 else k += abs(b[i]+1) b[i] = -1 end end end c = Array{Int}(n) l = 0 if a[1] > 0 c[1] = -1 l += abs(a[1]+1) else c[1] = 1 l += abs(a[1]-1) end for i in 2:n c[i] = a[i]+c[i-1] if c[i]*c[i-1] >= 0 if c[i-1] < 0 l += abs(c[i]-1) c[i] = 1 else l += abs(c[i]+1) c[i] = -1 end end end print(min(k,l)) end main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int N, A[100000]; int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> A[i]; } int sum = A[0], cnt = 0; if (sum < 0) { cnt += 1 - sum; sum = 1; } for (int i = 1; i < N; i++) { sum += A[i]; if (i % 2 == 1) { if (sum >= 0) { cnt += sum + 1; sum = -1; } } else { if (sum <= 0) { cnt += 1 - sum; sum = 1; } } } int sum2 = A[0], cnt2 = 0; if (sum2 > 0) { cnt2 += sum2 + 1; sum2 = -1; } for (int i = 1; i < N; i++) { sum2 += A[i]; if (i % 2 == 1) { if (sum2 <= 0) { cnt2 += 1 - sum2; sum2 = 1; } } else { if (sum2 >= 0) { cnt2 += sum2 + 1; 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
python3
#!usr/bin/env python3 from collections import defaultdict from collections import deque from heapq import heappush, heappop import sys import math import bisect import random import itertools sys.setrecursionlimit(10**5) stdin = sys.stdin bisect_left = bisect.bisect_left bisect_right = bisect.bisect_right def LI(): return list(map(int, stdin.readline().split())) def LF(): return list(map(float, stdin.readline().split())) def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split())) def II(): return int(stdin.readline()) def IF(): return float(stdin.readline()) def LS(): return list(map(list, stdin.readline().split())) def S(): return list(stdin.readline().rstrip()) def IR(n): return [II() for _ in range(n)] def LIR(n): return [LI() for _ in range(n)] def FR(n): return [IF() for _ in range(n)] def LFR(n): return [LI() for _ in range(n)] def LIR_(n): return [LI_() for _ in range(n)] def SR(n): return [S() for _ in range(n)] def LSR(n): return [LS() for _ in range(n)] mod = 1000000007 inf = float('INF') #A def A(): a = input().split() a = list(map(lambda x: x.capitalize(), a)) a,b,c = a print(a[0]+b[0]+c[0]) return #B def B(): a = II() b = II() if a > b: print("GREATER") if a < b: print("LESS") if a == b: print("EQUAL") return #C def C(): II() a = LI() def f(suma, b): for i in a[1:]: if suma * (suma + i) < 0: suma = suma + i continue b = b + (abs(suma + i) + 1) suma = (-1 * (suma > 0)) or 1 return b if a[0] == 0: ans = min(f(1, 1), f(-1, 1)) else: ans = min(f(a[0], 0), f(-a[0], 2 * abs(a[0]))) print(ans) return #D def D(): s = S() for i in range(len(s) - 1): if s[i] == s[i+1]: print(i + 1, i + 2) return for i in range(len(s) - 2): if s[i] == s[i + 2]: print(i + 1, i + 3) return print(-1, -1) return #Solve if __name__ == '__main__': C()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def count_loop(n, old_state, a_list, flag): count = 0 for i in range(1, int(n)): num_state = old_state + int(a_list[i]) if flag == 1: if num_state == 0: count += 1 old_state = -1 flag = -1 elif num_state > 0: count += num_state + 1 old_state = -1 flag = -1 else: old_state = num_state flag = -1 elif flag == -1: if num_state == 0: count += 1 old_state = 1 flag = 1 elif num_state < 0: count += abs(num_state) + 1 old_state = 1 flag = 1 else: old_state = num_state flag = 1 else: if num_state == 0: count += 1 flag = 0 elif num_state > 0: old_state = num_state flag = 1 else: old_state = num_state flag = -1 return count if __name__ == "__main__": n = input() a = input() a_list = a.split(" ") old_state = int(a_list[0]) count = 0 c1 = count_loop(n,old_state,a_list,1) c2 = count_loop(n,old_state,a_list,-1) c3 = count_loop(n,old_state,a_list,0)+1 if c1 <= c2 and c1 <= c3: print(c1) elif c2 < c1 and c2 < c3: print(c2) else: print(c3)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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 first_pos = 0; int sum1 = 0; for (int i = 0; i < n; i++) { sum1 += a[i]; if (i % 2 == 0) { if (sum1 <= 0) { first_pos += -sum1 + 1; sum1 = 1; } } else { if (sum1 >= 0) { first_pos += sum1 + 1; sum1 = -1; } } } int first_neg = 0; int sum2 = 0; for (int i = 0; i < n; i++) { sum2 += a[i]; if (i % 2 == 0) { if (sum2 >= 0) { first_neg += sum2 + 1; sum2 = -1; } } else { if (sum2 <= 0) { first_neg += -sum2 + 1; sum2 = 1; } } } cout << min(first_pos, first_neg) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll mod = LLONG_MAX; int a[100010]; int rui[100010]; int n; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; rui[0] = a[0]; for (int i = 1; i < n; i++) { rui[i] += a[i] + rui[i - 1]; } int temp = 0; int ans = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && rui[i] + temp == 0) { temp++; ans++; continue; } if (i % 2 == 1 && rui[i] + temp == 0) { temp--; ans++; continue; } if (i % 2 == 0 && rui[i] + temp < 0) { ans += abs(1 - (rui[i] + temp)); temp += 1 - (rui[i] + temp); } if (i % 2 == 1 && rui[i] + temp > 0) { ans += abs(-(1 + rui[i] + temp)); temp += -(1 + rui[i] + temp); } } if (ans < 0) ans = 1e9; temp = 0; int ans2 = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0 && rui[i] + temp == 0) { temp--; ans2++; continue; } if (i % 2 == 1 && rui[i] + temp == 0) { temp++; ans2++; continue; } if (i % 2 == 0 && rui[i] + temp > 0) { ans2 += abs(-(1 + rui[i] + temp)); temp += -(1 + rui[i] + temp); } if (i % 2 == 1 && rui[i] + temp < 0) { ans2 += abs(1 - (rui[i] + temp)); temp += 1 - (rui[i] + temp); } } if (ans2 < 0) ans2 = 1e9; 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
cpp
#include <bits/stdc++.h> using namespace std; //long long using ll = long long; // pair<int, int> using PII = pair<int, int>; //最大値、mod const int MOD = 1000000007; const int mod = 1000000007; const int INF = 1000000000; const long long LINF = 1e18; const int MAX = 510000; //出力系 #define print(x) cout << x << endl #define prints(x) cout << fixed << setprecision(20) << x << endl #define printc(x) cout << setw(2) << setfill('0') << x << endl; #define yes cout << "Yes" << endl #define YES cout << "YES" << endl #define no cout << "No" << endl #define NO cout << "NO" << endl //配列入力 vector<long long>vecin(ll n){ vector<long long>res(n); for(int i = 0; i < n; i++) cin >> res[i]; return res; } // begin() end() #define all(x) (x).begin(),(x).end() //for #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define rrep(i,a,b) for(int i=(a);i>(b);i--) #define rep(i,a,b) for(int i=(a);i<(b);i++) //最大公約数 ll gcd(ll x, ll y) { return y ? gcd(y,x%y) : x;} // 最小公倍数 unsigned lcm(unsigned a, unsigned b){ return a / gcd(a, b) * b; } // a = max(a, b), a = min(a, b) 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; } // num ^ pow(mod取る) ll pow_mod(ll num, ll pow, ll mod) { ll prod = 1; num %= mod; while (pow > 0) { if (pow & 1) prod = prod * num % mod; num = num * num % mod; pow >>= 1; } return prod; } // 二項係数(MODとる、1 ≦ k ≦ n ≦ 10^7 程度) // COMinit() // COM(x, y) // とコンビで使う // テーブルを作る前処理 long long fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++){ fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k){ if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } //重みつきUnionFInd template<class Abel> struct GUnionFind { vector<int> par; vector<int> rank; vector<Abel> diff_weight; GUnionFind(int n = 1, Abel SUM_UNITY = 0) { init(n, SUM_UNITY); } void init(int n = 1, Abel SUM_UNITY = 0) { par.resize(n); rank.resize(n); diff_weight.resize(n); for (int i = 0; i < n; ++i) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY; } int root(int x) { if (par[x] == x) { return x; } else { int r = root(par[x]); diff_weight[x] += diff_weight[par[x]]; return par[x] = r; } } Abel weight(int x) { root(x); return diff_weight[x]; } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y, Abel w) { w += weight(x); w -= weight(y); x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y), w = -w; if (rank[x] == rank[y]) ++rank[x]; par[y] = x; diff_weight[y] = w; return true; } Abel diff(int x, int y) { return weight(y) - weight(x); } }; // UnionFind struct UnionFind { vector<int> par; vector<int> rank; vector<ll> Size; UnionFind(int n = 1) { init(n); } void init(int n = 1) { par.resize(n + 1); rank.resize(n + 1); Size.resize(n + 1); for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, Size[i] = 1; } int root(int x) { if (par[x] == x) { return x; } else { int r = root(par[x]); return par[x] = r; } } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) ++rank[x]; par[y] = x; Size[x] += Size[y]; return true; } ll size(int x){ return Size[root(x)]; } }; //modint構造体 struct Mint { int val; Mint inv() const{ int tmp,a=val,b=mod,x=1,y=0; while(b)tmp=a/b,a-=tmp*b,swap(a,b),x-=tmp*y,swap(x,y); return Mint(x); } public: Mint():val(0){} Mint(ll x){if((val=x%mod)<0)val+=mod;} Mint pow(ll t){Mint res=1,b=*this; while(t){if(t&1)res*=b;b*=b;t>>=1;}return res;} Mint& operator+=(const Mint& x){if((val+=x.val)>=mod)val-=mod;return *this;} Mint& operator-=(const Mint& x){if((val+=mod-x.val)>=mod)val-=mod; return *this;} Mint& operator*=(const Mint& x){val=(ll)val*x.val%mod; return *this;} Mint& operator/=(const Mint& x){return *this*=x.inv();} bool operator==(const Mint& x) const{return val==x.val;} bool operator!=(const Mint& x) const{return val!=x.val;} bool operator<(const Mint& x) const{return val<x.val;} bool operator<=(const Mint& x) const{return val<=x.val;} bool operator>(const Mint& x) const{return val>x.val;} bool operator>=(const Mint& x) const{return val>=x.val;} Mint operator+(const Mint& x) const{return Mint(*this)+=x;} Mint operator-(const Mint& x) const{return Mint(*this)-=x;} Mint operator*(const Mint& x) const{return Mint(*this)*=x;} Mint operator/(const Mint& x) const{return Mint(*this)/=x;} }; struct factorial { vector<Mint> Fact, Finv; public: //factorial fact(10000010); //fact.nCr(a, b) //「fact」の部分は自由に名前変更可能 factorial(int maxx){ Fact.resize(maxx+1),Finv.resize(maxx+1); Fact[0]=Mint(1); rep(i,0,maxx)Fact[i+1]=Fact[i]*(i+1); Finv[maxx]=Mint(1)/Fact[maxx]; rrep(i,maxx,0)Finv[i-1]=Finv[i]*i; } Mint fact(int n,bool inv=0){if(inv)return Finv[n];else return Fact[n];} Mint nPr(int n,int r){if(n<0||n<r||r<0)return Mint(0);else return Fact[n]*Finv[n-r];} Mint nCr(int n,int r){if(n<0||n<r||r<0)return Mint(0);else return Fact[n]*Finv[r]*Finv[n-r];} }; //他のnCr使えない場合試す Mint com2(int n, int a) { Mint x = 1, y = 1; REP(i, a) { x *= n - i; y *= i + 1; } return x / y; } // 1 * 2 * 3 .... * n (mod) ll modfact(ll n) { if (n <= 1) return 1; return (n * modfact(n - 1)) % MOD; } // kが角度だった場合:cos(k * (PI / 180)); //const double PI = acos(-1);のまま使うと円周率(M_PIもあるよ) const double PI = acos(-1); // 多次元 vector 生成 例: auto dp = make_vec<long long>(N+1, 5, 5, 5); template<class T> vector<T> make_vec(size_t a){ return vector<T>(a); } template<class T, class... Ts> auto make_vec(size_t a, Ts... ts){ return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } //素因数分解 vector<pair<long long, int>>factorize(long long n){ vector<pair<long long, int>> res; for(long long i = 2; i * i <= n; ++i){ if(n % i) continue; res.emplace_back(i, 0); while(n % i == 0){ n /= i; res.back().second++; } } if(n != 1) res.emplace_back(n, 1); return res; } // 素数判定 bool primejudge(long long a){ if(a <= 1) return false; for(long long i = 2; i * i <= a; i++){ if(a % i == 0) return false; } return true; } int dy[4] = {0, 1, 0, -1}, dx[4] = {1, 0, -1, 0}; vector<int>graph[1000]; ll v(string s){ string ss; REP(i, s.length()){ if(s[i] != '.') ss += s[i]; } ll res = stoll(ss); return res; } int main() { int n; cin >> n; vector<int>a(n); REP(i, n)cin >> a[i]; // +- int aa = 0; int now = 0; REP(i, n){ now += a[i]; if(i % 2 == 0 && now <= 0) aa += -now + 1, now = 1; if(i % 2 == 1 && now >= 0) aa += now + 1, now = -1; } //-+ int b = 0; now = 0; REP(i, n){ now += a[i]; if(i % 2 == 0 && now >= 0) b += now + 1, now = -1; if(i % 2 == 1 && now <= 0) b += -now + 1, now = 1; } cout << min(aa, 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
cpp
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } signed main() { int n; cin >> n; long long ans = 0; long long sum = 0; bool fl = 0; for (int i = 0; i < (n); i++) { long long a; cin >> a; if (i == 0) { if (a == 0) { fl = 1; break; } sum += a; continue; } if (0 < sum) { if (0 < sum + a) { ans += sum + a + 1; sum = -1; } else if (sum + a == 0) { ans++; sum = -1; } else { sum += a; } } else { if (0 > sum + a) { ans += abs(sum + a) + 1; sum = 1; } else if (sum + a == 0) { ans++; sum = 1; } else { sum += a; } } } if (fl) { long long sum1 = 1, sum2 = -1; ans++; int ans2 = 1; for (int i = 0; i < (n - 1); i++) { long long a; cin >> a; if (0 < sum1) { if (0 < sum1 + a) { ans += sum1 + a + 1; sum1 = -1; } else if (sum1 + a == 0) { ans++; sum1 = -1; } else { sum1 += a; } } else { if (0 > sum1 + a) { ans += abs(sum1 + a) + 1; sum1 = 1; } else if (sum1 + a == 0) { ans++; sum1 = 1; } else { sum1 += a; } } if (0 < sum2) { if (0 < sum2 + a) { ans2 += sum2 + a + 1; sum2 = -1; } else if (sum2 + a == 0) { ans2++; sum2 = -1; } else { sum2 += a; } } else { if (0 > sum2 + a) { ans2 += abs(sum2 + a) + 1; sum2 = 1; } else if (sum2 + a == 0) { ans2++; sum2 = 1; } else { sum2 += a; } } } chmin(ans, ans2); } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = [int(i) for i in input().split()] s0 = a[0] count=0 if a[0]==0: if a[1]>0:s0-=1 else:s0+=1 count+=1 for i in range(1,n): s1 = s0+a[i] if s0*s1>=0: if s1>0: a[i]-=(abs(s1)+1) count+=(abs(s1)+1) elif s1<0: a[i]+=(abs(s1)+1) count+=(abs(s1)+1) elif s1==0: if s0>0: a[i]-=1 count+=1 elif s0<0: a[i]+=1 count+=1 s0 += a[i] print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int n, a; long long sum = 0, ans = 0; int s; cin >> n; cin >> sum; if (sum > 0) s = 1; else s = -1; for (int i = 1; i < n; i++) { cin >> a; sum += a; if (sum * s > 0) { ans += abs(sum) + 1; sum = -s; } if (sum > 0) s = 1; else s = -1; } if (sum == 0) ans++; 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, ans1 = 0, ans2 = 0, sum1 = 0, sum2 = 0; cin >> n; vector<int> a(n), s(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } s.at(0) = a.at(0); for (int i = 1; i <= n - 1; i++) { s.at(i) = a.at(i) + s.at(i - 1); } for (int i = 0; i < n; i++) { if (i % 2 == 0 && s.at(i) + sum1 <= 0) { sum2 += 1 - s.at(i) - sum1; ans1 += abs(1 - s.at(i) - sum1); } else if (i % 2 == 1 && s.at(i) + sum1 >= 0) { sum2 -= s.at(i) + sum1 + 1; ans1 += abs(s.at(i) + sum1 + 1); } sum1 = sum2; } sum1 = 0, sum2 = 0; for (int i = 0; i < n; i++) { if (i % 2 == 1 && s.at(i) + sum1 <= 0) { sum2 += 1 - s.at(i) - sum1; ans2 += abs(1 - s.at(i) - sum1); } else if (i % 2 == 0 && s.at(i) + sum1 >= 0) { sum2 -= s.at(i) + sum1 + 1; ans2 += abs(s.at(i) + sum1 + 1); } sum1 = sum2; } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = [int(i) for i in input().split()] ff = -1 for i in range(n): if A[i] > 0: ff = i break elif A[i] < 0: ff = i+1 break if A[0] != 0: ans = 0 S = A[0] f = A[0]//abs(A[0]) else: if ff == -1: ans = 1 S = 1 f = 1 else: if ff % 2 == 0: ans = 1 S = 1 f = 1 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 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> const int MGN = 8; const int ARY_SZ_MAX = 10000000; using namespace std; using ll = long long; using ull = unsigned long long; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>; using vl = vector<ll>; using vvl = vector<vl>; using vd = vector<double>; using vs = vector<string>; using pii = pair<int, int>; using pll = pair<ll, ll>; using psi = pair<string, int>; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vl A(N); for (int i = int(0); i < int(N); ++i) cin >> A[i]; vl s(N); A.push_back(0); s.push_back(0); ll ans = (INT_MAX / 2); ll cost = 0; s[0] = A[0]; for (int i = int(1); i < int(N + 1); ++i) { if (i % 2 == 0 && s[i - 1] <= 0) { cost += abs(s[i - 1]) + 1; s[i - 1] = 1; } else if (i % 2 == 1 && s[i - 1] >= 0) { cost += abs(s[i - 1]) + 1; s[i - 1] = -1; } s[i] = s[i - 1] + A[i]; } ans = min(ans, cost); cost = 0; s[0] = A[0]; for (int i = int(1); i < int(N + 1); ++i) { if (i % 2 == 0 && s[i - 1] >= 0) { cost += abs(s[i - 1]) + 1; s[i - 1] = -1; } else if (i % 2 == 1 && s[i - 1] <= 0) { cost += abs(s[i - 1]) + 1; s[i - 1] = 1; } s[i] = s[i - 1] + A[i]; } ans = min(ans, cost); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int INF = 100100100; const double PI = 3.14159265358979323846; int main() { int n, a[100001] = {0}; int res = 0; long long sum = 0; cin >> n; for (int i = 0; i < (n); ++i) cin >> a[i]; sum = a[0]; if (sum == 0) { if (a[1] >= 0) { sum--; } else if (a[1] < 0) { sum++; } res++; } for (int i = (1); i < (n); ++i) { long long newSum = sum + a[i]; long long prod = newSum * sum; if (prod < 0) { sum = newSum; } else if (prod >= 0) { sum = -sum / abs(sum); res += abs(sum - newSum); } } 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> const int INF = 2147483647; const long long int MOD = 1000000007; using namespace std; int main() { int n; cin >> n; vector<long long int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } long long int ans = (a[0] == 0) ? 1 : 0; long long int ans2 = ans; if (a[0] < 0) { ans += abs(a[0]) + 1; } bool minus = true; long long int total = a[0] == 0 ? 1 : a[0]; for (int i = 1; i < n; i++) { if (minus) { if (a[i] + total >= 0) { ans += a[i] + total + 1; total = -1; } else { total += a[i]; } } else { if (a[i] + total <= 0) { ans += abs(a[i] + total) + 1; total = 1; } else { total += a[i]; } } minus = !minus; } cout << endl; if (a[0] > 0) { ans2 += abs(a[0]) + 1; } minus = false; total = (a[0] == 0) ? -1 : a[0]; for (int i = 1; i < n; i++) { if (minus) { if (a[i] + total >= 0) { ans2 += a[i] + total + 1; total = -1; } else { total += a[i]; } } else { if (a[i] + total <= 0) { ans2 += abs(a[i] + total) + 1; total = 1; } else { total += a[i]; } } minus = !minus; } 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
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; } const long long LLINF = 1LL << 60; int main(void) { ios::sync_with_stdio(false); cin.tie(0); long long int i, n; long long int sum = 0, ans = 0, minans; cin >> n; vector<long long int> v(n, 0), w(n, 0); for (i = 0; i < n; i++) { cin >> v[i]; } sum = v[0]; w[0] = v[0]; for (i = 1; i < n; i++) { if ((sum < 0 && sum + v[i] > 0) || (sum > 0 && sum + v[i] < 0)) { w[i] = v[i]; sum += w[i]; continue; } if (sum < 0) { ans += abs(-1 * sum + 1 - v[i]); w[i] = -1 * sum + 1; sum += w[i]; if (sum == 0) { w[i]++; sum++; } } else { ans += abs(-1 * sum - 1 - v[i]); w[i] = -1 * sum - 1; sum += w[i]; if (sum == 0) { w[i]--; sum--; } } } minans = ans; ans = 0; w.clear(); sum = -1 * v[0]; w[0] = -1 * v[0]; for (i = 1; i < n; i++) { if ((sum < 0 && sum + v[i] > 0) || (sum > 0 && sum + v[i] < 0)) { w[i] = v[i]; sum += w[i]; continue; } if (sum < 0) { ans += abs(-1 * sum + 1 - v[i]); w[i] = -1 * sum + 1; sum += w[i]; if (sum == 0) { w[i]++; sum++; } } else { ans += abs(-1 * sum - 1 - v[i]); w[i] = -1 * sum - 1; sum += w[i]; if (sum == 0) { w[i]--; sum--; } } } minans = min(ans, minans); cout << minans << 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 = 1e9; const int MOD = 1e9 + 7; signed main() { int n; cin >> n; long long a[n]; for (int i = 0; i < (n); i++) { cin >> a[i]; } long long ans = 0, now = a[0], pre = a[0]; for (int i = 1; i < n; i++) { now += a[i]; if (pre * now >= 0) { if (now < 0) { ans += 1 - now; now = 1; } else if (now > 0) { ans += now + 1; now = -1; } else if (now == 0) { ans++; if (pre < 0) { now = 1; } else { now = -1; } } } pre = now; } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; long long int a[n], sum = 0, count = 0; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { sum += a[i]; if (sum == 0) { if (a[i + 1] > 0) { a[i]--; count++; } else if (a[i + 1] < 0) { a[i]++; count++; } else if (a[i + 1] == 0) { a[i]++; a[i + 1] -= 2; count += 3; } continue; } else if (sum > 0) { if (sum + a[i + 1] > 0) { count += sum + a[i + 1] + 1; a[i + 1] -= sum + a[i + 1] + 1; continue; } } else if (sum < 0) { if (sum + a[i + 1] < 0) { count += abs(sum + a[i + 1] + 1); a[i + 1] += abs(sum + a[i + 1] + 1); } } } cout << count; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) list_a = list(map(int, input().split())) sum_a = list_a[0] count_t = 0 for one_a in list_a[1:]: if sum_a > 0: if sum_a + one_a < 0: sum_a += one_a else: count_t += abs(sum_a) + one_a + 1 sum_a = -1 else: if sum_a + one_a > 0: sum_a += one_a else: count_t += abs(sum_a) - one_a + 1 sum_a = 1 sum_a = list_a[0] count_f = 0 for one_a in list_a[1:]: if sum_a > 0: if sum_a + one_a < 0: sum_a += one_a else: count_f += abs(sum_a) + one_a + 1 sum_a = -1 else: if sum_a + one_a > 0: sum_a += one_a else: count_f += abs(sum_a) - one_a + 1 sum_a = 1 print(min(count_t, count_f))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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, 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 { ans = cal(); } printf("%lld\n", ans); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = input() a = [int(i) for i in input.split()] X = 0 ans = 0 for i in a: X += i if X > 0: b = -1 - X ans += b - a[i+1] else: b = 1 - X ans += b - a[i+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 n; int d[100005]; int main(void) { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &d[i]); int ans1 = 0, sum = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 0) { int a = 0; if (sum + d[i] >= 0) { a = abs(sum + d[i] - (-1)); ans1 += a; sum = -1; } else sum += d[i]; } else { int a = 0; if (sum + d[i] <= 0) { a = abs(sum + d[i] - 1); ans1 += a; sum = 1; } else sum += d[i]; } } int ans2 = 0; sum = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 0) { int a = 0; if (sum + d[i] <= 0) { a = abs(sum + d[i] - (1)); ans2 += a; sum = 1; } else sum += d[i]; } else { int a = 0; if (sum + d[i] >= 0) { a = abs(sum + d[i] - (-1)); ans2 += a; sum = -1; } else sum += d[i]; } } printf("%d\n", min(ans1, ans2)); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; signed main() { long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; i++) cin >> a[i]; long long ans = INF; long long sum = 0, cnt = 0; for (long long i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0) { if (sum <= 0) { cnt += abs(sum) + 1; sum = 1; } } else { if (sum > 0) { cnt += abs(sum) + 1; sum = -1; } } } ans = min(ans, cnt); sum = 0; cnt = 0; for (long long i = 0; i < n; i++) { sum += a[i]; if (i % 2) { if (sum <= 0) { cnt += abs(sum) + 1; sum = 1; } } else { if (sum > 0) { cnt += abs(sum) + 1; sum = -1; } } } ans = min(ans, cnt); cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys input=sys.stdin.readline def main(): N = int(input()) A = list(map(int, input().split())) tmp = A.copy() mi = 10**9 for j in range(2): A = tmp.copy() s = 0 n = 0 if j == 0: # 偶数番目までの和が正 ⇒ A[0] を負にする if A[0] > 0: n += abs(A[0] +1) A[0] = -1 else: # 奇数番目までの和が正 ⇒ A[0] を正にする if A[0] < 0: n += abs(A[0] -1) A[0] = 1 s = A[0] for i in range(1,N): if s * (s+A[i]) >= 0: if s < 0: n += abs(-s+1 -A[i]) A[i] = -s+1 else: n += abs(-s-1 -A[i]) A[i] = -s-1 s += A[i] mi = min(mi, n) print(mi) 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; long long a[1000005] = {}; long long calc(long long *, long long); long long get_sign(long long); int main() { long long n, ans; cin >> n; for (long long i = 0; i < n; i++) { cin >> a[i]; } ans = calc(a, n); cout << ans << endl; return 0; } long long calc(long long *a, long long n) { long long cnt = 0; long long sign = 0; long long tmp; if (a[0] > 0) { sign = 1; } tmp = a[0]; for (long long i = 0; i < n - 1; i++) { tmp = tmp + a[i + 1]; if (sign == 1) { if (tmp >= 0) { cnt += tmp + 1; tmp = -1; } } else { if (tmp <= 0) { cnt += (-1 * tmp) + 1; tmp = 1; } } sign = get_sign(tmp); } return cnt; } long long get_sign(long long tmp) { if (tmp < 0) return 0; else return 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 ll = long long; using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<ll> v(n, 0ll); for (int i = (int)(0); i < (int)(n); i++) cin >> v[i]; vector<ll> memo(n + 1, 0ll); priority_queue<ll, vector<ll>, greater<ll> > pq; memo[0] = -1; memo[1] = v[0]; ll cnt = 0; for (int i = 1; i <= n; i++) { memo[i] = memo[i - 1] + v[i - 1]; if (not(memo[i - 1] * memo[i] < 0)) { if (memo[i] < 0) { int plus = memo[i] * (-1) + 1; memo[i] += plus; cnt += plus; } else if (memo[i] > 0) { int plus = memo[i] * -1 - 1; memo[i] += plus; cnt -= plus; } else { if (memo[i - 1] < 0) { memo[i]++; cnt++; } else { memo[i]--; cnt++; } } } } fill(memo.begin(), memo.end(), 0ll); memo[0] = 1; memo[1] = v[0]; pq.push(cnt); cnt = 0; for (int i = 2; i <= n; i++) { memo[i] = memo[i - 1] + v[i - 1]; if (not(memo[i - 1] * memo[i] < 0)) { if (memo[i] < 0) { int plus = memo[i] * (-1) + 1; memo[i] += plus; cnt += plus; } else if (memo[i] > 0) { int plus = memo[i] * -1 - 1; memo[i] += plus; cnt -= plus; } else { if (memo[i - 1] < 0) { memo[i]++; cnt++; } else { memo[i]--; cnt++; } } } } pq.push(cnt); cout << pq.top() << 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 INF = (1LL << 62); long long N; vector<long long> A, W; long long S[100002] = {INF * (-1)}; long long dp[100002] = {0}; void calcDP(int n) { if (n == 1) { if (W[1] != 0) { dp[1] = 0; } else { dp[1] = 1; if (W[2] <= 0) { W[1] = 1; } else { W[1] = -1; } S[1] = W[1]; } return; } else if (n == 2) { calcDP(1); if (W[1] > 0 && W[1] + W[2] < 0) { dp[2] = dp[1]; } else if (W[1] > 0 && W[1] + W[2] >= 0) { dp[2] = dp[1] + abs(W[2] - (-1 - W[1])); W[2] = -1 - W[1]; S[2] = S[1] + W[2]; } else if (W[1] < 0 && W[1] + W[2] > 0) { dp[2] = dp[1]; } else if (W[1] < 0 && W[1] + W[2] <= 0) { dp[2] = dp[1] + abs(W[2] - (1 - W[1])); W[2] = 1 - W[1]; S[2] = S[1] + W[2]; } } else { S[n] = S[n - 1] + W[n]; if ((S[n - 1] < 0 && S[n] > 0) || (S[n - 1] > 0 && S[n] < 0)) { dp[n] = dp[n - 1]; } else { long long dpa = -1, dpb = -1, dpc = -1; long long d, w, s; if (S[n - 2] > 0) { w = -1 - S[n - 2]; s = S[n - 2] + w; dpb = dp[n - 1] + abs(w - W[n - 1]) + abs(1 - s - W[n]); } else { w = 1 - S[n - 2]; s = S[n - 2] + w; dpb = dp[n - 1] + abs(w - W[n - 1]) + abs(-1 - s - W[n]); } if (S[n - 1] > 0) { dpc = dp[n - 1] + abs(-1 - S[n - 1] - W[n]); } else { dpc = dp[n - 1] + abs(1 - S[n - 1] - W[n]); } if (dpc <= dpb) { dp[n] = dpc; if (S[n - 1] > 0) { W[n] = -1 - S[n - 1]; } else { W[n] = 1 - S[n - 1]; } } else { dp[n] = dpb; W[n - 1] = w; S[n - 1] = S[n - 2] + W[n - 1]; W[n] = 0 - S[n - 1] - (abs(S[n - 1]) / S[n - 1]); } S[n] = S[n - 1] + W[n]; } return; } } int main(int argc, char* argv[]) { cin.tie(0); ios::sync_with_stdio(false); cin >> N; W.push_back(0); S[0] = 0; for (int i = 1; i <= N; i++) { long long a; cin >> a; A.push_back(a); W.push_back(a); if (i == 1) { S[1] = a; } else { S[i] = S[i - 1] + a; } } for (int i = 1; i <= N; i++) { calcDP(i); } printf("%lld\n", dp[N]); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; bool positive; if (a[0] > 0) positive = true; else positive = false; int total, cnt = 0; for (int i = 1; i < n; ++i) { total = accumulate(a.begin(), a.begin() + i, 0) + a[i]; if (positive && total >= 0) { a[i] -= (total + 1); cnt += (total + 1); positive = false; } else if (!positive && total <= 0) { a[i] += (abs(total) + 1); cnt += (abs(total) + 1); positive = true; } else positive = !positive; cout << a[i] << endl; } 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> vec(n); vector<long long> part(n + 1); part[0] = 0; for (int i = 0; i < n; i++) { cin >> vec[i]; part[i + 1] = part[i] + vec[i]; } long long ans = 0; bool isPlus = part[1] > 0 ? true : false; for (int i = 2; i < n + 1; i++) { int num = abs(part[i]); if (isPlus) { if (part[i] > 0) { ans += num; for (int j = i; j < n + 1; j++) part[j] -= num; } if (part[i] == 0) { ans++; for (int j = i; j < n + 1; j++) part[j]--; } isPlus = false; } else { if (part[i] < 0) { ans += num; for (int j = i; j < n + 1; j++) part[j] += num; } if (part[i] == 0) { ans++; for (int j = i; j < n + 1; j++) part[j]++; } isPlus = 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
UNKNOWN
fun main() { val n = readLine()!!.toInt() val a = readLine()!!.split(" ").map { it.toLong() } var answer = 0L var total = a[0] for (i in 1 until n) { val tmp = total total = total + a[i] if (total == 0L) { if (tmp > 0) { answer += 1 total = -1 } else if (tmp < 0) { answer += 1 total = 1 } } if (tmp > 0 && total > 0) { answer += (total + 1) total = -1 } else if (tmp < 0 && total < 0) { answer += (-total + 1) total = 1 } println(total) } println("answer $answer") }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, num[100003]; long long sum[100003], ans; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> num[i]; } sum[1] = num[1]; for (int i = 2; i <= n; i++) { if (sum[i - 1] > 0) { if (num[i] + sum[i - 1] < 0) { sum[i] = sum[i - 1] + num[i]; continue; } else { sum[i] = -1; ans += abs(sum[i] - (num[i] + sum[i - 1])); } } else { if (num[i] + sum[i - 1] > 0) { sum[i] = num[i] + sum[i - 1]; continue; } else { sum[i] = 1; ans += abs(sum[i] - (num[i] + sum[i - 1])); } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<bits/stdc++.h> using namespace std; #define fs first #define sc second #define pb push_back #define mp make_pair #define eb emplace_back #define ALL(A) A.begin(),A.end() #define RALL(A) A.rbegin(),A.rend() typedef long long ll; typedef pair<ll,ll> P; const ll mod=1000000007; const ll LINF=1LL<<60; const int INF=1<<30; int main(){ ll n,l; cin >> n; vector<ll> a(n); cin >> a[0]; for(ll i = 1; i < n; i++) { cin >> l; a[i] = a[i - 1] + l; } ll tmp = 0; ll count = 0; for(ll i = 0; i < n - 1; i++) { if (i == 0 && a[i] != 1){ count+=abs(a[i]-1); tmp-=a[i]-1; } if ((a[i] + tmp)* (a[i + 1] + tmp) >= 0){//和の符号が前後で同じ時 if ((a[i+1] + tmp) <= 0){ count += abs(a[i+1] + tmp - 1); tmp -= (a[i+1] + tmp - 1); } else{ count += abs(a[i] + tmp + 1); tmp -= (a[i] + tmp + 1); } } if (a[n - 1] +tmp == 0){ count++; } ll ans=count; count =0; ll tmp2=0; for(ll i = 0; i < n - 1; i++) { if (i == 0 && a[i] != -1){ count+=abs(a[i]+1); tmp2-=a[i]+1; } if ((a[i] + tmp2)* (a[i + 1] + tmp2) >= 0){//和の符号が前後で同じ時 if ((a[i+1] + tmp2) <= 0){ count += abs(a[i+1] + tmp2- 1); tmp2 -= (a[i+1] + tmp2 - 1); } else{ count += abs(a[i] + tmp2+ 1); tmp2 -= (a[i] + tmp2+ 1); } } // cout << a[i] + tmp << endl; } // cout << a[n - 1] << endl; if (a[n - 1] +tmp2 == 0){ count++; } cout << min(ans,count)<< endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int check(int sum, int ans, vector<int> T, int N, bool pre_pm) { for (int i = 1; i < N; i++) { if (pre_pm) { sum += T.at(i); while (0 <= sum) { sum--; ans++; } pre_pm = false; } else { sum += T.at(i); while (sum <= 0) { sum++; ans++; } pre_pm = true; } } return ans; } int main() { int N; vector<int> T; cin >> N; for (int i = 0; i < N; i++) { int tmp; cin >> tmp; T.push_back(tmp); } int ans = 0; int sum = 0; bool pre_pm; sum = T.at(0); if (0 <= sum) { pre_pm = true; int tmp1 = check(sum, ans, T, N, pre_pm); pre_pm = false; int tmp2 = check(-1, -1 - sum, T, N, pre_pm); tmp2 += (sum + 1); cout << min(tmp1, tmp2) << endl; } else { pre_pm = false; int tmp1 = check(sum, ans, T, N, pre_pm); pre_pm = true; int tmp2 = check(1, 1 + sum, T, N, pre_pm); tmp2 += (sum + 1); cout << min(tmp1, tmp2) << endl; } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def main(): N = int(input()) A = [int(i) for i in input().split()] ans1 = 0 S = [0] * N S[0] = A[0] for i in range(1, N): S[i] = S[i-1] + A[i] if S[i]*S[i-1] < 0: continue if S[i-1] > 0: S[i] = -1 ans1 += abs((-1) - (S[i-1]+A[i])) else: S[i] = 1 ans1 += abs(1 - (S[i-1]+A[i])) S2 = [0] * N ans2 = 0 if A[0] > 0: S2[0] = -1 ans2 += abs((-1) - A[0]) + A[0] else: S2[0] = 1 ans2 += abs(1 - A[0]) + A[0] for i in range(1, N): S2[i] = S2[i-1] + A[i] if S2[i]*S2[i-1] < 0: continue if S2[i-1] > 0: S2[i] = -1 ans2 += abs((-1) - (S2[i-1]+A[i])) else: S2[i] = 1 ans2 += abs(1 - (S2[i-1]+A[i])) print(min(ans1, ans2)) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int d[n]; for (int i = 0; i < n; i++) { cin >> d[i]; } long long count = 0; long long sum = d[0]; int f = 0; if (d[0] > 0) { f = -1; } if (d[0] < 0) { f = 1; } for (int i = 1; i < n; i++) { sum += d[i]; if (sum == 0) { if (f == 1) { count++; f = -1; sum = 1; continue; } if (f == -1) { count++; f = 1; sum = -1; continue; } } if (sum > 0) { if (f == 1) { f = -1; continue; } if (f == -1) { count += sum + 1; sum = -1; f = 1; continue; } } if (sum < 0) { if (f == -1) { f = 1; continue; } if (f == 1) { count += 1 - sum; sum = 1; f = -1; continue; } } } long long ccount = 0; long long ssum; int ff = 0; if (d[0] > 0) { ff = 1; ccount = 1 + d[0]; ssum = -1; } if (d[0] < 0) { ff = -1; ccount = 1 - d[0]; ssum = 1; } for (int i = 1; i < n; i++) { sum += d[i]; if (ssum == 0) { if (ff == 1) { ccount++; ff = -1; ssum = 1; continue; } if (ff == -1) { ccount++; ff = 1; ssum = -1; continue; } } if (ssum > 0) { if (f == 1) { ff = -1; continue; } if (ff == -1) { ccount += sum + 1; ssum = -1; ff = 1; continue; } } if (ssum < 0) { if (ff == -1) { ff = 1; continue; } if (ff == 1) { ccount += 1 - sum; ssum = 1; ff = -1; continue; } } } cout << min(count, ccount) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<long long> L(N); for (int i = 0; i < N; i++) { cin >> L.at(i); } long long v = 0, res = 0, le = 0; bool change_flag = true; for (int i = 0; i < N; i++) { if (i == 0) { v = L.at(i); } else { if (v > 0 && v + L.at(i) >= 0) { le = -1 - v - L.at(i); L.at(i) -= abs(le); v += L.at(i); res += abs(le); le = 0; } else if (v < 0 && v + L.at(i) <= 0) { le = 1 + v + L.at(i); L.at(i) += abs(le); v += L.at(i); res += abs(le); le = 0; } else { v += L.at(i); } } } cout << res << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; signed main() { long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < (long long)n; ++i) cin >> a[i]; long long ans = 0; long long sum = 0; for (long long i = 0; i < (long long)n; ++i) { if (sum < 0 and sum + a[i] <= 0) { ans += abs(a[i] - (1 - sum)); a[i] = 1 - sum; } else if (sum > 0 and sum + a[i] >= 0) { ans += abs(a[i] - (-sum - 1)); a[i] = -sum - 1; } sum += a[i]; } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# -*- coding: utf-8 -*- n = int(input().strip()) A_list = list(map(int, input().rstrip().split())) #----- S=A_list[0] cnt=0 for i in range(1,n): S_pre = S S += A_list[i] if S == 0: S += 1*(S_pre < 0) + (-1)*(S_pre > 0) cnt += 1 elif S * S_pre > 0: diff = abs(S) + 1 S += diff * (1*(S_pre < 0) + (-1)*(S_pre > 0)) cnt += diff 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
java
import java.util.Scanner; class Main { int n; int[] a; long cnt1; long cnt2; public static void main(String[] args) { Scanner sc = new Scanner(System.in); Main m = new Main(sc); m.solve(); sc.close(); } Main(Scanner sc) { n = sc.nextInt(); a = new int[n]; for(int i=0;i<n;i++){ a[i] = sc.nextInt(); } } void solve() { Thread t1 = new Thread(()->{calc_run1((a[0]>0)?0:(Math.abs(a[0])+1),1,(a[0]>0)?a[0]:1);}); t1.start(); Thread t2 = new Thread(()->{calc_run2((a[0]<0)?0:(Math.abs(a[0])+1),-1,(a[0]<0)?a[0]:-1);}); t2.start(); try { t1.join(); t2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Math.min(cnt1, cnt2)); } void calc_run1(long cnt,int sign,long sum){ for(int i=1;i<n;i++){ sum += a[i]; if(sum*sign>=0){ cnt += Math.abs(sum) + 1; sum = -sign; } sign *= -1; } cnt1 = cnt; System.out.println(cnt); } void calc_run2(long cnt,int sign,long sum){ for(int i=1;i<n;i++){ sum += a[i]; if(sum*sign>=0){ cnt += Math.abs(sum) + 1; sum = -sign; } sign *= -1; } cnt2 = cnt; System.out.println(cnt); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys def input(): return sys.stdin.readline().strip() sys.setrecursionlimit(20000000) def main(): N = int(input()) A = list(map(int, input().split())) S = A[0] cnt = 0 for i in range(1, N): s = S + A[i] if s == 0: if S < 0: cnt += 1 S = 1 else: cnt += 1 S = -1 else: if S * s > 0: if S < 0: cnt += abs(s) + 1 S = 1 else: cnt += s + 1 S = -1 else: S = s print(cnt) 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; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; bool diff(long long a, long long b) { if (a < 0 && b > 0) return true; if (a > 0 && b < 0) return true; return false; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector<int> a(n); for (int i = (0); i < (n); ++i) cin >> a[i]; long long ans = (a[0] == 0 ? 1 : 0), sum = (a[0] == 0 ? 1 : a[0]); for (int i = (1); i < (n); ++i) { if (diff(sum + a[i], sum)) { sum += a[i]; } else { long long need = (sum > 0 ? -1 : 1); long long now = need - sum; ans += abs(now - a[i]); sum = need; } } long long tmp = abs(a[0]) + 1; sum = (a[0] > 0 ? -1 : 1); for (int i = (1); i < (n); ++i) { if (diff(sum + a[i], sum)) { sum += a[i]; } else { long long need = (sum > 0 ? -1 : 1); long long now = need - sum; tmp += abs(now - a[i]); sum = need; } } cout << min(ans, tmp) << '\n'; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } void c_p_c() {} void parray(int a[], int n) { for (int i = 0; i < n; i++) { cout << a[i] << " "; } cout << "\n"; } int main() { long long i, n, m, k, c = 0, sum = 0; c_p_c(); long long no; cin >> n; int pre; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sum = a[0]; if (a[0] < 0) pre = 0; else pre = 1; int ans = 0; for (int i = 1; i < n; ++i) { if (pre == 0) { sum += a[i]; if (sum <= 0) { ans += 1 - (sum); sum = 1; } pre = 1; } else { sum += a[i]; if (sum >= 0) { ans += sum + 1; sum = -1; } pre = 0; } } cout << ans << "\n"; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { long long n, i, j, sw, sw2, count = 0, add = 0, adda = 0; cin >> n; vector<long long> a(n); for (i = 0; i < n; i++) { cin >> a[i]; adda += a[i]; } if (a[0] > 0) sw = 1; else sw = -1; add += a[0]; if ((adda > 0 && n % 2 == 1) || (adda < 0 && n % 2 == 0)) { } else { if (a[0] < 0) { while (a[0] != 1) { add++; a[0]++; count++; } } else { while (a[0] != -1) { add--; a[0]--; count++; } } } for (i = 1; i < n; i++) { add += a[i]; if (sw == 1) { if (add < 0) { } else { while (add != -1) { a[i]--; add--; count++; } } } else { if (add > 0) { } else { while (add != 1) { a[i]++; add++; count++; } } } if (a[i] > 0) sw = 1; else sw = -1; } cout << count << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; int *a; int ans = 0; cin >> n; a = new int[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int sum = 0; int opr1 = 0, opr2 = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 1 && sum <= 0) { int add = abs(sum) + 1; sum = 1; opr1 += add; } else if (i % 2 == 0 && sum >= 0) { int add = abs(sum) + 1; sum = -1; opr1 += add; } } sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0 && sum <= 0) { int add = abs(sum) + 1; sum = 1; opr2 += add; } else if (i % 2 == 1 && sum >= 0) { int add = abs(sum) + 1; sum = -1; opr2 += add; } } cout << opr1 << " " << opr2 << endl; ans = min(opr1, opr2); cout << ans << endl; delete (a); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int main() { size_t N; std::cin >> N; std::vector<int64_t> A(N); for (size_t n = 0; n < N; ++n) { std::cin >> A[n]; } int64_t a[2] = {0, 0}; for (size_t i = 0; i < 2; ++i) { int64_t c = 0; if (i == 0) { a[i] = 0; c = A[0]; } else { a[i] = abs(A[0]) + 1; c = (A[0] < 0) ? 1 : -1; } for (size_t n = 1; n < N; ++n) { if ((c < 0) == (c + A[n] <= 0)) { a[i] += abs(c + A[n]) + 1; c = (c < 0) ? 1 : -1; } else { c += A[n]; } } } std::cout << std::min(a[0], a[1]) << std::endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
import java.util.* fun main(args: Array<String>) { val sc = Scanner(System.`in`) val n = sc.nextInt() val a = (0 until n).map { sc.next().toLong() } println(problem059c(n, a)) } fun problem059c(n: Int, a: List<Long>): Long { val count1 = compute(n, a) val a = a.toMutableList() var a0 = a[0] var count = 0L if (a0 > 0) { val tmp = a0 + 1 a[0] = a0 - tmp count += tmp } else { val tmp = a0 - 1 a[0] = a0 - tmp count -= tmp } val count2 = compute(n, a) + count return Math.min(count1, count2) } fun compute(n: Int, a: List<Long>): Long { val a = a.toMutableList() var count = 0L var sum = 0L for (i in 0 until n) { val ai = a[i] sum = sum + ai if (sum == 0L) { break } if (i >= n - 1) { continue } val sum2 = sum + a[i + 1] if (sum * sum2 < 0) { continue } else { if (sum > 0) { val tmp = sum2 + 1 a[i + 1] = sum2 - tmp count += tmp } else { val tmp = sum2 - 1 a[i + 1] = sum2 - tmp count -= tmp } } } return count }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys n = int(input()) a = [int(n) for n in input().split()] sum = [0]*n sum[0] = a[0] ans = 0 for i in range(1,n): sum[i] = sum[i-1] while((sum[i]+a[i])*sum[i-1] >= 0): if(sum[i-1] > 0): a[i]-=1 ans+=1 else: a[i]+=1 ans+=1 # print(a) sum[i] += a[i] print(ans) # print(a) # print(sum)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
package main import ( "bufio" "fmt" "os" "strconv" ) func getScanner(fp *os.File) *bufio.Scanner { scanner := bufio.NewScanner(fp) scanner.Split(bufio.ScanWords) scanner.Buffer(make([]byte, 500001), 500000) return scanner } func getNextString(scanner *bufio.Scanner) string { scanner.Scan() return scanner.Text() } func getNextInt(scanner *bufio.Scanner) int { i, _ := strconv.Atoi(getNextString(scanner)) return i } func getNextInt64(scanner *bufio.Scanner) int64 { i, _ := strconv.ParseInt(getNextString(scanner), 10, 64) return i } func main() { fp := os.Stdin if len(os.Args) > 1 { fp, _ = os.Open(os.Args[1]) } scanner := getScanner(fp) writer := bufio.NewWriter(os.Stdout) n := getNextInt(scanner) aa := make([]int64, n) for i := 0; i < n; i++ { aa[i] = getNextInt64(scanner) } var sum, ans int64 sum = 0 for i := 0; i < n; i++ { if sum == 0 { sum = aa[i] continue } if sum < 0 { if sum+aa[i] > 0 { sum += aa[i] continue } ans += 1 - (sum + aa[i]) sum = 1 continue } if sum+aa[i] < 0 { sum += aa[i] continue } ans += 1 + (sum + aa[i]) sum = -1 } fmt.Fprintln(writer, ans) writer.Flush() }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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: b = [a[0]] else: su += 1 if b[1] > 0: b = [-1] else: b = [1] su = 0 for i in range(n-1): b.append(b[-1]+a[1+i]) if b[-1] * b[-2] > 0: su += abs(b[-1]) + 1 if b[-2] > 0: b[-1] = -1 else: b[-1] = 1 else: if b[-1] == 0: su += 1 if b[-2] > 0: b[-1] = -1 else: b[-1] = 1 print(su)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long int> a(n); for (int i = 0; i < (n); ++i) cin >> a[i]; long long int x = a[0]; long long int ans = 0, res; if (x < 0) { ans += x + 1; x = 1; } if (x == 0) { ans++; x++; } for (int i = 1; i < n; ++i) { if (x > 0 && x + a[i] > 0) { ans += x + a[i] + 1; x = -1; } else if (x < 0 && x + a[i] < 0) { ans += abs(x + a[i]) + 1; x = 1; } else { if (x > 0) { x = x + a[i]; ans += (x == 0); x -= (x == 0); } else { x = x + a[i]; ans += (x == 0); x += (x == 0); } } } res = ans; x = a[0]; ans = 0; if (x > 0) { ans += x + 1; x = -1; } if (x == 0) { ans++; x--; } for (int i = 1; i < n; ++i) { if (x > 0 && x + a[i] > 0) { ans += x + a[i] + 1; x = -1; } else if (x < 0 && x + a[i] < 0) { ans += abs(x + a[i]) + 1; x = 1; } else { if (x > 0) { x = x + a[i]; ans += (x == 0); x -= (x == 0); } else { x = x + a[i]; ans += (x == 0); x += (x == 0); } } } (res = min(res, ans)); 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
a = int(input()) b = list(map(int, input().split())) num = 0 k = b[0] + b[1] if k == 0: if b[0] >= 0: k = -1 else: k = 1 num += 1 for i in range(a-2): if k == 1: k += b[i+2] while k > -1: k -= 1 num += 1 else: k += b[i+2] while k < 1: k += 1 num += 1 print(num)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(int,input().split())) sum=0 cnt=0 # 奇数+ for i in range(n): sum+=a[i] if i%2==0: if sum>=0: cnt+=(sum+1) sum=-1 else: if sum<=0: cnt+=(1-sum) sum=1 cnt_sbst=cnt # 奇数- cnt=0 sum=0 for i in range(n): z+=a[i] if i%2==1: if sum>=0: cnt+=(sum+1) sum=-1 else: if sum<=0: cnt+=(1-sum) sum=1 cnt_plus=cnt ans=min(cnt_plus,cnt_sbst) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
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 rec aux sum sign cnt l = match l with | [] -> cnt | hd :: tl -> match sign with | `Plus -> if sum + hd <= 0 then let t = -(sum-1) in let c = t - hd in aux (sum+hd+c) `Minus (cnt+c) tl else aux (sum+hd) `Minus cnt tl | `Minus -> if sum + hd >= 0 then let t = (sum+1) in let c = t + hd in aux (sum+hd-c) `Plus (cnt+c) tl else aux (sum+hd) `Plus cnt tl in Printf.printf "%d\n" @@ aux 0 (if List.hd a_lst > 0 then `Plus else `Minus) 0 a_lst
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int main(void) { int n; std::cin >> n; std::vector<long long int> a(n); for (int i = 0; i < n; i++) { std::cin >> a[i]; } long long int count = 0; signed long long sum = a[0]; for (int i = 1; i < n; i++) { if (sum >= 0) { sum += a[i]; if (sum >= 0) { count += sum + 1; sum = -1; } } else { sum += a[i]; if (sum <= 0) { count += -sum + 1; sum = 1; } } } std::cout << count << std::endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; long[] s1 = new long[n]; for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } long sum1 = 0; s1[0] = a[0]; // - + - + となる場合 while (s1[0] >= 0) { s1[0]--; sum1++; } for (int i = 1; i < n; i++) { s1[i] = s1[i-1] + a[i]; // iが奇数の場合は正にする if (i % 2 != 0) { if (s1[i-1]*s1[i] >= 0) { sum1 = sum1 + 1 - s1[i]; s1[i] = 1; } } // iが偶数の場合は負にする else { if (s1[i-1]*s1[i] >= 0) { sum1 = sum1 + 1 + s1[i]; s1[i] = -1; } } } Arrays.fill(s1, 0); long sum2 = 0; s1[0] = a[0]; // + - + - となる場合 while (s1[0] <= 0) { s1[0]++; sum2++; } for (int i = 1; i < n; i++) { s1[i] = s1[i-1] + a[i]; // iが奇数の場合は負にする if (i % 2 != 0) { if (s1[i-1]*s1[i] >= 0) { sum2 = sum2 + 1 + s1[i]; s1[i] = -1; } } // iが偶数の場合は正にする else { if (s1[i-1]*s1[i] >= 0) { sum2 = sum2 + 1 - s1[i]; s1[i] = 1; } } } System.out.println(Math.min(sum1, sum2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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 seq(int a, vector<int> b) { long long count = 0; if (0 <= b[0] * a) { if (b[0] == 0) { b[0] = a; count++; } long long s = b[0]; for (int i = 1; i < b.size(); i++) { int k = (1 - 2 * (i % 2)) * a; s += b[i]; if (s * k <= 0) { count += 1 - k * s; s = k; } } } return count; } int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; if (a.at(0) == 0) cout << min(seq(1, a), seq(-1, a)) << endl; else if (a.at(0) > 0) cout << seq(1, a) << endl; else cout << seq(-1, a) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { private static final int MOD = (int)Math.pow(10, 9); public static void main(String[] args) { FastReader sc = new FastReader(); int n = sc.nextInt(); int[] nums = new int[n]; for (int i = 0; i < nums.length; i++) { nums[i] = sc.nextInt(); } long firstDigitPostive = 0; long prefixSum = 0; for (int i = 0; i < nums.length; i++) { prefixSum += nums[i]; if (i % 2 == 0) { // odd is postive if (prefixSum == 0) { prefixSum--; firstDigitPostive++; } else if (prefixSum < 0) { firstDigitPostive += (1 - prefixSum); prefixSum += 1 - prefixSum; } } else { // even is negative if (prefixSum == 0) { prefixSum++; } else if (prefixSum > 0) { firstDigitPostive += (1 + prefixSum); prefixSum -= (prefixSum + 1) ; } } } prefixSum = 0; long firstDigitNegative = 0; for (int i = 0; i < nums.length; i++) { prefixSum += nums[i]; if (i % 2 == 0) { // odd is negative if (prefixSum == 0) { prefixSum++; } else if (prefixSum > 0) { firstDigitNegative += (1 + prefixSum); prefixSum -= (prefixSum + 1) ; } } else { // even is postive if (prefixSum == 0) { prefixSum--; firstDigitNegative++; } else if (prefixSum < 0) { firstDigitNegative += (1 - prefixSum); prefixSum += 1 - prefixSum; } } } System.out.println(Math.min(firstDigitPostive, firstDigitNegative)); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); long n; cin >> n; vector<long> a(n); for (long i = 0; i < n; i++) cin >> 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; } } cout << j << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> v(n); for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); i += 1 - 2 * ((0) > (n))) cin >> v[i]; long long res1 = 0, res2 = 0; long long som = v[0]; if (som > 0) { for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 1) if (som < 0) continue; else { res1 += som + 1; som = -1; } else if (som > 0) continue; else { res1 += 1 - som; som = 1; } } res2 = v[0] + 1; som = -1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 0) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } } else { for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 0) if (som < 0) continue; else { res1 += som + 1; som = -1; } else if (som > 0) continue; else { res1 += 1 - som; som = 1; } } res2 = 1 - v[0]; som = 1; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { som = som + v[i]; if (i % 2 == 1) if (som < 0) continue; else { res2 += som + 1; som = -1; } else if (som > 0) continue; else { res2 += 1 - som; som = 1; } } } cout << min(res1, res2); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; public class Main{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] a = new long[n]; long[] s1 = new long[n]; long[] s2 = new long[n]; for(int i = 0; i < n; i++){ a[i] = sc.nextLong(); if(i == 0){ s1[0] = a[0]; s2[0] = a[0]; } else { s1[i] = s1[i-1]+a[i]; s2[i] = s2[i-1]+a[i]; } } sc.close(); long cnt1 = 0; long cnt2 = 0; for(int i = 0; i < n; i++){ if(i%2 == 0){ if(s1[i] <= 0){ long diff = 1-s1[i]; cnt1 += diff; for(int j = i; j < n; j++){ s1[j] += diff; } } } else { if(s1[i] >= 0){ long diff = s1[i] + 1; cnt1 += diff; for(int j = i; j < n; j++){ s1[j] -= diff; } } } } for(int i = 0; i < n; i++){ if(i%2 == 0){ if(s2[i] >= 0){ long diff = s2[i]+1; cnt2 += diff; for(int j = i; j < n; j++){ s2[j] -= diff; } } } else { if(s2[i] <= 0){ long diff = 1-s2[i]; cnt2 += diff; for(int j = i; j < n; j++){ s2[j] += diff; } } } } System.out.println(Math.min(cnt1, cnt2)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <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]; } long long s = a[0]; long long cnt = 0; if (s == 0) { if (a[1] > 0) { s = -1; cnt++; } else { s = 1; cnt++; } } for (int i = 1; i < n; i++) { if (s > 0) { if (s + a[i] < 0) s += a[i]; else { cnt += abs(s + a[i]) + 1; s = -1; } } else { if (s + a[i] > 0) s += a[i]; else { cnt += abs(s + a[i]) + 1; s = 1; } } } long long cnt2 = 0; s = a[0]; if (s == 0) { if (a[1] > 0) { s = -1; cnt2++; } else { s = 1; cnt2++; } } else if (s > 0) { cnt2 += s + 1; s = -1; } else { cnt2 += abs(s) + 1; s = 1; } for (int i = 1; i < n; i++) { if (s > 0) { if (s + a[i] < 0) s += a[i]; else { cnt2 += abs(s + a[i]) + 1; s = -1; } } else { if (s + a[i] > 0) s += a[i]; else { cnt2 += abs(s + a[i]) + 1; s = 1; } } } cout << (cnt < cnt2 ? cnt : cnt2); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } //偶数を正とする int counter=0; int sum=0; for(int i=0;i<n;i++) { sum+=a[i]; if(i%2==0 && sum<=0) { //-aだったら+1の操作をa回で0になり //正にするならそこからさらに+1 counter+=Math.abs(sum)+1; sum=1; }else if(i%2==1 && sum>=0){ //+aだったらa回の-1と1回の-1でマイナス counter+=sum+1; sum=-1; } } int counter1=0; int sum1=0; //奇数を正 for(int i=0;i<n;i++) { sum1+=a[i]; if(i%2==0 && sum1>=0) { //+aだったらa回の-1と1回の-1でマイナス counter1+=sum1+1; sum1=-1; }else if(i%2==1 && sum1<=0){ //-aだったら+1の操作をa回で0になり //正にするならそこからさらに+1 counter1+=Math.abs(sum1)+1; sum1=1; } } System.out.println(Math.max(counter,counter1)); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String args[]){ Scanner scanner = new Scanner(System.in); int count = 0; int l[] = new int[scanner.nextInt()]; for (int i = 0;i < l.length;++i){ l[i] = Integer.valueOf(scanner.next()); } for (int i = 0;i < l.length;++i){ int p = 0; int q = 0; for (int j = 0;j <= i;++j){ if(j != i) { p += l[j]; } q += l[j]; } if(q == 0||(q < 0&&p < 0)||(q > 0&&p > 0)){ int c = 1 + ((p > 0) ? 1 : -1) * q; count += c; l[i] += ((p > 0) ? -1 : 1) * c; } } System.out.println(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
java
import java.io.File; import java.io.IOException; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.Deque; import java.util.List; import java.util.Scanner; public class Main { //ABC059 public static void main(String[] args) throws IOException { //File file = new File("input.txt"); //Scanner in = new Scanner(file); Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] a = new int[n]; int[] sum = new int[n]; int ans = 0; for(int i = 0; i < n; i++){ a[i] = in.nextInt(); if(i == 0) sum[0] = a[0]; else sum[i] = sum[i-1] + a[i]; } /* for(int i = 0; i < n; i++) System.out.print(sum[i] + " "); System.out.println(); System.out.println(); */ int diff = 0; for(int i = 1; i < n; i++){ sum[i] += diff; /* System.out.print(i + ":"); for(int j = 0; j < n; j++) System.out.print(sum[j] + " "); System.out.println(); */ if(sum[i-1] < 0 && sum[i] <= 0){ int d = - sum[i] + 1; sum[i] += d; diff += d; ans += Math.abs(d); }else if(sum[i-1] > 0 && sum[i] >= 0){ int d = - sum[i] - 1; sum[i] += d; diff += d; ans += Math.abs(d); } } /* System.out.println(); for(int i = 0; i < n; i++) System.out.print(sum[i] + " "); System.out.println(); */ System.out.println(ans); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<bits/stdc++.h> using namespace std; #define ll long long // long long省略 #define pb push_back // push_back省略 #define mp make_pair // make_pair省略 #define fi first // first省略 #define se second // second省略 #define itn int // int誤字保険 #define count cout // cout誤字保険 #define vecotr vector // vector誤字保険 #define ednl endl // endl誤字保険 #define opt() cin.tie(0); ios::sync_with_stdio(false) // 入出力速度改善 #define rep(i,l,r) for(ll i=(l);i<(r);i++) // 範囲[l, r)で刻み1のfor文(順方向) #define repp(i,l,r,k) for(ll i=(l);i<(r);i+=(k)) // 範囲[l, r)で刻みkのfor文(順方向) #define rrep(i,l,r) for(ll i=(r-1);i>=(l);i--) // 範囲[l, r)で刻み1のfor文(逆方向) #define rrepp(i,l,r,k) for(ll i=(r-1);i>=(l);i-=(k)) // 範囲[l, r)で刻みkのfor文(逆方向) #define all(x) (x).begin(), (x).end() // vectorのポインタ位置指定用 #define max(p,q)((p)>(q)?(p):(q)) // max拡張 #define min(p,q)((p)<(q)?(p):(q)) // min拡張 #define bit(n,m)(((n)>>(m))&1) // 変数nのm番目のbitを取り出す template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } int dy[]={0, 1, 0, -1}; // 4方向近傍 int dx[]={1, 0, -1, 0}; // 4方向近傍 #define INF ((1LL<<62)-(1LL<<31)) //#define MOD 998244353 #define MOD 1000000007 #define invp(a,p) pom(a,p-2,p) //////////////////////////////////////////////////////////////////////////////////////// int main(){ ll n; cin >> n; ll a[n]; rep(i,0,n) cin >> a[i]; //1. 累積和に0がないか? // iで累積和が0になるなら、iから先に+1 //2. 累積和の符号が反転しない // 反転するまで1を加える、1を引く ll csum[n]; csum[0] = a[0]; rep(i,1,n){ csum[i] = csum[i-1] + a[i]; //cout << csum[i] << ednl; } //初手が0じゃない場合 if(csum[0] != 0){ ll p1 = 0, m1 = 0; bool flag = (csum[0] > 0); rep(i,1,n){ if((flag ? -1 : 1) * (csum[i] + p1 - m1) > 0){ flag = !flag; continue; }else{ if(flag){ //このステップでは負であるべき m1 += csum[i] + p1 - m1 + 1; }else{ //このステップでは生であるべき p1 += -(csum[i] + p1 - m1) + 1; } flag = !flag; } } ll res = p1 + m1; if(csum[0] > 0){ p1 = 0; m1 = csum[0] + 1; }else{ p1 = -csum[0] + 1; m1 = 0; } flag = !(csum[0] > 0); rep(i,1,n){ if((flag ? -1 : 1) * (csum[i] + p1 - m1) > 0){ flag = !flag; continue; }else{ if(flag){ //このステップでは負であるべき m1 += csum[i] + p1 - m1 + 1; }else{ //このステップでは生であるべき p1 += -(csum[i] + p1 - m1) + 1; } flag = !flag; } } res = min(p1 + m1, res); cout << res << ednl; }else{ //初手が0の場合で決め打ちで+1した場合 ll p1 = 1, ll m1 = 0; flag = true; rep(i,1,n){ if((flag ? -1 : 1) * (csum[i] + p1 - m1) > 0){ flag = !flag; continue; }else{ if(flag){ //このステップでは負であるべき m1 += csum[i] + p1 - m1 + 1; }else{ //このステップでは生であるべき p1 += -(csum[i] + p1 - m1) + 1; } flag = !flag; } } ll res = p1+m1; //初手が0の場合で決め打ちで-1した場合 p1 = 0, m1 = 1; flag = false; rep(i,1,n){ if((flag ? -1 : 1) * (csum[i] + p1 - m1) > 0){ flag = !flag; continue; }else{ if(flag){ //このステップでは負であるべき m1 += csum[i] + p1 - m1 + 1; }else{ //このステップでは生であるべき p1 += -(csum[i] + p1 - m1) + 1; } flag = !flag; } } res = min(res, p1+m1); } cout << res << ednl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 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]; 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; } } if(A[0]!=0){ cout<<ans<<"\n"; return; } B[0]=1; ans=INF; ll reans=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; } } chmin(ans,reans); B[0]=-1; reans=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; } } chmin(ans,reans); cout<<ans<<"\n"; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; public class Main{ public static void main(String[] args) { Scanner no=new Scanner(System.in); int n=no.nextInt(); int arr[]=new int[n]; // int sum1[]=new int[n]; for(int i=0;i<n;i++){ arr[i]=no.nextInt(); /* if(i==0){ sum1[i]=arr[i]; } else{ sum1[i]=sum1[i]+sum1[i-1]; }*/ } int sum=0; int count=0; if(arr[0]==0){ if(arr[1]>=0){ arr[0]=-1; } else{ arr[0]=1; } } if(arr[0]>0){ sum=arr[0]; for(int i=1;i<n;i++){ if(i%2==1&&sum+arr[i]>=0){ int t=arr[i]; arr[i]=(sum+1)*-1; count=count+Math.abs((t-arr[i])); // sum=sum+arr[i]; } else if(i%2==0&&sum+arr[i]<=0){ int t=arr[i]; arr[i]=(Math.abs(sum)+1); count=count+Math.abs((Math.abs(t)-arr[i])); //sum=sum+arr[i]; } /*else if(sum+arr[i]==0){ count++; }*/ sum=sum+arr[i]; } } else if(arr[0]<0){ sum=arr[0]; for(int i=1;i<n;i++){ if(i%2==1&&sum+arr[i]<=0){ int t=arr[i]; arr[i]=(Math.abs(sum)+1); count=count+Math.abs((Math.abs(t)-arr[i])); // sum=sum+arr[i]; // System.out.println(count); } else if(i%2==0&&sum+arr[i]>=0){ int t=arr[i]; arr[i]=(sum+1)*-1; count=count+Math.abs((t-arr[i])); //System.out.println(count); //sum=sum+arr[i]; } /* else if(sum+arr[i]==0){ count++; }*/ sum=sum+arr[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
UNKNOWN
#![allow(unused_mut)] #![allow(non_snake_case)] #![allow(unused_imports)] use std::collections::HashSet; use std::collections::HashMap; use std::collections::BTreeSet; use std::collections::VecDeque; use std::cmp::{max, min}; 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 mut a_s: Vec<i64> = (0..n).map(|_| input()).collect(); let mut ans = std::i64::MIN; for &s in [1, -1].iter() { let mut sign = s; let mut sum = 0; let mut lans = 0; for i in 0..n { sum += a_s[i]; // 意図したモノでない if sum * sign <= 0 { lans += (sum - sign).abs(); sum = sign; } sign *= -1; } ans = min(ans, lans); } println!("{}", ans); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import sys, os, math, bisect, itertools, collections, heapq, queue # from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall from decimal import Decimal from collections import defaultdict, deque # import fractions sys.setrecursionlimit(10000000) ii = lambda: int(sys.stdin.buffer.readline().rstrip()) il = lambda: list(map(int, sys.stdin.buffer.readline().split())) fl = lambda: list(map(float, sys.stdin.buffer.readline().split())) iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)] iss = lambda: sys.stdin.buffer.readline().decode().rstrip() sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split())) isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)] lcm = lambda x, y: (x * y) // math.gcd(x, y) # lcm = lambda x, y: (x * y) // fractions.gcd(x, y) MOD = 10 ** 9 + 7 MAX = float('inf') def main(): if os.getenv("LOCAL"): sys.stdin = open("input.txt", "r") N = ii() A = il() r1 = 0 S = [0] * N S[0] = A[0] for n in range(1, N): S[n] = S[n - 1] + A[n] if n % 2 != 0 and S[n] >= 0: r1 += S[n] + 1 S[n] = -1 elif n % 2 == 0 and S[n] <= 0: r1 += abs(S[n]) + 1 S[n] = 1 r2 = 0 S = [0] * N S[0] = A[0] for n in range(1, N): S[n] = S[n - 1] + A[n] if n % 2 != 0 and S[n] <= 0: r2 += abs(S[n]) + 1 S[n] = 1 elif n % 2 == 0 and S[n] >= 0: r2 += S[n] + 1 S[n] = -1 print(min(r1, r2)) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; long long a[100000]; bool flag = true; long long sum = 0; long long ans = 0; cin >> n; for (int i = 0; i < (int)(n); i++) { cin >> a[i]; } if (a[0] > 0) { flag = true; } else if (a[0] < 0) { flag = false; } else { if (a[1] >= 0) { a[0] = -1; flag = false; ans += 1; } else { a[0] = 1; flag = true; ans += 1; } } sum = a[0]; for (int i = (int)(1); i < (int)(n); i++) { sum += a[i]; if (flag == true) { if (sum >= 0) { ans += sum + 1; sum = -1; } } if (flag == false) { if (sum <= 0) { ans += -sum + 1; sum = 1; } } if (sum > 0) { flag = true; } else { flag = false; } } 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 ll = long long; using namespace std; struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20); }; } aaaaaaa; int MOD = 1e9 + 7; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int lcm(int a, int b) { return (a * b) / gcd(a, b); } int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int N; int main() { cin >> N; vector<int> a(N); for (int i = 0; i < (N); ++i) cin >> a[i]; int ans = 0, sum = a[0]; for (int i = 1; i <= (N - 1); ++i) { if ((sum + a[i]) * sum >= 0) { int k = a[i]; a[i] = (abs(sum) + 1) * (sum / abs(sum)) * (-1); ans += abs(k - a[i]); } sum += a[i]; } 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; int A[100001]; cin >> n; for (int i = 0; i < n; i++) { cin >> A[i]; } int sum = 0; int counter = 0; for (int i = 0; i < n; i++) { sum += A[i]; if (i % 2 == 0) { while (sum <= 0) { sum++; counter++; } } else { while (sum >= 0) { sum--; counter++; } } } int counterNeg = 0; sum = 0; for (int i = 0; i < n; i++) { sum += A[i]; if (i % 2 == 1) { while (sum <= 0) { sum++; counterNeg++; } } else { while (sum >= 0) { sum--; counterNeg++; } } } cout << min(counter, counterNeg) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
N = int(input()) a = [int(i) for i in input().split()] def pura(A): ans = 0 check = 0 if A[0] <= 0: ans += abs(1-A[0]) A[0] = 1 check += A[0] for i in range(1, N): if i % 2 != 0: if check + A[i] >= 0: ans += abs(A[i] + 1 + check) A[i] = -1 + check else: if check + A[i] <= 0: ans += abs(A[i] - (1 + check)) A[i] = 1 - check check += A[i] print(ans) return ans print(pura(a))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<long long> L(N); for (int i = 0; i < N; i++) { cin >> L.at(i); } long long v = 0, res = 0, le = 0; bool change_flag = true; for (int i = 0; i < N; i++) { if (i == 0) { v = L.at(i); } else { if (v > 0 && v + L.at(i) > 0) { le = -1 - v - L.at(i); L.at(i) += le; v += L.at(i); res += -le; le = 0; } else if (v > 0 && v + L.at(i) == 0) { le = -1; L.at(i) += le; v += L.at(i); res += -le; le = 0; } else if (v < 0 && v + L.at(i) < 0) { le = 1 - v - L.at(i); L.at(i) += le; v += L.at(i); res += le; le = 0; } else if (v < 0 && v + L.at(i) == 0) { le = 1; L.at(i) += le; v += L.at(i); res += le; le = 0; } else { v += L.at(i); } } } cout << res << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, ans; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } for (int i = 0; i < n - 1; i++) { int p = 0; for (int j = i + 1; j >= 0; j--) p += a.at(j); while (a.at(i) * a.at(i + 1) < 0 || p == 0) { if (a.at(i) > 0) { a.at(i + 1)--; ans++; continue; } if (a.at(i) < 0) { a.at(i + 1)++; ans++; continue; } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9; const long long MOD = 1e9 + 7; int main() { long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; ++i) { cin >> a[i]; } vector<long long> sum(n); long long ans = INF; for (long long i = 0; i < 2; ++i) { long long sum = 0; long long cnt = 0; for (long long j = 0; j < n; ++j) { sum += a[j]; if (j % 2 == i && sum <= 0) { cnt += 1 - sum; sum = 1; } else if (j % 2 == 1 - i && sum >= 0) { cnt += 1 + sum; sum = -1; } } ans = min(ans, cnt); } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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 nextposi = (a.at(0) < 0); int ans = 0; int sum = a.at(0); for (int i = 1; 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); } int ans = 0; if (a.at(0) == 0) { a.at(0) = 1; ans = solve(a); a.at(0) = -1; ans = min(ans, solve(a)) + 1; } else { ans = solve(a); } 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
def count_loop(n, old_state, count, a_list, flag): for i in range(1, int(n)): num_state = old_state + int(a_list[i]) if flag == 1: if num_state == 0: count += 1 old_state = -1 flag = -1 elif num_state > 0: count += num_state + 1 old_state = -1 flag = -1 else: old_state = num_state flag = -1 elif flag == -1: if num_state == 0: count += 1 old_state = 1 flag = 1 elif num_state < 0: count += abs(num_state) + 1 old_state = 1 flag = 1 else: old_state = num_state flag = 1 return count if __name__ == "__main__": n = input() a = input() a_list = a.split(" ") old_state = int(a_list[0]) count = 0 if old_state == 0: count += 1 c1 = count_loop(n,old_state,count,a_list,1) c2 = count_loop(n,old_state,count,a_list,-1) if c1 <= c2: print(c1) else: print(c2)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def main(): n = int(input()) A = list(map(int, input().split())) res = 0 sums = [] for i in range(n): if i == 0: sums.append(sum(A[:i]) + A[i]) if A[i] == 0: index = -1 for j in range(n): if A[j] != 0: index = A.index(A[j]) break if index == -1: res += 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: sums.append(sums[i-1] + A[i]) if sums[i] == 0: if sums[i-1] > 0: A[i] -= 1 sums[i] = sums[i-1] + A[i] res += 1 else: A[i] += 1 sums[i] = sums[i-1] + A[i] res += 1 elif (sums[i-1] > 0) and (sums[i] > 0): res += A[i] - (-sums[i-1] - 1) A[i] = -sums[i-1] - 1 sums[i] = sums[i-1] + A[i] elif (sums[i-1] < 0) and (sums[i] < 0): res += 1 - (sums[i-1] + A[i]) A[i] = abs(sums[i-1]) + 1 sums[i] = sums[i-1] + A[i] print(res) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) A=list(map(int,input().split())) S=[A[0]] for i in range(n-1): S.append(S[-1]+A[i+1]) ans1=0 s1=0 a=-1 for i in range(n): a=a*(-1) if a==1: ans1=ans1+max(0,1-(S[i]+s1)) s1=s1+max(0,1-(S[i]+s1)) else: ans1=ans1-min(0,-1-(S[i]+s1)) s1=s1+min(0,1-(S[i]+s1)) ans2=0 a=1 s2=0 for i in range(n): a=a*(-1) if a==1: ans2=ans2+max(0,1-(S[i]+s2)) s2=s2+max(0,1-(S[i]+s2)) else: ans2=ans2-min(0,-1-(S[i]+s2)) s2=s2+min(0,-1-(S[i]+s2)) print(min(ans1,ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, no, ans; int main() { cin >> n; int a; for (int i = 0; i < n; i++) { cin >> a; if (i == 0) { no = a; } else { if (no > 0) { no += a; if (no >= 0) { ans += no + 1; no = -1; } } else { no += a; if (no <= 0) { ans += no * -1 + 1; no = 1; } } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; constexpr auto INF = 1000000000; constexpr auto LLINF = 1LL << 62; constexpr auto mod = 1000000007; 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); int n; cin >> n; vector<int> a(n); for (int(i) = (0); (i) < (n); (i)++) cin >> a[i]; int p = 0, m = 0, sum = 0; for (int(i) = (0); (i) < (n); (i)++) { sum += a[i]; if (i % 2) { p += max(0, 1 + sum); chmin(sum, -1); } else { p += max(0, 1 - sum); chmax(sum, 1); } } sum = 0; for (int(i) = (0); (i) < (n); (i)++) { sum += a[i]; if (i % 2) { m += max(0, 1 - sum); chmax(sum, 1); } else { m += max(0, 1 + sum); chmin(sum, -1); } } cout << min(p, m) << "\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; const int N = 1000000; const long long mod = 1000000007; int main() { int n; cin >> n; vector<int> a(n); for (long long int i = 0; i < n; i++) cin >> a[i]; int a0_plus_sum = 0; int a0_plus_count = 0; int a0_minus_sum = 0; int a0_minus_count = 0; for (int i = 0; i < n; i++) { if (i % 2 == 1) { if (a0_plus_sum + a[i] < 0) { a0_plus_sum += a[i]; } else { a0_plus_count += 1 + (a0_plus_sum + a[i]); a0_plus_sum = -1; } } else if (i % 2 == 0) { if (a0_plus_sum + a[i] > 0) { a0_plus_sum += a[i]; } else { a0_plus_count += 1 - (a0_plus_sum + a[i]); a0_plus_sum = 1; } } } for (int i = 0; i < n; i++) { if (i % 2 == 1) { if (a0_minus_sum + a[i] > 0) { a0_minus_sum += a[i]; } else { a0_minus_count += -(a0_minus_sum + a[i]) + 1; a0_minus_sum = 1; } } else if (i % 2 == 0) { if (a0_minus_sum + a[i] < 0) { a0_minus_sum += a[i]; } else { a0_minus_count += (a0_minus_sum + a[i]) + 1; a0_minus_sum = -1; } } } std::cout << min(a0_minus_count, a0_plus_count) << std::endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } long long sum = a.at(0); long long op = 0; bool flag = sum > 0 ? 1 : 0; for (int j = 1; j < n; j++) { if (flag) { sum += a.at(j); while (sum >= 0) { op++; sum--; } flag = 0; } else { sum += a.at(j); while (sum <= 0) { op++; sum++; } flag = 1; } } cout << op << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def sgn(n): if n>0: return 1 elif n==0: return 0 else: return -1 class SegmentTree: # 初期化処理 # f : SegmentTreeにのせるモノイド # default : fに対する単位元 def __init__(self, size, f=lambda x,y : x+y, default=0): self.size = 2**(size-1).bit_length() # 簡単のため要素数Nを2冪にする self.default = default self.dat = [default]*(self.size*2-1) # 要素を単位元で初期化 self.f = f def update(self, i, x): i += self.size-1 self.dat[i] = x while i > 0: i = (i-1) >> 1 self.dat[i] = self.f(self.dat[i*2+1], self.dat[i*2+2]) def query(self, l, r, k=0, L=0, R=None): if R is None: R = self.size if R <= l or r <= L: return self.default if l <= L and R <= r: return self.dat[k] else: lres = self.query(l, r, k*2+1, L, (L+R) >> 1) rres = self.query(l, r, k*2+2, (L+R) >> 1, R) return self.f(lres, rres) n=int(input()) b=[int(i) for i in input().split()] l = SegmentTree(size=n) p0=0 for i in range(n): l.update(i,b[i]) for i in range(n): sg=(-1)**(i) v=l.query(0,i+1) #print(v,p0) if sgn(v) != sg: tmp= abs(v)+1 #print(f'{b[i]}---{tmp}--->{sg}') p0 += tmp l.update(i,b[i]+tmp*sg) l = SegmentTree(size=n) for i in range(n): l.update(i,b[i]) p1=0 #print() for i in range(n): sg=(-1)**(i+1) v=l.query(0,i+1) #print(v,p1) if sgn(v) != sg: tmp= abs(v)+1 #print(f'{b[i]}---{tmp}--->{sg}') p1 += tmp l.update(i,b[i]+tmp*sg) #print(l.query(0,i+1),p1) print(min(p0,p1))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int check(vector<double> &sum, int n) { for (int i = 0; i < n; i++) { if (sum[i] == 0) return i; if (i < n - 1 && sum[i] * sum[i + 1] > 0) return i + 1; } return 0; } void kasan(vector<double> &sum, int index, int pm, int n) { if (pm == 0) { for (int i = index; i < n; i++) sum[i]++; } if (pm == 1) { for (int i = index; i < n; i++) sum[i]--; } for (int j = 0; j < n; j++) { cout << sum[j] << endl; } cout << "" << endl; } int main() { int n; double a; double sum = 0; cin >> n; vector<double> sums(n); for (int i = 0; i < n; i++) { cin >> a; sum += a; sums[i] = sum; } int count = 0; while (check(sums, n) > 0) { int j = check(sums, n); if (sums[j - 1] > 0) kasan(sums, j, 1, n); else if (sums[j - 1] < 0) kasan(sums, j, 0, n); else if (sums[j] == 0) { if (sums[j - 1] > 0) kasan(sums, j, 1, n); else if (sums[j - 1] < 0) kasan(sums, j, 0, n); } count++; } cout << count << endl; return 0; }