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):
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

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

p = A[0] if A[0] > 0 else 1
pc = 0 if A[0] > 0 else 1 - A[0]
m = A[0] if A[0] < 0 else -1
mc = 0 if A[0] < 0 else 1 + abs(A[0])

for i in range(1, N):
    p += A[i]
    m += A[i]
    if i % 2 == 0:
        if p <= 0:
            pc += 1 - p
            p = 1
        if m >= 0:
            mc += 1 + m
            m = -1
    else:
        if p >= 0:
            pc += 1 + p
            p = -1
        if m <= 0:
            mc += 1 - m
            m = 1

print(min(pc, mc))

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

point = 0
point2 = 0

now = 0
now2 = 0

for i,j in enumerate(a):
    now += j
    now2 += j
    if i%2 == 0:
        if now <= 0:
            point += 1-now
            now = 1
        if now2 >= 0:
            point2 += 1+now2
            now2 = -1
    else:
        if now2 <= 0:
            point2 += 1-now2
            now2 = 1
        if now >= 0:
            point += 1+now
            now = -1

print(min(point,point2))

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

def check(mode):
    ans,s = 0,0
    if mode:
        for i in range(n):
            s += a[i]
            if i%2 == 0:
                if s <= 0:
                    ans += abs(s)+1
                    s = 1
            else:
                if s >= 0:
                    ans += abs(s)+1
                    s = -1
    else:
        for i in range(n):
            s += a[i]
            if i%2 == 0:
                if s >= 0:
                    ans += abs(s)+1
                    s = -1
            else:
                if s <= 0:
                    ans += abs(s)+1
                    s = 1
    return ans

print(min(check(True),check(False)))

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 == 1:
        if s <= 0:
            c1 += 1 - s
            s = 1

    else: 
        if s >= 0:
            c1 += 1 + s
            s = -1

c2 = 0
t = 0

for i in range(n):
    t += a[i]
    if i % 2 == 1:
        if t >= 0:
            c2 += 1 + t
            t = -1
    else:
        if t <= 0:
            c2 += 1 - t
            t = 1

print(min(c1, c2))

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 == 1:
        if s <= 0:
            c1 += 1 - s
            s = 1

    else:  
        if s >= 0:
            c1 += 1 + s
            s = -1

c2 = 0
s = 0


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

print(min(c1, c2))

Solution (Python):
from itertools import accumulate

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


def func(a, judge):
    sum_list = list(accumulate(a))
    x = 0
    num = 0

    length = len(sum_list)

    for i in range(length):
        if judge and sum_list[i] + x <= 0:
            num += 1 - (sum_list[i] + x)
            x += 1 - (sum_list[i] + x)

        if (not judge) and sum_list[i] + x >= 0:
            num += 1 + (sum_list[i] + x)
            x -= 1 + (sum_list[i] + x)

        judge = not judge

    return num


ans = min([func(A, True), func(A, False)])

print(ans)

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


    sumeven = 0
    cnteven = 0
    for i in range(n):
        sumeven += A[i]
        if i % 2 == 0:
            if sumeven <= 0:
                cnteven += abs(sumeven) + 1
                sumeven += abs(sumeven) + 1
        else:
            if sumeven >= 0:
                cnteven += (sumeven + 1)
                sumeven -= (sumeven + 1)


    sumodd = 0
    cntodd = 0
    for i in range(n):
        sumodd += A[i]
        if i % 2 == 1:
            if sumodd <= 0:
                cntodd += abs(sumodd) + 1
                sumodd += abs(sumodd) + 1
        else:
            if sumodd >= 0:
                cntodd += (sumodd + 1)
                sumodd -= (sumodd + 1)

    print(min(cnteven, cntodd))


resolve()

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    n=int(input())
    a=list(map(int,input().split()))
    ans=INF
    for f in range(2):
        s=0
        tmp=0
        for x in a:
            s+=x
            if f and s<=0:
                tmp+=1-s
                s=1
            elif not f and s>=0:
                tmp+=s+1
                s=-1
            f=not f
        ans=min(ans,tmp)
    print(ans)
    
    

