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:
import numpy as np
n = int(input())
a = list(map(int, input().split()))
c1 = 0
a0 = a[0]
if a0 == 0:
    c1 += 1
    a[0] = 1
sum = a[0]
for i in range(1, n):
    if not (np.sign(sum) != np.sign(sum + a[i]) and sum + a[i] != 0):
        if sum > 0:
            c1 += a[i] + sum + 1
            sum = -1
        else:
            c1 += -a[i] - sum + 1
            sum = 1
    else:
        sum += a[i]
c2 = abs(a[0]) + 1
if a0 == 0:
    c2 = 1
    a[0] = 1
sum = - a[0]
for i in range(1, n):
    if not (np.sign(sum) != np.sign(sum + a[i]) and sum + a[i] != 0):
        if sum > 0:
            c2 += a[i] + sum + 1
            sum = -1
        else:
            c2 += -a[i] - sum + 1
            sum = 1
    else:
        sum += a[i]
print(min(c1, c2))

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())
a = list(map(int, input().split()))
c1 = 0
sum = a[0]
for i in range(1, n):
    if not (np.sign(sum) != np.sign(sum + a[i]) and sum + a[i] != 0):
        if sum > 0:
            c1 += a[i] + sum + 1
            sum = -1
        else:
            c1 += -a[i] - sum + 1
            sum = 1
    else:
        sum += a[i]
c2 = abs(a[0]) + 1
sum = - a[0]
for i in range(1, n):
    if not (np.sign(sum) != np.sign(sum + a[i]) and sum + a[i] != 0):
        if sum > 0:
            c2 += a[i] + sum + 1
            sum = -1
        else:
            c2 += -a[i] - sum + 1
            sum = 1
    else:
        sum += a[i]
print(min(c1, c2))

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())
a = list(map(int, input().split()))
c1 = 0
sum = a[0]
for i in range(1, n):
    if not (np.sign(sum) != np.sign(sum + a[i]) and sum + a[i] != 0):
        if sum > 0:
            c1 += a[i] + sum + 1
            sum = -1
        else:
            c1 += -a[i] - sum + 1
            sum = 1
    else:
        sum += a[i]
c２ = abs(a[0]) + 1
sum = - a[0]
for i in range(1, n):
    if not (np.sign(sum) != np.sign(sum + a[i]) and sum + a[i] != 0):
        if sum > 0:
            c2 += a[i] + sum + 1
            sum = -1
        else:
            c2 += -a[i] - sum + 1
            sum = 1
    else:
        sum += a[i]
print(min(c1, c2))

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())
a = list(map(int, input().split()))
c = 0
sum = a[0]
for i in range(1, n):
    if not (np.sign(sum) != np.sign(sum + a[i]) and sum + a[i] != 0):
        if sum > 0:
            c += a[i] + sum + 1
            sum = -1
        else:
            c += -a[i] - sum + 1
            sum = 1
    else:
        sum += a[i]
print(c)

Submission status: Wrong Answer

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

for i in range(len(a)):
    if i == 0:
        count_1 += abs(1 - a[i])
    elif i % 2 == 1:
        count_1 += abs(-2 - a[i])
    elif i % 2 == 0:
        count_1 += abs(2 - a[i])

for i in range(len(a)):
    if i == 0:
        count_2 += abs(-1 - a[i])
    elif i % 2 == 1:
        count_2 += abs(2 - a[i])
    elif i % 2 == 0:
        count_2 += abs(-2 - a[i])

print(count_1 if count_1 < count_2 else count_2)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
count = 0
sum_ = 0
for i in range(n):
    if sum_ * (sum_+a[i]) >=0 and i!=0:
        if sum_ > 0:
            count += sum_+a[i]+1
            a[i] = -sum_-1
        elif sum_ < 0:
            count += -sum_-a[i]+1
            a[i] = -sum_+1
    sum_ += a[i]
print(count)

Submission status: Wrong Answer

