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 sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect
from heapq import heappush, heappop

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n = INT()
a = LIST()
if a[0] != 0:
	tmp_sum = a[0]
	ans = 0
else:
	if a[1] == 0:
		tmp_sum = 1
	else:
		tmp_sum = -1
	ans = 1
a = a[1:]
for x in a:
	if (tmp_sum + x) * tmp_sum < 0:  # 異符号
		tmp_sum += x
	else:
		 if tmp_sum > 0:
		 	ans += abs(-tmp_sum-1-x)
		 	tmp_sum = -1
		 else:
		 	ans += abs(-tmp_sum+1-x)
		 	tmp_sum = 1
print(ans)

Submission status: Wrong Answer

Solution code:
import  copy
n = int(input())
a = list(map(int, input().split()))
check = copy.deepcopy(a)
#####segfunc######
def segfunc(x,y):
    return x+y

def init(init_val):
    #set_val
    for i in range(len(init_val)):
        seg[i+num-1]=init_val[i]   
    #built
    for i in range(num-2,-1,-1) :
        seg[i]=segfunc(seg[2*i+1],seg[2*i+2]) 
    
def update(k,x):
    k += num-1
    seg[k] = x
    while k:
        k = (k-1)//2
        seg[k] = segfunc(seg[k*2+1],seg[k*2+2])
    
def query(p,q):
    if q<=p:
        return ide_ele
    p += num-1
    q += num-2
    res=ide_ele
    while q-p>1:
        if p&1 == 0:
            res = segfunc(res,seg[p])
        if q&1 == 1:
            res = segfunc(res,seg[q])
            q -= 1
        p = p//2
        q = (q-1)//2
    if p == q:
        res = segfunc(res,seg[p])
    else:
        res = segfunc(segfunc(res,seg[p]),seg[q])
    return res

#####単位元######
ide_ele = 0
num =2**(n-1).bit_length()
seg=[ide_ele]*(2*num - 1)
init(a)
ans_1 = 0
pre_sum = (-1) * a[0]
for i in range(n):
    q_sum = query(0,i+1)
    if q_sum * pre_sum >= 0:
        if pre_sum < 0:
            update(i, abs(pre_sum) + 1)
        else:
            update(i, (-1) * (pre_sum+1))
    pre_sum = query(0,i+1)
for i in range(n):
    ans_1 += abs(check[i] - seg[i + num - 1])
if a[0] == 0:
    a[0] = 1
else:
    a[0] *= -1
ans_2 = 0
pre_sum = (-1) * a[0]
init(a)
for i in range(n):
    q_sum = query(0,i+1)
    if q_sum * pre_sum >= 0:
        if pre_sum < 0:
            update(i, abs(pre_sum) + 1)
        else:
            update(i, (-1) * (pre_sum+1))
    pre_sum = query(0,i+1)
for i in range(n):
    ans_2 += abs(check[i] - seg[i + num - 1])
print(min(ans_1,ans_2))

Submission status: Wrong Answer

Solution code:
import  copy
n = int(input())
a = list(map(int, input().split()))
check = copy.deepcopy(a)
#####segfunc######
def segfunc(x,y):
    return x+y

def init(init_val):
    #set_val
    for i in range(len(init_val)):
        seg[i+num-1]=init_val[i]   
    #built
    for i in range(num-2,-1,-1) :
        seg[i]=segfunc(seg[2*i+1],seg[2*i+2]) 
    
def update(k,x):
    k += num-1
    seg[k] = x
    while k:
        k = (k-1)//2
        seg[k] = segfunc(seg[k*2+1],seg[k*2+2])
    
def query(p,q):
    if q<=p:
        return ide_ele
    p += num-1
    q += num-2
    res=ide_ele
    while q-p>1:
        if p&1 == 0:
            res = segfunc(res,seg[p])
        if q&1 == 1:
            res = segfunc(res,seg[q])
            q -= 1
        p = p//2
        q = (q-1)//2
    if p == q:
        res = segfunc(res,seg[p])
    else:
        res = segfunc(segfunc(res,seg[p]),seg[q])
    return res

