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:
# -*- coding: utf-8 -*-

trial_num = int(input())
number_lists = list(map(int, input().split()))

def main():
    sum_num = 0
    change_num = 0

    if(number_lists[0] < 0):
        for i in range(trial_num):
            number_lists[i] = -1 * number_lists[i]

    for i in range(trial_num):

        if(i == 0):
            sum_num = number_lists[0]
            continue
        else:
           if(i%2 == 1):
               if(sum_num + number_lists[i] < 0):
                    sum_num += number_lists[i]
                    continue
               else:
                   change_num += abs(sum_num + number_lists[i]) + 1
                   sum_num = -1
                   continue
           else:
                if (sum_num + number_lists[i] > 0):
                    sum_num += number_lists[i]
                    continue
                else:
                    change_num += abs(sum_num + number_lists[i]) + 1
                    sum_num = 1
                    continue

    print(change_num)




if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
lstA = list(map(int, input().split()))
cntCase1, cntCase2 = 0, 0


# 偶数番目を正、奇数番目を負とする場合
accum = 0
for i in range(n):
    ai = lstA[i]
    tmpSum = accum
    tmpSum += ai
    if i % 2 == 0:
        if tmpSum < 0:
            cntCase1 += abs(tmpSum) + 1
            accum = 1
        else:
            accum = tmpSum
    else:
        if tmpSum > 0:
            cntCase1 += abs(tmpSum) + 1
            accum = -1
        else:
            accum = tmpSum


# 偶数番目を負、奇数番目を正とする場合
accum = 0
for i in range(n):
    ai = lstA[i]
    tmpSum = accum
    tmpSum += ai
    if i % 2 == 0:
        if tmpSum > 0:
            cntCase2 += abs(tmpSum) + 1
            accum = -1
        else:
            accum = tmpSum
    else:
        if tmpSum < 0:
            cntCase2 += abs(tmpSum) + 1
            accum = 1
        else:
            accum = tmpSum


ans = min(cntCase1, cntCase2)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

a = list(map(int, input().split()))
s = [a[0]]

for i in range(1, n):
    s.append(s[-1]+a[i])


cnt_a = 0
base = 0
for i in range(n):
    if (base + s[i] > 0) and (i % 2 == 1) or (base + s[i] < 0) and (i % 2 == 0):
        cnt_a += (1 + abs(base + s[i]))
        base += (1 + abs(base + s[i])) * (-1) ** i


cnt_b = 0
base = 0
for i in range(n):
    if (base + s[i] < 0) and (i % 2 == 1) or (base + s[i] > 0) and (i % 2 == 0):
        cnt_b += (1 + abs(base + s[i]))
        base -= (1 + abs(base + s[i])) * (-1) ** i

print(min(cnt_a, cnt_b))

Submission status: Wrong Answer

Solution code:
n = int(input())

a = list(map(int, input().split()))
s = [a[0]]

for i in range(1, n):
    s.append(s[-1]+a[i])


cnt_a = 0
base = 0
for i in range(n):
    if (base + s[i] > 0) != (i % 2 == 0):
        cnt_a += (1 + abs(base + s[i]))
        base += (1 + abs(base + s[i])) * (-1) ** i


cnt_b = 0
base = 0
for i in range(n):
    if (base + s[i] < 0) != (i % 2 == 0):
        cnt_b += (1 + abs(base + s[i]))
        base -= (1 + abs(base + s[i])) * (-1) ** i

print(min(cnt_a, cnt_b))

Submission status: Wrong Answer

Solution code:
n = int(input())

a = list(map(int, input().split()))
s = [a[0]]

for i in range(1, n):
    s.append(s[-1]+a[i])


cnt_a = 0
base = 0
for i in range(n):
    if (base + s[i] > 0) != (i % 2 == 0):
        cnt_a += (1 + abs(base + s[i]))
        base += (1 + abs(base + s[i])) * (-1) ** i


