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:
def count_loop(n, old_state, count, a_list, flag):
    for i in range(1, int(n)):
        num_state = old_state + int(a_list[i])
        if flag == 1:
            if num_state >= 0:
                count += num_state + 1
                old_state = -1
            elif num_state < 0:
                old_state = num_state
            flag = -1
        elif flag == -1:
            if num_state <= 0:
                count += abs(num_state) + 1
                old_state = 1
            elif num_state > 0:
                old_state = num_state
            flag = 1
    return count

if __name__ == "__main__":
    n = input()
    a = input()
    a_list = a.split(" ")

    old_state = int(a_list[0])
    count = 0
    if old_state == 0:
        c1 = count_loop(n,1,1,a_list,1)
        c2 = count_loop(n,-1,1,a_list,-1)
    else:
        c1 = count_loop(n,old_state,count,a_list,1)
        c2 = count_loop(n,old_state,count,a_list,-1)
    print(min(c1,c2))

Submission status: Wrong Answer

Solution code:
def count_loop(n, old_state, count, a_list, flag):
    print(count)
    for i in range(1, int(n)):
        num_state = old_state + int(a_list[i])
        if flag == 1:
            if num_state >= 0:
                count += num_state + 1
                old_state = -1
            elif num_state < 0:
                old_state = num_state
            flag = -1
        elif flag == -1:
            if num_state <= 0:
                count += abs(num_state) + 1
                old_state = 1
            elif num_state > 0:
                old_state = num_state
            flag = 1
        print(count)
    return count

if __name__ == "__main__":
    n = input()
    a = input()
    a_list = a.split(" ")

    old_state = int(a_list[0])
    count = 0
    if old_state == 0:
        c1 = count_loop(n,1,1,a_list,1)
        c2 = count_loop(n,-1,1,a_list,-1)
    else:
        c1 = count_loop(n,old_state,count,a_list,1)
        print()
        c2 = count_loop(n,old_state,count,a_list,-1)
    print(min(c1,c2))

Submission status: Wrong Answer

Solution code:
def count_loop(n, old_state, a_list, flag):
    count = 0
    for i in range(1, int(n)):
        num_state = old_state + int(a_list[i])
        if flag == 1:
            if num_state == 0:
                count += 1
                old_state = -1
                flag = -1
            elif num_state > 0:
                count += num_state + 1
                old_state = -1
                flag = -1
            else:
                old_state = num_state
                flag = -1
        elif flag == -1:
            if num_state == 0:
                count += 1
                old_state = 1
                flag = 1
            elif num_state < 0:
                count += abs(num_state) + 1
                old_state = 1
                flag = 1
            else:
                old_state = num_state
                flag = 1
        else:
            if num_state == 0:
                count += 1
                flag = 0
            elif num_state > 0:
                old_state = num_state
                flag = 1
            else:
                old_state = num_state
                flag = -1
    return count

if __name__ == "__main__":
    n = input()
    a = input()
    a_list = a.split(" ")

    old_state = int(a_list[0])
    count = 0
    if old_state == 0:
        count += 1
        c1 = count_loop(n,1,a_list,0)
        c2 = count_loop(n,-1,a_list,0)
    else:
        c1 = count_loop(n,old_state,a_list,1)
        c2 = count_loop(n,old_state,a_list,-1)

    if c1 <= c2:
        print(c1)
    else:
        print(c2)

Submission status: Wrong Answer

Solution code:
def count_loop(n, old_state, count, a_list, flag):
    for i in range(1, int(n)):
        num_state = old_state + int(a_list[i])
        if flag == 1:
            if num_state == 0:
                count += 1
                old_state = -1
                flag = -1
            elif num_state > 0:
                count += num_state + 1
                old_state = -1
                flag = -1
            else:
                old_state = num_state
                flag = -1
        elif flag == -1:
            if num_state == 0:
                count += 1
                old_state = 1
                flag = 1
            elif num_state < 0:
                count += abs(num_state) + 1
                old_state = 1
                flag = 1
            else:
                old_state = num_state
                flag = 1
    return count

