Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))

ttl = a[0]
cst = 0

if a[0]>0:
    flg = 1

elif a[0]<0:
    flg = -1

for i in range(1,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
   


ttl = a[0]
cst2 = 0


   

print(min(cst,cst2))



Fixed submission:
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 = 0
cst = 0



cst = wh(cst,ttl,1)
ttl = 0
cst2 = 0
cst2 = wh(cst2,ttl,-1)
   

print(min(cst,cst2))

Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))

ttl = a[0]
cst = 0

if a[0]>0:
    flg = 1

elif a[0]<0:
    flg = -1

for i in range(1,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
   


ttl = a[0]
cst2 = 0

if a[0]>0:
    flg = -1
    cst2 += abs(ttl)+1
    ttl += 0-ttl-1 

elif a[0]<0:
    flg = 1
    cst2 += abs(ttl)+1
    ttl += 0-ttl+1 

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

    if ttl*flg < 0:
        flg *= -1
    else:
        if flg > 0:
            memo = abs(ttl)+1
            ttl -= memo
            cst2 += memo
        elif flg < 0:
            memo = abs(ttl)+1
            ttl += memo
            cst2 += memo
        flg *= -1
   

print(min(cst,cst2))



Fixed submission:
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 = 0
cst = 0



cst = wh(cst,ttl,1)
ttl = 0
cst2 = 0
cst2 = wh(cst2,ttl,-1)
   

print(min(cst,cst2))

Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))

ttl = a[0]
cst = 0

if a[0]>0:
    flg = 1

elif a[0]<0:
    flg = -1

for i in range(1,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
    


ttl = a[0]
cst2 = 0

if a[0]>0:
    flg = -1
    cst2 += abs(ttl)+1
    ttl += 0-ttl-1 

elif a[0]<0:
    flg = 1
    cst2 += abs(ttl)+1
    ttl += 0-ttl+1 

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

    if ttl*flg < 0:
        flg *= -1
    else:
        if flg > 0:
            memo = abs(ttl)+1
            ttl -= memo
            cst2 += memo
        elif flg < 0:
            memo = abs(ttl)+1
            ttl += memo
            cst2 += memo
        flg *= -1
    print(cst2," ",ttl)





Fixed submission:
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 = 0
cst = 0



cst = wh(cst,ttl,1)
ttl = 0
cst2 = 0
cst2 = wh(cst2,ttl,-1)
   

print(min(cst,cst2))