cnt_b = 0
base = 0
for i in range(n):
    if (base + s[i] > 0) == (i % 2 == 0):
        cnt_b += (1 + abs(base + s[i]))
        base -= (1 + abs(base + s[i])) * (-1) ** i

print(min(cnt_a, cnt_b))

Submission status: Wrong Answer

Solution code:

def read_input():
    n = int(input())
    alist = list(map(int, input().split()))
    return n, alist

def get_sign(x):
    if x > 0:
        return 1
    elif x < 0:
        return -1
    return 0

def submit():
    n, alist = read_input()

    # pattern 1
    s = alist[0]
    sign = get_sign(s)
    edit = 0

    if sign != 1:
        edit += 1 - s
        s = 1
        sign = get_sign(s)

    for a in alist[1:]:
        temp = s + a
        temp_sign = get_sign(temp)

        if sign == temp_sign:
            edit += temp_sign * temp
            temp -= temp

        if temp == 0:
            edit += 1
            temp -= sign

        s = temp
        sign = get_sign(s)

    edit1 = edit

    # pattern 2
    s = alist[0]
    sign = get_sign(s)
    edit = 0

    if sign != -1:
        edit += s + 1
        s = -1
        get_sign(s)

    for a in alist[1:]:
        temp = s + a
        temp_sign = get_sign(temp)

        if sign == temp_sign:
            edit += temp_sign * temp
            temp -= temp

        if temp == 0:
            edit += 1
            temp -= sign

        s = temp
        sign = get_sign(s)

    edit2 = edit

    print(min(edit1, edit2))


if __name__ == '__main__':
    submit()

Submission status: Wrong Answer

Solution code:

def read_input():
    n = int(input())
    alist = list(map(int, input().split()))
    return n, alist

def get_sign(x):
    if x > 0:
        return 1
    elif x < 0:
        return -1
    return 0

def submit():
    n, alist = read_input()

    # pattern 1
    s = alist[0]
    sign = get_sign(s)
    edit = 0
    for a in alist[1:]:
        temp = s + a
        temp_sign = get_sign(temp)

        if sign == temp_sign:
            edit += temp_sign * temp
            temp -= temp

        if temp == 0:
            edit += 1
            temp -= sign

        s = temp
        sign = get_sign(s)

    edit1 = edit

    # pattern 2
    s = alist[0]
    sign = get_sign(s)
    edit = 0

    edit += sign * s
    s -= s

    edit += 1
    s -= sign

    sign = get_sign(s)

    for a in alist[1:]:
        temp = s + a
        temp_sign = get_sign(temp)

        if sign == temp_sign:
            edit += temp_sign * temp
            temp -= temp

        if temp == 0:
            edit += 1
            temp -= sign

        s = temp
        sign = get_sign(s)

    edit2 = edit

    print(min(edit1, edit2))


if __name__ == '__main__':
    submit()

Submission status: Wrong Answer

Solution code:

def read_input():
    n = int(input())
    alist = list(map(int, input().split()))
    return n, alist

def get_sign(x):
    if x > 0:
        return 1
    elif x < 0:
        return -1
    return 0

def submit():
    n, alist = read_input()

    s = alist[0]
    sign = get_sign(s)
    edit = 0
    for a in alist[1:]:
        temp = s + a
        temp_sign = get_sign(temp)

        if sign == temp_sign:
            edit += temp_sign * temp
            temp -= temp_sign * temp

        if temp == 0:
            edit += 1
            temp -= sign

        s = temp
        sign = get_sign(s)

    print(edit)


if __name__ == '__main__':
    submit()

Submission status: Wrong Answer

Solution code:

def read_input():
    n = int(input())
    alist = list(map(int, input().split()))
    return n, alist

def get_sign(x):
    if x > 0:
        return 1
    elif x < 0:
        return -1
    return 0

