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 itertools import accumulate
import copy

def sol(acmA, sign=1):
  add = 0
  ans = 0
  sumA = acmA[0]
  for i in range(1,N):
    acmA[i] += add
    if sign == 1:
      if (i%2==0 and acmA[i-1]<0 and 0<acmA[i]) or (i%2==1 and 0<acmA[i-1] and acmA[i]<0) :continue
    else:
      if (i%2==1 and acmA[i-1]<0 and 0<acmA[i]) or (i%2==0 and 0<acmA[i-1] and acmA[i]<0) :continue
  
    tmp_add = -acmA[i]-1 if acmA[i] > 0 else -acmA[i]+1
    #print(i, tmp_add, acmA[i])
    acmA[i] += tmp_add
    sumA += acmA[i]
    add +=  tmp_add
    ans +=abs(tmp_add)
  if acmA == 0: return ans +1
  else: return ans

N = int(input())
A = list(map(int,input().split()))
acmA = list(accumulate(A))
acmA2 = copy.deepcopy(acmA)
print(min(sol(acmA, 1),sol(acmA2,-1)))
#print(acmA)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
import copy

def sol(acmA, sign=1):
  add = 0
  ans = 0
  for i in range(1,N):
    acmA[i] += add
    if sign == 1:
      if (i%2==0 and acmA[i-1]<0 and 0<acmA[i]) or (i%2==1 and 0<acmA[i-1] and acmA[i]<0) :continue
    else:
      if (i%2==1 and acmA[i-1]<0 and 0<acmA[i]) or (i%2==0 and 0<acmA[i-1] and acmA[i]<0) :continue
  
    tmp_add = -acmA[i]-1 if acmA[i] > 0 else -acmA[i]+1
    #print(i, tmp_add, acmA[i])
    acmA[i] += tmp_add
    add +=  tmp_add
    ans +=abs(tmp_add)
  return ans

N = int(input())
A = list(map(int,input().split()))
acmA = list(accumulate(A))
acmA2 = copy.deepcopy(acmA)
print(min(sol(acmA, 1),sol(acmA2,-1)))
#print(acmA)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

def sol(sign=1):
  add = 0
  ans = 0
  for i in range(1,N):
    acmA[i] += add
    if sign == 1:
      if (i%2==0 and acmA[i-1]*acmA[i]<0) or (i%2==1 and acmA[i-1]*acmA[i]>0) :continue
    else:
      if (i%2==1 and acmA[i-1]*acmA[i]<0) or (i%2==0 and acmA[i-1]*acmA[i]>0) :continue
  
    tmp_add = -acmA[i]-1 if acmA[i] > 0 else -acmA[i]+1
    #print(i, tmp_add, acmA[i])
    acmA[i] += tmp_add
    add +=  tmp_add
    ans +=abs(tmp_add)
  return ans

N = int(input())
A = list(map(int,input().split()))
acmA = list(accumulate(A))

print(min(sol(1),sol(-1)))
#print(acmA)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

N = int(input())
A = list(map(int,input().split()))
acmA = list(accumulate(A))

add = 0
ans = 0
for i in range(1,N):
  acmA[i] += add
  if acmA[i-1]*acmA[i]<0:continue
  
  tmp_add = -acmA[i]-1 if acmA[i] > 0 else -acmA[i]+1
  #print(i, tmp_add, acmA[i])
  acmA[i] += tmp_add
  add +=  tmp_add
  ans +=abs(tmp_add)
print(ans)
#print(acmA)

Submission status: Wrong Answer

Solution code:
def examC():
    N = I()
    a = LI()
    ansC = []
    """
        if a[0]<0:
        for i in range(N):
            a[i] = -a[i]
    """
    for l in range(2):
        ans = 0
        if l==0:
            if a[0]<0:
                sumA = 1
                ans += (-a[0]+1)
            else:
                sumA = a[0]
        elif l == 1:
            if a[0]>0:
                sumA = -1
                ans += (a[0]+1)
            else:
                sumA = a[0]
        for i in range(1,N):
            cur = sumA + a[i]
            if cur * sumA > 0:
                if sumA < 0:
                    ans += (-cur + 1)
                    sumA = 1
                else:
                    ans += cur + 1
                    sumA = -1
            elif cur == 0:
                if sumA < 0:
                    ans += 1
                    sumA = 1
                else:
                    ans += 1
                    sumA = -1
            else:
                sumA = cur
        ansC.append(ans)
    print(min(ansC))

