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: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    n = int(input())
    A = list(map(int, input().split()))
    def Chk(a, pos):
        cnt = 0
        tmp = 0

        for a in A:
            tmp += a

            if pos and tmp < 1:
                cnt += 1 - tmp
                tmp = 1
            elif not pos and tmp > -1:
                cnt += 1 + tmp
                tmp = -1

            pos = not pos
        return cnt

    print(min(Chk(A, True), Chk(A, False)))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

def Chk(a, pos):
    cnt = 0
    tmp = 0

    for a in A:
        tmp += a

        if pos and tmp < 1:
            cnt += 1 - tmp
            tmp = 1
        elif not pos and tmp > -1:
            cnt += 1 + tmp
            tmp = -1

        pos = not pos
    return cnt

print(min(Chk(A, True), Chk(A, False)))

Submission status: Accepted

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

kouho=[]

#奇数番目までの和を0以上にする場合
ans=0
cumsum=0
for i in range(n):
  cumsum+=a[i]
  if i%2:
    if cumsum<=0:
      ans+=abs(0-cumsum)+1
      cumsum=1
  elif not i%2:
    if cumsum>=0:
      ans+=(cumsum+1)
      cumsum=-1
kouho.append(ans)

ans=0
cumsum=0

#偶数番目の累積和を0より大きくしたい場合
for i in range(n):
  cumsum+=a[i]
  if not i%2:
    if cumsum<=0:
      ans+=abs(0-cumsum)+1
      cumsum=1
  elif i%2:
    if cumsum>=0:
      ans+=(cumsum+1)
      cumsum=-1
kouho.append(ans)
print(min(kouho))

Submission status: Accepted

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

# a_1 > 0 の場合を試す
res, sum = 0, 0
if 0 < A[0]: sum = A[0]
else: res, sum = -A[0] + 1, 1

for i in range(1, N):
    if 0 < sum:
        if -1 < sum + A[i]: # a_i を足して、符号が変わらない
            res += abs(A[i] + sum + 1)
            sum = -1
        else: # a_i を足して符号が変わる
            sum += A[i]
    else:
        if sum + A[i] < 1:
            res += abs(A[i] - (-sum + 1))
            sum = 1
        else:
            sum += A[i]

ans = res

# a_1 < 0 の場合を試す
res, sum = 0, 0
if A[0] < 0: sum = A[0]
else: res, sum = A[0] + 1, -1

for i in range(1, N):
    if 0 < sum:
        if -1 < sum + A[i]: # a_i を足して、符号が変わらない
            res += abs(A[i] + sum + 1)
            sum = -1
        else: # a_i を足して符号が変わる
            sum += A[i]
    else:
        if sum + A[i] < 1:
            res += abs(A[i] - (-sum + 1))
            sum = 1
        else:
            sum += A[i]

ans = min(ans, res)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
A = list(map(int, input().split(' ')))

even_ans, odd_ans = 0, 0

s = 0
for i, a in enumerate(A):
    s += a

    if i % 2 == 0:
        if s > 0:
            pass
        else:
            even_ans += 1 - s
            s = 1
    else:
        if s < 0:
            pass
        else:
            pass
            even_ans += s + 1
            s = -1



s = 0
for i, a in enumerate(A):
    s += a

    if i % 2 == 1:
        if s > 0:
            pass
        else:
            odd_ans += 1 - s
            s = 1
    else:
        if s < 0:
            pass
        else:
            pass
            odd_ans += s + 1
            s = -1


print(min(even_ans, odd_ans))

Submission status: Accepted

Solution code:
import copy
n=int(input())
a1=[int(i) for i in input().split()]
a2=copy.deepcopy(a1)
for i in range(1,n):
    a1[i]+=a1[i-1]
for i in range(1,n):
    a2[i]+=a2[i-1]

pm1,c1=0,0
pm2,c2=0,0

for i in range(n):
    a1[i]+=pm1
    if i%2==1:
        if a1[i]>=0:
            pm1-=(a1[i]+1)
            c1+=(a1[i]+1)
    else:
        if a1[i]<=0:
            pm1+=(-a1[i]+1)
            c1+=(-a1[i]+1)

