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()))
sum_a = [0]*(n+1)
count = 0
for i in range(0,n):
  sum_a[i+1]=a[i]+sum_a[i]
  if i%2==0:
    if sum_a[i+1]>0:
      continue
    else:
      count += 1+abs(sum_a[i+1])
      sum_a[i+1]=1    
  else: 
    if sum_a[i+1]<0:
      continue
    else:
      count += 1+abs(sum_a[i+1])
      sum_a[i+1]=-1

ans = count
sum_a = [0]*(n+1)
count = 0
for i in range(0,n):
  sum_a[i+1]=a[i]+sum_a[i]
  if i%2==1:
    if sum_a[i+1]>0:
      continue
    else:
      count += 1+abs(sum_a[i+1])
      sum_a[i+1]=1    
  else: 
    if sum_a[i+1]<0:
      continue
    else:
      count += 1+abs(sum_a[i+1])
      sum_a[i+1]=-1

print(min(count,ans))

Submission status: Accepted

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

total = 0
count1 = 0
for i in range(n):
    total += a[i]
    if i % 2 == 0:
        if total <= 0:
            tmp = abs(total) + 1
            total += tmp
            count1 += tmp
    else:
        if total >= 0:
            tmp = abs(total) + 1
            total -= tmp
            count1 += tmp
count2 = 0
total = 0
for i in range(n):
    total += a[i]
    if i % 2 != 0:
        if total <= 0:
            tmp = abs(total) + 1
            total += tmp
            count2 += tmp
    else:
        if total >= 0:
            tmp = abs(total) + 1
            total -= tmp
            count2 += tmp
print(min(count1, count2))

Submission status: Accepted

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

# plus
ans1 = 0
subtotal = 0
for i in range(n):
	subtotal += N[i]
	if i % 2 == 0 and subtotal <= 0:
		diff = abs(subtotal) + 1
		subtotal += diff
		ans1 += diff
	elif i % 2 != 0 and subtotal >= 0:
		diff = abs(subtotal) + 1
		subtotal -= diff
		ans1 += diff

#minus
ans2 = 0
subtotal = 0
for i in range(n):
	subtotal += N[i]
	if i % 2 != 0 and subtotal <= 0:
		diff = abs(subtotal) + 1
		subtotal += diff
		ans2 += diff
	elif i % 2 == 0 and subtotal >= 0:
		diff = abs(subtotal) + 1
		subtotal -= diff
		ans2 += diff

print(min(ans1, ans2))

Submission status: Accepted

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

def dfs(sign):
  cnt = 0
  tmp = 0
  for a in A:
    tmp += a
    if(tmp*sign >= 0):
      cnt += abs(tmp) + 1
      tmp = -sign
    sign *= -1
  return cnt

print(min(dfs(1), dfs(-1)))

Submission status: Accepted

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

res = 0
tmp = 0

for i in range(N):
    n = L[i] + tmp
    if i%2 == 0:
        if n >= 0:
            res += n + 1
            tmp = -1
        else:
            tmp = n
    else:
        if n <= 0:
            res += abs(n) + 1
            tmp = 1
        else:
            tmp = n
            
res1 = res

res = 0
tmp = 0
for i in range(N):
    n = L[i] + tmp
    if i%2 == 1:
        if n >= 0:
            res += n + 1
            tmp = -1
        else:
            tmp = n
    else:
        if n <= 0:
            res += abs(n) + 1
            tmp = 1
        else:
            tmp = n
print(min(res1, res))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a_t = [0]
for i in range(n):
    a_t.append(a[i] + a_t[i])
ans = 0
ans_ = 0
count = 0
count_ = 0
for i in range(1, n + 1):
    if i == 1:
        if a_t[i] >= 0:
            ans += a_t[i] + 1
            count += -(a_t[i]) - 1
    else:
        if a_t[i - 1] + count > 0:
            if a_t[i] + count > 0:
                ans += a_t[i] + count + 1
                count += -(a_t[i] + count) - 1
            if a_t[i] + count == 0:
                ans += 1
                count -= 1
        elif a_t[i - 1] + count < 0:
            if a_t[i] + count < 0:
                ans += -(a_t[i] + count) + 1
                count += -(a_t[i] + count) + 1
            if a_t[i] + count == 0:
                ans += 1
                count += 1