import sys
import copy
import bisect
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC()

Submission status: Wrong Answer

Solution code:
def examC():
    N = I()
    a = LI()
    ans = 0
    if a[0]<0:
        for i in range(N):
            a[i] = -a[i]
    if a[0]==0:
        ans +=1
        a[0]=1
    sumA = a[0]
    for i in range(1,N):
        cur = sumA+a[i]
        if cur*sumA>0:
            if sumA<0:
                ans += (-cur+1)
                sumA = 1
            else:
                ans += cur+1
                sumA = -1
        elif cur*sumA==0:
            if sumA<0:
                ans += 1
                sumA = 1
            else:
                ans += 1
                sumA = -1
        else:
            sumA = cur
    print(ans)

import sys
import copy
import bisect
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC()

Submission status: Wrong Answer

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    a = list(map(int, readline().split()))
    t = 0
    cnt = 0
    lt = -1 if a[0] > 0 else 1
    for i in range(n):
        lt = lt if i == 0 else t
        t += a[i]
        if lt * t >= 0:
            if t > 0:
                cnt += abs(t + 1)
                t -= abs(t + 1)
            elif t < 0:
                cnt += abs(t - 1)
                t += abs(t - 1)
            else:
                if lt < 0:
                    cnt += abs(t - 1)
                    t += abs(t - 1)
                else:
                    cnt += abs(t + 1)
                    t += abs(t + 1)
    print(cnt)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys
from functools import reduce
import copy
import math
from pprint import pprint
import collections
import bisect


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def int_inputs(num_of_input):
    ins = [int(input()) for i in range(num_of_input)]
    return ins


def solve(inputs):
    A = string_to_int(inputs[0])

    def j(x):
        if x > 0:
            return 1
        elif x == 0:
            return 0
        else:
            return -1

    end_0 = None
    next_0 = None
    for i, a in enumerate(A):
        if a == 0:
            end_0 = i
        elif end_0 is not None:
            next_0 = j(a)
            break
        else:
            break

    sum_n = None
    count = 0
    is_prev = None
    for i, a in enumerate(A):
        if sum_n is None:
            if end_0:
                if not next_0:
                    sum_n = 1
                else:
                    if end_0 % 2 != 0:
                        if next_0 == 1:
                            sum_n = 1
                        else:
                            sum_n = -1
                    else:
                        if next_0 == 1:
                            sum_n = -1
                        else:
                            sum_n = 1
                count += 1
            else:
                sum_n = a
            is_prev = j(sum_n)

        else:
            tmp_sum_n = sum_n + a
            if is_prev == 1 and j(tmp_sum_n) >= 0:
                count += abs(tmp_sum_n) + 1
                tmp_sum_n -= (abs(tmp_sum_n) + 1)
            elif is_prev == -1 and j(tmp_sum_n) <= 0:
                count += abs(tmp_sum_n) + 1
                tmp_sum_n += abs(tmp_sum_n) + 1
            sum_n = tmp_sum_n
            is_prev = j(sum_n)
    return count


def string_to_int(string):
    return list(map(int, string.split()))


if __name__ == "__main__":
    input()
    ret = solve(inputs(1))
    print(ret)

Submission status: Wrong Answer

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

cum_n = A.pop(0)
count = 0

for i in range(len(A)):
  temp_n = A.pop(0)
  temp_cum_n  = cum_n + temp_n
  
  if i==0 and cum_n==0:
    count += 1
    if temp_n > 0:
      cum_n = -1
    else:
      cum_n = 1
  elif cum_n*temp_cum_n >= 0:
    count += abs(temp_cum_n)+1
    if temp_cum_n > 0:
      cum_n = -1
    else:
      cum_n = 1
  else:
    cum_n = temp_cum_n

