Problem:


Score : 300 points

Problem Statement

You are given an integer sequence of length N. The i-th term in the sequence is a_i.
In one operation, you can select a term and either increment or decrement it by one.

At least how many operations are necessary to satisfy the following conditions?

For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.

For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.

Constraints

2 ≤ n ≤ 10^5

|a_i| ≤ 10^9

Each a_i is an integer.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print the minimum necessary count of operations.

Sample Input 1

4
1 -3 1 0

Sample Output 1

4

For example, the given sequence can be transformed into 1, -2, 2, -2 by four operations. The sums of the first one, two, three and four terms are 1, -1, 1 and -1, respectively, which satisfy the conditions.

Sample Input 2

5
3 -6 4 -5 7

Sample Output 2

0

The given sequence already satisfies the conditions.

Sample Input 3

6
-1 4 3 2 -5 4

Sample Output 3

8



Sample input:
4
1 -3 1 0


Sample output:
4

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
 
r_even = 0  # 偶数桁を＋にする場合の値
a_even = a[:]
sum_even = 0
for i in range(len(a)):
    sum_even += a_even[i]
    if i % 2 == 0:
        if sum_even <= 0:
            r_even += 1 - sum_even
            a_even[i] += 1 - sum_even
            sum_even += 1 - sum_even
    else:
        if sum_even > 0:
            r_even += sum_even + 1
            a_even[i] -= sum_even + 1
            sum_even -= sum_even + 1

r_odd = 0   #奇数桁を＋にする場合の値
a_odd = a[:]
sum_odd = 0
for i in range(len(a)):
    sum_odd += a_odd[i]
    if i % 2 != 0:
        if sum_odd <= 0:
            r_odd += 1 - sum_odd
            a_odd[i] += 1 - sum_odd
            sum_odd += 1 - sum_odd
    else:
         if sum_odd > 0:
            r_odd += sum_odd + 1
            a_odd[i] -= sum_odd + 1
            sum_odd -= sum_odd + 1

print(min(r_odd, r_even))

Submission status: Wrong Answer

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

odd = sum(lst1[::2])
even = sum(lst1[1::2])

if odd < even:
    need = "-"
else:
    need = "+"
ans = 0
now = 0#現在のi迄の和
for i in range(n):
    if lst1[i] < 0:
        if need == "-":
            if abs(now) >= abs(lst1[i]):
                ans += now+lst1[i]+1
                now = -1
            else:
                now += lst1[i]
            need = "+"
        else: #need == "+"
            ans += abs(now)-lst1[i] + 1
            now = 1
            
            need = "-"
    else:
        if need == "+":
            if abs(now) >= abs(lst1[i]):
                ans += abs(now)-lst1[i]+1
                now = 1
            else:
                now += lst1[i]
            need = "-"
        else: #need == "-"
            ans += abs(now)+lst1[i]+1
            now = -1
            
            need = "+"

print(ans)

Submission status: Wrong Answer

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

odd = sum(lst1[::2])
even = sum(lst1[1::2])


if odd < even:
    need = "-"
else:
    need = "+"
ans = 0
now = 0#現在のi迄の和
for i in range(n):
    if lst1[i] < 0:
        if need == "-":
            if abs(now) >= abs(lst1[i]):
                ans += now+lst1[i]+1
                now = -1
            else:
                now += lst1[i]
                need = "+"
        else: #need == "+"
            ans += abs(now)-lst1[i] + 1
            now = 1
            
            need = "-"
    else:
        if need == "+":
            if abs(now) >= abs(lst1[i]):
                ans += abs(now)-lst1[i]+1
                now = 1
            else:
                now += lst1[i]
                need = "-"
        else: #need == "-"
            ans += abs(now)+lst1[i]+1
            now = -1
            
            need = "+"

print(ans)

Submission status: Wrong Answer

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

odd = sum(lst1[::2])
even = sum(lst1[1::2])


if odd < even:
    need = "-"
else:
    need = "+"