def submit():
    n, alist = read_input()

    s = alist[0]
    sign = get_sign(s)
    edit = 0
    for a in alist[1:]:
        temp = s + a
        temp_sign = get_sign(temp)

        if sign == temp_sign:
            edit += temp_sign * temp
            temp -= temp

        if temp == 0:
            edit += 1
            temp -= sign


        s = temp
        sign = get_sign(s)

    print(edit)


if __name__ == '__main__':
    submit()

Submission status: Wrong Answer

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

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

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

count = 0
sabun = 0
for i in range(1, n):
    subl = l[i:i + 2]
    subl = [subl[0] + sabun, subl[1] + sabun]
    if 0 in subl:
        if subl[0] > 0:
            count += 1
            l[i + 1] = -1
        else:
            count += 1
            l[i + 1] = 1
    if subl[0] * subl[1] > 0:
        t = subl[1]
        m = True
        if t > 0:
            t = 1 + t
        else:
            t = 1 - t
            m = False
        if m:
            l[i + 1] -= t
            sabun -= t
        else:
            l[i + 1] += t
            sabun += t
        count += t
print(count)

Submission status: Wrong Answer

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

X = [a[0]]
ans = 0
#＋－＋－のとき
if a[0] >= 0:
    for i in range(n-1):
        #print(X[i], a[i+1])
        num = X[i] + a[i+1]
        if i % 2 != 0:      #iが奇数の時はnumの値は正
            if num < 0:
                X.append(1)
                ans += abs(num) + 1
            else:
                X.append(num)
        else:               #iが偶数のときnumの値は負
            if num > 0:
                X.append(-1)
                ans += abs(num) + 1
            else:
                X.append(num)
#－＋－＋のとき        
else:
    for i in range(n-1):
        #print(X[i], a[i+1])
        num = X[i] + a[i+1]
        if i % 2 != 0:      #iが奇数の時はnumの値は負
            if num < 0:
                X.append(num)
            else:
                ans += abs(num) + 1
                X.append(-1)
        else:               #iが偶数のときnumの値は正
            if num > 0:
                X.append(num)
            else:
                X.append(1)
                ans += abs(num) + 1
        
print(ans)

Submission status: Wrong Answer

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

X = [a[0]]
ans = 0
#＋－＋－のとき
if a[0] >= 0:
    for i in range(n-1):
        #print(X[i], a[i+1])
        num = X[i] + a[i+1]
        if i % 2 != 0:      #iが奇数の時はnumの値は正
            if num < 0:
                X.append(abs(num))
                ans += abs(num) + 1
            else:
                X.append(num)
        else:               #iが偶数のときnumの値は負
            if num > 0:
                X.append(num*-1)
                ans += abs(num) + 1
            else:
                X.append(num)
#－＋－＋のとき        
else:
    for i in range(n-1):
        #print(X[i], a[i+1])
        num = X[i] + a[i+1]
        if i % 2 != 0:      #iが奇数の時はnumの値は負
            if num < 0:
                X.append(num)
            else:
                ans += abs(num) + 1
                X.append(-1)
        else:               #iが偶数のときnumの値は正
            if num > 0:
                X.append(num)
            else:
                X.append(num*-1)
                ans += abs(num) + 1
        
print(ans)

Submission status: Wrong Answer

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

presum = a[0]
summ = 0
ans = 0

if a[0] == 0:
    if a[1] - a[0] > 0:
        ans += a[1] + 1
        a[0] = -a[1] - 1
    else:
        ans += 1 - a[1]
        a[0] =  1 - a[1]

for i in range(1,N):
    nsum = a[i] + presum
    if presum * nsum >= 0:
        if nsum == 0:
            if presum >= 0:
                a[i] -=  1
                ans += 1
                nsum = -1
            elif presum < 0:
                a[i] += 1
                ans += 1
                nsum = 1
        elif nsum > 0:
            ans += 1+presum+a[i]
            a[i] = -1 - presum
            nsum = -1
        elif nsum < 0:
            nsum = 1
            ans += 1 - presum - a[i]
            a[i]  =  1-presum
    presum = nsum
