Problem:


Score : 300 points

Problem Statement

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

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

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

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

Constraints

2 ≤ n ≤ 10^5

|a_i| ≤ 10^9

Each a_i is an integer.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print the minimum necessary count of operations.

Sample Input 1

4
1 -3 1 0

Sample Output 1

4

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

Sample Input 2

5
3 -6 4 -5 7

Sample Output 2

0

The given sequence already satisfies the conditions.

Sample Input 3

6
-1 4 3 2 -5 4

Sample Output 3

8



Sample input:
4
1 -3 1 0


Sample output:
4

Submission status: Wrong Answer

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N = I()
A = IL()

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
l = len(a)
b = []
for i in range(l):
  b.append(a[i])
  
ans = 0
Ans = 0
summary = a[0]

if(summary == 0):
  a[0] = 1
  ans+= 1
  b[0] = -1
  Ans+= 1
else:
  b[0] = int(-a[0]/ abs(a[0]))
  Ans+= abs(a[0]- b[0])

Summary = b[0]  
  
for i in range(1, l):
  if(summary* (summary+ a[i])>= 0):
    if(summary > 0):
      ans+= a[i]+ summary+ 1
      a[i] = -summary- 1 
      summary= -1
    else:
      ans+= -summary+ 1- a[i]
      a[i] = -summary+ 1
      summary= 1
  else:
    summary+= a[i]

for i in range(1, l):
  if(Summary* (Summary+ b[i])>= 0):
    if(Summary > 0):
      Ans+= b[i]+ Summary+ 1
      b[i] = -Summary- 1 
      Summary= -1
    else:
      Ans+= -Summary+ 1- b[i]
      b[i] = -Summary+ 1
      Summary= 1
  else:
    Summary+= b[i]
    