for i in range(1, n + 1):
    if i == 1:
        if a_t[i] <= 0:
            ans_ += -a_t[i] + 1
            count_ += -(a_t[i]) + 1
    else:
        if a_t[i - 1] + count_ > 0:
            if a_t[i] + count_ > 0:
                ans_ += a_t[i] + count_ + 1
                count_ += -(a_t[i] + count_) - 1
            if a_t[i] + count_ == 0:
                ans_ += 1
                count_ -= 1
        elif a_t[i - 1] + count_ < 0:
            if a_t[i] + count_ < 0:
                ans_ += -(a_t[i] + count_) + 1
                count_ += -(a_t[i] + count_) + 1
            if a_t[i] + count_ == 0:
                ans_ += 1
                count_ += 1
print(min(ans, ans_))

Submission status: Accepted

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

def calcOperation(base):
  sum = 0
  ans = 0
  sign = base
  for i in range(n):
    sum += a[i]
    if sum * sign >= 0:
      ans += abs(sum) + 1
      sum = -sign
    sign *= -1
  return ans

posi = calcOperation(1)
nega = calcOperation(-1)
print(min(posi, nega))

Submission status: Accepted

Solution code:
n = int(input())
A = [int(a) for a in input().split()]
ans1,ans2 = 0,0
total1,total2 = 0,0

for i in range(n):
    total1 += A[i]
    if i % 2== 0 and total1 <= 0:
        diff = abs(total1) + 1
        total1 += diff
        ans1 += diff
    elif i % 2 == 1 and total1 >= 0:
        diff = abs(total1) + 1
        total1 -= diff
        ans1 += diff
        
for i in range(n):
    total2 += A[i]
    if i % 2== 1 and total2 <= 0:
        diff = abs(total2) + 1
        total2 += diff
        ans2 += diff
    elif i % 2 == 0 and total2 >= 0:
        diff = abs(total2) + 1
        total2 -= diff
        ans2 += diff
        
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = map(int,input().split())|	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = list(map(int,input().split()))	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

import collections
def inpl(): return list(map(int, input().split()))

#### START
n = int(input())
a = list(map(int,input().split()))

ans = 1e16
for s in [1, -1]:
  res, csum = 0, 0
  for a_i in a:
    csum += a_i
    if csum * s <= 0:
      res += abs(csum-s)
      csum = s
    s *= -1
  ans = min(ans, res)

print(ans)

Submission status: Accepted

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

# evens are plus
ans1 = 0
subtotal = 0
for i in range(n):
    subtotal += A[i]
    if i % 2 == 0 and subtotal <= 0:
        diff = abs(subtotal) + 1
        subtotal += diff
        ans1 += diff
    elif i % 2 == 1 and subtotal >= 0:
        diff = abs(subtotal) + 1
        subtotal -= diff
        ans1 += diff

# odds are plus
ans2 = 0
subtotal = 0
for i in range(n):
    subtotal += A[i]
    if i % 2 == 1 and subtotal <= 0:
        diff = abs(subtotal) + 1
        subtotal += diff
        ans2 += diff
    elif i % 2 == 0 and subtotal >= 0:
        diff = abs(subtotal) + 1
        subtotal -= diff
        ans2 += diff

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
def psolve(positive, As):
    i = 0
    ans = 0
    for a in As:
        if positive:
            if i + a <= 0:
                ans += abs(1 - (i + a))
                a += 1 - (i + a)
        else:
            if i + a >= 0:
                ans += abs(-1 - (i + a))
                a += -1 - (i + a)
        i += a
        positive = not positive
    return ans


def solve(N, As):
    return min(psolve(True, As), psolve(False, As))


if __name__ == "__main__":
    n = int(input())
    As = list(map(int, input().split(" ")))
    print(solve(n, As))

Submission status: Accepted

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


def cumsum(a):
    c= [0] * len(a)
    c[0] = a[0]
    for i in range(1, len(a)):
        c[i] = a[i] + c[i-1]
    return c


def calc(a, signs):
    s = cumsum(a)

    op = 0
    ans = 0
    for i, sig in enumerate(signs):
        s[i] = s[i] + op
        mysign = sign(s[i])

        if mysign != sig:
            delta = sig*(abs(s[i]) + 1)
            op += delta
            s[i] += delta
            ans += abs(delta)
    return ans