if __name__ == "__main__":
    n = input()
    a = input()
    a_list = a.split(" ")

    old_state = int(a_list[0])
    count = 0
    if old_state == 0:
        count += 1

    c1 = count_loop(n,old_state,count,a_list,1)
    c2 = count_loop(n,old_state,count,a_list,-1)

    if c1 <= c2:
        print(c1)
    else:
        print(c2)

Submission status: Wrong Answer

Solution code:
def count_loop(n, old_state, a_list, flag):
    count = 0
    for i in range(1, int(n)):
        num_state = old_state + int(a_list[i])
        if flag == 1:
            if num_state == 0:
                count += 1
                old_state = -1
                flag = -1
            elif num_state > 0:
                count += num_state + 1
                old_state = -1
                flag = -1
            else:
                old_state = num_state
                flag = -1
        elif flag == -1:
            if num_state == 0:
                count += 1
                old_state = 1
                flag = 1
            elif num_state < 0:
                count += abs(num_state) + 1
                old_state = 1
                flag = 1
            else:
                old_state = num_state
                flag = 1
        else:
            if num_state == 0:
                count += 1
                flag = 0
            elif num_state > 0:
                old_state = num_state
                flag = 1
            else:
                old_state = num_state
                flag = -1
    return count

if __name__ == "__main__":
    n = input()
    a = input()
    a_list = a.split(" ")

    old_state = int(a_list[0])
    count = 0

    c1 = count_loop(n,old_state,a_list,1)
    c2 = count_loop(n,old_state,a_list,-1)
    c3 = count_loop(n,old_state,a_list,0)+1

    if c1 <= c2 and c1 <= c3:
        print(c1)
    elif c2 <= c1 and c2 <= c3:
        print(c2)
    else:
        print(c3)

Submission status: Wrong Answer

Solution code:
def count_loop(n, old_state, a_list, flag):
    count = 0
    for i in range(1, int(n)):
        num_state = old_state + int(a_list[i])
        if flag == 1:
            if num_state == 0:
                count += 1
                old_state = -1
                flag = -1
            elif num_state > 0:
                count += num_state + 1
                old_state = -1
                flag = -1
            else:
                old_state = num_state
                flag = -1
        elif flag == -1:
            if num_state == 0:
                count += 1
                old_state = 1
                flag = 1
            elif num_state < 0:
                count += abs(num_state) + 1
                old_state = 1
                flag = 1
            else:
                old_state = num_state
                flag = 1
        else:
            if num_state == 0:
                count += 1
                flag = 0
            elif num_state > 0:
                old_state = num_state
                flag = 1
            else:
                old_state = num_state
                flag = -1
    return count

if __name__ == "__main__":
    n = input()
    a = input()
    a_list = a.split(" ")

    old_state = int(a_list[0])
    count = 0

    c1 = count_loop(n,old_state,a_list,1)
    c2 = count_loop(n,old_state,a_list,-1)
    c3 = count_loop(n,old_state,a_list,0)+1

    if c1 <= c2 and c1 <= c3:
        print(c1)
    elif c2 < c1 and c2 < c3:
        print(c2)
    else:
        print(c3)

Submission status: Wrong Answer

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

while a[0] == 0:
    if a[1] > 0:
        a[0] = -1
    elif a[1] < 0:
        a[0] = 1
    a.pop(0)
    ans += 1

_a = list(map(lambda x: x * (-1), a))


def func(l: list):
    _ans = 0
    product = 0
    for i in range(len(l)):
        product += l[i]

        if i % 2 == 0:
            if product <= 0:
                _ans += abs(product) + 1
                product = 1

        elif i % 2 == 1:
            if product >= 0:
                _ans += abs(product) + 1
                product = -1
    return _ans

ans += min(func(a), func(_a))
print(ans)

Submission status: Wrong Answer

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

while a[0] == 0:
    if a[1] > 0:
        a[0] = -1
    elif a[1] < 0:
        a[0] = 1
    a.pop(0)
    ans += 1

if a[0] < 0:
    a = list(map(lambda x: x * (-1), a))

product = 0

for i in range(len(a)):
    product += a[i]

    if i % 2 == 0:
        if product <= 0:
            ans += abs(product) + 1
            product = 1

    elif i % 2 == 1:
        if product >= 0:
            ans += abs(product) + 1
            product = -1

print(ans)

Submission status: Wrong Answer

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

