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()))
pre_total = a[0]
cnt = 0
for i in range(1, len(a)):
    new_total = pre_total + a[i]
    
    if pre_total <0 and new_total <= 0:
        a[i] +=  abs(new_total) + 1
        cnt += abs(new_total) + 1
        new_total = pre_total + a[i]
        pre_total = new_total
            
    elif pre_total >0 and new_total >= 0:
        a[i] -=  abs(new_total) + 1
        cnt += abs(new_total) + 1
        new_total = pre_total + a[i]
        pre_total = new_total
    
    else:
        pre_total = new_total
        
print(cnt)

Submission status: Wrong Answer

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

temp = s[0]
cand1 = 0
for i in range(1,n):
    temp += s[i]
    if i%2 == 0:#正になってほしい
        if temp <= 0:
            cand1 += abs(temp-1)
            temp = 1
    else:#負になって欲しい
        if temp >= 0:
            cand1 += abs(temp+1)
            temp = -1

temp = s[0]
cand2 = 0
for i in range(1,n):
    temp += s[i]
    if i%2 == 0:
        if temp >= 0:
            cand2 += abs(temp+1)
            temp = -1
    else:
        if temp <= 0:
            cand2 += abs(temp-1)
            temp = 1

print(min(cand2,cand1))

Submission status: Wrong Answer

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

temp = s[0]
ans = 0

#第一項が正ならば
if temp > 0:
    for i in range(1,n):
        temp += s[i]
        if i%2 == 0:#正になってほしい
            if temp <= 0:
                ans += abs(temp-1)
                temp = 1
        else:#負になって欲しい
            if temp >= 0:
                ans += abs(temp+1)
                temp = -1
else:
    for i in range(1,n):
        temp += s[i]
        if i%2 == 0:
            if temp >= 0:
                ans += abs(temp+1)
                temp = -1
        else:
            if temp <= 0:
                ans += abs(temp-1)
                temp = 1

print(ans)

Submission status: Wrong Answer

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

temp = s[0]
ans = 0

#第一項が正ならば
if temp > 0:
    for i in range(1,n):
        temp += s[i]
        if i%2 == 0:#正になってほしい
            if temp <= 0:
                ans += abs(temp-1)
                temp = 1
        else:#負になって欲しい
            if temp >= 0:
                ans += abs(temp+1)
                temp = -1

if temp < 0:
    for i in range(1,n):
        temp += s[i]
        if i%2 == 0:
            if temp >= 0:
                ans += abs(temp+1)
                temp = -1
        else:
            if temp <= 0:
                ans += abs(temp-1)
                temp = 1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
sum = 0
res = [0,0]
for change in range(2):
  sum = 0
  for i in range(n):
    if i % 2 == change:
      if sum + a[i] < 0: # -だったら
        #temp = -1 - sum - a[i]
        temp = 1 - sum - a[i]
        res[change] += temp
        sum += temp
      elif sum + a[i] == 0:
        sum += 1
        res[change] += 1
    else:
      if sum + a[i] > 0:
        #temp = 1 - sum - a[i]
        temp = -1 - sum - a[i]
        res[change] -= temp
        sum += temp
      elif sum + a[i] == 0:
        sum += 1
        res[change] += 1
    sum += a[i]
print(min(res[0],res[1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int,input().split()))
a = [A,A]
res = [0,0]
sum = 0
for check in range(2):
  sum = 0
  if check == 1:
    if a[1][0] > 0:
      temp = -1 - a[1][0]
      a[1][0] += temp
      res[1] += temp * -1
    elif a[1][0] < 0:
      temp = 1 - a[1][0]
      a[1][0] += temp
      res[1] += temp
#  if a[check][0] == 0:
#    if check == 0:
#      a[0][0] += 1
#    else:
#      a[0][0] -= 1
#    res[check] += 1
  for i in range(n-1):
    sum += a[check][i]
    if sum * (sum + a[check][i+1]) >= 0:
      if sum > 0:
        temp = -1 - sum - a[check][i+1]
        a[check][i+1] += temp
        res[check] += temp * -1
      else:
        temp = 1 - sum - a[check][i+1]
        a[check][i+1] += temp
        res[check] += temp
print(min(res[0],res[1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int,input().split()))
S = [0]*N
S[0] = a[0]

for i in range(1,N):
    S[i] = S[i-1] + a[i]

count = [0]*N
num = [0]*2
for i in range(2):
    value = 0
    for j in range(N):
        if (S[j]+value)*((-1)**(i+j)) <= 0:
            num[i] += abs(S[j] + value - (-1)**(i+j))
            value += (-1)**(i+j) - (S[j]-value)
        #print(i,num,value)

#print(S)
print(min(num))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int,input().split()))
S = [0]*N
S[0] = a[0]

