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:
n = int(input())
A = list(map(int, input().split()))
ans = 0
total = A[0]
if A[0] == 0:
    ans += 1
    total = -A[1] // A[1]
for i in range(1, len(A)):
    print(total, ans, A[i], total + A[i])
    if total > 0:
        if total + A[i] >= 0:
            ans += total + A[i] + 1
            print("1 ans +=", total + A[i] + 1)
            total = -1
        else:
            total += A[i]
    else:
        if total + A[i] <= 0:
            ans += 1 - (total + A[i])
            print("2 ans +=", 1 - (total + A[i]))
            total = 1
        else:
            total += A[i]
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
import numpy as np
na = np.array(a).cumsum()
cnt = 0
hoge = 0
if(na[0] > 0):
  for i in range(n):
    delta = abs(na[i]) + 1
    if(i % 2 == 0 and na[i] <= 0):
      cnt = cnt + delta
      hoge += delta
      na[i] += hoge
    elif(i % 2 == 1 and na[i] >= 0):
      cnt = cnt + delta
      hoge -= delta
      na[i] -= hoge
    else:
      na[i]
else:
  for i in range(n):
    delta = abs(na[i]) + 1
    if(i % 2 == 1 and na[i] <= 0):
      cnt = cnt + delta
      hoge += delta
      na[i] += hoge
    elif(i % 2 == 0 and na[i] >= 0):
      cnt = cnt + delta
      hoge -= delta
      na[i] -= hoge
    else:
      na[i]
print(cnt)

Submission status: Wrong Answer

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

ans = 10**10

# 1 -1 1 -1...
cost1 = 0
s = 0
for i in range(n):
    t = 1 if i % 2 == 0 else -1
    c = 0
    if i % 2 == 0:
        if s + a[i] > 0:
            s += a[i]
        else:
            c = abs(s - t)
            s += c
            cost1 += abs(c - a[i])
    else:
        if s + a[i] < 0:
            s += a[i]
        else:
            c = abs(s - t)
            s -= c
            cost1 += abs(c - a[i])

cost2 = 0
s = 0
for i in range(n):
    t = -1 if i % 2 == 0 else 1
    c = 0
    if i % 2 == 1:
        if s + a[i] > 0:
            s += a[i]
        else:
            c = abs(s - t)
            s += c
            cost2 += abs(c - a[i])
    else:
        if s + a[i] < 0:
            s += a[i]
        else:
            c = abs(s - t)
            s -= c
            cost2 += abs(- c - a[i])


print(min(cost1, cost2))
# -1 1 -1 1..

Submission status: Wrong Answer

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

ans = 10**10

# 1 -1 1 -1...
cost1 = 0
s = 0
for i in range(n):
    t = 1 if i % 2 == 0 else -1
    c = 0
    if i % 2 == 0:
        if s + a[i] > 0:
            s += a[i]
        else:
            c = abs(s - t)
            s += c
            cost1 += abs(c - a[i])
    else:
        if s + a[i] < 0:
            s += a[i]
        else:
            c = abs(s - t)
            s -= c
            cost1 += abs(c - a[i])

cost2 = 0
s = 0
for i in range(n):
    t = -1 if i % 2 == 0 else 1
    c = 0
    if i % 2 == 1:
        if s + a[i] > 0:
            s += a[i]
        else:
            c = abs(s - t)
            s += c
            cost2 += abs(c - a[i])
    else:
        if s + a[i] < 0:
            s += a[i]
        else:
            c = abs(s - t)
            s -= c
            cost2 += abs(- c - a[i])