if __name__ == '__main__':
    main()

Solution (Python):
from itertools import accumulate

n = int(input())
a = [int(i) for i in input().split()]


def n_op(a, p):
    s = accumulate(a)
    x = 0
    n = 0

    for si in s:
        if p and si + x <= 0:
            n += 1 - (si + x)
            x += 1 - (si + x)
        if (not p) and si + x >= 0:
            n += 1 + (si + x)
            x -= 1 + (si + x)
        p = not p

    return n


answer = min([n_op(a, True), n_op(a, False)])
print(answer)

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

    n = NI()
    a = LI()
    ans = INF
    for s in (1,-1):
        acc = 0
        cnt = 0
        x = s
        for i in range(n):
            acc += a[i]
            if acc * x <= 0:
                cnt += abs(x-acc)
                acc = x
            x = -x
        ans = min(ans,cnt)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

    n = NI()
    bit = [0] * (n+1)

    def add(i,x):
        while i <= n:
            bit[i] += x
            i += i & -i

    def query(i):
        x = 0
        while i > 0:
            x += bit[i]
            i -= i & -i
        return x

    for i,a in enumerate(LI()):
        add(i+1,int(a))

    ans = INF

    b = [bit,copy.copy(bit)]
    for s in (1,-1):
        bit = b[(s+1)//2]
        cnt = 0
        x = s
        for i in range(1,n+1):
            z = query(i)
            if z * x <= 0:
                add(i,x-z)
                cnt += abs(x-z)
            x = -x
        ans = min(ans,cnt)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

    n = NI()
    bit = [0] * (n+1)
    A = LI()
    def add(i,x):
        while i <= n:
            bit[i] += x
            i += i & -i

    def query(i):
        x = 0
        while i > 0:
            x += bit[i]
            i -= i & -i
        return x

    for i,a in enumerate(A):
        add(i+1,int(a))

    ans = INF
    bit_c = copy.deepcopy(bit)
    for s in (1,-1):
        bit = copy.deepcopy(bit_c)
        cnt = 0
        x = s
        for i in range(1,n+1):
            z = query(i)
            if z * x <= 0:
                add(i,x-z)
                cnt += abs(x-z)
            x = -x
        ans = min(ans,cnt)
    print(ans)


if __name__ == '__main__':
    main()

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

c1 = 0
sum = 0
for i in range(n):
    sum += a[i]
    if i % 2 == 1:
        if sum <= 0:
            c1 += 1 - sum
            sum = 1
    else:
        if sum >= 0:
            c1 += 1 + sum
            sum = -1

c2 = 0
sum = 0
for i in range(n):
    sum += a[i]
    if i % 2 == 1:
        if sum >= 0:
            c2 += 1 + sum
            sum = -1
    else:
        if sum <= 0:
            c2 += 1 - sum
            sum = 1

print(min(c1, c2))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
count_1 = 0
count_2 = 0
sum_1 = 0
sum_2 = 0

for i in range(len(a)):
    sum_1 += a[i]
    if i % 2 == 1:
        if sum_1 <= 0:
            count_1 += 1 - sum_1
            sum_1 = 1
    elif i % 2 == 0:
        if sum_1 >= 0:
            count_1 += sum_1 + 1
            sum_1 = -1

for i in range(len(a)):
    sum_2 += a[i]
    if i % 2 == 1:
        if sum_2 >= 0:
            count_2 += sum_2 + 1
            sum_2 = -1
    elif i % 2 == 0:
        if sum_2 <= 0:
            count_2 += 1 - sum_2
            sum_2 = 1

print(count_1 if count_1 < count_2 else count_2)

Solution (Python):
n=input()
a=[int(i) for i in input().split()]
def chk(a,t):
    ans=0
    x=0
    for i in a:
        x+=i
        if t==True and x<1:
            ans+=1-x
            x=1
        elif t==False and x>-1:
            ans+=x+1
            x=-1
        t=not t
    return ans
print(min(chk(a,True),chk(a,False)))

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

o=0
e=0

if res_o[0]<=0:
    o+=1-res_o[0]
    res_o[0]=1
if res_e[0]>=0:
    e+=res_e[0]+1
    res_e[0]=-1

for i in range(1,n):
    res_o[i]=res_o[i-1]+a[i]
    res_e[i]=res_e[i-1]+a[i]
    if i%2==0 and res_o[i]<=0:
        o+=(1-res_o[i])
        res_o[i]=1
    elif i%2==1 and res_o[i]>=0:
        o+=(res_o[i]+1)
        res_o[i]=-1
    if i%2==0 and res_e[i]>=0:
        e+=(1+res_e[i])
        res_e[i]=-1
    elif i%2==1 and res_e[i]<=0:
        e+=(1-res_e[i])
        res_e[i]=1
print(min(e,o))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
minnum = float('inf')
for m in range(2):
    ans = 0
    s = [0]*(n+1)
    tmp = a[0] 
    if m == 0:
        if a[0] >= 0:
            ans += a[0]+1
            a[0] = -1 
    else:
        if a[0] <= 0:
            ans += abs(a[0])+1
            a[0] = 1

    for i in range(n):
        s[i+1] = s[i] + a[i]
        if s[i] < 0:
            if s[i+1] != 0 and s[i+1] > 0:
                continue
            else:
                ans += abs(s[i+1])+1
                s[i+1] = 1
        elif s[i] > 0:
            if s[i+1] != 0 and s[i+1] < 0:
                continue
            else:
                ans += abs(s[i+1])+1   
                s[i+1] = -1

    minnum = min(minnum,ans)
    a[0] = tmp

print(minnum)

Solution (Python):
def main():
    from itertools import accumulate

    N = int(input())
    *A, = map(int, input().split())
    *acc, = accumulate(A)

    def count(w: int = 1) -> int:
        
        
        ret = 0
        add_val = 0
        for i, x in enumerate(acc):
            if (i & 1) ^ w:
                b = max(0, 1 - (x + add_val))
                ret += b
                add_val += b

            else:
                b = max(0, (x + add_val) - (-1))
                ret += b
                add_val -= b
        return ret

    ans = min(
        count(w=0),
        count(w=1)
    )

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin
def check(n,s,x):
    cnt=0
    f=0
    for i in range(n):
        if i%2==0:
            if (s[i]+f)*x<=0:
                cnt+=abs(x-s[i]-f)
                f+=x-s[i]-f
        else:
            if (s[i]+f)*x>=0:
                cnt+=abs(-x-s[i]-f)
                f+=-x-s[i]-f
    return cnt

def main():
    
    readline=stdin.readline
    n=int(readline())
    a=list(map(int,readline().split()))

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

    cnt1=check(n,s,1)
    cnt2=check(n,s,-1)
    print(min(cnt1,cnt2))

if __name__=="__main__":
    main()

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))
n = ii()
A = iil()