while a[0] == 0:
    if a[1] > 0:
        a[0] = -1
    elif a[1] < 0:
        a[0] = 1
    else:
        a.pop(0)
    ans += 1

if a[0] < 0:
    a = list(map(lambda x: x * (-1), a))

product = 0

for i in range(len(a)):
    product += a[i]

    if i % 2 == 0:
        if product <= 0:
            ans += abs(product) + 1
            product = 1

    elif i % 2 == 1:
        if product >= 0:
            ans += abs(product) + 1
            product = -1

print(ans)

Submission status: Wrong Answer

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

if a[0] == 0:
    a[0] += 1
    ans += 1
elif a[0] < 0:
    a = list(map(lambda x: x * (-1), a))

product = a[0]

for i in range(1, n):
    product += a[i]

    if i % 2 == 0:
        if product <= 0:
            ans += abs(product) + 1
            product = 1

    elif i % 2 == 1:
        if product >= 0:
            ans += abs(product) + 1
            product = -1

print(ans)

Submission status: Wrong Answer

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

sum1 = sum2 = a[0]
cnt1 = cnt2 = 0

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

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



print(min(cnt1,cnt2))

Submission status: Wrong Answer

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

sum = a[0]
cnt = 0

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

print(cnt)

Submission status: Wrong Answer

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

cnt =0
sum_tmp=a[0]

def check(a,i,cnt,sum_tmp):
    sum1=sum_tmp   
    sum2 = sum_tmp+a[i]
    if sum1<0 and sum2 <=0:
         cnt  += abs(sum2)+1
         sum_tmp =1
    elif sum1>0 and sum2 >=0:
         cnt  += abs(sum2)+1
         sum_tmp=-1
    else:
         sum_tmp=sum2
    return cnt,sum_tmp
    
for i in range(1,n):
    cnt,sum_tmp=check(a,i,cnt,sum_tmp)

print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
# -*- coding: utf-8 -*-
# 整数の入力
n=int(input())
a=list(map(int, input().split()))
b=a[:]
c=a[:]
# 無変更チェック
if a[0]!=0:
  S=int(a[0])
  for i in range(1,n):
    if S<0 and S+a[i]<=0:
      break
    elif S>0 and S+a[i]>=0:
      break
    S+=a[i]
    if i==n-1:
      print(0)
      sys.exit()
# a[0]を1に変えた場合の計算
counter_2=abs(b[0]-1)
b[0]=1
S=b[0]
for i in range(1,n):
  if S<0 and S+b[i]<=0:
    counter_2+=-S-b[i]+1
    b[i]=-S+1
  elif S>0 and S+b[i]>=0:
    counter_2+=S+b[i]+1
    b[i]=-S-1
  S+=b[i]
# a[0]を-1に変えた場合の計算
counter_3=abs(c[0]+1)
c[0]=-1
S=c[0]
for i in range(1,n):
  if S<0 and S+c[i]<=0:
    counter_3+=-S-c[i]+1
    c[i]=-S+1
  elif S>0 and S+c[i]>=0:
    counter_3+=S+c[i]+1
    c[i]=-S-1
  S+=c[i]
print(min(counter_2,counter_3))

Submission status: Wrong Answer

Solution code:
import sys
# -*- coding: utf-8 -*-
# 整数の入力
n = int(input())
a = list(map(int, input().split()))
b = a[:]
# 無変更チェック
counter_1=0
S=int(a[0])
if a[0]!=0:
  for i in range(1,n):
    if S<0 and S+int(a[i])<=0:
      break
    elif S>0 and S+int(a[i])>=0:
      break
    S+=int(a[i])
    if i==n-1:
      print(counter_1)
      sys.exit()
# a[0]を1に変えた場合の計算
counter_1=abs(int(b[0])-1)
b[0]=1
S=b[0]
for i in range(1,n):
  if S<0 and S+int(b[i])<=0:
    counter_1+=-S-int(b[i])+1
    b[i]=-S+1
  elif S>0 and S+int(b[i])>=0:
    counter_1+=S+int(b[i])+1
    b[i]=-S-1
  S+=int(b[i])
