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:
n = int(input())
a = list(map(int,input().split()))

ans = 0
"""
if a[0] == 0:
  #a[1] とa[0]を足して|1|かつa[0]とa[1]+a[0]の符号を逆にしたい
  if a[1] > 0:
    a[0] = 1 - a[1]    
  elif a[1] < 0:
    a[0] = -1 - a[1]
  else: # a[0] a[1] 共に0の場合、a[2]に依存する　-> 両方やればいい
奇数番目と偶数ばんめのどちらを＋にするか  
"""
ans_odd = 0
total_odd = 0
ans_even = 0
total_even = 0
for i in range(n):
  if (i+1) % 2 == 1: #奇数ばんめ
    #奇数番目を＋、偶数番目を-にするパターン
    total_odd += a[i]
    if total_odd <= 0:
      ans_odd += abs(total_odd) + 1
      total_odd = 1
      
    #偶数番目を＋、奇数番目を-にするパターン
    total_even += a[i]
    if total_even >= 0:
      ans_even += (abs(total_even)+1)
      total_even = -1
      
  else: #偶数番目
    #奇数番目を＋、偶数番目を-にするパターン
    total_odd += a[i]
    if total_odd >= 0:
      ans_odd += abs(total_odd)+1
      total_odd = -1
      
    #偶数番目を＋、奇数番目を-にするパターン
    total_even += a[i]
    if total_even <= 0:
      ans_even += abs(total_even)+1
      total_even = 1
  """    
  print("total,ans_odd")    
  print(total_odd)
  print(ans_odd)
  print("total,ans_even")
  print(total_even)
  print(ans_even)
  """   
print(min([ans_odd, ans_even]))
#print()

Submission status: Accepted

Solution code:
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n = ni()
l = nl()
a = [0 for i in range(n + 1)]
for i in range(n):
    a[i + 1] = a[i] + l[i]
ans1 = 0
ans2 = 0
diff = 0
fluc = 0
b = a.copy()
for i in range(1, n + 1):
    a[i] += fluc
    diff = 0
    if i % 2 == 0 and a[i] <= 0:
        diff += -a[i] + 1
        fluc += diff
        ans1 += abs(diff)
    elif i % 2 == 1 and a[i] >= 0:
        diff -= a[i] + 1
        fluc += diff
        ans1 += abs(diff)
diff = 0
fluc = 0
a = b.copy()
for i in range(1, n + 1):
    a[i] += fluc
    diff = 0
    if i % 2 == 1 and a[i] <= 0:
        diff += -a[i] + 1
        fluc += diff
        ans2 += abs(diff)
    elif i % 2 == 0 and a[i] >= 0:
        diff -= a[i] + 1
        fluc += diff
        ans2 += abs(diff)
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
for i in range(1,n):
    a[i] += a[i-1]
a_1 = a.copy()
ans = 0
k = 0
for i in range(n):
    if i%2==0 and a_1[i]+k<=0:
        tmp = -a_1[i]-k+1
        k += tmp
        ans += abs(tmp)
    elif i%2==1 and a_1[i]+k>=0:
        tmp = -a_1[i]-k-1
        k += tmp
        ans += abs(tmp)
ans_2 = 0
k = 0
for i in range(n):
    if i%2==1 and a[i]+k<=0:
        tmp = -a[i]-k+1
        k += tmp
        ans_2 += abs(tmp)
    elif i%2==0 and a[i]+k>=0:
        tmp = -a[i]-k-1
        k += tmp
        ans_2 += abs(tmp)
print(min(ans,ans_2))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
s = 0
num = 0
sign = "+"
for a in A:
    s += a
    if sign == "+":
        if s > 0:
            pass
        else:
            num += abs(s) + 1
            s = 1
        sign = "-"
        continue
    if sign == "-":
        if s < 0:
            pass
        else:
            num += s + 1
            s = -1
        sign = "+"
        continue
