Problem:


Score : 300 points

Problem Statement

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

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

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

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

Constraints

2 ≤ n ≤ 10^5

|a_i| ≤ 10^9

Each a_i is an integer.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print the minimum necessary count of operations.

Sample Input 1

4
1 -3 1 0

Sample Output 1

4

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

Sample Input 2

5
3 -6 4 -5 7

Sample Output 2

0

The given sequence already satisfies the conditions.

Sample Input 3

6
-1 4 3 2 -5 4

Sample Output 3

8



Sample input:
4
1 -3 1 0


Sample output:
4

Submission status: Wrong Answer

Solution code:
import copy
import sys
write = sys.stdout.write

n = int(input())
A = list(map(int,input().split())) # +, -, +, ...
B = copy.deepcopy(A) #-, +, -, ...
sumA = []
sumB = []
cntA = 0
cntB = 0
if A[0] == 0:
    A[0] += 1
    B[0] -= 1
elif A[0] > 0:
    cntB += (B[0]+1)
    B[0] = -1
else:
    cntA += abs(A[0])+1
    A[0] = 1
sumA.append(A[0])
sumB.append(B[0])
#write("cntA : " + str(cntA) + " cntB : " + str(cntB) + "\n") 
for i in range(1, n):
    tempA = sumA[i-1] + A[i]
    tempB = sumB[i-1] + B[i]
    if i%2 == 1: #Aは-, Bは+
        if tempA == 0:
            #A[i] -= 1
            cntA += 1
            sumA.append(-1)
        elif tempA > 0:
            #A[i] -= abs(tempA) + 1
            cntA += abs(tempA) + 1
            sumA.append(-1)
        else:
            sumA.append(tempA)

        if tempB == 0:
            #B[i] += 1
            cntB += 1
            sumB.append(1)
        elif tempB < 0:
            #B[i] += abs(tempB) + 1
            cntB += abs(tempB) + 1
            sumB.append(1)
        else:
            sumB.append(tempB)
    else: #Aは+, Bは-
        if tempA == 0:
            cntA += 1
            sumA.append(1)
        elif tempA < 0:
            cntA += abs(tempA) + 1
            sumA.append(1)
        else:
            sumA.append(tempA)
        
        if tempB == 0:
            #B[i] -= 1
            cntB += 1
            sumB.append(-1)
        elif tempB > 0:
            #B[i] -= abs(tempB) + 1
            cntB += abs(tempB) + 1
            sumB.append(-1)
        else:
            sumB.append(tempB)
    #write("cntA : " + str(cntA) + " cntB : " + str(cntB) + "\n")

print(str(min(cntA, cntB)))

Submission status: Wrong Answer

Solution code:
import copy
import sys
write = sys.stdout.write

n = int(input())
A = list(map(int,input().split())) # +, -, +, ...
B = copy.deepcopy(A) #-, +, -, ...
sumA = []
sumB = []
cntA = 0
cntB = 0
if A[0] == 0:
    A[0] += 1
    B[0] -= 1
elif A[0] > 0:
    cntB += B[0]+1
    B[0] = -1
else:
    cntA += abs(A[0])+1
    A[0] = 1
sumA.append(A[0])
sumB.append(B[0]) 
for i in range(1, n):
    tempA = sumA[i-1] + A[i]
    tempB = sumB[i-1] + B[i]
    if i%2 == 1: #Aは-, Bは+
        if tempA == 0:
            #A[i] -= 1
            cntA += 1
            sumA.append(-1)
        elif tempA > 0:
            #A[i] -= abs(tempA) + 1
            cntA += abs(tempA) + 1
            sumA.append(-1)
        else:
            sumA.append(tempA)

        if tempB == 0:
            #B[i] += 1
            cntB += 1
            sumB.append(1)
        elif tempB < 0:
            #B[i] += abs(tempA) + 1
            cntB += abs(tempA) + 1
            sumB.append(1)
        else:
            sumB.append(tempB)
    else: #Aは+, Bは-
        if tempA == 0:
            cntA += 1
            sumA.append(1)
        elif tempA < 0:
            cntA += abs(tempA) + 1
            sumA.append(1)
        else:
            sumA.append(tempA)
        
        if tempB == 0:
            #B[i] -= 1
            cntB += 1
            sumB.append(-1)
        elif tempB > 0:
            #B[i] -= abs(tempB) + 1
            cntB += abs(tempB) + 1
            sumB.append(-1)
        else:
            sumB.append(tempB)
            