ans = 0
now = 0#現在のi迄の和
for i in range(n):
    if lst1[i] < 0:
        if need == "-":
            if abs(now) >= abs(lst1[i]):
                ans += abs(now)-lst1[i]+1
                now = -1
            else:
                now += lst1[i]
                need = "+"
        else: #need == "+"
            ans += abs(now)-lst1[i] + 1
            now = 1
            
            need = "-"
    else:
        if need == "+":
            if abs(now) >= abs(lst1[i]):
                ans += abs(now)-lst1[i]+1
                now = 1
            else:
                now += lst1[i]
                need = "-"
        else: #need == "-"
            ans += abs(now)+lst1[i]+1
            now = -1
            
            need = "+"

print(ans)

Submission status: Wrong Answer

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

odd = sum(lst1[::2])
even = sum(lst1[1::2])


if odd < even:
    need = "-"
else:
    need = "+"
ans = 0
now = 0#現在のi迄の和
for i in range(n):
    if lst1[i] < 0:
        if need == "-":
            if abs(now) >= abs(lst1[i]):
                ans += abs(now+lst1[i])+1
                now = -1
            else:
                now += lst1[i]
                need = "+"
        else: #need == "+"
            ans += abs(now)-lst1[i] + 1
            now = 1
            
            need = "-"
    else:
        if need == "+":
            if abs(now) >= abs(lst1[i]):
                ans += abs(now+lst1[i])+1
                now = 1
            else:
                now += lst1[i]
                need = "-"
        else: #need == "-"
            ans += abs(now)+lst1[i]+1
            now = -1
            
            need = "+"

print(ans)

Submission status: Wrong Answer

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

sum = list_a[0]
ans_even = 0
ans_odd = 0

for i in range(1,n) :
    if i%2 == 0 :
        if sum + list_a[i] <= 0 :
            ans_even += 1 - (sum + list_a[i])
            sum = 1
        else :
            sum = sum + list_a[i]
    else :
        if sum + list_a[i] > 0 :
            ans_even += 1 + (sum + list_a[i])
            sum = -1
        else :
            sum = sum + list_a[i]

for i in range(1,n) :
    if i%2 == 1 :
        if sum + list_a[i] <= 0 :
            ans_odd += 1 - (sum + list_a[i])
            sum = 1
        else :
            sum = sum + list_a[i]
    else :
        if sum + list_a[i] > 0 :
            ans_odd += 1 + (sum + list_a[i])
            sum = -1
        else :
            sum = sum + list_a[i]

print(min(ans_even, ans_odd))

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
S = list(map(int, input().split()))

mod_count = 0
mod_count = 0
pre_fugou = 1 ## 前の符号がなんであったか　1ならplus　0ならマイナス

for i in range(N):
    if i==0 and S[i]==0:
        if S[i+1]>0:
            S[i] -= 1
            mod_count += 1
            pre_fugou = 0
        else:
            S[i] += 1
            mod_count += 1
            pre_fugou = 1
    elif i==0 and S[i]>0:
        pre_fugou = 1
    elif i==0 and S[i]<0:
        pre_fugou = 0
    else:
        if pre_fugou == 1:
            while True:
                if sum(S[:i+1]) > -1:
                    S[i] -= 1
                    mod_count += 1
                    pre_fugou = 0
                else:
                    pre_fugou = 0
                    break
        elif pre_fugou == 0:
            while True:
                if sum(S[:i+1]) < 1:
                    S[i] += 1
                    mod_count += 1
                    pre_fugou = 1
                else:
                    pre_fugou = 1
                    break

Submission status: Wrong Answer

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