s = 0
num2 = 0
sign = "-"
for a in A:
    s += a
    if sign == "+":
        if s > 0:
            pass
        else:
            num2 += abs(s) + 1
            s = 1
        sign = "-"
        continue
    if sign == "-":
        if s < 0:
            pass
        else:
            num2 += s + 1
            s = -1
        sign = "+"
        continue
print(min(num, num2))

Submission status: Accepted

Solution code:
import sys
fastinput=sys.stdin.readline
n=int(fastinput())
ai=[int(i) for i in fastinput().split()]
#プラス始まり
goukei=0
sousa=0
for k,a in enumerate(ai):
    goukei+=a
    if not k%2:#even:plus
        if goukei<=0:
            sousa+=1-goukei
            goukei=1
    else:#odd:minus
        if goukei>=0:
            sousa+=goukei+1
            goukei=-1
ans1=sousa
#マイナス始まり
goukei=0
sousa=0
for k,a in enumerate(ai):
    goukei+=a
    if k%2:#odd:plus
        if goukei<=0:
            sousa+=1-goukei
            goukei=1
    else:#even:minus
        if goukei>=0:
            sousa+=goukei+1
            goukei=-1
ans2=sousa
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N = int(input())
    A = list(map(int, input().split()))

    cost_pn = 0
    cost_np = 0
    acc_pn = 0
    acc_np = 0
    for i in range(N):
        acc_pn += A[i]
        acc_np += A[i]

        if i % 2 == 0:
            if acc_pn <= 0:
                cost_pn += abs(acc_pn) + 1
                acc_pn = 1

            if acc_np >= 0:
                cost_np += abs(acc_np) + 1
                acc_np = -1

        else:
            if acc_pn >= 0:
                cost_pn += abs(acc_pn) + 1
                acc_pn = -1

            if acc_np <= 0:
                cost_np += abs(acc_np) + 1
                acc_np = 1

    return min(cost_pn, cost_np)


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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
acm1 = 0
acm2 = 0
ans1 = 0
ans2 = 0
for i, a in enumerate(A):
  acm1 += a
  acm2 += a
  if(i%2==1):
    if(acm1>=0):
      ans1 += acm1 + 1
      acm1 = -1
    if(acm2<=0):
      ans2 += -acm2 + 1
      acm2 = 1
  else:
    if(acm1<=0):
      ans1 += -acm1 + 1
      acm1 = 1
    if(acm2>=0):
      ans2 += acm2 + 1
      acm2 = -1
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
def main():
    n = input()

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

    ans = 10 ** 20
    for i in [-1, 1]:
        total = 0
        op_num = 0

        for a_i in a_s:
            total += a_i

            if total * i <= 0:
                op_num += abs(total - i)
                total = i
            
            i *= -1

        ans = min(ans, op_num)
    print(ans)
    

main()

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
ans=10**15
for i in[-1,1]:
    ansi,sum=0,0
    for a in A:
        sum+=a
        if sum*i<=0:ansi+=abs(sum-i);sum=i
        i*=-1
    ans=min(ans,ansi)
print(ans)

Submission status: Accepted

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

ret0 = 0
count0 = 0
for i in range(N):
    ret0 += A[i]
#     print("ret0",ret0)
#     print("count0",count0)
    if ret0 < 0 and i % 2 == 0:
#         print("A")
        count0 += abs(ret0) + 1
        ret0 = 1
    if ret0 > 0 and i % 2 == 1:
#         print("B")
        count0 += ret0 + 1
        ret0 = -1
#     print("ret0",ret0)
    if ret0 == 0 and i % 2 == 0:
        count0 += 1
        ret0 = 1
    if ret0 == 0 and i % 2 == 1:
        count0 += 1
        ret0 = -1
# print(count0)

ret1 = 0
count1 = 0
for i in range(N):
    ret1 += A[i]
#     print("ret1",ret1)
#     print("count1",count1)
    if ret1 > 0 and i % 2 == 0:
        count1 += ret1 + 1
        ret1 = -1
    if ret1 < 0 and i % 2 == 1:
        count1 += abs(ret1) + 1
        ret1 = 1