# a[0]を-1に変えた場合の計算
counter_2=abs(int(a[0])+1)
a[0]=-1
S=a[0]
for i in range(1,n):
  if S<0 and S+int(a[i])<=0:
    counter_2+=-S-int(a[i])+1
    a[i]=-S+1
  elif S>0 and S+int(a[i])>=0:
    counter_2+=S+int(a[i])+1
    a[i]=-S-1
  S+=int(a[i])
print(min(counter_1,counter_2))

Submission status: Wrong Answer

Solution code:
import sys
# -*- coding: utf-8 -*-
# 整数の入力
n = int(input())
a = list(map(int, input().split()))
b = a[:]
# 無変更チェック
counter_1=0
S=int(a[0])
for i in range(1,n):
  if S<0 and S+int(a[i])<=0:
    break
  elif S>0 and S+int(a[i])>=0:
    break
  S+=int(a[i])
  if i==n-1:
    print(counter_1)
    sys.exit()
# a[0]を1に変えた場合の計算
counter_1=abs(int(b[0])-1)
b[0]=1
S=b[0]
for i in range(1,n):
  if S<0 and S+int(b[i])<=0:
    counter_1+=-S-int(b[i])+1
    b[i]=-S+1
  elif S>0 and S+int(b[i])>=0:
    counter_1+=S+int(b[i])+1
    b[i]=-S-1
  S+=int(b[i])
# a[0]を-1に変えた場合の計算
counter_2=abs(int(a[0])+1)
a[0]=-1
S=a[0]
for i in range(1,n):
  if S<0 and S+int(a[i])<=0:
    counter_2+=-S-int(a[i])+1
    a[i]=-S+1
  elif S>0 and S+int(a[i])>=0:
    counter_2+=S+int(a[i])+1
    a[i]=-S-1
  S+=int(a[i])
print(min(counter_1,counter_2))

Submission status: Wrong Answer

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

n = int(raw_input())
numlist = (raw_input()).split(' ')

count = 0

i = 0
while (int(numlist[i]) == 0):
 i += 1

if (int(numlist[0]) == 0):
    if (int(numlist[i]) > 0):
        numlist[0] = (-1) ** i
    else:
        numlist[0] = (-1) ** (i+1)
    count += 1

sumlist = [int(numlist[0])]

for i in range(1, n):
  sumlist.append(sumlist[i-1] + int(numlist[i]))
  while (True):
    if (sumlist[i-1] > 0 and sumlist[i] > 0):  #i-1,i番目までのsumがともに正
      numlist[i] = int(numlist[i]) - (sumlist[i] + 1)
      count += sumlist[i] + 1
      sumlist[i] = -1
    elif (sumlist[i-1] < 0 and sumlist[i] < 0):  #i-1,i番目までのsumがともに負
      numlist[i] = int(numlist[i]) + ((-1)*sumlist[i] + 1)
      count += (-1)*sumlist[i] + 1
      sumlist[i] = 1
    elif (sumlist[i] == 0):  #i番目までのsum=0
      if (sumlist[i-1] > 0):
        numlist[i] = int(numlist[i]) - 1
        sumlist[i] -= 1
      if (sumlist[i-1] < 0):
        numlist[i] = int(numlist[i]) + 1
        sumlist[i] += 1
      count += 1
    else:
      break
    #print numlist
    #print sumlist

print count

Submission status: Wrong Answer

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

n = int(raw_input())
numlist = (raw_input()).split(' ')

count = 0

if (int(numlist[0]) == 0):
    if (int(numlist[1]) < 0):
        numlist[0] = -1
    else:
        numlist[0] = 1

sumlist = [int(numlist[0])]

for i in range(1, n):
  sumlist.append(sumlist[i-1] + int(numlist[i]))
  while (True):
    if (sumlist[i-1] > 0 and sumlist[i] > 0):  #i-1,i番目までのsumがともに正
      numlist[i] = int(numlist[i]) - (sumlist[i] + 1)
      count += sumlist[i] + 1
      sumlist[i] = -1
    elif (sumlist[i-1] < 0 and sumlist[i] < 0):  #i-1,i番目までのsumがともに負
      numlist[i] = int(numlist[i]) + ((-1)*sumlist[i] + 1)
      count += (-1)*sumlist[i] + 1
      sumlist[i] = 1
    elif (sumlist[i] == 0):  #i番目までのsum=0
      if (sumlist[i-1] > 0):
        numlist[i] = int(numlist[i]) - 1
        sumlist[i] -= 1
      if (sumlist[i-1] < 0):
        numlist[i] = int(numlist[i]) + 1
        sumlist[i] += 1
      count += 1
    else:
      break
    #print numlist
    #print sumlist