for i in range(n):
    a2[i]+=pm2
    if i%2==0:
        if a2[i]>=0:
            pm2-=(a2[i]+1)
            c2+=(a2[i]+1)
    else:
        if a2[i]<=0:
            pm2+=(-a2[i]+1)
            c2+=(-a2[i]+1)

#print(c1)
#print(c2)
print(min(c1,c2))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
B = [A[0]]
for i in range(1,N):
    B.append(B[-1]+A[i])
#plus_case
plus_ans = 0
c = 0
for i in range(N):
    current_value = B[i]+c
    if i%2 == 0:
        if current_value <= 0:
            plus_ans += abs(current_value-1)
            c -= current_value-1
    else:
        if current_value >= 0:
            plus_ans += abs(current_value+1)
            c -= current_value+1
#minus_case
minus_ans = 0
c = 0
for i in range(N):
    current_value = B[i]+c
    if i%2 == 0:
        if current_value >= 0:
            minus_ans += abs(current_value+1)
            c -= current_value+1
    else:
        if current_value <= 0:
            minus_ans += abs(current_value-1)
            c -= current_value-1
print(min(plus_ans,minus_ans))

Submission status: Accepted

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

sum_a = 0
ans_1 = 0
for i in range(N):
    sum_a += a[i]
    if sum_a >= 0 and i%2 == 0:
        ans_1 += sum_a + 1
        sum_a = -1
    elif sum_a <= 0 and i%2 != 0:
        ans_1 += -sum_a + 1
        sum_a = 1
        
sum_a = 0
ans_2 = 0
for i in range(N):
    sum_a += a[i]
    if sum_a >= 0 and i%2 != 0:
        ans_2 += sum_a + 1
        sum_a = -1
    elif sum_a <= 0 and i%2 == 0:
        ans_2 += -sum_a + 1
        sum_a = 1

print(min(ans_1, ans_2))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
ans=[0]*2
for pat in range(2):
  cur=0
  isplus=pat
  for i in range(N):
    cur+=A[i]
    if isplus:
      if cur<=0:
        ans[pat]+=abs(cur-1)
        cur=1
    else:
      if cur>=0:
        ans[pat]+=abs(cur-(-1))
        cur=-1
    isplus^=1

print(min(ans))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
res=10**15
for isplus in (True,False):
  cur=0
  ans=0
  for i in range(N):
    cur+=A[i]
    if isplus:
      if cur<=0:
        ans+=abs(cur-1)
        cur=1
      isplus=False
    else:
      if cur>=0:
        ans+=abs(cur-(-1))
        cur=-1
      isplus=True
  if res>ans:
    res=ans

print(res)

Submission status: Accepted

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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = LIST()

def check(A, mod2):
    cur = res = 0
    for i in range(N):
        cur += A[i]
        if i % 2 == mod2:
            # 0以下なら1にする
            if cur <= 0:
                # cur + x = 1 → x = 1 - cur
                A[i] += 1 - cur
                res += 1 - cur
                cur += 1 - cur
        else:
            # 0以上なら-1にする
            if cur >= 0:
                # cur - x = -1 → x = 1 + cur
                A[i] -= 1 + cur
                res += 1 + cur
                cur -= 1 + cur
    return res
    
ans = min(check(A[:], 0), check(A[:], 1))
print(ans)

Submission status: Accepted

Solution code:
# ABC059 C - Sequence
n=int(input())
a=list(map(int,input().split()))

total=a[0]
ans=0
if total<=0:
    ans=1-total
    total=1

for aa in a[1:]:
    if total>0:
        y=total+aa
        if y<0:
            total=y
        else:
            ans+=y+1
            total=-1
    else:
        y=total+aa
        if y>0:
            total=y
        else:
            ans+=1-y
            total=1


total=a[0]
ans2=0
if total>=0:
    ans2+=total+1
    total=-1

for aa in a[1:]:
    if total>0:
        y=total+aa
        if y<0:
            total=y
        else:
            ans2+=y+1
            total=-1
    else:
        y=total+aa
        if y>0:
            total=y
        else:
            ans2+=1-y
            total=1

print(min(ans,ans2))

Submission status: Accepted

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