#####単位元######
ide_ele = 0
num =2**(n-1).bit_length()
seg=[ide_ele]*(2*num - 1)
init(a)
ans_1 = 0
pre_sum = (-1) * a[0]
for i in range(n):
    q_sum = query(0,i+1)
    if q_sum * pre_sum >= 0:
        if pre_sum < 0:
            update(i, abs(pre_sum) + 1)
        else:
            update(i, (-1) * (pre_sum+1))
    pre_sum = query(0,i+1)
for i in range(n):
    ans_1 += abs(check[i] - seg[i + num - 1])
if a[0] == 0:
    a[0] = 1
    ans_2 = 1
else:
    ans_2 = abs(a[0] * 2)
    a[0] *= -1
pre_sum = (-1) * a[0]
init(a)
for i in range(n):
    q_sum = query(0,i+1)
    if q_sum * pre_sum >= 0:
        if pre_sum < 0:
            update(i, abs(pre_sum) + 1)
        else:
            update(i, (-1) * (pre_sum+1))
    pre_sum = query(0,i+1)
for i in range(n):
    ans_2 += abs(check[i] - seg[i + num - 1])
print(min(ans_1,ans_2))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
#####segfunc######
def segfunc(x,y):
    return x+y

def init(init_val):
    #set_val
    for i in range(len(init_val)):
        seg[i+num-1]=init_val[i]   
    #built
    for i in range(num-2,-1,-1) :
        seg[i]=segfunc(seg[2*i+1],seg[2*i+2]) 
    
def update(k,x):
    k += num-1
    seg[k] = x
    while k:
        k = (k-1)//2
        seg[k] = segfunc(seg[k*2+1],seg[k*2+2])
    
def query(p,q):
    if q<=p:
        return ide_ele
    p += num-1
    q += num-2
    res=ide_ele
    while q-p>1:
        if p&1 == 0:
            res = segfunc(res,seg[p])
        if q&1 == 1:
            res = segfunc(res,seg[q])
            q -= 1
        p = p//2
        q = (q-1)//2
    if p == q:
        res = segfunc(res,seg[p])
    else:
        res = segfunc(segfunc(res,seg[p]),seg[q])
    return res

#####単位元######
ide_ele = 0
num =2**(n-1).bit_length()
seg=[ide_ele]*(2*num - 1)
init(a)
ans_1 = 0
pre_sum = (-1) * a[0]
for i in range(n):
    q_sum = query(0,i+1)
    if q_sum * pre_sum >= 0:
        if pre_sum < 0:
            update(i, abs(pre_sum) + 1)
        else:
            update(i, (-1) * (pre_sum+1))
    pre_sum = query(0,i+1)
for i in range(n):
    ans_1 += abs(a[i] - seg[i + num - 1])
if a[0] == 0:
    a[0] = 1
    ans_2 = 1
else:
    ans_2 = abs(a[0] * 2)
    a[0] *= -1
pre_sum = (-1) * a[0]
init(a)
for i in range(n):
    q_sum = query(0,i+1)
    if q_sum * pre_sum >= 0:
        if pre_sum < 0:
            update(i, abs(pre_sum) + 1)
        else:
            update(i, (-1) * (pre_sum+1))
    pre_sum = query(0,i+1)
for i in range(n):
    ans_2 += abs(a[i] - seg[i + num - 1])
print(min(ans_1,ans_2))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
#####segfunc######
def segfunc(x,y):
    return x+y

def init(init_val):
    #set_val
    for i in range(len(init_val)):
        seg[i+num-1]=init_val[i]   
    #built
    for i in range(num-2,-1,-1) :
        seg[i]=segfunc(seg[2*i+1],seg[2*i+2]) 
    
def update(k,x):
    k += num-1
    seg[k] = x
    while k:
        k = (k-1)//2
        seg[k] = segfunc(seg[k*2+1],seg[k*2+2])
    
