Problem:
 Score : 300 points    Problem Statement You are given an integer sequence of length N . The i -th term in the sequence is a_i .
In one operation, you can select a term and either increment or decrement it by one.  At least how many operations are necessary to satisfy the following conditions?   For every i  (1≤i≤n) , the sum of the terms from the 1 -st through i -th term is not zero.  For every i  (1≤i≤n-1) , the sign of the sum of the terms from the 1 -st through i -th term, is different from the sign of the sum of the terms from the 1 -st through (i+1) -th term.       Constraints  2 ≤ n ≤ 10^5  |a_i| ≤ 10^9  Each a_i is an integer.         Input Input is given from Standard Input in the following format:  n  a_1  a_2  ...  a_n       Output Print the minimum necessary count of operations.        Sample Input 1 4
1 -3 1 0      Sample Output 1 4  For example, the given sequence can be transformed into 1, -2, 2, -2 by four operations. The sums of the first one, two, three and four terms are 1, -1, 1 and -1 , respectively, which satisfy the conditions.       Sample Input 2 5
3 -6 4 -5 7      Sample Output 2 0  The given sequence already satisfies the conditions.       Sample Input 3 6
-1 4 3 2 -5 4      Sample Output 3 8

Solution (Python):
class Bit:
    def __init__(self, n):
        """
        :param n: 最大の要素数
        """
        self.n = n
        self.tree = [0]*(n+1)
        self.depth = n.bit_length() - 1

    def sum(self, i):
        """ 区間[0,i) の総和を求める """
        s = 0
        i -= 1
        while i >= 0:
            s += self.tree[i]
            i = (i & (i + 1) )- 1
        return s

    def built(self, array):
        """ array を初期値とするBITを構築 """
        for i, a in enumerate(array):
            self.add(i, a)

    def add(self, i, x):
        """ i 番目の要素に x を足す """
        while i < self.n:
            self.tree[i] += x
            i |= i + 1

    def get(self, i, j):
        """ 部分区間和 [i, j) """
        if i == 0:
            return self.sum(j)
        return self.sum(j) - self.sum(i)

    def lower_bound(self, x, equal=False):
        """
        (a0+a1+...+ai < x となる最大の i (存在しない時は -1 ) , その時の a0+a1+...+ai )
        a0+a1+...+ai <= x としたい場合は equal = True
        二分探索であるため、ai>=0 を満たす必要がある
        """
        sum_ = 0
        pos = -1    
        if not equal:
            for i in range(self.depth, -1, -1):
                k = pos + (1 << i)
                if k < self.n and sum_ + self.tree[k] < x:  
                    sum_ += self.tree[k]
                    pos += 1 << i
        if equal:
            for i in range(self.depth, -1, -1):
                k = pos + (1 << i)
                if k < self.n and sum_ + self.tree[k] <= x: 
                    sum_ += self.tree[k]
                    pos += 1 << i
        return pos, sum_

    def __getitem__(self, i):
        """ [a0, a1, a2, ...] """
        return self.get(i, i+1)

    def __iter__(self):
        """ [a0, a1, a2, ...] """
        for i in range(self.n):
            yield self.get(i, i+1)

    def __str__(self):
        text1 = " ".join(["element:            "] + list(map(str, self)))
        text2 = " ".join(["cumsum(1-indexed):  "] + list(str(self.sum(i)) for i in range(1, self.n + 1)))
        return "\n".join((text1, text2))


def solve(sign):
    B = Bit(N)
    B.built(A)
    res = 0
    for i in range(N):
        tmp = B.sum(i + 1)
        if tmp * sign > 0:
            B.add(i, -1 * sign * (abs(tmp) + 1))
            res += abs(tmp) + 1
        elif tmp == 0:
            B.add(i, -1 * sign)
            res += 1
        sign *= -1
    return res

import sys
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))
print(min(solve(-1), solve(1)))

Solution (Python):
n=int(input())
A=list(map(int,input().split()))