def solve():
    N = int(input())
    a = [int(x) for x in input().split()]

    signs1 = [1] * N
    for i in range(N):
        if i%2==0:
            signs1[i] = -1
    signs2 = list(map(lambda x: -x, signs1))

    return min(calc(a, signs1), calc(a, signs2))


def main():
    print(solve())


if __name__ == '__main__':
    main()

Submission status: Accepted

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

cnt = 0
sgn = 1
s = 0
for _a in a:
    if (s + _a) * sgn <= 0:
        cnt += abs(_a + s - sgn)
        s = sgn
    else:
        s += _a
    sgn = -sgn

ans = cnt

cnt = 0
sgn = -1
s = 0
for _a in a:
    if (s + _a) * sgn <= 0:
        cnt += abs(_a + s - sgn)
        s = sgn
    else:
        s += _a
    sgn = -sgn

ans = min(ans, cnt)

print(ans)

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(A[i] + B[i-1])

cnt1, cum1 = 0, 0
for i in range(N):
    if i%2 == 0:
        if B[i] + cum1 > 0:
            pass
        else:
            d = 1 - (B[i] + cum1)
            cum1 += d
            cnt1 += abs(d)
    else:
        if B[i] + cum1 < 0:
            pass
        else:
            d = -1 - (B[i] + cum1)
            cum1 += d
            cnt1 += abs(d)

cnt2, cum2 = 0, 0
for i in range(N):
    if i%2 == 0:
        if B[i] + cum2 < 0:
            pass
        else:
            d = -1 - (B[i] + cum2)
            cum2 += d
            cnt2 += abs(d)
    else:
        if B[i] + cum2 > 0:
            pass
        else:
            d = 1 - (B[i] + cum2)
            cum2 += d
            cnt2 += abs(d)

print(min(cnt1, cnt2))

Submission status: Accepted

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

def f(A, op, acc = 0, cnt = 0):
    for i in range(n):
        acc += A[i]
        
        if i % 2 == 0 and op * acc <= 0:
            cnt += - op * acc + 1
            acc = op
                
        if i % 2 == 1and op * acc >= 0:
            cnt += op * acc + 1
            acc = -op
                
    if acc == 0:
        cnt += 1
        
    return cnt

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

Submission status: Accepted

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

def f(A, op, acc = 0, cnt = 0):
    for i in range(n):
        acc += A[i]
        
        if i % 2 == 0 and acc * op >= 0:
            cnt += op * acc + 1
            acc = - op
            
        if i % 2 == 1 and acc * op <= 0:
            cnt += - op * acc + 1
            acc = op
            
    if acc == 0:
        cnt += 1
        
    return cnt
        
print(min(f(A, -1), f(A, 1)))

Submission status: Accepted

Solution code:
def func(s, li):
    ans = 0
    for a in li:
        if s < 0 and s + a <= 0:
            ans += -(s + a) + 1
            s = 1
        elif s > 0 and s + a >= 0:
            ans += (s + a) + 1
            s = -1
        else:
            s = s + a
    return ans

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

ans0 = 0
s = li[0]
if s >= 0:
    ans0 += s + 1
    s = -1
ans0 += func(s, li[1:])
ans1 = 0
s = li[0]
if s <= 0:
    ans1 += -s + 1
    s = 1
ans1 += func(s, li[1:])

print(min(ans0, ans1))

Submission status: Accepted

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

ans1 = 0
s = A[0]
if s >= 0:
    ans1 = 1+s
    s = -1
for i in range(1, N):
    s += A[i]
    if i % 2 != 0:
        if s <= 0:
            ans1 += 1-s
            s = 1
    else:
        if s >= 0:
            ans1 -= -1-s
            s = -1

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(int(i) for i in input().split())  
ctplus = 0
ctminu = 0
checksum = 0
for i in range(n):
  checksum += a[i]
  if i%2 ==0:
    if checksum <= 0:
      ctplus += abs(checksum)+1
      checksum += abs(checksum)+1
  else:
    if checksum >= 0:
      ctplus += abs(checksum)+1
      checksum -= (abs(checksum)+1)
checksum = 0
for j in range(n):
  checksum += a[j]
  if j%2 ==1:
    if checksum <= 0:
      ctminu += abs(checksum)+1
      checksum += abs(checksum)+1
  else:
    if checksum >= 0:
      ctminu += abs(checksum)+1
      checksum -= (abs(checksum)+1)      
print(min(ctplus,ctminu))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):

    return [il(t) for _ in range(N)]