def query(p,q):
    if q<=p:
        return ide_ele
    p += num-1
    q += num-2
    res=ide_ele
    while q-p>1:
        if p&1 == 0:
            res = segfunc(res,seg[p])
        if q&1 == 1:
            res = segfunc(res,seg[q])
            q -= 1
        p = p//2
        q = (q-1)//2
    if p == q:
        res = segfunc(res,seg[p])
    else:
        res = segfunc(segfunc(res,seg[p]),seg[q])
    return res

#####単位元######
ide_ele = 0
num =2**(n-1).bit_length()
seg=[ide_ele]*(2*num - 1)
init(a)
ans = 0
pre_sum = (-1) * a[0]
for i in range(n):
    q_sum = query(0,i+1)
    if q_sum * pre_sum >= 0:
        if pre_sum < 0:
            update(i, abs(pre_sum) + 1)
        else:
            update(i, (-1) * (pre_sum+1))
    pre_sum = query(0,i+1)
for i in range(n):
    ans += abs(a[i] - seg[i + num - 1])
    a[i] = seg[i+num-1]
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n = int(input())
a = list(map(int,input().split()))
cnt = 0
current_sum = a[0]#現在の操作後の値
for i in range(n-1):
    if current_sum * (current_sum + a[i+1]) < 0:#符号が違う
        current_sum += a[i+1]
        cnt += 0#そのまま
    elif current_sum < 0:
        cnt += 1 - (current_sum + a[i+1])
        current_sum = 1 
    else:
        cnt += (current_sum + a[i+1]) +1 
        current_sum = -1
print(cnt)

Submission status: Wrong Answer

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

#前から貪欲でよいか
#a[0]を正か負に定めて貪欲

#a[0]が正と仮定してよい
if a[0]<0:
    for i in range(n):
        a[i]*=-1

#場合分け用
a2 = [a[i] for i in range(n)]


ans1 = 0
if not a[0]:
    ans1 += 1
    a[0] = 1
sm = a[0]

for i in range(1,n):
    sm2 = sm + a[i]
    #print(sm,sm2)
    if sm2*sm>0:
        if sm<0:#sm+a[i]=1
            ans1 += abs((1-sm)-a[i])
            a[i]=1-sm
            sm = 1
            
        else:#sm+a[i] = -1
            ans1 += abs(-1-sm-a[i])
            a[i]=-1-sm
            sm = -1
    else:
        sm=sm2

ans2 = abs(a2[0]+1)
a2[0]=-1
sm = -1

for i in range(1,n):
    sm2 = sm + a2[i]
    if sm2*sm>0:
        if sm<0:#sm+a[i]=1
            ans2 += abs((1-sm)-a[i])
            a2[i] = 1-sm
            sm = 1
        else:#sm+a[i] = -1
            ans2 += abs(-1-sm-a[i])
            a2[i]=-1-sm
            sm = -1
    else:
        sm =sm2
   

print(min(ans1,ans2))

Submission status: Wrong Answer

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

#前から貪欲でよいか
#a[0]を正か負に定めて貪欲

#a[0]が正と仮定してよい
if a[0]<0:
    for i in range(n):
        a[i]*=-1

#場合分け用
a2 = [a[i] for i in range(n)]


ans1 = 0
if not a[0]:
    ans1 += 1
    a[0] = 1
sm = a[0]

for i in range(1,n):
    sm2 = sm + a[i]
    #print(sm,sm2)
    if sm2*sm>=0:
       
        if sm<0:#sm+a[i]=1
            ans1 += abs((1-sm)-a[i])
            a[i]=1-sm
            sm = 1
            
        else:#sm+a[i] = -1
            ans1 += abs(-1-sm-a[i])
            a[i]=-1-sm
            sm = -1
    else:
        sm=sm2
    #print(a)
    """
    else:
        if sm>0:#sm+a[i]=-1
            ans1 += -1-sm-a[i]
            sm = -1
        else:#sm+a[i]=1
            ans1 += 1-sm-a[i] 
    """

ans2 = abs(a2[0]+1)
a2[0]=-1
sm = -1