s1=0
now=0
for i in range(n):
    now+=A[i]
    if i%2==1 and now<=0:
        s1+= (1-now)
        now=1
    if i%2==0 and now>=0:
        s1+= now+1
        now=-1

s2=0
now=0
for i in range(n):
    now+=A[i]
    if i%2==0 and now<=0:
        s2+= (1-now)
        now=1
    if i%2==1 and now>=0:
        s2+= now+1
        now=-1
print(min(s1,s2))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
c1=0
s=0
for i in range(n):
  s+=a[i]
  if i%2:
    if s>=0:
      c1+=s+1
      s=-1
  else:
    if s<=0:
      c1+=-s+1
      s=1
c2=0
s=0
for i in range(n):
  s+=a[i]
  if i%2:
    if s<=0:
      c2+=-s+1
      s=1
  else:
    if s>=0:
      c2+=s+1
      s=-1
print(min(c1,c2))

Solution (Python):


import sys
read = sys.stdin.read
readline = sys.stdin.readline

n,*a = map(int,read().split())


for i in range(1,n):
    a[i] += a[i-1]

aa = a[:]

sgn = 1
r1 = 0
v = 0
for i in range(n):
    a[i] += v
    if sgn:
        if a[i] <= 0:
            r1 += -a[i]+1
            v += -a[i]+1
    else:    
        if a[i] >= 0:
            r1 += a[i]+1
            v -= a[i]+1
    sgn ^= 1
    

a = aa
sgn = 0
r2 = 0
v = 0
for i in range(n):
    a[i] += v
    if sgn:
        if a[i] <= 0:
            r2 += -a[i]+1
            v += -a[i]+1
    else:    
        if a[i] >= 0:
            r2 += a[i]+1
            v -= a[i]+1
    sgn ^= 1


print(min(r1,r2))

Solution (Python):
def main():
    n = int(input())
    A = list(map(int, input().split()))
    s = 0
    c1 = 0
    for i, a in enumerate(A):
        s += a
        if i % 2 == 0:
            if s == 0:
                c1 += 1
            elif s < 0:
                c1 += 1 - s
            else:
                continue
            s = 1
        else:
            if s == 0:
                c1 += 1
            elif s > 0:
                c1 += s + 1
            else:
                continue
            s = -1
    s = 0
    c2 = 0
    for i, a in enumerate(A):
        s += a
        if i % 2 != 0:
            if s == 0:
                c2 += 1
            elif s < 0:
                c2 += 1 - s
            else:
                continue
            s = 1
        else:
            if s == 0:
                c2 += 1
            elif s > 0:
                c2 += s + 1
            else:
                continue
            s = -1
    print(min(c1, c2))


if __name__ == '__main__':
    main()

Solution (Python):
import sys


def func(alst, sign):
  cur, res = 0, 0
  for i in alst:
    cur += i
    if sign and cur <= 0:
      res += abs(cur) + 1
      cur = 1
    elif not sign and cur >= 0:
      res += abs(cur) + 1
      cur = -1
    sign = not sign
  return res


def main():
  n, *alst = map(int, sys.stdin.read().split())
  print(min(func(alst, True), func(alst, False)))


if __name__ == '__main__':
  main()

Solution (Python):
A=int(input())
l=list(map(int,input().split()))
sum_n=0
ans=0
caseA=0
for i in range(A):
   sum_n+=l[i]
   if i%2==0:
      if 0<sum_n:
         continue
      else:
         ans+=abs(1-sum_n)
         sum_n=1
   else:
      if sum_n < 0:
         continue
      else:
         ans+=abs(-1-sum_n)
         sum_n=-1
caseA=ans
ans=0
sum_n=0
for i in range(A):
   sum_n+=l[i]
   if i%2==1:
      if 0<sum_n:
         continue
      else:
         ans+=abs(1-sum_n)
         sum_n=1
   else:
      if sum_n < 0:
         continue
      else:
         ans+=abs(-1-sum_n)
         sum_n=-1
print(min(ans,caseA))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
ans = [0,0]