N = ii()
A = il()
ans = float("inf")
for sign in (1, -1):
    sum_a = 0
    c = 0
    for a in A:
        sum_a += a
        if sum_a * sign <= 0:
            # +1になるまで加算
            c += abs(sum_a) + 1
            # 総和は1or-1のため
            sum_a = sign
        sign *= -1
    ans = min(ans, c)
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):

    return [il(t) for _ in range(N)]


N = ii()
A = il()
ans = float("inf")
for sign in (1, -1):
    sum_a = 0
    c = 0
    for i in range(N):
        sum_a += A[i]
        if sum_a * sign <= 0:
            # +1になるまで加算
            c += abs(sum_a) + 1
            # 総和は1or-1のため
            sum_a = sign
        sign *= -1
    ans = min(ans, c)
print(ans)

Submission status: Accepted

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

ans1,ans2 = 0,0
res = 0
for i in range(N):
    res += A[i]
    if i%2==1:
        if res >= 0:
            ans1 += res+1
            res = -1
    else:
        if res <= 0:
            ans1 -= res-1
            res = 1

res = 0
for i in range(N):
    res += A[i]
    if i%2==1:
        if res <= 0:
            ans2 -= res-1
            res = 1
    else:
        if res >= 0:
            ans2 += res+1
            res = -1

print(min(ans1,ans2))

Submission status: Accepted

Solution code:
def solve(asum, a, ans):
    for i in range(1, n):
        # print(asum)
        asumplus = asum + a[i]
        if asumplus == 0 and asum > 0:
            asumplus -= 1
            ans += 1
        elif asumplus == 0 and asum < 0:
            asumplus -= 1
            ans += 1
        elif asumplus > 0 and asum > 0:
            ans += asumplus + 1
            asumplus = -1
        elif asumplus < 0 and asum < 0:
            ans += -asumplus + 1
            asumplus = 1
        asum = asumplus
    return ans

n = int(input())
a = list(map(int,input().split()))
asum = a[0]
if asum > 0:
    ans = min(solve(asum,a,0),solve(-1,a,asum+1))
elif asum < 0:
    ans = min(solve(asum,a,0),solve(1,a,1-asum))
else:
    ans = min(solve(1,a,1),solve(-1,a,1))
print(ans)

Submission status: Accepted

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

if a[0]>=0:
    f=1
else:
    f=-1

s=0
ans=0
for i in range(n):
    s+=a[i]
    if s*f<0:
        ans+=abs(s)+1
        s=f
    elif s==0:
        ans+=1
        s=f
    f*=-1


if a[0]>=0:
    f=-1
else:
    f=1
s=0
ans2=0
for i in range(n):
    s+=a[i]
    if s*f<0:
        ans2+=abs(s)+1
        s=f
    elif s==0:
        ans2+=1
        s=f
    f*=-1
    
print(min(ans,ans2))

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
n = int(input())
m = list(map(int, input().split()))

def solve(isPosi):
    ans = 0
    sm = 0
    
    for a in m:
        if isPosi:
            sm += a
            if sm >= 0:
                ans += (1+sm)
                sm = -1
            isPosi = False
        
        else:
            sm += a
            if sm <= 0:
                ans += (1-sm)
                sm = 1
            isPosi = True
    
    return ans

ans_p = solve(True)
ans_n = solve(False)

print(min(ans_p, ans_n))

Submission status: Accepted

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

ans1 = 0
ans2 = 0
a1 = a[:]
a2 = a[:]
num1 = 0
num2 = 0

for i in range(n):
    if i % 2 == 0:
        if num1 + a1[i] > 0:
            num1 += a1[i]

        else:
            ans1 += abs(a1[i] - (1 - num1))
            a1[i] = 1 - num1
            num1 = 1

        if num2 + a2[i] < 0:
            num2 += a2[i]

        else:
            ans2 += abs(a2[i] + 1 + num2)
            a2[i] = -1 -num2
            num2 = -1

    else:
        if num1 + a1[i] < 0:
            num1 += a1[i]

        else:
            ans1 += abs(a1[i] + 1 + num1)
            a1[i] = -1 -num1
            num1 = -1

        if num2 + a2[i]  > 0:
            num2 += a2[i]
        else:
            ans2 += abs(a2[i] -1 + num2)
            a2[i] = 1 - num2
            num2 = 1

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
S1,S2=[],[]
if A[0]>0:
  S1.append(A[0])
  S2.append(-1)
  a1,a2=0,A[0]+1
