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:
from sys import stdin


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    n = int(_in[0])  # type:int
    a_arr = list(map(int,_in[1].split(' ')))  # type:list(int)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    cnt = 0
    sum_ = 0
    for i in range(0,n):
        if i==0:
            if a_arr[0]==0:
                while a_arr[i]==0:
                    i+=1
                else:
                    if a_arr[i+1]>0:
                        sum_ = (-1)**i
                        cnt += 1
                    else:
                        sum_ = (-1)**(i+1)
                        cnt += 1
            else:
                sum_ = a_arr[i]
        else:
            if sum_<0:
                if sum_+a_arr[i]<=0:
                    cnt += 1-(sum_+a_arr[i])
                    sum_ = 1
                else:
                    sum_ = sum_+a_arr[i]
            else:
                if sum_+a_arr[i]>=0:
                    cnt += 1+(sum_+a_arr[i])
                    sum_ = -1
                else:
                    sum_ = sum_+a_arr[i]
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)

Submission status: Wrong Answer

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

sum = arr[0]
ans = 0
for i in range(1,N,1):
    if sum>0:
        fugo ='plus'
    else:
        fugo ='mainus'

    sum += arr[i]
    if sum>0:
        after_fugo ='plus'
    elif sum<0:
        after_fugo ='mainus'
    else:
        after_fugo = fugo

    if fugo == after_fugo:
        if fugo == 'plus':
            ans += abs(sum)+1
            if sum>=0:
                arr[i] = -(abs(sum)+1)
            else:
                arr[i] = abs(sum)+1
        if fugo == 'mainus':
            ans += abs(sum)+1
            if sum>=0:
                arr[i] = -(abs(sum)+1)
            else:
                arr[i] = abs(sum)+1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, *a = map(int, read().split())
now = a[0]
ans = 0
for i, aa in enumerate(a[1:]):
    if now > 0:
        if now + aa + 1 < 0:
            now += aa
        else:
            ans += max(0, now + aa + 1)
            now = -1
    else:
        if -(now + aa) + 1 < 0:
            now += aa
        else:
            ans += max(0, -(now + aa) + 1)
            now = 1
print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce
 
N =int(input())
A = list(map(int, input().split()))
def check(A):
    arr1 = [0]*N
    ans1 = 0
    for i in range(len(A)):
        if i ==0:
            arr1[0] = A[0]
        else:
            arr1[i] += arr1[i-1] + A[i]
            if arr1[i]*arr1[i-1] >0:
                if arr1[i-1] >=0: #-に
                    arr1[i] = -1 
                elif arr1[i-1] <=0: # 無理やり+にする. 
                    arr1[i] = 1 
                ans1 += abs(A[i])+1
          
    ans2 = 0
    arr2 = [0]*N
    #change head.
    for i in range(len(A)):
        if i ==0:
            if A[0]>=0:
                arr2[0] = -1
            elif A[0]<=0:
                arr2[0] = 1
            ans2 += abs(A[0])+1
        else:
            arr2[i] += arr2[i-1] + A[i]
            if arr2[i]*arr2[i-1] >0:
                if arr2[i-1]>=0:
                    arr2[i] = -1 
                elif arr2[i-1]<=0:
                    arr2[i] = 1 
                ans2 += abs(A[i])+1

    return min(ans1,ans2)
 
print(check(A))

Submission status: Wrong Answer

Solution code:
from functools import reduce
 