s = max(a[0],1)
ans[0] = abs(s-a[0])
for i in range(n-1):
    if s * (s + a[i+1]) >= 0:
        if s > 0:
            ans[0] += s + a[i+1] + 1
            s = -1
        else:
            ans[0] += 1 - (s + a[i+1])
            s = 1
    else:
            s += a[i+1]
s = min(-1,a[0])
ans[1] = abs(s-a[0])
for i in range(n-1):
    if s * (s + a[i+1]) >= 0:
        if s > 0:
            ans[1] += s + a[i+1] + 1
            s = -1
        else:
            ans[1] += 1 - (s + a[i+1])
            s = 1
    else:
            s += a[i+1]
print(min(ans))

Solution (Python):
n = int(input())

arr = [int(x) for x in input().split(" ")]

ans = 0

sum = 0

curr = True

for i in range(len(arr)):
    if ((sum + arr[i]) == 0 or ((sum + arr[i]) > 0) == curr):
        if curr:
            temp = -1 - sum
        else:
            temp = 1 - sum
        ans += abs(temp - arr[i])
        sum += temp
    else:
        sum += arr[i]
    curr = not curr

ans1 = 0

curr = False

sum = 0

for i in range(len(arr)):
    if ((sum + arr[i]) == 0 or ((sum + arr[i]) > 0) == curr):
        if curr:
            temp = -1 - sum
        else:
            temp = 1 - sum
        ans1 += abs(temp - arr[i])
        sum += temp
    else:
        sum += arr[i]
    curr = not curr

print(min(ans,ans1))

Solution (Python):
N, = map(int, input().split())
A = list(map(int, input().split()))
s = 0
r = 0
for i, a in enumerate(A):
    s += a
    if i % 2:
        if s >= 0:
            r += s+1
            s = -1
    else:
        if s <= 0:
            r += -s+1
            s = 1
s = 0
r2 = 0
for i, a in enumerate(A):
    s += a
    if i % 2:
        if s <= 0:
            r2 += -s+1
            s = 1
    else:
        if s >= 0:
            r2 += s+1
            s = -1
print(min(r, r2))

Solution (Python):
import copy

n = int(input())
a = list(map(int, input().split()))


def f(org_a, odd):
    a = copy.copy(org_a)
    ans = 0
    if a[0] == 0:
        a[0] = 1 if odd else -1
        ans += 1
    elif a[0] > 0 and not odd:
        ans += a[0] + 1
        a[0] = -1
    elif a[0] < 0 and odd:
        ans += -a[0] + 1
        a[0] = 1
    current_sum = a[0]
    for i in range(1, len(a)):
        move = 0
        is_positive = current_sum > 0
        current_sum += a[i]
        if is_positive and current_sum >= 0:
            move = -current_sum - 1
        elif not is_positive and current_sum <= 0:
            move = -current_sum + 1

        current_sum += move
        
        a[i] += move
        ans += abs(move)
    return ans


print(min(f(a, True), f(a, False)))

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)


MOD = int(1e09) + 7
INF = int(1e15)


def main():
    
    N = Scanner.int()
    A = Scanner.map_int()
    s1, s2 = 0, 0
    c1, c2 = 0, 0
    for i in range(N):
        if i % 2 == 0:
            if s1 + A[i] > 0:
                s1 += A[i]
            else:
                c1 += abs(s1 + A[i]) + 1
                s1 = 1
            if s2 + A[i] < 0:
                s2 += A[i]
            else:
                c2 += abs(s2 + A[i])+1
                s2 = -1
        else:
            if s1 + A[i] < 0:
                s1 += A[i]
            else:
                c1 += abs(s1 + A[i]) + 1
                s1 = -1
            if s2 + A[i] > 0:
                s2 += A[i]
            else:
                c2 += abs(s2 + A[i]) + 1
                s2 = 1
    print(min(c1, c2))
    return


if __name__ == "__main__":
    main()

Solution (Python):

N = int(input())
a = list(map(int,input().split()))

r = 0
res = 0
for i in range(N):
    sa = 0
    r += a[i]
    if i%2==0:
        if r <= 0:
            sa = 1-r
            r += sa
    else:
        if r >= 0:
            sa = r+1
            r -= sa
    res += sa