print(str(min(cntA, cntB)))

Submission status: Wrong Answer

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

count = 0

if A[0] == 0:
    A[0] = -1 if A[1]>0 else 1
    count += 1

t = A[0]
for a in A[1:]:
    if t*(t+a) >= 0:
        if t > 0:
            count += 1+(t+a)
            t = -1
        else:
            count += 1-(t+a)
            t = 1
    else:
        t = t+a

print(count)

Submission status: Wrong Answer

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

tmp = L[0]
res = 0

def diff(a,b):
    if a*b < 0:
        return True
    else:
        return False

if tmp == 0:
    tmp = -1
    res = 1

        
    for i in range(1, N):
        n = L[i] + tmp
        if diff(n, tmp):
            tmp = n
        else:
            if tmp < 0:
                res = res + abs(n) + 1
                tmp = 1
            else:
                res = res + abs(n) + 1
                tmp = -1
    res1 = res
    
    res = 1
    tmp = 1
    for i in range(1, N):
        n = L[i] + tmp
        if diff(n, tmp):
            tmp = n
        else:
            if tmp < 0:
                res = res + abs(n) + 1
                tmp = 1
            else:
                res = res + abs(n) + 1
                tmp = -1
    
    print(min(res, res1))
    
else:
    tmp = L[0]
    res = 0
    for i in range(1, N):
        n = L[i] + tmp
        if diff(n, tmp):
            tmp = n
        else:
            if tmp < 0:
                res = res + abs(n) + 1
                tmp = 1
            else:
                res = res + abs(n) + 1
                tmp = -1
    print(res)

Submission status: Wrong Answer

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

tmp = L[0]
res = 0

def diff(a,b):
    if a*b < 0:
        return True
    else:
        return False

if tmp > 0:
    flag = True
elif tmp < 0:
    flag = False
    
else:
    if L[1] > 0:
        tmp = -1
        res += 1
    else:
        tmp = 1
        res += 1
        
for i in range(1, N):
    n = L[i] + tmp
    if diff(n, tmp):
        tmp = n
    else:
        if tmp < 0:
            res = res + abs(n) + 1
            tmp = 1
        else:
            res = res + abs(n) + 1
            tmp = -1
    
print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
a_t = [0]
for i in range(n):
    a_t.append(a[i] + a_t[i])
ans = 0
ans_ = 0
count = 0
for i in range(1, n + 1):
    if i == 1:
        if a_t[i] >= 0:
            ans += a_t[i] + 1
            count += -(a_t[i]) - 1
    if a_t[i - 1] + count > 0:
        if a_t[i] + count > 0:
            ans += a_t[i] + count + 1
            count += -(a_t[i] + count) - 1
        if a_t[i] + count == 0:
            ans += 1
            count -= 1
    elif a_t[i - 1] + count < 0:
        if a_t[i] + count < 0:
            ans += -(a_t[i] + count) + 1
            count += -(a_t[i] + count) + 1
        if a_t[i] + count == 0:
            ans += 1
            count += 1
for i in range(1, n + 1):
    if i == 1:
        if a_t[i] <= 0:
            ans_ += -a_t[i] + 1
            count += -(a_t[i]) + 1
    if a_t[i - 1] + count > 0:
        if a_t[i] + count > 0:
            ans_ += a_t[i] + count + 1
            count += -(a_t[i] + count) - 1
        if a_t[i] + count == 0:
            ans_ += 1
            count -= 1
    elif a_t[i - 1] + count < 0:
        if a_t[i] + count < 0:
            ans_ += -(a_t[i] + count) + 1
            count += -(a_t[i] + count) + 1
        if a_t[i] + count == 0:
            ans_ += 1
            count += 1