print(count)

Submission status: Wrong Answer

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

even_is_negative = 0
odd_is_nagative = 0

for i in range(n):
    if i % 2 == 0 and a[i] < 0:
        even_is_negative += 1
    elif i % 2 == 1 and a[i] < 0:
        odd_is_nagative += 1

# print(even_is_negative)
# print(odd_is_nagative)

sum = a[0]
change = 0

if even_is_negative >= odd_is_nagative:
    for i in range(1, n):
        if i % 2 == 1 and sum + a[i] <= 0:
            change += abs(sum + a[i]) + 1
            sum = 1
            # print("a " + str(sum))
        elif i % 2 == 0 and sum + a[i] >= 0:
            change += sum + abs(a[i]) + 1
            sum = -1
            # print("b " + str(sum))
        else:
            sum += a[i]
            # print("c " + str(sum))

if even_is_negative < odd_is_nagative:
    for i in range(1, n):
        if i % 2 == 1 and sum + a[i] >= 0:
            change += sum + abs(a[i]) + 1
            sum = -1
            # print("a " + str(sum) + str(change))
        elif i % 2 == 0 and sum + a[i] <= 0:
            change += abs(sum + a[i]) + 1
            sum = 1
            # print("b " + str(sum)+ str(change))
        else:
            sum += a[i]
            # print("c " + str(sum)+ str(change))

if sum == 0:
    print(change + 1)
else:
    print(change)

Submission status: Wrong Answer

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

even_is_negative = 0
odd_is_nagative = 0

for i in range(n):
    if i % 2 == 0 and a[i] < 0:
        even_is_negative += 1
    elif i % 2 == 1 and a[i] < 0:
        odd_is_nagative += 1

# print(even_is_negative)
# print(odd_is_nagative)

sum = a[0]
change = 0

if even_is_negative >= odd_is_nagative:
    for i in range(1, n):
        if i % 2 == 1 and sum + a[i] <= 0:
            change += abs(sum + a[i]) + 1
            sum = 1
            # print("a " + str(sum))
        elif i % 2 == 0 and sum + a[i] >= 0:
            change += sum + a[i] + 1
            sum = -1
            # print("b " + str(sum))
        else:
            sum += a[i]
            # print("c " + str(sum))

if even_is_negative < odd_is_nagative:
    for i in range(1, n):
        if i % 2 == 1 and sum + a[i] >= 0:
            change += sum + a[i] + 1
            sum = -1
            # print("a " + str(sum) + str(change))
        elif i % 2 == 0 and sum + a[i] <= 0:
            change += abs(sum + a[i]) + 1
            sum = 1
            # print("b " + str(sum)+ str(change))
        else:
            sum += a[i]
            # print("c " + str(sum)+ str(change))

if sum == 0:
    print(change + 1)
else:
    print(change)

Submission status: Wrong Answer

Solution code:
import copy
n = int(input())
a = [int(i) for i in input().split()]
b=a.copy()
s0p = a[0]
s0n = b[0]
countp = 0
countn = 0

if s0p<=0:
  s0p+=(abs(s0p)+1)
  countp+=abs(s0p)

if s0n>=0:
  s0n-=(abs(s0n)+1)
  countn+=abs(s0n)

for i in range(1,n):
  s1 = s0p+a[i]
  if s0p*s1>=0:
    if s1>0:
      a[i]-=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1<0:
      a[i]+=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1==0:
      if s0p>0:
        a[i]-=1
        countp+=1
      elif s0p<0:
        a[i]+=1
        countp+=1
  s0p += a[i]

for i in range(1,n):
  s1 = s0n+b[i]
  if s0n*s1>=0:
    if s1>0:
      b[i]-=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1<0:
      b[i]+=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1==0:
      if s0n>0:
        b[i]-=1
        countn+=1
      elif s0n<0:
        b[i]+=1
        countn+=1
  s0n += b[i]

print(countp if countp<=countn else(countn))