for i in range(1,n):
    sm2 = sm + a2[i]
    #print(sm,sm2)
    if sm2*sm>=0:
        if sm<0:#sm+a[i]=1
            ans2 += abs((1-sm)-a[i])
            a2[i] = 1-sm
            sm = 1
        else:#sm+a[i] = -1
            ans2 += abs(-1-sm-a[i])
            a2[i]=-1-sm
            sm = -1
    else:
        sm =sm2
    #print(a2)
    """
    else:
        if sm>0:#sm+a[i]=-1
            ans2 += -1-sm-a[i]
            sm = -1
        else:#sm+a[i]=1
            ans2 += 1-sm-a[i]
    """

#print(ans1,ans2)
print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
# input = sys.stdin.readline
from bisect import *
from collections import *
from heapq import *
# import functools
# import itertools
# import math

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

def main(c,temp):
    if temp>0:
        flag=1
    elif temp<0:
        flag=0
    for i in range(1,n):
        temp+=A[i]
        if flag:
            if temp>=0:
                c+=temp-(-1)
                temp=-1
            flag=0
        else:
            if temp<=0:
                c+=1-temp
                temp=1
            flag=1
    return(c)

temp=A[0]
if temp:
    print(main(0,temp))
else:
    temp=1
    a=main(1,temp)
    temp=-1
    b=main(1,temp)
    print(min(a,b))

Submission status: Wrong Answer

Solution code:
n = int(input())

nList = list(map(int,input().split()))


gokei = nList[0]
count1 = 0
for i in range(1,n):
  gokei_tmp  =gokei+ nList[i]
  if (gokei < 0 and gokei_tmp > 0) or (gokei > 0 and gokei_tmp < 0):
    gokei = gokei_tmp
  else:
      count1 += abs(gokei_tmp)
      gokei_tmp -= gokei_tmp
      if gokei > 0:
        gokei_tmp -= 1
      else: 
        gokei_tmp += 1
      count1 += 1
      gokei = gokei_tmp
gokei = nList[0]
count2 = abs(gokei)
if nList[0] > 0:
  gokei -= gokei -1
else:
  gokei -= gokei +1
count2 += 1

for i in range(1,n):
  gokei_tmp  =gokei+ nList[i]
  if (gokei < 0 and gokei_tmp > 0) or (gokei > 0 and gokei_tmp < 0):
    gokei = gokei_tmp
  else:
      count2 += abs(gokei_tmp)
      gokei_tmp -= gokei_tmp
      if gokei > 0:
        gokei_tmp -= 1
      else: 
        gokei_tmp += 1
      count2 += 1
      gokei = gokei_tmp
 
if count1 >= count2: 
  print(count2)
else:
  print(count1)

Submission status: Wrong Answer

Solution code:
n = int(input())

nList = list(map(int,input().split()))


gokei = nList[0]
count = 0
for i in range(1,n):
  gokei_tmp  =gokei+ nList[i]
  if (gokei < 0 and gokei_tmp > 0) or (gokei > 0 and gokei_tmp < 0):
    gokei = gokei_tmp
  else:
      count += abs(gokei_tmp)
      gokei_tmp -= gokei_tmp
      if gokei > 0:
        gokei_tmp -= 1
      else: 
        gokei_tmp += 1
      count += 1
      gokei = gokei_tmp
  
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())

numbers = list(map(int, input().split()))
counter = 0
sum_in = 0
sum_in1 = numbers[0]
key = numbers[0]

if key >= 0:
    for i in range(len(numbers)):
        sum_in += numbers[i]
        if i % 2 == 0:
            if sum_in <= 0:
                sub = abs(sum_in) + 1
                sum_in += sub
                numbers[i] += sub
                counter += sub
        else:
            if sum_in >= 0:
                sub = abs(sum_in) + 1
                sum_in -= sub
                numbers[i] -= sub
                counter += sub

else:
    for i in range(len(numbers)):
        sum_in += numbers[i]

        if i % 2 != 0:
            if sum_in <= 0:
                sub = abs(sum_in) + 1
                sum_in += sub
                numbers[i] += sub
                counter += sub
        else:
            if sum_in >= 0:
                sub = abs(sum_in) + 1
                sum_in -= sub
                numbers[i] -= sub
                counter += sub