print count

Submission status: Wrong Answer

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

n = int(raw_input())
numlist = (raw_input()).split(' ')

count = 0

if (int(numlist[0]) == 0):
    if (int(numlist[1]) > 0):
        numlist[0] = -1
    else:
        numlist[0] = 1

sumlist = [int(numlist[0])]

for i in range(1, n):
  sumlist.append(sumlist[i-1] + int(numlist[i]))
  while (True):
    if (sumlist[i-1] > 0 and sumlist[i] > 0):  #i-1,i番目までのsumがともに正
      numlist[i] = int(numlist[i]) - (sumlist[i] + 1)
      count += sumlist[i] + 1
      sumlist[i] = -1
    elif (sumlist[i-1] < 0 and sumlist[i] < 0):  #i-1,i番目までのsumがともに負
      numlist[i] = int(numlist[i]) + ((-1)*sumlist[i] + 1)
      count += (-1)*sumlist[i] + 1
      sumlist[i] = 1
    elif (sumlist[i] == 0):  #i番目までのsum=0
      if (sumlist[i-1] > 0):
        numlist[i] = int(numlist[i]) - 1
        sumlist[i] -= 1
      if (sumlist[i-1] < 0):
        numlist[i] = int(numlist[i]) + 1
        sumlist[i] += 1
      count += 1
    else:
      break
    #print numlist
    #print sumlist

print count

Submission status: Wrong Answer

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

n = int(raw_input())
numlist = (raw_input()).split(' ')

count = 0

if (int(numlist[0]) == 0):
    if (int(numlist[1]) > 0):
        numlist[0] = -1
    else:
        numlist[0] = 1

sumlist = [int(numlist[0])]

for i in range(1, n):
  sumlist.append(sumlist[i-1] + int(numlist[i]))
  while (True):
    if (sumlist[i-1] > 0 and sumlist[i] > 0):  #i-1,i番目までのsumがともに正
      #numlist[i] = int(numlist[i]) - 1
      count += sumlist[i] + 1
      sumlist[i] = -1
    elif (sumlist[i-1] < 0 and sumlist[i] < 0):  #i-1,i番目までのsumがともに負
      #numlist[i] = int(numlist[i]) + 1
      count += (-1)*sumlist[i] + 1
      sumlist[i] = 1
    elif (sumlist[i] == 0):  #i番目までのsum=0
      if (sumlist[i-1] > 0):
        #numlist[i] = int(numlist[i]) - 1
        sumlist[i] -= 1
      if (sumlist[i-1] < 0):
        #numlist[i] = int(numlist[i]) + 1
        sumlist[i] += 1
      count += 1
    else:
      break
    #print numlist
    #print sumlist

print count

Submission status: Wrong Answer

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

n = int(raw_input())
numlist = (raw_input()).split(' ')

sumlist = [int(numlist[0])]
count = 0

for i in range(1, n):
  sumlist.append(sumlist[i-1] + int(numlist[i]))
  while (True):
    if (sumlist[i-1] > 0 and sumlist[i] > 0):  #i-1,i番目までのsumがともに正
      #numlist[i] = int(numlist[i]) - 1
      count += sumlist[i] + 1
      sumlist[i] = -1
    elif (sumlist[i-1] < 0 and sumlist[i] < 0):  #i-1,i番目までのsumがともに負
      #numlist[i] = int(numlist[i]) + 1
      count += (-1)*sumlist[i] + 1
      sumlist[i] = 1
    elif (sumlist[i] == 0):  #i番目までのsum=0
      if (sumlist[i-1] > 0):
        #numlist[i] = int(numlist[i]) - 1
        sumlist[i] -= 1
      if (sumlist[i-1] < 0):
        #numlist[i] = int(numlist[i]) + 1
        sumlist[i] += 1
      count += 1
    else:
      break

print count

Submission status: Wrong Answer

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

n = int(raw_input())
numlist = (raw_input()).split(' ')

sumlist = [int(numlist[0])]
count = 0