print(min(ans, ans_))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
a_t = [0]
for i in range(n):
    a_t.append(a[i] + a_t[i])
ans = 0
ans_ = 0
count = 0
for i in range(1, n + 1):
    if i == 1:
        if a_t[i] >= 0:
            ans += a_t[i] + 1
            count += -(a_t[i]) - 1
    if a_t[i - 1] + count > 0:
        if a_t[i] + count > 0:
            ans += a_t[i] + count + 1
            count += -(a_t[i] + count) - 1
        if a_t[i] + count == 0:
            ans += 1
            count -= 1
    elif a_t[i - 1] + count < 0:
        if a_t[i] + count < 0:
            ans += -(a_t[i] + count) + 1
            count += -(a_t[i] + count) + 1
        if a_t[i] + count == 0:
            ans += 1
            count += 1
for i in range(1, n + 1):
    if i == 1:
        if a_t[i] <= 0:
            ans_ += a_t[i] + 1
            count += -(a_t[i]) - 1
    if a_t[i - 1] + count > 0:
        if a_t[i] + count > 0:
            ans_ += a_t[i] + count + 1
            count += -(a_t[i] + count) - 1
        if a_t[i] + count == 0:
            ans_ += 1
            count -= 1
    elif a_t[i - 1] + count < 0:
        if a_t[i] + count < 0:
            ans_ += -(a_t[i] + count) + 1
            count += -(a_t[i] + count) + 1
        if a_t[i] + count == 0:
            ans_ += 1
            count += 1
print(min(ans, ans_))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if a[0]>0:
    P=0
    SUMP=a[0]
else:
    P=1-a[0]
    SUMP=1
for i in range(1,n):
    if i%2==0:
        if SUMP+a[i]>=1:
            P+=0
            SUMP=SUMP+a[i]
        else:
            P+=1-(SUMP+a[i])
            SUMP=1
    elif i%2==1:
        if SUMP+a[i]<=(-1):
            P+=0
            SUMP=SUMP+a[i]
        else:
            P+=SUMP+a[i]+1
            SUMP=(-1)
if a[0]<0:
    N=0
    SUMN=a[0]
else:
    N=1-a[0]
    SUMN=(-1)
for i in range(1,n):
    if i%2==1:
        if SUMN+a[i]>=1:
            N+=0
            SUMN=SUMN+a[i]
        else:
            N+=1-(SUMN+a[i])
            SUMN=1
    elif i%2==0:
        if SUMN+a[i]<=(-1):
            N+=0
            SUMN=SUMN+a[i]
        else:
            N+=SUMN+a[i]+1
            SUMN=(-1)
print(min(P,N))

Submission status: Wrong Answer

Solution code:
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = map(int,input().split())|	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = list(map(int,input().split()))	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

import collections
def inpl(): return list(map(int, input().split()))

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

ans = 1e16
for s in [1, -1]:
  res, csum = 0, 0
  for a_i in a:
    csum += a_i
    if csum * s < 0:
      res += abs(csum-s)
      csum = s
    s *= -1
  ans = min(ans, res)

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
ans=0
if A[0]==0:
    for i in range(1,n):
        if A[i]!=0:
            if A[i]>0:
                if i%2==0:
                    A[0]=1
                    ans+=1
                else:
                    A[0]=-1
                    ans+=1
            elif A[i]<0:
                if i%2==0:
                    A[0]=-1
                    ans+=1
                else:
                    A[0]=1
                    ans+=1
S=A[0]
for i in range(1,n):
    if S>0:
        if A[i]+S<0:
            S=S+A[i]
        else:
            ans+=A[i]+S+1
            S=-1
    else:
        if A[i]+S>0:
            S=S+A[i]
        else:
            ans+=1-(A[i]+S)
            S=1
print(ans)

Submission status: Wrong Answer

Solution code:
def solve(N, As):
    positive = True if As[0] < 0 else False
    for i, a in enumerate(As):
        if a != 0:
            if i % 2 == 0:
                positive = True if a < 0 else False
            else:
                positive = False if a < 0 else True
            break

    ans = 0
    i = As[0]
    if i == 0:
        ans = 1
        if positive:
            i = -1
        else:
            i = 1

    for a in As[1:]:
        if positive:
            if i + a <= 0:
                ans += abs(1 - (i + a))
                a += 1 - (i + a)
        else:
            if i + a >= 0:
                ans += abs(-1 - (i + a))
                a += -1 - (i + a)
        i += a
        positive = not positive

    return ans