for i in range(1,N):
    S[i] = S[i-1] + a[i]

count = [0]*N
num = [0]*2
for i in range(2):
    value = 0
    for j in range(N):
        if (S[j]+value)*((-1)**(i+j)) <= 0:
            num[i] += abs(S[j] + value - (-1)**(i+j))
            value += (-1)**(i+j) - S[j]
        #print(i,num,value)

#print(S)
print(min(num))

Submission status: Wrong Answer

Solution code:
n = input()
num_list = list(map(int,input().split()))
count = 0
numsum = 0
for i in range(len(num_list)):
    if i == 0:
        numsum += num_list[0]
    else:
        numsum_pre = numsum
        numsum += num_list[i]
    #    print(numsum)
        if  numsum == 0:
            count += 1
            if numsum_pre < 0:
                numsum += 1
            else:
                numsum-= 1

        if numsum_pre*numsum > 0:
            if numsum_pre < 0:
                count += abs(numsum) +1
                numsum += abs(numsum) +1
     #           print('count{}'.format(abs(numsum-numsum_pre) +1))
            else:
      #          print(numsum)
       #         print('count{}'.format(abs(numsum-numsum_pre) +1))
                count += abs(numsum) +1
                numsum-= (abs(numsum) +1)
        
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
 
lis=[]
now=0
for num in a:
    now+=num
    lis.append(now)
 
ans=10**20
cnt=0
sm=0
for i in range(len(lis)):
    if i%2==0:
        if lis[i]+sm >= 0:
            add = lis[i]+sm+1
            cnt+= add
            sm=-add
    else:
        if lis[i]+sm <= 0:
            add = abs(1-lis[i]-sm)
            cnt+= add
            sm=add
ans=min(ans,cnt)
 
cnt=0
sm=0
for i in range(len(lis)):
    if i%2==1:
        if lis[i]+sm >= 0:
            add = lis[i]+sm+1
            cnt+= add
            sm=-add
    else:
        if lis[i]+sm <= 0:
            add = abs(1-lis[i]-sm)
            cnt+= add
            sm=add
ans=min(ans,cnt)
print(ans)

Submission status: Wrong Answer

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

ans=10**10
s=0
cnt=0

for i in range(n):
    s+=a[i]
    if i%2==1:
        if s<=0:
            cnt += -s + 1
            s = 1
    else:
        if s>=0:
            cnt += s + 1
            s = -1
ans=min(ans,cnt)
s=0
cnt=0
for i in range(n):
    s+=a[i]
    if i%2==0:
        if s<=0:
            cnt += -s + 1
            s = 1
    else:
        if s>=0:
            cnt += s + 1
            s = -1

ans=min(ans,cnt)

print(ans)

Submission status: Wrong Answer

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

ans=10**10
s=0
cnt=0

for i in range(n):
    s+=a[i]
    if i%2==0:
        if s>=0:
            cnt += s + 1
            s = -1
    else:
        if s<=0:
            cnt += -s + 1
            s = 1
ans=min(ans,cnt)

s=0
cnt=0
for i in range(n):
    s+=a[i]
    if i%2==1:
        if s>=0:
            cnt += s + 1
            s = -1
    else:
        if s<=0:
            cnt += -s + 1
            s = 1
ans=min(ans,cnt)

print(ans)

Submission status: Wrong Answer

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

ans=10**10
s = 0
cnt=0
for i in range(n):
    s+=a[i]
    if i%2==1:
        if s<=0:
            cnt += -s + 1
            s = 1
    else:
        if s>=0:
            cnt += s + 1
            s = -1
ans=min(ans,cnt)
s = 0
cnt=0
for i in range(n):
    s+=a[i]
    if i%2==0:
        if s<=0:
            cnt += -s + 1
            s = 1
    else:
        if s>=0:
            cnt += s + 1
            s = -1

ans=min(ans,cnt)
print(ans)

Submission status: Wrong Answer

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

ans=10**10
s = 0
cnt=0
for i in range(n):
    s+=a[i]
    if i%2==1:
        if s<=0:
            cnt += -s + 1
            s = 1
    else:
        if s>=0:
            cnt += s + 1
            s = -1