for i in range(1, n):
  sumlist.append(sumlist[i-1] + int(numlist[i]))
  while (True):
    if (sumlist[i-1] > 0 and sumlist[i] > 1):  #i-1,i番目までのsumがともに正
      numlist[i] = int(numlist[i]) - 1
      sumlist[i] -= 1
      count += 1
    elif (sumlist[i-1] < 0 and sumlist[i] < 0):  #i-1,i番目までのsumがともに負
      numlist[i] = int(numlist[i]) + 1
      sumlist[i] += 1
      count += 1
    elif (sumlist[i] == 0):  #i番目までのsum=0
      if (sumlist[i-1] > 0):
        numlist[i] = int(numlist[i]) - 1
        sumlist[i] -= 1
      if (sumlist[i-1] < 0):
        numlist[i] = int(numlist[i]) + 1
        sumlist[i] += 1
      count += 1
    else:
      break

print count

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
n=int(input())
a=input().split()
counter=0
S=int(a[0])
# 出力
for i in range(1,n):
  if S<0 and S+int(a[i])<=0:
    counter+=-S-int(a[i])+1
    a[i]=-S+1
  elif S>0 and S+int(a[i])>=0:
    counter+=S+int(a[i])+1
    a[i]=-S-1
  S+=int(a[i])
print(counter)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
n=int(input())
a=input().split()
counter=0
# 出力
for i in range(1,n):
  S=0
  for j in range(0,i):
    S=S+int(a[j])
  if S<0 and S+int(a[i])<0:
    counter=counter-S-int(a[i])+1
    a[i]=-S+1
  elif S>0 and S+int(a[i])>0:
    counter=counter+S+int(a[i])+1
    a[i]=-S-1
print(counter)

Submission status: Wrong Answer

Solution code:

def check1(a):
  sum = 0
  ans = 0
  for i in range(len(a)):
    if(i % 2 == 0):
      sum += a[i]
      if(sum >= 0):
        a[i] += -abs(sum)-1
        sum += -abs(sum)-1
        ans += abs(sum)+1
    else:
      sum += a[i]
      if(sum <= 0):
        a[i] += abs(sum)+1
        sum += abs(sum)+1
        ans += abs(sum)+1
  return ans

def check2(a):
  sum = 0
  ans = 0
  for i in range(len(a)):
    if(i % 2 == 0):
      sum += a[i]
      if(sum <= 0):
        a[i] += abs(sum)+1
        sum += abs(sum)+1
        ans += abs(sum)+1
    else:
      sum += a[i]
      if(sum >= 0):
        a[i] += -abs(sum)-1
        sum += -abs(sum)-1
        ans += abs(sum)+1
  return ans



n = input()
b = input().split()
a = [int(b[i]) for i in range(len(b))]
a2 = list(a)

ans1 = check1(a)
ans2 = check2(a2)


print(ans1) if(ans1<ans2) else print(ans2)

Submission status: Wrong Answer

Solution code:
n1=int(input())
l1=list(map(int,input().split()))
total=l1[0]
Num=0
for j in range(1,n1):
    pretotal=total
    total=total+l1[j]
    if pretotal ==0:
        total=total+(total)/abs(total)
        Num=Num+1
    while (pretotal*total>0) or (total ==0):
        if total==0:
                Num=Num+1
                if pretotal<0:
                    total=1
                else:
                    total=-1
        elif pretotal<0:
                Num=Num-total+1
                total=+1
        elif pretotal>0:
                Num=Num+total+1
                total=-1
print(Num)

Submission status: Wrong Answer

Solution code:
n1=int(input())
l1=list(map(int,input().split()))
total=l1[0]
Num=0
for j in range(1,n1):
    pretotal=total
    total=total+l1[j]
    if pretotal ==0:
        total=total+(total)/abs(total)
        Num=Num+1
    while (pretotal*total>0) or (total ==0):
        if total==0:
                Num=Num+1
                if pretotal<0:
                    total=1
                else:
                    total=-1
        elif pretotal<0:
                Num=Num-total+1
                total=+1
                print(total)
        elif pretotal>0:
                Num=Num+total+1
                total=-1
print(Num)

Submission status: Wrong Answer

