Problem p03739 (python)

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

ans = 0
f = 1
num = 0
for i in range(n):
    num += a[i]
    if f:
        if num <= 0:
            ans += 1 - num
            num = 1
        f = 0
    else:
        if num >= 0:
            ans += abs(-1 - num)
            num = -1
        f = 1

ans_2 = 0
f = 1
num = 0
for i in range(n):
    num += a[i]
    if f:
        if num >= 0:
            ans_2 += abs(-1 - num)
            num = -1
        f = 0
    else:
        if num <= 0:
            ans_2 += 1 - num
            num = 1
        f = 1

print(min(ans,ans_2))

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

ans = 0
ans_2 = 0
o = 0
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "+"
        elif a[0] < 0:
            f = "-"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
o = 0
a = a_1
#print(a)
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "-"
            ans_2 += abs(-1 - a[0])
            a[i] = -1
        elif a[0] < 0:
            ans_2 += abs(1 - a[0])
            a[i] = 1
            f = "+"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans_2 += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans_2 += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans_2 += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans_2 += 1
                f = "+"
#print(a)
print(min(ans,ans_2))

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

ans = 0
ans_2 = 0
o = 0
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "+"
        elif a[0] < 0:
            f = "-"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
o = 0
a = a_1
#print(a)
for i in range(n):
    if i == 0:
        if a[i] == 0:
            f = "+"
            a[i] = 1
        elif a[0] > 0:
            f = "-"
            ans_2 += abs(-1 - a[0])
            a[i] = -1
        elif a[0] < 0:
            ans_2 += abs(1 - a[0])
            a[i] = 1
            f = "+"
    else:
        o += a[i-1]
        if f == "+":
            if a[i] + o > 0:
                c = -1 - o
                ans_2 += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + o == 0:
                    a[i] -= 1
                    ans_2 += 1
                f = "-"
        elif f == "-":
            if a[i] + o < 0:
                c = 1 - o
                ans_2 += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + o == 0:
                    a[i] += 1
                    ans_2 += 1
                f = "+"
#print(a)
print(min(ans,ans_2))

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

ans = 0
for i in range(n):
    if i == 0:
        if a[0] > 0:
            f = "+"
        elif a[0] < 0:
            f = "-"
    else:
        if f == "+":
            if a[i] + sum(a[:i]) > 0:
                c = -1 - sum(a[:i])
                ans += abs(c - a[i])
                a[i] = c
                f = "-"
            else:
                if a[i] + sum(a[:i]) == 0:
                    a[i] -= 1
                    ans += 1
                f = "-"
        elif f == "-":
            if a[i] + sum(a[:i]) < 0:
                c = 1 - sum(a[:i])
                ans += abs(c - a[i])
                a[i] = c
                f = "+"
            else:
                if a[i] + sum(a[:i]) == 0:
                    a[i] += 1
                    ans += 1
                f = "+"
#print(a)
print(ans)