elif A[0]==0:
  S1.append(1)
  S2.append(-1)
  a1,a2=1,1
else:
  S1.append(1)
  S2.append(A[0])
  a1,a2=-A[0]+1,0
for i in range(1,N):
  if S1[i-1]>0:
    if S1[i-1]+A[i]<0:
      S1.append(S1[i-1]+A[i])
    elif S1[i-1]+A[i]>=0:
      S1.append(-1)
      a1+=S1[i-1]+A[i]+1
  elif S1[i-1]<0:
    if S1[i-1]+A[i]>0:
      S1.append(S1[i-1]+A[i])
    elif S1[i-1]+A[i]<=0:
      S1.append(1)
      a1+=-S1[i-1]-A[i]+1
  if S2[i-1]>0:
    if S2[i-1]+A[i]<0:
      S2.append(S2[i-1]+A[i])
    elif S2[i-1]+A[i]>=0:
      S2.append(-1)
      a2+=S2[i-1]+A[i]+1
  elif S2[i-1]<0:
    if S2[i-1]+A[i]>0:
      S2.append(S2[i-1]+A[i])
    elif S2[i-1]+A[i]<=0:
      S2.append(1)
      a2+=-S2[i-1]-A[i]+1
print(min(a1,a2))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[]
for i in range(n):
  b.append(a[i])

ct1=0
if a[0]<=0:
  ct1+=1-a[0]
  a[0]=1
x=a[0]
for i in range(1,n):
  y=x+a[i]
  if i%2==1:
    if y>=0:
      ct1+=y+1
      a[i]=-x-1
  else:
    if y<=0:
      ct1+=1-y
      a[i]=-x+1
  x+=a[i]

ct2=0
if b[0]>=0:
  ct2+=b[0]+1
  b[0]=-1
x=b[0]
for i in range(1,n):
  y=x+b[i]
  if i%2==0:
    if y>=0:
      ct2+=y+1
      b[i]=-x-1
  else:
    if y<=0:
      ct2+=1-y
      b[i]=-x+1
  x+=b[i]

print(min(ct1,ct2))

Submission status: Accepted

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

sum = a[0]
ans1 = 0
if sum <= 0:
	sum = 1
	ans1 += abs(a[0]-1)

for i in range(1, n):
	nextsum = sum + a[i]
	if (sum>0 and nextsum<0) or (sum<0 and nextsum>0):
		sum = nextsum
	elif sum>0:
		ans1 += abs(nextsum+1)
		sum = -1
	elif sum<0:
		ans1 += abs(nextsum-1)
		sum = 1


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

for i in range(1, n):
	nextsum = sum + a[i]
	if (sum>0 and nextsum<0) or (sum<0 and nextsum>0):
		sum = nextsum
	elif sum>0:
		ans2 += abs(nextsum+1)
		sum = -1
	elif sum<0:
		ans2 += abs(nextsum-1)
		sum = 1

print(min(ans1, ans2))

Submission status: Accepted

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

ans1=0
ans2=0

#最初の符号が正の場合
sign=1
total=0
for i in range(n):
  total+= A[i]
  if total*sign<=0:   #符号が正しいかを判定（正が欲しい時に正とかけたら正、逆も然り）
    k=abs(total-sign)
    ans1+=k           #理想の値との差分
    total=sign        #暫定累積値を理想の値に変更
  sign*=-1            #一回ごとに符号を変更

#最初の符号が負の場合
sign=-1
total=0
for i in range(n):
  total+=A[i]
  if total*sign<=0:
    k=abs(total-sign)
    ans2+=k
    total=sign
  sign*=-1
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    aa = list(map(int, input().split()))

    s = 0
    res1 = 0
    for i in range(n):
        s += aa[i]
        if i % 2 == 0:
            if s <= 0:
                res1 += abs(s) + 1
                s = 1
        else:
            if s >= 0:
                res1 += abs(s) + 1
                s = -1

    s = 0
    res2 = 0
    for i in range(n):
        s += aa[i]
        if i % 2 == 0:
            if s >= 0:
                res2 += abs(s) + 1
                s = -1
        else:
            if s <= 0:
                res2 += abs(s) + 1
                s = 1

    print(min(res1, res2))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