N =int(input())
A = list(map(int, input().split()))
def check(A):
    arr1 = [0]*N
    ans1 = 0
    for i in range(len(A)):
        if i ==0:
            arr1[0] = A[0]
        else:
            arr1[i] += arr1[i-1] + A[i]
            if arr1[i]*arr1[i-1] >0:
                if arr1[i-1] >=0: #-に
                    arr1[i] = -1 
                elif arr1[i-1] <=0: # 無理やり+にする. 
                    arr1[i] = 1 
                print(arr1[i-1],arr1[i])
                ans1 += abs(A[i])+1
          
    ans2 = 0
    arr2 = [0]*N
    #change head.
    for i in range(len(A)):
        if i ==0:
            if A[0]>=0:
                arr2[0] = -1
            elif A[0]<=0:
                arr2[0] = 1
            ans2 += abs(A[0])+1
        else:
            arr2[i] += arr2[i-1] + A[i]
            if arr2[i]*arr2[i-1] >0:
                if arr2[i-1]>=0:
                    arr2[i] = -1 
                elif arr2[i-1]<=0:
                    arr2[i] = 1 
                print(arr2[i-1],arr2[i])
                ans2 += abs(A[i])+1

    return min(ans1,ans2)
 
print(check(A))

Submission status: Wrong Answer

Solution code:
from functools import reduce
 
N =int(input())
A = list(map(int, input().split()))
def check(A):
    arr1 = [0]*N
    ans1 = 0
    for i in range(len(A)):
        if i ==0:
            arr1[0] = A[0]
        else:
            arr1[i] += arr1[i-1] + A[i]
            if arr1[i]*arr1[i-1] >0:
                if arr1[i-1] >=0:
                    arr1[i] = -1
                elif arr1[i-1] <=0:
                    arr1[i] = 1
                ans1 += abs(arr1[i])+1
          
    ans2 = 0
    arr2 = [0]*N
    #change head.
    for i in range(len(A)):
        if i ==1:
            if A[0]>=0:
                arr2[0] = -1
            elif A[0]<=0:
                arr2[0] = 1
            ans2 += (abs(A[0])+1)
        else:
            arr2[i] += arr2[i-1] + A[i]
            if arr2[i]*arr2[i-1] >=0:
                if arr2[i-1]>=0:
                    arr2[i] = -1
                elif arr2[i-1]<=0:
                    arr2[i] = 1
            ans2 += abs(arr2[i])+1

    return min(ans1,ans2)
 
print(check(A))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int,input().split()))
s_prev = s =  A.pop(0)
ans = 0
#条件を満たしている数列を判定する
for a in A:
    s = s_prev+a
    #print("s={}".format(s))
    if s_prev*s < 0:
        s_prev += a
        #print("s_prev={}".format(s_prev))
        continue
    else:
        ans += abs(s_prev+a) + 1
        #print("ans={}".format(ans))
        s_prev = (s_prev<0)-(s_prev>0)
        #print("s_prev={}".format(s_prev))
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
now_a = a[0]
count = 0

'''
True = positive
False = negative
'''
# sign = True
# if now_a < 0:
#     sign = False
#
# for i in range(1, n):
#     next_a = now_a + a[i]
#     if sign:
#         if next_a >= 0:
#             count += next_a + 1
#             now_a = -1
#         else:
#             now_a = next_a
#         sign = False
#     else:
#         if next_a <= 0:
#             count += abs(next_a) + 1
#             now_a = 1
#         else:
#             now_a = next_a
#         sign = True
# print(count)

sa = a[0]
sign = True
if sa < 0:
    sign = False

for i in range(1, n):
    na = sum(a[:i+1])
    if sign and na >= 0:
        a[i] = -1 * (na + 1)
        count += na + 1
    elif not sign and na <= 0:
        a[i] = 1 - na
        count += 1 - na

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
now_a = a[0]
r, tmp, count = 0, 0, 0
# for i in range(1, n):
#     tmp = t + a[i]
#     if t < 0 and tmp < 0:
#         r = 1 - tmp
#     elif t > 0 and tmp > 0:
#         r = -tmp - 1
#     elif tmp == 0:
#         if t < 0:
#             r = 1 - t - a[i]
#         else:
#             r = -1 - t - a[i]
#     else:
#         r = 0
#     print(t,tmp, r)
#     count += abs(r)
#     t = tmp + r
#     print(t)
# print(count)