r = 0
res2 = 0
for i in range(N):
    sa = 0
    r += a[i]
    if i%2==1:
        if r <= 0:
            sa = 1-r
            r += sa
    else:
        if r >= 0:
            sa = r+1
            r -= sa
    res2 += sa
print(min(res,res2))

Solution (Python):

def modify_seq(seq: list, is_positive: bool) -> int:
    cur, cnt = 0, 0
    for i in seq:
        cur += i
        if is_positive and cur <= 0:
            cnt += abs(cur) + 1
            cur = 1
        elif not is_positive and cur >= 0:
            cnt += abs(cur) + 1
            cur = -1
        is_positive = not is_positive
    return cnt


def main():
    _, *A = map(int, open(0).read().split())
    print(min(modify_seq(A, True), modify_seq(A, False)))


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
lst = list(map(int, input().rstrip().split()))

cur, cnt_1 = 0, 0
for i, j in enumerate(lst):
    new = cur + j
    if i % 2 == 0 and new <= 0:
        cnt_1 += abs(new) + 1
        cur = 1
    elif i % 2 == 1 and new >= 0:
        cnt_1 += abs(new) + 1
        cur = -1
    else:
        cur = new


cur, cnt_2 = 0, 0
for i, j in enumerate(lst):
    new = cur + j
    if i % 2 == 0 and new >= 0:
        cnt_2 += abs(new) + 1
        cur = -1
    elif i % 2 == 1 and new <= 0:
        cnt_2 += abs(new) + 1
        cur = 1
    else:
        cur = new
print(min(cnt_1, cnt_2))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
now = a[0]
if now == 0:
  ans = 1
  now = 1
else:
  ans = 0
for i in a[1:]:
  t = now + i
  if now * t >= 0:
    if now >= 0:
      ans += t + 1
      now = -1
    else:
      ans += 1 - t
      now = 1
  else:
    now = t
    
if a[0] >= 0:
  now = -1
  ans2 = a[0] - now
else:
  now = 1
  ans2 = now - a[0]
for i in a[1:]:
  t = now + i
  if now * t >= 0:
    if now >= 0:
      ans2 += t + 1
      now = -1
    else:
      ans2 += 1 - t
      now = 1
  else:
    now = t
ans = min(ans,ans2)
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines



N,*A = map(int,read().split())

def solve(A):
    
    cum = 0
    cnt = 0
    for x in A:
        if cum <= 0:
            
            cum += x
            if cum <= 0:
                cnt += (1 - cum)
                cum = 1
        else:
            
            cum += x
            if cum >= 0:
                cnt += (1 + cum)
                cum = -1
    return cnt

x = solve(A)
y = solve([-x for x in A])
answer = min(x,y)
print(answer)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

def cost(a, neg=False):
    sign = -1 if neg else 1
    if sign * a[0] > 0:
        ret = 0
        cum = a[0]
    else:
        ret = abs(a[0] - sign)
        cum = sign
    for i in range(1, n):
        sign *= -1
        cum += a[i]
        if cum * sign > 0:
            continue
        else:
            ret += abs(cum - sign)
            cum = sign
    return ret

print(min(cost(a, False), cost(a, True)))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))


s=0
anse=0
for i in range(n):
    s+=a[i]
    if i%2==0 and s<=0:
        anse+=1-s
        s=1
    elif i%2==1 and s>=0:
        anse+=s+1
        s=-1


s=0
anso=0
for i in range(n):
    s+=a[i]
    if i%2==0 and s>=0:
        anso+=s+1
        s=-1
    elif i%2==1 and s<=0:
        anso+=1-s
        s=1

ans=min(anse,anso)

print(ans)

