Problem p03739 (python)

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

if A[0] != 0:
    ans1 = 0
    sum = A[0]
else:
    ans1 = 1
    sum = 1
# A[0]=0 のときは1にする。下で-1にするパターンも試している。なお0は不可。

for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        nextsum = 1 if sum < 0 else -1
        # fugo = sum // abs(sum) 割り算のときは0に気をつけろ！
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        sum = nextsum
        ans1 += dif

#A[0]を反転したほうがいいパターンの処理
ans2 = abs(A[0]) + 1
sum = 1 if A[0] < 0 else -1
#ある意味、ここでA[0]=0のときに-1にすることもやっている

for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        nextsum = 1 if sum < 0 else -1
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        sum = nextsum
        ans2 += dif
        
print(min(ans1, ans2))

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

ans1 = 0
sum = A[0]
for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        fugo = sum // abs(sum)
        nextsum = - fugo
        #print('fugo={}, nextsum={}'.format(fugo, nextsum))
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        #print('a_shoule_be={}, dif={}'.format(a_should_be, dif))
        sum = nextsum
        ans1 += dif

#print(ans1)

#A[0]を反転したほうがいいパターンの処理
ans2 = abs(A[0]) + 1
sum = - (A[0] // abs(A[0]))

for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        fugo = sum // abs(sum)
        nextsum = - fugo
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        sum = nextsum
        ans2 += dif
        
#print(ans2) 
print(min(ans1, ans2))

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

ans1 = 0
sum = A[0]
for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        fugo = sum // abs(sum)
        nextsum = - fugo
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        sum += a_should_be
        ans1 += dif

#print(ans1)

#A[0]を反転したほうがいいパターンの処理
ans2 = A[0] + 1
sum = - (A[0] // abs(A[0]))

for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        fugo = sum // abs(sum)
        nextsum = - fugo
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        sum += a_should_be
        ans2 += dif
        
#print(ans2) 
print(min(ans1, ans2))

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

ans1 = 0
sum = A[0]
for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        fugo = sum // abs(sum)
        nextsum = - fugo
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        sum += a_should_be
        ans1 += dif

print(ans1)

ans2 = A[0] + 1
sum = - (A[0] // abs(A[0]))

for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        fugo = sum // abs(sum)
        nextsum = - fugo
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        sum += a_should_be
        ans2 += dif
        
print(ans2) 
#A[0]を反転したほうがいいパターンの処理

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

ans = 0
sum = A[0]
for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        fugo = sum // abs(sum)
        nextsum = - fugo
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        sum += a_should_be
        ans += dif

print(ans)