#     print("ret1",ret1)
    if ret1 == 0 and i % 2 == 0:
        count1 += 1
        ret1 = -1
    if ret1 == 0 and i % 2 == 1:
        count1 += 1
        ret1 = 1
# print(count1)

print(min(count0, count1))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
ans1, ans2 = 0, 0
f = 0
f += a[0]
if f <= 0:
    f = 1
    ans1 += 1 - a[0]
for i in range(1, N):
    if f * (f + a[i]) < 0:
        f += a[i]
        continue
    ans1 += abs(f + a[i]) + 1
    if f > 0:
        f = -1
    else:
        f = 1
f = 0
f += a[0]
if f >= 0:
    f = -1
    ans2 += 1 + a[0]
for i in range(1, N):
    if f * (f + a[i]) < 0:
        f += a[i]
        continue
    ans2 += abs(f + a[i]) + 1
    if f > 0:
        f = -1
    else:
        f = 1
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
ans = 10**20

for i in[-1,1]:
    ansi,sum = 0,0
    for a in A:
        sum += a
        if sum*i <= 0:
            ansi +=abs(sum-i)
            sum = i
        i *= -1
    ans = min(ans,ansi)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
odd = 0
even = 0
temp = 0
for i in range(N):
    temp += a[i]
    if i % 2 == 0:
        # 奇数番目が正
        if temp < 1:
            odd += 1-temp
            temp = 1
    else:
        if temp > -1:
            odd += 1 + temp
            temp = -1
                   
temp = 0
for i in range(N):
    temp += a[i]
    if i % 2 == 0:
        # 奇数番目が負
        if temp > -1:
            even += 1 + temp
            temp = -1     
    else:
        if temp < 1:
            even += 1-temp
            temp = 1
print(min(odd,even))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = [0] * (n + 1)
c, d = 0, 0
for i in range(n):
    s[i + 1] = s[i] + a[i]
    if i % 2 == 0:
        if s[i + 1] == 0:
            c += 1
            s[i + 1] = 1
        elif s[i + 1] < 0:
            c += -s[i + 1] + 1
            s[i + 1] = 1
    else:
        if s[i + 1] == 0:
            c += 1
            s[i + 1] = -1
        elif s[i + 1] > 0:
            c += s[i + 1] + 1
            s[i + 1] = -1

for i in range(n):
    s[i + 1] = s[i] + a[i]
    if i % 2 != 0:
        if s[i + 1] == 0:
            d += 1
            s[i + 1] = 1
        elif s[i + 1] < 0:
            d += -s[i + 1] + 1
            s[i + 1] = 1
    else:
        if s[i + 1] == 0:
            d += 1
            s[i + 1] = -1
        elif s[i + 1] > 0:
            d += s[i + 1] + 1
            s[i + 1] = -1
print(min(c, d))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
#具体的な操作を考える必要はなくて，ただ部分和をひたすら考えていけばいい
answer1 = 0
answer2 = 0
sum1 = 0
sum2 = 0
# pmpm...
for i in range(n):
    sum1 += a[i]
    if i % 2 == 1:
        if sum1 >= 0:
            answer1 += sum1 - (-1)
            sum1 = -1
    if i % 2 == 0:
        if sum1 <= 0:
            answer1 += 1 - sum1
            sum1 = 1
# mpmp...
for i in range(n):
    sum2 += a[i]
    if i % 2 == 0:
        if sum2 >= 0:
            answer2 += sum2 - (-1)
            sum2 = -1
    if i % 2 == 1:
        if sum2 <= 0:
            answer2 += 1 - sum2
            sum2 = 1
print(min(answer1, answer2))

Submission status: Accepted

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