Submission status: Wrong Answer

Solution code:
import copy
n = int(input())
a = [int(i) for i in input().split()]
b=a.copy()
s0p = a[0]
s0n = b[0]
countp = 0
countn = 0

if a.count(0)==n:
  print(2*n-1)
  exit()

if s0p<=0:
  s0p+=(abs(s0p)+1)
  countp+=abs(s0p)

if s0n>=0:
  s0n-=(abs(s0n)+1)
  countn+=abs(s0n)

for i in range(1,n):
  s1 = s0p+a[i]
  if s0p*s1>=0:
    if s1>0:
      a[i]-=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1<0:
      a[i]+=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1==0:
      if s0p>0:
        a[i]-=1
        countp+=1
      elif s0p<0:
        a[i]+=1
        countp+=1
  s0p += a[i]

for i in range(1,n):
  s1 = s0n+b[i]
  if s0n*s1>=0:
    if s1>0:
      b[i]-=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1<0:
      b[i]+=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1==0:
      if s0n>0:
        b[i]-=1
        countn+=1
      elif s0n<0:
        b[i]+=1
        countn+=1
  s0n += b[i]

print(countp if countp<=countn else(countn))

Submission status: Wrong Answer

Solution code:
import copy
n = int(input())
a = [int(i) for i in input().split()]
b=a.copy()
s0p = a[0]
s0n = b[0]
countp = 0
countn = 0

if a.count(0)==n:
  print(2*n-1)
  exit()

if s0p<=0:
  s0p+=(abs(s0p)+1)
  countp+=abs(s0p)
  print(countp,abs(s0p)+1,s0p,a[0])

if s0n>=0:
  s0n-=(abs(s0n)+1)
  countn+=abs(s0n)

for i in range(1,n):
  s1 = s0p+a[i]
  if s0p*s1>=0:
    if s1>0:
      a[i]-=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1<0:
      a[i]+=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1==0:
      if s0p>0:
        a[i]-=1
        countp+=1
      elif s0p<0:
        a[i]+=1
        countp+=1
  s0p += a[i]

for i in range(1,n):
  s1 = s0n+b[i]
  if s0n*s1>=0:
    if s1>0:
      b[i]-=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1<0:
      b[i]+=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1==0:
      if s0n>0:
        b[i]-=1
        countn+=1
      elif s0n<0:
        b[i]+=1
        countn+=1
  s0n += b[i]

print(countp if countp<=countn else(countn))

Submission status: Wrong Answer

Solution code:
import copy
n = int(input())
a = [int(i) for i in input().split()]
b=a.copy()
s0p = a[0]
s0n = b[0]
countp = 0
countn = 0

if a.count(0)==n:
  print(2*n+1)
  exit()

if s0p<=0:
  s0p+=(abs(s0p)+1)
  countp+=(abs(s0p)+1)

if s0n>=0:
  s0n-=(abs(s0n)+1)
  countn+=(abs(s0n)+1)

for i in range(1,n):
  s1 = s0p+a[i]
  if s0p*s1>=0:
    if s1>0:
      a[i]-=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1<0:
      a[i]+=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1==0:
      if s0p>0:
        a[i]-=1
        countp+=1
      elif s0p<0:
        a[i]+=1
        countp+=1
  s0p += a[i]

for i in range(1,n):
  s1 = s0n+b[i]
  if s0n*s1>=0:
    if s1>0:
      b[i]-=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1<0:
      b[i]+=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1==0:
      if s0n>0:
        b[i]-=1
        countn+=1
      elif s0n<0:
        b[i]+=1
        countn+=1
  s0n += b[i]

print(countp if countp<=countn else(countn))

Submission status: Wrong Answer

Solution code:
import copy
n = int(input())
a = [int(i) for i in input().split()]
b=a.copy()
s0p = a[0]
s0n = b[0]
countp = 0
countn = 0

if a.count(0)==n:
  print(2*n+1)
  exit()

if s0p<=0:
  s0p+=(abs(s0p)+1)
  countp+=1