print(ans)

Submission status: Wrong Answer

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

presum = a[0]
summ = 0
ans = 0
for i in range(1,N):
    nsum = a[i] + presum
    if presum * nsum >= 0:
        if nsum == 0:
            if presum >= 0:
                a[i] -=  1
                ans += 1
                nsum = -1
            elif presum < 0:
                a[i] += 1
                ans += 1
                nsum = 1
        elif nsum > 0:
            ans += 1+presum+a[i]
            a[i] = -1 - presum
            nsum = -1
        elif nsum < 0:
            nsum = 1
            ans += 1 - presum - a[i]
            a[i]  =  1-presum
    presum = nsum
print(ans)

Submission status: Wrong Answer

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

fusei = 0
cnt = 0
if a[0] > 0:
    cnt = -1
    fusei += abs(a[0])+1
flag = 1
for i in range(1, n):
    if flag == 1:
        ai = a[i]
        if ai > 0:
            if ai + cnt <= 0:
                fusei += abs(cnt) + 1
                cnt = 1
            else:
                cnt += ai
        else:
            fusei += abs(cnt)+1
            cnt = 1
    else:
        ai = a[i]
        if ai < 0:
            if ai + cnt >= 0:
                fusei += abs(cnt) + 1
                cnt = -1
            else:
                cnt += ai
        else:
            fusei += abs(cnt) +1
            cnt = -1



seifu = 0
cnt = 0
if a[0] < 0:
    cnt = 1
    seifu += abs(a[0])+1
flag = -1
for i in range(1, n):
    if flag == 1:
        ai = a[i]
        if ai > 0:
            if ai + cnt <= 0:
                seifu += abs(cnt) + 1
                cnt = 1
            else:
                cnt += ai
        else:
            seifu += abs(cnt)+1
            cnt = 1
    else:
        ai = a[i]
        if ai < 0:
            if ai + cnt >= 0:
                seifu += abs(cnt) + 1
                cnt = -1
            else:
                cnt += ai
        else:
            seifu += abs(cnt) +1
            cnt = -1

print(min(fusei, seifu))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
cnt = 0
chg = 0
w = A[0]
for i in range(n - 1):
    nw = w + A[i + 1]
    if w > 0:
        if nw >= 0:
            cnt += nw + 1
            chg = -(nw  + 1)
    else:
        if nw <= 0:
            cnt += 1 - nw
            chg = 1 - nw
    w = nw + chg
    chg = 0
    
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
cnt = 0
w = A[0]
for i in range(n - 1):
  nw = w + A[i + 1]
  if w > 0:
    if nw >= 0:
      cnt += nw + 1
      chg = -(nw  + 1)
    else:
      if nw <= 0:
        cnt += 1 - nw
        chg = 1 - nw
        
    w = nw + chg
    chg = 0
    
print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=map(int,input().split())
ans1,ans2=0,0
m1,m2=0,0
k=1
for i in a:
    m1+=i
    m2+=i
    if k:
        k=0
        if m1>=0:
            ans1+=m1+1
            m1=-1
        if m2<=0:
            ans2+=1-m2
            m2=1
    else:
        k=1
        if m1<=0:
            ans1+=1-m1
            m1=1
        if m2>=0:
            ans2+=m2+1
            m2=-1
print(ans1,ans2)

Submission status: Wrong Answer

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

ans1 = 0
sum = A[0] if A[0] != 0 else 1
# A[0]=0 のときは1にする。下で-1にするパターンも試している。なお0は不可。

for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        nextsum = 1 if sum < 0 else -1
        # fugo = sum // abs(sum) 割り算のときは0に気をつけろ！
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        sum = nextsum
        ans1 += dif