cum = 0
ope = 0

for i,item in enumerate(A):
    cum += item
    if i%2 == 0 and cum <= 0:
        ope += abs(cum)+1
        cum = 1
    elif i%2 == 1 and cum >= 0:
        ope += cum+1
        cum = -1

tmp = ope

cum = 0
ope = 0

for i,item in enumerate(A):
    cum += item
    if i%2 == 1 and cum <= 0:
        ope += abs(cum)+1
        cum = 1
    elif i%2 == 0 and cum >= 0:
        ope += cum+1
        cum = -1

print(min(tmp,ope))

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = list(map(int, input().split()))

    cnt_odd_plus, cnt_odd_minus = 0, 0
    R_plus, R_minus = [0], [0]
    for i in range(n):
        if i % 2 == 0:
            
            if R_plus[-1] + A[i] <= 0:
                cnt_odd_plus += 1 - (R_plus[-1] + A[i])
                R_plus.append(1)
            else:
                R_plus.append(R_plus[-1] + A[i])

            
            if R_minus[-1] + A[i] >= 0:
                cnt_odd_minus += 1 + (R_minus[-1] + A[i])
                R_minus.append(-1)
            else:
                R_minus.append(R_minus[-1] + A[i])
        else:
            
            if R_plus[-1] + A[i] >= 0:
                cnt_odd_plus += 1 + (R_plus[-1] + A[i])
                R_plus.append(-1)
            else:
                R_plus.append(R_plus[-1] + A[i])

            
            if R_minus[-1] + A[i] <= 0:
                cnt_odd_minus += 1 - (R_minus[-1] + A[i])
                R_minus.append(1)
            else:
                R_minus.append(R_minus[-1] + A[i])

    res = min(cnt_odd_plus, cnt_odd_minus)
    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    n=int(readline())
    a=list(map(int,readline().split()))

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

    cnt1=0
    s1=s[:]
    f1=0
    
    for i in range(n):
        if i%2==0:
            if s1[i]+f1<=0:
                cnt1+=abs(1-s1[i]-f1)
                f1+=1-s1[i]-f1
        else:
            if s1[i]+f1>=0:
                cnt1+=abs(-1-s1[i]-f1)
                f1+=-1-s1[i]-f1
    
    cnt2=0
    s2=s[:]
    f2=0
    
    for i in range(n):
        if i%2==0:
            if s2[i]+f2>=0:
                cnt2+=abs(-1-s2[i]-f2)
                f2+=-1-s2[i]-f2
        else:
            if s2[i]+f2<=0:
                cnt2+=abs(1-s2[i]-f2)
                f2+=1-s2[i]-f2
    print(min(cnt1,cnt2))