if s0n>=0:
  s0n-=(abs(s0n)+1)
  countn+=1

for i in range(1,n):
  s1 = s0p+a[i]
  if s0p*s1>=0:
    if s1>0:
      a[i]-=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1<0:
      a[i]+=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1==0:
      if s0p>0:
        a[i]-=1
        countp+=1
      elif s0p<0:
        a[i]+=1
        countp+=1
  s0p += a[i]

for i in range(1,n):
  s1 = s0n+b[i]
  if s0n*s1>=0:
    if s1>0:
      b[i]-=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1<0:
      b[i]+=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1==0:
      if s0n>0:
        b[i]-=1
        countn+=1
      elif s0n<0:
        b[i]+=1
        countn+=1
  s0n += b[i]

print(countp if countp<=countn else(countn))

Submission status: Wrong Answer

Solution code:
import copy
n = int(input())
a = [int(i) for i in input().split()]
b=a.copy()
s0p = a[0]
s0n = b[0]
countp = 0
countn = 0

if a.count(0)==n:
  print(2*n+1)
  exit()
"""
if s0p<=0:
  while s0p<=0:
    s0p+=1
    countp+=1

if s0n>=0:
  while s0n>=0:
    s0n-=1
    countn+=1
"""
for i in range(1,n):
  s1 = s0p+a[i]
  if s0p*s1>=0:
    if s1>0:
      a[i]-=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1<0:
      a[i]+=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1==0:
      if s0p>0:
        a[i]-=1
        countp+=1
      elif s0p<0:
        a[i]+=1
        countp+=1
  s0p += a[i]

for i in range(1,n):
  s1 = s0n+b[i]
  if s0n*s1>=0:
    if s1>0:
      b[i]-=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1<0:
      b[i]+=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1==0:
      if s0n>0:
        b[i]-=1
        countn+=1
      elif s0n<0:
        b[i]+=1
        countn+=1
  s0n += b[i]

print(countp if countp<=countn else(countn))

Submission status: Wrong Answer

Solution code:
import copy
n = int(input())
a = [int(i) for i in input().split()]
b=a.copy()
s0p = a[0]
s0n = b[0]
countp = 0
countn = 0

if a.count(0)==n:
  print(2*n+1)
  exit()
"""
if s0p<=0:
  while s0p<=0:
    s0p+=1
    countp+=1

if s0n>=0:
  while s0n>=0:
    s0n-=1
    countn+=1
"""
for i in range(1,n):
  s1 = s0p+a[i]
  if s0p*s1>=0:
    if s1>0:
      a[i]-=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1<0:
      a[i]+=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1==0:
      if s0p>0:
        a[i]-=1
        countp+=1
      elif s0p<0:
        a[i]+=1
        countp+=1
  s0p += a[i]
"""
for i in range(1,n):
  s1 = s0n+b[i]
  if s0n*s1>=0:
    if s1>0:
      b[i]-=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1<0:
      b[i]+=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1==0:
      if s0n>0:
        b[i]-=1
        countn+=1
      elif s0n<0:
        b[i]+=1
        countn+=1
  s0n += b[i]
"""
print(countp if countp<=countn else(countn))

Submission status: Wrong Answer

Solution code:
import copy
n = int(input())
a = [int(i) for i in input().split()]
b=a.copy()
s0p = a[0]
s0n = b[0]
countp = 0
countn = 0

if a.count(0)==n:
  print(2*n+1)
  exit()

if s0p<=0:
  while s0p<=0:
    s0p+=1
    countp+=1

if s0n>=0:
  while s0n>=0:
    s0n-=1
    countn+=1
"""
for i in range(1,n):
  s1 = s0p+a[i]
  if s0p*s1>=0:
    if s1>0:
      a[i]-=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1<0:
      a[i]+=(abs(s1)+1)
      countp+=(abs(s1)+1)
    elif s1==0:
      if s0p>0:
        a[i]-=1
        countp+=1
      elif s0p<0:
        a[i]+=1
        countp+=1
  s0p += a[i]
"""
for i in range(1,n):
  s1 = s0n+b[i]
  if s0n*s1>=0:
    if s1>0:
      b[i]-=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1<0:
      b[i]+=(abs(s1)+1)
      countn+=(abs(s1)+1)
    elif s1==0:
      if s0n>0:
        b[i]-=1
        countn+=1
      elif s0n<0:
        b[i]+=1
        countn+=1
  s0n += b[i]