Solution code:
def main():
    n, *a = map(int, open(0).read().split())

    def chk(a, t):
        ans = 0
        x = 0
        for i in a:
            x += i
            if t and x < 1:
                ans += 1 - x
                x = 1
            elif t and x > -1:
                ans += x + 1
                x = -1
            t = not t
        return ans

    print(min(chk(a, True), chk(a, False)))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=0
res=0 #0が正、1が負
sum=a[0]
if a[0]<0:
    res=1
for i in range(n-1):
    sum = sum+a[i+1]
    if sum<0:
        if res==1:
            adj = 1-sum
            a[i+1]=a[i+1]+adj
            ans=ans+abs(adj)
            sum=sum+adj
            res = 0
        else:
            res=1
    elif sum>0:
        if res==0:
            adj = -1-sum
            a[i+1]=a[i+1]+adj
            ans=ans+abs(adj)
            sum=sum+adj
            res = 1
        else:
            res=0
    if sum==0:
        if res == 1:
            a[i+1]=a[i+1]+1
            sum=sum+1
            ans+=1
            res=0
        else:
            a[i+1]=a[i+1]-1
            sum=sum-1
            ans+=1
            res=1
print(ans)

Submission status: Wrong Answer

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

ans=0
for i in range(N):
    if i==0:
        a=A[0]
        if a==0:
            if A[1]>0:
                a=-1
                ans+=1
            else:
                a=1
                ans+=1
        SUM=a
    else:
        a=A[i]
        isplus=(SUM>0)
        if isplus:
            if a>0:
                ans+=SUM+1
                a=-1
            else:
                if SUM+a>=0:
                    
                    ans+=1+SUM+a
                    SUM=-1
                else:
                    SUM+=a
        else:
            if a>0:
                if SUM+a<=0:
                    ans+=-(SUM+a)+1
                    SUM=1
                else:
                    SUM+=a

            else:
                ans+=-a-SUM+1
                SUM=1
    #print(SUM)
print(ans)

Submission status: Wrong Answer

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))
n = ii()
A = iil()

cum = 0
ope = 0
# first plus case
for i,item in enumerate(A):
    cum += item
    if i%2 == 0 and cum <= 0:
        ope += abs(cum)+1
        cum = 1
    elif i%2 == 1 and cum >= 0:
        ope += cum+1
        cum = -1
#    print(cum)
tmp = ope

cum = 0
ope = 0
# first minus case
for i,item in enumerate(A):
    cum += item
    if i%2 == 1 and cum <= 0:
        ope += abs(cum)+1
        cum = 1
    elif i%2 == 0 and cum >= 0:
        ope += cum+1
        cum = -1
print(tmp,ope)
print(min(tmp,ope))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int , input().split()))
res=[0]*n
res[0]=a[0]

ng=0
ps=0
for i in range(1,n):
    res[i]=res[i-1]+a[i]


for i in range(n-1):
    s=res[i]-ng+ps
    if s*(res[i+1]+ps-ng)>=0:
        if s>0:
            ng+=abs(a[i+1]+s)+1
        else:
            ps+=abs(a[i+1]+s)+1
print(ps+ng)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def sol(S):
    ret = 0
    for a in A[1:]:
        b = a
        if S * (S + b) > 0:
            b = (abs(S) + 1) * (1 if S < 0 else -1)
        if S + b == 0:
            b = b - 1 if S >= 0 else b + 1
        ret += abs(b - a)
        S += b
    return ret

if A[0] == 0:
    ans = min(sol(1), sol(-1)) + 1