if __name__ == "__main__":
    n = int(input())
    As = list(map(int, input().split(" ")))
    print(solve(n, As))

Submission status: Wrong Answer

Solution code:
def solve(N, As):
    i = As[0]
    positive = True if As[0] < 0 else False
    ans = 0
    if i == 0:
        ans = 1
        i = 1
    for a in As[1:]:
        if positive:
            if i + a <= 0:
                ans += abs(1 - (i + a))
                a += 1 - (i + a)
        else:
            if i + a >= 0:
                ans += abs(-1 - (i + a))
                a += -1 - (i + a)
        i += a
        positive = not positive

    return ans


if __name__ == "__main__":
    n = int(input())
    As = list(map(int, input().split(" ")))
    print(solve(n, As))

Submission status: Wrong Answer

Solution code:
n = int(input())
*a, = map(int, input().split())
ans = 0

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

e0 = 1 if a[0] > 0 else -1
S = a[0]

for i in range(1, n):
    e = e0 * int((i % 2-0.5)*2)
    n = max(e*(S+a[i])+1, 0)
    S += a[i]+(-e*n)
    ans += n
print(ans)

Submission status: Wrong Answer

Solution code:
def sign(x):
    if x < 0:
        return -1
    elif x > 0:
        return 1
    else:
        return 0


def solve():
    N = int(input())
    a = [int(x) for x in input().split()]

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

    op = 0  # operation
    ans = 0
    for i in range(1, N):
        c0 = cumsum[i-1]
        c1 = cumsum[i] = cumsum[i] + op

        s0 = sign(c0)
        s1 = sign(c1)

        if s0 * s1 < 0:
            continue

        if s1 == 0:
            ans += 1
            if s0 < 0:
                op += 1
                cumsum[i] += 1
            else:
                op -= 1
                cumsum[i] -= 1
        else:
            delta = -s1 * (abs(cumsum[i]) + 1)
            cumsum[i] += delta
            op += delta
            ans += abs(delta)

        # print(cumsum)
    return ans


def main():
    print(solve())


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [int(i) for i in input().split()]

def f(A, op, acc = 0, cnt = 0):
    for i in range(n):
        acc += A[i]
        
        if i % 2 == 0:
            if op * acc <= 0:
                cnt += - op * acc + 1
                acc = 1
                
        if i % 2 == 1:
            if op * acc >= 0:
                cnt += op * acc + 1
                acc = -1
                
    if acc == 0:
        cnt += 1
        
    return cnt

print(min(f(A, 1), f(A, -1)))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = [int(i) for i in input().split()]

def f(A, op, acc = 0, cnt = 0):
    for i in range(n):
        acc += A[i]
        
        if i % 2 == 0 and acc * op > 0:
            cnt += op * acc + 1
            acc = - op
            
        if i % 2 == 1 and acc * op < 0:
            cnt += - op * acc + 1
            acc = op
            
    if acc == 0:
        cnt += 1
        
    return cnt
        
print(min(f(A, -1), f(A, 1)))

Submission status: Wrong Answer

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

ans1 = 0
s = A[0]
for i in range(1, N):
    s += A[i]
    if i % 2 != 0:
        if s <= 0:
            ans1 += 1-s
            s = 1
    else:
        if s >= 0:
            ans1 -= -1-s
            s = -1

ans2 = 0
s = A[0]
for i in range(1, N):
    s += A[i]
    if i % 2 == 0:
        if s <= 0:
            ans2 += 1-s
            s = 1
    else:
        if s >= 0:
            ans2 -= -1-s
            s = -1