if __name__=="__main__":
    main()

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

def sol(S):
    ret = 0
    for a in A[1:]:
        b = a
        if S * (S + b) > 0:
            b = (abs(S) + 1) * (1 if S < 0 else -1)
        if S + b == 0:
            b = b - 1 if S > 0 else b + 1
        ret += abs(b - a)
        S += b
    return ret

if A[0] == 0:
    ans = min(sol(1), sol(-1)) + 1
else:
    ans = min(sol(A[0]), sol(-A[0] // abs(A[0])) + abs(A[0]) + 1)
print(ans)

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

def sol(S):
    ret = 0
    for a in A[1:]:
        b = a
        if S * (S + b) > 0:
            b = (abs(S) + 1) * (1 if S < 0 else -1)
        if S + b == 0:
            b = b - 1 if S > 0 else b + 1
        ret += abs(b - a)
        S += b
    return ret

if A[0] == 0:
    ans = min(sol(1), sol(-1)) + 1
else:
    ans = min(sol(A[0]), sol(-1 if A[0] > 0 else 1) + abs(A[0]) + 1)
print(ans)

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    n=int(readline())
    a=list(map(int,readline().split()))

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

    flag=True
    
    for i in range(n):
        if i%2==0:
            if s[i]<=0:
                flag=False
                break
        else:
            if s[i]>=0:
                flag=False
                break

    if flag:
        print(0)
    else:  
        flag=True
        for i in range(n):
            if i%2==0:
                if s[i]>=0:
                    flag=False
                    break
            else:
                if s[i]<=0:
                    flag=False
                    break
        if flag:
            print(0)
        else:
            cnt1=0
            s1=s[:]
            f1=0
            
            for i in range(n):
                if i%2==0:
                    if s1[i]+f1<=0:
                        cnt1+=abs(1-s1[i]-f1)
                        f1+=1-s1[i]-f1
                else:
                    if s1[i]+f1>=0:
                        cnt1+=abs(-1-s1[i]-f1)
                        f1+=-1-s1[i]-f1
            
            cnt2=0
            s2=s[:]
            f2=0
            
            for i in range(n):
                if i%2==0:
                    if s2[i]+f2>=0:
                        cnt2+=abs(-1-s2[i]-f2)
                        f2+=-1-s2[i]-f2
                else:
                    if s2[i]+f2<=0:
                        cnt2+=abs(1-s2[i]-f2)
                        f2+=1-s2[i]-f2
            print(min(cnt1,cnt2))

if __name__=="__main__":
    main()

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

def sol(S):
    ret = 0
    for a in A[1:]:
        b = a
        if S * (S + b) > 0:
            b = (abs(S) + 1) * (1 if S < 0 else -1)
        if S + b == 0:
            b = b - 1 if S < 0 else b + 1
        ret += abs(b - a)
        S += b
    return ret

if A[0] == 0:
    ans = min(sol(1), sol(-1)) + 1
else:
    ans = min(sol(A[0]), sol(-1 if A[0] > 0 else 1) + abs(A[0]) + 1)
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = int(readline())
a = list(map(int, readline().split()))
if a[0] > 0:
    cnt2 = a[0] + 1
    now2 = -1
    cnt1 = 0
    now1 = a[0]
elif a[0] == 0:
    cnt2 = 1
    now2 = -1
    cnt1 = 1
    now1 = 1
else:
    cnt2 = 0
    now2 = a[0]
    cnt1 = -a[0] + 1
    now1 = 1
for i, aa in enumerate(a[1:]):
    if i % 2 == 0:
        if aa + now2 > 0:
            now2 += aa
        else:
            cnt2 += abs(now2 + aa) + 1
            now2 = 1
        if now1 + aa < 0:
            now1 += aa
        else:
            cnt1 += now1 + aa + 1
            now1 = -1
    else:
        if aa + now1 > 0:
            now1 += aa
        else:
            cnt1 += abs(now1 + aa) + 1
            now1 = 1
        if now2 + aa < 0:
            now2 += aa
        else:
            cnt2 += now2 + aa + 1
            now2 = -1
print(min(cnt1, cnt2))

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
res = max(0, -a[0] + 1)
sm = 1
if a[0] >= 1:
  res = 0
  sm = a[0]

for i in range(1, N):
  if sm > 0:
    res += max(-1, a[i] + sm) + 1
    sm = min(-1, sm + a[i])
  else:
    res += -min(1, sm + a[i]) + 1
    sm = max(1, sm + a[i])
  
res2 = max(0, a[0] + 1)
sm = -1
if a[0] <= -1:
  res2 = 0
  sm = a[0]
for i in range(1, N):
  if sm > 0:
    res2 += max(-1, a[i] + sm) + 1
    sm = min(-1, sm + a[i])
  else:
    res2 += -min(1, sm + a[i]) + 1
    sm = max(1, sm + a[i])
  

print(min(res, res2))

Solution (Python):
import sys
from collections import Counter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n=int(input())
a=lmp()
s=0
ans1=0
ans2=0
for i in range(n):
    if i%2==0:
        s+=a[i]
        if s>=0:
            ans1+=s-(-1)
            s=-1
    else:
        s+=a[i]
        if s<=0:
            ans1+=1-s
            s=1

s=0
for i in range(n):
    if i%2==1:
        s+=a[i]
        if s>=0:
            ans2+=s-(-1)
            s=-1
    else:
        s+=a[i]
        if s<=0:
            ans2+=1-s
            s=1
print(min(ans1,ans2))

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = inpl()
now = 0
res = INF
cnt = 0
for i in range(n):
    now += a[i]
    if i%2 == 0:
        if now <= 0:
            cnt += 1-now
            now = 1
    else:
        if now >= 0:
            cnt += now+1
            now = -1
    
res = min(res,cnt)
now = 0
cnt = 0
for i in range(n):
    now += a[i]
    if i%2:
        if now <= 0:
            cnt += -now + 1
            now = 1
    else:
        if now >= 0:
            cnt += now + 1
            now = -1
    
res = min(res,cnt)
print(res)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

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

ans = 10 ** 18


def calc(flag):
    total = 0
    count = 0
    for a in A:
        if flag:
            if total + a > 0:
                total += a
            else:
                count += 1 - (total + a)
                total = 1
            flag = False
        else:
            if total + a < 0:
                total += a
            else:
                count += (total + a) - (-1)
                total = -1
            flag = True
    return count

print (min(calc(True), calc(False)))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
acc = [a[0]]
for i in range(1, n):
    acc.append(acc[-1] + a[i])

res = 10 ** 100

def f(fir):
    prv_pos = fir > 0
    margin = fir - a[0]
    ret = abs(margin)
    for i in range(1, n):
        cur = margin + acc[i]
        if prv_pos and cur < 0:
            prv_pos = not prv_pos
            continue
        if not prv_pos and cur > 0:
            prv_pos = not prv_pos
            continue
        if prv_pos:
            need = -1
            sa = need - cur
            margin += sa
            ret += abs(sa)
        else:
            need = 1
            sa = need - cur
            margin += sa
            ret += abs(sa)
        prv_pos = not prv_pos
    return ret


res = min(res, f(1))
res = min(res, f(-1))
if a[0] != 0:
    res = min(res, f(a[0]))
print(res)

Solution (Python):
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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

ans1 = 0
acum = 0
for i in range(n):
  pre_plus = i % 2 == 0
  acum += A[i]
  if (pre_plus and acum < 0) or (not pre_plus and acum > 0):pass
  elif not pre_plus:
    ans1 += 1-acum
    acum = 1
  else:
    ans1 += abs(acum-(-1))
    acum = -1
  


ans2 = 0
pre_plus = 1
acum = 0
for i in range(n):
  pre_plus= i % 2 == 1
  acum += A[i]
  if (pre_plus and acum < 0) or (not pre_plus and acum > 0):pass
  elif not pre_plus:
    ans2 += 1-acum
    acum = 1
  else:
    ans2 += abs(acum-(-1))
    acum = -1

print(min(ans1,ans2))

Solution (Python):
import sys
import numpy as np
import copy

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = list(map(int, input().split()))

    
    S = 0
    cnt = 0
    for i in range(N):
        S += A[i]
        if i % 2 == 0:
            
            if S > 0:
                continue
            else:
                cnt += abs(S) + 1
                S += abs(S) + 1
        else:
            
            if S < 0:
                pass
            else:
                cnt += abs(S) + 1
                S -= abs(S) + 1
    ans = cnt

    
    S = 0
    cnt = 0
    for i in range(N):
        S += A[i]
        if i % 2 == 1:
            
            if S > 0:
                continue
            else:
                cnt += abs(S) + 1
                S += abs(S) + 1
        else:
            
            if S < 0:
                continue
            else:
                cnt += abs(S) + 1
                S -= abs(S) + 1

    if ans > cnt:
        ans = cnt
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)


def main():
    N = int(input())
    A = list(map(int, input().split()))
    if A[0] == 0:
        answer = []
        for a in (1, -1):
            cnt = 1
            S = a
            for i in range(1, N):
                s = S + A[i]
                if A[i] == 0:
                    if S > 0:
                        cnt += S + 1
                        S = -1
                    else:
                        cnt += abs(S) + 1
                        S = 1
                else:
                    if s == 0:
                        if S < 0:
                            cnt += 1
                            S = 1
                        else:
                            cnt += 1
                            S = -1
                    else:
                        if S * s > 0:
                            if S < 0:
                                cnt += abs(s) + 1
                                S = 1
                            else:
                                cnt += s + 1
                                S = -1
                        else:
                            S = s
            answer.append(cnt)
        print(min(answer))
    else:
        if A[0] > 0:
            cnt1 = 0
            S1 = A[0]
            cnt2 = A[0] + 1
            S2 = -1
        else:
            cnt1 = 0
            S1 = A[0]
            cnt2 = abs(A[0]) + 1
            S2 = 1
        answer = []
        for cnt, S in [(cnt1, S1), (cnt2, S2)]:
            for i in range(1, N):
                s = S + A[i]
                if A[i] == 0:
                    if S > 0:
                        cnt += S + 1
                        S = -1
                    else:
                        cnt += abs(S) + 1
                        S = 1
                else:
                    if s == 0:
                        if S < 0:
                            cnt += 1
                            S = 1
                        else:
                            cnt += 1
                            S = -1
                    else:
                        if S * s > 0:
                            if S < 0:
                                cnt += abs(s) + 1
                                S = 1
                            else:
                                cnt += s + 1
                                S = -1
                        else:
                            S = s
            answer.append(cnt)
        print(min(answer))


if __name__ == "__main__":
    main()

Solution (Python):
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()))
a_sum_pn = 0
a_sum_np = 0
ans_pn = 0
ans_np = 0
flag = 1
for i in range(n):
  a_sum_pn += a[i]
  a_sum_np += a[i]
  if sign(a_sum_pn) != flag:
    ans_pn += abs(flag-a_sum_pn)
    a_sum_pn = flag
  if sign(a_sum_np) != -flag:
    ans_np += abs(-flag-a_sum_np)
    a_sum_np = -flag
  flag *= -1