else:
    ans = min(sol(A[0]), sol(-A[0] // A[0]) + abs(A[0]) + 1)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int , input().split()))
res=[0]*n
res[0]=a[0]

ng=0
ps=0
for i in range(1,n):
    res[i]=res[i-1]+a[i]


for i in range(n-1):
    s=res[i]
    if s*(res[i+1]+ps-ng)>0:
        if s>0:
            ng+=abs(a[i]+s)+1
        else:
            ps+=abs(a[i]+s)+1
print(ps+ng)

Submission status: Wrong Answer

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

def sol(S):
    ret = 0
    for a in A[1:]:
        b = a
        if S * (S + b) > 0:
            b = (abs(S) + 1) * (1 if S < 0 else -1)
        if S + b == 0:
            b = b - 1 if b < 0 else b + 1
        ret += abs(b - a)
        S += b
    return ret

if A[0] == 0:
    ans = min(sol(1), sol(-1)) + 1
else:
    ans = min(sol(A[0]), sol(-A[0] // A[0]) + abs(A[0]) + 1)
print(ans)

Submission status: Wrong Answer

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

def sol(S):
    ret = 0
    for a in A[1:]:
        b = a
        if S * (S + b) > 0:
            b = (abs(S) + 1) * (1 if S < 0 else -1)
        if S + b == 0:
            b = b - 1 if b < 0 else b + 1
        ret += abs(b - a)
        S += b
    return ret

print(sol(A[0]))

Submission status: Wrong Answer

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

def sol(S):
    ret = 0
    for a in A[1:]:
        b = a
        if S * (S + b) > 0:
            b = (abs(S) + 1) * (1 if S < 0 else -1)
        if S + b == 0:
            b = b - 1 if b < 0 else b + 1
        ret += abs(b - a)
        S += b
    return ret

ans = min(
    sol(A[0]),
    (sol(0) + abs(A[0])) if A[0] < 0 else (sol(-1) + abs(A[0]) + 1)
)
print(ans)

Submission status: Wrong Answer

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

def sol(S):
    ret = 0
    for a in A[1:]:
        b = a
        if S * (S + b) > 0:
            b = (abs(S) + 1) * (1 if S < 0 else -1)
        if S + b == 0:
            b = b - 1 if b < 0 else b + 1
        ret += abs(b - a)
        S += b
    return ret

ans = min(
    sol(A[0]),
    sol(-1 if A[0] >= 0 else 0) + abs(A[0]) + (1 if A[0] >= 0 else 0)
)
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    n=int(readline())
    a=list(map(int,readline().split()))

    s=[a[0]]*n
    for i in range(1,n):
        s[i]=s[i-1]+a[i]

    flag=True
    #1,-1,...
    for i in range(n):
        if i%2==0:
            if s[i]<=0:
                flag=False
                break
        else:
            if s[i]>=0:
                flag=False
                break

    if flag:
        print(0)
    else:  #-1,1,...
        flag=True
        for i in range(n):
            if i%2==0:
                if s[i]>=0:
                    flag=False
                    break
            else:
                if s[i]<=0:
                    flag=False
                    break
        if flag:
            print(0)
        else:
            cnt1=0
            s1=s[:]
            f1=0
            #1,-1,...
            for i in range(n):
                if i%2==0:
                    if s1[i]+f1<=0:
                        cnt1+=abs(1-s1[i]-f1)
                        f1+=1-s1[i]
                        s1[i]=1
                else:
                    if s1[i]+f1>=0:
                        cnt1+=abs(-1-s1[i]-f1)
                        f1+=-1-s1[i]
                        s1[i]=-1
            
            cnt2=0
            s2=s[:]
            f2=0
            #-1,1,...
            for i in range(n):
                if i%2==0:
                    if s2[i]+f2>=0:
                        cnt2+=abs(-1-s2[i]-f2)
                        f2+=-1-s2[i]
                        s2[i]=-1
                else:
                    if s2[i]+f2<=0:
                        cnt2+=abs(1-s2[i]-f2)
                        f2+=1-s2[i]
                        s2[i]=1
            print(min(cnt1,cnt2))

if __name__=="__main__":
    main()

Submission status: Wrong Answer

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

def sol(S):
    ret = 0
    B = [S]
    for a in A[1:]:
        b = a
        if S * (S + b) > 0:
            b = (abs(S) + 1) * (1 if S < 0 else -1)
        if S + b == 0:
            b = b - 1 if b < 0 else b + 1
        ret += abs(b - a)
        S += b
        B.append(b)
    return ret

ans = min(
    sol(A[0]),
    sol(-A[0] // abs(A[0]) if A[0] != 0 else -1) + abs(A[0]) + 1
)
print(ans)

Submission status: Wrong Answer

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

def sol(S):
    ret = 0
    B = [S]
    for a in A[1:]:
        b = a
        if S * (S + b) > 0:
            b = (abs(S) + 1) * (1 if S < 0 else -1)
        if S + b == 0:
            b = b - 1 if b < 0 else b + 1
        ret += abs(b - a)
        S += b
        B.append(b)
    return ret

ans = min(
    sol(A[0]),
    sol(-A[0] // abs(A[0])) + abs(A[0]) + 1 if A[0] != 0 else 10**18
)
print(ans)

Submission status: Wrong Answer

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))
import numpy
n = ii()
A = iil()
cum = numpy.cumsum(A)
#print(cum)
#print(type(cum))
now = -1*cum[0]
ans = 0
ope = 0
for i,item in enumerate(cum):
    num = item+ope
    if num == 0:
        ans += 1
        ope += 1 if now < 0 else -1
        num += 1 if now < 0 else -1
    elif num*now > 0:
        ans += abs(num)+1
        ope -= (abs(num)+1)*num//abs(num)
        num -= (abs(num)+1)*num//abs(num)
#    print(ans,ope,num)
    now = num
now = -1*cum[0]
ans2 = 0
ope = 0
for i,item in enumerate(cum):
    num = item+ope
    if num == 0:
        ans2 += 1
        ope += 1 if now < 0 else -1
        num += 1 if now < 0 else -1
    elif num*now > 0:
        ans2 += abs(num)+1
        ope -= (abs(num)+1)*num//abs(num)
        num -= (abs(num)+1)*num//abs(num)
#    print(ans,ope,num)
    now = num
print(min(ans,ans2))

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = list(map(int, input().split()))

    res = 0
    R = [0]
    for i in range(n):
        if i == 0:
            if A[i] == 0:
                res += 1
                if A[i + 1] >= 0:
                    R.append(-1)
                else:
                    R.append(1)
            else:
                R.append(R[-1] + A[i])
        else:
            if R[-1] >= 0 and R[-1] + A[i] >= 0:
                res += R[-1] + A[i] + 1
                R.append(-1)
            elif R[-1] <= 0 and R[-1] + A[i] <= 0:
                res += abs(R[-1] + A[i]) + 1
                R.append(1)
            else:
                R.append(R[-1] + A[i])

    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))
import numpy
n = ii()
A = iil()
cum = numpy.cumsum(A)
#print(cum)
#print(type(cum))
now = -1*cum[0]
ans = 0
ope = 0
for i,item in enumerate(cum):
    num = item+ope
    if num == 0:
        ans += 1
        ope += 1 if now < 0 else -1
        num += 1 if now < 0 else -1
    elif num*now > 0:
        ans += abs(num)+1
        ope -= (abs(num)+1)*num//abs(num)
        num -= (abs(num)+1)*num//abs(num)
#    print(ans,ope,num)
    now = num
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = int(readline())
a = list(map(int, readline().split()))
if a[0] > 0:
    cnt2 = a[0] + 1
    now2 = -1
    cnt1 = 0
    now1 = a[0]
else:
    cnt2 = 0
    now2 = a[0]
    cnt1 = -a[0] + 1
    now1 = 1

for i, aa in enumerate(a[1:]):
    if i % 2 == 0:
        if aa + now2 > 0:
            now2 += aa
        else:
            cnt2 += abs(now2 + aa) + 1
            now2 = 1
        if now1 + aa < 0:
            now1 += aa
        else:
            cnt1 += now1 + aa + 1
            now1 = -1
    else:
        if aa + now1 > 0:
            now1 += aa
        else:
            cnt1 += abs(now1 + aa) + 1
            now1 = 1
        if now2 + aa < 0:
            now2 += aa
        else:
            cnt2 += now2 + aa + 1
            now2 = -1
print(min(cnt1, cnt2))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
res = -min(0, ~a[0])
sm = 1
if a[0] >= 1:
  res = 0
  sm = a[0]
#print(res, sm)
for i in range(1, N):
  if sm > 0:
    res += max(-1, a[i] + sm) + 1
    sm = min(-1, sm + a[i])
  else:
    res += -min(1, sm + a[i]) + 1
    sm = max(1, sm + a[i])
  #print(res, sm)
res2 = max(0, a[0] + 1)
sm = -1
if a[0] <= -1:
  res = 0
  sm = a[0]
for i in range(1, N):
  if sm > 0:
    res2 += max(-1, a[i] + sm) + 1
    sm = min(-1, sm + a[i])
  else:
    res2 += -min(1, sm + a[i]) + 1
    sm = max(1, sm + a[i])
  #print(res2, sm)
print(min(res, res2))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
res = -max(0, ~a[0])
sm = 1
if a[0] > 1: sm = a[0]
for i in range(1, N):
  if sm > 0:
    res += max(-1, a[i] + sm) + 1
    sm = min(-1, sm + a[i])
  else:
    res += -min(1, sm + a[i]) + 1
    sm = max(1, sm + a[i])
  #print(res, sm)
res2 = max(0, a[0] + 1)
sm = -1
if a[0] < -1: sm = a[0]
for i in range(1, N):
  if sm > 0:
    res2 += max(-1, a[i] + sm) + 1
    sm = min(-1, sm + a[i])
  else:
    res2 += -min(1, sm + a[i]) + 1
    sm = max(1, sm + a[i])
print(min(res, res2))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
acc = [a[0]]
for i in range(1, n):
    acc.append(acc[-1] + a[i])

res = 10 ** 100

def f(fir):
    prv_pos = fir > 0
    margin = fir - a[0]
    ret = abs(margin)
    for i in range(1, n):
        cur = margin + acc[i]
        if prv_pos and cur < 0:
            prv_pos = not prv_pos
            continue
        if not prv_pos and cur > 0:
            prv_pos = not prv_pos
            continue
        if prv_pos:
            need = -1
            sa = need - cur
            margin += sa
            ret += abs(sa)
        else:
            need = 1
            sa = need - cur
            margin += sa
            ret += abs(sa)
        prv_pos = not prv_pos
    return ret


res = min(res, f(1))
res = min(res, f(-1))
res = min(res, f(a[0]))
print(res)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
res = -max(0, ~a[0])
sm = 1
if a[0] > 1: sm = a[0]
for i in range(1, N):
  if sm > 0:
    res += max(0, a[i] + sm + 1)
    sm = min(-1, sm + a[i])
  else:
    res += -min(0, sm - a[i] + 1)
    sm = max(1, sm + a[i])
  #print(res, sm)
res2 = max(0, a[0] + 1)
sm = -1
if a[0] < -1: sm = a[0]
for i in range(1, N):
  if sm > 0:
    res2 += max(0, a[i] + sm + 1)
    sm = min(-1, sm + a[i])
  else:
    res2 += -min(0, sm - a[i] + 1)
    sm = max(1, sm + a[i])
print(min(res, res2))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
res = -max(0, ~a[0])
sm = 1
if a[0] > 1: sm = a[0]
for i in range(1, N):
  if sm > 0:
    res += max(0, a[i] + sm + 1)
    sm = min(-1, a[i])
  else:
    res += max(0, sm - a[i] + 1)
    sm = max(1, a[i])
res2 = max(0, a[0] + 1)
sm = -1
if a[0] < -1: sm = a[0]
for i in range(1, N):
  if sm > 0:
    res2 += max(0, a[i] + sm + 1)
    sm = min(-1, a[i])
  else:
    res2 += max(0, sm - a[i] + 1)
    sm = max(1, a[i])
print(min(res, res2))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
import copy

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = list(map(int, input().split()))
    c1 = np.cumsum(A)
    c2 = copy.deepcopy(c1)

    # + - + - の順番
    cnt = 0 if c1[0] > 0 else abs(c1[0]) + 1
    val = 0 if c1[0] > 0 else abs(c1[0]) + 1
    for i in range(N):
        if i % 2 == 0:
            # +
            c1[i] += val
            if c1[i] > 0:
                pass
            else:
                cnt += abs(c1[i]) + 1
                val += abs(c1[i]) + 1
        else:
            # -
            c1[i] += val
            if c1[i] < 0:
                pass
            else:
                cnt += c1[i] + 1
                val -= c1[i] + 1
    ans = cnt

    # - + - +の順番
    cnt = 0 if c2[0] < 0 else abs(c2[0]) + 1
    val = 0 if c2[0] < 0 else abs(c2[0]) + 1
    for i in range(N):
        if i % 2 == 1:
            # +
            c2[i] += val
            if c2[i] > 0:
                pass
            else:
                cnt += abs(c2[i]) + 1
                val += abs(c2[i]) + 1
        else:
            # -
            c2[i] += val
            if c2[i] < 0:
                pass
            else:
                cnt += c2[i] + 1
                val -= c2[i] + 1

    if ans > cnt:
        ans = cnt
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)


def main():
    N = int(input())
    A = list(map(int, input().split()))
    S = A[0]
    cnt = 0
    if A[0] == 0:
        answer = []
        for a in (1, -1):
            cnt = 1
            S = a
            for i in range(1, N):
                s = S + A[i]
                if A[i] == 0:
                    if S > 0:
                        cnt += S + 1
                        S = -1
                    else:
                        cnt += abs(S) + 1
                        S = 1
                else:
                    if s == 0:
                        if S < 0:
                            cnt += 1
                            S = 1
                        else:
                            cnt += 1
                            S = -1
                    else:
                        if S * s > 0:
                            if S < 0:
                                cnt += abs(s) + 1
                                S = 1
                            else:
                                cnt += s + 1
                                S = -1
                        else:
                            S = s
            answer.append(cnt)
        print(min(answer))
    else:
        for i in range(1, N):
            s = S + A[i]
            if A[i] == 0:
                if S > 0:
                    cnt += S + 1
                    S = -1
                else:
                    cnt += abs(S) + 1
                    S = 1
            else:
                if s == 0:
                    if S < 0:
                        cnt += 1
                        S = 1
                    else:
                        cnt += 1
                        S = -1
                else:
                    if S * s > 0:
                        if S < 0:
                            cnt += abs(s) + 1
                            S = 1
                        else:
                            cnt += s + 1
                            S = -1
                    else:
                        S = s
        print(cnt)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)


