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()))

res = []
for start in [1, -1]:

    ans = 0
    _a = [a[0]]
    prev_sign = start
    for i in range(0, N):
        c = a[i]
        if c >= 0 and prev_sign > 0:
            ans += abs(c - (-1))
            c = -1
        elif c <= 0 and prev_sign < 0:
            ans += abs(c - 1)
            c = 1
        if c > 0:
            prev_sign = 1
        else:
            prev_sign = -1
        _a.append(c)

    __a = [_a[0]]
    acm_sum = _a[0]
    for i in range(1, N):
        c = _a[i]
        if abs(acm_sum) >= abs(c):
            if c < 0:
                c = -1*(abs(acm_sum)+1)
            else:
                c = abs(acm_sum)+1
        acm_sum += c
        ans += abs(c - _a[i])
        __a.append(c)
    res.append(ans)

print(min(res))

Submission status: Wrong Answer

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

res = []
for start in [1, -1]:

    ans = 0
    _a = [a[0]]
    prev_sign = start
    for i in range(1, N):
        c = a[i]
        if c >= 0 and prev_sign > 0:
            ans += abs(c - (-1))
            c = -1
        elif c <= 0 and prev_sign < 0:
            ans += abs(c - 1)
            c = 1
        if c > 0:
            prev_sign = 1
        else:
            prev_sign = -1
        _a.append(c)

    __a = [_a[0]]
    acm_sum = _a[0]
    for i in range(1, N):
        c = _a[i]
        if abs(acm_sum) >= abs(c):
            if c < 0:
                c = -1*(abs(acm_sum)+1)
            else:
                c = abs(acm_sum)+1
        acm_sum += c
        ans += abs(c - _a[i])
        __a.append(c)
    res.append(ans)

print(min(res))

Submission status: Wrong Answer

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

ans = 0
_a = [a[0]]
prev_sign = 1
if a[0] < 0:
    prev_sign = -1
for i in range(1, N):
    c = a[i]
    if c >= 0 and prev_sign > 0:
        ans += abs(c - (-1))
        c = -1
    elif c <= 0 and prev_sign < 0:
        ans += abs(c - 1)
        c = 1
    if c > 0:
        prev_sign = 1
    else:
        prev_sign = -1
    _a.append(c)

__a = [_a[0]]
acm_sum = _a[0]
for i in range(1, N):
    c = _a[i]
    if abs(acm_sum) >= abs(c):
        if c < 0:
            c = -1*(abs(acm_sum)+1)
        else:
            c = abs(acm_sum)+1
    acm_sum += c
    ans += abs(c - _a[i])
    __a.append(c)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = input()
a = list(map(int, input().split()))
 
def f(x):
  cnt = 0
  cur = 0
  for ai in a:
    cur += ai
    cur *= x
    if cur <= 0:
      cnt += 1 - cur
      cur = 1
    x *= -1
  return cnt
 
print(min(f(1), f(-1)))

Submission status: Wrong Answer

Solution code:
import numpy

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

ans=0
sum=0
if a[0]==0:
  a[0]=1
  ans=1
  sum=1
  for j in a[1:]:
    if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0:
      ans+=abs(sum+j)+1
      sum=-numpy.sign(sum)
    else:
      sum+=j
  pans=ans

  a[0]=-1
  ans=1
  sum=-1
  for j in a[1:]:
    if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0:
      ans+=abs(sum+j)+1
      sum=-numpy.sign(sum)
    else:
      sum+=j
  mans=ans

  ans=min(pans,mans)

else:
  for j in a:
    if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0:
      ans+=abs(sum+j)+1
      sum=-numpy.sign(sum)
    else:
      sum+=j
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy

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

ans=0
sum=0
if a[0]==0:
  a[0]=1
  ans+=1
  sum=1
  for j in a[1:]:
    if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0:
      ans+=abs(sum+j)+1
      sum=-numpy.sign(sum)
    else:
      sum+=j
  pans=ans

  a[0]=-1
  ans+=1
  sum=-1
  for j in a[1:]:
    if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0:
      ans+=abs(sum+j)+1
      sum=-numpy.sign(sum)
    else:
      sum+=j
  mans=ans

  ans=min(pans,mans)

else:
  for j in a:
    if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0:
      ans+=abs(sum+j)+1
      sum=-numpy.sign(sum)
    else:
      sum+=j
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy

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