'''
True = positive
False = negative
'''
sign = True
if now_a < 0:
    sign = False

for i in range(1, n):
    next_a = now_a + a[i]
    if sign:
        if next_a >= 0:
            count += next_a + 1
            now_a = -1
        else:
            now_a = next_a
        sign = False
    else:
        if next_a <= 0:
            count += abs(next_a) + 1
            now_a = 1
        else:
            now_a = next_a
        sign = True
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
t = a[0]
r, tmp, count = 0, 0, 0
for i in range(1, n):
    tmp = t + a[i]
    if t < 0 and tmp < 0:
        r = 1 - tmp
    elif t > 0 and tmp > 0:
        r = -tmp - 1
    elif tmp == 0:
        if t < 0:
            r = 1 - t - a[i]
        else:
            r = -1 - t - a[i]
        # if a[i] < 0:
        #     r = a[i] - 1
        # elif a[i] > 0:
        #     r = a[i] + 1
    else:
        r = 0
    count += abs(r)
    t = tmp + r

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
t = a[0]
r, tmp, count = 0, 0, 0
for i in range(1, n):
    tmp = t + a[i]
    if t < 0 and tmp < 0:
        r = 1 - tmp
    elif t > 0 and tmp > 0:
        r = -tmp - 1
    elif tmp == 0:
        if t < 0:
            r += 1 - t - a[i]
        else:
            r += -1 - t - a[i]
    else:
        r = 0
    count += abs(r)
    t = tmp + r

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
s, ans, i = 0, 0, 0
while a[i] == 0 and i < n:
    ans += 2
    i += 1
ans = max(0, ans - 1)
if i == n:
    print(ans)
    exit()

if ans > 0:
    if abs(a[i]) == 1:
        ans += 1
        s = a[i]
    else:
        s = abs(a[i]) // a[i] * (abs(a[i]) - 1)
else:
    s = a[0]

##print('{} {} {}'.format(a[i], ans, s))
i += 1
for j in range(i, n):
    if abs(a[j]) > abs(s) and (a[j] == abs(a[j])) != (s == abs(s)):
        s += a[j]
    else:
        pre_s = s
        s = -1 * s // abs(s)
        ans += abs(a[j] - s + pre_s)
##    print('{} {} {}'.format(a[j], ans, s))
print(ans)

Submission status: Wrong Answer

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

count_list = []
previous = ''

flag_list = ['odd','even']

for f in flag_list:
    count = 0
    summary = 0
    for i,a in enumerate(a_list):
        if i == 0:
            if f == 'odd':
                previous = '+'
                if a <= 0:
                    a_result = 1
                    count += 1-a
                else:
                    a_result = a
            else:
                previous = '-'
                if a >= 0:
                    a_result = 1
                    count += 1-a
                else:
                    a_result = a
            summary += a_result
        else:
            if previous == '+':
                if summary + a < 0:
                    a_result = a
                else:
                    a_result = (-1)*abs(-1-summary)
                    count += abs(a-a_result)
                previous = '-'
            else:
                if summary + a > 0:
                    a_result = a
                else:
                    a_result = abs(1-summary)
                    count += abs(a-a_result)
                previous = '+'
            summary += a_result
    count_list.append(count)

print(min(count_list))

Submission status: Wrong Answer

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

count = 0
summary = 0
previous = ''