def check(flag):
    acc = 0
    cnt = 0

    flag = flag
    for i in a:
        acc += i
        if (flag == 1 and flag > acc) or (flag == -1 and flag < acc) or i == 0:
            cnt += abs(flag - acc)
            acc = flag
        flag *= -1
    return cnt

print(min(check(1), check(-1)))

Submission status: Accepted

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

def f(presum, x, fugo):
  if (presum+x)*fugo<=0:
    sa=fugo-presum-x
    prosum=fugo
  else:
    sa=0
    prosum=presum+x
  return sa, prosum

out1=0
y=0
for i in range(n):
  x, y=f(y, a[i], (-1)**i)
  out1+=abs(x)
  
out2=0
y=0
for i in range(n):
  x, y=f(y, a[i], -(-1)**i)
  out2+=abs(x)
  
print(min(out1, out2))

Submission status: Accepted

Solution code:
def F(list_A, sign):
  total = 0
  cnt = 0
  for a in list_A:
    total += a
    if sign==True and total < 1:
      cnt += 1- total
      total = 1
    elif sign==False and total > -1:
      cnt += total + 1
      total = -1
    
    sign = not sign
  return cnt

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

ans = min(F(A, True), F(A, False))
print(ans)

Submission status: Accepted

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

ans = 10**15
for i in range(2):
    c = 0
    S = 0
    for j in range(n):
        S += A[j]
        if i % 2 == j % 2 and S <= 0:
            c += abs(S)+1
            S = 1
        elif i % 2 != j % 2 and S >= 0:
            c += S+1
            S = -1
    ans = min(ans, c)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A = [int(_) for _ in input().split()]


def f(sign):
    ans = 0
    s = 0
    for i in range(N):
        a = A[i]
        s += a
        if s == 0:
            ans += 1
            s = sign
        else:
            a_sign = s // abs(s)
            if a_sign != sign:
                ans += abs(s) + 1
                s = sign
        sign *= -1
    return ans


print(min(f(1), f(-1)))

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    ans = 0
    s = a[0]
    if s <= 0:
        ans += 1-a[0]
        s = 1
    f = 1
    for i in a[1:]:
        s += i
        if f and (s >= 0):
            ans += s+1
            s = -1
        if not f and (s <= 0):
            ans += 1-s
            s = 1
        f ^= 1
    m = ans
    ans = 0
    s = a[0]
    if s >= 0:
        ans += a[0]+1
        s = -1
    f = 0
    for i in a[1:]:
        s += i
        if f and (s >= 0):
            ans += s+1
            s = -1
        if not f and (s <= 0):
            ans += 1-s
            s = 1
        f ^= 1
    print(min(m,ans))
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
def pm(input_list):
    calc_result = [0]
    cnt = 0
    
    for i in range(n):
        if i % 2 == 0: plus = True
        else: plus = False

        x = input_list[i] + calc_result[i]
        if plus:
            if x > 0:
                calc_result.append(x)
            
            else:
                cnt += 1 - x
                calc_result.append(1)

        else:
            if x < 0:
                calc_result.append(x)
            
            else:
                cnt += abs(-1 - x)
                calc_result.append(-1)

    return cnt

def mp(input_list):
    calc_result = [0]
    cnt = 0
    
    for i in range(n):
        if i % 2 == 0: minus = True
        else: minus = False

        x = input_list[i] + calc_result[i]
        if minus:
            if x < 0:
                calc_result.append(x)
            
            else:
                cnt += abs(-1 - x)
                calc_result.append(-1)

        else:
            if x > 0:
                calc_result.append(x)
            
            else:
                cnt += 1 - x
                calc_result.append(1)

    return cnt


n = int(input())
input_list = list(map(int, input().split()))

print(min(pm(input_list), mp(input_list)))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

# i番目までの和が正(i: 奇数)
res1 = 0
cum = 0
for i in range(n):
    cum += a[i]
    if i % 2 == 1:
        if cum <= 0:
            res1 += (1 - cum)
            cum = 1
    else:
        if cum >= 0:
            res1 += (cum + 1)
            cum = -1