ans=0
sum=0
if a[0]==0:
  a[0]=1
  ans+=1
  for j in a:
    if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0:
      ans+=abs(sum+j)+1
      sum=-numpy.sign(sum)
    else:
      sum+=j
  pans=ans

  a[0]=-1
  ans+=1
  for j in a:
    if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0:
      ans+=abs(sum+j)+1
      sum=-numpy.sign(sum)
    else:
      sum+=j
  mans=ans

  ans=min(pans,mans)

else:
  for j in a:
    if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0:
      ans+=abs(sum+j)+1
      sum=-numpy.sign(sum)
    else:
      sum+=j
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy

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

ans=0
sum=0
for j in a:
  if numpy.sign(sum)==numpy.sign(sum+j) or numpy.sign(sum+j)==0:
    ans+=abs(sum+j)+1
    sum=-numpy.sign(sum)
  else:
    sum+=j
print(ans)

Submission status: Wrong Answer

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

tmp_sum = a[0]
cnt = 0
if a[0] >= 0:
    for i in range(1,n):
        tmp_sum += a[i]
        if i % 2 != 0:
            if tmp_sum >= 0:
                cnt += tmp_sum + 1
                tmp_sum -= tmp_sum + 1
        elif i % 2 == 0:
            if tmp_sum <= 0:
                cnt += abs(tmp_sum) + 1
                tmp_sum += abs(tmp_sum) + 1
elif a[0] < 0:
    for i in range(1,n):
        tmp_sum += a[i]
        if i % 2 != 0:
            if tmp_sum <= 0:
                cnt += abs(tmp_sum) + 1
                tmp_sum += abs(tmp_sum) + 1
        elif i % 2 == 0:
            if tmp_sum >= 0:
                cnt += tmp_sum + 1
                tmp_sum -= tmp_sum + 1

print(cnt)

Submission status: Wrong Answer

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

ruiseki = [0] * N
ans = 2 ** 30 + 1
for j in range(2):
    cnt= 0
    ruiseki[0] = A[0]
    if j == 0:
        if A[0] <= 0:
            ruiseki[0] = 1
            cnt += 1 - A[0]
    else:
        if A[0] >= 0:
            ruiseki[0] = -1
            cnt += 1 + A[0]
    for i in range(1,N):
        ruiseki[i] = ruiseki[i-1]+A[i]
        totalp = 0
        if ruiseki[i] * ruiseki[i-1] >0:
            totalp= (int(ruiseki[i])>0)*(-1)*(1 + ruiseki[i]) +  (int(ruiseki[i])<0) * (1 - ruiseki[i])

        elif ruiseki[i] == 0:
            if ruiseki[i-1] > 0:
                totalp = -1
            else:
                totalp = 1
        ruiseki[i] += totalp 
        cnt += abs(totalp)
    ans = min(ans,cnt)
        
print(ans)

Submission status: Wrong Answer

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

asum = [a[0]]
ansp,ansm,tempp,tempm = 0,0,0,0
for i in range(1,N):
    A = asum[i-1] + a[i]
    asum += [A]
    A1 = A + tempp
    if A1 == 0:
        ansp += 1
        tempp += ((i%2)*2-1)
    elif i%2==0 and A1 < 0:
        ansp += -A1 + 1
        tempp += -A1 + 1
    elif i%2!=0 and A1 > 0:
        ansp += A1 + 1
        tempp += -A1 - 1

    A2 = A + tempm
    if A2 == 0:
        ansm += 1
        tempm += -((i%2)*2-1)
    elif i%2!=0 and A2 < 0:
        ansm += -A2 + 1
        tempm += -A2 + 1
    elif i%2==0 and A2 > 0:
        ansm += A2 + 1
        tempm += -A2 - 1

print(min(ansp,ansm))