def main():
    N = int(input())
    A = list(map(int, input().split()))
    S = A[0]
    cnt = 0
    if A[0] == 0:
        for a in (1, -1):
            cnt = 1
            S = a
            for i in range(1, N):
                s = S + A[i]
                if A[i] == 0:
                    if S > 0:
                        cnt += S + 1
                        S = -1
                    else:
                        cnt += abs(S) + 1
                        S = 1
                else:
                    if s == 0:
                        if S < 0:
                            cnt += 1
                            S = 1
                        else:
                            cnt += 1
                            S = -1
                    else:
                        if S * s > 0:
                            if S < 0:
                                cnt += abs(s) + 1
                                S = 1
                            else:
                                cnt += s + 1
                                S = -1
                        else:
                            S = s
    else:
        for i in range(1, N):
            s = S + A[i]
            if A[i] == 0:
                if S > 0:
                    cnt += S + 1
                    S = -1
                else:
                    cnt += abs(S) + 1
                    S = 1
            else:
                if s == 0:
                    if S < 0:
                        cnt += 1
                        S = 1
                    else:
                        cnt += 1
                        S = -1
                else:
                    if S * s > 0:
                        if S < 0:
                            cnt += abs(s) + 1
                            S = 1
                        else:
                            cnt += s + 1
                            S = -1
                    else:
                        S = s
        print(cnt)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)