for i,a in enumerate(a_list):
    if i == 0:
        if a > 0:
            previous = '+'
        elif a < 0:
            previous = '-'
        else:
            flag = False
            for idx,num in enumerate(a_list):
                if num != 0:
                    flag = True
                    if num > 0:
                        if i % 2 == 1:
                            previous = '-'
                            a = -1
                        else:
                            previous = '+'
                            a = 1
                    else:
                        if i % 2 == 0:
                            previous = '-'
                            a = -1
                        else:
                            previous = '+'
                            a = 1
            if flag == False:
                previous = '+'
                a = 1
            count += 1
        summary += a
    else:
        if previous == '+':
            if summary + a < 0:
                a_result = a
            else:
                a_result = (-1)*abs(-1-summary)
                count += abs(a-a_result)
            previous = '-'
        else:
            if summary + a > 0:
                a_result = a
            else:
                a_result = abs(1-summary)
                count += abs(a-a_result)
            previous = '+'
        summary += a_result

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
def judge(count):
  idx = 0
  wa = a[0]
  for i in range(2,n+1):
    if count == -1:
      if wa + a[i-1] < 0:
        wa += a[i-1]
      else:
        check = wa + a[i-1] + 1
        j = a[i-1]
        a[i-1] = j - check
        idx += check
        wa = -1
    elif count == 1:
      if wa +a[i-1] > 0:
        wa += a[i-1]
      else:
        check = 1 - wa - a[i-1]
        j = a[i-1]
        a[i-1] = check+ j
        idx += check
        wa = 1
    count *= -1
  return idx
if a[0] > 0:
  b = judge(-1)
  print(b)
elif a[0] < 0:
  c = judge(1)
  print(c)
else:
  a[0] = 1
  b = judge(-1)
  a[0] = -1
  c = judge(1)
  print(min(b,c))

Submission status: Wrong Answer

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

b = [0]*n
b[0]=a[0]
pt = 0
m = 0
ans = 0
for i in range(1,n):
    b[i]=b[i-1]+a[i]
bo = b[::2]
be = b[1::2]
if sum(bo)>sum(be):
    pt=0
else:
    pt=1

if pt==0:
    for i in range(n):
        if i%2==0 and b[i]+m<=0:
            ans+=1-b[i]-m
            m+=1-b[i]-m
        elif i%2==1 and b[i]+m>=0:
            ans+=1+b[i]+m
            m+=-1-b[i]-m
if pt==1:
    for i in range(n):
        if i%2==1 and b[i]+m<=0:
            ans+=1-b[i]-m
            m+=1-b[i]-m
        elif i%2==0 and b[i]+m>=0:
            ans+=1+b[i]+m
            m+=-1-b[i]-m
print(ans)

Submission status: Wrong Answer

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

b = [0]*n
b[0]=a[0]
st = 0
pt = 0
m = 0
ans = 0
for i in range(1,n):
    b[i]=b[i-1]+a[i]
for i in range(n):
    if b[i]!=0:
        st=i
        break
if (st%2==0 and b[st]>0) or (st%2==1 and b[st]<0):
    pt=0
else:
    pt=1

if pt==0:
    for i in range(n):
        if i%2==0 and b[i]+m<=0:
            ans+=1-b[i]-m
            m+=1-b[i]-m
        elif i%2==1 and b[i]+m>=0:
            ans+=1+b[i]+m
            m+=-1-b[i]-m
if pt==1:
    for i in range(n):
        if i%2==1 and b[i]+m<=0:
            ans+=1-b[i]-m
            m+=1-b[i]-m
        elif i%2==0 and b[i]+m>=0:
            ans+=1+b[i]+m
            m+=-1-b[i]-m
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
if l[0]!=0:
    count=0
    suml=l[0]
    for i in range(1,n):
        if suml>0 and suml+l[i]<0:
            suml+=l[i]
        elif suml<0 and suml+l[i]>0:
            suml+=l[i]
        elif suml>0 and suml+l[i]>=0:
            count+=suml+l[i]+1
            suml=-1
        else:
            count+=-suml-l[i]+1
            suml=1
    a1=count
    count=abs(l[0])+1
    suml=-l[0]
    for i in range(1,n):
        if suml>0 and suml+l[i]<0:
            suml+=l[i]
        elif suml<0 and suml+l[i]>0:
            suml+=l[i]
        elif suml>0 and suml+l[i]>=0:
            count+=suml+l[i]+1
            suml=-1
        else:
            count+=-suml-l[i]+1
            suml=1
    a2=count
    print(min(a1,a2))
    