Submission status: Wrong Answer

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

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

    """考え方
    ruiseki[i] := i番目までの累積和

    ・ruiseki[i]のiが偶数番目を+にするパターン
    ・ruiseki[i]のiが偶数番目を-にするパターン

    の2通りを求め、小さい方を答えにする
    """

    # 偶数番目を+にするパターン
    ans1 = 0
    ruiseki = [0] * N
    ruiseki[0] = A[0]
    if ruiseki[0] == 0:
        ruiseki[0] = 1
        ans1 += 1

    for i in range(1, N):
        ruiseki[i] = ruiseki[i-1] + A[i]

        if i%2 != 0:
            # iが奇数
            if ruiseki[i] >= 0:
                diff = abs(ruiseki[i] - (-1))
                ans1 += diff
                ruiseki[i] = -1
        else:
            # iが偶数
            if ruiseki[i] <= 0:
                diff = abs(ruiseki[i] - 1)
                ans1 += diff
                ruiseki[i] = 1
    
    # 偶数番目を-にするパターン
    ans2 = 0
    ruiseki = [0] * N
    ruiseki[0] = A[0]
    if ruiseki[0] == 0:
        ruiseki[0] = -1
        ans2 += 1

    for i in range(1, N):
        ruiseki[i] = ruiseki[i-1] + A[i]

        if i%2 != 0:
            # iが奇数
            if ruiseki[i] <= 0:
                diff = abs(ruiseki[i] - 1)
                ans2 += diff
                ruiseki[i] = 1
        else:
            # iが偶数
            if ruiseki[i] >= 0:
                diff = abs(ruiseki[i] - (-1))
                ans2 += diff
                ruiseki[i] = -1
    
    print(min(ans1, ans2))


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

    ans = 0
    """
    (1) A = [0, 0, 0, 0, 0, 100, ...] みたいなときは
        A = [-1, 2, -2, 2, -2, 100, ...] にしたい
    (2) A = [0, 0, 0, 0, 100, ...] みたいなときは
        A = [1, -2, 2, -2, 100, ...] にしたい
    """
    if A[0] == 0:
        hugou = 1  # 最後の符号(1:+, -1:-)
        for i in range(1, N):
            if A[i] == 0:
                continue
            if A[i] > 0:
                hugou = 1
                break
            else:
                hugou = -1
                break
        
        for j in range(i-1, 0, -1):
            if hugou == -1:
                A[j] = 2
            else:
                A[j] = -2
            hugou *= -1
            ans += 2
        
        if hugou > 0:
            A[0] = -1
        else:
            A[0] = 1
        ans += 1

    # ruiseki[i] := i番目までの累積和
    ruiseki = [0] * N
    ruiseki[0] = A[0]
    for i in range(1, N):
        i_sum = A[i] + ruiseki[i-1]

        if ruiseki[i-1] > 0:
            # ruiseki[i]をマイナス値にする必要がある
            if i_sum < 0:
                ruiseki[i] = i_sum
            else:
                diff = abs(i_sum - (-1))
                ans += diff
                A[i] -= diff
                i_sum -= diff
                ruiseki[i] = i_sum
        elif ruiseki[i-1] < 0:
            # ruiseki[i]をプラス値にする必要がある
            if i_sum > 0:
                ruiseki[i] = i_sum
            else:
                diff = abs(i_sum - 1)
                ans += diff
                A[i] += diff
                i_sum += diff
                ruiseki[i] = i_sum
    
    print(ans)

        


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

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

    ans = 0
    """
    (1) A = [0, 0, 0, 0, 0, 100, ...] みたいなときは
        A = [-1, 2, -2, 2, -2, 100, ...] にしたい
    (2) A = [0, 0, 0, 0, 100, ...] みたいなときは
        A = [1, -2, 2, -2, 100, ...] にしたい
    """
    if A[0] == 0:
        hugou = 1  # 最後の符号(1:+, -1:-)
        for i in range(1, N):
            if A[i] == 0:
                continue
            if A[i] > 0:
                hugou = 1
                break
            else:
                hugou = -1
                break
        
        for j in range(i-1, 0, -1):
            if hugou == -1:
                A[j] = 2
            else:
                A[j] = -2
            hugou *= -1
            ans += 2
        
        if hugou > 0:
            A[0] = -1
        else:
            A[0] = 1
        ans += 1

    # ruiseki[i] := i番目までの累積和
    ruiseki = [0] * N
    ruiseki[0] = A[0]
    for i in range(1, N):
        i_sum = A[i] + ruiseki[i-1]

        if ruiseki[i-1] > 0:
            # ruiseki[i]をマイナス値にする必要がある
            if i_sum < 0:
                ruiseki[i] = i_sum
            else:
                diff = abs(i_sum - (-1))
                ans += diff
                A[i] -= diff
                i_sum -= diff
                ruiseki[i] = i_sum
        elif ruiseki[i-1] < 0:
            # ruiseki[i]をプラス値にする必要がある
            if i_sum > 0:
                ruiseki[i] = i_sum
            else:
                diff = abs(i_sum - 1)
                ans += diff
                A[i] += diff
                i_sum += diff
                ruiseki[i] = i_sum
    
    print(ans)

        


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

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

    # dp[i] := i番目までの最小の操作回数
    dp = [None] * N
    dp[0] = 0

    ans = 0
    # A = [0 0 0 0 0 100 ...] みたいなときは
    # A = [-1 2 -2 2 -2 100 ...] にしたい
    if A[0] == 0:
        hugou = 1
        for i in range(1, N):
            if A[i] == 0:
                continue
            if A[i] > 0:
                hugou = 1
                break
            else:
                hugou = -1
                break
        
        for j in range(i, 0, -1):
            if hugou == -1:
                A[j] = 2
            else:
                A[j] = -2
            hugou *= -1
            ans += 2
        
        if hugou > 0:
            A[0] = -1
        else:
            A[0] = 1
        ans += 1

    # ruiseki[i] := i番目までの累積和
    ruiseki = [0] * N
    ruiseki[0] = A[0]
    for i in range(1, N):
        total = A[i] + ruiseki[i-1]
        if ruiseki[i-1] > 0:
            # ruiseki[i]をマイナス値にする必要がある
            if total < 0:
                ruiseki[i] = total
            else:
                diff = abs(total - (-1))
                ans += diff
                A[i] -= diff
                total -= diff
                ruiseki[i] = total
        else:
            # ruiseki[i]をプラス値にする必要がある
            if total > 0:
                ruiseki[i] = total
            else:
                diff = abs(total - 1)
                ans += diff
                A[i] += diff
                total += diff
                ruiseki[i] = total
    
    print(ans)

        


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import numpy as np
n=int(input())
a=list(map(int,input().split()))
r=[0]
for i in range(n):
    r.append(r[i]+a[i])