def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    a_sum = a_list[0]
    if a_list[0] > 0:
        sign = "plus"
    else:
        sign = "minus"
    ans1 = 0
    for i in range(1, n):
        if sign == "plus":
            sign = "minus"
            if a_sum + a_list[i] == 0:
                ans1 += 1
                a_sum = -1
            elif a_sum + a_list[i] > 0:
                ans1 += a_sum + 1 + a_list[i]
                a_sum = -1
            else:
                a_sum += a_list[i]
        elif sign == "minus":
            sign = "plus"
            if a_sum + a_list[i] == 0:
                ans1 += 1
                a_sum = 1
            elif a_sum + a_list[i] < 0:
                ans1 += -1 * a_sum + 1 + -1 * a_list[i]
                a_sum = 1
            else:
                a_sum += a_list[i]

    a_sum = 0
    if a_list[0] > 0:
        sign = "plus"
    else:
        sign = "minus"
    ans2 = 0
    for i in range(0, n):
        if sign == "plus":
            sign = "minus"
            if a_sum + a_list[i] == 0:
                ans2 += 1
                a_sum = -1
            elif a_sum + a_list[i] > 0:
                ans2 += a_sum + 1 + a_list[i]
                a_sum = -1
            else:
                a_sum += a_list[i]
        elif sign == "minus":
            sign = "plus"
            if a_sum + a_list[i] == 0:
                ans2 += 1
                a_sum = 1
            elif a_sum + a_list[i] < 0:
                ans2 += -1 * a_sum + 1 + -1 * a_list[i]
                a_sum = 1
            else:
                a_sum += a_list[i]

    print(min(ans1, ans2))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    a_sum = a_list[0]
    if a_list[0] > 0:
        sign = "plus"
    else:
        sign = "minus"
    ans1 = 0
    for i in range(1, n):
        if sign == "plus":
            sign = "minus"
            if a_sum + a_list[i] == 0:
                ans1 += 1
                a_sum = -1
            elif a_sum + a_list[i] > 0:
                ans1 += a_sum + 1 + a_list[i]
                a_sum = -1
            else:
                a_sum += a_list[i]
        elif sign == "minus":
            sign = "plus"
            if a_sum + a_list[i] == 0:
                ans1 += 1
                a_sum = 1
            elif a_sum + a_list[i] < 0:
                ans1 += -1 * a_sum + 1 + -1 * a_list[i]
                a_sum = 1
            else:
                a_sum += a_list[i]

    a_sum = a_list[0]
    if a_list[0] > 0:
        sign = "plus"
    else:
        sign = "minus"
    ans2 = 0
    for i in range(0, n):
        if sign == "plus":
            sign = "minus"
            if a_sum + a_list[i] == 0:
                ans2 += 1
                a_sum = -1
            elif a_sum + a_list[i] > 0:
                ans2 += a_sum + 1 + a_list[i]
                a_sum = -1
            else:
                a_sum += a_list[i]
        elif sign == "minus":
            sign = "plus"
            if a_sum + a_list[i] == 0:
                ans2 += 1
                a_sum = 1
            elif a_sum + a_list[i] < 0:
                ans2 += -1 * a_sum + 1 + -1 * a_list[i]
                a_sum = 1
            else:
                a_sum += a_list[i]

    print(min(ans1, ans2))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    a_sum = a_list[0]
    if a_list[0] > 0:
        sign = "plus"
    else:
        sign = "minus"
    ans1 = 0
    for i in range(1, n):
        if sign == "plus":
            sign = "minus"
            if a_sum + a_list[i] == 0:
                ans1 += 1
                a_sum = -1
            elif a_sum + a_list[i] > 0:
                ans1 += a_sum + 1 + a_list[i]
                a_sum = -1
            else:
                a_sum += a_list[i]
        elif sign == "minus":
            sign = "plus"
            if a_sum + a_list[i] == 0:
                ans1 += 1
                a_sum = 1
            elif a_sum + a_list[i] < 0:
                ans1 += -1 * a_sum + 1 + -1 * a_list[i]
                a_sum = 1
            else:
                a_sum += a_list[i]

    ans2 = 0
    for i in range(0, n):
        if sign == "plus":
            sign = "minus"
            if a_sum + a_list[i] == 0:
                ans2 += 1
                a_sum = -1
            elif a_sum + a_list[i] > 0:
                ans2 += a_sum + 1 + a_list[i]
                a_sum = -1
            else:
                a_sum += a_list[i]
        elif sign == "minus":
            sign = "plus"
            if a_sum + a_list[i] == 0:
                ans2 += 1
                a_sum = 1
            elif a_sum + a_list[i] < 0:
                ans2 += -1 * a_sum + 1 + -1 * a_list[i]
                a_sum = 1
            else:
                a_sum += a_list[i]

    print(min(ans1, ans2))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    a_sum = a_list[0]
    if a_list[0] > 0:
        sign = "plus"
    else:
        sign = "minus"
    ans = 0
    for i in range(1, n):
        if sign == "plus":
            sign = "minus"
            if a_sum + a_list[i] == 0:
                ans += 1
                a_sum = -1
            elif a_sum + a_list[i] > 0:
                ans += a_sum + 1 + a_list[i]
                a_sum = -1
            else:
                a_sum += a_list[i]
        elif sign == "minus":
            sign = "plus"
            if a_sum + a_list[i] == 0:
                ans += 1
                a_sum = 1
            elif a_sum + a_list[i] < 0:
                ans += -1 * a_sum + 1 + -1 * a_list[i]
                a_sum = 1
            else:
                a_sum += a_list[i]
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc059/tasks/arc072_a
# 2020/02/21
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    a_sum = a_list[0]
    if a_list[0] > 0:
        sign = "plus"
    else:
        sign = "minus"
    ans = 0
    for i in range(1, n):
        if sign == "plus":
            sign = "minus"
            if a_sum + a_list[i] == 0:
                a_sum -= 1
                ans += 1
            elif a_sum + a_list[i] > 0:
                ans += a_sum + 1 + a_list[i]
                a_sum = -1
            else:
                a_sum += a_list[i]
        elif sign == "minus":
            sign = "plus"
            if a_sum + a_list[i] == 0:
                a_sum += 1
                ans += 1
            elif a_sum + a_list[i] < 0:
                ans += (-1 * a_sum + 1 + -1 * a_list[i])
                a_sum = 1
            else:
                a_sum += a_list[i]
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-