# i番目までの和が正(i: 偶数)
res2 = 0
cum = 0
for i in range(n):
    cum += a[i]
    if i % 2 == 0:
        if cum <= 0:
            res2 += (1 - cum)
            cum = 1
    else:
        if cum >= 0:
            res2 += (cum + 1)
            cum = -1

print(min(res1,res2))

Submission status: Accepted

Solution code:

N = int(input())

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

# +先行の場合
ans_plus = 0
total = 0
for i in range(N):
    total += A[i]
    # 奇数個目（合計が正を期待）
    if i % 2 == 0:
        if total <= 0:
            ans_plus += (1 - total)
            total = 1
    # 偶数個目（合計が負を期待）
    else:
        if total >= 0:
            ans_plus += (total + 1)
            total = -1

ans_minus = 0
total = 0
for i in range(N):
    total += A[i]
    # 奇数個目（合計が負を期待）
    if i % 2 == 0:
        if total >= 0:
            ans_minus += (1 + total)
            total = -1
    # 偶数個目（合計が正を期待）
    else:
        if total <= 0:
            ans_minus += (-total + 1)
            total = 1

print(min(ans_plus, ans_minus))

Submission status: Accepted

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

count = 0
s = 0
  
# print(L)
  
loopnum = n//2

#+-+-...

for i in range(loopnum):
  s = s + L[2*i]
  if s <= 0:
    subt = -s + 1
    count += subt
    s = s + subt
  # print("s, count = {0}, {1}".format(s, count))
  s = s + L[2*i+1]
  if s >= 0:
    subt = s + 1 
    count += subt
    s = s - subt
  # print("s, count = {0}, {1}".format(s, count))
    
if n%2 == 1:
  s = s + L[-1]
  if s <= 0:
    subt = -s + 1
    count += subt

cand1 = count
# print(cand1)

count = 0
s = 0

#-+-+...

for i in range(loopnum):
  s = s + L[2*i]
  if s >= 0:
    subt = s + 1
    count += subt
    s = s - subt
  # print("s, count = {0}, {1}".format(s, count))
  s = s + L[2*i+1]
  if s <= 0:
    subt = -s + 1 
    count += subt
    s = s + subt
  # print("s, count = {0}, {1}".format(s, count))
    
if n%2 == 1:
  s = s + L[-1]
  if s >= 0:
    subt = s + 1
    count += subt

cand2 = count
# print(cand2)

print(min(cand1,cand2))

Submission status: Accepted

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

import itertools
al_ac = list(itertools.accumulate(al))

ans = 0
init = 1

fix = 0
for a in al_ac:
    a += fix
    if init == 1:
        if a >= 1:
            init *= -1
            continue
        else:
            ans += abs(init - a)
            fix += init - a
            init *= -1
    else:
        if a <= -1:
            init *= -1
            continue
        else:
            ans += abs(init - a)
            fix += init - a
            init *= -1
ans2 = 0
init = -1

fix = 0
for a in al_ac:
    a += fix
    if init == 1:
        if a >= 1:
            init *= -1
            continue
        else:
            ans2 += abs(init - a)
            fix += init - a
            init *= -1
    else:
        if a <= -1:
            init *= -1
            continue
        else:
            ans2 += abs(init - a)
            fix += init - a
            init *= -1

print(min(ans, ans2))

Submission status: Accepted

Solution code:
# coding: utf-8
# Here your code

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

result_1 = 0
before_sum = a[0]

if a[0] <= 0:
    before_sum = 1
    result_1 += 1 + abs(a[0])

after_sum = before_sum

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if i % 2 == 0:
        if after_sum <= 0:
            result_1 += 1 + abs(after_sum)
            after_sum = 1
    else:
        if after_sum >= 0:
            result_1 += 1 + abs(after_sum)
            after_sum = -1

result_2 = 0
before_sum = a[0]

if a[0] >= 0:
    before_sum = -1
    result_2 += 1 + abs(a[0])

after_sum = before_sum

for i in range(1,N):
    before_sum = after_sum
    after_sum = before_sum + a[i]
    if i % 2 == 1:
        if after_sum <= 0:
            result_2 += 1 + abs(after_sum)
            after_sum = 1
    else:
        if after_sum >= 0:
            result_2 += 1 + abs(after_sum)
            after_sum = -1