r.pop(0)
q=[r[i] for i in range(n)]

pm=[1-2*(i%2) for i in range(n)]
mp=[1-2*((i+1)%2) for i in range(n)]

sum1,sum2=0,0
sousa1,sousa2=0,0

for i in range(n):
    if np.sign(r[i]+sousa1) != pm[i]:
        sum1+=abs(pm[i]-r[i]-sousa1)
        sousa1=pm[i]-r[i]
        
for i in range(n):
    if np.sign(q[i]+sousa1) != mp[i]:
        sum2+=abs(mp[i]-q[i]-sousa2)
        sousa2=mp[i]-q[i]

print(min(sum1,sum2))

Submission status: Wrong Answer

Solution code:
import numpy as np
n=int(input())
a=list(map(int,input().split()))
r=[0]
for i in range(n):
    r.append(r[i]+a[i])
r.pop(0)
print(r)
q=[r[i] for i in range(n)]

pm=[1-2*(i%2) for i in range(n)]
mp=[1-2*((i+1)%2) for i in range(n)]

sum1,sum2=0,0
sousa1,sousa2=0,0

for i in range(n):
    if np.sign(r[i]) != pm[i]:
        sum1+=abs(pm[i]-r[i])
        sousa1=pm[i]-r[i]
        for j in range(n-i-1):
            r[i+j+1]=r[i+j+1]+sousa1
        
for i in range(n):
    if np.sign(q[i]) != mp[i]:
        sum2+=abs(mp[i]-q[i])
        sousa2=mp[i]-q[i]
        for j in range(n-i-1):
            q[i+j+1]=q[i+j+1]+sousa2
        
print(min(sum1,sum2))

Submission status: Wrong Answer

Solution code:
import numpy as np
n=int(input())
a=list(map(int,input().split()))
r=[0]
for i in range(n):
    r.append(r[i]+a[i])
r.pop(0)

pm=[1-2*(i%2) for i in range(n)]
mp=[1-2*((i+1)%2) for i in range(n)]

sum1,sum2=0,0
sousa1,sousa2=0,0

for i in range(n):
    if np.sign(r[i]+sousa1) != pm[i]:
        sum1+=abs(pm[i]-r[i]-sousa1)
        sousa1=pm[i]-r[i]-sousa1
        