print(min(ans, Ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
l = len(a)
b = []
for i in range(l):
  b.append(a[i])
  
ans = 0
Ans = 0
summary = a[0]

if(summary == 0):
  a[0] = 1
  ans+= 1
  b[0] = -1
  Ans+= 1
else:
  b[0] = int(-a[0]/ abs(a[0]))
  Ans+= abs(a[0]- b[0])

Summary = b[0]  
  
for i in range(1, l):
  if(summary* (summary+ a[i])>= 0):
    if(summary > 0):
      ans+= a[i]+ summary+ 1
      a[i] = -summary- 1 
      summary= -1
    else:
      ans+= -summary+ 1- a[i]
      a[i] = -summary+ 1
      summary= 1
  else:
    summary+= a[i]

for i in range(1, l):
  if(Summary* (Summary+ b[i])>= 0):
    if(Summary > 0):
      Ans+= b[i]+ Summary+ 1
      b[i] = -Summary- 1 
      Summary= -1
    else:
      Ans+= -Summary+ 1- b[i]
      b[i] = -Summary+ 1
      Summary= 1
  else:
    Summary+= b[i]
    
print(min(ans, Ans))
print(a, b)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
l = len(a)
b = []
for i in range(l):
  b.append(a[i])
  
ans = 0
Ans = 0
summary = a[0]
Summary = b[0]

if(summary == 0):
  a[0] = 1
  ans+= 1
  b[0] = -1
else:
  b[0] = int(-a[0]/ abs(a[0]))
  Ans+= abs(a[0]- b[0])

for i in range(1, l):
  if(summary* (summary+ a[i])>= 0):
    if(summary > 0):
      ans+= a[i]+ summary+ 1
      a[i] = -summary- 1 
      summary= -1
    else:
      ans+= -summary+ 1- a[i]
      a[i] = -summary+ 1
      summary= 1
  else:
    summary+= a[i]

for i in range(1, l):
  if(Summary* (Summary+ b[i])>= 0):
    if(Summary > 0):
      Ans+= b[i]+ Summary+ 1
      b[i] = -Summary- 1 
      Summary= -1
    else:
      Ans+= -Summary+ 1- b[i]
      b[i] = -Summary+ 1
      Summary= 1
  else:
    Summary+= b[i]
    
print(min(ans, Ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
l = len(a)
ans = 0
summary = a[0]

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

for i in range(1, l):
  if(summary* (summary+ a[i])>= 0):
    if(summary > 0):
      ans+= a[i]+ summary+ 1
      a[i] = -summary- 1 
      summary= -1
    else:
      ans+= -summary+ 1- a[i]
      a[i] = -summary+ 1
      summary= 1
  else:
    summary+= a[i]
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
l = len(a)
ans = 0
summary = a[0]

for i in range(1, l):
  if(summary* (summary+ a[i])>= 0):
    if(summary > 0):
      ans+= a[i]+ summary+ 1
      a[i] = -summary- 1 
      summary= -1
    else:
      ans+= -summary+ 1- a[i]
      a[i] = -summary+ 1
      summary= 1
  else:
    summary+= a[i]
print(ans)

Submission status: Wrong Answer

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


# first : positive case
a_sum = seq[0]
op = 0

if a_sum > 0:
    pass
else:
    a_sum = 1
    op += abs(a_sum) + 1

for a in seq[1:]:
    tmp = a_sum + a
    if tmp * a_sum < 0:
        a_sum = tmp
    elif a_sum < 0:
        #diff = 1 - a_sum - a
        a_sum = 1
        op += abs(tmp) + 1
    elif a_sum > 0:
        #diff = -1 - a_sum - a
        a_sum = -1
        op += abs(tmp) + 1

op1 = op

# first : negative case

a_sum = seq[0]
op = 0

if a_sum < 0:
    pass
else:
    a_sum = -1
    op += abs(a_sum) + 1

for a in seq[1:]:
    tmp = a_sum + a
    if tmp * a_sum < 0:
        a_sum = tmp
    elif a_sum < 0:
        #diff = 1 - a_sum - a
        a_sum = 1
        op += abs(tmp) + 1
    elif a_sum > 0:
        #diff = -1 - a_sum - a
        a_sum = -1
        op += abs(tmp) + 1
op2 = op
print(min(op1, op2))

Submission status: Wrong Answer

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

a_sum = seq[0]
op = 0

for a in seq[1:]:
    tmp = a_sum + a
    if tmp * a_sum < 0:
        a_sum = tmp
    elif a_sum < 0:
        diff = 1 - a_sum - a
        a_sum = 1
        op += abs(diff)
    elif a_sum > 0:
        diff = -1 - a_sum - a
        a_sum = -1
        op += abs(diff)

print(op)

Submission status: Wrong Answer

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

a_sum = seq[0]
op = 0

for a in seq[1:]:
    tmp = a_sum + a
    if tmp * a_sum < 0:
        a_sum = tmp
    elif a_sum < 0:
        print(a_sum, a)
        diff = 1 - a_sum - a
        a_sum = 1
        op += abs(diff)

    elif a_sum > 0:

        diff = -1 - a_sum - a
        a_sum = -1
        op += abs(diff)

print(op)

Submission status: Wrong Answer

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

a_sum = seq[0]
op = 0

for a in seq[1:]:
    tmp = a_sum + a
    if tmp * a_sum < 0:
        a_sum = tmp
    elif a_sum < 0:
        while a_sum * (a_sum + a) >= 0:
            a += 1
            op += 1
        a_sum += a
    elif a_sum > 0:
        while a_sum * (a_sum + a) >= 0:
            a -= 1
            op += 1
        a_sum += a

print(op)

Submission status: Wrong Answer

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

ans = 0
temp = a[0]

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


print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
ans1 = 0
ans2 = 0
summ = 0
summ2 = 0
a2 = a[:]


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

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

print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from itertools import accumulate

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

    ans = 10**18
    diff = [0, 0]# a[0]<0, a[0]>0それぞれの初期コスト
    for i in range(2):
        if a[0] * [-1,1][i] >= 0:
            diff[i] = [-1,1][i] * (abs(a[0])+1)

    for j in range(2):
        ans2 = abs(diff[j])
        for i in range(1,n):
            p = a[i] + diff[j]
            q = a[i-1] + diff[j]
            if p * q >= 0:
                ans2 += abs(p)+1
                diff[j] -= p+1
        ans = min(ans, ans2)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from itertools import accumulate

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

    ans = 10**18
    diff = [None, None]# a[0]<0, a[0]>0それぞれの初期コスト
    for i in range(2):
        if a[0] * [-1,1][i] < 0:
            diff[i] = 0
        else:
            diff[i] = [-1,1][i] * (abs(a[0])+1)
    for j in range(2):
        ans2 = abs(diff[j])
        for i in range(1,n):
            p = a[i] + diff[j]
            q = a[i-1] + diff[j]
            if p * q >= 0:
                tmp = -q//abs(q) - p
                ans2 += abs(tmp)
                diff[j] += tmp
        ans = min(ans, ans2)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from itertools import accumulate

def main():
    n = int(input())
    a = list(map(int, input().split()))
    #a = list(accumulate(a))

    ans = 10**18
    diff = [None, None]# a[0]<0, a[0]>0それぞれの初期コスト
    for i in range(2):
        if a[0] * [-1,1][i] < 0:
            diff[i] = 0
        else:
            diff[i] = [-1,1][i] * (abs(a[0])+1)
    for j in range(2):
        ans2 = abs(diff[j])
        for i in range(1,n):
            p = a[i] + diff[j]
            q = a[i-1] + diff[j]
            if p * q >= 0:
                tmp = -q//abs(q) - p
                ans2 += abs(tmp)
                diff[j] += tmp
        ans = min(ans, ans2)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from itertools import accumulate

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

    ans = 10**18
    diff = [None, None]# a[0]<0, a[0]>0それぞれの初期コスト
    for i in range(2):
        if a[0] * [-1,1][i] < 0:
            diff[i] = 0
        else:
            diff[i] = [-1,1][i] * (abs(a[0])+1)
    for d in diff:
        ans2 = abs(d)
        for i in range(1,n):
            p = a[i] + d
            q = a[i-1] + d
            if p * q >= 0:
                tmp = -q//abs(q) - p
                ans2 += abs(tmp)
                d += tmp
        ans = min(ans, ans2)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from itertools import accumulate

def main():
    n = int(input())
    a = list(map(int, input().split()))
    a = list(accumulate(a))
    
    if a[0] != 0:
        ans = 0
        diff = 0
        for i in range(1,n):
            p = a[i] + diff
            q = a[i-1] + diff
            if p * q >= 0:
                tmp = -q//abs(q) - p
                ans += abs(tmp)
                diff += tmp
        print(ans)
    else:
        ans = 10**18
        for d in [-1,1]:
            ans2 = 1
            for i in range(1,n):
                p = a[i] + d
                q = a[i-1] + d
                if p * q >= 0:
                    tmp = -q//abs(q) - p
                    ans2 += abs(tmp)
                    d += tmp
            ans = min(ans, ans2)
        print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
b=0
count=0
x=0
if A[0]>0:
    x=-1
else:
    x=1
for a in A:
    b+=a
    if x==1:
        if b<0:
            x=-1
        else:
            count+=1+b
            b=-1
            x=-1
    else:
        if b>0:
            x=1
        else:
            count+=1-b
            b=1
            x=1
print(count if b!=0 else count+1)

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
b=0
count=0
x=0
if A[0]>0:
    x=-1
else:
    x=1
for a in A:
    b+=a
    if x==1:
        if b<0:
            x=-1
        else:
            count+=1+b
            b=-1
            x=-1
    else:
        if b>0:
            x=1
        else:
            count+=1-b
            b=1
            x=1
print(count)

Submission status: Wrong Answer

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

def solver(_sign):
    s = A[0]
    count = 0

    if _sign and s <= 0: # head is positive
        count += abs(s) + 1
        s = 1
    elif s >= 0: # head is negative
        count += abs(s) + 1
        s = -1

    for a in A[1:]:
        prev = s
        sign = prev > 0
        s += a
        if s == 0:
            count += 1
            if sign: # previous is positive
                s = -1
            else: # prev is negative
                s = 1
        elif sign == (s > 0): # previous and current have the same sign
            count += abs(s)+1
            if s > 0:
                s = -1
            else:
                s = 1
        else:
            pass

    return count

print(min(solver(True), solver(False)))

Submission status: Wrong Answer

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

def solver(_sign):
    s = A[0]
    count = 0

    if s == 0:
        count += 1
        if n == 1:
            return count
        else:
            if _sign: # 先頭が正
                s = 1
            else:
                s = -1

    for a in A[1:]:
        prev = s
        sign = prev > 0
        s += a
        if s == 0:
            count += 1
            if sign: # previous is positive
                s = -1
            else: # prev is negative
                s = 1
        elif sign == (s > 0): # previous and current have the same sign
            count += abs(s)+1
            if s > 0:
                s = -1
            else:
                s = 1
        else:
            pass

    return count

print(min(solver(True), solver(False)))

Submission status: Wrong Answer

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

s = A[0]
count = 0

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

for a in A[1:]:
    prev = s
    sign = prev > 0
    s += a
    if s == 0:
        count += 1
        if sign: # previous is positive
            s = -1
        else: # prev is negative
            s = 1
    elif sign == (s > 0): # previous and current have the same sign
        count += abs(s)+1
        if s > 0:
            s = -1
        else:
            s = 1
    else:
        pass

print(count)

Submission status: Wrong Answer

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

prev = A[0]
s = prev
sign = prev > 0
count = 0
for a in A[1:]:
    prev = s
    sign = prev > 0
    s += a
    if s == 0:
        count += 1
        if sign: # previous is positive
            s -= 1
        else: # prev is negative
            s += 1
    elif sign == (s > 0): # previous and current have the same sign
        count += abs(s)+1
        if s > 0:
            s = -1
        else:
            s = 1
    else:
        pass

print(count)

Submission status: Wrong Answer

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


ans = 0
s = 0

if a[0] == 0:
    ans += 1
    a[0] = 1

s = a[0]

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


print(ans)

Submission status: Wrong Answer

Solution code:
def resolve():
    # 整数 1 つ
    n = int(input())
    # 整数複数個
    # a, b = map(int, input().split())
    # 整数 N 個 (改行区切り)
    # N = [int(input()) for i in range(N)]
    # 整数 N 個 (スペース区切り)
    A = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    # A = [list(map(int, input().split())) for i in range(H)]

    sumi = 0
    cnt = 0
    for i in range(n-1):
        sumi += A[i]
        suminext = sumi + A[i+1]
        if sumi * suminext < 0:
            continue
        else:
            change = abs(suminext) +1
            cnt += change
            if sumi < 0:
                A[i+1] = A[i+1] + change
            else:
                A[i+1] = A[i+1] - change

    print(cnt)


resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    # 整数 1 つ
    n = int(input())
    # 整数複数個
    # a, b = map(int, input().split())
    # 整数 N 個 (改行区切り)
    # N = [int(input()) for i in range(N)]
    # 整数 N 個 (スペース区切り)
    A = list(map(int, input().split()))
    # 整数 (縦 H 横 W の行列)
    # A = [list(map(int, input().split())) for i in range(H)]

    sumi = A[0]
    cnt = 0
    for i in range(1, n-1):
        sumi += A[i]
        suminext = sumi + A[i+1]
        if sumi * suminext < 0:
            continue
        else:
            change = abs(suminext) +1
            cnt += change
            if sumi < 0:
                A[i+1] = A[i+1] + change
            else:
                A[i+1] = A[i+1] - change

    print(cnt)


resolve()

Submission status: Wrong Answer

Solution code:
n = int(input())
al = list(map(int, input().split()))
m = n//2
mm = n % 2

temp = al[0]
res = 0

def ddd(temp,al,m,mm,res):   
    if temp > 0 and mm ==1:
        for i in range(1,m+1):
            temp +=al[i*2-1]
            if temp <0:
                pass
            else:
                res += temp+1
                temp = -1
            
            temp +=al[i*2]
            if temp >0:
                pass
            else:
                res +=1-temp
                temp = 1
        return(res)
        
    if temp > 0 and mm ==0:
        for i in range(1,m):
            temp +=al[i*2-1]
            if temp <0:
                pass
            else:
                res += temp+1
                temp = -1
            
            temp +=al[i*2]
            if temp >0:
                pass
            else:
                res +=1-temp
                temp = 1
        temp += al[n-1]
        if temp <0:
            pass
        else:
             res += temp+1
             temp = -1
        return(res)
        
    if temp < 0 and mm ==1:
        for i in range(1,m+1):
            temp +=al[i*2-1]
            if temp >0:
                pass
            else:
                res += 1-temp
                temp = 1
            
            temp +=al[i*2]
            if temp <0:
                pass
            else:
                res +=temp+1
                temp = -1
        return(res)
        
    if temp < 0 and mm ==0:
        for i in range(1,m):
            temp +=al[i*2-1]
            if temp >0:
                pass
            else:
                res += 1-temp
                temp = 1
            
            temp +=al[i*2]
            if temp <0:
                pass
            else:
                res +=temp+1
                temp = -1
        temp += al[n-1]
        if temp >0:
            pass
        else:
             res += 1-temp
             temp = 1
        return(res)

if al[0]==0:
    temp = 1
    res = 1
    dpl =ddd(temp,al,m,mm,res)
    temp = -1
    res = 1
    dmi =ddd(temp,al,m,mm,res)
    print(min(dpl,dmi))
else:
    print(ddd(temp,al,m,mm,res))

Submission status: Wrong Answer

Solution code:
n = int(input())
al = list(map(int, input().split()))
m = n//2
mm = n % 2

temp = al[0]
res = 0

def ddd(temp,al,m,mm,res):   
    if temp > 0 and mm ==1:
        for i in range(1,m+1):
            temp +=al[i*2-1]
            if temp <0:
                pass
            else:
                res += temp+1
                temp = -1
            
            temp +=al[i*2]
            if temp >0:
                pass
            else:
                res +=1-temp
                temp = 1
        return(res)
        
    if temp > 0 and mm ==0:
        for i in range(1,m):
            temp +=al[i*2-1]
            if temp <0:
                pass
            else:
                res += temp+1
                temp = -1
            
            temp +=al[i*2]
            if temp >0:
                pass
            else:
                res +=1-temp
                temp = 1
        temp += al[n-1]
        if temp <0:
            pass
        else:
             res += temp+1
             temp = -1
        return(res)
        
    if temp < 0 and mm ==1:
        for i in range(1,m+1):
            temp +=al[i*2-1]
            if temp >0:
                pass
            else:
                res += 1-temp
                temp = 1
            
            temp +=al[i*2]
            if temp <0:
                pass
            else:
                res +=temp+1
                temp = -1
        return(res)
        
    if temp < 0 and mm ==0:
        for i in range(1,m):
            temp +=al[i*2-1]
            if temp >0:
                pass
            else:
                res += 1-temp
                temp = 1
            
            temp +=al[i*2]
            if temp <0:
                pass
            else:
                res +=temp+1
                temp = -1
        temp += al[n-1]
        if temp >0:
            pass
        else:
             res += 1-temp
             temp = 1
        return(res)

print(ddd(temp,al,m,mm,res))

Submission status: Wrong Answer

Solution code:
n = int(input())
al = list(map(int, input().split()))
m = n//2
mm = n % 2

temp = al[0]
res = 0

def ddd(temp,al,m,mm,res):   
    if temp > 0 and mm ==1:
        for i in range(1,m+1):
            temp +=al[i*2-1]
            if temp <0:
                pass
            else:
                res += temp+1
                temp = -1
            
            temp +=al[i*2]
            if temp >0:
                pass
            else:
                res +=1-temp
                temp = 1
        print(res)
        exit()
        
    if temp > 0 and mm ==0:
        for i in range(1,m):
            temp +=al[i*2-1]
            if temp <0:
                pass
            else:
                res += temp+1
                temp = -1
            
            temp +=al[i*2]
            if temp >0:
                pass
            else:
                res +=1-temp
                temp = 1
        temp += al[n-1]
        if temp <0:
            pass
        else:
             res += temp+1
             temp = -1
        print(res)
        exit()
        
    if temp < 0 and mm ==1:
        for i in range(1,m+1):
            temp +=al[i*2-1]
            if temp >0:
                pass
            else:
                res += 1-temp
                temp = 1
            
            temp +=al[i*2]
            if temp <0:
                pass
            else:
                res +=temp+1
                temp = -1
        print(res)
        exit()
        
    if temp < 0 and mm ==0:
        for i in range(1,m):
            temp +=al[i*2-1]
            if temp >0:
                pass
            else:
                res += 1-temp
                temp = 1
            
            temp +=al[i*2]
            if temp <0:
                pass
            else:
                res +=temp+1
                temp = -1
        temp += al[n-1]
        if temp >0:
            pass
        else:
             res += 1-temp
             temp = 1
        print(res)
        exit()
ddd(temp,al,m,mm,res)

Submission status: Wrong Answer

Solution code:
n = int(input())
al = list(map(int, input().split()))
m = n//2
mm = n % 2

temp = al[0]
res = 0

if temp > 0 and mm ==1:
    for i in range(1,m+1):
        temp +=al[i*2-1]
        if temp <0:
            pass
        else:
            res += temp+1
            temp = -1
        
        temp +=al[i*2]
        if temp >0:
            pass
        else:
            res +=1-temp
            temp = 1
    print(res)
    exit()
    
if temp > 0 and mm ==0:
    for i in range(1,m):
        temp +=al[i*2-1]
        if temp <0:
            pass
        else:
            res += temp+1
            temp = -1
        
        temp +=al[i*2]
        if temp >0:
            pass
        else:
            res +=1-temp
            temp = 1
    temp += al[n-1]
    if temp <0:
        pass
    else:
         res += temp+1
         temp = -1
    print(res)
    exit()
    
if temp < 0 and mm ==1:
    for i in range(1,m+1):
        temp +=al[i*2-1]
        if temp >0:
            pass
        else:
            res += 1-temp
            temp = 1
        
        temp +=al[i*2]
        if temp <0:
            pass
        else:
            res +=temp+1
            temp = -1
    print(res)
    exit()
    
if temp < 0 and mm ==0:
    for i in range(1,m):
        temp +=al[i*2-1]
        if temp >0:
            pass
        else:
            res += 1-temp
            temp = 1
        
        temp +=al[i*2]
        if temp <0:
            pass
        else:
            res +=temp+1
            temp = -1
    temp += al[n-1]
    if temp >0:
        pass
    else:
         res += 1-temp
         temp = 1
    print(res)
    exit()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
#n = 6
#a = [-1,4,3,2,-5,4]

total = a[0]
ans = 0
for i in range(1,n):
  total2 = total + a[i]

  if not total2 * total < 0:

    if total > 0 :
      if total2 == 0 :
        x = 1
      elif total2 > 0 :
        x = total2 + 1
      else :
        x = abs(total2) - 1
      total2 = -1
    
    else:
      if total2 == 0 :
        x = 1
      elif total2 > 1:
        x = total2 - 1
      else:
        x = abs(total2) + 1
      total2 = 1

    ans += x
  total = total2

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())
a = list(map(int, input().split()))
c1 = 0
a0 = a[0]
if a0 == 0:
    a[0] = 1
    c1 += 1
sum = a[0]
for i in range(1, n):
    if np.sign(sum) == -np.sign(sum + a[i]):
        sum += a[i]
    else:
        if sum > 0:
            c1 += sum + a[i] + 1
            sum = -1
        else:
            c1 += -sum - a[i] + 1
            sum = 1

c2 = 0
if a0 == 0:
    a[0] = -1
    c2 += 1
else:
    c2 += a[0] + 1
    if a[0] > 0:
        a[0] = -1
    else:
        a[0] = 1
sum = a[0]
for i in range(1, n):
    if np.sign(sum) == -np.sign(sum + a[i]):
        sum += a[i]
    else:
        if sum > 0:
            c2 += sum + a[i] + 1
            sum = -1
        else:
            c2 += -sum - a[i] + 1
            sum = 1

print(min(c1, c2))