ans=min(ans,cnt)
s = 0
cnt=0
for i in range(n):
    s+=a[i]
    if i%2==1:
        if s<=0:
            cnt += -s + 1
            s = 1
    else:
        if s>=0:
            cnt += s + 1
            s = -1

ans=min(ans,cnt)
print(ans)

Submission status: Wrong Answer

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

ans=10**10
s=0
cnt=0
for i in range(n):
    s+=a[i]
    if i%2==0:
        if s>=0:
            cnt+=1+s
            s=-1
    else:
        if s<=0:
            cnt+=1-s
            s=1
ans=min(ans,cnt)
s=0
cnt=0
for i in range(n):
    s+=a[i]
    if i%2==1:
        if s>=0:
            cnt+=1+s
            s=-1
    else:
        if s<=0:
            cnt+=1-s
            s=1
ans=min(ans,cnt)

print(ans)

Submission status: Wrong Answer

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

ans=10**10
now=cnt=0
for i in range(len(a)):
    now+=a[i]
    if i%2==0:
        if now>=0:
            cnt+=now+1
            now=-1
    else:
        if now<=0:
            cnt+=1-now
            now=1
ans=min(ans,cnt)

now=cnt=0
for i in range(len(a)):
    now+=a[i]
    if i%2==1:
        if now>=0:
            cnt+=(now+1)
            now=-1
    else:
        if now<=0:
            cnt+=1-now
            now=1
ans=min(ans,cnt)
print(ans)

Submission status: Wrong Answer

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

lis=[]
now=0
for num in a:
    now+=num
    lis.append(now)

ans=10**10
cnt=0
sm=0
for i in range(len(lis)):
    if i%2==0:
        if lis[i]+sm >= 0:
            add = lis[i]+sm+1
            cnt+= add
            sm-=add
    else:
        if lis[i]+sm <= 0:
            add = 1-lis[i]-sm
            cnt+= add
            sm+=add
ans=min(ans,cnt)

cnt=0
sm=0
for i in range(len(lis)):
    if i%2==1:
        if lis[i]+sm >= 0:
            add = lis[i]+sm+1
            cnt+= add
            sm-=add
    else:
        if lis[i]+sm <= 0:
            add = 1-lis[i]-sm
            cnt+= add
            sm+=add
ans=min(ans,cnt)
print(ans)

Submission status: Wrong Answer

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

lis=[]
now=0
for num in a:
    now+=num
    lis.append(now)

ans=10**10
cnt=0
sm=0
for i in range(len(lis)):
    if i%2==0:
        if lis[i]+sm >= 0:
            add = lis[i]+sm+1
            cnt+= add
            sm=-add
    else:
        if lis[i]+sm <= 0:
            add = abs(1-lis[i]-sm)
            cnt+= add
            sm=add
ans=min(ans,cnt)

cnt=0
sm=0
for i in range(len(lis)):
    if i%2==1:
        if lis[i]+sm >= 0:
            add = lis[i]+sm+1
            cnt+= add
            sm=-add
    else:
        if lis[i]+sm <= 0:
            add = abs(1-lis[i]-sm)
            cnt+= add
            sm=add