print(counter)

Submission status: Wrong Answer

Solution code:
n = int(input())

numbers = list(map(int, input().split()))
counter = 0
sum_in = 0
sum_in1 = numbers[0]

if numbers[0] >= 0:
    for i in range(len(numbers)):
        sum_in += numbers[i]
        if i % 2 == 0:
            if sum_in <= 0:
                sub = abs(sum_in) + 1
                sum_in += sub
                numbers[i] += sub
                counter += sub
        else:
            if sum_in >= 0:
                sub = abs(sum_in) + 1
                sum_in -= sub
                numbers[i] -= sub
                counter += sub

else:
    for i in range(len(numbers)):
        sum_in += numbers[i]

        if i % 2 != 0:
            if sum_in <= 0:
                sub = abs(sum_in) + 1
                sum_in += sub
                numbers[i] += sub
                counter += sub
        else:
            if sum_in >= 0:
                sub = abs(sum_in) + 1
                sum_in -= sub
                numbers[i] -= sub
                counter += sub

print(str(counter))

Submission status: Wrong Answer

Solution code:
n = int(input())

numbers = list(map(int, input().split()))
counter = 0
sum_in = 0
sum_in1 = numbers[0]

if numbers[0] >= 0:
    for i in range(len(numbers)):
        sum_in += numbers[i]
        if i % 2 == 0:
            if sum_in <= 0:
                sub = abs(sum_in) + 1
                sum_in += sub
                numbers[i] += sub
                counter += sub
        else:
            if sum_in >= 0:
                sub = abs(sum_in) + 1
                sum_in -= sub
                numbers[i] -= sub
                counter += sub

else:
    for i in range(len(numbers)):
        sum_in += numbers[i]

        if i % 2 != 0:
            if sum_in <= 0:
                sub = abs(sum_in) + 1
                sum_in += sub
                numbers[i] += sub
                counter += sub
        else:
            if sum_in >= 0:
                sub = abs(sum_in) + 1
                sum_in -= sub
                numbers[i] -= sub
                counter += sub

print(counter)

Submission status: Wrong Answer

Solution code:
n = int(input())

numbers = list(map(int, input().split()))
counter = 0
sum_i_n = 0
sum_i_n_1 = numbers[0]

for i in range(len(numbers) - 1):
    sum_i_n += numbers[i]
    sum_i_n_1 += numbers[i + 1]

    if sum_i_n == 0:
        numbers[i] += 1
        sum_i_n += 1
        sum_i_n_1 += 1
        counter += 1
    if sum_i_n * sum_i_n_1 > 0:
        sub_n = abs(sum_i_n)
        sub_n1 = abs(sum_i_n_1)
        if abs(sum_i_n) - abs(sum_i_n_1) > 0:
            if sum_i_n_1 > 0:
                numbers[i + 1] -= (sub_n1 + 1)
                sum_i_n_1 -= (sub_n1 + 1)
                counter += (sub_n1 + 1)
            else:
                numbers[i + 1] += (sub_n1 + 1)
                sum_i_n_1 += (sub_n1 + 1)
                counter += (sub_n1 + 1)
        else:
            if sum_i_n > 0:
                numbers[i] -= (sub_n + 1)
                sum_i_n -= (sub_n + 1)
                sum_i_n_1 -= (sub_n + 1)
                counter += (sub_n + 1)
            else:
                numbers[i] += (sub_n + 1)
                sum_i_n += (sub_n + 1)
                sum_i_n_1 += (sub_n + 1)
                counter += (sub_n + 1)
#        sub = abs(sum_i_n_1) + 1
#        counter += sub
#        if sum_i_n_1 > 0:
#            numbers[i + 1] -= sub
#            sum_i_n_1 -= sub
#        else:
#            numbers[i + 1] += sub
#            sum_i_n_1 += sub

if sum_i_n_1 == 0:
    counter += 1

print(counter)

Submission status: Wrong Answer

Solution code:
n = int(input())

numbers = list(map(int, input().split()))
counter = 0
sum_i_n = 0
sum_i_n_1 = numbers[0]

