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 itertools

def main():
    N = int(input())
    A = tuple(map(int, input().split()))

    a = list(itertools.accumulate(A))
    even, odd = [], []
    e_apnd, o_apnd = even.append, odd.append
    for i, v in enumerate(a):
        if i % 2:
            o_apnd(v)
        else:
            e_apnd(v)

    if sum(even) > sum(odd):
        func = (plus, minus)
    elif sum(even) < sum(odd):
        func = (minus, plus)
    elif max(even) > max(odd):
        func = (plus, minus)
    else:
        func = (minus, plus)

    cnt = 0
    add_ = 0
    for i, v in enumerate(a):
        ret = func[i%2](v + add_)
        add_ += ret
        cnt += abs(ret)
    print(cnt)

def plus(n):
    if n <= 0:
        x = 1 - n
        return x
    else:
        return 0

def minus(n):
    if n >= 0:
        x = -1 - n
        return x
    else:
        return 0

main()

Submission status: Wrong Answer

Solution code:
import itertools

def main():
    N = int(input())
    A = tuple(map(int, input().split()))

    a = list(itertools.accumulate(A))
    even, odd = 0, 0
    for i, v in enumerate(a):
        if i % 2:
            odd += v
        else:
            even += v

    if even > odd:
        func = (plus, minus)
    else:
        func = (minus, plus)

    cnt = 0
    add_ = 0
    for i, v in enumerate(a):
        ret = func[i%2](v + add_)
        add_ += ret
        cnt += abs(ret)
    print(cnt)

def plus(n):
    if n <= 0:
        x = 1 - n
        return x
    else:
        return 0

def minus(n):
    if n >= 0:
        x = -1 - n
        return x
    else:
        return 0


main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def k(n, a, p):
    x = 0
    c = 0
    for i in range(n):
        s = x + a[i]
        if (s > 0) == p: x = s
        else:
            c += abs(s) + 1
            if p: x = 1
            else: x = -1
        p = not p
    return c

n = int(input())
a = [int(x) for x in input().split()]
print(min(k(n, a, True), k(n, a, False)))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
s = []
sign = []
debug_total = [0 for i in range(n)]
ans = 0
if a[0]==0:
    if a[1]<0:
        a[0]=1
    else:
        a[0]=-1
    ans+=1
if a[0]<0:
    sign.append(-1)
else:
    sign.append(1)
total = a[0]
debug_total[0]=total
op = 0
#print('total_in: ',total)
#print('ans_in: ',ans)
for i in range(1,n):
    total+=a[i]
    if total==0:
        sign.append(sign[i-1]*-1)
        total += sign[i-1]*-1
        a[i] += sign[i-1]*-1
        ans += 1
    elif total<0:
        sign.append(-1)
    else:
        sign.append(1)
    if sign[i]==sign[i-1]:
        op = abs(total)+1
        if total+op==0 or total-op==0:
            op+=1
        if sign[i]==1:
            total -= op
            a[i] -= op
        else:
            total += op
            a[i] += op
        ans += op
        sign[i] *= -1
    debug_total[i] = total
    #print('==========================')
    #print('i: ',i)
    #print('total: ',total)
    #print('ans: ',ans)
    #print('a: ',a)
    #print('total_list: ', debug_total)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
s = []
sign = []
debug_total = [0 for i in range(n)]
ans = 0
if a[0]==0:
    if a[1]<0:
        a[0]=1
    else:
        a[0]=-1
    ans+=1
if a[0]<0:
    sign.append(-1)
else:
    sign.append(1)
total = a[0]
debug_total[0]=total
op = 0
#print('total_in: ',total)
#print('ans_in: ',ans)
#print('a : ', a)
for i in range(1,n):
    total+=a[i]
    if total==0:
        sign.append(sign[i-1]*-1)
        total += sign[i-1]*-1
        a[i] += sign[i-1]*-1
        ans += 1
    elif total<0:
        sign.append(-1)
    else:
        sign.append(1)
    if sign[i]==sign[i-1]:
        op = abs(total)+1
        if total+op==0 or total-op==0:
            op+=1
        if sign[i]==1:
            total -= op
            a[i] -= op
        else:
            total += op
            a[i] += op
        ans += op
        sign[i] *= -1
    debug_total[i] = total
    #print('==========================')
    #print('i: ',i)
    #print('total: ',total)
    #print('ans: ',ans)
    #print('a: ',a)
    #print('total_list: ', debug_total)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