ans=min(ans,cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
# https://atcoder.jp/contests/abc059/tasks/arc072_a
# 累積和を作っておいて、その累積和の符号が交互になるようにしていく

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


class cumsum1d:  # 一次元累積和クラス
    def __init__(self, ls: list):
        '''
        1次元リストを受け取る
        '''
        from itertools import accumulate
        self.ls_accum = [0] + list(accumulate(ls))

    def total(self, i, j):
        # もとの配列lsにおける[i,j)の中合計
        return self.ls_accum[j] - self.ls_accum[i]

    def __call__(self, i):
        # i番目までの合計
        return self.ls_accum[i + 1]


N = read_a_int()
A = read_ints()

A = cumsum1d(A)
# ただし第一項が0とか途中0になる場合がめんどくさい
# →二通りやって少ないほうでいいじゃん
ans1 = 0
bias = 0
pre_sign = 1  # 第一項が負と仮定
for i in range(N):
    now = A(i) + bias
    if (-1, 1)[pre_sign] * now < 0:  # 異符号
        pass
    elif (-1, 1)[pre_sign] * now >= 0:  # 同符号
        ans1 += abs(now) + 1
        bias += (1, -1)[pre_sign] * ans1
    pre_sign = 1 - pre_sign

ans2 = 0
bias = 0
pre_sign = 0  # 第一項が正と仮定
for i in range(N):
    now = A(i) + bias
    if (-1, 1)[pre_sign] * now < 0:  # 異符号
        pass
    elif (-1, 1)[pre_sign] * now >= 0:  # 同符号
        ans2 += abs(now) + 1
        bias += (1, -1)[pre_sign] * ans2
    pre_sign = 1 - pre_sign
print(min(ans1, ans2))

Submission status: Wrong Answer

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

"""
i番目を+1すると、
i+k番目は+(k+1)される
回数を保持しておいて、後で足す?
"""
from itertools import accumulate
acc = list(accumulate(As))
# print(acc)
prev = acc[0]
ans = 0
cnt = 0 #累積カウント
for a in acc[1:]:
    a += cnt
    # print("cnt",cnt,"pre",prev,"a",a)
    if prev > 0 and a >= 0:
        cnt -= (a+1)
        ans += a+1
        prev = -1
    elif prev < 0 and a <= 0:
        cnt += abs(a-1)
        ans += abs(a-1)
        prev = 1
    else:
        prev = a
print(ans)

Submission status: Wrong Answer

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

"""
i番目を+1すると、
i+k番目は+(k+1)される
回数を保持しておいて、後で足す?
"""
from itertools import accumulate
acc = list(accumulate(As))
# print(acc)
prev = acc[0]
ans = 0
cnt = 0 #累積カウント
for a in acc[1:]:
    a += cnt 
    # print(a,"a")
    if prev * a < 0:
        prev = a
    elif prev > 0 and a == 0:
        cnt -= 1
        ans += 1
        prev = -1
    elif prev < 0 and a == 0:
        cnt += 1
        ans += 1
        prev = 1
    elif prev > 0 and a > 0:
        # print("aa")
        cnt_prev = prev - 1
        cnt -= cnt_prev
        ans += cnt_prev
        a -= cnt_prev
        if a < 0:
            prev = a
        else:
            cnt -= (a+1) #上で引いたぶんで消さなくて良い可能性はある
            ans += (a+1)
            prev = -1
    else: #prev < 0 and a <0
        # print("bb")
        cnt_prev = -1 - prev
        cnt += cnt_prev
        ans += cnt_prev
        a += cnt_prev
        if a > 0:
            prev = a
        else:
            cnt += (a+1)
            ans += (a+1)
            prev = 1
    
    # print(prev,ans,cnt)

print(ans)

Submission status: Wrong Answer

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

s = 0
cnt = 0
for i in range(n):
    s += a[i]
    if i % 2 == 0:
        if s <= 0:
            cnt += abs(s) + 1
            s = 1
    else:
        if s >= 0:
            cnt += abs(s) + 1
            s -= 1
ans1 = cnt

s = 0
cnt = 0
for i in range(n):
    s += a[i]
    if i % 2 != 0:
        if s <= 0:
            cnt += abs(s) + 1
            s = 1
    else:
        if s >= 0:
            cnt += abs(s) + 1
            s = -1
ans2 = cnt

print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(n: int, a: "List[int]"):
    def _solve():
        from itertools import cycle
        ab = 0
        for aa in map(lambda a_o: a_o[0]*a_o[1], zip(a, cycle([1, -1] if a[0] > 0 else [-1, 1]))):
            ab -= aa
            if ab >= 0:
                yield ab + 1
                ab = -1
            ab = abs(ab)
    return sum(_solve())

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(n)]  # type: "List[int]"
    print(solve(n, a))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(n: int, a: "List[int]"):
    def _solve():
        from itertools import cycle
        ab = 0
        for aa in map(lambda a_o: a_o[0]*a_o[1], zip(a, cycle([1, -1] if a[0] > 0 else [-1, 1]))):
            ab -= aa
            if ab >= 0:
                yield ab + 1
                ab = 1
            ab = abs(ab)
    return sum(_solve())

# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(n)]  # type: "List[int]"
    print(solve(n, a))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
INF = 10**5
MOD = 10**9 + 7
sys.setrecursionlimit(100000000)
from functools import lru_cache

def main():
    n = int(input())
    a = list(map(int,input().split()))
    
    i = 0
    ans = 0
    while a[i] == 0:
        if i == 0:
            ans += 1
        else:
            ans += 2
        i += 1  
    
    if i != 0 and a[i] > 0:
        a[i - 1] = -1
    elif i != 0 and a[i] < 0:
        a[i - 1] = 1
    else:
        i += 1

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

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, *A = map(int, open(0).read().split())
def sgn(n):
    return 0 if n==0 else 1 if n>0 else -1