for i in range(len(numbers) - 1):
    # sum_i_n = sum(numbers[:i + 1])
    # sum_i_n_1 = sum(numbers[:i + 2])
    sum_i_n += numbers[i]
    sum_i_n_1 += numbers[i + 1]

    if sum_i_n == 0:
        numbers[i] += 1
        sum_i_n += 1
        sum_i_n_1 += 1
        counter += 1
    if sum_i_n * sum_i_n_1 >= 0:
        sub = abs(sum_i_n_1) + 1
        counter += sub
        if sum_i_n_1 > 0:
            numbers[i + 1] -= sub
            sum_i_n_1 -= sub
        else:
            numbers[i + 1] += sub
            sum_i_n_1 += sub

if sum_i_n_1 == 0:
    counter += 1

print(counter)

Submission status: Wrong Answer

Solution code:
n = int(input())

numbers = list(map(int, input().split()))
counter = 0
sum_i_n = 0
sum_i_n_1 = numbers[0]

for i in range(len(numbers) - 1):
    # sum_i_n = sum(numbers[:i + 1])
    # sum_i_n_1 = sum(numbers[:i + 2])
    sum_i_n += numbers[i]
    sum_i_n_1 += numbers[i + 1]

    if sum_i_n == 0:
        numbers[i] += 1
        sum_i_n += 1
        sum_i_n_1 += 1
        counter += 1
    if sum_i_n * sum_i_n_1 > 0:
        sub = abs(sum_i_n_1) + 1
        counter += sub
        if sum_i_n_1 > 0:
            numbers[i + 1] -= sub
            sum_i_n_1 -= sub
        else:
            numbers[i + 1] += sub
            sum_i_n_1 += sub

if sum_i_n_1 == 0:
    counter += 1

print(counter)

Submission status: Wrong Answer

Solution code:
n = int(input())

numbers = list(map(int, input().split()))
counter = 0
sum_i_n = 0
sum_i_n_1 = numbers[0]

for i in range(len(numbers) - 1):
    # sum_i_n = sum(numbers[:i + 1])
    # sum_i_n_1 = sum(numbers[:i + 2])
    sum_i_n += numbers[i]
    sum_i_n_1 += numbers[i + 1]

    if sum_i_n == 0:
        numbers[i] += 1
        sum_i_n += 1
        sum_i_n_1 += 1
        counter += 1
    if sum_i_n * sum_i_n_1 > 0:
        sub = abs(sum_i_n_1) + 1
        counter += sub
        if sum_i_n_1 > 0:
            numbers[i + 1] -= sub
            sum_i_n_1 -= sub
        else:
            numbers[i + 1] += sub
            sum_i_n_1 += sub
if sum(numbers) == 0:
    counter += 1

print(counter)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n=II()
    aa=LI()
    cs=aa[0]
    ans=0
    for a in aa[1:]:
        if cs>0:
            if a>=-cs:
                ans+=abs(-cs-1-a)
                cs=-1
            else:cs+=a
        else:
            if a <= -cs:
                ans += abs(-cs + 1 - a)
                cs = 1
            else:
                cs += a
    print(ans)

main()

Submission status: Wrong Answer

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

cnt=0
for i in range(1,n):
    
    #print(a)
    now_tmp = sum(a[:i])
    next_tmp = now_tmp + a[i]
    #print(i, now_tmp, next_tmp)

    # 符号が逆転していればOK かつ 現在までの総和が0でない
    #  異なる符号を掛けるとマイナスになる
    if now_tmp * next_tmp <0 and now_tmp !=0:
        continue
    else:
        # 現在の合計がマイナスの場合
        if now_tmp < 0:
            a[i] += -next_tmp+1
            cnt +=abs(next_tmp+1)

        # 現在の合計がプラスの場合
        elif now_tmp > 0 :
            a[i] += -next_tmp-1
            cnt +=abs(next_tmp+1)
        # 現在の合計が0の場合
        elif now_tmp == 0 :
            # 1個前がプラスの場合、
            if sum(a[:i-1]) > 0:
                a[i] += -next_tmp+1
                cnt +=abs(next_tmp+1)
            # 1個前がマイナスの場合
            else:
                a[i] += -next_tmp+1
                cnt +=abs(next_tmp+1)
                