#A[0]を反転したほうがいいパターンの処理
ans2 = abs(A[0]) + 1
sum = 1 if A[0] < 0 else -1
#ある意味、ここでA[0]=0のときに-1をおいていることをやっている

for a in A[1:]:
    if (sum + a) * sum < 0:
        sum += a
    else:
        nextsum = 1 if sum < 0 else -1
        a_should_be = nextsum - sum
        dif = abs(a_should_be - a)
        sum = nextsum
        ans2 += dif
        
print(min(ans1, ans2))

Submission status: Wrong Answer

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

s = 0
num = 0
for i, x in enumerate(a):
    if i == 0:
        if x == 0:
            num += 1
            for j, y in enumerate(a):
                if j == n - 1:
                    s = 1
                    break
                elif y > 0:
                    s = -1
                    break
                elif y < 0:
                    s = 1
                    break
        else:
            s = x
    else:
        if s > 0:
            if s + x < 0:
                s += x
            else:
                num += abs(-s-x-1)
                s = -1
        else:
            if s + x > 0:
                s += x
            else:
                num += abs(-s-x+1)
                s = 1
print(num)

Submission status: Wrong Answer

Solution code:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n = f()[0]
    a = f()
    
    suma = [0] * n
    minop = 1e9

    for greater in [True, False]:
        oper = 0
        greater0 = greater
        for i in range(n):
            if i == 0:
                suma[i] = a[i]
            else:
                suma[i] = a[i] + suma[i-1]
            
            greater0 = not greater0
            
            if greater0 and suma[i]<=0:
                oper +=  1 - suma[i]
                suma[i] = 1
                continue
            
            if (not greater0) and suma[i]>=0:
                oper += 1 + suma[i]
                suma[i] = -1
                continue

        minop = min(minop, oper)
    
    print(minop)
            
solve()

Submission status: Wrong Answer

Solution code:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n = f()[0]
    a = f()
    
    suma = [0] * n
    minop = 1e9

    for greater0 in [True, False]:
        oper = 0
        for i in range(n):
            if i == 0:
                suma[i] = a[i]
            else:
                suma[i] = a[i] + suma[i-1]
            
            greater0 = not greater0
            
            if greater0 and suma[i]<=0:
                oper +=  1 - suma[i]
                suma[i] = 1
                continue
            
            if (not greater0) and suma[i]>=0:
                oper += 1 + suma[i]
                suma[i] = -1
                continue

        minop = min(minop, oper)
    
    print(minop)
            

        

solve()

Submission status: Wrong Answer

Solution code:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n = f()[0]
    a = f()
    
    suma = [0] * n
    minop = 1e9

    for greater0 in [True, False]:
        oper = 0
        for i in range(n):
            if i == 0:
                suma[i] = a[i]
                if suma[i]<0:
                    greater0 = False
            else:
                suma[i] = a[i] + suma[i-1]
            
            greater0 = not greater0
            
            if greater0 and suma[i]<=0:
                oper +=  1 - suma[i]
                suma[i] = 1
                continue
            
            if (not greater0) and suma[i]>=0:
                oper += 1 + suma[i]
                suma[i] = -1
                continue

        minop = min(minop, oper)
    
    print(oper)
            

        

solve()

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int,input().split()))
sum_A = 0
ans = 0
current_zero = 0 #先頭からの0の連続数

if(n == 1):
  if(A[0] != 0): ans = 0
  else: ans += 1