for i in range(n):
    if np.sign(r[i]+sousa2) != mp[i]:
        sum2+=abs(mp[i]-r[i]-sousa2)
        sousa2=mp[i]-r[i]-sousa2
        
print(min(sum1,sum2))

Submission status: Wrong Answer

Solution code:
import numpy as np
n=int(input())
a=list(map(int,input().split()))
r=[0]
for i in range(n):
    r.append(r[i]+a[i])
r.pop(0)

pm=[1-2*(i%2) for i in range(n)]
mp=[1-2*((i+1)%2) for i in range(n)]

sum1,sum2=0,0
sousa1,sousa2=0,0

for i in range(n):
    if np.sign(r[i]+sousa1) != pm[i]:
        sum1+=abs(pm[i]-r[i]-sousa1)
        sousa1+=1-2*(i%2)-r[i]
        
for i in range(n):
    if np.sign(r[i]+sousa2) != mp[i]:
        sum2+=abs(mp[i]-r[i]-sousa2)
        sousa2+=1-2*((i+1)%2)-r[i]
        
print(min(sum1,sum2))

Submission status: Wrong Answer

Solution code:
import numpy as np
n=int(input())
a=list(map(int,input().split()))
r=[0]
for i in range(n):
    r.append(r[i]+a[i])
r.pop(0)
print(r)

pm=[1-2*(i%2) for i in range(n)]
mp=[1-2*((i+1)%2) for i in range(n)]
print(pm)
print(mp)

sum1,sum2=0,0
sousa1,sousa2=0,0

for i in range(n):
    if np.sign(r[i]+sousa1) != pm[i]:
        sum1+=abs(pm[i]-r[i]-sousa1)
        sousa1+=1-2*(i%2)-r[i]
        
for i in range(n):
    if np.sign(r[i]+sousa2) != mp[i]:
        sum2+=abs(mp[i]-r[i]-sousa2)
        sousa2+=1-2*((i+1)%2)-r[i]
        
print(min(sum1,sum2))

Submission status: Wrong Answer

Solution code:
import numpy as np
n=int(input())
a=list(map(int,input().split()))
r=[0]
for i in range(n):
    r.append(r[i]+a[i])
r.pop(0)

sum1,sum2=0,0
sousa1,sousa2=0,0

for i in range(n):
    if np.sign(r[i]+sousa1) != 1-2*(i%2):
        sum1+=abs(1-2*(i%2)-r[i]-sousa1)
        sousa1+=1-2*(i%2)-r[i]
        
for i in range(n):
    if np.sign(r[i]+sousa2) != 1-2*((i+1)%2):
        sum2+=abs(1-2*((i+1)%2)-r[i]-sousa2)
        sousa2+=1-2*((i+1)%2)-r[i]
        
print(min(sum1,sum2))

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())
A = [int(x) for x in input().split()]
sum_start_plus = 0
sum_start_minus = 0
ans_start_plus = 0
ans_start_minus = 0

# プラス始まりの場合を計算する
for i in range(n):
    sum_start_plus += A[i]
    if i % 2 == 0:
        if np.sign(sum_start_plus) == 1:
            continue
        else:
            ans_start_plus += abs(1 - sum_start_plus)
            sum_start_plus = 1
    else:
        if np.sign(sum_start_plus) == -1:
            continue
        else:
            ans_start_plus += abs(-1 - sum_start_plus)
            sum_start_plus = -1

# マイナス始まりの場合を計算する
for i in range(n):
    sum_start_minus += A[i]
    print(sum_start_minus)
    if i % 2 == 0:
        if np.sign(sum_start_minus) == -1:
            continue
        else:
            ans_start_minus += abs(-1 - sum_start_minus)
            sum_start_minus = -1
    else:
        if np.sign(sum_start_minus) == 1:
            continue
        else:
            ans_start_minus += abs(1 - sum_start_minus)
            sum_start_minus = 1

print(min(ans_start_plus, ans_start_minus))

Submission status: Wrong Answer

Solution code:
n=int(input())
arr=list(map(int,input().split()))
ans1=0
ans2=0
if arr[0]>0:
  lsum=arr[0]
else:
  lsum=1
  ans1+=1-arr[0]
