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



Sample input:
4
1 -3 1 0


Sample output:
4

Submission status: Wrong Answer

Solution code:
"""ABC059 C Sequence diff:
"""

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

cum = [0]*N
cum[0] = A[0]
for i in range(1, N):
    cum[i] = cum[i-1] + A[i]
cumcop = cum.copy()
# + - + - ...
start = N
for i in range(N):
    if i % 2 == 0:
        if cum[i] <= 0:
            start = i
            break
    else:
        if cum[i] >= 0:
            start = i
            break

if start == N:
    print(0)
    exit()

ans1 = 0
for i in range(start, N):
    cum[i] = cum[i-1] + A[i]
    if i % 2 == 0:
        if cum[i] <= 0:
            ans1 += abs(cum[i]) + 1
            A[i] += abs(cum[i]) + 1
            cum[i] = 1
    else:
        if cum[i] >= 0:
            ans1 += abs(cum[i]) + 1
            A[i] -= abs(cum[i]) + 1
            cum[i] = -1
    # cum[i] = cum[i-1] + A[i]

# - + - + 
A = Acop.copy()
cum = cumcop.copy()
start = N
for i in range(N):
    if i % 2 == 1:
        if cum[i] <= 0:
            start = i
            break
    else:
        if cum[i] >= 0:
            start = i
            break

if start == N:
    print(0)
    exit()

ans2 = 0
for i in range(start, N):
    cum[i] = cum[i-1] + A[i]
    if i % 2 == 1:
        if cum[i] <= 0:
            ans2 += abs(cum[i]) + 1
            A[i] += abs(cum[i]) + 1
            cum[i] = 1
    else:
        if cum[i] >= 0:
            ans2 += abs(cum[i]) + 1
            A[i] -= abs(cum[i]) + 1
            cum[i] = -1
    # cum[i] = cum[i-1] + A[i]
print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
Acop = A.copy()

cum = [0]*N
cum[0] = A[0]
for i in range(1, N):
    cum[i] = cum[i-1] + A[i]
cumcop = cum.copy()
# + - + - ...
start = N
for i in range(N):
    if i % 2 == 0:
        if cum[i] <= 0:
            start = i
            break
    else:
        if cum[i] >= 0:
            start = i
            break

if start == N:
    print(0)
    exit()

ans1 = 0
for i in range(start, N):
    cum[i] = cum[i-1] + A[i]
    if i % 2 == 0:
        if cum[i] <= 0:
            ans1 += abs(cum[i]) + 1
            A[i] += abs(cum[i]) + 1
            cum[i] = 1
    else:
        if cum[i] >= 0:
            ans1 += abs(cum[i]) + 1
            A[i] -= abs(cum[i]) + 1
            cum[i] = -1
    # cum[i] = cum[i-1] + A[i]

# - + - + 
A = Acop
cum = cumcop
start = N
for i in range(N):
    if i % 2 == 1:
        if cum[i] <= 0:
            start = i
            break
    else:
        if cum[i] >= 0:
            start = i
            break

if start == N:
    print(0)
    exit()

ans2 = 0
for i in range(start, N):
    cum[i] = cum[i-1] + A[i]
    if i % 2 == 1:
        if cum[i] <= 0:
            ans2 += abs(cum[i]) + 1
            A[i] += abs(cum[i]) + 1
            cum[i] = 1
    else:
        if cum[i] >= 0:
            ans2 += abs(cum[i]) + 1
            A[i] -= abs(cum[i]) + 1
            cum[i] = -1
    # cum[i] = cum[i-1] + A[i]
print(min(ans1, ans2))

Submission status: Wrong Answer

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

#初期化
dp = [[0,0]]*N
if A[0] == 0:
  for i in range(1,N):
    if A[i] > 0:
      dp[i-1][0] = 2*i-1
      dp[i-1][1] = -1
      break
    elif A[i] < 0:
      dp[i-1][0] = 2*i-1
      dp[i-1][1] = 1
      break
    else:
      pass
  else:
    dp[N-1][0] = 2*N-1
else:
  dp[0][1] = A[0]