print(min(result_1,result_2))

Submission status: Accepted

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

def sig(v):
    if v >0:
        return "+"
    elif v<0:
        return "-"
    else:
        return "0"

c_total_sig="+"
c_total=0
cost1=0

for i in range(n):
    temp_total=c_total+a[i]
    temp_total_sig=sig(temp_total)
    #print(i,c_total,temp_total)
    plus=a[i]
    if c_total_sig==temp_total_sig:
        #print(i,c_total,temp_total)
        dif=abs(temp_total)+1
        cost1+=dif
        if temp_total_sig=="-":
            plus=a[i]+dif
        elif temp_total_sig=="+":
            plus=a[i]-dif
    if temp_total_sig=="0" and c_total_sig=="-":
        dif=abs(temp_total)+1
        cost1+=dif
        plus=a[i]+dif
    elif temp_total_sig=="0" and c_total_sig=="+":
        dif=abs(temp_total)+1
        cost1+=dif
        plus=a[i]-dif
    c_total+=plus
    c_total_sig=sig(c_total)

c_total_sig="-"
c_total=0
cost2=0
#print("---------------")
for i in range(n):
    temp_total=c_total+a[i]
    temp_total_sig=sig(temp_total)
    #print(i,c_total,temp_total)
    plus=a[i]
    if c_total_sig==temp_total_sig:
        #print(i,c_total,temp_total)
        dif=abs(temp_total)+1
        cost2+=dif
        if temp_total_sig=="-":
            plus=a[i]+dif
        elif temp_total_sig=="+":
            plus=a[i]-dif
    if temp_total_sig=="0" and c_total_sig=="-":
        dif=abs(temp_total)+1
        cost2+=dif
        plus=a[i]+dif
    elif temp_total_sig=="0" and c_total_sig=="+":
        dif=abs(temp_total)+1
        cost2+=dif
        plus=a[i]-dif
    c_total+=plus
    c_total_sig=sig(c_total)
print(min(cost1,cost2))

Submission status: Accepted

Solution code:
import itertools

n=int(input())
a = list(map(int, input().split()))
b = list(itertools.accumulate(a))

sum=0
count=0

sum1=0
count1=0

for i in range(n):
    if i%2==0:
        if b[i]+sum>0:
            continue
        else:
            count+=1-(b[i]+sum)
            sum+=1-(b[i]+sum)

    else:
        if b[i]+sum<0:
            continue
        else:
            count+=b[i]+sum+1
            sum-=b[i]+sum+1

for i in range(n):
    if i%2==0:
        if b[i]+sum1<0:
            continue
        else:
            count1+=b[i]+sum1+1
            sum1-=b[i]+sum1+1


    else:
        if b[i]+sum1>0:
            continue
        else:
            count1+=1-(b[i]+sum1)
            sum1+=1-(b[i]+sum1)

print(min(count,count1))

Submission status: Accepted

Solution code:
import sys

n, *a = map(int, sys.stdin.read().split())

def main():
    s1 = s2 = c1 = c2 = 0
    for i in range(n):
        s1 += a[i]
        s2 += a[i]
        if i & 1:
            if s1 >= 0:
                c1 += abs(-1 - s1)
                s1 = -1

            if s2 <= 0:
                c2 += 1 - s2
                s2 = 1
        else:
            if s1 <= 0:
                c1 += 1 - s1
                s1 = 1

            if s2 >= 0:
                c2 += abs(-1 - s2)
                s2 = -1
    
    return min(c1, c2)

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
a = list(map(int,input().split()))
def operate(ls,x):
    sm = 0
    ans = 0
    ans += abs(ls[0]-x)
    k = x
    for i in range(1,len(ls)):
        sm += k
        if sm > 0:
            if abs(ls[i]) <= sm or sm < ls[i]:
                ans += abs(ls[i]+sm+1)
                k = -sm-1
            else:
                k = ls[i]
        if sm < 0:
            if abs(ls[i]) <= abs(sm) or sm > ls[i]:
                ans += abs(-sm+1-ls[i])
                k = -sm+1
            else:
                k = ls[i]
    return ans