def check(a_list: list) -> int:
    if a_list[0] == 0:
        return 0

    count=0
    sum=a_list[0]
    for num in a_list[1:]:
        sum_next = sum + num
        
        # flag (+,-,0) -> (1,-1,0)
        sum_p_m_sign = (sum>0) - (sum<0)
        sum_next_p_m_sign = (sum_next>0) - (sum_next<0)
        
        sign_chck = sum_p_m_sign * sum_next_p_m_sign

        if sign_chck == 1:
            # 符号が一致した場合、+1 or -1まで操作
            diff = (abs(sum_next) + 1) * (-1)*sum_next_p_m_sign
            sum += num + diff
            count += abs(diff)

        elif sign_chck == 0:
            # sum_next=0の時
            diff = (-1)*sum_p_m_sign
            sum += num + diff
            count += abs(diff)
        
        else:
            sum = sum + num
 
    return count
        
#----------
n = int(input().strip())
a_list = list(map(int, input().rstrip().split()))
#----------
print( check(a_list) )

Submission status: Wrong Answer

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

total = a[0]
cnt = 0
for i in range(1, n):
    if (total > 0 and total + a[i] < 0) or (total < 0 and total + a[i] > 0):
        pass
    elif total > 0 and total + a[i] >= 0:
        print(i)
        while total + a[i] >= 0:
            a[i] -= 1
            cnt += 1
    elif total < 0 and total + a[i] <= 0:
        while total + a[i] <= 0:
            a[i] += 1
            cnt += 1
    total += a[i]
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
ans_plusstart = 0
sum_plusstart = 0
ans_minusstart = 0
sum_minusstart = 0
if A[0] < 0:
    ans_plusstart += 1 - A[0]
    sum_minusstart = A[0]
    sum_plusstart = 1
else:
    ans_minusstart += 1 + A[0]
    sum_minusstart = -1
    sum_plusstart = max(1, A[0])
    if A[0] == 0:
        ans_plusstart = 1
for i in range(1, N):
    #print(sum_plusstart, ans_plusstart, sum_minusstart, ans_minusstart)
    if i % 2 == 0:  # スタートと同じ記号にしたい
        if A[i] + sum_plusstart > 0:
            sum_plusstart = A[i] + sum_plusstart
        if A[i] + sum_plusstart <= 0:
            ans_plusstart += 1-(A[i] + sum_plusstart)
            sum_plusstart = 1

        if A[i] + sum_minusstart < 0:
            sum_minusstart = A[i] + sum_minusstart
        if A[i] + sum_minusstart >= 0:
            ans_minusstart += 1 + A[i] + sum_minusstart
            sum_minusstart = -1

    else:  # スタートと違う記号にしたい
        if A[i] + sum_plusstart < 0:
            sum_plusstart = A[i] + sum_plusstart
        if A[i] + sum_plusstart >= 0:
            ans_plusstart += 1+A[i] + sum_plusstart
            sum_plusstart = -1

        if A[i] + sum_minusstart > 0:
            sum_minusstart = A[i] + sum_minusstart
        if A[i] + sum_minusstart <= 0:
            ans_minusstart += 1 - (A[i] + sum_minusstart)
            sum_minusstart = -1