else:
 for i in range(n):
  if(i == 0):
    if(A[i] != 0): sum_A += A[i]
    else:
      if(A[i+1]>0): sum_A,ans = -1,1 #A[0]->-1
      elif(A[i+1]<0): sum_A,ans = 1,1 #A[0]->1
      else: current_zero += 1
        
  elif(sum_A == 0):
    current_zero += 1
    if(i == n-1):
      ans += 2*(current_zero-1)+1
    else:
      if(A[i+1]>0): sum_A,ans = -1,2*(current_zero-1)+1 #A[i]->-1
      elif(A[i+1]<0): sum_A,ans = 1,2*(current_zero-1)+1 #A[i]->1

  else:
    if(sum_A > 0): judge = -1 #次の和は 負
    else: judge = 1 #次の和は 正
    sum_A += A[i]
    if(judge == 1):
      if(sum_A > 0): continue
      else:
        ans += abs(1-sum_A)
        sum_A = 1
    else: #judge == -1
      if(sum_A < 0): continue
      else:
        ans += abs(sum_A-(-1))
        sum_A = -1
 
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n = ni()
a = nl()

summ = [0 for i in range(n + 1)]
for i in range(n):
    summ[i + 1] = summ[i] + a[i]
diff = 0
temp = 0
ans = 0
zerocount = 0
for i in range(2, n + 1):
    summ[i] += temp
    summ[i - 1] += diff
    diff = 0
    if summ[i - 1] * summ[i] >= 0:
        if summ[i] < 0:
            ans += zerocount
            zerocount = 0
            diff += -summ[i] + 1
        elif summ[i] > 0:
            ans += zerocount
            zerocount = 0
            diff -= summ[i] + 1
        else:
            if summ[i - 1] > 0:
                diff -= 1
            elif summ[i - 1] < 0:
                diff += 1
            else:
                zerocount += 1
    temp += diff
    ans += abs(diff)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

sum = 0
count = 0

for i in range(0,n - 1):
    sum += As[i]
    if sum > 0:
        if sum + As[i + 1] > 0:
            count += max(sum + As[i + 1] + 1,0)
            As[i + 1] -= sum +  As[i + 1] + 1
    elif sum == 0:
        count += 1
        if As[i + 1] >=0:
            sum -= 1
        else:
            sum += 1
    else:
        if sum + As[i + 1] < 0:
            count += max(abs(sum + As[i + 1] - 1),0)
            As[i + 1] += abs(sum + As[i + 1] - 1)

sum += As[-1]
if sum == 0:
    count += 1

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
for i in range(1,n):
    a[i] += a[i-1]
a_1 = a.copy()
ans = 0
for i in range(n):
    if i%2==0 and a_1[i]<=0:
        tmp = -a_1[i]-1
        for j in range(i,n):
            a_1[j] += tmp
        ans += abs(tmp)
    elif i%2==1 and a_1[i]>=0:
        tmp = -a_1[i]+1
        for j in range(i,n):
            a_1[j] += tmp
        ans += abs(tmp)
ans_2 = 0
for i in range(n):
    if i%2==1 and a[i]<=0:
        tmp = -a[i]+1
        for j in range(i,n):
            a[j] += tmp
        ans_2 += abs(tmp)
    elif i%2==0 and a[i]>=0:
        tmp = -a[i]-1
        for j in range(i,n):
            a[j] += tmp
        ans_2 += abs(tmp)
print(min(ans,ans_2))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
s = 0
num = 0
sign = "+" if A[0] >= 0 else "-"
for a in A:
    s += a
    if sign == "+":
        if s > 0:
            pass
        else:
            num += abs(s) + 1
            s = 1
        sign = "-"
        continue
    if sign == "-":
        if s < 0:
            pass
        else:
            num += s + 1
            s = -1
        sign = "+"
        continue
if A[0] != 0:
    print(num)
    exit()
s = 0
num2 = 0
sign = "-"
for a in A:
    s += a
    if sign == "+":
        if s > 0:
            pass
        else:
            num2 += abs(s) + 1
            s = 1
        sign = "-"
        continue
    if sign == "-":
        if s < 0:
            pass
        else:
            num2 += s + 1
            s = -1
        sign = "+"
        continue
print(min(num, num2))

Submission status: Wrong Answer