print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
a = [int(n) for n in input().split()]
b = [n for n in a]
sum = [0]*n
sum[0] = a[0]
ans = 0
for i in range(1,n):
    sum[i] = sum[i-1]
    if((sum[i]+a[i])*sum[i-1] >= 0):
        if(sum[i-1] > 0):
            ans+=sum[i-1] + a[i]+1
            a[i]-=sum[i-1] + a[i]+1
        else:
            ans+=1 - sum[i-1] - a[i]
            a[i]+=1 - sum[i-1] - a[i]
    sum[i] += a[i]

sum[0] = 1 if b[0] < 0 else -1
ansa = abs(b[0]) + 1
for i in range(1,n):
    sum[i] = sum[i-1]
    if((sum[i]+b[i])*sum[i-1] >= 0):
        if(sum[i-1] > 0):
            ansa+=sum[i-1] + b[i]+1
            b[i]-=sum[i-1] + b[i]+1
        else:
            ansa+=1 - sum[i-1] - b[i]
            b[i]+=1 - sum[i-1] - b[i]
    sum[i] += b[i]
print(min(ans,ansa))
# print(a)
# print(sum)

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
a = [int(n) for n in input().split()]
sum = [0]*n
sum[0] = a[0]
ans = 0
for i in range(1,n):
    sum[i] = sum[i-1]
    if((sum[i]+a[i])*sum[i-1] >= 0):
        if(sum[i-1] > 0):
            ans+=sum[i-1] + a[i]+1
            a[i]-=sum[i-1] + a[i]+1
        else:
            ans+=1 - sum[i-1] - a[i]
            a[i]+=1 - sum[i-1] - a[i]
    sum[i] += a[i]
print(ans)
# print(a)
# print(sum)

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
a = [int(n) for n in input().split()]
sum = [0]*n
sum[0] = a[0]
ans = 0
for i in range(1,n):
    sum[i] = sum[i-1]
    while((sum[i]+a[i])*sum[i-1] >= 0):
        if(sum[i-1] > 0):
            ans+=sum[i-1] + a[i]+1
            a[i]-=sum[i-1] + a[i]+1
        else:
            ans+=1 - sum[i-1] - a[i]
            a[i]+=1 - sum[i-1] - a[i]
        # print(a)
    sum[i] += a[i]
print(ans)
# print(a)
# print(sum)

Submission status: Wrong Answer

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

ans1,ans2 = 0,0
res = A[0]
for i in range(1,N):
    res += A[i]
    if i%2==1:
        if res >= 0:
            ans1 += res+1
            res = -1
    else:
        if res <= 0:
            ans1 -= res-1
            res = 1

res = A[0]
for i in range(1,N):
    res += A[i]
    if i%2==1:
        if res <= 0:
            ans2 -= res-1
            res = 1
    else:
        if res >= 0:
            ans2 += res+1
            res = -1

print(min(ans1,ans2))

Submission status: Wrong Answer

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

ans1,ans2 = 0,0
res = A[0]
for i in range(1,N):
    res += A[i]
    if i%2==1:
        if res >= 0:
            ans1 += res+1
            res = -1
    else:
        if res <= 0:
            ans1 -= res-1
            res = 1

for i in range(1,N):
    res += A[i]
    if i%2==1:
        if res <= 0:
            ans2 -= res-1
            res = 1
    else:
        if res >= 0:
            ans2 += res+1
            res = -1

print(min(ans1,ans2))

Submission status: Wrong Answer

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

S = a[0]
ans = 0
for _a in a[1:]:
    if (S < 0 and _a > -S) or (S > 0 and _a < -S):
            S += _a
            continue
    else:
        if S < 0:
            ans += -S + 1 - _a
            S = 1
        elif S > 0:
            ans += _a + S + 1
            S = -1
print(ans)

Submission status: Wrong Answer

Solution code:
def solve(asum, a, ans):
    for i in range(1, n):
        # print(asum)
        asumplus = asum + a[i]
        if asumplus == 0 and asum > 0:
            asumplus -= 1
            ans += 1
        elif asumplus == 0 and asum < 0:
            asumplus -= 1
            ans += 1
        elif asumplus > 0 and asum > 0:
            ans += asumplus + 1
            asumplus = -1
        elif asumplus < 0 and asum < 0:
            ans += -asumplus + 1
            asumplus = 1
        asum = asumplus
    return ans