print(min(ans_minusstart, ans_plusstart))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
st = 0
while a[st]==0:
  st+=1
if (a[st]>0 and st%2==0) or (a[st]<0 and st%2==1):
  fg=1
else:
  fg=-1
ret = 0
tot = 0
for i in range(n):
  tot+=a[i]
  if tot*fg<=0:
    ret += abs(fg - tot)
    tot += fg - tot
  fg*=-1
print(ret)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
def solve(a,f):
  ans=0
  for i in range(1,n):
    a[i]+=a[i-1]
    if not f:
      if a[i]>=0:
        ans+=a[i]+1
        a[i]=-1
    else:
      if a[i]<=0:
        ans+=a[i]*-1+1
        a[i]=1
    # print(a,ans,f)
    f^=True
  return ans
print(min(solve(A,True),solve(A,False)))

Submission status: Wrong Answer

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

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]
  #チェック
  ##符号が等しいか、ゼロなら、A[i - 1]と異符号で絶対値が1の数にする
  if totals[i - 1] * totals[i] >= 0:
    if totals[i - 1] < 0:
      con += abs(1 - totals[i])
      totals[i] = 1
    else:
      con += abs(-1 - totals[i])
      totals[i] = -1
      
print(con)

Submission status: Wrong Answer

Solution code:
N = int(input())
a_lst = [int(x) for x in input().split()]
b_lst = []
for i in a_lst:
    b_lst.append(i)

def my_sign(num):
    return (num > 0) - (num < 0)

cnt_p = 0
cnt_n = 0
sum_lst = []
sum2_lst = []

for i in range(N):
    if i == 0:
        if a_lst[i] == 0:
            a_lst[i] = 1
            cnt_p += 1
        sum_lst.append(a_lst[i])
    else:
        sum_lst.append(a_lst[i] + sum_lst[i - 1])
        if my_sign(sum_lst[i]) == my_sign(sum_lst[i - 1]) or my_sign(sum_lst[i]) == 0:
            cnt_p += max(-my_sign(sum_lst[i - 1]), sum_lst[i]) - min(-my_sign(sum_lst[i - 1]), sum_lst[i])
            a_lst[i] += -my_sign(sum_lst[i - 1]) - sum_lst[i]
            sum_lst[i] = -my_sign(sum_lst[i - 1])
for i in range(N):
    if i == 0:
        if b_lst[i] == 0:
            b_lst[i] = -1
            cnt_n += 1
        sum2_lst.append(b_lst[i])
    else:
        sum2_lst.append(b_lst[i] + sum2_lst[i - 1])
        if my_sign(sum2_lst[i]) == my_sign(sum2_lst[i - 1]) or my_sign(sum2_lst[i]) == 0:
            cnt_n += max(-my_sign(sum2_lst[i - 1]), sum2_lst[i]) - min(-my_sign(sum2_lst[i - 1]), sum2_lst[i])
            b_lst[i] += -my_sign(sum2_lst[i - 1]) - sum2_lst[i]
            sum2_lst[i] = -my_sign(sum2_lst[i - 1])
print(min(cnt_p, cnt_n))

Submission status: Wrong Answer

Solution code:
N = int(input())
a_lst = [int(x) for x in input().split()]
b_lst = a_lst

def my_sign(num):
    return (num > 0) - (num < 0)

sum = 0
cnt_p = 0
cnt_n = 0
sum_lst = []
sum2_lst = []

for i in range(N):
    if i == 0:
        if a_lst[i] == 0:
            a_lst[i] = 1
            cnt_p += 1
        sum_lst.append(a_lst[i])
    else:
        sum_lst.append(a_lst[i] + sum_lst[i - 1])
        if my_sign(sum_lst[i]) == my_sign(sum_lst[i - 1]) or my_sign(sum_lst[i]) == 0:
            cnt_p += max(-my_sign(sum_lst[i - 1]), sum_lst[i]) - min(-my_sign(sum_lst[i - 1]), sum_lst[i])
            a_lst[i] += -my_sign(sum_lst[i - 1]) - sum_lst[i]
            sum_lst[i] = -my_sign(sum_lst[i - 1])