def func(list1,op,acc = 0,count = 0):
    for i in range(n):
        acc += list1[i]
        if i % 2 == 1 and acc * op >= 0:
            count += op * acc + 1
            acc = -op
        elif i % 2 == 0 and acc * op <= 0:
            count += -op * acc + 1
            acc = op
    if acc == 0:
        count += 1
    return count
ans = min(func(A,1),func(A,-1))
print(ans)

Submission status: Accepted

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

# a[0]の符号を正にするか負にするかの2択で
# 後は一意に決まる

_sum = 0
count = 0
out = 0
# a[0]の符号を正にする場合
for i in range(len(a)):
    if i % 2 == 1:  # a[i]までの和を負にする
        if _sum + a[i] >= 0:
            count += abs(_sum + a[i] - (-1))
            _sum = -1
        else:
            _sum += a[i]
    else:  # a[i]までの和を正にする
        if _sum + a[i] <= 0:
            count += abs(1 - (_sum + a[i]))
            _sum = 1
        else:
            _sum += a[i]
# print("count:{} sum:{}".format(count, _sum))
out = count
_sum = 0
count = 0
# a[0]の符号を負にする場合
for i in range(len(a)):
    if i % 2 == 0:  # a[i]までの和を負にする
        if _sum + a[i] >= 0:
            count += abs(_sum + a[i] - (-1))
            _sum = -1
        else:
            _sum += a[i]
    else:  # a[i]までの和を正にする
        if _sum + a[i] <= 0:
            count += abs(1 - (_sum + a[i]))
            _sum = 1
        else:
            _sum += a[i]
            # print("count:{} sum:{}".format(count, _sum))
print(min(out, count))

Submission status: Accepted

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

m = 0
c = 0

for i in range(n):
    m += a[i]

    if i % 2 == 0:
        if m <= 0:
            c += abs(m) + 1
            m += abs(m) + 1
    else:
        if m >= 0:
            c += m + 1
            m -= m + 1

m = 0
d = 0

for i in range(n):
    m += a[i]

    if i % 2 == 1:
        if m <= 0:
            d += abs(m) + 1
            m += abs(m) + 1
    else:
        if m >= 0:
            d += m + 1
            m -= m + 1

print(min(c, d))

Submission status: Accepted

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

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

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

print(min(c, d))

Submission status: Accepted

Solution code:
# encoding: utf-8
n = int(input())
a = list(map(int, input().split()))

anss = [0] * 2
for init in range(2):
    ans = 0
    state = init
    sum_tmp = 0
    for i, ai in enumerate(a):
        ans_tmp = 0
        # 0
        if i == 0:
            if state == 1 and ai <= 0: ans_tmp += (-ai + 1)
            elif state == 0 and ai >= 0: ans_tmp += (ai + 1)
            sum_tmp = ai + ans_tmp if state == 1 else ai - ans_tmp
            ans += ans_tmp
            continue
        # > 1
        state = 1 - state
        ans_tmp = 0
        if state == 1:
            sum_tmp += ai
            if sum_tmp <= 0: ans_tmp += (-sum_tmp + 1) # 1
            sum_tmp += ans_tmp
        else:
            sum_tmp += ai
            if sum_tmp >= 0: ans_tmp += (sum_tmp + 1) # -1
            sum_tmp -= ans_tmp
        ans += ans_tmp
    anss[init] = ans

print(min(anss))

Submission status: Accepted

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

s = 0 # 累積和
ans1 = 0
# plus-minus-plus...
for i in range(n):
    s += a[i]
    if i%2==0:
        if s>0: continue
        else: 
            ans1 += abs(s-1)
            s = 1
    else: 
        if s<0: continue
        else: 
            ans1 += abs(s-(-1))
            s = -1

s = 0
ans2 = 0
# minus-plus...
for i in range(n):
    s += a[i]
    if i%2==1:
        if s>0: continue
        else: 
            ans2 += abs(s-1)
            s = 1
    else: 
        if s<0: continue
        else: 
            ans2 += abs(s-(-1))
            s = -1
            
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
def op(sign, n, a):
    if sign * a[0] <= 0:
        count = abs(a[0]) + 1
        S = sign
    else:
        count = 0
        S = a[0]

    for i in a[1:]:
        if S * (S + i) > 0:
            count += abs(S + i) + 1
            S = -1 if S > 0 else 1
        elif S + i == 0:
            count += 1
            S = -1 if S > 0 else 1
        else:
            S += i

    return count