#dp    
for j in range(N-1):
  S_j = dp[j][1]
  if (S_j > 0 and A[j+1] < -S_j) or (S_j < 0 and A[j+1] > -S_j):
    dp[j+1][0] = dp[j][0]
    dp[j+1][1] = dp[j][1]+A[j+1]
  elif S_j > 0:
    dp[j+1][0] = dp[j][0]+S_j+A[j+1]+1
    dp[j+1][1] = -1
  elif S_j < 0:
    dp[j+1][0] = dp[j][0]-S_j-A[j+1]+1
    dp[j+1][1] = -1

print(dp[N-1][0])

Submission status: Wrong Answer

Solution code:
def f(c, cost):
    for i in a[1:]:
        if c > 0:
            if c + i >= 0:
                cost += c + i + 1
                c = -1
            else:
                c += i
        else:
            if c + i <= 0:
                cost += 1 - c - i
                c = 1
            else:
                c += i
    return c, cost


n = int(input())
a = list(map(int, input().split()))
cost = (a[0] == 0)
c = a[0] + (a[0] == 0)
c, cost = f(c, cost)
tmp = cost
cost = 0
if a[0] >= 0:
    cost = a[0] + 1
    c = -1
else:
    cost = -a[0] + 1
    c = 1
c, cost = f(c, cost)
print(min(cost, tmp))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(i) for i in input().split(" ")]
(o,s) = (0,0)
for i in range(n):
    k=a[i]
    if (s+a[i]==0): a[i]= -s+1 if (s<0) else 1+s
    elif ((s<0 and s+a[i]<0) or (s>0 and s+a[i]>0)): a[i]=-s+1 if (s+a[i]<0) else -s-1
    o+=abs(k-a[i])
    s+=a[i]
print(o)

Submission status: Wrong Answer

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

def Cost(a, Sum):
    if Sum > 0:
        if Sum + a < 0:
            return 0
        else:
            return abs(Sum + a) + 1
    if Sum < 0:
        if Sum + a > 0:
            return 0
        else:
            return abs(Sum + a) + 1

Sum = lambda a, preSum:preSum + a if Cost(a, preSum) == 0 else (-1)**int(preSum > 0)

pCost = Cost(A[0],-1) -1
pSum = (lambda:A[0] if A[0] > 0 else 1)()
nCost = Cost(A[0],1) -1
nSum = (lambda:A[0] if A[0] < 0 else -1)()
for a in A[1:]:
    pCost += Cost(a, pSum)
    pSum = Sum(a, pSum)
    nCost += Cost(a, nSum)
    nSum = Sum(a, nSum)
print(min(pCost, nCost))

Submission status: Wrong Answer

Solution code:
# vim: fileencoding=utf-8


def calc(a, flg):
    ans = 0
    cursol = a[0]
    if flg == 1:
        if cursol <= 0:
            cursol = 1
            ans = abs(cursol) + 1
    elif flg == -1:
        if cursol >= 0:
            cursol = -1
            ans = abs(cursol) + 1
    for i in a[1:]:
        t = cursol + i
        if cursol > 0:
            if t >= 0:
                ans += t + 1
                cursol = -1
            else:
                cursol = t
        elif cursol < 0:
            if t <= 0:
                ans += abs(t) + 1
                cursol = 1
            else:
                cursol = t
    return ans


def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = min(calc(a, 1), calc(a, -1))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# vim: fileencoding=utf-8


def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    p = 0
    q = 0
    flg = 0
    if a[0] == 0:
        if a[1] > 0:
            flg = -1
        else:
            flg = 1
        ans = 1
    elif a[0] > 0:
        p = a[0]
        flg = 1
    else:
        q = abs(a[0])
        flg = -1

    for i in a[1:]:
        # print(i, p, q, ans)
        if i > 0:
            p += i
        elif i < 0:
            q += abs(i)
        if flg == 1:
            if p >= q:
                t = p - q + 1
                ans += t
                q += t
            flg = -1
        elif flg == -1:
            if q >= p:
                t = q - p + 1
                ans += t
                p += t
            flg = 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# vim: fileencoding=utf-8


def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    p = 0
    q = 0
    flg = 0
    if a[0] > 0:
        p = a[0]
        flg = 1
    else:
        q = abs(a[0])
        flg = -1

    for i in a[1:]:
        # print(i, p, q, ans)
        if i > 0:
            p += i
        elif i < 0:
            q += abs(i)
        if flg == 1:
            if p >= q:
                t = p - q + 1
                ans += t
                q += t
            flg = -1
        elif flg == -1:
            if q >= p:
                t = q - p + 1
                ans += t
                p += t
            flg = 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# vim: fileencoding=utf-8