for i in range(N):
    if i == 0:
        if b_lst[i] == 0:
            b_lst[i] = -1
            cnt_n += 1
        sum2_lst.append(b_lst[i])
    else:
        sum2_lst.append(b_lst[i] + sum2_lst[i - 1])
        if my_sign(sum2_lst[i]) == my_sign(sum2_lst[i - 1]) or my_sign(sum2_lst[i]) == 0:
            cnt_n += max(-my_sign(sum2_lst[i - 1]), sum2_lst[i]) - min(-my_sign(sum2_lst[i - 1]), sum2_lst[i])
            b_lst[i] += -my_sign(sum2_lst[i - 1]) - sum2_lst[i]
            sum2_lst[i] = -my_sign(sum2_lst[i - 1])
print(min(cnt_p, cnt_n))

Submission status: Wrong Answer

Solution code:
N = int(input())
a_lst = [int(x) for x in input().split()]

def my_sign(num):
    return (num > 0) - (num < 0)

sum = 0
cnt = 0
sum_lst = []

for i in range(N):
    if i == 0:
        if a_lst[i] == 0:
            a_lst[i] = 1
            cnt += 1
        sum_lst.append(a_lst[i])
    else:
        sum_lst.append(a_lst[i] + sum_lst[i - 1])
        if my_sign(sum_lst[i]) == my_sign(sum_lst[i - 1]) or my_sign(sum_lst[i]) == 0:
            cnt += max(-my_sign(sum_lst[i - 1]), sum_lst[i]) - min(-my_sign(sum_lst[i - 1]), sum_lst[i])
            a_lst[i] += -my_sign(sum_lst[i - 1]) - sum_lst[i]
            sum_lst[i] = -my_sign(sum_lst[i - 1])
#print(a_lst)
#print(sum_lst)
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
a_lst = [int(x) for x in input().split()]

def my_sign(num):
    return (num > 0) - (num < 0)

sum = 0
cnt = 0
sum_lst = []

for i in range(N):
    if i == 0:
        if a_lst[i] == 0:
            a_lst[i] = 1
        sum_lst.append(a_lst[i])
    else:
        sum_lst.append(a_lst[i] + sum_lst[i - 1])
        if my_sign(sum_lst[i]) == my_sign(sum_lst[i - 1]) or my_sign(sum_lst[i]) == 0:
            cnt += max(-my_sign(sum_lst[i - 1]), sum_lst[i]) - min(-my_sign(sum_lst[i - 1]), sum_lst[i])
            a_lst[i] += -my_sign(sum_lst[i - 1]) - sum_lst[i]
            sum_lst[i] = -my_sign(sum_lst[i - 1])
#print(a_lst)
#print(sum_lst)
print(cnt)

Submission status: Wrong Answer

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

sum = a[0]
ans = 0
if sum > 0:
    flg = 1
else:
    flg = -1
for i in range(1, n):
    sum += a[i]
    if flg == 1:
        if sum < 0:
            flg = -1
        else:
            ans += sum + 1
            sum = -1
            flg = -1
    else:
        if sum > 0:
            flg = 1
        else:
            ans += (sum) * (-1) + 1
            sum = 1
            flg = 1

print(ans)

Submission status: Wrong Answer

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

sum = a[0]
ans = 0
if sum > 0:
    flg = 1
else:
    flg = -1
for i in range(1, n):
    sum += a[i]
    if flg == 1:
        if sum < 0:
            pass
            flg = -1
        else:
            ans += sum + 1
            sum = -1
            flg = -1
    else:
        if sum > 0:
            pass
            flg = 1
        else:
            ans += (sum) * (-1) + 1
            sum = 1
            flg = 1

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
a = inl()
ne = sum(a[::2])
no = sum(a[1::2])
p0 = (ne>no)
mn = BIG
for p0 in [True,False]:
 cnt = 0
 acc = 0
 #ddprint("ne {} no {} p0 {} a {}".format(ne,no,p0,a))
 for i in range(n):
    if (p0 if i%2==0 else not p0):
        x = max(0, 1-a[i]-acc)
        cnt += x
        acc += a[i]+x
    else:
        x = max(0, a[i]+acc+1)
        cnt += x
        acc += a[i]-x
    #ddprint("cnt {} acc {}".format(cnt,acc))
 mn = min(mn,cnt)