print(countp if countp<=countn else(countn))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A):
    S = [A[0]]
    for a in A[1:]:
        S.append(S[-1]+a)

    sig = -1 if S[0] > 0 else 1
    o = 0
    ans = 0
    for s in S:
        s += o
        if s == 0:
            ans += 1
            o -= sig
        elif sig * s > 0:
            ans += abs(s) + 1
            o -= sig*(abs(s) + 1)
        sig *= -1
        
    return ans


def main():
    N = read_int()
    A = read_int_n()
    print(slv(N, A))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
s0 = a[0]
count=0
flag = False
if a.count(0)==n:
  print(2*n+1)
  exit()
i=0
while a[i]==0 and i<n:
  i+=1
  flag=True
if flag:
  if a[i]>0:
    if i%2==0:s0+=1
    else:s0-=1
    count+=1
  else:
    if i%2==0:s0-=1
    else:s0+=1
    count+=1

for i in range(1,n):
  s1 = s0+a[i]
  if s0*s1>=0:
    if s1>0:
      a[i]-=(abs(s1)+1)
      count+=(abs(s1)+1)
    elif s1<0:
      a[i]+=(abs(s1)+1)
      count+=(abs(s1)+1)
    elif s1==0:
      if s0>0:
        a[i]-=1
        count+=1
      elif s0<0:
        a[i]+=1
        count+=1
  s0 += a[i]
print(count)

Submission status: Wrong Answer

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

def calc_tmp_ans(tmp_ans, cum_sum):
    for i in range(1, N):
        #print(i, tmp_ans)
        prev_cum_sum = cum_sum
        cum_sum += a[i]
        if cum_sum * prev_cum_sum >= 0:
            tmp_ans += abs(cum_sum) + 1
            if prev_cum_sum > 0:
                cum_sum = -1
            else:
                cum_sum = 1
    return tmp_ans

ans = 1000000000000000
if a[0] == 0:
    ans = min(ans, calc_tmp_ans(1, 1))
    ans = min(ans, calc_tmp_ans(-1, 1))
else:
    tmp_ans = 0
    cum_sum = a[0]
    ans = min(ans, calc_tmp_ans(tmp_ans, cum_sum))
    if a[0] > 0:
        ans = min(ans, calc_tmp_ans(a[0] + 1, -1))
    else:
        ans = min(ans, calc_tmp_ans(abs(a[0]) + 1, 1))
print(ans)

Submission status: Wrong Answer

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

ans = 1000000000000000
tmp_ans = 0
cum_sum = a[0]
for i in range(1, N):
    #print(i, tmp_ans)
    prev_cum_sum = cum_sum
    cum_sum += a[i]
    if cum_sum * prev_cum_sum >= 0:
        tmp_ans += abs(cum_sum) + 1
        if prev_cum_sum > 0:
            cum_sum = -1
        else:
            cum_sum = 1
