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:
n = int(input())
a = [int(a) for a in input().split()]

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


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

print(min(times, times2))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

previous = a[0]
for i in range(1, n):
    if previous < 0:
        if previous + a[i] > 0:
            previous = previous + a[i]
        else:
            break       
    elif previous > 0:
        if previous + a[i] < 0:
            previous = previous + a[i]
        else:
            break
else:
    print(0)
    exit()

times1 = 0
times2 = 0
if a[0] < 0:
    times1 += abs(1 + abs(a[0]) - a[1])
    times2 += 1 - a[0] + abs(-2 - a[1])
    for i in range(3, n+1):
        if i % 2 != 0:
            times1 += abs(-2 - a[i-1])
            times2 += abs(2 - a[i-1])
        else:
            times1 += abs(2 - a[i-1])
            times2 += abs(-2 - a[i-1])
else:
    times1 += abs(-(1 + a[0]) - a[1])
    times2 += abs(-1 - a[0]) + abs(2 - a[1])
    for i in range(3, n+1):
        if i % 2 != 0:
            times1 += abs(2 - a[i-1])
            times2 += abs(-2 - a[i-1])
        else:
            times1 += abs(-2 - a[i-1])
            times2 += abs(2 - a[i-1])

print(min(times1, times2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = [0, 0]
if a[0] == 0:
    sum = 1
    ans[0] = 1
else:
    sum = a[0]


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

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

print(min(ans))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
r_sum = [0]*(N+1)
sum = a[0]
ans = 0
for i in range(1, N):
    if sum < 0:
        if abs(sum) < a[i]:
            sum += a[i]
        else:
            ans += abs(sum)-a[i]+1
            sum += 1
    elif sum > 0:
        if sum + a[i] < 0:
            sum += a[i]
        else:
            ans += abs(-1-sum-a[i])
            sum = -1


print(ans)

Submission status: Wrong Answer

Solution code:
def z(s,l):
	for i in range(n-1):
		r=l+a[i+1]
		if r*l>=0:
			if l<=0:
				s+=1-r
				r=1
			else:
				s+=1+r
				r=-1
		print(a[i+1],r,s)
		l=r
	return s
n=int(input())
a=list(map(int,input().split()))
s1=0
l1=a[0]
if a[0]<=0:
	s1=1-a[0]
	l1=1
s2=0
l2=a[0]
if a[0]>=0:
	s2=a[0]+1
	l2=-1
print(min(z(s1,l1),z(s2,l2)))

Submission status: Wrong Answer

Solution code:
def z(s,l):
	for i in range(n-1):
		r=l+a[i+1]
		if r*l>=0:
			if l<=0:
				s+=1-r
				r=1
			else:
				s+=1+r
				r=-1
		l=r
	return s

n=int(input())
a=list(map(int,input().split()))
s1=0
l1=a[0]
if a[0]<=0:
	s1=1-a[0]
	l1=1
s2=0
l2=a[0]
if a[0]>=0:
	s1=a[0]-1
	l1=-1
print(min(z(s1,l1),z(s2,l2)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

# +, -, +, -, ...
sum = 0
count = 0
for i in range(n):
    sum += a[i]
    if i % 2 == 0:
        if sum <= 0:
            sum = 1
            count += abs(sum) + 1
    else:
        if sum >= 0:
            sum = -1
            count += abs(sum) + 1

# -, +, -, +, ...
sum2 = 0
count2 = 0
for i in range(n):
    sum2 += a[i]
    if i % 2 == 0:
        if sum2 >= 0:
            sum2 = -1
            count2 += abs(sum2) + 1
    else:
        if sum2 <= 0:
            sum2 = 1
            count2 += abs(sum2) + 1
            
print(min(count, count2))

Submission status: Wrong Answer

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

ruisekiwa = []
tmp = 0
for x in a:
    tmp += x
    ruisekiwa.append(tmp)


ans = tmp = cnt = 0
for x, y in zip(ruisekiwa, ruisekiwa[1:]):
    x += tmp
    y += tmp
    if (x == abs(x) and y != abs(y)) or (x != abs(x) and y == abs(y)) and y != 0:
        cnt += 1
    else:
        hoge = 0
        if y > 0:
            hoge += -1-y
        elif y < 0:
            hoge += 1-y
        else:
            if x > 0:
                hoge = -1
            else:
                hoge = 1
        tmp += hoge
        ans += abs(hoge)

if len(ruisekiwa) - 1 == cnt:
    print('0')
else:
    print(ans)

Submission status: Wrong Answer

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

ruisekiwa = []
tmp = 0
for x in a:
    tmp += x
    ruisekiwa.append(tmp)

ans = cnt = 0
for x, y in zip(ruisekiwa, ruisekiwa[1:]):
    if (x == abs(x) and y != abs(y)) or (x != abs(x) and y == abs(y)):
        cnt += 1
    else:
        ans += 1

if len(ruisekiwa) - 1 == cnt:
    print('0')
else:
    print(ans*2)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
cnt=0
for i in range(0,n):
    if i==0: num=a[0]
    else:
        if a[0] > 0:
            tmp = num + a[i]
            if i % 2 == 0:
                if tmp > 0:
                    num += a[i]
                    continue
                elif tmp == 0:
                    cnt+=1
                    num += a[i]+1
                    continue
                elif tmp < 0:
                    while True:
                        tmp += 1
                        cnt += 1
                        if tmp > 0:
                            a[i]+=cnt
                            break
                    num += a[i]
            if i % 2 == 1:
                if tmp < 0:
                    num += a[i]
                    continue
                elif tmp == 0:
                    cnt+=1
                    num += a[i]-1
                    continue
                elif tmp > 0:
                    while True:
                        tmp -= 1
                        cnt += 1
                        if tmp < 0:
                            a[i]-=cnt
                            break
                    num += a[i]
        elif a[0] < 0:
            tmp = num + a[i]
            if i % 2 == 1:
                if tmp > 0:
                    num += a[i]
                    continue
                elif tmp == 0:
                    cnt+=1
                    num += a[i]+1
                    continue
                elif tmp < 0:
                    while True:
                        tmp += 1
                        cnt += 1
                        if tmp > 0:
                            a[i] += cnt
                            break
                    num += a[i]
            if i % 2 == 0:
                if tmp < 0:
                    num += a[i]
                    continue
                elif tmp == 0:
                    cnt+=1
                    num += a[i]-1
                    continue
                elif tmp > 0:
                    while True:
                        tmp -= 1
                        cnt += 1
                        if tmp < 0:
                            a[i] -= cnt
                            break
                    num += a[i]

print(cnt)

Submission status: Wrong Answer

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

idx=0
ans=0
sum_last=0

for i in a:
    if i!=0:
        break
    idx+=1

if idx==len(a):
    ans=2*idx-1
elif idx>0:
    ans=2*idx-1
    if a[idx]>0:
        sum_last=-1
    else:
        sum_last=1
else:
    sum_last=a[0]
    ans=0
    idx=1

for i in a[idx:]:
    sum_cur=sum_last+i
    if sum_cur*sum_last>=0:
        ans+=abs(sum_cur)+1
        if sum_last>0:
            sum_last=-1
        else:
            sum_last=1
    else:
        sum_last=sum_cur
print(ans)

Submission status: Wrong Answer

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

idx=0
ans=0
sum_last=0

for i in a:
    if i!=0:
        break
    idx+=1

if idx==n:
    ans=2*n-1
elif idx>0:
    ans=2*n-1
    if a[idx]>0:
        sum_last=-1
    else:
        sum_last=1
else:
    sum_last=a[0]
    ans=0
    idx=1

for i in a[idx:]:
    sum_cur=sum_last+i
    if sum_cur*sum_last>=0:
        ans+=abs(sum_cur)+1
        if sum_last>0:
            sum_last=-1
        else:
            sum_last=1
    else:
        sum_last=sum_cur
print(ans)

Submission status: Wrong Answer

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

idx=0
ans=0
sum_last=0

for i in a:
    if i!=0:
        break
    idx+=1

ans=idx
if idx==len(a):
    pass
elif idx>0:
    if a[idx]>0:
        sum_last=-1
    else:
        sum_last=1
else:
    sum_last=a[0]
    idx+=1

for i in a[idx:]:
    sum_cur=sum_last+i
    if sum_cur*sum_last>=0:
        ans+=abs(sum_cur)+1
        if sum_last>0:
            sum_last=-1
        else:
            sum_last=1
    else:
        sum_last=sum_cur
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(lambda x: int(x), input().split()))
a.sort()
cnt=[]
val=[]
last=-1
for i in a:
    if i == last:
        cnt[-1]+=1
    else:
        cnt.append(1)
        val.append(i)
        last = i

low_val=-10
low_cnt=0
mid_val=0
mid_cnt=0
ans=0
for i, v in enumerate(val):
    if v>=low_val+3:
        ans=max(ans, low_cnt+mid_cnt+cnt[i])
        low_val=v
        low_cnt=cnt[i]
        mid_val=0
        mid_cnt=0
    elif v==low_val+2:
        ans=max(ans, low_cnt+mid_cnt+cnt[i])
        if mid_val>0:
            low_val=mid_val
            low_cnt=mid_cnt
            mid_val=v
            mid_cnt=cnt[i]
        else:
            low_val=v
            low_cnt=cnt[i]
            mid_val=0
            mid_cnt=0
    if v==low_val+1:
        mid_val=v
        mid_cnt=cnt[i]
ans=max(ans, low_cnt+mid_cnt)
print(ans)

Submission status: Wrong Answer

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

def wh(cst,ttl,flg):
    for i in range(n):
        ttl += a[i]
    

        if ttl*flg < 0:
            flg *= -1
        else:
            if flg > 0:
                memo = abs(ttl)+1
                ttl -= memo
                cst += memo
            elif flg < 0:
                memo = abs(ttl)+1
                ttl += memo
                cst += memo
            flg *= -1

    return cst




ttl = a[0]
cst = 0



cst = wh(cst,ttl,1)
ttl = a[0]
cst2 = 0
cst2 = wh(cst2,ttl,-1)
   

print(min(cst,cst2))

Submission status: Wrong Answer

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

def wh(cst,ttl,flg):
    for i in range(1,n):
        ttl += a[i]
    

        if ttl*flg < 0:
            flg *= -1
        else:
            if flg > 0:
                memo = abs(ttl)+1
                ttl -= memo
                cst += memo
            elif flg < 0:
                memo = abs(ttl)+1
                ttl += memo
                cst += memo
            flg *= -1

    return cst




ttl = a[0]
cst = 0

if a[0]>=0:
    flg = 1

elif a[0]<0:
    flg = -1

cst = wh(cst,ttl,flg)

   


ttl = a[0]
cst2 = 0

if a[0]>0:
    flg = -1
    cst2 += abs(ttl)+1
    ttl += 0-ttl-1 

elif a[0]<0:
    flg = 1
    cst2 += abs(ttl)+1
    ttl += 0-ttl+1 

cst2 = wh(cst2,ttl,flg)
   

print(min(cst,cst2))

Submission status: Wrong Answer

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

ttl = a[0]
cst = 0

if a[0]>=0:
    flg = 1

elif a[0]<0:
    flg = -1

for i in range(1,n):
    ttl += a[i]
    

    if ttl*flg < 0:
        flg *= -1
    else:
        if flg > 0:
            memo = abs(ttl)+1
            ttl -= memo
            cst += memo
        elif flg < 0:
            memo = abs(ttl)+1
            ttl += memo
            cst += memo
        flg *= -1
   


ttl = a[0]
cst2 = 0

if a[0]>0:
    flg = -1
    cst2 += abs(ttl)+1
    ttl += 0-ttl-1 

elif a[0]<0:
    flg = 1
    cst2 += abs(ttl)+1
    ttl += 0-ttl+1 

for i in range(1,n):
    ttl += a[i]
    

    if ttl*flg < 0:
        flg *= -1
    else:
        if flg > 0:
            memo = abs(ttl)+1
            ttl -= memo
            cst2 += memo
        elif flg < 0:
            memo = abs(ttl)+1
            ttl += memo
            cst2 += memo
        flg *= -1
   

print(min(cst,cst2))

Submission status: Wrong Answer

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

ttl = a[0]
cst = 0

if a[0]>=0:
    flg = 1

elif a[0]<0:
    flg = -1

for i in range(1,n):
    ttl += a[i]
    

    if ttl*flg < 0:
        flg *= -1
    else:
        if flg > 0:
            memo = abs(ttl)+1
            ttl -= memo
            cst += memo
        elif flg < 0:
            memo = abs(ttl)+1
            ttl += memo
            cst += memo
        flg *= -1
   


ttl = a[0]
cst2 = 0


   

print(min(cst,cst2))

Submission status: Wrong Answer

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

ttl = a[0]
cst = 0

if a[0]>=0:
    flg = 1

elif a[0]<0:
    flg = -1

for i in range(1,n):
    ttl += a[i]
    

    if ttl*flg < 0:
        flg *= -1
    else:
        if flg > 0:
            memo = abs(ttl)+1
            ttl -= memo
            cst += memo
        elif flg < 0:
            memo = abs(ttl)+1
            ttl += memo
            cst += memo
        flg *= -1
   



   

print(cst)

Submission status: Wrong Answer

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

ttl = a[0]
cst = 0

if a[0]>0:
    flg = 1
elif a[0] == 0:
    for i in range(1,n):
        if a[i]==0:
            continue
        elif a[i]>0:
            cst += 1
            ttl -= 1
            flg = -1
            break
        elif a[i]<0:
            cst += 1
            ttl += 1
            flg = 1
            break
else:
    flg = -1

for i in range(1,n):
    ttl += a[i]
    

    if ttl*flg < 0:
        flg *= -1
    else:
        if flg > 0:
            memo = abs(ttl)+abs(-1)
            ttl -= memo
            cst += memo
        elif flg < 0:
            memo = abs(ttl)+abs(-1)
            ttl += memo
            cst += memo
        flg *= -1
    

print(cst)

Submission status: Wrong Answer

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

ttl = a[0]
cst = 0

if a[0]>0:
    flg = 1
elif a[0] == 0:
    cst += 1
    ttl += 1
    flg = 1
else:
    flg = -1

for i in range(1,n):
    ttl += a[i]
    

    if ttl*flg < 0:
        flg *= -1
    else:
        if flg > 0:
            memo = abs(ttl)+abs(-1)
            ttl -= memo
            cst += memo
        elif flg < 0:
            memo = abs(ttl)+abs(-1)
            ttl += memo
            cst += memo
        flg *= -1
    

print(cst)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[int(i) for i in input().split()]
sm=l[0]
req=0 
pr=1 
if sm>0:
    pass 
else:
    req+=-sm+1 
    sm=1 
for i in range(1,n):
    if pr==1:
        n1=sm+l[i]
        if n1<0:
            sm+=l[i]
            pass 
        else: 
            sm+=l[i]
            req+=sm+1 
            sm=-1 
    else: 
        n1=sm+l[i]
        if n1>0:
            sm+=l[i]
            pass 
        else: 
            sm+=l[i]
            req+=(-sm+1) 
            sm=1 
    pr=1-pr  
req1=0 
pr=0
sm=l[0]
if sm<0:
    pass 
else:
    req1+=sm+1 
for i in range(1,n):
    if pr==1:
        n1=sm+l[i]
        if n1<0:
            sm+=l[i]
            pass 
        else: 
            sm+=l[i]
            req1+=sm+1 
            sm=-1 
    else: 
        n1=sm+l[i]
        if n1>0:
            sm+=l[i]
            pass 
        else: 
            sm+=l[i]
            req1+=(-sm+1) 
            sm=1 
    pr=1-pr  
print(min(req,req1))

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[int(i) for i in input().split()]
sm=l[0]
req=0 
if sm==0:
    sm+=1 
    req+=1 
    pr=1 
elif sm<0:
    pr=0 
else:
    pr=1 
for i in range(1,n):
    if pr==1:
        n1=sm+l[i]
        if n1<0:
            sm+=l[i]
            pass 
        else: 
            sm+=l[i]
            req+=sm+1 
            sm=-1 
    else: 
        n1=sm+l[i]
        if n1>0:
            sm+=l[i]
            pass 
        else: 
            sm+=l[i]
            req+=(-sm+1) 
            sm=1 
    pr=1-pr  
print(req)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
ans=0
if l[0]==0:
    for i in range(n):
        if l[i]!=0:
            s=-1**(i%2)
            ans=1
k1=l[0]

for i in range(1,n):
    if k1>0:
        k1+=l[i]
        if k1>=0:
            ans+=k1+1
            k1=-1
        continue
    if k1<0:
        k1+=l[i]
        if k1<=0:
            ans+=abs(k1)+1
            k1=1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(2000000)

def re(r,k,l):
    if k==0:return r
    while(k<l[0]):
        l=l[1:]
    return re(r+1,k-l[0],l)





n=int(input())
i=1
l6=[]
ans=10**5
while(i<n):
    l6.append(i)
    i*=6
i=1
l9=[]
while(i<n):
    l9.append(i)
    i*=9
l6.sort(reverse=True)
l9.sort(reverse=True)
for i in range(n+1):
    ans=min(ans,re(0,n-i,l6)+re(0,i,l9))
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
ans=0
if l[0]==0:
    for i in range(n):
        if l[i]!=0:
            s=-1**(i%2)
            ans=1
k1=l[0]

for i in range(1,n):
    if k1>0:
        k1+=l[i]
        if k1>=0:
            ans+=k1+1
            k1=-1
        continue
    if k1<0:
        k1+=l[i]
        if k1<=0:
            ans+=abs(k1)+1
            k1=1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

check=a[0]
ans=0
if check==0:
    for i in range(1,n):
        if check+a[i]==0:
            continue
            
        elif check+a[i]>0:
            if i%2==0:
                check=1
                ans=1
                break
            else:
                check=-1
                ans=1
                break
        else:
            if i%2==0:
                check=-1
                ans=1
                break
            else:
                check=1
                ans=1
                break
    else:
        check=1
        ans=1
                

for i in range(1,n):
    check2=check+a[i]

    if check<0:
        if check2>0:
            check=check2
        else:
            ans+=(abs(check2)+1)
            check=1
    else:
        if check2<0:
            check=check2
        else:
            ans+=(abs(check2)+1)
            check=-1

print(ans)

Submission status: Wrong Answer

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

check=a[0]
ans=0
if check==0:
    for i in range(1,n):
        if check+a[i]==0:
            continue
            
        elif check+a[i]>0:
            check=-1
            ans=1
            break
        else:
            check=1
            ans=1
            break

for i in range(1,n):
    check2=check+a[i]

    if check<0:
        if check2>0:
            check=check2
        else:
            ans+=(abs(check2)+1)
            check=1
    else:
        if check2<0:
            check=check2
        else:
            ans+=(abs(check2)+1)
            check=-1

print(ans)

Submission status: Wrong Answer

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

check=a[0]
ans=0
if check==0:
    for i in range(1,n):
        if check+a[i]==0:
            continue
            
        elif check+a[i]>0:
            check=-1
            ans=1
            break
        else:
            check=1
            ans=1
            break

for i in range(1,n):
    check2=check+a[i]

    if check<0:
        if check2>0:
            check=check2
        else:
            ans+=abs(check2)+1
            check=1
    else:
        if check2<0:
            check=check2
        else:
            ans+=abs(check2)+1
            check=-1

print(ans)

Submission status: Wrong Answer

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

check=a[0]
ans=0
if check==0:
    for i in range(1,n):
        if check+a[i]==0:
            continue
            
        elif check+a[i]>0:
            check=-1
            ans=1
            break
        else:
            check=1
            ans=1
            break

for i in range(1,n):
    check2=check+a[i]
    if check<0:
        if check2>0:
            check=check2
        else:
            ans+=abs(check2+1)
            check=1
    else:
        if check2<0:
            check=check2
        else:
            ans+=abs(check2+1)
            check=-1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int,input().split()))
ans1 = A[0]
ans = ans1
A = A[1:]
cnt = 0
m = 0
for a in A:
  m += 1
  if ans1 > 0:
    ans += a
    if m % 2 == 1 and ans >= 0:
      cnt += (abs(ans)+1)
      ans -= (abs(ans)+1)
    elif m % 2 == 0 and ans <= 0:
      cnt += (abs(ans)+1)
      ans += (abs(ans)+1)
  else:
    ans += a
    if m % 2 == 1 and ans <= 0:
      cnt += (abs(ans)+1)
      ans += (abs(ans)+1)
    elif m % 2 == 0 and ans >= 0:
      cnt += (abs(ans)+1)
      ans -= (abs(ans)+1)
print(cnt)

Submission status: Wrong Answer

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

sum_a = list_a[0]
count_t = 0
for one_a in list_a[1:]:
  if sum_a > 0:
    if sum_a + one_a < 0:
      sum_a += one_a
    else:
      count_t += abs(sum_a) + one_a + 1
      sum_a = -1
  else:
    if sum_a + one_a > 0:
      sum_a += one_a
    else:
      count_t += abs(sum_a) - one_a + 1
      sum_a = 1

sum_a = list_a[0]
count_f = 0
for one_a in list_a[1:]:
  if sum_a > 0:
    if sum_a + one_a < 0:
      sum_a += one_a
    else:
      count_f += abs(sum_a) + one_a + 1
      sum_a = -1
  else:
    if sum_a + one_a > 0:
      sum_a += one_a
    else:
      count_f += abs(sum_a) - one_a + 1
      sum_a = 1
    
print(min(count_t, count_f))

Submission status: Wrong Answer

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

sum_a = list_a[0]
count_t = 0
if sum_a < 0:
  count_t += 1 + abs(sum_a)
for i in range(1, n):
  if sum_a > 0:
    if sum_a + list_a[i] >= 0:
      diff = abs(-1 - list_a[i] - sum_a)
    else:
      diff = 0
    sum_a = sum_a + list_a[i] - diff
    count_t += diff
  else:
    if sum_a + list_a[i] <= 0:
      diff = abs(1 - sum_a - list_a[i])
    else:
      diff = 0
    sum_a = sum_a + list_a[i] + diff
    count_t += diff

sum_a = list_a[0]
count_f = 0
if sum_a > 0:
  count_f += 1 + abs(sum_a)
for i in range(1, n):
  if sum_a > 0:
    if sum_a + list_a[i] >= 0:
      diff = abs(-1 - list_a[i] - sum_a)
    else:
      diff = 0
    sum_a = sum_a + list_a[i] - diff
    count_f += diff
  else:
    if sum_a + list_a[i] <= 0:
      diff = abs(1 - sum_a - list_a[i])
    else:
      diff = 0
    sum_a = sum_a + list_a[i] + diff
    count_f += diff
    
print(min(count_t, count_f))

Submission status: Wrong Answer

Solution code:
n = int(input())
list_a = list(map(int, input().split()))
sum_a = list_a[0]

count = 0
for i in range(1, n):
  if sum_a > 0:
    if sum_a + list_a[i] >= 0:
      diff = abs(-1 - list_a[i] - sum_a)
    else:
      diff = 0
    sum_a = sum_a + list_a[i] - diff
    count += diff
  else:
    if sum_a + list_a[i] <= 0:
      diff = abs(1 - sum_a - list_a[i])
    else:
      diff = 0
    sum_a = sum_a + list_a[i] + diff
    count += diff
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int, input().split()))
last_sum = l[0]
ans_l = []
for j in range(2):
    ans = 0
    if j == 0:
        if last_sum <= 0:
            ans += 1 - last_sum
            last_sum += 1 - last_sum
    else:
        if last_sum >= 0:
            ans += -1 - last_sum
            last_sum += -1 - last_sum
    for i in range(n - 1):
        # print(last_sum)
        if last_sum > 0:
            if last_sum + l[i + 1] < 0:
                last_sum += l[i + 1]
            else:
                a = -1 - last_sum - l[i + 1]
                ans += abs(a)
                last_sum += a + l[i + 1]
        else:
            if last_sum + l[i + 1] > 0:
                last_sum += l[i + 1]
            else:
                a = 1 - last_sum - l[i + 1]
                ans += abs(a)
                last_sum += a + l[i + 1]
    ans_l.append(ans)