for i in range(1,n):
  if i%2==1:
    if arr[i]<-lsum:
      lsum+=arr[i]
    else:
      ans1+=(abs(lsum)+1)+arr[i]
      lsum=-1
  else:
    if abs(lsum)<arr[i]:
      lsum+=arr[i]
    else:
      ans1+=(abs(lsum)+1)-arr[i]
      lsum=1
if arr[0]<0:
  lsum=arr[0]
else:
  ans2+=arr[0]+1
  lsum=-1
for i in range(1,n):
  if i%2==1:
    if arr[i]<-lsum:
      lsum+=arr[i]
    else:
      ans2+=(abs(lsum)+1)+arr[i]
      lsum=-1
  else:
    if abs(lsum)<arr[i]:
      lsum+=arr[i]
    else:
      ans2+=(abs(lsum)+1)-arr[i]
      lsum=1
ans=min(ans1,ans2)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
arr=list(map(int,input().split()))
ans=0
lsum=arr[0]
if arr[0]>0:
  for i in range(1,n):
    if i%2==1:
      if arr[i]<-lsum:
        lsum+=arr[i]
      else:
        ans+=(abs(lsum)+1)+arr[i]
        lsum=-1
    else:
      if abs(lsum)<arr[i]:
        lsum+=arr[i]
      else:
        ans+=(abs(lsum)+1)-arr[i]
        lsum=1
elif arr[0]<0:
  for i in range(1,n):
    if i%2==0:
      if arr[i]<-lsum:
        lsum+=arr[i]
      else:
        ans+=(abs(lsum)+1)+arr[i]
        lsum=-1
    else:
      if abs(lsum)<arr[i]:
        lsum+=arr[i]
      else:
        ans+=(abs(lsum)+1)-arr[i]
        lsum=1
else:
  ans1=1
  ans2=1
  lsum=1
  for i in range(1,n):
    if i%2==1:
      if arr[i]<-lsum:
        lsum+=arr[i]
      else:
        ans1+=(abs(lsum)+1)+arr[i]
        lsum=-1
    else:
      if abs(lsum)<arr[i]:
        lsum+=arr[i]
      else:
        ans1+=(abs(lsum)+1)-arr[i]
        lsum=1
  lsum=-1
  for i in range(1,n):
    if i%2==1:
      if arr[i]<-lsum:
        lsum+=arr[i]
      else:
        ans2+=(abs(lsum)+1)+arr[i]
        lsum=-1
    else:
      if abs(lsum)<arr[i]:
        lsum+=arr[i]
      else:
        ans2+=(abs(lsum)+1)-arr[i]
        lsum=1
  ans=min(ans1,ans2)
print(ans)

Submission status: Wrong Answer

Solution code:
a = list(map(int, input().split()))
t1, s1 = 0, 0
for i, x in enumerate(a):
    s1 += x
    if i % 2 == 0:
        if s1 < 1:
            t1 += (1-s1)
            s1 = 1
    else:
        if s1 > -1:
            t1 += (s1+1)
            s1 = -1
t2, s2 = 0, 0
for i, x in enumerate(a):
    s2 += x
    if i % 2 == 0:
        if s2 > -1:
            t2 += (s2+1)
            s2 = -1
    else:
        if s2 < 1:
            t2 += (1-s2)
            s2 = 1
print(min(t1, t2))

Submission status: Wrong Answer

Solution code:
def sequence(lis, first_value):
    count = 0
    now_sum = first_value
    pre_sum = first_value
    for num in lis[1:]:
        now_sum += num
        if now_sum == 0 or pre_sum * (now_sum) > 0:
            count += int(abs(now_sum) + 1)
            now_sum = -pre_sum/abs(pre_sum)
        pre_sum = now_sum
    return count

n = int(input())
a = [int(i) for i in input().split()]
if a[0] == 0:
    print(min(sequence(a, 1), sequence(a, -1))+1)
else:
    print(sequence(a, a[0]))

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7

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

def solve(a):

    if a[0] > 0:
        status_pos = True
    else:
        status_pos = False

    tmp = 0
    ans = 0
    for i in range(n):
        tmp += a[i]
        if tmp == 0:
            ans += 1
            if status_pos:
                tmp = -1
            else:
                tmp = 1

        elif status_pos and tmp < 0:
            ans += 1+abs(tmp)
            tmp = 1
        elif status_pos == False and tmp > 0:
            ans += 1+abs(tmp)
            tmp = -1
        status_pos = not(status_pos)
    return ans