Solution (Python):
def main():
    n=int(input())
    a = list(map(int,input().split()))
    sp=0
    sm=0
    cntp=0
    cntm=0
    for i in range(n):
        sp+=a[i]
        sm+=a[i]
        if abs(sp)>=1 and sp//(abs(sp))==(-1)**i:
            sp+=0
        else:
            cntp+=abs(sp)+1
            sp=(-1)**i
        if abs(sm)>=1 and sm//(abs(sm))==(-1)**(i+1):
            sm+=0
        else:
            cntm+=abs(sm)+1
            sm=(-1)**(i+1)
    print(min(cntp,cntm))
    
if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
 
ans = 0

def func(l, pn):
  ans = 0
  sum_ = 0
  for i in range(n):
    sum_ += l[i]
    if sum_ >= 0 and pn[i] < 0:
      ans += sum_+1
      sum_ = -1
    elif sum_ <= 0 and pn[i] > 0:
      ans += -sum_+1
      sum_ = 1
    else:
      continue
  return ans

pn1 = [1 if i%2 == 0 else -1 for i in range(n)]
pn2 = [-1 if i%2 == 0 else 1 for i in range(n)]

print(min(func(l, pn1), func(l, pn2)))

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
 
ans = 0

def func(l, pn):
  ans = 0
  sum_ = 0
  for i in range(n):
    sum_ += l[i]
    if sum_ >= 0 and pn[i] < 0:
      ans += sum_+1
      sum_ = -1
    elif sum_ <= 0 and pn[i] > 0:
      ans += -sum_+1
      sum_ = 1
    else:
      continue
  return ans

pn1 = [1 if i%2 == 0 else -1 for i in range(n)]
pn2 = [-1 if i%2 == 0 else 1 for i in range(n)]

print(min(func(l, pn1), func(l, pn2)))

Solution (Python):
N = int(input())
A = list(map(int,input().split()))

s = ans1 = 0
for i,a in enumerate(A):
    s += a
    if i%2:
        if s <= 1:
            ans1 += 1-s
            s = 1
    else:
        if s >= -1:
            ans1 += s+1
            s = -1

s = ans2 = 0
for i,a in enumerate(A):
    s += a
    if i%2==0:
        if s <= 1:
            ans2 += 1-s
            s = 1
    else:
        if s >= -1:
            ans2 += s+1
            s = -1

print(min(ans1,ans2))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

pos = 0
now = 0
for i, a in enumerate(A):
    now += a
    if i % 2 == 0:  
        if now <= 0:
            pos += -now + 1
            now = 1
    else:
        if now >= 0:
            pos += now + 1
            now = -1

neg = 0
now = 0
for i, a in enumerate(A):
    now += a
    if i % 2 == 0:
        if now >= 0:
            neg += now + 1
            now = -1
    else:
        if now <= 0:
            neg += -now + 1
            now = 1

print(min(pos, neg))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = A.copy()
ans = [0, 0]
flag1 = True
flag2 = False
for i in range(0, N):
    if i != 0:
        A[i] += A[i-1]
    if flag1:
        if A[i] <= 0:
            ans[0] += (abs(A[i])+1)
            A[i] = 1
        flag1 = False
    else:
        if A[i] >= 0:
            ans[0] += (abs(A[i])+1)
            A[i] = -1
        flag1 = True
for i in range(0, N):
    if i != 0:
        B[i] += B[i-1]
    if flag2:
        if B[i] <= 0:
            ans[1] += (abs(B[i])+1)
            B[i] = 1
        flag2 = False
    else:
        if B[i] >= 0:
            ans[1] += (abs(B[i])+1)
            B[i] = -1
        flag2 = True
print(min(ans))

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int,input().split()))
    
    tmp = 0
    tmpans1 = 0
    for i in range(N):
        tmp += A[i]
        if i%2==0 and tmp<=0:
            tmpans1 += 1-tmp
            tmp = 1
        elif i%2==1 and tmp >= 0:
            tmpans1 += 1+tmp
            tmp = -1
    tmp = 0
    tmpans2 = 0
    for i in range(N):
        tmp += A[i]
        if i%2==0 and tmp>=0:
            tmpans2 += 1+tmp
            tmp = -1
        elif i%2==1 and tmp <= 0:
            tmpans2 += 1-tmp
            tmp = 1
    
    print(min(tmpans1,tmpans2))
    
    
    