else:
    count=1
    suml1=1
    for i in range(1,n):
        if suml1>0 and suml1+l[i]<0:
            suml1+=l[i]
        elif suml1<0 and suml1+l[i]>0:
            suml1+=l[i]
        elif suml1>0 and suml1+l[i]>=0:
            count+=suml1+l[i]+1
            suml1=-1
        else:
            count+=-suml1-l[i]+1
            suml1=1
    k1=count
    count=1
    suml2=-1
    for i in range(1,n):
        if suml2>0 and suml2+l[i]<0:
            suml2+=l[i]
        elif suml2<0 and suml2+l[i]>0:
            suml2+=l[i]
        elif suml2>0 and suml2+l[i]>=0:
            count+=suml2+l[i]+1
            suml2=-1
        else:
            count+=-suml2-l[i]+1
            suml2=1
    k2=count
    print(min(k1,k2))

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
if l[0]!=0:
    count=0
    suml=l[0]
    for i in range(1,n):
        if suml>0 and suml+l[i]<0:
            suml+=l[i]
        elif suml<0 and suml+l[i]>0:
            suml+=l[i]
        elif suml>0 and suml+l[i]>=0:
            count+=suml+l[i]+1
            suml=-1
        else:
            count+=-suml-l[i]+1
            suml=1
    print(count)
else:
    count=1
    suml1=1
    for i in range(1,n):
        if suml1>0 and suml1+l[i]<0:
            suml1+=l[i]
        elif suml1<0 and suml1+l[i]>0:
            suml1+=l[i]
        elif suml1>0 and suml1+l[i]>=0:
            count+=suml1+l[i]+1
            suml1=-1
        else:
            count+=-suml1-l[i]+1
            suml1=1
    k1=count
    count=1
    suml2=-1
    for i in range(1,n):
        if suml2>0 and suml2+l[i]<0:
            suml2+=l[i]
        elif suml2<0 and suml2+l[i]>0:
            suml2+=l[i]
        elif suml2>0 and suml2+l[i]>=0:
            count+=suml2+l[i]+1
            suml2=-1
        else:
            count+=-suml2-l[i]+1
            suml2=1
    k2=count
    print(min(k1,k2))

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
if l[0]!=0:
    count=0
    suml=l[0]
    for i in range(1,n):
        if suml>0 and suml+l[i]<0:
            suml+=l[i]
            continue
        elif suml<0 and suml+l[i]>0:
            suml+=l[i]
            continue
        elif suml>0 and suml+l[i]>=0:
            count+=suml+l[i]+1
            suml=-1
        else:
            count+=-suml-l[i]+1
            suml=1
    print(count)
else:
    count=1
    suml1=1
    for i in range(1,n):
        if suml1>0 and suml1+l[i]<0:
            suml1+=l[i]
            continue
        elif suml1<0 and suml1+l[i]>0:
            suml1+=l[i]
            continue
        elif suml1>0 and suml1+l[i]>=0:
            count+=suml1+l[i]+1
            suml1=-1
        else:
            count+=-suml1-l[i]+1
            suml1=1
    k1=count
    count=1
    suml2=-1
    for i in range(1,n):
        if suml2>0 and suml2+l[i]<0:
            suml2+=l[i]
            continue
        elif suml2<0 and suml2+l[i]>0:
            suml2+=l[i]
            continue
        elif suml2>0 and suml2+l[i]>=0:
            count+=suml2+l[i]+1
            suml2=-1
        else:
            count+=-suml2-l[i]+1
            suml2=1
    k2=count
    print(min(k1,k2))

Submission status: Wrong Answer

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


ans = 0
sumTime = time[0]
'''
if (time[0] < 0):
  before = False
else:
  before = True
'''
for i in range(1, n):
    temp = time[i] + sumTime
    check = temp * sumTime
    if (check < 0):
        sumTime = temp
    else:
        dif = abs(temp) + 1
        ans += dif
        if (sumTime < 0):
            sumTime = 1
        else:
            sumTime = -1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input()) #a[0] = 0の場合を考慮できてない説？