def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    cursol = a[0]
    if cursol == 0:
        if a[1] >= 0:
            cursol = -1
        else:
            cursol = 1
        ans += 1

    for i in a[1:]:
        t = cursol + i
        if cursol > 0:
            if t >= 0:
                ans += t + 1
                cursol = -1
            else:
                cursol = t
        elif cursol < 0:
            if t <= 0:
                ans += abs(t) + 1
                cursol = 1
            else:
                cursol = t
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# vim: fileencoding=utf-8


def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    cursol = a[0]
    for i in a[1:]:
        t = cursol + i
        if cursol > 0:
            if t >= 0:
                ans += t + 1
                cursol = -1
            else:
                cursol = t
        elif cursol < 0:
            if t <= 0:
                ans += abs(t) + 1
                cursol = 1
            else:
                cursol = t
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

plus_ans = 0
minus_ans = 0

for i in range(len(A)):
    if i == 0:
        if A[0] <= 0:
            plus_ans += A[0] + 1
            sum = 1
        else:
            sum = A[0]
    else:
        if sum > 0:
            if sum + A[i] >= 0:
                plus_ans += abs(sum + A[i]) + 1
                sum = -1
            else:
                sum += A[i]
        elif sum < 0:
            if sum + A[i] <= 0:
                plus_ans += abs(sum + A[i]) + 1
                sum = 1
            else:
                sum += A[i]

for i in range(len(A)):
    if i == 0:
        if A[0] >= 0:
            minus_ans += A[0] + 1
            sum = -1
        else:
            sum = A[0]
    else:
        if sum > 0:
            if sum + A[i] >= 0:
                minus_ans += abs(sum + A[i]) + 1
                sum = -1
            else:
                sum += A[i]
        elif sum < 0:
            if sum + A[i] <= 0:
                minus_ans += abs(sum + A[i]) + 1
                sum = 1
            else:
                sum += A[i]

print(min(plus_ans, minus_ans))

Submission status: Wrong Answer

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

sum = A[0]
ans = 0

if sum == 0:
    if A[1] >= 0:
        sum = -1
    elif A[1] < 0:
        sum = 1
    ans += 1

for i in range(1, len(A)):
    if sum > 0:
        if sum + A[i] >= 0:
            ans += abs(sum + A[i]) + 1
            sum = -1
        else:
            sum += A[i]
    elif sum < 0:
        if sum + A[i] <= 0:
            ans += abs(sum + A[i]) + 1
            sum = 1
        else:
            sum += A[i]
print(ans)

Submission status: Wrong Answer

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

sum = A[0]
ans = 0

if sum == 0:
    ans += 1
    sum = 1

for i in range(1, len(A)):
    if sum > 0:
        if sum + A[i] >= 0:
            ans += abs(sum + A[i]) + 1
            sum = -1
        else:
            sum += A[i]
    elif sum < 0:
        if sum + A[i] <= 0:
            ans += abs(sum + A[i]) + 1
            sum = 1
        else:
            sum += A[i]
print(ans)

Submission status: Wrong Answer

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

sum = A[0]
ans = 0
if sum == 0:
    sum += 1
    ans += 1
for i in range(1, len(A)):
    if sum > 0:
        if sum + A[i] >= 0:
            ans += abs(sum + A[i]) + 1
            sum = -1
        else:
            sum += A[i]
            continue
    elif sum < 0:
        if sum + A[i] <= 0:
            ans += abs(sum + A[i]) + 1
            sum = 1
        else:
            sum += A[i]
            continue
print(ans)

Submission status: Wrong Answer

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

x = a[0]

ans = float('inf')
for _ in range(2):
    tmp = 0
    if _:
        if a[0] > 0:
            x = a[0]
        else:
            x = 1 - a[0]
            tmp = 1 - a[0]
    else:
        if a[0] < 0:
            x = a[0]
        else:
            x = -1 - a[0]
            tmp = 1 + a[0]
    
    for i in range(1, n):
        y = x + a[i]
        if x*y >= 0:
            if x > 0:
                tmp += y+1
                y -= y+1
            elif x < 0:
                tmp -= y-1
                y -= y-1
        x = y
    ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
al = list(map(int, input().split()))