ans = solve(a)

ans2 = abs(a[0])+1
if a[0] > 0:
    a[0] = -1
else:
    a[0] = 1
ans2 += solve(a)
print(min(ans,ans2))

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7

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


if a[0] > 0:
    status_pos = True
else:
    status_pos = False

tmp = 0
ans = 0
for i in range(n):
    tmp += a[i]
    if tmp == 0:
        ans += 1
        if status_pos:
            tmp = -1
        else:
            tmp = 1

    elif status_pos and tmp < 0:
        ans += 1+abs(tmp)
        tmp = 1
    elif status_pos == False and tmp > 0:
        ans += 1+abs(tmp)
        tmp = -1
    status_pos = not(status_pos)

print(ans)
# ans2 = abs(a[0])+1
# if a[0] > 0:
#     a[0] = -1
# else:
#     a[0] = 1
#
# tmp = a[0]
# if a[0] > 0:
#     status_pos = True
# else:
#     status_pos = False
# for i in range(1,n):
#     tmp += a[i]
#     if status_pos and tmp <= 0:
#         ans2 += 1-tmp
#         tmp = 1
#     elif status_pos == False and tmp >= 0:
#         ans2 += 1+tmp
#         tmp = -1
#     status_pos = not(status_pos)
#
# print(min(ans,ans2))

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7

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

sums = [0 for i in range(n)]

tmp = 0
if a[0] > 0:
    status_pos = True
else:
    status_pos = False

ans = 0
for i in range(n):
    tmp += a[i]
    if status_pos and tmp <= 0:
        ans += 1-tmp
        tmp = 1
    elif status_pos == False and tmp >= 0:
        ans += 1+tmp
        tmp = -1
    status_pos = not(status_pos)

print(ans)
# ans2 = abs(a[0])+1
# if a[0] > 0:
#     a[0] = -1
# else:
#     a[0] = 1
#
# tmp = a[0]
# if a[0] > 0:
#     status_pos = True
# else:
#     status_pos = False
# for i in range(1,n):
#     tmp += a[i]
#     if status_pos and tmp <= 0:
#         ans2 += 1-tmp
#         tmp = 1
#     elif status_pos == False and tmp >= 0:
#         ans2 += 1+tmp
#         tmp = -1
#     status_pos = not(status_pos)
#
# print(min(ans,ans2))

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math

mod = 10**9+7

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

sums = [0 for i in range(n)]

tmp = 0
if a[0] > 0:
    status_pos = True
else:
    status_pos = False

ans = 0
for i in range(n):
    tmp += a[i]
    if status_pos and tmp <= 0:
        ans += 1-tmp
        tmp = 1
    elif status_pos == False and tmp >= 0:
        ans += 1+tmp
        tmp = -1
    status_pos = not(status_pos)

ans2 = abs(a[0])+1
if a[0] > 0:
    a[0] = -1
else:
    a[0] = 1

tmp = a[0]
if a[0] > 0:
    status_pos = True
else:
    status_pos = False
for i in range(1,n):
    tmp += a[i]
    if status_pos and tmp <= 0:
        ans2 += 1-tmp
        tmp = 1
    elif status_pos == False and tmp >= 0:
        ans2 += 1+tmp
        tmp = -1
    status_pos = not(status_pos)

print(min(ans,ans2))
"""
1 -2 2 -2
1 -1 1 1
"""

Submission status: Wrong Answer

Solution code:
N = int(input())
*A, = map(int,input().split())
ans1 = 0
S = A[0]
if S <= 0:
    S = 1
    ans1 = abs(S)+1
for a in A[1:]:
    S1 = S+a
    if S1*S >= 0:
        ans1 += abs(S1)+1
        S1 = -S//abs(S)
    S = S1
ans2 = 0
S = A[0]
if S >= 0:
    S = -1
    ans2 = abs(S)+1
for a in A[1:]:
    S1 = S+a
    if S1*S >= 0:
        ans2 += abs(S1)+1
        S1 = -S//abs(S)
    S = S1
print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
N = int(input())
*A, = map(int,input().split())
ans1 = 0
S = A[0]
if S <= 0:
    S = 1
    ans1 = -S+1
for a in A[1:]:
    S1 = S+a
    if S1*S >= 0:
        ans1 += abs(S1)+1
        S1 = -S//abs(S)
    S = S1