print(cost1, cost2)
print(min(cost1, cost2))
# -1 1 -1 1..

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    A = [int(a) for a in input().split()]
    min_count = float('inf')
    if A[0] != 0:
        tot = A[0]
        count = 0
        for a in A[1:]:
            if tot > 0 and tot + a >= 0:
                count += tot+a+1
                tot = -1
            elif tot < 0 and tot + a <= 0:
                count += -(tot+a)+1
                tot = 1
            else:
                tot += a
        min_count = min(min_count, count)
    else:
        for i in [1, -1]:
            count = 1
            tot = i
            for a in A[1:]:
                if tot > 0 and tot + a >= 0:
                    count += tot+a+1
                    tot = -1
                elif tot < 0 and tot + a <= 0:
                    count += -(tot+a)+1
                    tot = 1
                else:
                    tot += a
            min_count = min(min_count, count)
    return min_count

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    A = [int(a) for a in input().split()]
    min_count = float('inf')
    tot = 0
    if A[0] != 0:
        tot = A[0]
        count = 0
        for a in A[1:]:
            if tot > 0 and tot + a >= 0:
                count += tot+a+1
                tot = -1
            elif tot < 0 and tot + a <= 0:
                count += -(tot+a)+1
                tot = 1
            else:
                tot += a
        min_count = min(min_count, count)
    else:
        count = 1
        for i in [1, -1]:
            tot = i
            for a in A[1:]:
                if tot > 0 and tot + a >= 0:
                    count += tot+a+1
                    tot = -1
                elif tot < 0 and tot + a <= 0:
                    count += -(tot+a)+1
                    tot = 1
                else:
                    tot += a
            min_count = min(min_count, count)
    return min_count

if __name__ == '__main__':
    print(main())

Submission status: Wrong Answer

Solution code:
def is_plus(x):
    return 0 <= x

_=int(input())
A = list(map(int, input().split()))


total_cnt = 1 if A[0] == 0 else 0
cur_sum = A[0] + total_cnt
pre_sum = cur_sum
for a in A[1:]:
    cur_sum += a
    if is_plus(pre_sum) == is_plus(cur_sum):
        total_cnt += abs(cur_sum) + 1
        cur_sum = -1 if is_plus(cur_sum) else +1
    elif cur_sum == 0:
        total_cnt += 1
        cur_sum = 1
    else:
        pass
    pre_sum = cur_sum

print(total_cnt)

Submission status: Wrong Answer

Solution code:
_,*A=map(int,open(0).read().split())

def is_plus(x):
  return 0 <= x

total_cnt=1 if A[0]==0 else 0
cur_sum=A[0]+total_cnt
pre_sum=cur_sum
for a in A[1:]:
  cur_sum+=a
  if is_plus(pre_sum)==is_plus(cur_sum) or cur_sum==0:
    total_cnt+=abs(cur_sum)+1
    cur_sum=-1 if is_plus(cur_sum) else +1
  pre_sum=cur_sum

print(total_cnt)

Submission status: Wrong Answer

Solution code:

N = int(input())

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

cum_A = [0] * N

ans = 0
if A[0] == 0:
    ans += 1
    cum_A[0] = 1
else:
    cum_A[0] = A[0]


for i in range(1,N):
    if cum_A[i-1] < 0:
        if cum_A[i-1] + A[i] >= 1:
            cum_A[i] = cum_A[i-1] + A[i]
        else:
            ans += 1 - (cum_A[i-1] + A[i])
            cum_A[i] = 1
    else:
        if cum_A[i-1] + A[i] <= -1:
            cum_A[i] = cum_A[i-1] + A[i]
        else:
            ans += 1 + (cum_A[i-1] + A[i])
            cum_A[i] = -1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