def main():
    n = int(input())
    a = list(map(int, input().split()))
    c1 = op(1, n, a)
    c2 = op(-1, n, a)
    print(min(c1, c2))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans1 = 0
ans2 = 0

tmp = 0
for i in range(N): # even -> positive
    a = A[i]
    tmp += a
    if i % 2 == 0:
        if tmp <= 0:
            ans1 += abs(tmp) + 1
            tmp = 1
    else:
        if tmp >= 0:
            ans1 += abs(tmp) + 1
            tmp = -1

tmp = 0
for i in range(N): # even -> positive
    a = A[i]
    tmp += a
    if i % 2 != 0:
        if tmp <= 0:
            ans2 += abs(tmp) + 1
            tmp = 1
    else:
        if tmp >= 0:
            ans2 += abs(tmp) + 1
            tmp = -1

ans = min(ans1,ans2)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

#positive
pos_count = 0
sign = 1
cumsum = 0
for a in A:
    cumsum += a
    if cumsum * sign <= 0:
        pos_count += abs(sign - cumsum)
        cumsum = sign
    sign *= -1

#negative
neg_count = 0
sign = -1
cumsum = 0
for a in A:
    cumsum += a
    if cumsum * sign <= 0:
        neg_count += abs(sign - cumsum)
        cumsum = sign
    sign *= -1

print(min(pos_count, neg_count))

Submission status: Accepted

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

ansl = [0, 0]
s = [0, 0]

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

print(min(ansl))

Submission status: Accepted

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

result = []

for i in range(2):
    num = 0
    r = 0
    for j in range(len(a)):
        num += a[j]
        if (j + i) % 2 == 0:
            if num <= 0:
                r -= num - 1
                num = 1

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

    result.append(r)

print(min(result))

Submission status: Accepted

Solution code:
n = int(input())
a = input().split()
a = [int(m) for m in a]
q = []
k = 0
kco = 0
#+-
for i in range(n):
    if i == 0:
        if a[i] > 0:
            q.append(a[i])
        else:
            q.append(1)
            kco += 1 - a[i]
        k += q[i]
    else:
        k += a[i]
        if i % 2 != 0:
            if k < 0:
                q.append(a[i])
            else:
                q.append(a[i]-k-1)
                kco += k + 1
            k += q[i] - a[i]
        if i % 2 == 0:
            if k > 0:
                q.append(a[i])
            else:
                q.append(a[i]-k+1)
                kco += -k + 1
            k += q[i] - a[i]
xco = kco
q = []
k = 0
kco = 0
#-+
for i in range(n):
    if i == 0:
        if a[i] < 0:
            q.append(a[i])
        else:
            q.append(-1)
            kco += a[i] + 1
        k += q[i]
    else:
        k += a[i]
        if i % 2 == 0:
            if k < 0:
                q.append(a[i])
            else:
                q.append(a[i]-k-1)
                kco += k + 1
            k += q[i] - a[i]
        if i % 2 != 0:
            if k > 0:
                q.append(a[i])
            else:
                q.append(a[i]-k+1)
                kco += -k + 1
            k += q[i] - a[i]
yco = kco
print(min(xco, yco))

Submission status: Accepted

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
 
def pura(a):
    A = a[:]
    ans = 0
    check = 0
    if A[0] <= 0:
        ans += 1 + abs(A[0])
        A[0] = 1
        
    check += A[0]
    
    for i in range(1, N):
        if i % 2 != 0:
            if check + A[i] >= 0:
                ans += abs(A[i] + 1 + check)
                A[i] = -1 - check
        else:
            if check + A[i] <= 0:
                ans += abs(A[i] - (1 - check))
                A[i] = 1 - check
        check += A[i]
    return ans
 
def mai(a):
    A = a[:]
    ans = 0
    check = 0
    if A[0] >= 0:
        ans +=1 + abs(A[0])
        A[0] = -1
        
    check += A[0]
    
    for i in range(1, N):
        if i % 2 != 0:
            if check + A[i] <= 0:
                ans += abs(A[i] - (1 - check))
                A[i] = 1 - check
        else:
            if check + A[i] >= 0:
                ans += abs(A[i] +( 1 + check))
                A[i] = -1 - check
        check += A[i]
    return ans
print(min(pura(a), mai(a)))