n = int(input())
a = list(map(int,input().split()))
asum = a[0]
if asum != 0:
    ans = solve(asum,a,0)
else:
    ans = min(solve(1,a,1),solve(-1,a,1))
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(i) for i in input().split()]

if a[0]>=0:
    f=1
else:
    f=-1

s=0
ans=0
for i in range(n):
    s+=a[i]
    if s*f<0:
        ans+=abs(s)+1
        s=f
    elif s==0:
        ans+=1
        s=f
    f*=-1
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
S1,S2=[],[]
if A[0]>0:
  S1.append(A[0])
  S2.append(-1)
  a1,a2=0,A[0]+1
elif A[0]==0:
  S1.append(1)
  S2.append(-1)
  a1,a2=1,1
else:
  S1.append(1)
  S2.append(A[0])
  a1,a2=-A[0]+1,0
for i in range(1,N):
  if S1[i-1]>0:
    if S1[i-1]+A[i]<0:
      S1.append(S1[i-1]+A[i])
    elif S1[i-1]+A[i]>=0:
      S1.append(-1)
      a1+=S1[i-1]+A[i]+1
  elif S1[i-1]<0:
    if S1[i-1]+A[i]>0:
      S1.append(S1[i-1]+A[i])
    elif S1[i-1]+A[i]<=0:
      S1.append(-1)
      a1+=-S1[i-1]-A[i]+1
  if S2[i-1]>0:
    if S2[i-1]+A[i]<0:
      S2.append(S2[i-1]+A[i])
    elif S2[i-1]+A[i]>=0:
      S2.append(-1)
      a2+=S2[i-1]+A[i]+1
  elif S2[i-1]<0:
    if S2[i-1]+A[i]>0:
      S2.append(S2[i-1]+A[i])
    elif S2[i-1]+A[i]<=0:
      S2.append(-1)
      a2+=-S2[i-1]-A[i]+1
print(min(a1,a2))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[]
for i in range(n):
  b.append(a[i])

ct1=0
if a[0]<=0:
  ct1+=1-a[0]
  a[0]=1
x=a[0]
for i in range(1,n):
  y=x+a[i]
  if i%2==1:
    if y>=0:
      ct1+=y+1
      a[i]=-x-1
  else:
    if y<=0:
      ct1+=1-y
      a[i]=-x+1
  x+=a[i]

ct2=0
if b[0]>=0:
  ct2+=b[0]-1
  b[0]=-1
x=b[0]
for i in range(1,n):
  y=x+b[i]
  if i%2==0:
    if y>=0:
      ct2+=y+1
      b[i]=-x-1
  else:
    if y<=0:
      ct2+=1-y
      b[i]=-x+1
  x+=b[i]

print(min(ct1,ct2))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[]
for i in range(n):
  b.append(a[i])

ct1=0
if a[0]<=0:
  a[0]=1
  ct1+=1-a[0]
x=a[0]
for i in range(1,n):
  y=x+a[i]
  if i%2==1:
    if y>=0:
      ct1+=y+1
      a[i]=-x-1
  else:
    if y<=0:
      ct1+=1-y
      a[i]=-x+1
  x+=a[i]

ct2=0
if b[0]>=0:
  b[0]=-1
  ct2+=b[0]-1
z=b[0]
for i in range(1,n):
  w=z+b[i]
  if i%2==0:
    if w>=0:
      ct2+=w+1
      b[i]=-z-1
  else:
    if w<=0:
      ct2+=1-w
      b[i]=-z+1
  z+=b[i]

print(min(ct1,ct2))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[]
for i in range(n):
  b.append(a[i])

ct1=0
if a[0]<=0:
  a[0]=1
  ct1+=1-a[0]
x=a[0]
for i in range(1,n):
  y=x+a[i]
  if i%2==1:
    if y>=0:
      ct1+=y+1
      a[i]=-x-1
  else:
    if y<=0:
      ct1+=1-y
      a[i]=-x+1
  x+=a[i]

ct2=0
if b[0]>=0:
  b[0]=-1
  ct2+=b[0]-1