def is_diff_sign(s, t):
    return (s > 0 > t) or (s < 0 < t)


count = 0
prev_sum = al[0]
prev_positive = al[0] > 0
for a in al[1:]:
    _sum = prev_sum + a
    if is_diff_sign(prev_sum, _sum):
        prev_sum = _sum
        prev_positive = prev_sum > 0
        continue
    if prev_positive:
        prev_sum = -1
    else:
        prev_sum = 1
    prev_positive = not prev_positive
    count += abs(_sum) + 1

print(count)

Submission status: Wrong Answer

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

x = a[0]
ans = 0
for i in range(1, n):
    y = x + a[i]
    # print('xy', x, y)
    if x*y >= 0:
        if x > 0:
            ans += y+1
            y -= y+1
        elif x < 0:
            ans -= y-1
            y -= y-1
    x = y
print(ans)

Submission status: Wrong Answer

Solution code:
import copy

n = int(input())
a = list(map(int, input().split()))
b = copy.copy(a)
c = copy.copy(a)
if a[0] == 0:
    b[0] = 1
    c[0] = -1
# print(a,b,c)
def sequence(x):
    if x[0] == 0:
        return(-1)
    else:
        cnt = 0
        sum = x[0]
        s = [x[0]]
        for i in range(1,n):
            sum = sum + x[i]
            if s[i-1]*sum < 0:
                s.append(sum)
                flag = 3
            else:
                if s[i-1] > 0:
                    cnt = cnt + (sum + 1)
                    sum = -1
                    s.append(sum)
                    flag = 4
                elif s[i-1] < 0:
                    cnt = cnt + (-1)*(sum - 1)
                    sum = 1
                    s.append(sum)
                    flag = 5
        return(cnt)
A = sequence(a)
B = sequence(b)+1
C = sequence(c)+1
# print(A,B,C)
D = min([i for i in (A,B,C) if i >= 0])
print(D)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
acc = [0]*n
acc[0] = a[0]
def solve():
  for i in range(1,n):
    acc[i] = a[i]+acc[i-1]
  oddans = 0
  odddiff = 0
  #simulation in case of odd is positive
  for i in range(n):
    if i%2 == 0 and (acc[i]+odddiff)<0:
      oddans += abs(acc[i]+odddiff)+1
      odddiff += (abs(acc[i]+odddiff))+1
    elif i%2 != 0 and (acc[i]+odddiff)>0:
      oddans += acc[i]+odddiff+1
      odddiff -= acc[i]+odddiff+1
  #neg
  evenans = 0
  evendiff = 0
  for i in range(n):
    if i%2 == 0 and (acc[i]+evendiff)>=0:
      evenans += (acc[i]+evendiff+1) 
      evendiff -= (acc[i]+evendiff+1)
    elif i%2 != 0 and (acc[i]+evendiff)<=0:
      evenans += (abs(acc[i]+evendiff)+1)
      evendiff += (abs(acc[i]+evendiff)+1)
  return min(evenans,oddans)
  
print(solve())

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
tot = 0
cnt = 0
for i in range(N):
    tot += A[i]
    if i%2==0:
        if tot<0:
            cnt += abs(tot)+1
            tot = 1
    if i%2==1:
        if tot>0:
            cnt += tot+1
            tot = -1
cmin = cnt
cnt = 0
tot = 0
for i in range(N):
    tot += A[i]
    if i%2==0:
        if tot>0:
            cnt += tot+1
            tot = -1
    else:
        if tot<0:
            cnt += abs(tot)+1
            tot = 1
cmin = min(cmin,cnt)
print(cmin)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
s = a[0]
ans = 0
for e in a[1:]:
    if s == 0:
        s = 1
        ans += 1
    if (s+e)*s >= 0:
        if s+e < 0:
            ans += 1-(s+e)
            s = 1
        else:
            ans += 1+(s+e)
            s = -1
    else:
        s += e

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
s = a[0]
ans = 0
for e in a[1:]:
    if (s+e)*s >= 0:
        if s+e < 0:
            ans += 1-(s+e)
            s = 1
        else:
            ans += 1+(s+e)
            s = -1
    else:
        s += e
print(ans)

Submission status: Wrong Answer

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