answer = 1e15
for s in range(2): # start from + or -
  prev_cumsum=0
  num_man = 0
  for i, a in enumerate(A):
    if (prev_cumsum + a)*(-1)**(i+s) > 0:
      prev_cumsum += a
      #print(prev_cumsum)
    else:
      num_man += abs((-1)**(i+s) - prev_cumsum -a)
      prev_cumsum = (-1)**(i+s)
      #print(prev_cumsum)
  answer = min(answer, num_man)
  #print(num_man)
print(answer)

Submission status: Accepted

Solution code:
def sequence(N: int, A: list) -> int:
    op1, op2 = 0, 0
    s1, s2 = 0, 0

    for i, a in enumerate(A):
        s1, s2 = s1 + a, s2 + a

        if i % 2 > 0:  # odd
            if s1 <= 0:
                op1 += abs(s1) + 1
                s1 += abs(s1) + 1
            if s2 >= 0:
                op2 += abs(s2) + 1
                s2 -= abs(s2) + 1
        else:  # even
            if s1 >= 0:
                op1 += abs(s1) + 1
                s1 -= abs(s1) + 1
            if s2 <= 0:
                op2 += abs(s2) + 1
                s2 += abs(s2) + 1

    return min(op1, op2)


if __name__ == "__main__":
    N = int(input())
    A = [int(s) for s in input().split()]
    ans = sequence(N, A)
    print(ans)

Submission status: Accepted

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

c1 = 0
c2 = 0
sum1 = 0 # 偶数番目が正
sum2 = 0 # 奇数番目が正
for i in range(n):
    sum1 += an[i]
    sum2 += an[i]
    if i%2 == 0:
        if sum1 <= 0:
            p = abs(sum1) + 1
            c1 += p
            sum1 += p
        if sum2 >= 0:
            p = abs(sum2) + 1
            c2 += p
            sum2 -= p
    else:
        if sum1 >= 0:
            p = abs(sum1) + 1
            c1 += p
            sum1 -= p
        if sum2 <= 0:
            p = abs(sum2) + 1
            c2 += p
            sum2 += p

print(min(c1, c2))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
c=0
d=0
e=0
f=0
for i in range(n):
    k=a[i]
    e+=k
    f+=k
    if i%2==0:
        if e<=0:
            c+=1-e
            e=1
        if f>=0:
            d+=f+1
            f=-1
    else:
        if e>=0:
            c+=1+e
            e=-1
        if f<=0:
            d+=1-f
            f=1
print(min(c,d))

Submission status: Accepted

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

sum = a[0]
count1 = 0
count2 = 0

sum1 = 0
sum2 = 0

for i in range(n) :
    sum1 += a[i]
    sum2 += a[i]
    if i % 2 == 0 :
        if sum1 >= 0 :
            count1 += abs(sum1) + 1
            sum1 = -1
        if sum2 <= 0 :
            count2 += abs(sum2) + 1
            sum2 = 1
    elif i % 2 == 1 :
        if sum1 <= 0 :
            count1 += abs(sum1) + 1
            sum1 = 1
        if sum2 >= 0 :
            count2 += abs(sum2) + 1
            sum2 = -1

print(min(count1, count2))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
trueans = float('inf')
for k in [-1,1]:
    sign = k
    ans = 0
    s = 0
    for i in range(n):
        s += a[i]
        if s == 0:
            s = sign
            ans += 1
        elif s//abs(s) == sign:
            pass
        else:
            ans += abs(sign-s)
            s = sign
        sign *= -1
    trueans = min(ans,trueans)