x=b[0]
for i in range(1,n):
  y=x+b[i]
  if i%2==0:
    if y>=0:
      ct2+=y+1
      b[i]=-x-1
  else:
    if y<=0:
      ct2+=1-y
      b[i]=-x+1
  x+=b[i]

print(min(ct1,ct2))

Submission status: Wrong Answer

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

if abs(A[1]-A[0])!=0:
  tmp=A[1]+A[0]
  if tmp>0 and A[0]>0:
    count+=abs(-1-(A[1]+A[0]))
    tmp=-1
  elif tmp<0 and A[0]<0:
    count+=abs(1-(A[1]+A[0]))
    tmp=1
else:
  count+=abs(-1-(A[1]+A[0]))
  tmp=-1


for i in range(2,n):
  if tmp<0:
    if tmp+A[i]<=0:
      count+= abs(1-(A[i]+tmp))
      tmp=1
    else:
      tmp+=A[i]
      continue
  else:
    if tmp+A[i]>=0:
      count+=abs(-1-(A[i]+tmp))
      tmp=-1
    else:
      tmp+=A[i]
      continue
print(count)

Submission status: Wrong Answer

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

if abs(A[1]-A[0])!=0:
  tmp=A[1]+A[0]
else:
  count+=abs(-1-(A[1]+A[0]))
  tmp=-1


for i in range(2,n):
  if tmp<0:
    if tmp+A[i]<=0:
      count+= abs(1-(A[i]+tmp))
      tmp=1
    else:
      tmp+=A[i]
      continue
  else:
    if tmp+A[i]>=0:
      count+=abs(-1-(A[i]+tmp))
      tmp=-1
    else:
      tmp+=A[i]
      continue
print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
count=0
tmp=A[1]-A[0]
if tmp==0:
  count+=abs(-1-(A[1]+A[0]))
  tmp=-1


for i in range(2,n):
  if tmp<0:
    if tmp+A[i]<=0:
      count+= abs(1-(A[i]+tmp))
      tmp=1
    else:
      tmp+=A[i]
      continue
  else:
    if tmp+A[i]>=0:
      count+=abs(-1-(A[i]+tmp))
      tmp=-1
    else:
      tmp+=A[i]
      continue
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int,input().split()))
def func(list1,op,acc = 0,count = 0):
    for i in range(n):
        acc += list1[i]
        if i % 2 == 1 and acc * op >= 0:
            count += op * acc + 1
            acc = -op
        elif i % 2 == 0 and acc * op <= 0:
            count += -op * acc + 1
            acc = op
    if acc == 0:
        count += 1
    return count
if A[0] > 0:
    ans = func(A,1)
elif A[0] < 0:
    ans = func(A,-1)
elif A[0] == 0:
    ans = min(func(A,1),func(A,-1))
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]

# a[0]の符号を正にするか負にするかの2択で
# 後は一意に決まる

_sum = 0
count = 0
out = 0
# a[0]の符号を正にする場合
if a[0] <= 0:
    count += a[0] - (-1)
    _sum = 1
else:
    _sum = a[0]
# print("count:{} sum:{}".format(count, _sum))
for i in range(1, len(a)):
    if i % 2 == 1:
        # a[i]までの和を負にする
        # a[i] >= 0なら「-1」にする
        if _sum + a[i] >= 0:
            count += _sum + a[i] - (-1)
            _sum = -1
        else:
            _sum += a[i]
    else:
        # a[i]までの和を正にする
        # a[i] <= 0なら「1」にする
        if _sum + a[i] <= 0:
            count += 1 - (_sum + a[i])
            _sum = 1
        else:
            _sum += a[i]
    # print("count:{} sum:{}".format(count, _sum))
# print()
out = count
_sum = 0
count = 0
# a[0]の符号を負にする場合
if a[0] >= 0:
    count += 1 - a[0]
    _sum = -1
else:
    _sum = a[0]