cnt = 0
flag = 0
for i in range(1,n):
    if a[0] != 0:
        flag = 1
        break
    elif a[0] == 0 and a[i] != 0:
        sign = int(a[i]/a[i])
        a[0] = ((-1)**i)*sign
        flag = 2
        break
    else:
        continue
if flag == 0:
    a[0] = 1
if flag != 1:
    cnt = 1

sum = a[0]
s = [a[0]]
for k in range(1,n):
    sum = sum + a[k]
    if s[k-1]*sum < 0:
        s.append(sum)
        flag = 3
    else:
        if s[k-1] > 0:
            cnt = cnt + (sum + 1)
            sum = -1
            s.append(sum)
            flag = 4
        elif s[k-1] < 0:
            cnt = cnt + (-1)*(sum - 1)
            sum = 1
            s.append(sum)
            flag = 5
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
sum = a[0]
s = [a[0]]
cnt = 0
for i in range(1,n):
    sum = sum + a[i]
    if sum*s[i-1] < 0:
        s.append(sum)
    else:
        if sum > 0:
            cnt = cnt + (sum+1)
            sum = -1
        elif sum < 0:
            cnt = cnt + (-1)*(sum-1)
            sum = 1
        elif sum == 0:
            if s[i-1]<0:
                cnt = cnt + 1
                sum = 1
            elif s[i-1]>0:
                cnt = cnt + 1
                sum = -1
        s.append(sum)
print(cnt)

Submission status: Wrong Answer

Solution code:
n=input()
a=list(map(int,input().split()))
ans=10**18
for s in -a[1]+1,a[0],-a[1]-1:
  c=abs(a[0]-s)
  for i in a[1:]:
    if s<0:
      b=-s+1
      c+=max(0,b-i)
      s+=max(i,b)
    else:
      b=-s-1
      c+=max(0,i-b)
      s+=min(i,b)
  ans=min(ans,c)
print(ans)

Submission status: Wrong Answer

Solution code:
n=input()
a=list(map(int,input().split()))
s=a[0]
c=0
for i in a[1:]:
  if s<0:
    b=-s+1
    c+=max(0,b-i)
    s+=max(i,b)
  else:
    b=-s-1
    c+=max(0,i-b)
    s+=min(i,b)
ans=c
s=a[-1]
c=0
for i in a[::-1][1:]:
  if s<0:
    b=-s+1
    c+=max(0,b-i)
    s+=max(i,b)
  else:
    b=-s-1
    c+=max(0,i-b)
    s+=min(i,b)
print(min(ans,c))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = A[:]

# evenがプラスの時
sum_even = 0
ans_even = 0

for i in range(N):
  sum_even += A[i]
  if i % 2 == 0:
    if sum_even <= 0:
      ans_even += abs(sum_even) + 1
      sum_even += abs(sum_even) + 1
  else:
    if sum_even >= 0:
      ans_even += abs(sum_even) + 1
      sum_even += abs(sum_even) + 1

# oddがプラスの時
sum_odd = 0
ans_odd = 0
for i in range(N):
  sum_odd += A[i]
  if i % 2 == 1:
    if sum_odd <= 0:
      ans_odd += abs(sum_odd) + 1
      sum_odd += abs(sum_odd) + 1
  else:
    if sum_odd >= 0:
      ans_odd += abs(sum_odd) + 1
      sum_odd += abs(sum_odd) + 1

answer = min(ans_even, ans_odd)
print(str(answer))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8


def run():
    n, *A = map(int, read().split())
    v = 0
    acum = []
    for a in A:
        v += a
        acum.append(v)
    # greedy
    cums = 0
    count = 0
    if not A[0]:
        V = 1
    else:
        V = A[0] // abs(A[0])

    for a in acum[1:]:
        #print(a, '---------')
        V *= -1
        if (a + cums) * V > 0:
            continue
        else:
            update = abs(a + cums) + 1
            cums += (update) * V
            count += update
            #print(V, cums, count)
    print(count)



if __name__ == "__main__":
    run()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
now_s = 0
ans = 0
for i in range(n):
    pre_s = now_s
    now_s = pre_s + a[i]
    if pre_s < 0:
        if now_s <= 0:
            if abs(pre_s) < abs(now_s):
                ans += abs(pre_s) + 1
                now_s = a[i] + 1
            else:
                ans += abs(now_s) + 1
                now_s = 1
    elif pre_s > 0:
        if now_s >= 0:
            if abs(pre_s) < abs(now_s):
                ans += abs(pre_s) + 1
                now_s = a[i] - 1
            else:
                ans += abs(now_s) + 1
                now_s = -1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