s = []
sign = []
ans = 0
if a[0]==0:
    if a[1]<0:
        a[0]=-1
    else:
        a[0]=1
    ans+=1
if a[0]<0:
    sign.append(-1)
else:
    sign.append(1)
total = a[0]
op = 0
for i in range(1,n):
    total+=a[i]
    if total==0:
        sign.append(sign[i-1]*-1)
        total += sign[i-1]*-1
        ans += 1
    elif total<0:
        sign.append(-1)
    else:
        sign.append(1)
    if sign[i]==sign[i-1]:
        op = abs(total)+1
        if total+op==0 or total-op==0:
            op+=1
        if sign[i]==1:
            total -= op
        else:
            total += op
        ans += op
        sign[i] *= -1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
num,answer1= a[0],0
if a[0]==0:
	num,answer = 1,1
for i in range(1,n):
	if num*(num+a[i]) >= 0:
		x = (-num)//abs(num)
		answer1 += abs((x-num)-a[i])
		num = x
	else:
		num += a[i]
if a[0]==0: num,answer2 = -1,1
else: num,answer2 = -a[0]//abs(a[0]),abs(a[0]-(-a[0]//abs(a[0])))
for i in range(1,n):
	if num*(num+a[i]) >= 0:
		x = (-num)//abs(num)
		answer2 += abs((x-num)-a[i])
		num = x
	else:
		num += a[i]
print(min(answer1,answer2))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
num,answer = a[0],0
if num==0:
	num = -a[1]//abs(a[1])
	answer = 1
for i in range(1,n):
	if num*(num+a[i]) >= 0:
		x = (-num)//abs(num)
		answer += abs((x-num)-a[i])
		num = x
	else:
		num += a[i]