Solution code:
n1=int(input())
l1=list(map(int,input().split()))
total=l1[0]
Num=0
for j in range(1,n1):
    if total ==0:
        total=total+1
        Num=Num+1
        
    pretotal=total
    total=total+l1[j]
    while (pretotal*total>0) or (total ==0):
        if pretotal<0:
                Num=Num-total+1
                total=+1
        elif pretotal>0:
                Num=Num+total+1
                total=-1
        elif total==0:
                Num=Num+1
                total=total+1
print(Num)

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())
a = list(map(int,input().split()))
b = list(a)
sum = 0
p = 0
cnt_g=0
cnt_k=0
if a[0]<0:
    for i in range(n):
        sum += a[i]
        if i>0:
            p = a[i]
            if (sum - a[i]) > 0:
                if sum >= 0:
                    a[i] = -(sum-a[i])-1
                    cnt_g += np.abs(p-a[i])
                    sum = -1
            else:
                if sum <= 0:
                    a[i] = -(sum-a[i])+1
                    cnt_g += np.abs(p-a[i])
                    sum = 1
    sum = 0
    cnt_k = -b[0]+1
    sum = 1
    for i in range(1,n):
        sum += b[i]
        p = b[i]
        if (sum - b[i]) > 0:
            if sum >= 0:
                b[i] = -(sum-b[i])-1
                cnt_k += np.abs(p-b[i])
                sum = -1
        else:
            if sum <= 0:
                b[i] = -(sum-b[i])+1
                cnt_k += np.abs(p-b[i])
                sum = 1
else:
    cnt_g = a[0]+1
    sum = -1
    for i in range(1,n):
        sum += a[i]
        p = a[i]
        if (sum - a[i]) > 0:
            if sum >= 0:
                a[i] = -(sum-a[i])-1
                cnt_g += np.abs(p-a[i])
                sum = -1
        else:
            if sum <= 0:
                a[i] = -(sum-a[i])+1
                cnt_g += np.abs(p-a[i])
                sum = 1
    sum = 0
    for i in range(n):
            sum += b[i]
            if i>0:
                p = b[i]
                if (sum - b[i]) > 0:
                    if sum >= 0:
                        b[i] = -(sum-b[i])-1
                        cnt_k += np.abs(p-b[i])
                        sum = -1
                else:
                    if sum <= 0:
                        b[i] = -(sum-b[i])+1
                        cnt_k += np.abs(p-b[i])
                        sum = 1
if cnt_k<cnt_g:
    print (cnt_k)
else : print (cnt_g)

Submission status: Wrong Answer

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

ans = 1e16
for s in (1, -1):
    res, acc = 0, 0
    for a in A:
        acc += a
        if acc * s <= 0:
            res += abs(acc-s)
            acc = s
        s *= -1
        print("%d " % acc, end="")
    print()
    ans = min(ans, res)

print(ans)

Submission status: Wrong Answer

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

ans = 1e9
for sign in (1, -1):
    s = sign
    res, acc = 0, 0
    for a in A:
        acc += a
        if acc * s <= 0:
            res += abs(acc-s)
            acc = 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 = 1e9
for sign in (1, -1):
    s = sign
    res, acc = 0, 0
    for a in A:
        acc += a
        if acc * s <= 0:
            res += abs(acc-s)
            acc = s
        s *= -1
        print(acc, a, res)
    ans = min(ans, res)
    print()

print(ans)

Submission status: Wrong Answer

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

count = 0

if a[0] == 0:
    for i in range(n):
        if a[i] != 0:
            if a[i] % 2 == 0:
                a[0] = a[i]//abs(a[i])
            else:
                a[0] = -a[i]//abs(a[i])
            count = 1
            break
    if not(count):
        a[0] = 1
        count += 1

s = [0]*n
s[0] = a[0]