now_s = 0
ans = 0
for i in range(n-1):
    now_s += a[i]
    next_s = now_s + a[i+1]
    if now_s == 0:
        if next_s > 0:
            now_s -= 1
            ans += 1
        else:
            now_s += 1
            ans += 1
    if now_s*next_s >= 0:
        ans += abs(next_s) + 1
        a[i+1] = -1*(now_s+1)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(x) for x in input().split()]

seq_sum = a[0]
times_odd = 0

if a[0] == 0:
    times_odd += 1
    seq_sum = 1

for i in range(1, n):
    if seq_sum > 0 and seq_sum + a[i] >= 0:
        times_odd += seq_sum + a[i] + 1
        seq_sum = -1
    elif seq_sum < 0 and seq_sum + a[i] <= 0:
        times_odd += abs(seq_sum + a[i]) + 1
        seq_sum = 1
    else:
        seq_sum += a[i]

seq_sum = -a[0]
times_even = 2 * a[0]

if a[0] == 0:
    times_even += 1
    seq_sum = -1

for i in range(1, n):
    if seq_sum > 0 and seq_sum + a[i] >= 0:
        times_even += seq_sum + a[i] + 1
        seq_sum = -1
    elif seq_sum < 0 and seq_sum + a[i] <= 0:
        times_even += abs(seq_sum + a[i]) + 1
        seq_sum = 1
    else:
        seq_sum += a[i]

ans = min(times_odd, times_even)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(x) for x in input().split()]

seq_sum = a[0]
times = 0


for i in range(1, n):
    if seq_sum > 0 and seq_sum + a[i] >= 0:
        times += seq_sum + a[i] + 1
        seq_sum = -1
    elif seq_sum < 0 and seq_sum + a[i] <= 0:
        times += abs(seq_sum + a[i]) + 1
        seq_sum = 1
    else:
        seq_sum += a[i]

print(times)

Submission status: Wrong Answer

Solution code:
def sign(x):
    return (x > 0) - (x < 0)


def main():
    n = int(input())
    a = [int(an) for an in input().split()]
    total = a[0]
    if total == 0:
        total -= a[1] + sign(a[1])
    sign_total = sign(total)
    ans = 0
    for i in range(1, n):
        total += a[i]
        if a[i] == 0:
            total += sign_total * -1
            ans += 1
        sign_tmp = sign(total)
        if total == 0 or sign_total == sign_tmp:
            val = total + sign_total
            ans += val * sign_total
            total -= val
        sign_total *= -1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def sign(x):
    return (x > 0) - (x < 0)


def main():
    n = int(input())
    a = [int(an) for an in input().split()]
    total = a[0]
    sign_total = sign(a[0])
    ans = 0
    for i in range(1, n):
        total += a[i]
        if a[i] == 0:
            total += sign_total * -1
            ans += 1
        sign_tmp = sign(total)
        if total == 0 or sign_total == sign_tmp:
            val = total + sign_total
            ans += val * sign_total
            total -= val
        sign_total *= -1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def sign(x):
    return (x > 0) - (x < 0)


def main():
    n = int(input())
    a = [int(an) for an in input().split()]
    total = a[0]
    sign_total = sign(a[0])
    ans = 0
    for i in range(1, n):
        total += a[i]
        sign_tmp = sign(total)
        if total == 0 or sign_total == sign_tmp:
            val = 0
            if sign_total > 0:
                val = total + 1
                ans += val
            else:
                val = total - 1
                ans -= val
            total -= val
        sign_total *= -1

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(x) for x in input().split()]

def odd_positive(List, n):
    sum_a = 0
    cost = 0
    i = 0
    for a in List:
        i += 1
        nextsum = sum_a + a
        if i & 1 and nextsum <= 0:
            cost = - nextsum + 1
            sum_a = 1
        elif (not i & 1) and nextsum >= 0:
            cost = nextsum + 1
            sum_a = -1
    return cost


def odd_negative(List, n):
    sum_a = 0
    cost = 0
    i = 0
    for a in List:
        i += 1
        nextsum = sum_a + a
        if i & 1 and nextsum >= 0:
            cost = nextsum + 1
            sum_a = -1
        elif (not i & 1) and nextsum <= 0:
            cost = - nextsum + 1
            sum_a = 1
    return cost
    