print(answer)

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
lis = []
cou = 0
li = list(map(int,input().split()))
for i in range(n):
    if li[i] >= 3200:
        cou += 1
    else:
        if math.floor(li[i] / 400) not in lis:
            lis.append(li[i] // 400)
if len(lis) == 0:
    print(1,min(len(lis) + cou,8))
else:
    print(len(lis),min(8,len(lis) + cou))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


def is_positive(sumval,modify):
	if sumval <= 0:
		modify += abs(sumval - 1)
		sumval = 1

	return sumval,modify

def is_negative(sumval,modify):
	if sumval >= 0:
		modify += abs(sumval + 1)
		sumval = (-1)


	return sumval,modify

pos_sum,pos_modi = is_negative(a[0],0)
neg_sum,neg_modi = is_positive(a[0],0)

for i in range(1,N):

	tmp_sum,tmp_modi = is_positive(pos_sum + a[i],pos_modi)
	pos_sum,pos_modi = is_negative(neg_sum + a[i],neg_modi)
	neg_sum = neg_sum
	neg_modi = tmp_modi

print(min(pos_modi,neg_modi))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
https://abc059.contest.atcoder.jp/tasks/arc072_a
WA
"""
import sys
from sys import stdin
input = stdin.readline


def check_sign(n):
    if n > 0:
        return 1
    elif n < 0:
        return -1
    else:
        return 0


def solve(A):
    p_ans = 0
    n_ans = 0
    i_ans = 0

    # 最初をプラス側に振った場合の解
    total = A[0]
    prev_sign = check_sign(total)
    if prev_sign == 0:
        p_ans += 1
        total += 1
        prev_sign = 1

    for a in A[1:]:
        total += a
        sign = check_sign(total)
        if sign == 0:
            total -= prev_sign
            p_ans += 1
        elif prev_sign != sign:
            prev_sign = sign
        else:
            p_ans += (abs(total) + 1)
            if prev_sign < 0:
                total = 1
                prev_sign = 1
            else:
                total = -1
                prev_sign = -1

    # 最初をマイナス側に振った場合の解
    total = A[0]
    prev_sign = check_sign(total)
    if prev_sign == 0:
        n_ans += 1
        total -= 1
        prev_sign = -1

    for a in A[1:]:
        total += a
        sign = check_sign(total)
        if sign == 0:
            total -= prev_sign
            n_ans += 1
        elif prev_sign != sign:
            prev_sign = sign
        else:
            n_ans += (abs(total) + 1)
            if prev_sign < 0:
                total = 1
                prev_sign = 1
            else:
                total = -1
                prev_sign = -1

    #
    total = A[0]
    prev_sign = check_sign(total)
    if prev_sign == 0:
        i_ans += 1
        total += 1
        prev_sign = 1
    else:
        i_ans += (total + 1)
        if prev_sign > 0:
            prev_sign = -1
            total = -1
        else:
            prev_sign = 1
            total = 1

    for a in A[1:]:
        total += a
        sign = check_sign(total)
        if sign == 0:
            total -= prev_sign
            i_ans += 1
        elif prev_sign != sign:
            prev_sign = sign
        else:
            i_ans += (abs(total) + 1)
            if prev_sign < 0:
                total = 1
                prev_sign = 1
            else:
                total = -1
                prev_sign = -1

    return min(p_ans, n_ans, i_ans)


def main(args):
    n = int(input())
    A = [int(x) for x in input().split()]
    ans = solve(A)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
https://abc059.contest.atcoder.jp/tasks/arc072_a
WA
"""
import sys
from sys import stdin
input = stdin.readline


def check_sign(n):
    if n > 0:
        return 1
    elif n < 0:
        return -1
    else:
        return 0


def solve(A):
    p_ans = 0
    n_ans = 0

    # 最初をプラス側に振った場合の解
    total = A[0]
    prev_sign = check_sign(total)
    if prev_sign == 0:
        p_ans += 1
        total += 1
        prev_sign = 1

    for a in A[1:]:
        total += a
        sign = check_sign(total)
        if sign == 0:
            total -= prev_sign
            p_ans += 1
        elif prev_sign != sign:
            prev_sign = sign
        else:
            p_ans += (abs(total) + 1)
            if prev_sign < 0:
                total = 1
                prev_sign = 1
            else:
                total = -1
                prev_sign = -1

    # 最初をマイナス側に振った場合の解
    total = A[0]
    prev_sign = check_sign(total)
    if prev_sign == 0:
        n_ans += 1
        total -= 1
        prev_sign = -1

    for a in A[1:]:
        total += a
        sign = check_sign(total)
        if sign == 0:
            total -= prev_sign
            n_ans += 1
        elif prev_sign != sign:
            prev_sign = sign
        else:
            n_ans += (abs(total) + 1)
            if prev_sign < 0:
                total = 1
                prev_sign = 1
            else:
                total = -1
                prev_sign = -1
    return min(p_ans, n_ans)


def main(args):
    n = int(input())
    A = [int(x) for x in input().split()]
    ans = solve(A)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
https://abc059.contest.atcoder.jp/tasks/arc072_a

"""
import sys
from sys import stdin
input = stdin.readline


def check_sign(n):
    if n > 0:
        return 1
    elif n < 0:
        return -1
    else:
        return 0


def solve(A):
    p_ans = 0
    n_ans = 0

    total = A[0]
    prev_sign = check_sign(total)

    # 最初をプラス側に振った場合の解
    if prev_sign == 0:
        p_ans += 1
        total += 1
        prev_sign = 1

    for a in A[1:]:
        total += a
        sign = check_sign(total)
        if sign == 0:
            total -= prev_sign
            p_ans += 1
        elif prev_sign != sign:
            prev_sign = sign
        else:
            p_ans += (abs(total) + 1)
            if prev_sign < 0:
                total = 1
                prev_sign = 1
            else:
                total = -1
                prev_sign = -1

    # 最初をマイナス側に振った場合の解
    if prev_sign == 0:
        n_ans += 1
        total -= 1
        prev_sign = -1

    for a in A[1:]:
        total += a
        sign = check_sign(total)
        if sign == 0:
            total -= prev_sign
            n_ans += 1
        elif prev_sign != sign:
            prev_sign = sign
        else:
            n_ans += (abs(total) + 1)
            if prev_sign < 0:
                total = 1
                prev_sign = 1
            else:
                total = -1
                prev_sign = -1
    return min(p_ans, n_ans)


def main(args):
    n = int(input())
    A = [int(x) for x in input().split()]
    ans = solve(A)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
import math


"""
e.g. N=3 a=[-1 1 1]
 -1 2  1 :1
 -1 2 -2 :4

e.g. N=3 a=[0 0 -1]
 1  0 -1 :1
 1 -2 -1 :3
 1 -2  2 :6

 -1 0 -1 :1
 -1 2 -1 :3
 -1 2 -2 :4

e.g. N=3 a=[0 0 1]
 1  0 1 :1
 1 -2 1 :3
 1 -2 2 :4

 -1 0  1 :1
 -1 2  1 :3
 -1 2 -2 :6

e.g. N=4 a=[0 0 0 -1]
 1  0 0 -1 :1
 1 -2 0 -1 :3
 1 -2 2 -1 :5
 1 -2 2 -2 :6

 -1 0  0 -1 :1
 -1 2  0 -1 :3
 -1 2 -2 -1 :5
 -1 2 -2  2 :8

e.g. N=4 a=[0 0 0 1]
 1  0 0  1 :1
 1 -2 0  1 :3
 1 -2 2  1 :5
 1 -2 2 -2 :8

 -1 0  0 1 :1
 -1 2  0 1 :3
 -1 2 -2 1 :5
 -1 2 -2 2 :6
"""


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

    ans = 0

    # 最初から0続きのとき、操作が少なくなるように同じ符号にする
    def sign(x):
        if x == 0:
            return 0
        return int(math.copysign(1, x))

    if a[0] == 0:
        for i in range(1, n):
            if a[i] != 0:
                sign_a = sign(a[i])
                if i % 2 == 0:
                    # odd: 1-indexed
                    a[0] = sign_a
                else:
                    a[0] = -sign_a

                ans += 1
                break
    s = a[0]

    for i in range(1, n):
        s1 = s + a[i]
        sign_s = sign(s)
        sign_s1 = sign(s1)

        if s1 == 0 or sign_s == sign_s1:
            # 逆方向に変更
            # new_s1 = -sign_s = s+a[i] + diff
            diff = -(sign_s + s + a[i])
            a[i] += diff
            ans += abs(diff)

        s += a[i]

        #print(i, a, sep="\t")
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math

N = int(input())
a = [int(i) for i in input().split()]
num = 0
i = 0
if a[0] == 0:
    while a[i] == 0:
        i += 1
    if i % 2 == 0:
        a[0] = math.copysign(1, a[i])
    else:
        a[0] = math.copysign(1, -a[i])
    num += 1
old = a[0]
sam = a[0]
for n in range(2):
    num = 0
    if n != 0:
        sam = -a[0] - int(math.copysign(1, a[0]))
        old = sam
    for i in range(1, len(a)):
        sam += a[i]
        sam_sign = int(math.copysign(1, sam))
        old_sign = int(math.copysign(1, old))
        if sam_sign == old_sign or sam == 0:
            num += abs(sam) + 1
            a[i] = a[i] + (-old_sign)*(abs(sam)+1)
        old += a[i]
        sam = old
    if n == 0:
        num2 = num
print(min(num,num2))

Submission status: Wrong Answer

Solution code:
import math

N = int(input())
a = [int(i) for i in input().split()]
num = 0
i = 0
if a[0] == 0:
    while a[i] == 0:
        i += 1
    if i % 2 == 0:
        a[0] = math.copysign(1, a[i])
    else:
        a[0] = math.copysign(1, -a[i])
    num += 1
old = a[0]
sam = a[0]
for n in range(2):
    if n != 0:
        old = -a[0]
        sam = -a[0]
    for i in range(1, len(a)):
        sam += a[i]
        sam_sign = int(math.copysign(1, sam))
        old_sign = int(math.copysign(1, old))
        if sam_sign == old_sign or sam == 0:
            num += abs(sam) + 1
            a[i] = a[i] + (-old_sign)*(abs(sam)+1)
        old += a[i]
        sam = old
    if n == 0:
        num2 = num
print(min(num,num2))

Submission status: Wrong Answer

Solution code:
import math

N = int(input())
a = [int(i) for i in input().split()]
num = 0
i = 0
if a[0] == 0:
    while a[i] == 0:
        i += 1
    if i % 2 == 0:
        a[0] = math.copysign(1, a[i])
    else:
        a[0] = math.copysign(1, -a[i])
    num += 1
old = a[0]
sam = a[0]
for i in range(1, len(a)):
    sam += a[i]
    sam_sign = int(math.copysign(1, sam))
    old_sign = int(math.copysign(1, old))
    if sam_sign == old_sign or sam == 0:
        num += abs(sam) + 1
        a[i] = a[i] + (-old_sign)*(abs(sam)+1)
    old += a[i]
    sam = old
print(int(num))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
sam = a[0]
num = 0
if sam == 0 and a[1] > 0:
    sam -= 1
    num += 1
elif sam == 0:
    sam += 1
    num += 1
old = sam
for i in range(1, len(a)):
    sam += a[i]
    if sam >= 0 and old > 0:
        num += (abs(sam) + 1)
        sam -= (sam + 1)
    elif sam <= 0 and old < 0:
        num += (abs(sam) + 1)
        sam -= (sam - 1)
    old = sam
print(num)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
sam = a[0]
old = sam
num = 0
for i in range(1, len(a)):
    sam += a[i]
    if sam >= 0 and old > 0:
        num += (abs(sam) + 1)
        sam -= (sam + 1)
    elif sam <= 0 and old < 0:
        num += (abs(sam) + 1)
        sam -= (sam - 1)
    old = sam
print(num)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
sam = a[0]
old = sam
num = 0
for i in range(1, len(a)):
    sam += a[i]
    if sam >= 0 and old > 0:
        num += (abs(sam) + 1)
        sam -= (sam + 1)
    elif sam <= 0 and old < 0:
        num += (abs(sam) + 1)
        sam -= (sam + 1)
    old = sam
print(num)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
sam = a[0]
old = sam
num = 0
for i in range(1, len(a)):
    sam += a[i]
    if sam >= 0 and old > 0:
        num += (abs(sam) + 1)
        sam -= (sam + 1)
    elif sam <= 0 and old < 0:
        num += (abs(sam) + 1)
        sam -= (sam + 1)
    old = sam
    print(sam)
print(num)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
sam = a[0]
old = sam
num = 0
for i in range(1, len(a)):
    sam += a[i]
    if sam >= 0 and old > 0:
        num += (abs(sam) + 1)
        sam -= (-sam + 1)
    elif sam <= 0 and old < 0:
        num += (abs(sam) + 1)
        sam += (-sam + 1)
    old = sam
print(num)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
for num,i in enumerate(a):
    if i!=0:
        non0=num
        break
if non0==0:
    ans=0
elif non0==1:
    ans=2
else:
    ans=(non0-2)*2+2
Sum=a[non0]
if a[non0]>0:
    flag=+1
else:
    flag=-1
for i in range(non0+1,n):
    Sum+=a[i]
    if flag==1:
        if Sum>=0:
            ans+=Sum+1
            Sum=-1
        flag=-1
    elif flag==-1:
        if Sum<=0:
            ans+=1-Sum
            Sum=1
        flag=1
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict


def solve(pre_is_positive, total, ans, a_list):
    for a in a_list:
        total += a
        if pre_is_positive:
            if total == 0:
                ans += 1
                total = -1
            elif total > 0:
                ans += abs(total) + 1
                total = -1
        elif not pre_is_positive:
            if total == 0:
                ans += 1
                total = 1
            elif total < 0:
                ans += abs(total) + 1
                total = 1

        pre_is_positive = total > 0

    return ans


def main():
    n = int(input())
    a_list = list(map(int, input().split()))

    total = a_list[0]
    if total > 0:
        ans = solve(True, total, 0, a_list[1:])
        ans = min(ans, solve(False, -1, abs(total) + 1, a_list[1:]))
    else:
        ans = solve(False, total, 0, a_list[1:])
        ans = min(ans, solve(True, 1, abs(total) + 1, a_list[1:]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

#偶数番目を正とする(0,2,4,…)

#深いコピー
import copy
a_listg = copy.deepcopy(a_list)

a_sumg = 0
count_g = 0

for j in range(N):
    if j%2 != 0:
        if a_sumg + a_listg[j] > -1:
            temp = a_listg[j]
            a_listg[j] = -1 -a_sumg
            count_g += abs(temp - a_listg[j])
    else:
        if a_sumg + a_listg[j] < 1:
            temp = a_listg[j]
            a_listg[j] = 1 - a_sumg
            count_g += abs(temp - a_listg[j])

    a_sumg += a_listg[j]

#偶数番目を負とする(0,2,4,…)

#深いコピー
import copy
a_listk = copy.deepcopy(a_list)

a_sumk = 0
count_k = 0

for j in range(N):
    if j%2 == 0:
        if a_sumk + a_listk[j] > -1:
            temp = a_listk[j]
            a_listk[j] = -1 -a_sumg
            count_k += abs(temp - a_listk[j])
    else:
        if a_sumk + a_listk[j] < 1:
            temp = a_listk[j]
            a_listk[j] = 1 - a_sumg
            count_k += abs(temp - a_listk[j])

    a_sumk += a_listk[j]

print(min([count_g,count_k]))

Submission status: Wrong Answer

Solution code:
#入力部
N = int(input())
a_list = list(map(int,input().split()))
#偶数番目を正とする(0,2,4,…)

#深いコピー
import copy
a_listg = copy.deepcopy(a_list)

a_sumg = 0
xg = 0

for j in range(N):
    if j%2 != 0:
        if a_sumg + a_listg[j] > -1:
            temp = a_listg[j]
            a_listg[j] = -1 -a_sumg
            xg += abs(temp - a_listg[j])
    else:
        if a_sumg + a_listg[j] < 1:
            temp = a_listg[j]
            a_listg[j] = 1 - a_sumg
            xg += abs(temp - a_listg[j])

    a_sumg += a_listg[j]

#奇数番目を正とする(0,2,4,…)

#深いコピー
import copy
a_listk = copy.deepcopy(a_list)

a_sumk = 0
xk = 0

for j in range(N):
    if j%2 == 0:
        if a_sumk + a_listk[j] > -1:
            temp = a_listk[j]
            a_listk[j] = -1 -a_sumg
            xk += abs(temp - a_listk[j])
    else:
        if a_sumk + a_listk[j] < 1:
            temp = a_listk[j]
            a_listk[j] = 1 - a_sumg
            xk += abs(temp - a_listk[j])

    a_sumk += a_listk[j]

#2パターンの内小さい方を出力
print(min([xg,xk]))

Submission status: Wrong Answer

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

S = []
S.append(0)
cnt = 0

P =[]
x=0
y=0
for i in range(n):
    if a[i] != 0:
        if i%2 ==0:
            x=1
        else:
            x=-1
        if a[i] >0:
            y=1
        else:
            y=-1
        break

if x*y>=0:
    P.append(1)
else:
    P.append(-1)


for i in range(n-1):
    P.append(-P[i])


for i in range(n):
    if (S[i] + a[i])*P[i]<=0:
        cnt += abs(P[i]-S[i]-a[i])
        a[i] = P[i]-S[i]
    S.append(S[i]+a[i])

print(cnt)

Submission status: Wrong Answer

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

P =[]

if a[0]>0:
    P.append(1)
else:
    P.append(-1)

for i in range(n-1):
    P.append(-P[i])

S = []
S.append(a[0])
cnt = 0
for i in range(1,n):
    if (S[i-1] + a[i])*P[i]<=0:
        cnt += abs(P[i]-S[i-1]-a[i])
        a[i] = P[i]-S[i-1]
    S.append(S[i-1]+a[i])

print(cnt)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    N = input()
    array = map(int, raw_input().split(" "))

    ans = 0
    total = array[0]

    totalZero = False
    if total == 0:
        totalZero = True
    
    flag = False
    if total > 0:
        flag = True
        
    for a in array[1:]:

        if totalZero == True:
            ans += 1
            if a > 0:
                total = -1
            else:
                total = 1
            totalZero = False
            
        if total + a > 0 and total > 0:
            total += a
            ans += total + 1
            total = -1
        elif total + a < 0 and total < 0:
            total += a
            ans += abs(total - 1)
            total = 1              
        elif total + a == 0:
            totalZero = True            
        else:
            total += a

    if totalZero == True:
        ans += 1

    print ans

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':

    N = input()
    array = raw_input().split()

    ans = 0
    total = int(array[0])

    totalZero = False
    if total == 0:
        totalZero = True
    
    
    flag = False
    if total > 0:
        flag = True
        
    for a in array[1:]:

        if totalZero == True:
            ans += 1
            if a > 0:
                total = -1
                flag = False
            else:
                total = 1
                flag = True
            totalZero = False
            
        total += int(a)
        
        if total > 0 and flag == True:
            ans += total + 1
            total = -1
                
        elif total < 0 and flag ==False:
            ans += -1 * total + 1
            total = 1
                          
        elif total == 0:
            totalZero = True

        if total > 0:
            flag = True
        elif total < 0:
            flag = False

        
    if totalZero == True:
        ans += 1

    print ans

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':

    N = input()
    array = raw_input().split()

    ans = 0
    total = int(array[0])

    totalZero = False
    if total == 0:
        totalZero = True
    
    
    flag = False
    if total >= 0:
        flag = True
        
    for a in array[1:]:

        if totalZero == True:
            ans += 1
            if a > 0:
                total = -1
                flag = False
            else:
                total = 1
                flag = True
            totalZero = False
            
        total += int(a)
        
        if total > 0 and flag == True:
            ans += total + 1
            total = -1
                
        elif total < 0 and flag ==False:
            ans += -1 * total + 1
            total = 1
                          
        elif total == 0:
            totalZero = True

        if total > 0:
            flag = True
        elif total < 0:
            flag = False

        
    if totalZero == True:
        ans += 1

    print ans

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = [int(i) for i in input().split(' ')]
total = a_list[0]
pre_total = 0
counter = 0

if total == 0:
    total = -1*a_list[1]/abs(a_list[1])

for a in a_list[1:]:
    pre_total = total
    total += a
    if pre_total>0 and total>=0:
        counter += total+1
        total = -1
    elif pre_total<0 and total<=0:
        counter += abs(total)+1
        total = 1
    else:
        pass

print(counter)

Submission status: Wrong Answer

Solution code:
# nの把握
n = int(input())
# 数列の把握
AL = list(map(int, input().split()))

"""
条件１：すべてのi(1≦i≦n) に対し、第 1 項から第 i 項までの和は 0 でない
条件２：すべてのi(1≦i≦n−1) に対し、i 項までの和と i+1 項までの和の符号が異なる

条件２より、i項までの総和とi+1項までの総和は、正負又は負正という順番で進行する。
条件を満たさないとき、iについて、総和が1or-1となるまで手順を実施する。
"""

#正負を確かめる関数
def PN(AL):
	ISUM = 0
	P = 0
	for i in range(n):
		ISUM += AL[i]
		if i % 2 == 0:
			if ISUM > 0:
				pass
			else:
				P += abs(1 - ISUM)
				ISUM = 1
		else:
 			if ISUM < 0:
 				pass
 			else:
 				P += abs(-1 - ISUM)
 				ISUM = 1
	return P

#負正を確かめる関数
def NP(AL):
	ISUM = 0
	P = 0
	for i in range(n):
		ISUM += AL[i]
		if i % 2 == 0:
			if ISUM < 0:
				pass
			else:
				P += abs(-1 - ISUM)
				ISUM = -1
		else:
			if ISUM > 0:
 				pass
			else:
 				P += abs(1- ISUM)
 				ISUM = 1
	return P

ANSWER = min(PN(AL), NP(AL))
print(ANSWER)