ans2 = 0
S = A[0]
if S >= 0:
    S = -1
    ans1 = -S+1
for a in A[1:]:
    S1 = S+a
    if S1*S >= 0:
        ans2 += abs(S1)+1
        S1 = -S//abs(S)
    S = S1
print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
R = [0] * n
R[0] = A[0]
for i in range(1, n):
  R[i] = R[i-1] + A[i]

# search
def solve(r, inc=0):
  ans = 0
  is_plus = (r[0] > 0)
  for i in range(1, n):
    if (is_plus and r[i]+inc >= 0) or (not is_plus and r[i]+inc <= 0):
      ans += abs(r[i]+inc) + 1
      if (r[i]+inc) > 0:
        inc -= abs(r[i]+inc) + 1
      else:
        inc += abs(r[i]+inc) + 1
    is_plus = (r[i]+inc > 0)
  return ans

# normal
ret = solve(R, 0)
#print(R, ret)

# modify 
is_plus = (R[0] > 0)
ans0 = abs(R[0]) + 1
if is_plus:
  for i in range(n):
    R[i] -= abs(R[0]) + 1
else:
  for i in range(n):
    R[i] += abs(R[0]) + 1
ret2 = ans0 + solve(R, 0)
#print(R, ret2)

print(min(ret, ret2))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
R = [0] * n
R[0] = A[0]
for i in range(1, n):
  R[i] = R[i-1] + A[i]

# search
def solve(r, inc=0):
  ans = 0
  is_plus = (r[0] > 0)
  for i in range(1, n):
    if (is_plus and r[i]+inc < 0) or (not is_plus and r[i]+inc > 0):
      pass
    else:
      ans += abs(r[i]+inc) + 1
      if is_plus:
        inc -= abs(r[i]+inc) + 1
      else:
        inc += abs(r[i]+inc) + 1
    is_plus = (r[i]+inc > 0)
  return ans

# normal
ret = solve(R, 0)

# modify 
is_plus = (R[0] > 0)
ans0 = abs(R[0]) + 1
if is_plus:
  for i in range(n):
    R[i] -= abs(R[0]) + 1
else:
  for i in range(n):
    R[i] += abs(R[0]) + 1

print(min(ret, ans0 + solve(R, 0)))

Submission status: Wrong Answer

Solution code:
n = int(input())
A = list(map(int, input().split()))
R = [0] * n
R[0] = A[0]
for i in range(1, n):
  R[i] = R[i-1] + A[i]


def solve(r, inc=0):
  ans = 0
  # search
  is_plus = (r[0] > 0)
  for i in range(1, n):
    if (is_plus and r[i]+inc < 0) or (not is_plus and r[i]+inc > 0):
      pass
    else:
      ans += abs(r[i]+inc) + 1
      if is_plus:
        inc -= abs(r[i]+inc) + 1
      else:
        inc += abs(r[i]+inc) + 1
    is_plus = (r[i]+inc > 0)
  return ans


ret = solve(R, 0)

# modify 
is_plus = (R[0] > 0)
ans0 = abs(R[0]) + 1
if is_plus:
  for i in range(n):
    R[i] -= abs(R[0]) + 1
  else:
    for i in range(n):
      R[i] += abs(R[0]) + 1
print(min(ret, ans0 + solve(R, 0)))

Submission status: Wrong Answer

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

trial_num = int(input())
number_lists = list(map(int, input().split()))

def main():
    sum_num = 0
    change_num = 0

    if(number_lists[0] < 0):
        for i in range(trial_num):
            number_lists[i] = -1 * number_lists[i]
    elif(number_lists[0] == 0):
        number_lists[0] = 1
        change_num += 1

    for i in range(trial_num):

        if(i == 0):
            sum_num = number_lists[0]
        else:
           if(i%2 == 1):
               if(sum_num + number_lists[i] < 0):
                    sum_num += number_lists[i]
               else:
                   change_num += abs(sum_num + number_lists[i]) + 1
                   sum_num = -1
           else:
                if (sum_num + number_lists[i] > 0):
                    sum_num += number_lists[i]
                else:
                    change_num += abs(sum_num + number_lists[i]) + 1
                    sum_num = 1


    print(change_num)




if __name__ == "__main__":
    main()