ans = min(odd_positive(a,n), odd_negative(a,n)) 
print(ans)

Submission status: Wrong Answer

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

sum = a[0]
change = 0

if a[0]==0:
  idx=0
  while a[idx]==0:
    idx+=1
    
  for i in range(idx-1,-1,-1):
    val = 0
    tempsum = sum+a[i]
    if sum < 0 and tempsum <=0:
      val = 1 - tempsum
    if sum > 0 and tempsum >=0:
      val = -1 - tempsum
    sum = tempsum + val
    change += abs(val)
sum = a[0]

for i in range(1,n):
  val = 0
  tempsum = sum+a[i]
  if sum < 0 and tempsum <=0:
    val = 1 - tempsum
    
  if sum > 0 and tempsum >=0:
    val = -1 - tempsum
    
  sum = tempsum + val
  change += abs(val)
print(change)

Submission status: Wrong Answer

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

change = 0
if a[0]==0:
  i=0
  while a[i]==0:
    i+=1
  if a[i]<0:
    a[i-1]=1
    for idx in range(i-2,-1,-1):
      a[idx]=a[idx+1]*-1
  if a[i]>0:
    a[i-1]=-1
    for idx in range(i-2,-1,-1):
      a[idx]=a[idx+1]*-1
  change += i
sum = a[0]

for i in range(1,n):
  val = 0
  tempsum = sum+a[i]
  if sum < 0 and tempsum <=0:
    val = 1 - tempsum
    
  if sum > 0 and tempsum >=0:
    val = -1 - tempsum
    
  sum = tempsum + val
  change += abs(val)
print(change)

Submission status: Wrong Answer

Solution code:

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


def solve(s1, s2):
    "if start == true, assume the first of the sum is positive."
    res = 0
    sum = 0
    for i in range(n):
        sum += a[i]
        if sum <= 0 and i % 2 == s1:
            res += abs(sum) + 1
            sum = 1
        elif sum >= 0 and i % 2 == s2:
            res += abs(sum) + 1
            sum = -1
    return res


print(min(solve(0, 1), solve(1, 2)))

Submission status: Wrong Answer

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

sum = a[0]
change = 0
if a[0]==0:
  i=0
  while a[i]==0:
    i+=1
  if a[i]<0:
    a[i-1]=1
    for idx in range(i-2,-1,-1):
      a[idx]=a[i-1]*-1
  if a[i]>0:
    a[i-1]=-1
    for idx in range(i-2,-1,-1):
      a[idx]=a[i-1]*-1
  change += i-1

for i in range(1,n):
  val = 0
  tempsum = sum+a[i]
  if sum < 0 and tempsum <=0:
    val = 1 - tempsum
    
  if sum > 0 and tempsum >=0:
    val = -1 - tempsum
    
  sum = tempsum + val
  change += abs(val)
  
print(change)

Submission status: Wrong Answer

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

sum = a[0]
change = 0
if a[0]==0:
  i=0
  while a[i]==0:
    i+=1
  if a[i]<0:
    a[i-1]=1
    for idx in range(i-2,-1,-1):
      a[idx]=a[i-1]*-1
  if a[i]>0:
    a[i-1]=-1
    for idx in range(i-2,-1,-1):
      a[idx]=a[i-1]*-1
  change += i
#print(a)

d=[a[0]]
cd=[a[0]]

for i in range(1,n):
  val = 0
  tempsum = sum+a[i]
  d.append(tempsum)
  if sum < 0 and tempsum <=0:
    val = 1 - tempsum
    
  if sum > 0 and tempsum >=0:
    val = -1 - tempsum
    
  sum = tempsum + val
  cd.append(sum)
  change += abs(val)

  
#print(d)
#print(cd)
print(change)

Submission status: Wrong Answer

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

sum = a[0]
change = 0

for i in range(1, n):
    val = 0
    tempsum = sum + a[i]
    if sum < 0 and tempsum <= 0:
        val = 1 - tempsum
    if sum > 0 and tempsum >= 0:
        val = -1 - tempsum
    if sum == 0 and a[i] > 0:
        val = -1
    if sum == 0 and a[i] < 0:
        val = 1
    sum = tempsum + val
    change += abs(val)

print(change)