Solution code:
import sys
fastinput=sys.stdin.readline
n=int(fastinput())
ai=[int(i) for i in fastinput().split()]
#プラス始まり
goukei=0
sousa=0
for k,a in enumerate(ai):
    goukei+=a
    if k==0:
        continue
    elif not k%2:#even:plus
        if goukei<=0:
            sousa+=1-goukei
            goukei=1
    else:#odd:minus
        if goukei>=0:
            sousa+=goukei+1
            goukei=-1
ans1=sousa
#マイナス始まり
goukei=0
sousa=0
for k,a in enumerate(ai):
    goukei+=a
    if k==0:
        continue
    if k%2:#odd:plus
        if goukei<=0:
            sousa+=1-goukei
            goukei=1
    else:#even:minus
        if goukei>=0:
            sousa+=goukei+1
            goukei=-1
ans2=sousa
print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
import sys
fastinput=sys.stdin.readline
n=int(fastinput())
ai=[int(i) for i in fastinput().split()]
#プラス始まり
goukei=0
sousa=0
for a in ai:
    goukei+=a
    if a%2:#even:plus
        if goukei<=0:
            sousa+=1-goukei
            goukei=1
    else:#odd:minus
        if goukei>=0:
            sousa+=goukei+1
            goukei=-1
ans1=sousa
#マイナス始まり
goukei=0
sousa=0
for a in ai:
    goukei+=a
    if not a%2:#odd:plus
        if goukei<=0:
            sousa+=1-goukei
            goukei=1
    else:#even:minus
        if goukei>=0:
            sousa+=goukei+1
            goukei=-1
ans2=sousa
print(min(ans1,ans2))

Submission status: Wrong Answer

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

count = 0
flag = 0
diff = 0
def r_flag(n):
  if n > 0:
    return 1
  elif n < 0:
    return -1
  else:
    return 0

flag = r_flag(cum_a[0])
for i in range(1, len(cum_a)):
  tmp_flag = r_flag(cum_a[i]+diff)
  if tmp_flag * flag == -1:
    flag = tmp_flag
  elif tmp_flag * flag == 1:
    if tmp_flag == 1:
      count += abs(-1-(cum_a[i]+diff))
      diff += -1-(cum_a[i]+diff)
      flag = -1
    else:
      count += abs(1-(cum_a[i]+diff))
      diff += 1-(cum_a[i]+diff)
      flag = 1
  else:
    try:
      next_flag = r_flag(cum_a[i+1]-diff)
      if next_flag == 1:
        diff -= 1
        count += 1
      else:
        diff += 1
        count += 1
    except:
      diff += 1
      count += 1
print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())

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

res=X[0]
cnt=0

if X[0]==0:
    cnt+=1
    res=1

        
for i in range(1,n):
    res+=X[i]
    if res>=0 and res-X[i]>0:
        cnt+=(1+res)
        res=-1
    elif res<=0 and res-X[i]<0:
        cnt+=(1-res)
        res=1
    

print(cnt)

Submission status: Wrong Answer

Solution code:
n,a=int(input()),list(map(int,input().split()));ans=0
if a==[0]*n:print(n*2-1);exit()
ma=[0,0]
for i in range(1,n):
  if ma[0]<abs(a[i]):ma[0]=abs(a[i]);ma[1]=i
if a[ma[1]]>0:m=(1if ma[1]%2==0else-1)
else:m=(1if ma[1]%2!=0else-1)
if a[0]*m<=0:ans+=abs(m-a[0])
m*=-1
b=[a[0]];m=(1if a[0]<0else-1)
for i in range(1,n):
  b.append(b[i-1]+a[i])
  if b[i-1]*b[i]>=0:
    ans+=abs(m-b[i])
    b[i]=m
  m*=-1
print(ans)

Submission status: Wrong Answer

Solution code:
n,a=int(input()),list(map(int,input().split()));ans=0
if a[0]==0:
  bol=True
  for i in range(1,n):
    if a[i-1]!=a[i]:
      bol=False
      if a[i]>0:a[0]=(1if i%2==0else-1)
      else:a[0]=(1if i%2!=0else-1)
      ans+=1;break
  if bol:print(n*2-1);exit()