print(cnt)

Submission status: Wrong Answer

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

cnt=0
for i in range(1,n):
    
    # 条件満たすまでループ
    for _ in range(3):
        #print(a)
    
        now_tmp = sum(a[:i])
        next_tmp = sum(a[:i+1])
        #print(i, now_tmp, next_tmp)
        
        # 符号が逆転していればOK かつ 現在までの総和が0でない
        #  異なる符号を掛けるとマイナスになる
        if now_tmp * next_tmp <0 and now_tmp !=0:
            break
        else:
            # 現在の合計がマイナスの場合
            if now_tmp < 0:
                a[i] += -next_tmp+1
                cnt +=abs(next_tmp+1)

            # 現在の合計がプラスの場合
            elif now_tmp > 0 :
                a[i] += -next_tmp-1
                cnt +=abs(next_tmp+1)
            # 現在の合計が0の場合
            elif now_tmp == 0 :
                # 1個前がプラスの場合、
                if sum(a[:i-1]) > 0:
                    a[i] += -next_tmp+1
                    cnt +=abs(next_tmp+1)
                # 1個前がマイナスの場合
                else:
                    a[i] += -next_tmp+1
                    cnt +=abs(next_tmp+1)
                
print(cnt)

Submission status: Wrong Answer

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

cnt=0
for i in range(1,n):
    
    # 条件満たすまでループ
    for _ in range(3):
        #print(a)
    
        now_tmp = sum(a[:i])
        next_tmp = sum(a[:i+1])
        #print(i, now_tmp, next_tmp)
        
        # 符号が逆転していればOK かつ 現在までの総和が0でない
        #  異なる符号を掛けるとマイナスになる
        if now_tmp * next_tmp <0 and now_tmp !=0:
            break
        else:
            # 現在の合計がマイナスの場合
            if now_tmp < 0:
                a[i] += next_tmp+1
                cnt +=abs(next_tmp+1)

            # 現在の合計がプラスの場合
            elif now_tmp > 0 :
                a[i] += -next_tmp-1
                cnt +=abs(next_tmp+1)
            # 現在の合計が0の場合
            elif now_tmp == 0 :
                # 1個前がプラスの場合、
                if sum(a[:i-1]) > 0:
                    a[i] += -next_tmp+1
                    cnt +=abs(next_tmp+1)
                # 1個前がマイナスの場合
                else:
                    a[i] += next_tmp+1
                    cnt +=abs(next_tmp+1)
                
print(cnt)

Submission status: Wrong Answer

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

totals = [0] * N
totals[0] = A[0]
con = 0

if totals[0] == 0:
  for i in range(1,N):
    if A[i] != 0:
      f = A[i]
      if f > 0:
        totals[0] = -1
      else:
        totals[0] = 1
      break
  else:
    totals[0] = 1
  con += 1

for i in range(1,N):
  totals[i] = totals[i - 1] + A[i]
  if totals[i - 1] * totals[i] >= 0:

    if totals[i - 1] < 0:
      con += abs(1 - totals[i])
      totals[i] += abs(1 - totals[i])
    elif totals[i - 1] > 0:
      con += abs(-1 - totals[i])
      totals[i] -= abs(-1 - totals[i])
  
print(con)

Submission status: Wrong Answer

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

totals = [0] * N
totals[0] = A[0]
con = 0

for i in range(1,N):
  totals[i] = totals[i - 1] + A[i]
  #チェック
  ##符号が等しいか、ゼロなら操作
  if totals[i - 1] * totals[i] >= 0:
    if totals[i - 1] < 0:
      con += abs(1 - totals[i])
      totals[i] += abs(1 - totals[i])
    else:
      con += abs(-1 - totals[i])
      totals[i] -= abs(-1 - totals[i])
      