anstot = []
if a[0] > 0:
    inv = -1
if a[0] < 0:
    inv = 1
if a[0] != 0:
    anstot.append(operate(a,a[0]))
    anstot.append(operate(a,inv))
if a[0] == 0:
    a[0] = 1
    anstot.append(1+operate(a,1))
    a[0] = -1
    anstot.append(1+operate(a,-1))    
print(min(anstot))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf')
ans = 0 ;count = 0 ;pro = 1

n=int(input())
A=list(map(int,input().split()))
def solve(k,d=0):
    count=0
    S=A[0]
    if S*k>=0:
        count+=abs(S+k)
        S=-k
    for i in range(1,n):
        t=1 if S<0 else -1
        judge=S*(S+A[i])
        if judge<0:
            S+=A[i]
        elif judge==0:
            if i!=0:count+=1
            S=t
        else:
            count+=abs(A[i]+S)+1
            S=t
        if d:print(S,count)
    return count

print(min(solve(1),solve(-1)))
# print(solve(1,d=0),solve(-1,d=1))

Submission status: Accepted

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

ans1 = 0
ans2 = 0
cur1 = 0
cur2 = 0

for i in range(n):
    cun = a[i]
    # iが偶数のときプラス
    if i % 2 == 0:
        # プラスじゃないとき
        if a[i] + cur1 <= 0:
            ans1 += -(a[i] + cur1) + 1
            cun = -cur1 + 1
    # iが奇数のときマイナス
    else:
        # マイナスじゃないとき
        if a[i] + cur1 >= 0:
            ans1 += a[i] + cur1 + 1
            cun = -cur1 - 1
    cur1 += cun

for i in range(n):
    cun = a[i]
    # iが偶数のときマイナス
    if i % 2 == 0:
        # マイナスじゃないとき
        if a[i] + cur2 >= 0:
            ans2 += a[i] + cur2 + 1
            cun = -cur2 - 1
    # iが奇数のときプラス
    else:
        # プラスじゃないとき
        if a[i] + cur2 <= 0:
            ans2 += -(a[i] + cur2) + 1
            cun = -cur2 + 1
    cur2 += cun

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
#59c
n = int(input())
a_list = [int(e) for e in input().split()]

#print("#####################################")

first_minus_pettern_ans = 0
first_plus_pettern_ans = 0
first_minus_pettern_cumsum = 0
first_plus_pettern_cumsum = 0

for i in range(n): #n<10**5
    
    first_minus_pettern_cumsum += a_list[i]
    first_plus_pettern_cumsum += a_list[i]
    #print(i,":",a_list[i],"  ",first_minus_pettern_cumsum,"/",first_plus_pettern_cumsum)
    
    if i%2 == 0:
        if first_minus_pettern_cumsum >=0:
            #print("初項が負で奇数項なのに正!")
            first_minus_pettern_ans += abs(first_minus_pettern_cumsum) + 1
            first_minus_pettern_cumsum -= abs(first_minus_pettern_cumsum) + 1
        if first_plus_pettern_cumsum <=0:
            #print("初項が正で奇数項なのに負!")
            first_plus_pettern_ans += abs(first_plus_pettern_cumsum) + 1
            first_plus_pettern_cumsum += abs(first_plus_pettern_cumsum) + 1
        
    if i%2 == 1:
        if first_plus_pettern_cumsum >=0:
            #print("初項が正で偶数項なのに正!",)
            first_plus_pettern_ans += abs(first_plus_pettern_cumsum) + 1
            first_plus_pettern_cumsum -= abs(first_plus_pettern_cumsum) + 1
        if first_minus_pettern_cumsum <=0:
            #print("初項が負で偶数項なのに負!")
            first_minus_pettern_ans += abs(first_minus_pettern_cumsum) + 1
            first_minus_pettern_cumsum += abs(first_minus_pettern_cumsum) + 1
            
    #print(i,":",a_list[i],"  ",first_minus_pettern_cumsum,"/",first_plus_pettern_cumsum)
            
print(min(first_plus_pettern_ans,first_minus_pettern_ans))

Submission status: Accepted

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