main()

Solution (Python):
import sys
input = sys.stdin.readline
def main():
    N = int(input())
    A = list(map(int,input().split()))
    tmp_o = [0 for i in range(N+1)]
    tmp_e = [0 for i in range(N+1)]
    count_o = 0
    count_e = 0
    for i in range(N):
        tmp_o[i+1] = tmp_o[i]+A[i]
        if i%2 == 1 and tmp_o[i+1] <= 0:
            count_o += 1-tmp_o[i+1]
            tmp_o[i+1] = 1
        elif i%2 == 0 and tmp_o[i+1] >= 0:
            count_o += tmp_o[i+1]+1
            tmp_o[i+1] = -1
        else:
            continue
    for i in range(N):
        tmp_e[i+1] = tmp_e[i]+A[i]
        if i%2 == 0 and tmp_e[i+1] <= 0:
            count_e += 1-tmp_e[i+1]
            tmp_e[i+1] = 1
        elif i%2 == 1 and tmp_e[i+1] >= 0:
            count_e += tmp_e[i+1]+1
            tmp_e[i+1] = -1
        else:
            continue

    print(min(count_o,count_e))

if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

def sequence(plus_start):
    cost = 0
    cum = 0

    for i in range(N):
        tmp = cum + A[i]
        target = tmp
        if plus_start:
            if i % 2 == 0:
                if tmp <= 0:
                    target = 1
            else:
                if tmp >= 0:
                    target = -1
        else:
            if i % 2 == 0:
                if tmp >= 0:
                    target = -1
            else:
                if tmp <= 0:
                    target = 1
        diff = target - tmp
        cum += A[i] + diff
        cost += abs(diff)
    return cost

print(min(sequence(True), sequence(False)))

Solution (Python):



def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def solve(la):
    ans = 0
    s = la[0]
    for i in range(1, len(la)):
        a = la[i]
        ns = s+a
        
        if s*ns < 0:
            s = ns
            continue
        ans += abs(ns)+1
        if ns < 0:
            ns += abs(ns)+1
        else:
            ns -= abs(ns)+1
        s = ns
    return ans

def main():
    n = ri()
    la = rli()
    first = la[0]

    ans1 = 0
    if first <= 0:
        ans1 += abs(first)+1
        la[0] = 1
    ans1 += solve(la)

    ans2 = 0
    la[0] = first
    if first >= 0:
        ans2 += abs(first)+1
        la[0] = -1
    ans2 += solve(la)
    print(min(ans1, ans2))


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
A = [int(i) for i in input().split()]
ct_1 = 0
ct_2 = 0
aa = 0

j = 1
for i in range(n):
    if (aa + A[i]) * j <= 0:
        ct_1 += abs(aa + A[i]) + 1
        aa = j
    else:
        aa += A[i]
    j *= -1

aa = 0
j = -1
for i in range(n):
    if (aa + A[i]) * j <= 0:
        ct_2 += abs(aa + A[i]) + 1
        aa = j
    else:
        aa += A[i]
    j *= -1

print(min(ct_1, ct_2))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a1=0
t=0
for i in range(n):
    t+=a[i]
    if i%2==0:
        if t<=0:
            a1+=1-t
            t=1
    else:
        if t>=0:
            a1+=1+t
            t=-1
a2=0
t=0
for i in range(n):
    t+=a[i]
    if i%2==1:
        if t<=0:
            a2+=1-t
            t=1
    else:
        if t>=0:
            a2+=1+t
            t=-1
print(min(a1,a2))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a1=0
t=0
for i in range(n):
    t+=a[i]
    if i%2==0:
        if t<=0:
            a1+=1-t
            t=1
    else:
        if t>=0:
            a1+=1+t
            t=-1
a2=0
t=0
for i in range(n):
    t+=a[i]
    if i%2==1:
        if t<=0:
            a2+=1-t
            t=1
    else:
        if t>=0:
            a2+=1+t
            t=-1
