Problem p03739 (python)

Fixed submission:
def solve():

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

    b = [a[i] for i in range(n)]

   # print(a)
   # print(b)

    #Even
    sum_e = 0
    ans_e = 0

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

    #Odd
    sum_o = 0
    ans_o = 0

    for i in range(n):
        sum_o += b[i]
        if i%2 == 1:
            if sum_o <= 0:
                ans_o += abs(sum_o) + 1
                sum_o += abs(sum_o) + 1
        else:
            if sum_o >= 0:
                ans_o += abs(sum_o) + 1
                sum_o -= abs(sum_o) + 1

    if ans_o <= ans_e:
        ans = ans_o
    else:
        ans = ans_e


    print(ans)

if __name__ ==  "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
def solve():

    n = int(input())
    a = list(map(int, input().split()))
    b = [a[i] for i in range(n)]

   # print(a)
   # print(b)

    i = 0
    sum = 0

    ans = 0

    for i in range(n-1):
        sum += a[i]
        if sum > 0 and sum+a[i+1] > 0:
            tmp = -1 - sum
            ans += abs(tmp - a[i+1])
            a[i+1] = tmp
        elif sum < 0 and sum+a[i+1] < 0:
            tmp = 1 - sum
            ans += abs(tmp - a[i+1])
            a[i+1] = tmp

   # print(ans)
   # print(a)

    tmp_ans_1 = ans

    ans = 0
    sum = 0
    i = 0

    if b[0] > 0:
        ans += abs(-1 - b[0])
        b[0] = -1
    elif b[0] < 0:
        ans += abs(1 - b[0])
        b[0] = 1
    for i in range(n-1):
        sum += b[i]
        if sum > 0 and sum+b[i+1] > 0:
            tmp = -1 - sum
            ans += abs(tmp - b[i+1])
            b[i+1] = tmp
        elif sum < 0 and sum+b[i+1] < 0:
            tmp = 1 - sum
            ans += abs(tmp - b[i+1])
            b[i+1] = tmp

   # print(ans)
   # print(b)           
    
    tmp_ans_2 = ans
    
    if tmp_ans_1 <= tmp_ans_2:
        ans = tmp_ans_1
    elif tmp_ans_2 < tnp_ans_1:
        ans = tmp_ans_2

    print(ans)

if __name__ ==  "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
def solve():

    n = int(input())
    a = list(map(int, input().split()))
    b = [a[i] for i in range(n)]

   # print(a)
   # print(b)

    i = 0
    sum = 0

    ans = 0

    for i in range(n-1):
        sum += a[i]
        if sum > 0 and sum+a[i+1] > 0:
            tmp = -1 - sum
            ans += abs(tmp - a[i+1])
            a[i+1] = tmp
        elif sum < 0 and sum+a[i+1] < 0:
            tmp = 1 - sum
            ans += abs(tmp - a[i+1])
            a[i+1] = tmp

   # print(ans)
   # print(a)

    tmp_ans_1 = ans

    ans = 0
    sum = 0
    i = 0

    if b[0] > 0:
        ans += abs(-1 - b[0])
        b[0] = -1
    elif b[0] < 0:
        ans += abs(1 - b[0])
        b[0] = 1
    for i in range(n-1):
        sum += b[i]
        if sum > 0 and sum+b[i+1] > 0:
            tmp = -1 - sum
            ans += abs(tmp - b[i+1])
            b[i+1] = tmp
        elif sum < 0 and sum+b[i+1] < 0:
            tmp = 1 - sum
            ans += abs(tmp - b[i+1])
            b[i+1] = tmp

   # print(ans)
   # print(b)           
    
    tmp_ans_2 = ans
    
    if tmp_ans_1 <= tmp_ans_2:
        ans = tmp_ans_1
    elif tmp_ans_2 < tnp_ans_1:
        ans = tmp_ans_2

    print(ans)

if __name__ ==  "__main__":
    solve()