def main():
    N = int(input())
    A = list(map(int, input().split()))
    S = A[0]
    cnt = 0
    for i in range(1, N):
        s = S + A[i]
        if s == 0:
            if S < 0:
                cnt += 1
                S = 1
            else:
                cnt += 1
                S = -1
        else:
            if S * s > 0:
                if S < 0:
                    cnt += abs(s) + 1
                    S = 1
                else:
                    cnt += s + 1
                    S = -1
            else:
                S = s
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
counter = 0            ####操作回数
A.reverse()
S = 0           
a = A.pop()
if a==0:
  counter += 1
  while A:
    b = A.pop()
    if b == 0:
      counter += 2
    elif b>0:
      A.append(b)
      S = -1
      break
    elif b<0:
      A.append(b)
      S = 1
      break
else:
  S += a
    
    
while A:
  c = A.pop()
  if c>=0 and S>0:
    counter += abs(c+S)+1
    S = -1
  elif c<=0 and S<0:
    counter += abs(c+S)+1
    S = 1
  elif S<0 and S+c<=0:
    counter += abs(S+c)+1
    S = 1
  elif S>0 and S+c>=0:
    counter += abs(S+c)+1
    S = -1    
  else:
    S += c
    
print(counter)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
counter = 0            ####操作回数
A.reverse()
S = 0           
a = A.pop()
if a==0:
  counter += 1
  while A:
    b = A.pop()
    if b == 0:
      counter += 2
    elif b>0:
      A.append(b)
      S = -1
      break
    elif b<0:
      A.append(b)
      S = 1
      break