def check(f):
        flag = f 
        t = 0 
        c = 0 
        for i in range(n):
                if t + a[i] <= 0 and flag == 1:
                        c += 1 - t - a[i]
                        t = 1 
                elif t + a[i] >= 0 and flag == -1: 
                        c += 1 + t + a[i]
                        t = -1
                else:
                        t += a[i] 
                flag *= -1
        return c 
    
total_p = check(1)
total_m = check(-1)
print(min(total_p, total_m))

Submission status: Accepted

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

s = 0
tmp1 = 0
for i in range(N):
  s += A[i]
  if i % 2 == 0:
    if s >= 0:
      tmp1 += s + 1
      s = -1
  else:
    if s <= 0:
      tmp1 += -s + 1
      s = 1
#print(tmp1)

s = 0
tmp2 = 0
for i in range(N):
  s += A[i]
  if i % 2 == 0:
    if s <= 0:
      tmp2 += -s + 1
      s = 1
  else:
    if s >= 0:
      tmp2 += s + 1
      s = -1
#print(tmp2)

print(min(tmp1, tmp2))

Submission status: Accepted

Solution code:




def main()->None:
    # 回答見た
    n = int(input())
    a = list(map(int, input().split()))
    range_N = range(n)
    acounter = 0
    bcounter = 0
    num = 0
    for i in range_N:
        num += a[i]
        if i % 2 == 0:
            if num <= 0:
                acounter += abs(num) + 1
                num = 1

        else:
            if num >= 0:
                acounter += num + 1
                num = -1


    num = 0
    for i in range_N:
        num += a[i]
        if i % 2 != 0:
            if num <= 0:
                bcounter += abs(num) + 1
                num = 1

        else:
            if num >= 0:
                bcounter += num + 1
                num = -1

    print(min(acounter, bcounter))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
rui = [0] * n
cnt = 0
if a[0] > 0:
    rui[0] = a[0]
else:
    rui[0] = 1
    cnt += abs(a[0]) + 1
for i in range(1, n):
    tmp = rui[i - 1] + a[i]
    if tmp == 0:
        if rui[i - 1] > 0:
            rui[i] = tmp - 1
            cnt += 1
        else:
            rui[i] = tmp + 1
            cnt += 1
    else:
        if rui[i - 1] > 0 and tmp > 0:
            rui[i] = -1
            cnt += abs(tmp) + 1
        elif rui[i - 1] < 0 and tmp < 0:
            rui[i] = 1
            cnt += abs(tmp) + 1
        else:
            rui[i] = tmp

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

print(min(cnt, cnt1))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
currentSum = 0
count3 = 0
count4 = 0

currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count3 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count3 += abs(currentSum) + 1
        currentSum = -1
    elif A[i] <= 0 and restSum == 0:
        count3 += abs(currentSum) + 1
        currentSum = 1
        
currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count4 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count4 += abs(currentSum) + 1
        currentSum = -1
    elif A[i] >= 0 and restSum == 0:
        count4 += abs(currentSum) + 1
        currentSum = -1
        
print(min(count3, count4))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
currentSum = 0
count1 = 0
count2 = 0
count3 = 0
count4 = 0

for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count1 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count1 += abs(currentSum) + 1
        currentSum = -1
    elif currentSum == 0 and restSum == 0:
        count1 += 1
        currentSum = -1

currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count2 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count2 += abs(currentSum) + 1
        currentSum = -1
    elif currentSum == 0 and restSum == 0:
        count2 += 1
        currentSum = 1

currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count3 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count3 += abs(currentSum) + 1
        currentSum = -1
    elif A[i] <= 0 and restSum == 0:
        count3 += abs(currentSum) + 1
        currentSum = 1
        
currentSum = 0
for i in range(N):
    restSum = currentSum
    currentSum += A[i]
    if currentSum <= 0 and restSum < 0:
        count4 += abs(currentSum) + 1
        currentSum = 1
    elif currentSum >= 0 and restSum > 0:
        count4 += abs(currentSum) + 1
        currentSum = -1
    elif A[i] >= 0 and restSum == 0:
        count4 += abs(currentSum) + 1
        currentSum = -1
        
print(min(count1, count2, count3, count4))