a = list(map(int,input().split()))
ans1,ans2 = 0,0
sum1,sum2 = a[0],a[0]
for i in range(1,n):
    sum1 += a[i]
    if i%2 == 1 and sum1 >= 0:
        ans1 += sum1 + 1
        sum1 = -1
    elif i%2 == 0 and sum1 <= 0:
        ans1 += -sum1 + 1
        sum1 = 1
for i in range(1,n):
    sum2 += a[i]
    if i%2 == 0 and sum2 >= 0:
        ans2 += sum2 + 1
        sum2 = -1 #いや違くね？
    elif i%2 == 1 and sum2 <= 0:
        ans2 += -sum2 + 1
        sum2 = 1
print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = 0
sum_a = a[0]
if sum_a > 0:
    for i in range(1,n):
        sum_a += a[i]
        if i%2 == 1 and sum_a >= 0:
            ans += sum_a + 1
            sum_a = -1
        elif i%2 == 0 and sum_a <= 0:
            ans += -sum_a + 1
            sum_a = 1
else:
    for i in range(1,n):
        sum_a += a[i]
        if i%2 == 0 and sum_a >= 0:
            ans += sum_a + 1
            sum_a = -1 #いや違くね？
        elif i%2 == 1 and sum_a <= 0:
            ans += -sum_a + 1
            sum_a = 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = 0
sum_a = a[0]
if sum_a > 0:
    for i in range(1,n):
        sum_a += a[i]
        if i%2 == 1 and sum_a >= 0:
                ans += sum_a + 1
                sum_a = -1 #いや違くね？
        elif i%2 == 0 and sum_a <= 0:
                ans += -sum_a + 1
                sum_a = 1
else:
    for i in range(1,n):
        sum_a += a[i]
        if i%2 == 0 and sum_a >= 0:
                ans += sum_a + 1
                sum_a = -1 #いや違くね？
        elif i%2 == 1 and sum_a <= 0:
                ans += -sum_a + 1
                sum_a = 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

from itertools import accumulate
acc = a
acc = list(accumulate(acc))
# a[i]+...+a[j]までは　b[j+1]-b[i]


def dfs(ind,prev,tmp,ans):
    if ind == n:
        return ans
    now = acc[ind]

    if prev +tmp > 0:
        if now + tmp >= 0:
            ans +=abs(now + tmp +1)
            tmp += -(now + tmp +1)
    elif prev +tmp < 0:
        if now + tmp <= 0:
            ans +=abs(now + tmp -1)
            tmp += -(now + tmp -1)

    return dfs(ind+1, now+tmp,tmp,ans)


ansM = dfs(0,-1,0,0)
ansP = dfs(0,+1,0,0)

print(min(ansM,ansP))

Submission status: Wrong Answer

Solution code:
def resolve(L):
    # L[0]!=0を起点とする
    cnt = 0
    s = L[0]
    for i in range(1,len(L)):
        a = L[i]
        if(s>0 and s+a>=0):
            L[i] = -s-1
            cnt += (s+a+1)
            s = -1
        elif(s<0 and s+a<=0):
            L[i] = -s+1
            cnt += (-s-a+1)
            s = 1
        else:
            s += a
    return cnt      

def ans(L):
    a = L[0]
    c0,c1=0,0

    if (a>0):
        c0 = resolve(L)
        c1 = (a+1) + resolve([-1]+L[1:])
    elif (a<0):
        c0 = resolve(L)
        c1 = (-a+1) + resolve([1]+L[1:])
    else:
        c0 = 1 + resolve([1]+L[1:])
        c1 = 1 + resolve([-1]+L[1:])
    
    return(min(c0,c1))