print(mn)

Submission status: Wrong Answer

Solution code:
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
a = inl()
ne = sum(a[::2])
no = sum(a[1::2])
p0 = (ne>no)
cnt = 0
acc = 0
#ddprint("ne {} no {} p0 {} a {}".format(ne,no,p0,a))
for i in range(n):
    if (p0 if i%2==0 else not p0):
        x = max(0, 1-a[i]-acc)
        cnt += x
        acc += a[i]+x
    else:
        x = max(0, a[i]+acc+1)
        cnt += x
        acc += a[i]-x
    #ddprint("cnt {} acc {}".format(cnt,acc))
print(cnt)

Submission status: Wrong Answer

Solution code:
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n = inn()
a = inl()
ne = sum(a[::2])
no = sum(a[1::2])
p0 = (ne>no)
cnt = 0
acc = a[0]
for i in range(1,n):
    if (p0 if i%2==0 else not p0):
        x = max(0, 1-a[i]-acc)
        cnt += x
        acc += a[i]+x
    else:
        x = max(0, a[i]+acc+1)
        cnt += x
        acc += a[i]-x
print(cnt)

Submission status: Wrong Answer

Solution code:
def countave(n , a):
    ans = 100000

    for i in range(2):
        kei = 0
        forans = 0
        for w in range(n):
            if (w + i) % 2:
                if kei + int(a[w]) > -1:
                    forans += abs(kei + int(a[w])) + 1
                    kei = -1
                else:
                    kei += int(a[w])
            else:
                if kei + int(a[w]) < 1:
                    forans += abs(kei + int(a[w])) + 1
                    kei = 1
                else:
                    kei += int(a[w])

        ans = min(ans , forans)

    return ans

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def countave(n , a):
    ans = 100000

    for i in range(2 , 4):
        kei = 0
        forans = 0
        #偶数+
        if i % 2 == 0:
            fast = 1
        else:
            fast = -1
        for w in range(n):
            if fast < 0 and kei + int(a[w]) >= 0:
                forans += abs(kei + int(a[w])) + 1
                kei = -1
                fast = 1
            elif fast > 0 and kei + int(a[w]) <= 0:
                forans += abs(kei + int(a[w])) + 1
                kei = 1
                fast = -1
            else:
                kei += int(a[w])
                fast *= -1
        ans = min(ans , forans)

    return ans

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

s_pos = 0
s_neg = 0

# start neg val
cum = 0
for i in range(n):
    cum += a[i]
    if i % 2 == 0 and cum >= 0:
        s_neg += abs(cum) + 1
        cum = -1
    if i % 2 != 0 and cum <= 0:
        s_neg += abs(cum) + 1
        cum = 1

# start pos val
cum = 0
for i in range(n):
    cum += a[i]
    if i % 2 == 0 and cum <= 0:
        s_pos += abs(cum) + 1
        cum = -1
    if i % 2 != 0 and cum >= 0:
        s_pos += abs(cum) + 1
        cum = 1

ans = min(s_pos, s_neg)
print(ans)

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()

def solve(lis):
	lis = lis.copy()
	tmp_sum = lis[0]
	lis = lis[1:]
	cnt = 0
	for x in lis:
		if (tmp_sum + x) * tmp_sum < 0:  # 異符号
			tmp_sum += x
		else:
			 if tmp_sum > 0:
			 	cnt += abs(-tmp_sum-1-x)
			 	tmp_sum = -1
			 else:
			 	cnt += abs(-tmp_sum+1-x)
			 	tmp_sum = 1
	return cnt

ans = 0
if A[0] > 0:
	ans = solve(A)
	A[0] = -1
	ans = min(ans, solve(A)+abs(A[0]+1))
elif A[0] < 0:
	ans = solve(A)
	A[0] = 1
	ans = min(ans, solve(A)+abs(A[0]-1))
else:
	A[0] = 1
	ans = solve(A)+1
	A[0] = -1
	ans = min(ans, solve(A)+1)
print(ans)