C = [0, 0]
S = [1, -1]

for a in A:
    for i, s in enumerate(S):
        sgn_sum = sgn(s)
        if sgn(s+a) == -sgn_sum:
            S[i] += a
        else:
            C[i] += abs(s+a+sgn_sum)
            S[i] = -sgn_sum

print(min(C))

Submission status: Wrong Answer

Solution code:
n, *A = map(int, open(0).read().split())
def sgn(n):
    return 0 if n==0 else 1 if n>0 else -1

C = [0, 0]
S = [1, -1]

for a in A:
    for i, s in enumerate(S):
        sgn_sum = sgn(s)
        if sgn(s+a) != sgn_sum:
            S[i] += a
        else:
            C[i] += abs(s+a+sgn_sum)
            S[i] = -sgn_sum

print(min(C))

Submission status: Wrong Answer

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

def solve(tmp):
    ans = 0
    for i in range(1, n):
        if tmp * (tmp + a[i]) < 0:
            tmp += a[i]
        else:
            ans += abs(tmp + a[i]) + 1
            if tmp < 0:
                tmp = 1
            else:
                tmp = - 1
    return ans
  
ans = 0
if a[0] == 0:
    ans = min(solve(1) + 1, solve(-1) + 1)
else:
    ans = solve(a[0])
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

A = a[0]

if A != 0:
    ans = 0

    for i in range(1, n):
        nextA = A + a[i]
        if (A > 0 and nextA < 0) or (A < 0 and nextA > 0):
            A = nextA
        elif nextA == 0 and A > 0:
            ans += 1
            A = -1
        elif nextA == 0 and A < 0:
            ans += 1
            A = 1
        elif A > 0:
            ans += abs(nextA) + 1
            A = -1
        else:
            ans += abs(nextA) + 1
            A = 1

    print(ans)
    sys.exit()

# ans1 = 1
# A1 = 1
# for i in range(1, n):
#     nextA = A1 + a[i]
#     if (A1 > 0 and nextA < 0) or (A1 < 0 and nextA > 0):
#         A1 = nextA
#     elif nextA == 0 and A1 > 0:
#         ans1 += 1
#         A1 = -1
#     elif nextA == 0 and A1 < 0:
#         ans1 += 1
#         A1 = 1
#     elif A1 > 0:
#         ans1 += abs(nextA) + 1
#         A1 = -1
#     else:
#         ans1 += abs(nextA) + 1
#         A1 = 1

# ans2 = 1
# A2 = -1
# for i in range(1, n):
#     nextA = A2 + a[i]
#     if (A2 > 0 and nextA < 0) or (A2 < 0 and nextA > 0):
#         A2 = nextA
#     elif nextA == 0 and A2 > 0:
#         ans2 += 1
#         A2 = -1
#     elif nextA == 0 and A2 < 0:
#         ans2 += 1
#         A2 = 1
#     elif A2 > 0:
#         ans2 += abs(nextA) + 1
#         A2 = -1
#     else:
#         ans2 += abs(nextA) + 1
#         A2 = 1

# print(min(ans1, ans2))

Submission status: Wrong Answer

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

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

A = a[0]

if A != 0:
    ans = 0

    for i in range(1, n):
        nextA = A + a[i]
        if (A > 0 and nextA < 0) or (A < 0 and nextA > 0):
            A = nextA
        elif nextA == 0 and A > 0:
            ans += 1
            A = -1
        elif nextA == 0 and A < 0:
            ans += 1
            A = 1
        elif A > 0:
            ans += abs(nextA) + 1
            A = -1
        else:
            ans += abs(nextA) + 1
            A = 1

    print(ans)
    sys.exit()

ans1 = 1
A1 = 1
for i in range(1, n):
    nextA = A1 + a[i]
    if (A1 > 0 and nextA < 0) or (A1 < 0 and nextA > 0):
        A1 = nextA
    elif nextA == 0 and A1 > 0:
        ans1 += 1
        A1 = -1
    elif nextA == 0 and A1 < 0:
        ans1 += 1
        A1 = 1
    elif A1 > 0:
        ans1 += abs(nextA) + 1
        A1 = -1
    else:
        ans1 += abs(nextA) + 1
        A1 = 1