print(min(ans_l))

Submission status: Wrong Answer

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

ans = 0
if a[0] > 0:
    op = "+"
elif a[0] < 0:
    op = "-"
elif a[0] == 0:
    ans += 1
    if a[1] > 0:
        a[0] = -1
        op = "-"
    else:
        a[0] = 1
        op = "+"

total = a[0]
for i in range(1, n):
    if (total+a[i]) > 0 and op == "+":
        ans += abs((-1 - total) - a[i])
        total = -1
        op = "-"
    elif (total+a[i]) < 0 and op == "-":
        ans += abs((1 - total) - a[i])
        total = 1
        op = "+"
    elif total+a[i] == 0:
        ans += 1
        if op == "+":
            total = -1
            op = "-"
        else:
            total = 1
            op = "+"
    else:
        total += a[i]
        if op == "+":
            op = "-"
        else:
            op = "+"
print(ans)

Submission status: Wrong Answer

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

ans = 0
if a[0] > 0:
    op = "+"
elif a[0] < 0:
    op = "-"
elif a[0] == 0:
    ans += 1
    if a[1] > 0:
        a[0] = -1
        op = "-"
    else:
        a[0] = 1
        op = "+"

total = a[0]
for i in range(1, n):
    if (total+a[i]) >= 0 and op == "+":
        ans += abs((-1 - total) - a[i])
        total = -1
        op = "-"
    elif (total+a[i]) < 0 and op == "-":
        ans += abs((1 - total) - a[i])
        total = 1
        op = "+"
    elif total+a[i] == 0:
        ans += 1
        if op == "+":
            total = -1
            op = "-"
        else:
            total = 1
            op = "+"
    else:
        total += a[i]
        if op == "+":
            op = "-"
        else:
            op = "+"
print(ans)