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> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx") using namespace std; template <typename T> inline void priv(vector<T> a) { for (int i = 0; i < a.size(); i++) { cerr << a[i] << ((i == a.size() - 1) ? "\n" : " "); } } inline void fastio() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } int_fast64_t gcd(int_fast64_t a, int_fast64_t b) { int_fast64_t c = max(a, b); int_fast64_t d = min(a, b); return c == 0 || d == 0 ? c : gcd(c % d, d); } int_fast64_t lcm(int_fast64_t a, int_fast64_t b) { return a == 0 || b == 0 ? 0 : a * b / gcd(a, b); } int_fast64_t modfact(int_fast64_t a) { int_fast64_t b = 1; for (int i = 2; i <= a; i++) b = b * i % 1000000007LL; return b; } int_fast64_t modpow(int_fast64_t a, int_fast64_t n) { int_fast64_t b = 1; while (n > 0) { if (n & 1) b = b * a % 1000000007LL; a = a * a % 1000000007LL; n >>= 1; } return b; } int_fast64_t modcomb(int_fast64_t n, int_fast64_t k) { int_fast64_t b = 1; k = min(n - k, k); for (int i = n; i >= n - k + 1; i--) b = b * i % 1000000007LL; return b * modpow(modfact(k), 1000000007LL - 2) % 1000000007LL; } int_fast64_t N, S, ans; int_fast64_t A[100001]; int main() { fastio(); cin >> N; for (int i = 0; i < N; i++) cin >> A[i]; ans = 0, S = A[0]; for (int i = 1; i <= N - 1; i++) { if (S * (S + A[i]) < 0) { S += A[i]; } else { if (S > 0) { ans += abs(A[i] - (-S - 1)); S = -1; } else { ans += abs(A[i] - (-S + 1)); S = 1; } } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-} {-# OPTIONS_GHC -fno-warn-unused-imports #-} {-# OPTIONS_GHC -fno-warn-missing-signatures #-} {-# OPTIONS_GHC -fno-warn-unused-binds #-} {-# OPTIONS_GHC -fno-warn-incomplete-patterns #-} {-# OPTIONS_GHC -fno-warn-name-shadowing #-} {-# LANGUAGE OverloadedStrings #-} -- {-# LANGUAGE FlexibleContexts #-} -- {-# LANGUAGE MultiWayIf #-} -- {-# LANGUAGE BangPatterns #-} -- {-# LANGUAGE ViewPatterns #-} -- {-# LANGUAGE TupleSections #-} import System.IO hiding (char8) import Control.Applicative import Control.Monad import Data.List import Data.Tuple import Data.Int import Data.Char import Data.Function (on) import Data.Ord (comparing) import Data.Monoid (mappend) import Data.Array -- import Data.Array.Unboxed -- import Data.Array.IArray -- import Data.Array.ST -- import Data.Array.MArray -- import Data.Array.Unsafe -- import Data.Array.Base(unsafeRead, unsafeWrite) -- import Data.Array.IO import Data.Ix import Data.Maybe -- import Data.Monoid hiding ((<>)) import qualified Data.ByteString.Char8 as BS import qualified Data.ByteString.Lazy.Char8 as BL import Data.ByteString.Builder -- import Data.Graph -- import Data.Vector.Unboxed ((//), (++), (!), (!?)) -- import qualified Data.Vector.Unboxed as U -- import Data.IntSet (IntSet) -- import qualified Data.IntSet as IntSet -- import Data.IntMap.Strict (IntMap) -- import qualified Data.IntMap.Strict as IntMap -- import Data.Sequence ((|>), (<|), (><),ViewR(..), ViewL(..), Seq) -- import qualified Data.Sequence as Seq -- import Data.Foldable (toList, minimumBy) -- import Debug.Trace main = solve <$ getInt1 <*> getInt64s >>= print solve = solve' 0 0 . scanl (+) 0 where solve' _ ans [_] = ans solve' acc ans (x:x2:xs) | x2+acc == 0 = solve' (acc-signum x) (ans+1) (x2+acc-signum x:xs) | signum x == signum (x2+acc) = solve' (acc+d) (ans+abs d) (x2+acc+d:xs) | otherwise = solve' acc ans (x2+acc:xs) where d = - signum (x2+acc) * (abs (x2+acc) + 1) -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- getInt64s :: IO [Int64] getInt64s = readInt64s <$> BS.getLine getInt1 :: IO Int getInt1 = readInt1 <$> BS.getLine getInt2 :: IO (Int, Int) getInt2 = readInt2 <$> BS.getLine getInt3 :: IO (Int, Int, Int) getInt3 = readInt3 <$> BS.getLine getInts :: IO [Int] getInts = readInts <$> BS.getLine getIntN :: Int -> IO [Int] getIntN n = map readInt1 <$> replicateM n BS.getLine format :: Show a => Maybe a -> IO () format Nothing = putStrLn "NO" format (Just a) = putStrLn "YES" >> print a -- [1,2,3] -> 1 2 3 putInts :: [Int] -> IO () -- putInts [] = return () -- putInts xs = BL.putStrLn . toLazyByteString . foldl1 mappend . intersperse (char8 ' ') $ map intDec xs putInts = putStrLn . unwords . map show readInt1 :: BS.ByteString -> Int readInt1 = fst . fromJust . BS.readInt readInt2 :: BS.ByteString -> (Int,Int) readInt2 = toTuple . readInts readInt3 :: BS.ByteString -> (Int,Int,Int) readInt3 = toTriple . readInts readInts :: BS.ByteString -> [Int] readInts = map readInt1 . BS.words readInt641 :: BS.ByteString -> Int64 readInt641 = fromIntegral . fst . fromJust . BS.readInteger readInt642 :: BS.ByteString -> (Int64,Int64) readInt642 = toTuple . readInt64s readInt643 :: BS.ByteString -> (Int64,Int64,Int64) readInt643 = toTriple . readInt64s readInt64s :: BS.ByteString -> [Int64] readInt64s = map readInt641 . BS.words readInteger1 :: BS.ByteString -> Integer readInteger1 = fst . fromJust . BS.readInteger readInteger2 :: BS.ByteString -> (Integer,Integer) readInteger2 = toTuple . readIntegers readInteger3 :: BS.ByteString -> (Integer,Integer,Integer) readInteger3 = toTriple . readIntegers readIntegers :: BS.ByteString -> [Integer] readIntegers = map readInteger1 . BS.words toTuple :: [a] -> (a, a) toTuple [x, y] = (x, y) toTriple :: [a] -> (a, a, a) toTriple [x, y, z] =(x, y, z) fromTuple :: (a, a) -> [a] fromTuple (x, y) = [x, y] fromTriple :: (a, a, a) -> [a] fromTriple (x, y, z) = [x, y, z] -- if not applying, use "id" applyTuple :: (a -> a') -> (b -> b') -> (a, b) -> (a', b') applyTuple f g (x, y) = (f x, g y) applyTriple :: (a -> a') -> (b -> b') -> (c -> c') -> (a, b, c) -> (a', b', c') applyTriple f g h (x, y, z) = (f x, g y, h z) -- @since 4.8.0.0 sortOn' :: Ord b => (a -> b) -> [a] -> [a] sortOn' f = map snd . sortBy (comparing fst) . map (\x -> let y = f x in y `seq` (y, x))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); long long sum = 0, cost = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; if (a[0] > 0) { if (i % 2 == 0 && sum < 0) { cost += 1 - sum; sum = 1; } if (i % 2 == 1 && sum > 0) { cost += sum + 1; sum = -1; } } else { if (i % 2 == 0 && sum > 0) { cost += 1 + sum; sum = -1; } if (i % 2 == 1 && sum < 0) { cost += 1 - sum; sum = 1; } } } cout << cost << 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> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } long sum_ = a[0]; long count = 0; if (a[0] > 0) { for (int i = 1; i < n; ++i) { if (i % 2 == 1) { if (sum_ + a[i] >= 0) { long r = -(sum_ + a[i]) - 1; count += abs(r); a[i] += r; } } else { if (sum_ + a[i] <= 0) { long r = -(sum_ + a[i]) + 1; count += abs(r); a[i] += r; } } sum_ += a[i]; } } else { for (int i = 1; i < n; ++i) { if (i % 2 == 1) { if (sum_ + a[i] <= 0) { long r = -(sum_ + a[i]) + 1; count += abs(r); a[i] += r; } } else { if (sum_ + a[i] >= 0) { long r = -(sum_ + a[i]) - 1; count += abs(r); a[i] += r; } } sum_ += a[i]; } } cout << count << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long n; cin >> n; int i; long int a[n], su, cnt, cnt2; cnt = 0; cnt2 = 0; for (i = 0; i < n; i++) { cin >> a[i]; } su = 0; for (i = 0; i < n; i++) { su += a[i]; if (a[0] >= 0) { if (i % 2 == 0) { if (su <= 0) { cnt += 1 - su; su = 1; } } else { if (su >= 0) { cnt += su + 1; su = -1; } } } else { if (i % 2 == 0) { if (su >= 0) { cnt += su + 1; su = -1; } } else { if (su <= 0) { cnt += 1 - su; su = 1; } } } } su = 0; for (i = 0; i < n; i++) { su += a[i]; if (a[0] <= 0) { if (i % 2 == 0) { if (su <= 0) { cnt2 += 1 - su; su = 1; } } else { if (su >= 0) { cnt2 += su + 1; su = -1; } } } else { if (i % 2 == 0) { if (su >= 0) { cnt2 += su + 1; su = -1; } } else { if (su <= 0) { cnt2 += 1 - su; su = 1; } } } } cout << min(cnt, cnt2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int 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 = 0; long long int tmp, old_sum = a[0], sum = 0; for (int i = 1; i < n; ++i) { sum = old_sum + a[i]; if (sum == 0) { ans++; if (old_sum >= 0) { sum--; } else { sum++; } } if ((old_sum >= 0) && (sum >= 0)) { ans += (sum + 1); sum = -1; } else if ((old_sum < 0) && (sum < 0)) { ans += abs(sum - 1); sum = 1; } old_sum = sum; } tmp = 0; old_sum = 0, sum = 0; tmp += abs(a[0]) + 1; old_sum -= a[0]; for (int i = 1; i < n; ++i) { sum = old_sum + a[i]; if (sum == 0) { tmp++; if (old_sum >= 0) { sum--; } else { sum++; } } if ((old_sum >= 0) && (sum >= 0)) { tmp += (sum + 1); sum = -1; } else if ((old_sum < 0) && (sum < 0)) { tmp += abs(sum - 1); sum = 1; } old_sum = sum; } cout << min(ans, tmp) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> A(N); for (int x = 0; x < (N); x++) { cin >> A.at(x); } long long sign = A.at(0); long long ans = 0; for (int x = 0; x < (N - 1); x++) { if (sign < 0) { if (sign + A.at(x + 1) <= 0) { ans += abs(A.at(x + 1) - (abs(sign) + 1)); A.at(x + 1) = abs(sign) + 1; } } else { if (sign + A.at(x + 1) >= 0) { ans += abs(A.at(x + 1) - (-abs(sign) - 1)); A.at(x + 1) = -abs(sign) - 1; } } sign += A.at(x + 1); } cout << ans << endl; ; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { cin.sync_with_stdio(false); int n; cin >> n; long long a[n], sum[n]; bool ok = true; for (int i = (int)(0); i < (int)(n); i++) { cin >> a[i]; if (i == 0) sum[i] = a[i]; else sum[i] = sum[i - 1] + a[i]; if (sum[i] == 0 || (i > 0 && sum[i] * sum[i - 1] < 0)) ok = false; } if (ok) { cout << 0 << endl; } else { long long sump = (a[0] != 0) ? a[0] : 1, sumq = (a[0] != 0) ? -a[0] : -1; long long p = (a[0] != 0) ? 0 : 1, q = (a[0] != 0) ? 0 : 1; for (int i = (int)(1); i < (int)(n); i++) { if ((sump + a[i]) * sump < 0) sump = sump + a[i]; else if (sump < 0) p += abs(1 - (sump + a[i])), sump = 1; else if (sump > 0) p += abs(-1 - (sump + a[i])), sump = -1; if ((sumq + a[i]) * sumq < 0) sumq = sumq + a[i]; else if (sumq < 0) q += abs(1 - (sumq + a[i])), sumq = 1; else if (sumq > 0) q += abs(-1 - (sumq + a[i])), sumq = -1; } cout << min(p, q) << endl; } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) A = list(map(int, input().split())) B = [0]*(n+1) ans = 0 for i in range(n): temp = B[i]+A[i] if i == 0: if temp == 0: ans += 1 temp += 1 B[i+1] = temp else: B[i+1] =temp else: if B[i]*temp > 0: if temp < 0: ans += 1-temp B[i+1] = 1 else: ans += temp+1 B[i+1] = -1 elif temp == 0: if B[i] > 0: ans += 1 B[i+1] = -1 else: ans += 1 B[i+1] = 1 else: B[i+1] = temp ans_ = 0 for i in range(n): temp = B[i]+A[i] if i == 0: if temp == 0: ans_ += 1 temp -= 1 B[i+1] = temp else: B[i+1] =temp else: if B[i]*temp > 0: if temp < 0: ans_ += 1-temp B[i+1] = 1 else: ans_ += temp+1 B[i+1] = -1 elif temp == 0: if B[i] > 0: ans_ += 1 B[i+1] = -1 else: ans_ += 1 B[i+1] = 1 else: B[i+1] = temp print(min(ans, ans_))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; long long a[1000010]; int n; unsigned long long solve() { unsigned long long sum = 0; long long oo = 0, flag; if (a[0] > 0) flag = -1; else if (a[0] < 0) flag = 1; for (int i = 0; i < n; i++) { oo += a[i]; if (flag == 1) { if (oo >= 0) { sum += oo + 1; oo = -1; } } if (flag == -1) { if (oo <= 0) { sum += 0 - oo + 1; oo = 1; } } flag = -flag; } return sum; } int main() { while (scanf("%d", &n) != EOF) { unsigned long long sum; for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); } if (a[0] == 0) { a[0] = 1; unsigned long long sum1 = solve(); a[0] = -1; unsigned long long sum2 = solve(); sum = min(sum1, sum2) + 1; } else { unsigned long long sum1 = solve(); sum = sum1; } printf("%lld\n", sum); } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
import std.stdio, std.algorithm, std.conv, std.array, std.string; void main() { auto n = readln.chomp.to!uint; auto as = readln.chomp.split(" ").map!(to!long).array; long op; auto sum = as[0]; foreach (a; as[1..$]) { if (sum < 0) { if ((sum + a) <= 0) { op += (1 - (sum + a)); sum = 1; } else { sum += a; } } else { if ((sum + a) >= 0) { op += sum + a + 1; sum = -1; } else { sum += a; } } } writeln(op); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; long long a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } long long cnt = 0; long long ans = 0; long long p = -1; if (a[0] > 0) p = 1; for (int i = 0; i < n; ++i) { cnt += a[i]; if (cnt * p <= 0) { long long g = cnt * -1 + p; ans += (g * p); cnt = cnt + g; } p *= -1; } cout << (ans) << "\n"; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { long long int n, ans = 0, sum = 0; cin >> n; vector<long long int> a(n); for (size_t i = 0; i < n; i++) { cin >> a[i]; if (i == 0) sum += a[i]; else { if (sum * (sum + a[i]) > 0) { if (sum < 0) { if (a[i] < 0) { a[i] += abs(sum) * 2; ans += abs(sum) * 2; } else { a[i] += abs(sum); ans += abs(sum); } } else if (sum > 0) { if (a[i] > 0) { a[i] -= abs(sum) * 2; ans += abs(sum) * 2; } else { a[i] -= abs(sum); ans += abs(sum); } } } if (sum + a[i] == 0) { if (sum > 0) a[i]--; else a[i]++; ans++; } sum += a[i]; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> a(N); for (int i=0; i<N; i++) { cin >> a.at(i); } int ans1 = 0, ans2 = 0; sum = 0; for (int i=0; i<N; i++) { sum += a.at(i); if (i%2 == 0 and sum <= 0) { ans1 += -sum+1; //sumは負 sum = 1; } else if (i%2 == 1 and sum >= 0) { ans1 += sum+1; sum = -1; } } sum = 0; for (int i=0; i<N; i++) { ans2 += a.at(i); if (i%2 == 0 and sum >= 0) { ans2 += sum+1; sum = -1; } else if (i%2 == 1 and sum <= 0) { ans2 += -sum+1; sum = 1; } } int ans = min(ans1, ans2); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def sequence(N: int, A: list) -> int: s = A[0] op = 0 for a in A[1:]: print(s, '->', s+a) if s < 0: if s + a > 0: # OK s = s + a continue else: op += 1 - (s + a) s = 1 else: # s > 0 if s + a < 0: # OK s = s + a continue else: op += (s + a) - (-1) s = -1 return op if __name__ == "__main__": N = int(input()) A = [int(s) for s in input().split()] ans = sequence(N, A) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
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 ans = 0; if (a[0] == 0 && a[1] >= 0) { a[0] = -1; ans++; } else if (a[0] == 0 && a[1] < 0) { a[0] = 1; ans++; } for (int i = 1; i < n; i++) { a[i] += a[i - 1]; if (a[i] >= 0 && a[i - 1] > 0) { ans += abs(a[i] + 1); a[i] = -1; } else if (a[i] <= 0 && a[i - 1] < 0) { ans += abs(a[i] - 1); 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
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int sum; cin >> sum; int answer = 0; for (int i = 1; i < n; i++) { int a; cin >> a; int nextSum = sum + a; if ((sum > 0 && nextSum < 0) || (sum < 0 && nextSum > 0)) { sum = nextSum; } else if (nextSum == 0) { sum = sum > 0 ? -1 : 1; answer += 1; } else if (nextSum > 0) { sum = -1; answer += nextSum + 1; } else if (nextSum < 0) { sum = 1; answer += nextSum * -1 + 1; } } cout << answer << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long[] a = new long[n]; for(int i = 0; i < n; i++) { a[i] = sc.nextLong(); } long sum = a[0]; long x = 0; if(a[0] == 0) { if(a[1] >= 0) { sum = -1; x++; } else { sum = 1; x++; } } long count = 0; for(int i = 1; i < n; i++) { if(sum * (sum + a[i]) >= 0) { if(sum < 0) { count = -sum - a[i] + 1; } else { count = -sum - a[i] - 1; } } sum += a[i] + count; x += Math.abs(count); count = 0; } System.out.println(x); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); long n = sc.nextLong(); long a[] = new long[(int)n]; for(int i = 0; i < n; i++) { a[i] = sc.nextLong(); } long sum = a[0]; if(sum == 0) { boolean flag = true; int cnt = 1; while(flag) { if(a[cnt] > 0) { if(cnt % 2 == 0) { sum = 1; } else { sum = -1; } flag = false; } else if(a[cnt] < 0) { if(cnt % 2 == 0) { sum = -1; } else { sum = 1; } flag = false; } cnt++; } } long ans = 0; for(int i = 1; i < n; i++) { if(sum > 0) { if(sum + a[i] >= 0) { ans += sum + a[i] + 1; sum = -1; } else { sum = sum + a[i]; } } else { if(sum + a[i] <= 0) { ans += Math.abs(a[i] + sum) + 1; sum = 1; } else { sum = sum + a[i]; } } } 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
python3
N = int(input()) A = list(map(int, input().split())) currentSum = 0 count1 = 0 count2 = 0 count3 = 0 count4 = 0 for i in range(N): restSum = currentSum currentSum += A[i] if currentSum <= 0 and restSum < 0: count1 += abs(currentSum) + 1 currentSum = 1 elif currentSum >= 0 and restSum > 0: count1 += abs(currentSum) + 1 currentSum = -1 elif currentSum == 0 and restSum == 0: count1 += 1 currentSum = -1 currentSum = 0 for i in range(N): restSum = currentSum currentSum += A[i] if currentSum <= 0 and restSum < 0: count2 += abs(currentSum) + 1 currentSum = 1 elif currentSum >= 0 and restSum > 0: count2 += abs(currentSum) + 1 currentSum = -1 elif currentSum == 0 and restSum == 0: count2 += 1 currentSum = 1 currentSum = 0 for i in range(N): restSum = currentSum currentSum += A[i] if currentSum <= 0 and restSum < 0: count3 += abs(currentSum) + 1 currentSum = 1 elif currentSum >= 0 and restSum > 0: count3 += abs(currentSum) + 1 currentSum = -1 elif A[i] <= 0 and restSum == 0: count3 += 1 currentSum = 1 currentSum = 0 for i in range(N): restSum = currentSum currentSum += A[i] if currentSum <= 0 and restSum < 0: count4 += abs(currentSum) + 1 currentSum = 1 elif currentSum >= 0 and restSum > 0: count4 += abs(currentSum) + 1 currentSum = -1 elif A[i] >= 0 and restSum == 0: count4 += 1 currentSum = -1 print(min(count1, count2, count3, count4))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) b = [0]*n b[0]=a[0] pt = 0 m = 0 ans = 0 for i in range(1,n): b[i]=b[i-1]+a[i] bo = b[::2] be = b[1::2] if sum(bo)>sum(be): pt=0 else: pt=1 if pt==0: for i in range(n): if i%2==0 and b[i]+m<=0: ans+=1-b[i]-m m+=1-b[i]-m elif i%2==1 and b[i]+m>=0: ans+=1+b[i]+m m+=-1-b[i]-m if pt==1: for i in range(n): if i%2==1 and b[i]+m<=0: ans+=1-b[i]-m m+=1-b[i]-m elif i%2==0 and b[i]+m>=0: ans+=1+b[i]+m m+=-1-b[i]-m print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); unsigned long op = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } if (a[0] == 0) { for (int i = 1; i < n; i++) { if (a[i] == 0) { continue; } else if (a[i] > 0) { if (i % 2 == 0) { a[0] = 1; } else { a[0] = -1; } op = 1; break; } else { if (i % 2 == 0) { a[0] = -1; } else { a[0] = 1; } op = 1; break; } } if (op == 0) { a[0] = 1; op = 1; } } long sum = a[0]; for (int i = 1; i < n; i++) { if (sum > 0) { if (sum + a[i] >= 0) { op += abs(-1 - sum - a[i]); sum = -1; } else { sum += a[i]; } } else { if (sum + a[i] <= 0) { op += abs(1 - sum - a[i]); sum = 1; } else { sum += a[i]; } } } cout << op << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<bits/stdc++.h> #define int long long #define r(i,n) for(int i=0;i<n;i++) using namespace std; int a[100009],n; int d1(){ int sum=0,ans=0; for(int i=0;i<n;i++){ if(i%2==0){ if(sum+a[i]>=0)ans+=sum-a[i]+1,sum=-1; else sum+=a[i]; } else{ if(sum+a[i]<=0)ans+=sum-a[i]-1,sum=1; else sum+=a[i]; } } return ans; } int d2(){ int sum=0,ans=0; for(int i=0;i<n;i++){ if(i%2==1){ if(sum+a[i]>=0)ans+=sum-a[i]+1,sum=-1; else sum+=a[i]; } else{ if(sum+a[i]<=0)ans+=sum-a[i]-1,sum=1; else sum+=a[i]; } } return ans; } main(){ cin>>n; r(i,n)cin>>a[i]; cout<<min(d1(),d2())<<endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int dx[4] = {1, 0, 0, -1}; int dy[4] = {0, 1, -1, 0}; using namespace std; bool cmp_P(const pair<long long int, long long int> &a, const pair<long long int, long long int> &b) { return a.second < b.second; } int main() { long long int tmp = 0, n, sum = 0, v, res = 0; cin >> n; vector<long long int> a(n + 1); for (int i = 0; i < (int)(n); i++) cin >> a[i]; if (a[0] != 0) { v = abs(a[0]) / a[0]; sum = a[0]; for (int i = 1; i < n; i++) { if (v == -1) { if (a[i] + sum <= 0) { tmp += abs(1 - a[i] - sum); sum = 1; } else { sum += a[i]; } } else { if (a[i] + sum >= 0) { tmp += abs(-1 - a[i] - sum); sum = -1; } else { sum += a[i]; } } v = -v; } v = -abs(a[0]) / a[0]; sum = v; res += abs(a[0]) + 1; for (int i = 1; i < n; i++) { if (v == -1) { if (a[i] + sum <= 0) { res += abs(1 - a[i] - sum); sum = 1; } else { sum += a[i]; } } else { if (a[i] + sum >= 0) { res += abs(-1 - a[i] - sum); sum = -1; } else { sum += a[i]; } } v = -v; } res = min(res, tmp); } else { v = 1; tmp++; sum = 1; for (int i = 1; i < n; i++) { if (v == -1) { if (a[i] + sum <= 0) { tmp += abs(1 - a[i] - sum); sum = 1; } else { sum += a[i]; } } else { if (a[i] + sum >= 0) { tmp += abs(-1 - a[i] - sum); sum = -1; } else { sum += a[i]; } } v = -v; } v = 1; res++; sum = 1; for (int i = 1; i < n; i++) { if (v == -1) { if (a[i] + sum <= 0) { res += abs(1 - a[i] - sum); sum = 1; } else { sum += a[i]; } } else { if (a[i] + sum >= 0) { res += abs(-1 - a[i] - sum); sum = -1; } else { sum += a[i]; } } v = -v; } res = min(res, tmp); } cout << res << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, count, sum, x, bsum, s, count2; vector<int> a, b; cin >> n; a.resize(n); b.resize(n); cin >> a[0]; for (i = 1; i < n; i++) { cin >> a[i]; } b = a; count = 0; count2 = 0; if (a[0] <= 0) { count = abs(a[0]) + 1; a[0] = 1; } sum = 0; for (int i = 0; i < n - 1; i++) { sum += a[i]; if (sum + a[i + 1] == 0) { count++; if (sum > 0) { a[i + 1]--; } else { a[i + 1]++; } } if (sum > 0 && (sum + a[i + 1]) > 0 || sum < 0 && (sum + a[i + 1]) < 0) { if (sum > 0) { x = sum + 1; s = x + a[i + 1]; a[i + 1] = -x; count += abs(s); } else { x = sum - 1; s = x + a[i + 1]; a[i + 1] = -x; count += abs(s); } } } if (b[0] >= 0) { count2 = abs(b[0]) + 1; b[0] = -1; } sum = 0; for (int i = 0; i < n - 1; i++) { sum += b[i]; if (sum + b[i + 1] == 0) { count2++; if (sum > 0) { b[i + 1]--; } else { b[i + 1]++; } } if (sum > 0 && (sum + b[i + 1]) > 0 || sum < 0 && (sum + b[i + 1]) < 0) { if (sum > 0) { x = sum + 1; s = x + b[i + 1]; b[i + 1] = -x; count2 += abs(s); } else { x = sum - 1; s = x + b[i + 1]; b[i + 1] = -x; count2 += abs(s); } } } cout << min(count, count2); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long int cnt; long long int x; long long int ans = 0; cin >> cnt; for (int i = 1; i < n; i++) { cin >> x; if (cnt < 0) { if (0 < cnt + x) { cnt += x; } else { ans += 1 - (cnt + x); cnt = 1; } } else if (0 < cnt) { if (cnt + x < 0) { cnt += x; } else { ans += (cnt + x) + 1; cnt = -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
java
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.OutputStream; import java.io.IOException; import java.io.FileReader; import java.io.FileWriter; import java.util.Arrays; import java.util.Collections; import java.util.ArrayList; import java.util.List; import java.util.HashSet; import java.util.Comparator; import java.util.Set; import java.util.HashMap; import java.util.Map; public class Main { // 標準入力 static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 標準入力数値配列用 int static int[] inputval() throws Exception { String[] strarray = br.readLine().trim().split(" "); int[] intarray = new int[strarray.length]; for (int i = 0; i < intarray.length; i++) { intarray[i] = Integer.parseInt(strarray[i]); } return intarray; } /* 標準入力数値配列用 long */ static long[] inputLongArr() throws Exception { String[] strarray = br.readLine().trim().split(" "); long[] longarray = new long[strarray.length]; for (int i = 0; i < longarray.length; i++) { longarray[i] = Long.parseLong(strarray[i]); } return longarray; } // 標準入力数値リスト用 int static List<Integer> inputIntList() throws Exception { List<String> strList = Arrays.asList(br.readLine().trim().split(" ")); List<Integer> intList = new ArrayList<Integer>(); for (String elem : strList){ intList.add(Integer.parseInt(elem)); } return intList; } // 標準入力数値配列用 integer 降順ソート用 static Integer[] inputvalInteger() throws Exception { String[] strarray = br.readLine().trim().split(" "); Integer[] intarray = new Integer[strarray.length]; for (int i = 0; i < intarray.length; i++) { intarray[i] = Integer.parseInt(strarray[i]); } return intarray; } /*標準入力long*/ static long inputLong() throws Exception { return Long.parseLong(br.readLine()); } /*標準入力long*/ static int inputInt() throws Exception { return Integer.parseInt(br.readLine()); } public static void main(String[] args) throws Exception { // write your code here int n = inputInt(); long [] al = inputLongArr(); long sum = al[0]; long ans = 0; boolean nextPlusF = al[0] < 0; for(int i=1;i<n;i++){ sum += al[i]; if(nextPlusF && sum <=0){ ans += 1-sum; sum += ans; }else if ((! nextPlusF) && sum >= 0){ ans += sum +1; sum -= ans; } nextPlusF = !nextPlusF; } 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
python3
import copy n = int(input()) a = list(map(int, input().split())) check = copy.deepcopy(a) #####segfunc###### def segfunc(x,y): return x+y def init(init_val): #set_val for i in range(len(init_val)): seg[i+num-1]=init_val[i] #built for i in range(num-2,-1,-1) : seg[i]=segfunc(seg[2*i+1],seg[2*i+2]) def update(k,x): k += num-1 seg[k] = x while k: k = (k-1)//2 seg[k] = segfunc(seg[k*2+1],seg[k*2+2]) def query(p,q): if q<=p: return ide_ele p += num-1 q += num-2 res=ide_ele while q-p>1: if p&1 == 0: res = segfunc(res,seg[p]) if q&1 == 1: res = segfunc(res,seg[q]) q -= 1 p = p//2 q = (q-1)//2 if p == q: res = segfunc(res,seg[p]) else: res = segfunc(segfunc(res,seg[p]),seg[q]) return res #####単位元###### ide_ele = 0 num =2**(n-1).bit_length() seg=[ide_ele]*(2*num - 1) init(a) ans_1 = 0 pre_sum = (-1) * a[0] for i in range(n): q_sum = query(0,i+1) if q_sum * pre_sum >= 0: if pre_sum < 0: update(i, abs(pre_sum) + 1) else: update(i, (-1) * (pre_sum+1)) pre_sum = query(0,i+1) for i in range(n): ans_1 += abs(check[i] - seg[i + num - 1]) if a[0] == 0: a[0] = 1 ans_2 = 1 else: ans_2 = abs(a[0] * 2) a[0] *= -1 pre_sum = (-1) * a[0] init(a) for i in range(n): q_sum = query(0,i+1) if q_sum * pre_sum >= 0: if pre_sum < 0: update(i, abs(pre_sum) + 1) else: update(i, (-1) * (pre_sum+1)) pre_sum = query(0,i+1) for i in range(n): ans_2 += abs(check[i] - seg[i + num - 1]) print(min(ans_1,ans_2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) _arr = list(map(int, input().split())) ans = [] for pm in (1, -1): arr = _arr[:] c = 0 prev = 0 for i in range(n): t = prev + arr[i] if prev == 0 and t == 0: arr[i] = pm elif prev > 0 and t >= 0: diff = t + 1 c += diff arr[i] -= diff elif prev < 0 and t <= 0: diff = -1 * t + 1 c += diff arr[i] += diff prev += arr[i] ans.append(c) print(min(ans))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int counter = 0; if (a[0] >= 0) { for (int i = 1; i < n; i++) { int total = 0; for (int j = 0; j <= i; j++) { total += a[j]; } if (i % 2 == 0 && total <= 0) { counter += abs(total - 1); a[i] += abs(total - 1); } else if (i % 2 != 0 && total >= 0) { counter += abs(total - (-1)); a[i] -= abs(total - (-1)); } } } else { for (int i = 1; i < n; i++) { int total = 0; for (int j = 0; j <= i; j++) { total += a[j]; } if (i % 2 == 0 && total >= 0) { counter += abs(total - (-1)); a[i] -= abs(total - (-1)); } else if (i % 2 != 0 && total <= 0) { counter += abs(total - 1); a[i] += abs(total - 1); } } } cout << counter << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n], sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } int kekka = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0 && sum >= 0) { kekka += sum + 1; sum = -1; } if (i % 2 == 1 && sum <= 0) { kekka += -sum + 1; sum = 1; } } int result = 0; sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (i % 2 == 0 && sum <= 0) { result += -sum + 1; sum = 1; } if (i % 2 == 1 && sum >= 0) { result += sum + 1; sum = -1; } } cout << min(kekka, result) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# https://atcoder.jp/contests/abc059/tasks/arc072_a # 累積和を作っておいて、その累積和の符号が交互になるようにしていく import sys read = sys.stdin.readline def read_ints(): return list(map(int, read().split())) def read_a_int(): return int(read()) class cumsum1d: # 一次元累積和クラス def __init__(self, ls: list): ''' 1次元リストを受け取る ''' from itertools import accumulate self.ls_accum = [0] + list(accumulate(ls)) def total(self, i, j): # もとの配列lsにおける[i,j)の中合計 return self.ls_accum[j] - self.ls_accum[i] def __call__(self, i): # i番目までの合計 return self.ls_accum[i + 1] N = read_a_int() A = read_ints() A = cumsum1d(A) # ただし第一項が0とか途中0になる場合がめんどくさい # →二通りやって少ないほうでいいじゃん ans1 = 0 bias = 0 pre_sign = 1 # 第一項が負と仮定 for i in range(N): now = A(i) + bias if (-1, 1)[pre_sign] * now < 0: # 異符号 pass elif (-1, 1)[pre_sign] * now >= 0: # 同符号 ans1 += abs(now) + 1 bias += (1, -1)[pre_sign] * ans1 pre_sign = 1 - pre_sign ans2 = 0 bias = 0 pre_sign = 0 # 第一項が正と仮定 for i in range(N): now = A(i) + bias if (-1, 1)[pre_sign] * now < 0: # 異符号 pass elif (-1, 1)[pre_sign] * now >= 0: # 同符号 ans2 += abs(now) + 1 bias += (1, -1)[pre_sign] * ans2 pre_sign = 1 - pre_sign print(min(ans1, ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace ::std; int N, a[100000], A, ans[2]; int min(int x, int y) { if (x > y) return y; return x; } int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> a[i]; } A = 0; for (int i = 0; i < N; i++) { A += a[i]; if (A >= 0 && i % 2 == 0) { ans[0] += A + 1; A = -1; } if (A <= 0 && i % 2 == 1) { ans[0] += -A + 1; A = 1; } } A = 0; for (int i = 0; i < N; i++) { A += a[i]; if (A >= 0 && i % 2 == 1) { ans[1] += A + 1; A = -1; } if (A <= 0 && i % 2 == 0) { ans[1] += -A + 1; A = 1; } } cout << min(ans[0], ans[1]); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1000000000; const long long MOD = (long long)1e9 + 7; template <class T> inline T in() { T x; cin >> x; return x; } signed main() { long long n = in<long long>(); vector<long long> v(n, 0); for (long long i = 0; i < n; i++) { if (i == 0) cin >> v[i]; else { long long x = in<long long>(); v[i] = v[i - 1] + x; } } long long sign = v[0] / abs(v[0]); long long sum = 0; long long cnt1 = 0; for (long long i = 1; i < n; i++) { if (v[i] * sign >= 0) { long long d = (sign > 0 ? (v[i] + 1) * -1 : (v[i] - 1) * -1); sum += d; cnt1 += abs(d); } if (i < n - 1) v[i + 1] += sum; sign *= -1; } sum = 0; long long cnt2 = 0; sign *= v[0] / abs(v[0]) * -1; for (long long i = 0; i < n; i++) { if (v[i] * sign >= 0) { long long d = (sign > 0 ? (v[i] + 1) * -1 : (v[i] - 1) * -1); sum += d; cnt2 += abs(d); } if (i < n - 1) v[i + 1] += sum; sign *= -1; } cout << min(cnt1, cnt2) << "\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
UNKNOWN
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Numerics; using static System.Console; using static System.Math; using static System.Array; class Program { public static void Main() { var n = int.Parse(ReadLine()); var a = ReadLine().Split().Select(long.Parse).ToArray(); var b = new long[2, n]; var sum = new long[2, n]; var count = new int[2]; for(int i = 0; i < n; i++) { b[0, i] = a[i]; b[1, i] = -a[i]; } for(int i = 0; i < 2; i++) { for(int j = 0; j < n; j++) { if(j == 0) sum[i, j] = b[i, j]; else sum[i, j] = b[i, j] + sum[i, j - 1]; if(j % 2 == 0) { while(sum[i, j] <= 0) { b[i, j]++; count[i]++; if(j == 0) sum[i, j] = b[i, j]; else sum[i, j] = b[i, j] + sum[i, j - 1]; } } else { while(sum[i, j] >= 0) { b[i, j]--; count[i]++; if(j == 0) sum[i, j] = b[i, j]; else sum[i, j] = b[i, j] + sum[i, j - 1]; } } } } WriteLine(count.Min()); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, ansa = 0, ansb = 0, suma = 0, sumb = 0; cin >> n; for (int i = 0; i < (n); i++) { int c; cin >> c; suma += c; sumb += c; if (i % 2 == 0) { if (suma <= 0) { ansa += 1 - c - suma; suma = 1; } if (sumb >= 0) { ansb += sumb + c + 1; sumb = -1; } } else { if (suma >= 0) { ansa += suma + c + 1; suma = -1; } if (sumb <= 0) { ansb += 1 - c - sumb; sumb = 1; } } } cout << min(ansa, ansb) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
main :: IO() main = do getLine lis <- words <$> getLine let numlis = map read lis print $ solve numlis 0 solve :: [Integer] -> Integer -> Integer solve [] x = 0 solve (x:xs) 0 | x /= 0 = solve xs x | otherwise = min (solve xs (-1) + 1) (solve xs 1 + 1) solve (x:xs) m | m > 0 = max 0 (sum+1) + solve xs (min sum (-1)) | m < 0 = max 0 (1-sum) + solve xs (max sum 1) where sum = m+x
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = [int(i) for i in input().split()] a = [int(i) for i in input().split()] total = 0 fugo = 0 count = 0 for i in a: total += i if(fugo == 0): total = i if(total > 0): fugo = 1 else: fugo = -1 continue elif(fugo > 0): fugo = -1 if(total >= 0): while(total>=0): count += 1 total -= 1 elif(fugo < 0): fugo = 1 if(total <= 0): while(total<=0): count += 1 total += 1 print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# -*- coding: utf-8 -*- N = int(input()) a = [int(n) for n in input().split()] count_a = 0 #+start count_b = 0 #-start nowsum = a[0] if nowsum > 0: for n in range(1, N): if nowsum * (nowsum + a[n]) >= 0: count_a += abs(nowsum + a[n]) + 1 if nowsum < 0: nowsum = 1 else: nowsum = -1 else: nowsum += a[n] count_b += nowsum + 1 a[0] = -1 nowsum = -1 for n in range(1, N): if nowsum * (nowsum + a[n]) >= 0: count_b += abs(nowsum + a[n]) + 1 if nowsum < 0: nowsum = 1 else: nowsum = -1 else: nowsum += a[n] print(min(count_a, count_b)) elif nowsum < 0: for n in range(1, N): if nowsum * (nowsum + a[n]) >= 0: count_a += abs(nowsum + a[n]) + 1 if nowsum < 0: nowsum = 1 else: nowsum = -1 else: nowsum += a[n] count_b += abs(nowsum) + 1 a[0] = 1 nowsum = 1 for n in range(1, N): if nowsum * (nowsum + a[n]) >= 0: count_b += abs(nowsum + a[n]) + 1 if nowsum < 0: nowsum = 1 else: nowsum = -1 else: nowsum += a[n] print(min(count_a, count_b)) else: a[0] = 1 count_a += 1 nowsum = 1 for n in range(1, N): if nowsum * (nowsum + a[n]) >= 0: count_a += abs(nowsum + a[n]) + 1 if nowsum < 0: nowsum = 1 else: nowsum = -1 else: nowsum += a[n] a[0] = -1 count_b += 1 nowsum = -1 for n in range(1, N): if nowsum * (nowsum + a[n]) >= 0: count_b += abs(nowsum + a[n]) + 1 if nowsum < 0: nowsum = 1 else: nowsum = -1 else: nowsum += a[n] print(min(count_a, count_b))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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())) cum_a = [0 for _ in range(n)] cum_a[0] = a[0] for i in range(n-1): cum_a[i+1] = cum_a[i] + a[i+1] count = 0 flag = 0 diff = 0 def r_flag(n): if n > 0: return 1 elif n < 0: return -1 else: return 0 flag = r_flag(cum_a[0]) for i in range(1, len(cum_a)): tmp_flag = r_flag(cum_a[i]+diff) if tmp_flag * flag == -1: flag = tmp_flag elif tmp_flag * flag == 1: if tmp_flag == 1: count += abs(-1-(cum_a[i]+diff)) diff += -1-(cum_a[i]+diff) flag = -1 else: count += abs(1-(cum_a[i]+diff)) diff += 1-(cum_a[i]+diff) flag = 1 else: try: next_flag = r_flag(cum_a[i+1]-diff) if next_flag == 1: diff -= 1 count += 1 else: diff += 1 count += 1 except: diff += 1 count += 1 print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using vpii = vector<pair<int, int>>; using vpll = vector<pair<ll, ll>>; int N; ll solve(vector<ll> &A, ll cur) { ll ans = 0; for (int i = 1; i < N; i++) { if (cur > 0 && cur + A[i] >= 0) { ans += abs(-1 - (cur + A[i])); cur = -1; } else if (cur < 0 && cur + A[i] <= 0) { ans += abs(1 - (cur + A[i])); cur = 1; } else cur += A[i]; } return (ans); } int main(void) { cin >> N; vector<ll> A(N); for (int i = 0; i < N; i++) cin >> A[i]; ll ans1 = 0, ans2 = 0; ll cur = A[0]; if (cur <= 0) { cur = 1; ans1 += abs(1 - cur); } ans1 += solve(A, cur); cur = A[0]; if (cur >= 0) { cur = -1; ans2 += abs(-1 - cur); } ans2 += solve(A, cur); cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int getsign(long long int n) { if (n > 0) { return 1; } if (n < 0) { return -1; } return -1; } int count(int sign0, long long a[], int n) { long long int sum = 0; int sign = sign0; long long int count = 0; for (int i = 0; i < n; ++i) { sum += a[i]; if (getsign(sum) != sign) { count += abs(sign - sum); sum = sign; } sign = (sign * -1); } return count; } int main() { int n; cin >> n; long long a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } cout << min(count(1, a, n), count(-1, a, n)) << 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
from itertools import accumulate N = int(input()) A = list(map(int,input().split())) acmA = list(accumulate(A)) add = 0 ans = 0 for i in range(1,N): acmA[i] += add if acmA[i-1]*acmA[i]<0:continue tmp_add = -acmA[i]-1 if acmA[i] > 0 else -acmA[i]+1 #print(i, tmp_add, acmA[i]) acmA[i] += tmp_add add += tmp_add ans +=abs(tmp_add) print(ans) #print(acmA)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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() { vector<int> a, b; int n, ansa = 0, ansb = 0; cin >> n; for (int i = 0; i < n; ++i) { int tmp; cin >> tmp; a.push_back(tmp); b.push_back(tmp); } if (a[0] < 0) { for (int i = 1; i < n; ++i) { a[i] += a[i - 1]; if (i % 2 == 1) { if (a[i] <= 0) { ansa = ansa + 1 - a[i]; a[i] = 1; } } else { if (a[i] >= 0) { ansa = ansa + 1 + a[i]; a[i] = -1; } } } ansb = 1 - b[0]; b[0] = 1; for (int i = 1; i < n; ++i) { b[i] += b[i - 1]; if (i % 2 == 1) { if (b[i] >= 0) { ansb = ansb + 1 + b[i]; b[i] = -1; } } else { if (b[i] <= 0) { ansb = ansb + 1 - b[i]; b[i] = 1; } } } } else { for (int i = 1; i < n; ++i) { a[i] += a[i - 1]; if (i % 2 == 1) { if (a[i] >= 0) { ansa = ansa + 1 + a[i]; a[i] = -1; } } else { if (a[i] <= 0) { ansa = ansa + 1 - a[i]; a[i] = 1; } } } ansb = b[0] + 1; b[0] = -1; for (int i = 1; i < n; ++i) { b[i] += b[i - 1]; if (i % 2 == 1) { if (b[i] <= 0) { ansb = ansb + 1 - b[i]; b[i] = 1; } } else { if (b[i] >= 0) { ansb = ansb + 1 + b[i]; b[i] = -1; } } } } cout << min(ansa, ansb) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num_count = sc.nextInt(); int[] array = new int[num_count]; int first_plus_cost = 0; int sum = 0; for(int i = 0;i < num_count;i++){ array[i] = sc.nextInt(); } for(int i = 0;i < num_count;i++){ int temp = sum; temp += array[i]; if(i % 2 == 0 && temp <= 0){ int cost = 1 - temp; first_plus_cost += cost; //temp += cost; temp = 1; //System.out.println("0: i: " + i + ",cost: " + cost); }else if(i % 2 == 1 && temp >= 0){ int cost = 1 + temp; first_plus_cost += cost; //temp -= cost; temp = -1; //System.out.println("1: i: " + i + ",cost: " + cost); } sum = temp; } //System.out.println(); int second_plus_cost = 0; sum = 0; for(int i = 0;i < num_count;i++){ int temp = sum; temp += array[i]; if(i % 2 == 0 && temp >= 0){ int cost = 1 + temp; second_plus_cost += cost; //temp -= cost; temp = -1; //System.out.println("2: i: " + i + ",cost: " + cost); }else if(i % 2 == 1 && temp <= 0){ int cost = 1 - temp; second_plus_cost += cost; //temp += cost; temp = 1; //System.out.println("3: i: " + i + ",cost: " + cost); } sum = temp; } int min_cost = first_plus_cost < second_plus_cost ? first_plus_cost : second_plus_cost; System.out.println(min_cost); sc.close(); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, x = 0, a[100001], ans = 0; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; a[i] == 0; ++i) { ans = 2 * (i + 1) - 1; x = i + 1; } int sum1 = a[x], sum2 = a[x]; for (int i = x + 1; i < n; i++) { sum2 += a[i]; if (sum2 >= 0 && sum1 > 0) { ans += abs(sum2) + 1; a[i] = a[i] - abs(sum2) - 1; sum2 = sum2 - abs(sum2) - 1; } if (sum2 <= 0 && sum1 < 0) { ans += abs(sum2) + 1; a[i] = a[i] + abs(sum2) + 1; sum2 = sum2 + abs(sum2) + 1; } sum1 = sum2; } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; const int MOD = 1000000007; const int mod = 1000000007; const int INF = 1000000000; const long long LINF = 1e18; const int MAX = 510000; int code(int n) { if (n < 0) return 1; else if (n > 0) return 0; else return 2; } int main() { int n; long long int sum = 0; long long int ans = 0; long long int ans2 = 0; cin >> n; vector<long long int> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } sum = a.at(0); if (sum != 0) { for (int i = 1; i < n; i++) { if (sum + a.at(i) == 0) { ans++; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else if (code(sum + a.at(i)) == code(sum)) { ans += abs(sum + a.at(i)) + 1; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else { sum = a.at(i) + sum; } } cout << ans << endl; return 0; } else if (sum == 0) { sum = -1; ans = 1; for (int i = 1; i < n; i++) { if (sum + a.at(i) == 0) { ans++; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else if (code(sum + a.at(i)) == code(sum)) { ans += abs(sum + a.at(i)) + 1; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else { sum = a.at(i) + sum; } } sum = 1; ans2 = 1; for (int i = 1; i < n; i++) { if (sum + a.at(i) == 0) { ans2++; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else if (code(sum + a.at(i)) == code(sum)) { ans2 += abs(sum + a.at(i)) + 1; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else { sum = a.at(i) + sum; } } cout << min(ans, ans2) << endl; } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> a(n); for (int i = 0; i < (n); i++) cin >> a[i]; long long tmp = a[0]; long long ans1 = 0; if (a[0] <= 0) { ans1 += abs(a[0] - 1); a[0] = 1; } long long sum = a[0]; for (int i = 0; i < n - 1; i++) { sum = sum + a[i + 1]; if (i % 2 == 0 && sum > 0) { ans1 += abs(sum + 1); sum = -1; } else if (i % 2 != 0 && sum < 0) { ans1 += abs(sum - 1); sum = 1; } } a[0] = tmp; long long ans2 = 0; if (a[0] >= 0) { ans2 += abs(a[0] + 1); a[0] = -1; } sum = a[0]; for (int i = 0; i < n - 1; i++) { sum = sum + a[i + 1]; if (i % 2 == 0 && sum < 0) { ans2 += abs(sum - 1); sum = 1; } else if (i % 2 != 0 && sum > 0) { ans2 += abs(sum + 1); sum = -1; } } ans2 += 1; cout << min(ans1, ans2); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } long long sum1 = a.at(0); long long sum2 = a.at(0); long long op1 = 0; long long op2 = 0; if (sum1 <= 0) { sum1 = 1; op1 += -1 * sum1 + 1; } if (sum2 >= 0) { sum2 = -1; op2 += sum2 + 1; } for (int j = 1; j < n; j++) { if (sum1 > 0) { sum1 += a.at(j); if (sum1 >= 0) { op1 += (sum1 + 1); sum1 = -1; } } else { sum1 += a.at(j); if (sum1 <= 0) { op1 += (-1 * sum1 + 1); sum1 = 1; } } if (sum2 > 0) { sum2 += a.at(j); if (sum2 >= 0) { op2 += (sum2 + 1); sum2 = -1; } } else { sum2 += a.at(j); if (sum2 <= 0) { op2 += (-1 * sum2 + 1); sum2 = 1; } } } cout << (op1 > op2 ? op2 : op1) << 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 int N, count = 0; cin >> N; vector<long long int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; long long int su = A[0]; bool plus = A[0] > 0; for (int i = 1; i < N; i++) { plus = !plus; su += A[i]; if (plus) { if (su <= 0) { count += -1 * su + 1; su = 1; } } else { if (su >= 0) { count += su + 1; su = -1; } } } 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
import Control.Monad import Data.List main=do _<-getLine (a:as)<-map read.words<$>getLine::IO[Int] print $ min (sum.snd $ mapAccumL f a as) ((+) (1 + (abs a)) $ sum $ snd $ mapAccumL f (a`div`(abs a)) as) f a b | signum a * signum (a+b) < 0 = (a+b,0) | a < 0 = (1, 1-(a+b)) | otherwise = ((-1), 1+(a+b))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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
def solve(n, a) res=0 sum=a[0] 1.upto(n-1) do |i| pre = sum sum += a[i] next if pre*sum < 0 res += sum.abs + 1 sum = pre<0 ? 1 : -1 end res end n=gets.to_i a=gets.split.map &:to_i if a[0]!=0 p solve(n, a) else a[0]=1 s1 = 1+solve(n, a.dup) a[0]=-1 s2 = 1+solve(n, a.dup) p [s1, s2].min end
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
import copy n = int(input()) a = list(map(int,input().split())) b = [0] for i in range(len(a)): b.append(b[i] + a[i]) del b[0] c = copy.copy(b) first_minus = 0 first_plus = 0 for i in range(len(b)): if(i%2 == 0): if (b[i] >= 0): diff = abs(b[i]) + 1 first_minus = first_minus + diff b = list(map(lambda x: x - diff, b)) if (c[i] <= 0): diff = abs(c[i]) + 1 first_plus = first_plus + diff c = list(map(lambda x: x + diff, c)) else: if (b[i] <= 0): diff = abs(b[i]) + 1 first_minus = first_minus + diff b = list(map(lambda x: x + diff, b)) if (c[i] >= 0): diff = abs(c[i]) + 1 first_plus = first_plus + diff c = list(map(lambda x: x - diff, c)) print(min(first_minus,first_plus))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int a[n]; for (long long i = (0); i < (n); i++) { cin >> a[i]; } for (long long i = (0); i < (n - 1); i++) { a[i + 1] += a[i]; } long long ans1 = 0, cnt = 0; for (long long i = (0); i < (n); i++) { if (i % 2 == 0) { if (a[i] + cnt <= 0) { ans1 += -(a[i] + cnt) + 1; cnt += -(a[i] + cnt) + 1; } } else { if (a[i] + cnt >= 0) { ans1 += a[i] + cnt + 1; cnt -= a[i] + cnt + 1; } } } long long ans2 = 0; cnt = 0; for (long long i = (0); i < (n); i++) { if (i % 2 == 1) { if (a[i] + cnt <= 0) { ans2 += -(a[i] + cnt) + 1; cnt += -(a[i] + cnt) + 1; } } else { if (a[i] + cnt >= 0) { ans2 += a[i] + cnt + 1; cnt -= a[i] + cnt + 1; } } } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def countave(n , a): ans = 100000 for i in range(2): kei = 0 forans = 0 for w in range(n): if (w + i) % 2: if kei + int(a[w]) > -1: forans += abs(kei + int(a[w])) + 1 kei = -1 else: kei += int(a[w]) else: if kei + int(a[w]) < 1: forans += abs(kei + int(a[w])) + 1 kei = 1 else: kei += int(a[w]) ans = min(ans , forans) return ans def main(): n = int(input()) a = list(map(int, input().split())) print(countave(n , a)) if __name__ == '__main__': main()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>; const int INF = 1001001001; const int MOD = 1000000007; const long long INFL = (1LL << 60); const double EPS = 1e-9; bool meet(vi a) { bool ret = true; bool pos = (a[0] > 0); int sum = a[0]; for (int i = (1); i < (int)(a.size()); i++) { sum += a[i]; if ((pos && i % 2 && sum >= 0) || (pos && !(i % 2) && sum <= 0) || (!pos && i % 2 && sum <= 0) || (!pos && !(i % 2) && sum >= 0)) { ret = false; break; } } return ret; } uint64_t solve(vi a, uint64_t res) { int sum = a[0]; bool pos = (a[0] > 0); for (int i = (1); i < (int)(a.size()); i++) { if ((pos && i % 2 && sum + a[i] >= 0) || (!pos && !(i % 2) && sum + a[i] >= 0)) { res += abs(sum + a[i] - (-1)); a[i] = -1 - sum; } else if ((pos && !(i % 2) && sum + a[i] <= 0) || (!pos && i % 2 && sum + a[i] <= 0)) { res += abs(sum + a[i] - 1); a[i] = 1 - sum; } sum += a[i]; } return res; } int main() { int N; cin >> N; vi a(N); for (int i = 0; i < (int)(N); i++) cin >> a[i]; bool flg = meet(a); bool pos = (a[0] > 0); uint64_t res = 0; uint64_t res1 = solve(a, res); res = 0; if (pos) { res += abs(a[0] - (-1)); a[0] = -1; } else { res += abs(a[0] - 1); a[0] = 1; } uint64_t res2 = solve(a, res); res = min(res1, res2); if (flg) res = 0; 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() { long n = 0; long sum[2] = {0, 0}, ans[2] = {0, 0}; long hoge, foo; long flag[2] = {0, 1}; scanf("%d", &n); int in[n]; for (int i = 0; i < n; i++) { scanf("%ld", &in[i]); } for (int i = 0; i < n; i++) { long tmp[2]; tmp[0] = in[i]; tmp[1] = tmp[0]; if (i == 0) { sum[0] = tmp[0]; sum[1] = tmp[1]; continue; } long foo[2] = {tmp[0], tmp[0]}; for (int j = 0; j < 2; j++) { if (sum[j] + tmp[j] <= 0 && !flag[j]) { tmp[j] = abs(sum[j]) + 1; ans[j] += abs(sum[j]) - abs(foo[j]) + 1; sum[j] += tmp[j]; flag[j] = 1; } else if (sum[j] + tmp[j] > 0 && !flag[j]) { flag[j] = 1; sum[j] += tmp[j]; } else if (sum[j] + tmp[j] < 0 && flag[j]) { sum[j] += tmp[j]; flag[j] = 0; } else if (sum[j] + tmp[j] >= 0 && flag[j]) { tmp[j] = -1 * (abs(sum[j]) + 1); ans[j] += abs(sum[j]) + abs(foo[j]) + 1; sum[j] += tmp[j]; flag[j] = 0; } } } printf("%ld\n", ans[0] < ans[1] ? ans[0] : ans[1]); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int ans = 0; vector<int> vec(100000); vector<int> rui(100000); for (int i = (int)(0); i < (int)(N); ++i) { cin >> vec[i]; } rui[0] = vec[0]; for (int i = 1; i < N; i++) { rui[i] = rui[i - 1] + vec[i]; } int G = vec[0]; int X = 0; if (G >= 0) { for (int i = (int)(0); i < (int)(N); ++i) { if (i % 2 == 0) { if (rui[i] <= 0) { X = abs(1 - rui[i]); ans += X; } } else { if (rui[i] >= 0) { X = abs(-1 - rui[i]) * -1; ans += abs(X); } } for (int j = (int)(0); j < (int)(N); ++j) { rui[j] += X; } } } else { for (int i = (int)(0); i < (int)(N); ++i) { X = 0; if (i % 2 == 0) { if (rui[i] >= 0) { X = abs(-1 - rui[i]) * -1; ans += abs(X); } } else { if (rui[i] <= 0) { X = abs(1 - rui[i]); ans += abs(X); } } for (int j = (int)(0); j < (int)(N); ++j) { rui[j] += X; } } } 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()) arr = list(map(int, input().split())) sum = arr[0] ans = 0 for i in range(1,N,1): if sum>0: fugo ='plus' else: fugo ='mainus' sum += arr[i] if sum>0: after_fugo ='plus' elif sum<0: after_fugo ='mainus' else: after_fugo = fugo if fugo == after_fugo: if fugo == 'plus': ans += abs(sum)+1 if sum>=0: arr[i] = -(abs(sum)+1) else: arr[i] = abs(sum)+1 if fugo == 'mainus': ans += abs(sum)+1 if sum>=0: arr[i] = -(abs(sum)+1) else: arr[i] = abs(sum)+1 print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
n = gets.to_i as = gets.split.map(&:to_i) cnt1 = 0 sum = as[0] n.times.with_index(1) do |_,i| break if as[i].nil? if sum < 0 if sum + as[i] > 0 sum += as[i] next else x = 1 - sum - as[i] cnt1 += x sum = 1 end elsif sum > 0 if sum + as[i] < 0 sum += as[i] next else x = -1 - sum - as[i] cnt1 += x.abs sum = -1 end end end cnt2 = (as[0] * 2).abs sum = as[0] * -1 n.times.with_index(1) do |_,i| break if as[i].nil? if sum < 0 if sum + as[i] > 0 sum += as[i] next else x = 1 - sum - as[i] cnt2 += x sum = 1 end elsif sum > 0 if sum + as[i] < 0 sum += as[i] next else x = -1 - sum - as[i] cnt2 += x.abs sum = -1 end end end puts [cnt1, cnt2].min
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int sign(long long n) { return ((n > 0) - (n < 0)); } int main() { int n; scanf("%d", &n); long long sum = 0; long long ans = 0; for (int i = 0; i < n; i++) { int ra; scanf("%d", &ra); long long a = ra; if (sum == 0) { if (a == 0) { a++; ans++; } } else if (sum + a == 0) { a - sign(sum); ans++; } else if (sign(sum + a) + sign(sum) != 0) { long long tmp = a; if (sum + a > 0) { a = a - (sum + a) - 1; } else { a = a - (sum + a) + 1; } ans += abs(tmp - a); } sum += a; } printf("%llu\n", ans); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int t; vector<int> answer(2); int sumi; bool flag = true; cin >> t; vector<int> A(t); for (int i = 0; i < t; i++) { cin >> A[i]; } for (int j = 0; j < 2; j++) { for (int i = 0; i < t; i++) { sumi += A[i]; if (sumi == 0) { answer[i] += 1; if (flag) { sumi = -1; } else { sumi = 1; } } else if (sumi > 0 == flag) { answer[i] += abs(sumi) + 1; if (sumi > 0) { sumi = -1; } else { sumi = 1; } } flag = !flag; } flag = false; } cout << max(answer[0], answer[1]) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<long long> a(N); long long prev = 1000000000000000000, sum = 0, cnt = 0; for (int i = 0; i < (int)(N); i++) { cin >> a[i]; sum += a[i]; if (i == 0 && a[i] == 0) { if (a[i + 1] >= 0) sum -= 1; else sum += 1; cnt++; } if (i != 0) { if (prev <= 0 && sum <= 0) { cnt += (1 - sum); sum = 1; } else if (prev >= 0 && sum >= 0) { cnt += (abs(-1 - sum)); sum = -1; } } prev = sum; } 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; const int INF = 1e9; const int MOD = 1e9 + 7; const long long LINF = 1e18; using Graph = vector<vector<int>>; using Edge = map<pair<int, int>, int>; template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T &val) { std::fill((T *)array, (T *)(array + N), val); } long long gcd(long long a, long long b) { if (a % b == 0) return (b); else return (gcd(b, a % b)); } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } int main() { cout << fixed << setprecision(15); long long N; cin >> N; vector<long long> A(N); vector<long long> B(N); long long start = 0; bool first = true; for (long long i = 0; i < (long long)(N); ++i) { cin >> A[i]; if (first) { if (A[i] != 0) { start = i; } first = false; } if (i == 0) { B[i] = A[i]; } else { B[i] = A[i] + B[i - 1]; } } long long ans = 0; long long total = 0; if (A[start] > 0) { total = -1; if (start == 0) { total = 0; } for (long long i = start; i < N; i++) { if ((i - start) % 2 == 0) { if (B[i] + total <= 0) { ans += abs(B[i] + total) + 1; total += abs(B[i] + total) + 1; } } else { if (B[i] + total >= 0) { ans += abs(B[i] + total) + 1; total -= (abs(B[i] + total) + 1); } } } } else { total = 1; if (start == 0) { total = 0; } for (long long i = start; i < N; i++) { if ((i - start) % 2 == 0) { if (B[i] + total >= 0) { ans += abs(B[i] + total) + 1; total -= (abs(B[i] + total) + 1); } } else { if (B[i] + total <= 0) { ans += abs(B[i] + total) + 1; total += (abs(B[i] + total) + 1); } } } } if (start != 0) { ans += 2 * start - 1; } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; int N[100000]; cin >> n; for (int i = 0; i < n; i++) { cin >> N[i]; } for (int i = 1; i < n; i++) { N[i] = N[i] + N[i - 1]; } int ans = 0; if (N[0] == 0) { if (N[1] <= 0) { for (int i = 0; i < n; i++) { N[i]++; ans = 1; } } else { for (int i = 0; i < n; i++) { N[i] = N[i] - 1; } ans = 1; } } for (int i = 1; i < n; i++) { if (N[i - 1] < 0) { while (N[i] <= 0) { for (int j = i; j < n; j++) { N[j]++; } ans++; } } if (N[i - 1] > 0) { while (N[i] >= 0) { for (int j = i; j < n; j++) { N[j] = N[j] - 1; } ans++; } } } cout << ans; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using long long = long long; template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long INF = 1e9; long long n; vector<long long> a; int main() { cin >> n; a.resize(n); for (long long i = 0; i < (n); ++i) cin >> a[i]; long long sum; long long ans = 0; sum = a[0]; { sum = -1; ans += abs(a[0] - sum); for (long long i = 0; i < (n - 1); ++i) { if (sum < 0) { if (sum + a[i + 1] > 0) { sum += a[i + 1]; continue; } else { ans += 1 - sum - a[i + 1]; sum = 1; } } else { if (sum + a[i + 1] < 0) { sum += a[i + 1]; continue; } else { ans += a[i + 1] + sum + 1; sum = -1; } } } long long ans2 = 1; sum = 1; ans += abs(a[0] - sum); for (long long i = 0; i < (n - 1); ++i) { if (sum < 0) { if (sum + a[i + 1] > 0) { sum += a[i + 1]; continue; } else { ans2 += 1 - sum - a[i + 1]; sum = 1; } } else { if (sum + a[i + 1] < 0) { sum += a[i + 1]; continue; } else { ans2 += a[i + 1] + sum + 1; sum = -1; } } } chmin(ans, ans2); } long long ans3 = 0; sum = a[0]; for (long long i = 0; i < (n - 1); ++i) { if (sum == 0) continue; if (sum < 0) { if (sum + a[i + 1] > 0) { sum += a[i + 1]; continue; } else { ans3 += 1 - sum - a[i + 1]; sum = 1; } } else { if (sum + a[i + 1] < 0) { sum += a[i + 1]; continue; } else { ans3 += a[i + 1] + sum + 1; sum = -1; } } } chmin(ans, ans3); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n=int(input()) a=list(map(int , input().split())) res=[0]*n res[0]=a[0] ng=0 ps=0 for i in range(1,n): res[i]=res[i-1]+a[i] for i in range(n-1): s=res[i] if s*(res[i+1]+ps-ng)>0: if s>0: ng+=abs(a[i]+s)+1 else: ps+=abs(a[i]+s)+1 print(ps+ng)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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, temp; vector<int> a; scanf("%d", &N); int start = 0; bool v = false; for (int i = 0; i < N; i++) { scanf("%d", &temp); if (temp == 0) { if (!v) { start += 1; } } else if (!v) v = true; a.push_back(temp); } int sum = 0, cnt = 0; if (start != 0) { cnt = 2 * (start - 1) + 1; if (a[start] > 0) { if (a[start] > 1) { sum = a[start] - 1; } else { sum = 1; cnt += 1; } } else { if (a[start] < -1) { sum = a[start] + 1; } else { sum = -1; cnt += 1; } } } else { sum = a[start]; } start++; for (size_t i = start; i != a.size(); i++) { if (sum + a[i] == 0) { if (sum > 0) { sum = -1; } else { sum = 1; } cnt += 1; continue; } if (sum + a[i] > 0 && sum > 0) { cnt += sum + a[i] + 1; sum = -1; } else if (sum + a[i] < 0 && sum < 0) { cnt += 1 - sum - a[i]; sum = 1; } else { sum += a[i]; } } if (sum == 0) cnt += 1; printf("%d\n", cnt); return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a1(n); vector<int> a2(n); for (int i = 0; i < n; i++) { cin >> a1.at(i); a2.at(i) = a1.at(i); } int ans1 = 0; int flag1 = 1; int sum = 0; for (int i = 0; i < n; i++) { sum += a1.at(i); if (flag1 == 1) { flag1 = -1; if (sum <= 0) { ans1 += -sum + 1; sum = 1; } } else if (flag1 == -1) { flag1 = 1; if (sum >= 0) { ans1 += sum + 1; sum = -1; } } } int ans2 = 0; flag1 = -1; sum = 0; for (int i = 0; i < n; i++) { sum += a2.at(i); if (flag1 == 1) { flag1 = -1; if (sum <= 0) { ans2 += -sum + 1; sum = 1; } } else if (flag1 == -1) { flag1 = 1; if (sum >= 0) { ans2 += sum + 1; sum = -1; } } } cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split())) a_t = [0] for i in range(n): a_t.append(a[i] + a_t[i]) ans = 0 ans_ = 0 count = 0 for i in range(1, n + 1): if i == 1: if a_t[i] >= 0: ans += a_t[i] + 1 count += -(a_t[i]) - 1 if a_t[i - 1] + count > 0: if a_t[i] + count > 0: ans += a_t[i] + count + 1 count += -(a_t[i] + count) - 1 if a_t[i] + count == 0: ans += 1 count -= 1 elif a_t[i - 1] + count < 0: if a_t[i] + count < 0: ans += -(a_t[i] + count) + 1 count += -(a_t[i] + count) + 1 if a_t[i] + count == 0: ans += 1 count += 1 for i in range(1, n + 1): if i == 1: if a_t[i] <= 0: ans_ += a_t[i] + 1 count += -(a_t[i]) - 1 if a_t[i - 1] + count > 0: if a_t[i] + count > 0: ans_ += a_t[i] + count + 1 count += -(a_t[i] + count) - 1 if a_t[i] + count == 0: ans_ += 1 count -= 1 elif a_t[i - 1] + count < 0: if a_t[i] + count < 0: ans_ += -(a_t[i] + count) + 1 count += -(a_t[i] + count) + 1 if a_t[i] + count == 0: ans_ += 1 count += 1 print(min(ans, ans_))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INFL = 1e18; const int INF = 1001001001; const int mod = 1000000007; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < (n); i++) cin >> a[i]; int ans1 = 0, ans2 = 0; long long sum = 0; for (int i = 0; i < (n); i++) { sum += a[i]; if (i % 2 == 0) { if (sum <= 0) { ans1 += abs(sum) + 1; sum = 1; } } else { if (sum >= 0) { ans1 += sum + 1; sum = -1; } } } sum = 0; for (int i = 0; i < (n); i++) { sum += a[i]; if (i % 2 == 1) { if (sum <= 0) { ans2 += abs(sum) + 1; sum = 1; } } else { if (sum >= 0) { ans2 += sum + 1; sum = -1; } } } cout << min(ans1, ans2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<long long, long long>; constexpr char ln = '\n'; constexpr long long MOD = 1000000007LL; constexpr long long INF = 1000000009LL; constexpr long long LINF = 1000100010001000100LL; 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() { int n; cin >> n; vector<ll> A(n); for (int i = 0; i < (n); i++) cin >> A[i]; ll res1 = 0, sum = A[0]; for (int i = (1); i < (n); i++) { sum += A[i]; if (i % 2 == 1 && sum <= 0) { ll num = -sum + 1; sum += num; res1 += num; } else if (i % 2 == 0 && sum >= 0) { ll num = sum + 1; sum -= num; res1 += num; } } ll res2 = 0; sum = A[0]; for (int i = (1); i < (n); i++) { sum += A[i]; if (i % 2 == 0 && sum <= 0) { ll num = -sum + 1; sum += num; res2 += num; } else if (i % 2 == 1 && sum >= 0) { ll num = sum + 1; sum -= num; res2 += num; } } cout << min(res1, res2) << ln; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
main :: IO () main = do _ <- readLn :: IO Int (a0:as) <- (map read . words) <$> getLine print $ solve (0, a0, as) solve :: (Int, Int, [Int]) -> Int solve (x, 0, 0:as) = min (solve (x+1, 1, as)) (solve (x+1, -1, as)) solve (x, _, []) = x solve (x, s, a0:as) = solve (x+k, s + a0 + k, as) where m = if s > 0 then -1 else 1 k = m - (s + a0)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
l = int(input()) n = [int(i) for i in input().split()] count = 0 nsum = n[0] for i in range(1, l): while(True): print("i:" + str(i)) print("n:" + str(n[i])) print(("nsum:" + str(nsum))) if (n[i-1] < 0 and n[i] <= 0) or (n[i-1] > 0 and n[i] >= 0) or (nsum * (nsum + n[i]) >= 0): if n[i-1] < 0: count += 1 n[i] += 1 else: count += 1 n[i] -= 1 else: nsum += n[i] break print(count)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.util.NoSuchElementException; public class Main { static void exec(MyScanner in, PrintWriter out) { int N = in.nextInt(); int[] a = new int[N]; for (int i = 0; i < N; i += 1) { a[i] = in.nextInt(); } long c = 0; int sum = a[0]; int signum = Integer.signum(a[0]); if (signum == 0) { signum = 1; } for (int i = 1; i < N; i += 1) { int n = sum + a[i]; int s = Integer.signum(n); if (s == 0) { c += 1; sum = -signum; } else if (s != signum) { sum = n; } else { c += Math.abs((sum >= 0 ? -1 : 1) * (Math.abs(sum) + 1) - a[i]); sum = -signum; } signum = -signum; } out.println(c); } public static void main(String[] args) { PrintWriter w = new PrintWriter(System.out); exec(new MyScanner(System.in), w); w.flush(); } static class MyScanner { static final int BUFFER_SIZE = 1024; private final InputStream in; private final byte[] buffer = new byte[BUFFER_SIZE]; private int point; private int readLength; MyScanner(InputStream in) { this.in = in; } private int readByte() { if (point < readLength) { int result = buffer[point]; point += 1; return result; } try { readLength = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (readLength == -1) { return -1; } point = 1; return buffer[0]; } static boolean isPrintableCharExceptSpace(int c) { return 33 <= c && c <= 126; } String next() { StringBuilder b = new StringBuilder(); int c = readByte(); while (!(c == -1 || isPrintableCharExceptSpace(c))) { c = readByte(); } if (c == -1) { throw new NoSuchElementException(); } do { b.appendCodePoint(c); c = readByte(); } while (c != -1 && isPrintableCharExceptSpace(c)); return b.toString(); } long nextLong() { int c = readByte(); while (!(c == -1 || isPrintableCharExceptSpace(c))) { c = readByte(); } if (c == -1) { throw new NoSuchElementException(); } boolean minus = false; if (c == '-') { minus = true; c = readByte(); } long result = 0L; do { if (!('0' <= c && c <= '9')) { throw new InputMismatchException(); } result = result * 10L + (c - '0'); c = readByte(); } while (c != -1 && isPrintableCharExceptSpace(c)); return minus ? -result : result; } int nextInt() { long n = nextLong(); if (Integer.MIN_VALUE <= n && n <= Integer.MAX_VALUE) { return (int) n; } throw new InputMismatchException(); } double nextDouble() { return Double.parseDouble(next()); } } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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> arr(n); for (int i = 0; i < n; ++i) { cin >> arr[i]; } int sum = 0; int cnt_a = 0; for (int i = 0; i < n; ++i) { sum += arr[i]; if (i % 2 == 0 and sum < 1) { cnt_a += (1 - sum); sum = 1; } if (i % 2 == 1 and sum > -1) { cnt_a += (sum + 1); sum = -1; } } sum = 0; int cnt_b = 0; for (int i = 0; i < n; ++i) { sum += arr[i]; if (i % 2 == 1 and sum < 1) { cnt_b += (1 - sum); sum = 1; } if (i % 2 == 0 and sum > -1) { cnt_b += (sum + 1); sum = -1; } } cout << min(cnt_a, cnt_b) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int a(long long int z) { if (z > 0) return 1; else if (z < 0) return -1; else return 0; } int main() { long long int n, sum = 0, in, ans = 0; cin >> n >> sum; for (int i = 1; i < n; i++) { cin >> in; if (i == 1) { if (sum == 0) { sum = -1 * a(in); ans++; } } if (a(sum) * a(in) < 0 && abs(sum) < abs(in)) { sum += in; continue; } else if (a(sum) * a(in) < 0) { ans += abs(sum) - abs(in) + 1; if (sum > 0) sum = -1; else sum = 1; continue; } ans += abs(sum) + abs(in) + 1; if (a(sum) < 0) { sum = 1; } else { sum = -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
java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] a = new int[n]; int[] s = new int[n]; long sum = 0; a[0] = scanner.nextInt(); sum += a[0]; boolean check; if(a[0] < 0){ check = false; }else{ check = true; } long count = 0; for(int i=1;i<n;i++){ a[i] = scanner.nextInt(); long x = sum + a[i]; long y = 0; if(check && x >= 0){ y = -1 - x; }else if(!check && x < 0){ y = 1 - x; }else if(!check && x == 0){ y = 1; } a[i] += y; count += Math.abs(y); sum += a[i]; //System.out.println(y + " " + a[i] + " " + sum); check = !check; } if(sum == 0){ count ++; } System.out.println(count); } }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; 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) { 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
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1e9 + 7; int main() { ll n; cin >> n; ll a[n]; for (int i = 0; i < n; i++) cin >> a[i]; ll before = 1, tmp = 0, sum = a[0]; if (a[0] / llabs(a[0]) != 1) { tmp += 1 - a[0]; sum = 1; } for (int i = 1; i < n; i++) { sum += a[i]; before *= -1; if (sum == 0) { sum = before; tmp++; } else if (before != (sum / llabs(sum))) { tmp += llabs(before) + llabs(sum); sum = before; } } ll ans = tmp; before = -1, tmp = 0, sum = a[0]; if (a[0] > -1) { tmp += 1 + a[0]; sum = -1; } for (int i = 1; i < n; i++) { sum += a[i]; before *= -1; if (sum == 0) { sum = before; tmp++; } else if (before != (sum / llabs(sum))) { tmp += llabs(before) + llabs(sum); sum = before; } } ans = min(ans, tmp); 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 n; int main() { cin >> n; int a[n + 1]; for (int i = 0; i < n; i++) cin >> a[i]; bool flag; long long sum; long long ans1 = 0; sum = a[0]; flag = true; for (int i = 1; i < n; i++) { sum += a[i]; if (flag && sum <= 0) { ans1 += -sum + 1; sum = 1; } else if (!flag && sum >= 0) { ans1 += sum + 1; sum = -1; } if (flag) flag = false; else flag = true; } long long ans2 = 0; sum = a[0]; flag = false; for (int i = 1; i < n; i++) { sum += a[i]; if (flag && sum <= 0) { ans2 += -sum + 1; sum = 1; } else if (!flag && sum >= 0) { ans2 += sum + 1; sum = -1; } if (flag) flag = false; else flag = true; } cout << min(ans1, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <iostream> using namespace std; int main() { int n; long long *a cin >> n; a = new long long[n]; long long ans = 0; long long sum = 0; for (int i = 0; i < n; i++) cin >> a[i]; int flag = -1; if (a[0] > 0) flag = 1; else if (a[0] == 0) flag = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (flag > 0 || flag == 0) { if (sum <= 0) { while (sum <= 0) { sum++; ans++; } } flag = -1; } else if (flag < 0) { if (sum >= 0) { while (sum >= 0) { sum--; ans++; } } flag = 1; } } cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int N; int a; vector<int> V; cin >> N; for (int i = 0; i < N; i++) { cin >> a; V.push_back(a); } int _sum = V[0]; int count = 0; for (int i = 1; i < N; i++) { if (_sum < 0) { _sum = _sum + V[i]; if (_sum < 0) { count = 1 - _sum; cout << count << endl; _sum = 1; } } else { _sum = _sum + V[i]; if (_sum > 0) { count = 1 + _sum; cout << count << endl; _sum = -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 ll = long long; using ld = long double; int MOD = 1e9 + 7; using namespace std; struct UnionFind { vector<int> par; UnionFind(int N) : par(N) { for (int i = 0; i < N; i++) par[i] = i; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return; par[rx] = ry; } bool same(int x, int y) { int rx = root(x); int ry = root(y); return rx == ry; } }; int main() { ll n; cin >> n; int sum = 0; int reg_1 = 0; vector<int> vec(n); for (int i = 0; i < n; i++) { cin >> vec[i]; } for (int i = 0; i < n; i++) { if (i % 2 == 0) { sum += vec[i]; if (sum <= 0) { reg_1 += (1 - sum); sum = 1; } } else { sum += vec[i]; if (sum >= 0) { reg_1 += (sum + 1); sum = -1; } } } sum = 0; int reg_2 = 0; for (int i = 0; i < n; i++) { if (i % 2 == 1) { sum += vec[i]; if (sum <= 0) { reg_2 += (1 - sum); sum = 1; } } else { sum += vec[i]; if (sum >= 0) { reg_2 += (sum + 1); sum = -1; } } } cout << min(reg_1, reg_2) << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; int solve(bool sign, vi &a, int N) { int S = a.at(0); int ans = 0; if (sign) { if (S <= 0) { ans = -S + 1; S = 1; } } else { if (S >= 0) { ans = S + 1; S = -1; } } for (int i = (1); i < (N); ++i) { if (S > 0) { S += a.at(i); if (S >= 0) { ans += (S + 1); S = -1; } } else { S += a.at(i); if (S <= 0) { ans += (-S + 1); S = 1; } } } printf("ans = %d\n", ans); return ans; } int main() { int N; cin >> N; vi a(N); for (int i = (0); i < (N); ++i) { cin >> a.at(i); } int ans; ans = min(solve(false, a, N), solve(true, a, N)); cout << ans << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int,input().split())) def wh(cst,ttl,flg): for i in range(n): ttl += a[i] if ttl*flg < 0: flg *= -1 else: if flg > 0: memo = abs(ttl)+1 ttl -= memo cst += memo elif flg < 0: memo = abs(ttl)+1 ttl += memo cst += memo flg *= -1 return cst ttl = a[0] cst = 0 cst = wh(cst,ttl,1) ttl = a[0] cst2 = 0 cst2 = wh(cst2,ttl,-1) print(min(cst,cst2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 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 qualified Data.Vector.Unboxed as VU import qualified Data.ByteString.Char8 as B import Data.Char solve :: VU.Vector Int -> Int -> Int solve vec n | VU.length vec == 2 && VU.sum vec == 0 = 1 | VU.length vec == 2 && VU.sum vec /= 0 = 0 | otherwise = minimum $ map fst [f, g] where t = VU.take 2 vec d = VU.drop 2 vec f = VU.foldl' step (fst $ init t) d g = VU.foldl' step (snd $ init t) d init :: VU.Vector Int -> ((Int, Int), (Int, Int)) init vec | a + b == 0 = ((1, 1), (1, negate 1)) | a + b > 0 = ((0, a + b), (1 + a + b, negate 1)) | otherwise = ((0, a + b), (1 - (a+b), 1)) where a = VU.head vec b = VU.last vec step :: (Int, Int) -> Int -> (Int, Int) step (res, acc) x | acc + x == 0 = (res + 1, negate (signum acc)) | (signum acc) /= signum (acc + x) = (res, acc + x) | otherwise = let aim = negate $ signum acc y = aim - (acc + x) in (res + abs y, aim) main = do n <- readLn :: IO Int as <- VU.unfoldrN n (B.readInt . B.dropWhile isSpace) <$> B.getLine print $ solve as n
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
def sign(x): if x<0: return -1 elif x>0: return 1 else: return 0 n = int(input()) a = list(map(int,input().split())) cumulative_sum = a[0] flag = sign(cumulative_sum) ans = 0 for i in range(1,n): cumulative_sum += a[i] if sign(cumulative_sum) == flag or sign(cumulative_sum) == 0: ans += abs(-flag - cumulative_sum) flag = sign(cumulative_sum) print(ans)
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# coding: utf-8 # Here your code N = int(input()) a = [int(i) for i in input().split()] result_1 = 0 before_sum =a[0] if a[0] == 0: before_sum = 1 result_1 += 1 after_sum =a[0] for i in range(1,N): before_sum = after_sum after_sum = before_sum + a[i] if before_sum * after_sum > 0: if after_sum < 0: result_1 += 1 - after_sum after_sum = 1 elif after_sum > 0: result_1 += 1 + after_sum after_sum = -1 elif before_sum * after_sum == 0: result_1 += 2 if before_sum < 0: after_sum = 1 else: after_sum = -1 if a[0] < 0: before_sum = 1 elif a[0] >= 0: before_sum = -1 after_sum =before_sum result_2 = 1 + abs(before_sum) for i in range(1,N): before_sum = after_sum after_sum = before_sum + a[i] if before_sum * after_sum > 0: if after_sum < 0: result_2 += 1 - after_sum after_sum = 1 elif after_sum > 0: result_2 += 1 + after_sum after_sum = -1 elif before_sum * after_sum == 0: result_2 += 2 if before_sum < 0: after_sum = 1 else: after_sum = -1 print(after_sum) print(min(result_1,result_2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; const int MOD = 1000000007; const int mod = 1000000007; const int INF = 1000000000; const long long LINF = 1e18; const int MAX = 510000; int code(int n) { if (n < 0) return 1; else if (n > 0) return 0; else return 2; } int main() { int n; int sum = 0; long long int ans = 0; long long int ans2 = 0; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } sum = a.at(0); if (sum != 0) { for (int i = 1; i < n; i++) { if (sum + a.at(i) == 0) { ans++; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else if (code(sum + a.at(i)) == code(sum)) { ans += abs(sum + a.at(i)) + 1; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else { sum = a.at(i) + sum; } } cout << ans << endl; return 0; } else if (sum == 0) { sum = -1; ans = 1; for (int i = 1; i < n; i++) { if (sum + a.at(i) == 0) { ans++; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else if (code(sum + a.at(i)) == code(sum)) { ans += abs(sum + a.at(i)) + 1; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else { sum = a.at(i) + sum; } } sum = 1; ans2 = 1; for (int i = 1; i < n; i++) { if (sum + a.at(i) == 0) { ans2++; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else if (code(sum + a.at(i)) == code(sum)) { ans2 += abs(sum + a.at(i)) + 1; if (sum > 0) sum = -1; else if (sum < 0) sum = 1; } else { sum = a.at(i) + sum; } } } cout << min(ans, ans2) << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# encoding: utf-8 n = int(input()) a = list(map(int, input().split())) anss = [0] * 2 for init in range(2): ans = 0 state = init sum_tmp = 0 for i, ai in enumerate(a): ans_tmp = 0 # 0 if i == 0: if state == 1 and ai <= 0: ans_tmp += (-ai + 1) elif state == 0 and ai >= 0: ans_tmp += (ai + 1) sum_tmp = ai + ans_tmp if state == 1 else ai - ans_tmp continue # > 1 state = 1 - state ans_tmp = 0 if state == 1: sum_tmp += ai if sum_tmp <= 0: ans_tmp += (-sum_tmp + 1) # 1 sum_tmp += ans_tmp ans += ans_tmp else: sum_tmp += ai if sum_tmp >= 0: ans_tmp += (sum_tmp + 1) # -1 sum_tmp -= ans_tmp ans += ans_tmp anss[init] = ans print(min(anss))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; inline signed wait() { return 0; } inline void dout(const char *arg, ...) {} template <typename T> inline void SWAP(T &a, T &b) { T t = a; a = b; b = t; } inline void CSWAP(char *&a, char *&b) { char *t = a; a = b; b = t; } void CombSort(int N, int *ar, int order_ascending = 1) { if (N <= 1) return; int h = int(N / 1.3); int flag; int i; while (true) { flag = 0; for (i = 0; i + h < N; ++i) { if ((order_ascending && ar[i] > ar[i + h]) || (!order_ascending && ar[i] < ar[i + h])) { swap<int>(ar[i], ar[i + h]); flag = 1; } } if (h == 1 && !flag) break; if (h == 9 || h == 10) h = 11; if (h > 1) h = int(h / 1.3); } } void CombSort(int N, long long int *ar, int order_ascending = 1) { if (N <= 1) return; int h = int(N / 1.3); int flag; int i; while (true) { flag = 0; for (i = 0; i + h < N; ++i) { if ((order_ascending && ar[i] > ar[i + h]) || (!order_ascending && ar[i] < ar[i + h])) { swap<long long int>(ar[i], ar[i + h]); flag = 1; } } if (h == 1 && !flag) break; if (h == 9 || h == 10) h = 11; if (h > 1) h = int(h / 1.3); } } int EuclideanAlgorithm(int N, int *ar) { for (int n = 0; n < N - 1; ++n) { while (true) { if (ar[n] % ar[n + 1] == 0 || ar[n + 1] % ar[n] == 0) { ar[n + 1] = ar[n] < ar[n + 1] ? ar[n] : ar[n + 1]; break; } if (ar[n] > ar[n + 1]) { ar[n] %= ar[n + 1]; } else { ar[n + 1] %= ar[n]; } } } return ar[N - 1]; } template <typename T> void CombSort(int N, T *ar, int order_ascending = 1) { if (N <= 1) return; int i, flag; int h = int(N / 1.3); while (true) { flag = 0; for (i = 0; i + h < N; ++i) { if (order_ascending && ar[i].SortValue > ar[i + h].SortValue || !order_ascending && ar[i].SortValue < ar[i + h].SortValue) { swap<T>(ar[i], ar[i + h]); flag = 1; } } if (h > 1) { h = int(h / 1.3); if (h == 9 || h == 10) h = 11; } else { if (!flag) break; } } } struct UnionFind { vector<int> par; UnionFind(int N) : par(N) { for (int i = 0; i < N; i++) par[i] = i; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(int x, int y) { int rx = root(x); int ry = root(y); if (rx == ry) return; par[rx] = ry; } bool same(int x, int y) { int rx = root(x); int ry = root(y); return rx == ry; } }; void Replace(char *c, int len, char before, char after) { for (int i = 0; i < len; ++i) { if (c[i] == before) c[i] = after; } } void Replace(char *c, char before, char after) { int len = strlen(c); Replace(c, len, before, after); } class csNode { public: csNode() {} }; class csStack { public: csStack() { num = 0; } void alloc(int size) { param = new int[size]; } void sort(int order = 1) { if (num > 1) CombSort(num, param, order); } int num; int *param; void push(int p) { param[num++] = p; } }; class csPosition { public: csPosition() { x = y = 0; } int x, y; }; template <typename T> class csPos { public: csPos() { x = y = 0; } T x, y; }; char s[200010]; signed main() { long long int n; scanf("%lld", &n); long long int cnt = 0; long long int i; long long int a, b, sum = 0; scanf("%lld", &b); sum = b; long long int flag; b < 0 ? flag = -1 : flag = 1; for (i = 1; i < n; ++i) { scanf("%lld", &a); sum += a; if (flag == -1 && sum < 0) { cnt += 1 - sum; sum = 1; } if (flag == 1 && sum > 0) { cnt += sum + 1; sum = -1; } if (sum == 0) { cnt++; if (flag == 1) { sum = -1; } else { sum = 1; } } flag *= -1; } cout << cnt << endl; ; return wait(); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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<int> a) { int time = 0; int pre_sum = a.at(0); int n = a.size(); if (a.at(0) < 0) { for (int i = 1; i < n; i++) { int sum = pre_sum + a.at(i); if (i % 2 == 1 && sum <= 0) { time += abs(sum - 1); sum = 1; } else if (i % 2 == 0 && sum >= 0) { time += abs(sum + 1); sum = -1; } pre_sum = sum; } } else if (a.at(0) > 0) { for (int i = 1; i < n; i++) { int sum = pre_sum + a.at(i); if (i % 2 == 0 && sum <= 0) { time += abs(sum - 1); sum = 1; } else if (i % 2 == 1 && sum >= 0) { time += abs(sum + 1); sum = -1; } pre_sum = sum; } } return time; } int zerocheck(vector<int> a) { a.at(0) = 1; int time1 = check(a) + 1; a.at(0) = -1; int time2 = check(a) + 1; int time = max(time1, time2); return time; } int main() { int n; cin >> n; int time = 0; vector<int> a(n); for (auto& x : a) { cin >> x; } if (a.at(0) == 0) { time = zerocheck(a); } else { time = check(a); } cout << time << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
n = int(input()) a = list(map(int, input().split( ))) #前から貪欲でよいか #a[0]を正か負に定めて貪欲 #a[0]が正と仮定してよい if a[0]<0: for i in range(n): a[i]*=-1 #場合分け用 a2 = [a[i] for i in range(n)] ans1 = 0 if not a[0]: ans1 += 1 a[0] = 1 sm = a[0] for i in range(1,n): sm2 = sm + a[i] #print(sm,sm2) if sm2*sm>=0: if sm<0:#sm+a[i]=1 ans1 += abs((1-sm)-a[i]) a[i]=1-sm sm = 1 else:#sm+a[i] = -1 ans1 += abs(-1-sm-a[i]) a[i]=-1-sm sm = -1 else: sm=sm2 #print(a) """ else: if sm>0:#sm+a[i]=-1 ans1 += -1-sm-a[i] sm = -1 else:#sm+a[i]=1 ans1 += 1-sm-a[i] """ ans2 = abs(a2[0]+1) a2[0]=-1 sm = -1 for i in range(1,n): sm2 = sm + a2[i] #print(sm,sm2) if sm2*sm>=0: if sm<0:#sm+a[i]=1 ans2 += abs((1-sm)-a[i]) a2[i] = 1-sm sm = 1 else:#sm+a[i] = -1 ans2 += abs(-1-sm-a[i]) a2[i]=-1-sm sm = -1 else: sm =sm2 #print(a2) """ else: if sm>0:#sm+a[i]=-1 ans2 += -1-sm-a[i] sm = -1 else:#sm+a[i]=1 ans2 += 1-sm-a[i] """ #print(ans1,ans2) print(min(ans1,ans2))
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } long long ans = 0; if (a[0] >= 0) { long long sum = a[0]; for (int i = 1; i < n; ++i) { if (i % 2 == 1) { if (sum + a[i] < 0) { sum += a[i]; } else { ans += sum + a[i] + 1; sum = -1; } } else { if (sum + a[i] > 0) { sum += a[i]; } else { ans += abs(sum + a[i] - 1); sum = 1; } } } } else { long long sum = a[0]; for (int i = 1; i < n; ++i) { if (i % 2 == 1) { if (sum + a[i] > 0) { sum += a[i]; } else { ans += abs(sum + a[i] - 1); sum = 1; } } else { if (sum + a[i] < 0) { sum += a[i]; } else { ans += sum + a[i] + 1; sum = -1; } } } } cout << ans << endl; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> int ctoi(const char c) { if ('0' <= c && c <= '9') return (c - '0'); return -1; } using namespace std; using pii = pair<int, int>; long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } int main() { long long N, A[100007], msum = 0, psum = 0, mct, pct; cin >> N; for (int i = 0; i < (N); i++) { cin >> A[i]; } for (int i = 0; i < (N); i++) { if (i % 2 == 0) { if (A[i] + psum > 0) { psum += A[i]; } else { pct += -(A[i] + psum) + 1; psum = 1; } if (A[i] + msum >= 0) { mct += A[i] + msum + 1; msum = -1; } else { msum += A[i]; } } else { if (A[i] + msum > 0) { msum += A[i]; } else { mct += -(A[i] + msum) + 1; msum = 1; } if (A[i] + psum >= 0) { pct += A[i] + psum + 1; psum = -1; } else { psum += A[i]; } } } cout << min(pct, mct); }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; long long sum = a[0]; long long cnt = 0; if (sum == 0) { int ind = 1; for (int i = 0; i < n; i++) { if (a[i] != 0) ind = i; break; } if (a[ind] > 0) sum = (ind % 2 == 0 ? 1 : -1); else sum = (ind % 2 == 0 ? -1 : 1); cnt++; } for (int i = 1; i < n; i++) { long long nsum = sum + a[i]; if (sum > 0 && nsum < 0 || sum < 0 && nsum > 0) { sum = nsum; continue; } sum = (sum > 0 ? -1 : 1); cnt += (nsum == 0 ? 1 : abs(nsum) + 1); } cout << cnt << endl; return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
# coding: utf-8 import sys #from operator import itemgetter sysread = sys.stdin.buffer.readline read = sys.stdin.buffer.read #from heapq import heappop, heappush #from collections import defaultdict sys.setrecursionlimit(10**7) #import math #from itertools import product, accumulate, combinations, product #import bisect import numpy as np #from copy import deepcopy #from collections import deque #from decimal import Decimal #from numba import jit INF = 1 << 50 EPS = 1e-8 def run(): n, *A = map(int, read().split()) v = 0 acum = [] for a in A: v += a acum.append(v) # greedy cums = 0 count = 0 V = A[0] // abs(A[0]) for a in acum[1:]: #print(a, '---------') V *= -1 if (a + cums) * V > 0: continue else: update = abs(a + cums) + 1 cums += (update) * V count += update #print(V, cums, count) print(count) if __name__ == "__main__": run()
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "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() { while (1) { int a, b = 0, c = 0; int n; cin >> n >> a; b += a; for (int i = 1; i < n; i++) { cin >> a; while ((a + b) / b >= 0) { if (a + b > 0) a--; else if (a + b < 0) a++; else if (b > 0) a--; else if (b < 0) a++; c++; } b += a; } cout << c << endl; } return 0; }
p03739 AtCoder Beginner Contest 059 - Sequence
You are given an integer sequence of length N. The i-th term in the sequence is a_i. In one operation, you can select a term and either increment or decrement it by one. At least how many operations are necessary to satisfy the following conditions? * For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero. * For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term. Constraints * 2 ≤ n ≤ 10^5 * |a_i| ≤ 10^9 * Each a_i is an integer. Input Input is given from Standard Input in the following format: n a_1 a_2 ... a_n Output Print the minimum necessary count of operations. Examples Input 4 1 -3 1 0 Output 4 Input 5 3 -6 4 -5 7 Output 0 Input 6 -1 4 3 2 -5 4 Output 8
{ "input": [ "5\n3 -6 4 -5 7", "4\n1 -3 1 0", "6\n-1 4 3 2 -5 4" ], "output": [ "0", "4", "8" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> int main(void) { int n, i, check = 0; long long int a, count = 0, sum = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%lld", &a); sum += a; if (check == 1 && sum >= 0) { count += (1 + sum); sum = -1; } else if (check == -1 && sum <= 0) { count += (1 - sum); sum = 1; } if (sum > 0) { check = 1; } else { check = -1; } } printf("%lld", count); return 0; }