N = int(input())
L = [int(x) for x in input().split(' ')]

print(ans(L))

Submission status: Wrong Answer

Solution code:
def resolve(SL):
    # L[0]!=0を起点とする
    cnt = 0
    for i in range(len(SL)-1):
        s0 = SL[i]
        s1 = SL[i+1]
        if(s0>0 and s1>=0):
            SL[(i+1):] = [s-(s1+1) for s in SL[(i+1):]]
            cnt += (s1+1)
        elif(s0<0 and s1<=0):
            SL[(i+1):] = [s+(-s1+1) for s in SL[(i+1):]]
            cnt += (-s1+1)  
        print(SL)
    return cnt
            
    
def ans(L):
    SL = [sum(L[:(i+1)]) for i in range(len(L))]
    c0,c1=0,0
    
    if (L[0]>0):
        c0 = resolve(SL)
        c1 = (L[0]+1) + resolve(list(map(lambda x:x-(L[0]+1), SL)))
    elif (L[0]<0):
        c0 = resolve(SL)
        c1 = (-L[0]+1) + resolve(list(map(lambda x:x+(-L[0]+1), SL)))
    else:
        c0 = 1 + resolve(list(map(lambda x:x+1, SL)))
        c1 = 1 + resolve(list(map(lambda x:x-1, SL)))
    
    return(min(c0,c1))
  
N = int(input())
L = [int(x) for x in input().split(' ')]

print(ans(L))

Submission status: Wrong Answer

Solution code:
def resolve(SL):
    # L[0]!=0を起点とする
    cnt = 0
    for i in range(len(SL)-1):
        s0 = SL[i]
        s1 = SL[i+1]
        if(s0>0 and s1>=0):
            SL[(i+1):] = [s-(s1+1) for s in SL[(i+1):]]
            cnt += (s1+1)
        elif(s0<0 and s1<=0):
            SL[(i+1):] = [s+(-s1+1) for s in SL[(i+1):]]
            cnt += (-s1+1)  
#         print(SL)
    return cnt
            
def ans(L):
  SL = [sum(L[:(i+1)]) for i in range(len(L))]
  c0,c1=0,0
  
  if (L[0]>0):
      c0 = resolve(SL)
      c1 = (L[0]+1) + resolve(list(map(lambda x:x-(L[0]+1), SL)))
  elif (L[0]<0):
      c0 = resolve(L)
      c1 = (-L[0]+1) + resolve(list(map(lambda x:x+(-L[0]+1), SL)))
  else:
      c0 = 1 + resolve(list(map(lambda x:x+1, SL)))
      c1 = 1 + resolve(list(map(lambda x:x-1, SL)))
  
  return(min(c0,c1))

N = int(input())
L = [int(x) for x in input().split(' ')]

print(ans(L))

Submission status: Wrong Answer

Solution code:
n = int(input())

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

total = a[0]
ans = 0
hugo = 0
if total == 0:
    if a[1] >= 0:
        total = -1
        ans = 1
        hugo = -1
    else:
        total = 1
        ans = 1
        hugo = 1
elif total > 0:
    ans = 0
    hugo = 1
elif total < 0:
    ans = 0
    hugo = -1


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

print(ans)

Submission status: Wrong Answer

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

    def solve(positive, A):
        total = A[0]
        ope = 0
        for i in range(1, len(A)):
            # print("####")
            # print(total)
            # print(ope)
            if positive:
                # 次は負の数=正の数になるなら補正
                if total + A[i] >= 0:
                    ope += total + A[i] + 1
                    total = total + A[i] - (total + A[i]) - 1
                else:
                    total += A[i]
            else:
                # 次は正の数=負の数になるなら補正
                if total + A[i] <= 0:
                    ope += abs(total + A[i]) + 1
                    total = total + A[i] + abs(total + A[i]) + 1
                else:
                    total += A[i]
            positive = (not positive)
        return ope
    
    print(min(solve(True, A), solve(False, A)))