else:
  S += a
    
    
while A:
  c = A.pop()
  if c>=0 and S>0:
    counter += abs(c+S)+1
    S = -1
  elif c<=0 and S<0:
    counter += abs(c+S)+1
    S = 1
  elif (c>0 and S<0) and S+c<=0:
    counter += abs(S+c)+1
    S = 1
  elif (c<0 and S>0) and S+c>=0:
    counter += abs(S+c)+1
    S = -1    
  else:
    S += c
    
print(counter)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
now_a = a[0]
count = 0

'''
True = positive
False = negative
'''
sign = True
if now_a < 0:
    sign = False

for i in range(1, n):
    next_a = now_a + a[i]
    if sign:
        if next_a >= 0:
            count += next_a + 1
            now_a = -1
        else:
            now_a = next_a
        sign = False
    else:
        if next_a <= 0:
            count += abs(next_a) + 1
            now_a = 1
        else:
            now_a = next_a
        sign = True
print(count)

Submission status: Wrong Answer

Solution code:
def sign(x):
  if x<0:
    return -1
  elif x>0:
    return 1
  else:
    return 0
n = int(input())
a = list(map(int,input().split()))
cumulative_sum = a[0]
flag = sign(cumulative_sum)
ans = 0
for i in range(1,n):
  cumulative_sum += a[i]
  if sign(cumulative_sum) == flag or sign(cumulative_sum) == 0:
    ans += abs(-flag-cumulative_sum)
    cumulative_sum = -flag
  flag = sign(cumulative_sum)