b=[a[0]];m=("+"if a[0]<0else"-")
for i in range(1,n):
  b.append(b[i-1]+a[i])
  if b[i-1]*b[i]>=0:
    r=eval(m+"1")-b[i]
    ans+=abs(r)
    b[i]+=r
  m=("+"if b[i]<0else"-")
print(ans)

Submission status: Wrong Answer

Solution code:
n,a=int(input()),list(map(int,input().split()));ans=0
if a[0]==0:
  bol=True
  for i in range(1,n):
    if a[i-1]!=a[i]:
      bol=False
      if a[i]>0:a[0]=(1if i%2==0else-1)
      else:a[0]=(1if i%2!=0else-1)
    ans+=1;break
  if bol:print(n*2-1);exit()
b=[a[0]];m=("+"if a[0]<0else"-")
for i in range(1,n):
  b.append(b[i-1]+a[i])
  if b[i-1]*b[i]>=0:
    r=eval(m+"1")-b[i]
    ans+=abs(r)
    b[i]+=r
  m=("+"if b[i]<0else"-")
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
ans1, ans2 = 0, 0
f = 0
f += a[0]
if f <= 0:
    f = 1
    ans1 += 1 - a[0]
for i in range(1, N):
    if f * (f + a[i]) < 0:
        f += a[i]
        continue
    ans1 += abs(f + a[i]) + 1
    if f > 0:
        f = -1
    else:
        f = 1
f = 0
f += a[0]
if f >= 0:
    f = -1
    ans2 += 1 + a[i]
for i in range(1, N):
    if f * (f + a[i]) < 0:
        f += a[i]
        continue
    ans2 += abs(f + a[i]) + 1
    if f > 0:
        f = -1
    else:
        f = 1
print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
ans1, ans2 = 0, 0
f = 0
f += a[0]
if f <= 0:
    f = 1
    ans1 += 1 - a[0]
for i in range(1, N):
    if f * (f + a[i]) < 0:
        f += a[i]
        continue
    ans1 += abs(f + a[i]) + 1
    if f > 0:
        f = -1
    else:
        f = 1
f = 0
f += a[0]
if f >= 0:
    f = -1
    ans2 += 1 - a[i]
for i in range(1, N):
    if f * (f + a[i]) < 0:
        f += a[i]
        continue
    ans2 += abs(f + a[i]) + 1
    if f > 0:
        f = -1
    else:
        f = 1
print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
ans1, ans2 = 0, 0
f = a[0]
if f <= 0:
    f = 1
    ans1 += 1 - a[0]
for i in range(1, N):
    if f * (f + a[i]) < 0:
        f += a[i]
        continue
    ans1 += abs(f + a[i]) + 1
    if f > 0:
        f = -1
    else:
        f = 1
f = a[0]
if f >= 0:
    f = -1
    ans2 += 1 - a[i]
for i in range(1, N):
    if f * (f + a[i]) < 0:
        f += a[i]
        continue
    ans2 += abs(f + a[i]) + 1
    if f > 0:
        f = -1
    else:
        f = 1
print(min(ans1, ans2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
ans1, ans2 = 0, 0
f = a[0]
if f <= 0:
    f = 1
    ans1 += 1 - a[0]
for i in range(1, N):
    if f * (f + a[i]) < 0:
        f += a[i]
        continue
    ans1 += abs(f + a[i]) + 1
    if f > 0:
        f = -1
    else:
        f = 1
f = -a[0]
if f >= 0:
    f = -1
    ans2 += 1 - a[i]
for i in range(1, N):
    if f * (f + a[i]) < 0:
        f += a[i]
        continue
    ans2 += abs(f + a[i]) + 1
    if f > 0:
        f = -1
    else:
        f = 1
print(min(ans1, ans2))