nums = list(map(int, input().split()))
ans = 10**10+1
for start in [-1, 1]:
  before = start
  cnt = 0
  sum_n = 0
  for num in nums:
    sum_n += num
    if before*sum_n >= 0:
      if before < 0:
        cnt += 1-sum_n
        sum_n = 1
      else:
        cnt += 1+sum_n
        sum_n = -1
    before = sum_n
  ans = min(ans, cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
nums = list(map(int, input().split()))
ans = 10**10+1
for start in [-1, 1]:
  before = start
  cnt = 0
  sum_n = 0
  for num in nums:
    sum_n += num
    if before*sum_n >= 0:
      if before < 0:
        cnt += abs(1-sum_n)
        sum_n = 1
      else:
        cnt += abs(-1-sum_n)
        sum_n = -1
    before = sum_n
  ans = min(ans, cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
nums = list(map(int, input().split()))
ans = 10**5+1
for start in [-1, 1]:
  before = start
  cnt = 0
  sum_n = 0
  for num in nums:
    sum_n += num
    if before*sum_n >= 0:
      if before < 0:
        cnt += abs(1-sum_n)
        sum_n = 1
      else:
        cnt += abs(-1-sum_n)
        sum_n = -1
    before = sum_n
  ans = min(ans, cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
nums = list(map(int, input().split()))
sum_n = 0
ans = 10**5+1
for start in [-1, 1]:
  before = start
  cnt = 0
  for num in nums:
    sum_n += num
    if before*sum_n >= 0:
      if before < 0:
        cnt += abs(1-sum_n)
        before = 1
      else:
        cnt += abs(-1-sum_n)
        before = -1
    else:
      before = sum_n
    print(cnt)
  ans = min(ans, cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
nums = int(input())
ary = list(map(int,input().split()))

#puls no toki
if ary[0] < 0 :
    ary[0] = ary[0] + ary[0] +1
elif ary[0] == 0:
    ary[0] += 1
sum = ary[0]
count = 0

for i in range (1,nums):
    sum += ary[i]

    if i % 2 == 1:
        if sum > 0 :
            count += abs(sum)+1
            sum = -1
        elif sum == 0:
            sum = -1
            count += 1
    elif i % 2 == 0:
        if sum < 0:
            count += abs(sum)+1
            sum  = 1
        elif sum == 0:
            sum = 1
            count += 1

result_plus = count

#minus no toki
sum = ary[0]
if ary[0] > 0:
    ary[0] = ary[0] - ary[0] -1
elif ary[0] == 0:
    ary[0] -= 1

count = 0

for i in range(1,nums):
    sum += ary[i]
    if i % 2 == 1:
        if sum < 0:
            count += abs(sum)+1
            sum = 1
        elif sum == 0:
            count += 1
            sum = 1
    if i % 2 == 0:
        if sum > 0:
            count += abs(sum)+1
            sum = -1
        elif sum == 0:
            count += 1
            sum = -1

result_minus = count

if result_plus > result_minus:
    print(result_minus)
else:
    print(result_plus)

Submission status: Wrong Answer

Solution code:
from copy import copy
n = int(input())
a = [int(x) for x in input().split()]
ans1=[(-1)**i for i in range(n)]

b=copy(a)
res_b=0
sb=0
c=copy(a)
res_c=0
sc=0
for i in range(n):
  
  sb+=a[i]
  print("sb:",sb)
  if ans1[i]*sb>0:
    pass
  else:
    b[i]=ans1[i]-(sb-b[i])
    sb=sb-a[i]+b[i]
    res_b+=abs(b[i]-a[i])

  sc+=a[i]
  if -1*ans1[i]*sc>0:
    pass
  else:
    c[i]=-1*ans1[i]-(sc-c[i])
    sc=sc-a[i]+c[i]
    res_c+=abs(c[i]-a[i])
  
print(min(res_b,res_c))

Submission status: Wrong Answer

Solution code:
from copy import copy
n = int(input())
a = [int(x) for x in input().split()]
ans1=[(-1)**i for i in range(n)]

b=copy(a)
res_b=0
c=copy(a)
res_c=0

for i in range(n):
  if ans1[i]*sum(b[:i+1])>0:
    c[i]=-1*ans1[i]-sum(c[:i])
    res_c+=abs(c[i]-a[i])
  else:
    b[i]=ans1[i]-sum(b[:i])
    res_b+=abs(b[i]-a[i])


print(min(res_b,res_c))

Submission status: Wrong Answer

Solution code:

import bisect
import collections
import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint2d(rows): return [getints() for _ in range(rows)]
def getgrid(rows): return [input() for _ in range(rows)]
def array1d(n, value): return [value for _ in range(n)]
def array2d(n, m, value): return [array1d(m, value) for _ in range(n)]

n = getint()
values = getints()

def solve(values, prev):
    res = 0
    for v in values:
        s = prev + v
        add = 0
        if prev > 0 and s >= 0:
            add = -s - 1
        if prev < 0 and s <= 0:
            add = -s + 1
        res += abs(add)
        prev = s + add
    return res

res1, res2 = 0, 0
if values[0] <= 0:
    add = abs(values[0]) + 1
    res1 = solve(values[1:], values[0] + add) + add
    res2 = solve(values[1:], values[0])
else:
    res1 = solve(values[1:], values[0])
    add = -(values[0] + 1)
    res2 = solve(values[1:], values[0] + add) + abs(add)

print(min(res1, res2))

Submission status: Wrong Answer

Solution code:
#! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3
# a[0] == 0 のケース、要検証

n = int(input())
a = list(map(int, input().split()))
count_p = 0  # 操作回数(正)
count_m = 0  # 操作回数(負)
s = 0  # 現時点の和


def counter(a, s):
    if s * (s + a) < 0:
        return 0
    else:
        if s < 0:
            return 1 - (s + a)
        else:
            return -1 - (s + a)


# a[0]を+にする場合
if a[0] <= 0:
    count_p = 1 - a[0]
    s = 1
else:
    s = a[0]

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

# a[0]を-にする場合
if a[0] >= 0:
    count_m = 1 - a[0]
    s = -1
else:
    s = a[0]


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

print(str(min(count_p, count_m)))

Submission status: Wrong Answer

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

def j(B,sign):
    ans,cur = 0,[0]
    for i in range(n):
        cur.append(cur[i]+B[i])
        cs = np.sign(cur[i+1])
        if cs != sign:
            if sign == -1:
                ans += -(-cur[i]-1)+B[i]
                cur[i+1] = -1
            else:
                ans += -cur[i]+1-B[i]
                cur[i+1] = 1
        sign *= -1
    return ans

print(j(A,np.sign(A[0])))

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(n: int, a: "List[int]"):

    # 正スタート
    tot = 0
    countA = 0
    for i, x in enumerate(a):
        tot += x
        if i % 2 == 0:
            if tot <= 0:
                countA += -tot+1
                tot = 1
        elif i % 2 == 1:
            if tot >= 0:
                countA += tot+1
                tot = -1

    tot = 0
    countB = 0
    for i, x in enumerate(a):
        tot += x
        if i % 2 == 1:
            if tot <= 0:
                countB += -tot+1
                tot = 1
        elif i % 2 == 0:
            if tot >= 1:
                countB += tot+1
                tot = -1
    print(min(countA, countB))

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(n)]  # type: "List[int]"
    solve(n, a)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
lista=list(map(int,input().split()))
updown=""
goukei=0
count=0
if lista[0]<0:
    updown="up"
else:
    updown="down"
goukei=goukei+lista[0]
print(goukei,count,updown)
for i in range(1,n):
    if updown=="down":
        if lista[i]+goukei<0:
            goukei+=lista[i]
            updown="up"
            print(goukei,count,updown)
        elif lista[i]+goukei>0:
            count=count+abs(lista[i]+goukei)+1
            goukei=-1
            down="up"
            print(goukei,count,updown)
            
    elif updown=="up":
        if lista[i]+goukei>0:
            goukei+=lista[i]
            updown="down"
            print(goukei,count,updown)
            
        elif lista[i]+goukei<0:
            count=count+abs(lista[i]+goukei)+1
            goukei=1
            updown="down"
            print(goukei,count,updown)    
print(count)

Submission status: Wrong Answer

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

ans = 0

sum_ = as_[0]
if sum_ == 0 and as_[1] >= 0:
  sum_ -= 1
  ans += 1
elif sum_ == 0 and as_[1] < 0:
  sum_ += 1
  ans += 1
else:
  pass

for i in range(1, n):
  new_sum_ = sum_+as_[i]
  if sum_ > 0 and new_sum_ >= 0:
    as_[i] -= (1+new_sum_)
    ans += 1+new_sum_
    sum_ = -1
  elif sum_ < 0 and new_sum_ <= 0:
    as_[i] += (1-new_sum_)
    ans += 1-new_sum_
    sum_ = 1
  else:
    sum_ = new_sum_
print(ans)

Submission status: Wrong Answer

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

ans = 0

sum_ = as_[0]
if sum_ == 0 and as_[1] <= 0:
  sum_ += 1
  ans += 1
elif sum_ == 0 and as_[1] < 0:
  sum_ += 1
  ans += 1
else:
  pass

for i in range(1, n):
  new_sum_ = sum_+as_[i]
  if sum_ > 0 and new_sum_ >= 0:
    as_[i] -= (1+new_sum_)
    ans += 1+new_sum_
    sum_ = -1
  elif sum_ < 0 and new_sum_ <= 0:
    as_[i] += (1-new_sum_)
    ans += 1-new_sum_
    sum_ = 1
  else:
    sum_ = new_sum_
print(ans)

Submission status: Wrong Answer

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

ans = 0

sum_ = as_[0]
if sum_ == 0 and as_[1] <= 0:
  sum_ += 1
  ans += 1
elif sum_ == 0 and as_[1] < 0:
  sum_ += 1
  ans += 1
else:
  pass

for i in range(1, n):
  new_sum_ = sum_+as_[i]
  if sum_ > 0 and new_sum_ >= 0:
    as_[i] -= (1+new_sum_)
    ans += 1+new_sum_
    sum_ = -1
  elif sum_ < 0 and new_sum_ <= 0:
    as_[i] += (1-new_sum_)
    ans += 1-new_sum_
    sum_ = 1
  else:
    sum_ = new_sum_

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
sun = 0
ans1 = 0
for i in range(n):
  if i == 0:
    SUN = 1
  else:
    SUN = sun
  now = a[i]
  if SUN * (sun+now) >= 0:
    ans1 += abs(sun+now) + 1
    if sun > 0:
      sun = -1
    else:
      sun = 1
  else:
    sun += now
sun = 0
ans2 = 0
for i in range(n):
  now = a[i]
  if i == 0:
    SUN = -1
  else:
    SUN = sun
  if SUN * (sun+now) >= 0:
    ans2 += abs(sun+now) + 1
    if sun > 0:
      sun = -1
    else:
      sun = 1
  else:
    sun += now
#print(ans1,ans2)
print(min(ans1,ans2))

Submission status: Wrong Answer

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

total = 0
count = 0
pos_or_neg = True  # True = +, False = -

if sum(a_list) >= 0:
    if n % 2 == 0:
        pos_or_neg = False
    else:
        pos_or_neg = True
else:
    if n % 2 == 0:
        pos_or_neg = True
    else:
        pos_or_neg = False

for a in a_list:
    total += a
    if pos_or_neg:
        if total <= 0:
            count += -total + 1
            total += -total + 1
        pos_or_neg = False

    else:
        if total >= 0:
            count += total + 1
            total -= total + 1
        pos_or_neg = True

print(count)

Submission status: Wrong Answer

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

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


def calc() :
    ret = 0
    s = a[0]
    for i in range(1, n) :
        pre = sign(s)
        cur = sign(s+a[i])
        if pre == cur or cur == 0:
            d = (abs(s) + 1) * pre * -1
            ret += abs(d - a[i])
            s = 1 * pre * -1
        else :
            s += a[i]
    return ret
    
if a[0] == 0 :
    a[0] = 1
    s = calc()
    a[1] = -1
    t = calc()
    print(min(s, t) + 1)
else :
    print(calc())

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
l = len(a)
cnt = 0
a_ = 0
x = -1
# -から
for i in range(l):
    a_ += a[i]
    if x == -1:
        if a_ > x:
            cnt += a_ -x
            a_ = -1
    else:
        if a_ < x:
            cnt += x-a_
            
            a_ = 1

    x *= -1

cnt1 = 0

x1 = 1
# +から
for i in range(l):
    a_ += a[i]
    if x1 == -1:
        if a_ > x1:
            cnt += a_ -x
            a_ = -1
    else:
        if a_ < x1:
            cnt += x1-a_
            
            a_ = 1

    x1 *= -1

print(min(x, x1))

Submission status: Wrong Answer

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


def solve(A, N):
    S = [0]*N
    answer = 0
    total = 0
    for i in range(N):
        total += A[i]
        S[i] = total
        if total == 0:
            d = 1 if S[i-1] < 0 else -1
            A[i] += d
            answer += 1
            total += d
        else:
            # S[i-1] < 0 and S[i] > 0
            # S[i-1] > 0 and S[i] < 0
            # S[i-1] > 0 and S[i] > 0
            if S[i-1] < 0 and S[i] < 0:
                answer += -S[i]+1
                total += -S[i]+1
            elif S[i-1] > 0 and S[i] > 0:
                answer += S[i]+1
                total += -(S[i]+1)
        S[i] = total
        #print(i, answer, "S[i-1]=%d"%S[i-1], "S[i]=%d"%S[i], sep="\t")
    return answer

if A[0] == 0:
    A[0] = -1
    m1 = solve(A, N) + 1
    A[0] = +1
    m2 = solve(A, N) + 1
    print(min(m1, m2))
else:
    print(solve(A, N))

Submission status: Wrong Answer

Solution code:
N = int(input())
a =list(map(int,input().split()))
a1=[0]*N
a2=[0]*N

s1 =a[0]
s2 =a[0]
#正の数から始まるパターン
c1 = 0
if s1 ==0:
    s1 = 1
    c1 = 1
elif s1 <0:
    a1[0]=1
    c1 = -s1+1

for i in range(1,N):
    if s1*(s1+a[i])>=0 and s1>0:
        a1[i] = -s1-1
        c1 += abs(a[i]-a1[i])
    elif s1*(s1+a[i])>=0 and s1<0:
        a1[i] = -s1+1
        c1 += abs(a1[i]-a[i])
    else:
        a1[i]=a[i]
    s1 = s1+a1[i]
#負の数から始まるパターン

c2=0
if s2 ==0:
    s2 = 1
    c2 =1
elif s2 >0:
    a2[0]=-1
    c2 = s2+1
    
for i in range(1,N):
    if s2*(s2+a[i])>=0 and s2>0:
        a2[i] = -(s2+1)
        c2 +=abs(a[i]-a2[i])
    elif s2*(s2+a[i])>=0 and s2<0:
        a2[i] = -s2+1
        c2 +=abs(a2[i]-a[i])
    else:
        a2[i]=a[i]
    s2 = s2+a2[i]    
    


print(min(c1,c2))

Submission status: Wrong Answer

Solution code:
N = int(input())
a =list(map(int,input().split()))
a1=[0]*N
a2=[0]*N

s1 =a[0]
s2 =a[0]
#正の数から始まるパターン
c1 = 0
if s1 ==0:
    s1 = 1
    c1 = 1
elif s1 <0:
    a1[0]=1
    c1 = -s1+1

for i in range(1,N):
    if s1*(s1+a[i])>=0 and s1>0:
        a1[i] = -(s1+1)
        c1 +=a[i]-a1[i]
    elif s1*(s1+a[i])>=0 and s1<0:
        a1[i] = -s1+1
        c1 +=a1[i]-a[i]
    else:
        a1[i]=a[i]
    s1 = s1+a1[i]
#負の数から始まるパターン

c2=0
if s2 ==0:
    s2 = 1
    c2 =1
elif s2 >0:
    a2[0]=-1
    c2 = s2+1
    
for i in range(1,N):
    if s2*(s2+a[i])>=0 and s2>0:
        a2[i] = -(s2+1)
        c2 +=a[i]-a2[i]
    elif s2*(s2+a[i])>=0 and s2<0:
        a2[i] = -s2+1
        c2 +=a2[i]-a[i]
    else:
        a2[i]=a[i]
    s2 = s2+a2[i]    
    


print(min(c1,c2))

Submission status: Wrong Answer

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

s =a[0]
c = 0
if s ==0:
    s = 1
    c = 1
    
for i in range(1,N):
    if s*(s+a[i])>=0 and s>0:
        c = c+abs(s+a[i])+1
        a[i] = a[i]-(abs(s+a[i])+1)
    elif s*(s+a[i])>=0 and s<0:
        c = c+abs(s+a[i])+1
        a[i] = a[i]+abs(s+a[i])+1
    s = s+a[i]

print(c)

Submission status: Wrong Answer

Solution code:
N = int(input())
nums = list(map(int, input().split()))
sum_n = 0
before = 0
ans = 10*14
for n in [-1, 1]:
  cnt = 0
  before = n
  sum_n = nums[0]
  if sum_n * before >= 0:
    if before > 0:
      cnt += abs(-1-sum_n)
      sum_n = -1
    else:
      cnt += abs(1-sum_n)
      sum_n = 1
  before = sum_n
  for num in nums[1:]:
    sum_n += num
    if before * sum_n >= 0:
      if before > 0:
        cnt += abs(-1-sum_n)
        sum_n = -1
      else:
        cnt += abs(1-sum_n)
        sum_n = 1
    before = sum_n
  ans = min(ans, cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
nums = list(map(int, input().split()))
sum_n = 0
before = 0
ans = 10*14
for n in [-1, 1]:
  cnt = 0
  before = n
  sum_n = nums[0]
  if sum_n * before >= 0:
    if before > 0:
      sum_n = -1
    else:
      sum_n = 1
    cnt += abs(sum_n - before)
  before = sum_n
  for num in nums[1:]:
    sum_n += num
    if before * sum_n >= 0:
      if before > 0:
        cnt += abs(-1-sum_n)
        sum_n = -1
      else:
        cnt += abs(1-sum_n)
        sum_n = 1
    before = sum_n
  ans = min(ans, cnt)
print(ans)

Submission status: Wrong Answer

Solution code:

import itertools
from collections import Counter
from collections import defaultdict
import bisect
from heapq import heappush, heappop

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

    ans = 10**10

    cumulative = 0
    count = 0
    for i in range(len(a)):
        cumulative += a[i]
        if i % 2 == 0: # positive
            if cumulative <= 0:
                count += abs(cumulative) + 1
                cumulative += (abs(cumulative) + 1)
        else:  # negative
            if cumulative >= 0:
                count += abs(cumulative) + 1
                cumulative -= (abs(cumulative) + 1)
        # print('count = {} c = {}'.format(count, cumulative))
    ans = min(ans, count)

    cumulative = 0
    count = 0
    for i in range(len(a)):
        cumulative += a[i]
        if i % 2 == 0: # negative
            if cumulative >= 0:
                count += abs(cumulative) + 1
                cumulative -= (abs(cumulative) + 1)
        else:  # positive
            if cumulative <= 0:
                count += abs(cumulative) + 1
                cumulative += (abs(cumulative) + 1)
        # print('count = {} c = {}'.format(count, cumulative))

    ans = min(ans, count)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

import itertools
from collections import Counter
from collections import defaultdict
import bisect
from heapq import heappush, heappop

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

    ans = 0

    cumulative = 0
    count = 0
    for i in range(len(a)):
        cumulative += a[i]
        if i % 2 == 0: # positive
            if cumulative <= 0:
                count += abs(cumulative) + 1
                cumulative -= (abs(cumulative) + 1)
        else:  # negative
            if cumulative >= 0:
                count += abs(cumulative) + 1
                cumulative += (abs(cumulative) + 1)
    ans = max(ans, count)

    cumulative = 0
    count = 0
    for i in range(len(a)):
        cumulative += a[i]
        if i % 2 == 0: # negative
            if cumulative >= 0:
                count += abs(cumulative) + 1
                cumulative -= (abs(cumulative) + 1)
        else:  # positive
            if cumulative <= 0:
                count += abs(cumulative) + 1
                cumulative -= (abs(cumulative) + 1)

    ans = max(ans, count)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

import itertools
from collections import Counter
from collections import defaultdict
import bisect
from heapq import heappush, heappop

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

    ans = 0

    cumulative = 0
    count = 0
    for i in range(len(a)):
        cumulative += a[i]
        if i % 2 == 0: # positive
            if cumulative <= 0:
                count += abs(cumulative) + 1
                cumulative -= (abs(cumulative) + 1)
        else:  # negative
            if cumulative >= 0:
                count += abs(cumulative) + 1
                cumulative += (abs(cumulative) + 1)
    ans = max(ans, count)

    for i in range(len(a)):
        cumulative += a[i]
        if i % 2 == 0: # negative
            if cumulative >= 0:
                count += abs(cumulative) + 1
                cumulative -= (abs(cumulative) + 1)
        else:  # positive
            if cumulative <= 0:
                count += abs(cumulative) + 1
                cumulative -= (abs(cumulative) + 1)

    ans = max(ans, count)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

import itertools
from collections import Counter
from collections import defaultdict
import bisect
from heapq import heappush, heappop

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

    cumulative = 0
    ans = 0
    for v in a:
        if cumulative == 0: # first time
            cumulative += v
        else:
            if cumulative > 0:
                if cumulative + v >= 0:
                    ans += abs(cumulative + v) + 1
                    cumulative += v - (abs(cumulative + v) + 1)
                else:
                    cumulative += v
            else:
                if cumulative + v <= 0:
                    ans += abs(cumulative + v) + 1
                    cumulative += v + (abs(cumulative + v) + 1)
                else:
                    cumulative += v
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# 必要になったら結果が1か-1になるまで加えたり引いたりする方針
# 実装がちょっと無駄に考える 符号変わったりするとか先頭が0の場合とか

n = int(input())
arr = list(map(int, input().split()))
tmp = arr[0]


def f(arr, tmp):
    count = 0
    for i in range(1, n):
        if tmp + arr[i] < 0 < tmp:
            # 加えたら符号が+から-になる場合
            tmp += arr[i]
        elif tmp < 0 < tmp + arr[i]:
            # 加えたら符号が-から+になる場合
            tmp += arr[i]
        elif 0 <= tmp + arr[i] <= tmp or 0 <= tmp <= tmp + arr[i]:
            # 加えても+から-にはならず+のまま
            count += abs(-1 - tmp - arr[i])
            tmp = -1
        elif tmp + arr[i] <= tmp <= 0 or tmp <= tmp + arr[i] <= 0:
            # 加えても-から+にはならず-のまま
            count += abs(1 - tmp - arr[i])
            tmp = 1
        else:
            # runtime errorの出し方がわからんのでTLEを狙う（おい
            while True:
                if 1 == 0:
                    break

    return count


if tmp == 0:
    if arr[1] != 0:
        print(min(f(arr, 1), f(arr, -1)) + 1)
    else:
      # もうこんなんうまくやる方法なんて思いつかんわ
        b = []
        ni = -2
        o = 0
        for i, a in enumerate(arr):
            if a == 0:
                if i == 0:
                    b.append(1)
                    o += 1
                elif i == 1:
                    b.append(ni)
                    o += 2
                else:
                    ni *= -1
                    b.append(ni)
                    o += 2
            else:
                break
        b_r = list(map(lambda x: x * -1, b))
        arr_a = b + arr[len(b) :]
        arr_b = b_r + arr[len(b_r) :]
        print(min(f(arr_a, 1), f(arr_b, -1)) + o)

else:
    print(f(arr, tmp))

Submission status: Wrong Answer

Solution code:
# 必要になったら結果が1か-1になるまで加えたり引いたりする方針
# 実装がちょっと考える 符号変わったりするとか先頭が0の場合とか

n = int(input())
arr = list(map(int, input().split()))
tmp = arr[0]


def f(arr, tmp):
    count = 0
    for i in range(1, n):
        if tmp + arr[i] < 0 < tmp:
            # 加えたら符号が+から-になる場合
            tmp += arr[i]
        elif tmp < 0 < tmp + arr[i]:
            # 加えたら符号が-から+になる場合
            tmp += arr[i]
        elif 0 <= tmp + arr[i] <= tmp or 0 <= tmp <= tmp + arr[i]:
            # 加えても+から-にはならず+のまま
            count += abs(-1 - tmp - arr[i])
            tmp = -1
        elif tmp + arr[i] <= tmp <= 0 or tmp <= tmp + arr[i] <= 0:
            # 加えても-から+にはならず-のまま
            count += abs(1 - tmp - arr[i])
            tmp = 1
        else:
            # runtime errorの出し方がわからんのでTLEを狙う（おい
            while True:
                if 1 == 0:
                    break

    return count


if tmp == 0:
    print(min(f(arr, 1), f(arr, -1)) + 1)
else:
    print(f(arr, tmp))