for i in range(1,n):
    s[i] = s[i-1] + a[i]

    if s[i] == 0:
        tmp = -s[i-1]//abs(s[i-1])
        s[i] += tmp
        count += abs(tmp)
    elif s[i] * s[i-1] > 0:
        tmp = (-s[i]) + (-s[i]//abs(s[i]))
        s[i] += tmp
        count += abs(tmp)


print(count)

Submission status: Wrong Answer

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

s = [0]*n
s[0] = a[0]
count = 0

for i in range(1,n):
    s[i] = s[i-1] + a[i]

    if s[i] == 0:
        tmp = -s[i-1]//abs(s[i-1])
        s[i] += tmp
        count += abs(tmp)
    elif s[i] * s[i-1] > 0:
        tmp = (-s[i]) + (-s[i]//abs(s[i]))
        s[i] += tmp
        count += abs(tmp)


print(count)

Submission status: Wrong Answer

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

s = [0]*n
s[0] = a[0]
count = 0

for i in range(1,n):
    s[i] = s[i-1] + a[i]

    if s[i] * s[i-1] > 0:
        tmp = (-s[i]) + (-s[i]//abs(s[i]))
        s[i] += tmp
        count += abs(tmp)

    if s[i] == 0:
        tmp = -s[i-1]//abs(s[i-1])
        s[i] += tmp
        count += abs(tmp)

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
b = [int(x) for x in input().split()]
a = list()
temp = 0
count1 = 0
count2 = 0

a = b.copy()

if a[0] == 0:
	a[0] = 1
	count1 = 1

sum = a[0]
for i in range(1, n):
	if abs(a[i]) <= abs(sum) or a[i] * sum >= 0:
		count1 += abs(temp - a[i])
		if sum > 0:
			temp = -1 * abs(sum) - 1
		else:
			temp = abs(sum) + 1
		a[i] = temp
	sum += a[i]

a = b.copy()

count2 = abs(a[0]) + 1
if a[0] >= 0:
	a[0] = -1
else:
	a[0] = 1
	
sum = a[0]
for i in range(1, n):
	if abs(a[i]) <= abs(sum) or a[i] * sum >= 0:
		count2 += abs(temp - a[i])
		if sum > 0:
			temp = -1 * abs(sum) - 1
		else:
			temp = abs(sum) + 1
		a[i] = temp
	sum += a[i]

print(min(count1, count2))

Submission status: Wrong Answer

Solution code:
n = int(input())
b = [int(x) for x in input().split()]
a = list()
temp = 0
count1 = 0
count2 = 0

a = b.copy()

if a[0] == 0:
	a[0] = 1
	count1 = 1

sum = a[0]
for i in range(1, n):
	if abs(a[i]) <= abs(sum) or a[i] * sum >= 0:
		if sum > 0:
			temp = -1 * abs(sum) - 1
			count1 += abs(temp - a[i])
		else:
			temp = abs(sum) + 1
			count1 += abs(temp - a[i])
		a[i] = temp
	sum += a[i]

a = b.copy()

count2 = abs(a[0]) + 1
if a[0] >= 0:
	a[0] = -1
else:
	a[0] = 1
	
sum = a[0]
for i in range(1, n):
	if abs(a[i]) <= abs(sum) or a[i] * sum >= 0:
		count2 += abs(sum - a[i]) + 1
		if sum > 0:
			temp = -1 * abs(sum) - 1
			count2 += abs(temp - a[i])
		else:
			temp = abs(sum) + 1
			count2 += abs(temp - a[i])
		a[i] = temp
	sum += a[i]

print(min(count1, count2))

Submission status: Wrong Answer

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

n = input()
a = map(int, raw_input().split())
b = a
asum = [0]*len(a)
bsum = [0]*len(a)

asum[0] = a[0]
bsum[0] = b[0]
cnt_a = 0
cnt_b = 0
if(a[0]==0):
    asum[0] = 1
    cnt_a = 1
    bsum[0] = -1
    cnt_b = 1

for i in range(len(a)-1):
    asum[i+1] = a[i+1] + asum[i]
    if(i%2==0 and asum[i+1]>=0):
        cnt_a += asum[i+1]+1
        asum[i+1] = -1
    elif(i%2==1 and asum[i+1]<=0):
        cnt_a += (-1)*asum[i+1]+1
        asum[i+1] = 1
for i in range(len(b)-1):
    bsum[i+1] = b[i+1] + bsum[i]
    if(i%2==0 and bsum[i+1]<=0):
        cnt_b += (-1)*bsum[i+1]+1
        bsum[i+1] = 1
    elif(i%2==1 and bsum[i+1]>=0):
        cnt_b += bsum[i+1]+1
        bsum[i+1] = -1
            
print(min(cnt_a,cnt_b))