print(min(a1,a2))

Solution (Python):
N = int(input())
A = list(map(int,input().split()))

def calc(a, t):
    ans = 0
    p_flag = t < 0
    t += a
    if p_flag:
        if t < 0:
            ans = 1 - t
            t = 1
    else:
        if t >= 0:
            ans = 1 + t
            t = -1
    if t == 0:
        ans += 1
        if p_flag:
            t = 1
        else:
            t = -1

    return ans, t

ans_p = 0
t_p = 0
ans_m = 0
t_m = 0
if A[0] > 0:
    ans_p = 0
    t_p = A[0]
    ans_m = A[0]+1
    t_m = -1
elif A[0] < 0:
    ans_p = -A[0]+1
    t_p = 1
    ans_m = 0
    t_m = A[0]
else:
    ans_p = 1
    t_p = 1
    ans_m = 1
    t_m = -1

for i in range(1, N):
    a, t_p = calc(A[i], t_p)
    ans_p += a
    a, t_m = calc(A[i], t_m)
    ans_m += a

print(min(ans_p, ans_m))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))


ans = 0
ansans = 0
now = 0

for i in range(n):
    now += a[i]
    if i % 2 == 0:
        if now < 0:
            ans += abs(now) + 1
            now = 1
        elif now == 0:
            ans += 1
            now = 1
    else:
        if now >= 0:
            ans += abs(now) + 1
            now = -1
        elif now == 0:
            ans += 1
            now = -1

now = 0
for i in range(n):
    now += a[i]
    if i % 2 == 0:
        if now > 0:
            ansans += abs(now) + 1
            now = -1
        elif now == 0:
            ansans += 1
            now = -1
    else:
        if now < 0:
            ansans += abs(now) + 1
            now = 1
        elif now == 0:
            ansans += 1
            now = 1

print(min(ans, ansans))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
pm_sum = 0
pm_count = 0
mp_sum = 0
mp_count = 0
for i in range(n):
    pm_sum += a[i]
    mp_sum += a[i]
    if i % 2 == 0:
        pm_d = max(0, 1 - pm_sum)
        mp_d = max(0, mp_sum - (-1))
        pm_sum += pm_d
        mp_sum -= mp_d
    else:
        pm_d = max(0, pm_sum - (-1))
        mp_d = max(0, 1 - mp_sum)
        pm_sum -= pm_d
        mp_sum += mp_d
    pm_count += pm_d
    mp_count += mp_d
print(min(pm_count, mp_count))

Solution (Python):

def main():
    n = int(input())
    lst = list(map(int, input().rstrip().split()))
    cnt = [0] * 2
    
    
    cur = 0
    for i, j in enumerate(lst):
        cur += j
        if i % 2 == 0 and cur <= 0:
            cnt[0] += abs(cur) + 1
            cur = 1
        elif i % 2 == 1 and cur >= 0:
            cnt[0] += abs(cur) + 1
            cur = -1

    
    cur = 0
    for i, j in enumerate(lst):
        cur += j
        if i % 2 == 0 and cur >= 0:
            cnt[1] += abs(cur) + 1
            cur = -1
        elif i % 2 == 1 and cur <= 0:
            cnt[1] += abs(cur) + 1
            cur = 1

    print(min(cnt))


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    n = int(input())
    lst = list(map(int, input().rstrip().split()))
    
    cur, cnt_1 = 0, 0
    for i, j in enumerate(lst):
        new = cur + j
        if i % 2 == 0 and new <= 0:
            cnt_1 += abs(new) + 1
            cur = 1
        elif i % 2 == 1 and new >= 0:
            cnt_1 += abs(new) + 1
            cur = -1
        else:
            cur = new

    
    cur, cnt_2 = 0, 0
    for i, j in enumerate(lst):
        new = cur + j
        if i % 2 == 0 and new >= 0:
            cnt_2 += abs(new) + 1
            cur = -1
        elif i % 2 == 1 and new <= 0:
            cnt_2 += abs(new) + 1
            cur = 1
        else:
            cur = new
    print(min(cnt_1, cnt_2))