print(ans)

Submission status: Wrong Answer

Solution code:
def sign(x):
  if x<0:
    return -1
  elif x>0:
    return 1
  else:
    return 0
n = int(input())
a = list(map(int,input().split()))
cumulative_sum = a[0]
flag = sign(cumulative_sum)
ans = 0
for i in range(1,n):
  cumulative_sum += a[i]
  if sign(cumulative_sum) == flag or sign(cumulative_sum) == 0:
    ans += abs(-flag - cumulative_sum)
  flag = sign(cumulative_sum)
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    n = int(_in[0])  # type:int
    a_arr = list(map(int,_in[1].split(' ')))  # type:list(int)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    cnt = 0
    sum_ = 0
    for i in range(0,n):
        if i==0:
            if a_arr[0]==0:
                while a_arr[i]==0:
                    i+=1
                else:
                    if a_arr[i]>0:
                        sum_ = (-1)**i
                        cnt += 1
                    else:
                        sum_ = (-1)**(i+1)
                        cnt += 1
            else:
                sum_ = a_arr[i]
        else:
            if sum_<0:
                if sum_+a_arr[i]<=0:
                    cnt += 1-(sum_+a_arr[i])
                    sum_ = 1
                else:
                    sum_ = sum_+a_arr[i]
            else:
                if sum_+a_arr[i]>=0:
                    cnt += 1+(sum_+a_arr[i])
                    sum_ = -1
                else:
                    sum_ = sum_+a_arr[i]
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)