print(min(ans_pn,ans_np))

Solution (Python):
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    n = int(_in[0])  
    a_arr = list(map(int,_in[1].split(' ')))  
    
    cnt0 = 0
    sum_ = 0
    for i in range(0,n):
        if i%2 == 0:
            if sum_+a_arr[i]<=0:
                cnt0 += 1-(sum_+a_arr[i])
                sum_ = 1
            else:
                sum_ = sum_+a_arr[i]
        else:
            if sum_+a_arr[i]>=0:
                cnt0 += 1+(sum_+a_arr[i])
                sum_ = -1
            else:
                sum_ = sum_+a_arr[i]
    cnt1 = 0
    sum_ = 0
    for i in range(0,n):
        if i%2 == 1:
            if sum_+a_arr[i]<=0:
                cnt1 += 1-(sum_+a_arr[i])
                sum_ = 1
            else:
                sum_ = sum_+a_arr[i]
        else:
            if sum_+a_arr[i]>=0:
                cnt1 += 1+(sum_+a_arr[i])
                sum_ = -1
            else:
                sum_ = sum_+a_arr[i]
    cnt = min(cnt0,cnt1)
    
    print(cnt)

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

cnt1=0
asum=0

for i in range(n):
  asum+=alist[i]
  if i%2==0:
    if asum<=0:
      cnt1+=1-asum
      asum+=1-asum
  else:
    if asum>=0:
      cnt1+=asum+1
      asum-=asum+1
      
cnt2=0
asum=0

for i in range(n):
  asum+=alist[i]
  if i%2==0:
    if asum>=0:
      cnt2+=asum+1
      asum-=asum+1
  else:
    if asum<=0:
      cnt2+=1-asum
      asum+=1-asum


print(min(cnt1,cnt2))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, *a = map(int, read().split())
ans = float('inf')
for i in (-1, 1):
    cnt, now = 0, 0
    for aa in a:
        now += aa
        if now * i <= 0:
            cnt += abs(now - i)
            now = i
        i *= -1
    ans = min(ans, cnt)
print(ans)

Solution (Python):
import numpy as np

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


cost1 = 0
cum = 0
for i in range(n):
    cum += arr[i]
    tmp = abs(cum) + 1
    if i%2==1 and cum<=0: cost1 += tmp ; cum += tmp
    if i%2==0 and cum>=0: cost1 += tmp ; cum -= tmp


cost2 = 0
cum = 0
for i in range(n):
    cum += arr[i]
    tmp = abs(cum) + 1
    if i%2==1 and cum>=0: cost2 += tmp ; cum -= tmp
    if i%2==0 and cum<=0: cost2 += tmp ; cum += tmp


ans = min(cost1, cost2)
print(ans)