print(tmp_ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
s0 = a[0]
count=0
if a[0]==0:
  if a[1]>0:s0-=1  
  else:s0+=1
  count+=1
for i in range(1,n):
  s1 = s0+a[i]
  if s0*s1>=0:
    if s1>0:
      a[i]-=(abs(s1)+1)
      count+=(abs(s1)+1)
    elif s1<0:
      a[i]+=(abs(s1)+1)
      count+=(abs(s1)+1)
    elif s1==0:
      if s0>0:
        a[i]-=1
        count+=1
      elif s0<0:
        a[i]+=1
        count+=1
  s0 += a[i]
print(count)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

def f(times, prefix_sum):
    for i in range(1, n):
        if prefix_sum < 0:
            if prefix_sum + a[i] > 0:
                prefix_sum += a[i]
            else:
                times += 1 - (prefix_sum + a[i])
                prefix_sum = 1      
        elif prefix_sum > 0:
            if prefix_sum + a[i] < 0:
                prefix_sum += a[i]
            else:
                times += abs(-1 - (prefix_sum + a[i]))
                prefix_sum = -1
    
    return times

t1 = 0
p1 = a[0]

t2 = 0
if a[0] > 0:
    t2 += abs(-1 - a[0])
    p2 = -1
elif a[0] < 0:
    t2 += 1 - a[0]
    p2 = 1
else:  # a[0] == 0
    t1 = 1
    p1 = -1
    t2 = 1
    p2 = 1

print(min(f(t1, p1), t2))

Submission status: Wrong Answer

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

def f(times, prefix_sum):
    for i in range(1, n):
        if prefix_sum < 0:
            if prefix_sum + a[i] > 0:
                prefix_sum += a[i]
            else:
                times += 1 - (prefix_sum + a[i])
                prefix_sum = 1      
        elif prefix_sum > 0:
            if prefix_sum + a[i] < 0:
                prefix_sum += a[i]
            else:
                times += abs(-1 - (prefix_sum + a[i]))
                prefix_sum = -1
    
    return times

t1 = 0
p1 = a[0]
t1 = f(t1, p1)

t2 = 0
if a[0] > 0:
    t2 += abs(-1 - a[0])
    p2 = -1
else:
    t2 += 1 - a[0]
    p2 = 1
t2 = f(t2, p2)

print(min(t1, t2))

Submission status: Wrong Answer

Solution code:
# c_WA2.py よりはWAが減った

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

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


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

print(min(times, times2))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
s0 = a[0]
count=0
if a[0]==0:
  s0+=1
  count+=1
for i in range(1,n):
  s1 = s0+a[i]
  if s0*s1>=0:
    if s1>0:
      a[i]-=(abs(s1)+1)
      count+=(abs(s1)+1)
    elif s1<0:
      a[i]+=(abs(s1)+1)
      count+=(abs(s1)+1)
    elif s1==0:
      if s0>0:
        a[i]-=1
        count+=1
      elif s0<0:
        a[i]+=1
        count+=1
  s0 += a[i]
print(count)

Submission status: Wrong Answer

Solution code:
input()
seq = list(map(int, input().rstrip("\n").split()))

iter_ = iter(seq)
ans1 = 0
for i, num in enumerate(iter_):
    if i == 0:
        sum_ = num
        if sum_ <= 0:
            ans1 += abs(sum_ - 1)
            sum_ = 1
    else:
        sum_ += num
        if i % 2 and sum_ >= 0:
                ans1 += abs(sum_ + 1)
                sum_ = -1
        elif sum_ <= 0:
                ans1 += abs(sum_ - 1)
                sum_ = 1

iter_ = iter(seq)
ans2 = 0
for i, num in enumerate(iter_):
    if i == 0:
        sum_ = num
        if sum_ >= 0:
            ans2 += abs(sum_ + 1)
            sum_ = -1
    else:
        sum_ += num
        if i % 2 and sum_ <= 0:
                ans2 += abs(sum_ - 1)
                sum_ = 1
        elif sum_ >= 0:
                ans2 += abs(sum_ + 1)
                sum_ = -1

print(min(ans1, ans2))

Submission status: Wrong Answer

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

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


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

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

print(min(times, times2, times3))

Submission status: Wrong Answer

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

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


times2 = 0
# if a[0] > 0:
#     times2 += abs(-1 - a[0])
#     previous = -1
# else:
#     times2 += 1 - a[0]
#     previous = 1
previous = -a[0]
for i in range(1, n):
    if previous < 0:
        if previous + a[i] > 0:
            previous = previous + a[i]
        else:
            times2 += 1 - (previous + a[i])
            previous = 1      
    elif previous > 0:
        if previous + a[i] < 0:
            previous = previous + a[i]
        else:
            times2 += abs(-1 - (previous + a[i]))
            previous = -1

print(min(times, times2))