print(con)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
    N = I()
    A = LI()
    cumsum = [0]
    for i in range(N):
        cumsum.append(A[i] + cumsum[i])
    cumsum = cumsum[1:]
    prev_plus = cumsum[0] > 0
    cnt = 0
    total_sum = 0
    for i, c in enumerate(cumsum):
        if i == 0:
            continue
        c = c + total_sum
        if prev_plus:
            # if minus, ignore
            if c < 0:
                pass
            else:
                cnt += (c - (-1))
                total_sum -= (c - (-1))
        else:
            if c > 0:
                pass
            else:
                cnt += (1 - c)
                total_sum += (1 - c)
        prev_plus = not prev_plus
    print(cnt)
main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

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

b = a[:]
acc = 0

for i in range(n):
    if i%2==0:
        if acc+b[i]<=0:
            b[i] += 1-(acc+b[i])
    else:
        if acc+b[i]>=0:
            b[i] -= acc+b[i]+1
    
    acc += b[i]

c = a[:]
acc = 0

for i in range(n):
    if i%2==0:
        if acc+c[i]>=0:
            c[i] -= acc+c[i]+1
    else:
        if acc+b[i]<=0:
            c[i] += 1-(acc+c[i])
    
    acc += c[i]

ans = min(sum(abs(ai-bi) for ai, bi in zip(a, b)), sum(abs(ai-ci) for ai, ci in zip(a, c)))

print(ans)

Submission status: Wrong Answer

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

print(answer)

Submission status: Wrong Answer

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

def exec(sign):
    a = [x for x in arr]
    res = 0

    if a[0] == 0:
        a[0] = sign
        res += 1

    x = 0
    for i in range(n-1):
        x += a[i]
        tmp = sign - (x + a[i+1])
        if sign < 0:
            tmp = min(tmp, 0)
        else:
            tmp = max(tmp, 0)
        res += abs(tmp)
        a[i+1] += tmp
        sign *= (-1)
    return res

print(min(exec(1), exec(-1)))

Submission status: Wrong Answer

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

def exec(sign):
    a = [x for x in arr]
    res = 0

    if a[0] == 0:
        a[0] = sign * (-1)
        res += 1

    x = 0
    for i in range(n-1):
        x += a[i]
        tmp = sign - (x + a[i+1])
        if sign < 0:
            tmp = min(tmp, 0)
        else:
            tmp = max(tmp, 0)
        res += abs(tmp)
        a[i+1] += tmp
        sign *= (-1)
    return res

print(min(exec(1), exec(-1)))

Submission status: Wrong Answer

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

def exec(sign):
    a = [x for x in arr]
    res = 0

    if a[0] == 0:
        a[0] -= sign
        res += 1

    x = 0
    for i in range(n-1):
        x += a[i]
        tmp = sign - (x + a[i+1])
        if sign < 0:
            tmp = min(tmp, 0)
        else:
            tmp = max(tmp, 0)
        res += abs(tmp)
        a[i+1] += tmp
        sign *= (-1)
    return res

print(min(exec(1), exec(-1)))

Submission status: Wrong Answer

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

def exec(sign):
    a = [x for x in arr]
    res = 0

    if a[0] == 0:
        a[0] = 1
        res += 1

    x = 0
    for i in range(n-1):
        x += a[i]
        tmp = sign - (x + a[i+1])
        if sign < 0:
            tmp = min(tmp, 0)
        else:
            tmp = max(tmp, 0)
        res += abs(tmp)
        a[i+1] += tmp
        sign *= (-1)
    return res

print(min(exec(1), exec(-1)))

Submission status: Wrong Answer

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

for i in range(n-1):
    if a[i] != 0:
        if i == 0:
            break
        else:
            sign =  a[i] // abs(a[i])
            if i % 2 == 1:
                a[0] -= sign
            else:
                a[0] += sign

res = 0
x = 0
for i in range(n-1):
    x += a[i]
    sign = (x // abs(x)) * (-1)
    tmp = sign - (x + a[i+1])
    if sign < 0:
        tmp = min(tmp, 0)
    else:
        tmp = max(tmp, 0)
    res += abs(tmp)
    a[i+1] += tmp

print(res)