ans2 = 1
A2 = -1
for i in range(1, n):
    nextA = A2 + a[i]
    if (A2 > 0 and nextA < 0) or (A2 < 0 and nextA > 0):
        A2 = nextA
    elif nextA == 0 and A2 > 0:
        ans2 += 1
        A2 = -1
    elif nextA == 0 and A2 < 0:
        ans2 += 1
        A2 = 1
    elif A2 > 0:
        ans2 += abs(nextA) + 1
        A2 = -1
    else:
        ans2 += abs(nextA) + 1
        A2 = 1

print(min(ans1, ans2))

Submission status: Wrong Answer

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

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

A = a[0]
ans = 0

for i in range(1, n):
    nextA = A + a[i]
    if (A > 0 and nextA < 0) or (A < 0 and nextA > 0):
        A = nextA
    elif A > 0:
        ans += nextA + 1
        A = -1
    else:
        ans += abs(nextA) + 1
        A = 1

print(ans)

Submission status: Wrong Answer

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    a=LI()
    ans=0
    pos=-1

    S=a[0]
    if S==0:
        S=1
    if S>0:
        pos=1
    
    
    for i in range(N-1):
        S+=a[i+1]
        if S>0:
            Sp=1
        elif S<0:
            Sp=-1
        else:
            Sp=0
        
        
        if Sp!=pos*(-1):
            target=-1*pos
            ans+=abs(target-S)
            S=target
        pos*=-1
        
    if a[0]==0:
        S=-1
        for i in range(N-1):
            S+=a[i+1]
        if S>0:
            Sp=1
        elif S<0:
            Sp=-1
        else:
            Sp=0
        
        
        if Sp!=pos*(-1):
            target=-1*pos
            ans+=abs(target-S)
            S=target
        pos*=-1
    print(ans)
        
        

main()

Submission status: Wrong Answer

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

n = int(input().strip())
A_list = list(map(int, input().rstrip().split()))
#-----

S=A_list[0]

cnt=0
for i in range(1,n):
    S_pre = S
    S += A_list[i]
    
    if S == 0:
        S += 1*(S_pre < 0) + (-1)*(S_pre > 0)
        cnt  += 1
    elif S * S_pre > 0:
        diff = abs(S) + 1
        S += diff * (1*(S_pre < 0) + (-1)*(S_pre > 0))
        cnt += diff

print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
ret = 0
if A[0] == 0:
  i = 0
  while A[i] == 0:
    i += 1
  if (i%2 == 1 and A[i] > 0) or (i%2 == 0 and A[i] < 0):
    A[0] = -1
  else:
    A[0] = 1
  ret += 1
s = A[0]
for e in A[1:]:
  #print(e,ret,s,s+e)
  if 0 < s and 0 <= s + e:
    ret += abs(s + e + 1)
    s = -1
  elif s < 0 and s + e <= 0:
    ret += abs(s + e - 1)
    s = 1
  else:
    s += e
print(ret)

Submission status: Wrong Answer

Solution code:
a = [1,-3, 1, 0]
X = 0
ans = 0
for i in a:
    X += i
    if X > 0:
        b = -1 - X
        ans += b - a[i+1]
    else:
        b = 1 - X
        ans += b - a[i+1]
print (ans)

Submission status: Wrong Answer

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

prev = A[0]
cnt = 0
for i in range(1, n):
  #print("prev", prev)
  if prev > 0:
    if prev + A[i] > 0:
      cnt += abs(prev + A[i]) + 1
      prev = -1
    elif prev + A[i] == 0:
      cnt += 1
      prev = -1
    else: 
      prev += A[i]
  elif prev < 0:
    if prev + A[i] < 0:
      cnt += abs(prev + A[i]) + 1
      prev = 1
    elif prev + A[i] == 0:
      cnt += 1
      prev = 1
    else:  
      prev += A[i]
  #print("iter, cur#, adjust, sum til cur#", i, A[i], cnt, prev)

print(cnt)

Submission status: Wrong Answer

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

def test(total, res):
    res = 0
    hugou = total > 0
    for i in a[1:]:
        total += i
        if hugou == (total>0):
            res += abs(total)+1
            if hugou:
                total = -1
            else:
                total = 1
        hugou = total>0
    return res

    
res = 0
total = a[0]
if a[0]<=0:
    total = 1
    res += abs(a[0])+1
res1 = test(total, res)

res = 0
total = a[0]
if a[0]>=0:
    total = -1
    res += abs(a[0])+1
res2 = test(total, res)

print(min(res1, res2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
check = ''
if a[0] > 0:
    check = '+'
else:
    check = '-'

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
check = ''
if a[0] > 0:
    check = '+'
else:
    check = '-'

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