print(trueans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = [a[0]]
for i in range(1, n):
    s.append(s[i-1]+a[i])
ans1, ans2 = 0, 0
for i in range(n):
    if s[i]*((-1)**i) <= 0:
        ans1 += abs(s[i]-(-1)**i)
        s[i] = (-1)**i
    if i+1 <= n-1:
        s[i+1] = s[i]+a[i+1]
s = [a[0]]
for i in range(1, n):
    s.append(s[i-1]+a[i])
for i in range(n):
    if s[i]*((-1)**(i+1)) <= 0:
        ans2 += abs(s[i]-(-1)**(i+1))
        s[i] = (-1)**(i+1)
    if i+1 <= n-1:
        s[i+1] = s[i]+a[i+1]
print(min(ans1, ans2))

Submission status: Accepted

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

A_sum = 0
res0 = 0
for i,j in enumerate(A):
    A_sum += j
    if (i+1) % 2 != 0 and A_sum >= 0:
        res0 += A_sum + 1
        A_sum = -1
    if (i+1) % 2 == 0 and A_sum <= 0:
        res0 += abs(A_sum - 1)
        A_sum = 1
#     print("A_sum", A_sum)
#     print("res0", res0)
# print(res0)

A_sum = 0
res1 = 0
for i,j in enumerate(A):
    A_sum += j
    if (i+1) % 2 != 0 and A_sum <= 0:
        res1 += abs(A_sum - 1)
        A_sum = 1
    if (i+1) % 2 == 0 and A_sum >= 0:
        res1 += A_sum + 1
        A_sum = -1
#     print("A_sum", A_sum)
#     print("res1", res1)
# print(res1)

print(min(res0, res1))

Submission status: Accepted

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

S,cnt,res1 = 0,0,0
for a in A:
	S += a
	cnt += 1
	if cnt%2 == 1 and S <= 0:
		res1 += abs(S-1)
		S = 1
	elif cnt%2 == 0 and S >= 0:
		res1 += S+1
		S = -1

S,cnt,res2 = 0,0,0
for a in A:
	S += a
	cnt += 1
	if cnt%2 == 1 and S >= 0:
		res2 += S+1
		S = -1
	elif cnt%2 == 0 and S <= 0:
		res2 += abs(S-1)
		S = 1

print(min(res1,res2))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
s1 = 0
s2 = 0
cnt1 = 0
cnt2 = 0
for i in range(n):
    s1 += a[i]
    s2 += a[i]
    if i % 2 == 0:
        if s1 <= 0:
            cnt1 += abs(s1) + 1
            s1 += abs(s1) + 1
        if s2 >= 0:
            cnt2 += s2 + 1
            s2 -= s2 + 1
    else:
        if s1 >= 0:
            cnt1 += s1 + 1
            s1 -= s1 + 1
        if s2 <= 0:
            cnt2 += abs(s2) + 1
            s2 += abs(s2) + 1
print(min(cnt1,cnt2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
count = 0
wa = 0
ans = 10 ** 18
#+-+-...
for i in range(n):
    wa += a[i]
    if i % 2 == 0 and wa <= 0:
        count += abs(wa) + 1
        wa = 1
    elif i % 2 == 1 and wa >= 0:
        count += abs(wa) + 1
        wa = -1
ans = min(ans, count)

#-+-+...
count = 0
wa = 0
for i in range(n):
    wa += a[i]
    if i % 2 == 0 and wa >= 0:
        count += abs(wa) + 1
        wa = -1
    elif i % 2 == 1 and wa <= 0:
        count += abs(wa) + 1
        wa = +1
ans = min(ans, count)

print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
n = int(input())
a_list = list(map(int, input().split()))
cost1, cost2 = 0, 0
a_sum1 = a_list[0]
if a_sum1 <= 0:
    cost1 += abs(a_sum1)+1
    a_sum1 = 1
flag1 = 1
a_sum2 = a_list[0]
if a_sum2 >= 0:
    cost2 += a_sum2+1
    a_sum2 = -1
flag2 = -1
for i in range(1, n):
    a_sum1 += a_list[i]
    if flag1 == 1:
        if a_sum1 >= 0:
            cost1 += a_sum1+1
            a_sum1 = -1
        flag1 = -1
    else:
        if a_sum1 <= 0:
            cost1 += abs(a_sum1)+1
            a_sum1 = 1
        flag1 = 1
    a_sum2 += a_list[i]
    if flag2 == 1:
        if a_sum2 >= 0:
            cost2 += a_sum2+1
            a_sum2 = -1
        flag2 = -1
    else:
        if a_sum2 <= 0:
            cost2 += abs(a_sum2)+1
            a_sum2 = 1
        flag2 = 1
print(min(cost1, cost2))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    total = [[0] * (N+1) for _ in range(2)]
    ans = [0] * 2
    for i in range(N):
        for n in range(2):
            total[n][i+1] = total[n][i] + A[i]
            if (i+n) % 2 == 0:
                if total[n][i+1] <= 0:
                    ans[n] += 1 - total[n][i+1]
                    total[n][i+1] = 1
            else:
                if total[n][i+1] >= 0:
                    ans[n] += total[n][i+1] + 1
                    total[n][i+1] = -1
    print(min(ans))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
def main(now,A,ans):
    for i in range(1,N):
        if now<0 and now+A[i]<=0:
            ans+=abs(now)-A[i]+1
            now=1
        elif now>0 and now+A[i]>=0:
            ans+=now+A[i]+1
            now=-1
        else:
            now+=A[i]
    return ans
z=float('inf')
x=main(A[0],A,0)
if A[0]>0:
    now=-1
    ans=A[0]+1
elif A[0]<0:
    now=1
    ans=abs(A[0])+1
elif A[0]==0:
    z=main(-1,A,1)
    now=1
    ans=1
y=main(now,A,ans)
print(min(x,y,z))

Submission status: Accepted

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

#print(A)

def getSign(a):
	if a < 0:
		return -1
	elif a == 0:
		return 0
	else:
		return 1

counts = []
for beforeSign in [-1,1]:
	count = 0
	sumN = 0
	for i in range(N):
		sumN += A[i]

		#print("be",i,sumN,A[i],count,beforeSign)

		if 0 <= beforeSign * sumN:
			add = -sumN - beforeSign
			sumN += add
			count += abs(add)
		
		beforeSign = getSign(sumN)
		#print("af",i,sumN,A[i],count,beforeSign)
	
	counts.append(count)
	
print(min(counts))

Submission status: Accepted

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

def f(x):
  cnt = 0
  tmp = 0
  for a in A:
    tmp += a
    #print(tmp, x)
    if(tmp*x >= 0):
      cnt += abs(tmp)+1
      tmp = -x
    x *= -1
    #print(tmp, x)
  return cnt

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

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def calc(A, N):
    s = 0L
    count = 0
    pre_sign = 0
    for i in range(N):
        s += A[i]
        if s == 0:
            if pre_sign == 1:
                s -= 1L
            elif pre_sign == -1:
                s += 1L
            count += 1
        elif pre_sign == s/abs(s):
            if s < 0L:
                ope = 1L - s
            else:
                ope = -1L - s
            s += ope
            count += abs(ope)
        pre_sign = s/abs(s)
    return count


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    A_minus = [int(input_raw[i]) for i in range(N)]
    A_plus = [int(input_raw[i]) for i in range(N)]
    firstA = A_minus[0]
    first = 0
    if firstA >= 0:
        first = A_minus[0]  + 1
        A_minus[0] = -1

    count_minus =calc(A_minus, N) + first

    first = 0
    if firstA <= 0:
        first = 1 - A_plus[0]
        A_plus[0] = 1

    count_plus =calc(A_plus, N) + first
    count = min(count_minus, count_plus)


    print count




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#ABC059C
N=int(input())
A=list(map(int,input().split()))
ans=[0,0]
i=[1,0]
for c in range(2):
	summ=0
	for a in A:
		summ+=a
		if summ==0:
			ans[c]+=abs(summ-(-1)**i[c])
			summ=(-1)**i[c]
		elif (summ>0)==i[c]:
			ans[c]+=abs(summ-(-1)**i[c])
			summ=(-1)**i[c]
		i[c]=(i[c]+1)%2
print(min(ans))

Submission status: Accepted

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

#A

#B

#C
n = I()
a = LI()
b = [a[i] for i in range(n)]
ans = 0
k = a[0]
if a[0] == 0:
    a[0] = 1
    ans = 1
    k = 1
for i in range(1,n):
    if k*(k+a[i]) >= 0:
        if k < 0:
            ans += abs(1-k-a[i])
            a[i] = 1-k
            k = 1
        else:
            ans += abs(-1-k-a[i])
            a[i] = -1-k
            k = -1
    else:
        k += a[i]
if b[0] >= 0:
    ans2 = b[0]+1
    k = -1
    b[0] = -1
else:
    ans2 = 1-b[0]
    k = 1
    b[0] = 1
for i in range(1,n):
    if k*(k+b[i]) >= 0:
        if k < 0:
            ans2 += abs(1-k-b[i])
            b[i] = 1-k
            k = 1
        else:
            ans2 += abs(-1-k-b[i])
            b[i] = -1-k
            k = -1
    else:
        k += b[i]
print(min(ans,ans2))

#D
"""
x,y = LI()
if abs(x-y) <= 1:
    print("Brown")
else:
    print("Alice")
"""
#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
s1 = s2 = su = 0
for i in range(n):
  su += a[i]
  if (i % 2 == 0 and su <= 0):
    s1 += abs(su) + 1
    su = 1
  elif(i % 2 == 1 and su >= 0):
    s1 += abs(su) + 1
    su = -1
su = 0
for i in range(n):
  su += a[i]
  if (i % 2 == 0 and su >= 0):
    s2 += abs(su) + 1
    su = -1
  elif (i % 2 == 1 and su <= 0):
    s2 += abs(su) + 1
    su = 1
print(min(s1,s2))

Submission status: Accepted

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

total = [0,0]
count = [0,0]

for i in range(2):
    for j in range(N):
        total[i] += A[j]
        if (i+j)%2 == 0:
            if total[i] >= 0:
                count[i] += abs(total[i]+1)
                total[i] = -1
        else:
            if total[i] <= 0:
                count[i] += abs(total[i]-1)
                total[i] = 1

print(min(count))

Submission status: Accepted

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

#+,-,+,-
asum = [0]*(N)
ansp = 0
if(a[0] <= 0):
    ansp+=1-a[0]
    asum[0]=1
else:
    asum[0] = a[0]
for i in range(1,N):
    asum[i] = asum[i-1] + a[i]
    if(asum[i-1]*asum[i] >= 0):
        if(asum[i-1]>0):
            ansp+=1+asum[i]
            asum[i]=-1
        else:
            ansp+=1-asum[i]
            asum[i]=1
# print(asum)
# print(ansp)
#-,+,-,+,,,
ansn = 0
if(a[0] >= 0 ):
    ansn+=1+a[0]
    asum[0]=-1
else:
    asum[0] = a[0]
for i in range(1,N):
    asum[i] = asum[i-1] + a[i]
    if(asum[i-1]*asum[i] >= 0):
        if(asum[i-1]>0):
            ansn+=1+asum[i]
            asum[i]=-1
        else:
            ansn+=1-asum[i]
            asum[i]=1
# print(asum)
# print(ansn)
print(min(ansp,ansn))

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int,input().split()))
accum = 0
cnt = 0
for i in range(N):
    accum += L[i]
    if i % 2 == 0 and accum <= 0:
        cnt += 1 - accum
        accum += (1 - accum)
    if i % 2 == 1 and accum >= 0:
        cnt += accum - (-1)
        accum -= (accum + 1)
  #  print(cnt,accum,"1")
ans = cnt
accum = 0
cnt = 0
for i in range(N):
    accum += L[i]
    if i % 2 == 1 and accum <= 0:
        cnt += 1 - accum
        accum += 1 - accum
    if i % 2 == 0 and accum >= 0:
        cnt += accum - (-1)
        accum -= accum + 1
  #  print(cnt,accum)
ans = min(ans,cnt)
print(ans)

Submission status: Accepted

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

def f(x):
    ans = 0
    cnt = 0
    for i in range(n):
        if i % 2 == x:
            if cnt + A[i] < 0:
                cnt += A[i]
            else:
                ans += abs(cnt + A[i] - (-1))
                cnt = -1
        else:
            if cnt + A[i] > 0:
                cnt += A[i]
            else:
                ans += abs(cnt + A[i] - 1)
                cnt = 1
    return ans

print(min(f(0), f(1)))

Submission status: Accepted

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

ans = 0
cnt = 0
for i in range(n):
    if i % 2 == 0:
        if cnt + A[i] < 0:
            cnt += A[i]
        else:
            ans += abs(cnt + A[i] - (-1))
            cnt = -1
    else:
        if cnt + A[i] > 0:
            cnt += A[i]
        else:
            ans += abs(cnt + A[i] - 1)
            cnt = 1
            
ans2 = 0
cnt2 = 0
for i in range(n):
    if i % 2 == 1:
        if cnt2 + A[i] < 0:
            cnt2 += A[i]
        else:
            ans2 += abs(cnt2 + A[i] - (-1))
            cnt2 = -1
    else:
        if cnt2 + A[i] > 0:
            cnt2 += A[i]
        else:
            ans2 += abs(cnt2 + A[i] - 1)
            cnt2 = 1
                
print(min(ans, ans2))

Submission status: Accepted

Solution code:
import copy
import sys
write = sys.stdout.write

n = int(input())
A = list(map(int,input().split())) # +, -, +, ...
B = copy.deepcopy(A) #-, +, -, ...
sumA = []
sumB = []
cntA = 0
cntB = 0
if A[0] == 0:
    A[0] += 1
    cntA += 1
    B[0] -= 1
    cntB += 1
elif A[0] > 0:
    cntB += (B[0]+1)
    B[0] = -1
else:
    cntA += abs(A[0])+1
    A[0] = 1
sumA.append(A[0])
sumB.append(B[0])
#write("cntA : " + str(cntA) + " cntB : " + str(cntB) + "\n") 
for i in range(1, n):
    tempA = sumA[i-1] + A[i]
    tempB = sumB[i-1] + B[i]
    if i%2 == 1: #Aは-, Bは+
        if tempA == 0:
            #A[i] -= 1
            cntA += 1
            sumA.append(-1)
        elif tempA > 0:
            #A[i] -= abs(tempA) + 1
            cntA += (abs(tempA) + 1)
            sumA.append(-1)
        else:
            sumA.append(tempA)

        if tempB == 0:
            #B[i] += 1
            cntB += 1
            sumB.append(1)
        elif tempB < 0:
            #B[i] += abs(tempB) + 1
            cntB += (abs(tempB) + 1)
            sumB.append(1)
        else:
            sumB.append(tempB)
    else: #Aは+, Bは-
        if tempA == 0:
            cntA += 1
            sumA.append(1)
        elif tempA < 0:
            cntA += (abs(tempA) + 1)
            sumA.append(1)
        else:
            sumA.append(tempA)
        
        if tempB == 0:
            #B[i] -= 1
            cntB += 1
            sumB.append(-1)
        elif tempB > 0:
            #B[i] -= abs(tempB) + 1
            cntB += (abs(tempB) + 1)
            sumB.append(-1)
        else:
            sumB.append(tempB)
    #write("cntA : " + str(cntA) + " cntB : " + str(cntB) + "\n")

print(str(min(cntA, cntB)))

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
A=list(map(int,input().split()))
ans1,total=0,0
for i in range(n):
    total+=A[i]
    if i%2==0 and total<=0:
        tmp=-total+1
        total=1
        ans1+=tmp
    elif i%2==1 and total>=0:
        tmp=total+1
        total=-1
        ans1+=tmp
ans2,total=0,0
for i in range(n):
    total+=A[i]
    if i%2==0 and total>=0:
        tmp=total+1
        total=-1
        ans2+=tmp
    elif i%2==1 and total<=0:
        tmp=-total+1
        total=1
        ans2+=tmp
print(min(ans1,ans2))