if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    total = A[0]
    ope = 0
    positive = True if A[0] > 0 else False
    for i in range(1, len(A)):
        if positive:
            # 次は負の数=正の数になるなら補正
            if total + A[i] >= 0:
                ope += abs(total + A[i]) + 1
                total = total + A[i] - abs(total + A[i]) - 1
            else:
                total += A[i]
        else:
            # 次は正の数=負の数になるなら補正
            if total + A[i] <= 0:
                ope += abs(total + A[i]) + 1
                total = total + A[i] + abs(total + A[i]) + 1
            else:
                total += A[i]
        positive = (not positive)
    print(ope)



if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

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

def solve(A):
  res = 0
  pre_a = A[0]
  for a in A[1:]:
    a += pre_a
    if a * pre_a < 0:
      pre_a = a
      continue
    diff = - a + (-1)**(pre_a > 0)
    res += abs(diff)
    pre_a = a + diff
  return res
  
if A[0] != 0:
  ans = solve(A)
else:
  A[0] = 1
  ans = solve(A) + 1
  A[0] = -1
  ans = min(ans, solve(A) + 1)

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
import sys
import math

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

#print(odd)
#print(pair)

## odd
count_odd = 0
current_sum = 0
for i in range(len(a)):
    if i % 2 == 1:
        if current_sum + a[i] < 1:
            diff = 1 - (current_sum + a[i])
            a[i] += diff
            count_odd += diff
    elif i % 2 == 0:
        if current_sum + a[i] > -1:
            diff = -1 - (current_sum + a[i])
            a[i] += diff
            count_odd += -1 * diff

    current_sum += a[i]


## pair
count_pair = 0
current_sum = 0
for i in range(len(a)):
    if i % 2 == 1:
        if current_sum + a[i] > -1:
            diff = -1 - (current_sum + a[i])
            a[i] += diff
            count_pair += -1 * diff
    elif i % 2 == 0:
        if current_sum + a[i] < 1:
            diff = 1 - (current_sum + a[i])
            a[i] += diff
            count_pair += diff
    else:
        print("error")

    current_sum += a[i]


print(min(count_odd, count_pair))

Submission status: Wrong Answer

Solution code:
from sys import stdin
import sys
import math

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

pair = 0
odd = 0


for i in range(0, len(a), 2):
    pair += a[i]

for i in range(1, len(a), 2):
    odd += a[i]

#print(odd)
#print(pair)


count = 0
current_sum = 0
for i in range(len(a)):
    ## odd
    if i % 2 == 1 and odd >= pair:
        if current_sum + a[i] < 1:
            diff = 1 - (current_sum + a[i])
            a[i] += diff
            count += diff
    ## odd
    elif i % 2 == 1 and odd < pair:
        if current_sum + a[i] > -1:
            diff = -1 - (current_sum + a[i])
            a[i] += diff
            count += -1 * diff
    ## pair
    elif i % 2 == 0 and odd >= pair:
        if current_sum + a[i] > -1:
            diff = -1 - (current_sum + a[i])
            a[i] += diff
            count += -1 * diff
    elif i % 2 == 0 and odd < pair:
        if current_sum + a[i] < 1:
            diff = 1 - (current_sum + a[i])
            a[i] += diff
            count += diff
    else:
        print("error")

    current_sum += a[i]


print(count)

Submission status: Wrong Answer

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

sum = a[0]
if sum > 0:
    flag = 1
else:
    flag = -1

ans = 0
for i in range(1, n):
    if flag == 1 and sum + a[i] >= 0:
        #print("OK1")
        ans += abs(sum) + a[i]+ 1
        sum = -1
    elif flag == -1 and sum + a[i] <= 0:
        #print("OK2")
        ans += abs(sum) - a[i] + 1
        sum = 1
    else:
        #print("OK3")
        sum += a[i]
    flag = -flag
    #print(ans, sum, flag)

print(ans)