# print("count:{} sum:{}".format(count, _sum))
for i in range(1, len(a)):
    if i % 2 == 0:
        # a[i]までの和を負にする
        # a[i] >= 0なら「-1」にする
        if _sum + a[i] >= 0:
            count += _sum + a[i] - (-1)
            _sum = -1
        else:
            _sum += a[i]
    else:
        # a[i]までの和を正にする
        # a[i] <= 0なら「1」にする
        if _sum + a[i] <= 0:
            count += 1 - (_sum + a[i])
            _sum = 1
        else:
            _sum += a[i]
    # print("count:{} sum:{}".format(count, _sum))
print(min(out, count))

Submission status: Wrong Answer

Solution code:
# encoding: utf-8
n = int(input())
a = list(map(int, input().split()))

anss = [0] * 2
for init in range(2):
    ans = 0
    state = init
    sum_tmp = 0
    for i, ai in enumerate(a):
        ans_tmp = 0
        # 0
        if i == 0:
            if state == 1 and ai <= 0: ans_tmp += (-ai + 1)
            elif state == 0 and ai >= 0: ans_tmp += (ai + 1)
            sum_tmp = ai + ans_tmp if state == 1 else ai - ans_tmp
            continue
        # > 1
        state = 1 - state
        ans_tmp = 0
        if state == 1:
            sum_tmp += ai
            if sum_tmp <= 0: ans_tmp += (-sum_tmp + 1) # 1
            sum_tmp += ans_tmp
            ans += ans_tmp
        else:
            sum_tmp += ai
            if sum_tmp >= 0: ans_tmp += (sum_tmp + 1) # -1
            sum_tmp -= ans_tmp
            ans += ans_tmp
    anss[init] = ans

print(min(anss))

Submission status: Wrong Answer

Solution code:
def op(pos, n, a):
    if pos:
        S = 1 if a[0] == 0 else a[0]
    else:
        S = -1 if a[0] == 0 else a[0]
    count = 1 if a[0] == 0 else 0

    for i in a[1:]:
        if S * (S + i) > 0:
            count += abs(S + i) + 1
            S = -1 if S > 0 else 1
        elif S + i == 0:
            count += 1
            S = -1 if S > 0 else 1
        else:
            S += i

    return count


def main():
    n = int(input())
    a = list(map(int, input().split()))
    c1 = op(True, n, a)
    c2 = op(False, n, a)
    print(min(c1, c2))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def op(pos, n, a):
    if pos:
        S = 1 if a[0] == 0 else a[0]
    else:
        S = -1 if a[0] == 0 else a[0]
    count = 1 if S == 0 else 0

    for i in a[1:]:
        if S * (S + i) > 0:
            count += abs(S + i) + 1
            S = -1 if S > 0 else 1
        elif S + i == 0:
            count += 1
            S = -1 if S > 0 else 1
        else:
            S += i

    return count


def main():
    n = int(input())
    a = list(map(int, input().split()))
    c1 = op(True, n, a)
    c2 = op(False, n, a)
    print(min(c1, c2))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def op(pos, n, a):
    if pos:
        S = 1 if a[0] == 0 else a[0]
    else:
        S = -1 if a[0] == 0 else a[0]
    count = 1 if S == 0 else 0

    for i in a[1:]:
        if S * (S + i) > 0:
            count += abs(S + i) + 1
            S = -1 if S > 0 else 1
        elif S + i == 0:
            count += 1
            S = -1 if S > 0 else 1
        else:
            S += i

    return count


def main():
    n = int(input())
    a = list(map(int, input().split()))
    c1 = op(0, n, a)
    c2 = op(1, n, a)
    print(min(c1, c2))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def op(pos, n, a):
    if pos:
        S = 1 if a[0] == 0 else a[0]
    else:
        S = -1 if a[0] == 0 else a[0]
    count = 1 if S == 0 else 0

    for i in a[1:]:
        if S * (S + i) > 0:
            count += abs(S + i) + 1
            S = -1 if S > 0 else 1
        elif S + i == 0:
            count += 1
            S = -1 if S > 0 else 1
        else:
            S += i

    return count


def main():
    n = int(input())
    a = list(map(int, input().split()))
    c1 = op(1, n, a)
    c2 = op(2, n, a)
    print(min(c1, c2))


if __name__ == "__main__":
    main()