if __name__ == "__main__":
    main()

Solution (Python):

import sys
input = sys.stdin.readline

def main():
    n = int(input())
    lst = tuple(map(int, input().split()))
    
    
    cur, cnt_1 = 0, 0
    for i, j in enumerate(lst):
        new = cur + j
        if i % 2 == 0 and new <= 0:
            cnt_1 += abs(new) + 1
            cur = 1
        elif i % 2 == 1 and new >= 0:
            cnt_1 += abs(new) + 1
            cur = -1
        else:
            cur += j

    
    cur, cnt_2 = 0, 0
    for i, j in enumerate(lst):
        new = cur + j
        if i % 2 == 0 and new >= 0:
            cnt_2 += abs(new) + 1
            cur = -1
        elif i % 2 == 1 and new <= 0:
            cnt_2 += abs(new) + 1
            cur = 1
        else:
            cur += j
    
    print(min(cnt_1, cnt_2))


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    n = int(input())
    lst = list(map(int, input().rstrip().split()))
    
    
    cur, cnt_1 = 0, 0
    for i, j in enumerate(lst):
        new = cur + j
        if i % 2 == 0 and new <= 0:
            cnt_1 += abs(new) + 1
            cur = 1
        elif i % 2 == 1 and new >= 0:
            cnt_1 += abs(new) + 1
            cur = -1
        else:
            cur += j

    
    cur, cnt_2 = 0, 0
    for i, j in enumerate(lst):
        new = cur + j
        if i % 2 == 0 and new >= 0:
            cnt_2 += abs(new) + 1
            cur = -1
        elif i % 2 == 1 and new <= 0:
            cnt_2 += abs(new) + 1
            cur = 1
        else:
            cur += j
    
    print(min(cnt_1, cnt_2))


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
a = [int(_) for _ in input().split()]
ans1 = 0
ans2 = 0
tmp1 = 0
tmp2 = 0

flag = False
for i in a:
    tmp1 += i
    if flag and tmp1 <= 0:
        ans1 += 1-tmp1
        tmp1 = 1
    elif not flag and tmp1 >= 0:
        ans1 += tmp1+1
        tmp1 = -1
    
    if flag:
        flag = False
    else:
        flag = True

flag = True
for i in a:
    tmp2 += i
    if flag and tmp2 <= 0:
        ans2 += 1-tmp2
        tmp2 = 1
    elif not flag and tmp2 >= 0:
        ans2 += tmp2+1
        tmp2 = -1
    
    if flag:
        flag = False
    else:
        flag = True

print(min(ans1, ans2))

Solution (Python):
n = int(input())
*A, = map(int, input().split())
even = 0; odd = 0;
S = 0
for i, a in enumerate(A):
    S += a
    if i % 2 == 0 and S <= 0:
        even += -S + 1
        S = 1
    elif i % 2 == 1 and S >= 0:
        even += S + 1
        S = -1
S = 0
for i, a in enumerate(A):
    S += a
    if i % 2 == 0 and S >= 0:
        odd += S + 1
        S = -1
    elif i % 2 == 1 and S <= 0:
        odd += -S + 1
        S = 1
print(min(even, odd))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
x,y,z=0,0,0
for i in range(n):
    x+=a[i]
    if i%2==0:
        if x>0:pass
        else:
            y+=1-x
            x=1
    else:
        if x<0:pass
        else:
            y+=x+1
            x=-1
x=0
for i in range(n):
    x+=a[i]
    if i%2==1:
        if x>0:pass
        else:
            z+=1-x
            x=1
    else:
        if x<0:pass
        else:
            z+=x+1
            x=-1
print(min(y,z))

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
num = float('inf')
for k in [0, 1]:
    s = 0
    tmp = 0
    for i in range(N):
        s += a[i]
        if i % 2 == k:
            if s <= 0:
                tmp += abs(s) + 1
                s = 1
        else:
            if s >= 0:
                tmp += abs(s) + 1
                s = -1
    num = min(tmp, num)

print(num)