Problem:


Score : 300 points

Problem Statement

You are given an integer sequence of length N. The i-th term in the sequence is a_i.
In one operation, you can select a term and either increment or decrement it by one.

At least how many operations are necessary to satisfy the following conditions?

For every i (1≤i≤n), the sum of the terms from the 1-st through i-th term is not zero.

For every i (1≤i≤n-1), the sign of the sum of the terms from the 1-st through i-th term, is different from the sign of the sum of the terms from the 1-st through (i+1)-th term.

Constraints

2 ≤ n ≤ 10^5

|a_i| ≤ 10^9

Each a_i is an integer.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print the minimum necessary count of operations.

Sample Input 1

4
1 -3 1 0

Sample Output 1

4

For example, the given sequence can be transformed into 1, -2, 2, -2 by four operations. The sums of the first one, two, three and four terms are 1, -1, 1 and -1, respectively, which satisfy the conditions.

Sample Input 2

5
3 -6 4 -5 7

Sample Output 2

0

The given sequence already satisfies the conditions.

Sample Input 3

6
-1 4 3 2 -5 4

Sample Output 3

8



Sample input:
4
1 -3 1 0


Sample output:
4

Submission status: Wrong Answer

Solution code:
import copy
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
ans1, ans2 = 0, 0
f = a[0]
if f == 0:
    f = 1
    ans1 += 1
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 = -a[0]
if f == 0:
    f = -1
    ans2 += 1
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: Wrong Answer

Solution code:
import copy
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
ans1, ans2 = 0, 0
f = a[0]
if f == 0:
    f = 1
    ans1 += 1
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 = -a[0]
if f == 0:
    f = -1
    ans1 += 1
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: Wrong Answer

Solution code:
import copy
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
ans1, ans2 = 0, 0
f = 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 = -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: Wrong Answer

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
  p[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: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
#具体的な操作を考える必要はなくて，ただ部分和をひたすら考えていけばいい
answer1 = 0
answer2 = 0
sum1 = a[0]
sum2 = a[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: Wrong Answer

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

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

    for i, a in enumerate(A[1:]):
        s1, s2 = s1 + a, s2 + a
        if i % 2:  # 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: Wrong Answer

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

    for i, a in enumerate(A[1:]):
        s1, s2 = s1 + a, s2 + a
        if i % 2:  # 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(s1, s2)


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

Submission status: Wrong Answer

Solution code:
def sequence(N: int, A: list) -> int:
    def count_op(s: int) -> int:
        op = 0

        if s * A[0] > 0:
            s = A[0]
        else:
            s = s / abs(s)
            op = abs(1 - s)

        for a in A[1:]:
            if s < 0:
                if s + a > 0:
                    # OK
                    s = s + a
                    continue
                else:
                    op += 1 - (s + a)
                    s = 1
            else:  # s > 0
                if s + a < 0:
                    # OK
                    s = s + a
                    continue
                else:
                    op += (s + a) - (-1)
                    s = -1
        return op

    return min(count_op(1), count_op(-1))


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

Submission status: Wrong Answer

Solution code:
def sequence(N: int, A: list) -> int:
    def count_op(s: int) -> int:
        s = (s // abs(s)) * A[0]
        op = 0
        for a in A[1:]:
            if s < 0:
                if s + a > 0:
                    # OK
                    s = s + a
                    continue
                else:
                    op += 1 - (s + a)
                    s = 1
            else:  # s > 0
                if s + a < 0:
                    # OK
                    s = s + a
                    continue
                else:
                    op += (s + a) - (-1)
                    s = -1
        return op

    return min(count_op(1), count_op(-1))


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

Submission status: Wrong Answer

Solution code:
def sequence(N: int, A: list) -> int:
    s = A[0]
    op = 0

    for a in A[1:]:
        if s < 0:
            if s + a > 0:
                # OK
                s = s + a
                continue
            else:
                op += 1 - (s + a)
                s = 1
        else:  # s > 0
            if s + a < 0:
                # OK
                s = s + a
                continue
            else:
                op += (s + a) - (-1)
                s = -1

    return op


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

Submission status: Wrong Answer

Solution code:
def sequence(N: int, A: list) -> int:
    s = A[0]
    op = 0

    for a in A[1:]:
        print(s, '->', s+a)
        if s < 0:
            if s + a > 0:
                # OK
                s = s + a
                continue
            else:
                op += 1 - (s + a)
                s = 1
        else:  # s > 0
            if s + a < 0:
                # OK
                s = s + a
                continue
            else:
                op += (s + a) - (-1)
                s = -1

    return op


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

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())
a = [int(_) for _ in input().split()]
ans = 0
zeroflag = True
zerocnt = 0
tmp = 0

for i in a:
    if zeroflag and i == 0:
        zerocnt += 1
    elif zeroflag and i != 0:
        zeroflag = False
        if zerocnt != 0 and abs(i) == 1:
            ans += 1    
            tmp += np.sign(i)*2
        elif zerocnt != 0:
            tmp = i - np.sign(i)
        else:
            tmp = i
    else:
        if np.sign(tmp+i) == np.sign(tmp) or np.sign(tmp+i) == 0:
            ans += abs((tmp+i) + np.sign(tmp))
            tmp = -np.sign(tmp)
        else:
            tmp += i

if zerocnt != 0:
    ans += 2*zerocnt - 1

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())
a = [int(_) for _ in input().split()]
ans = 0
zeroflag = True
zerocnt = 0
tmp = 0

for i in a:
    if zeroflag and i == 0:
        zerocnt += 1
    elif zeroflag and i != 0:
        zeroflag = False
        if zerocnt != 0 and abs(i) == 1:
            ans += 1    
            tmp += np.sign(i)*2
        else:
            tmp = i
    else:
        if np.sign(tmp+i) == np.sign(tmp) or np.sign(tmp+i) == 0:
            ans += abs((tmp+i) + np.sign(tmp))
            tmp = -np.sign(tmp)
        else:
            tmp += i

if zerocnt != 0:
    ans += 2*zerocnt - 1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
trueans = 1000000000000
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: Wrong Answer

Solution code:
import numpy as np
from copy import deepcopy
n = int(input())
a = list(map(int, input().split()))
c = [0] * n
for i in range(n):
    c[i] = c[i - 1] + a[i]
c = np.array(c)
ans1, ans2 = 0, 0
tmp = deepcopy(c)
for i in range(n):
    t = tmp[i]
    if i % 2 == 0 and tmp[i] >= 0:
        tmp -= t + 1
        ans1 += t + 1
    elif i % 2 == 1 and tmp[i] <= 0:
        tmp += -t + 1
        ans1 += -t + 1
tmp = deepcopy(c)
for i in range(n):
    t = c[i]
    if i % 2 == 1 and tmp[i] >= 0:
        tmp -= t + 1
        ans2 += t + 1
    elif i % 2 == 0 and tmp[i] <= 0:
        tmp += -t + 1
        ans2 += -t + 1
print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n = int(input())
a = [int(i) for i in input().split()]
ans = 0
da = 0
if a[0] == 0:
    ans += 1
    if a[1] >= 0:
        a[0] = -1
        da += -1
    else:
        a[0] = 1
        da += 1
b = list(accumulate(a))
for i, (x, y) in enumerate(zip(b[:-1], b[1:])):
    if (x+da)*(y+da) < 0:
        continue
    else:
        y += da
        ans += abs(y)+1
        if y>=0:
            da += -1-y
        else:
            da += 1-y
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n = int(input())
a = [int(i) for i in input().split()]
ans = 0
if a[0] == 0:
    ans += 1
    if a[1] >= 0:
        a[0] = -1
    else:
        a[0] = 1
da = 0
b = list(accumulate(a))
for i, (x, y) in enumerate(zip(b[:-1], b[1:])):
    if (x+da)*(y+da) < 0:
        continue
    else:
        y += da
        ans += abs(y)+1
        if y>=0:
            da += -1-y
        else:
            da += 1-y
print(ans)

Submission status: Wrong Answer

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

sum = a[0]
count = 0

for i in range(1, n) :

    if sum == 0 :
        count += 1
        if a[i] > 0 :
            sum = -1
        elif a[i] < 0 :
            sum = 1

    temp = sum
    sum += a[i]
    if temp > 0 and sum > 0 :
        count += sum + 1
        sum = -1
    elif temp < 0 and sum < 0 :
        count += -sum + 1
        sum = 1

if sum == 0 :
    count += 1

print(count)

Submission status: Wrong Answer

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

sum = a[0]
count = 0

for i in range(1, n) :

    if sum == 0 :
        count += 1
        if a[i] > 0 :
            sum = -1
        elif a[i] > 0 :
            sum = 1

    temp = sum
    sum += a[i]
    if temp > 0 and sum > 0 :
        count += sum + 1
        sum = -1
    elif temp < 0 and sum < 0 :
        count += -sum + 1
        sum = 1

if sum == 0 :
    count += 1

print(count)

Submission status: Wrong Answer

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

sum = a[0]
count = 0

for i in range(1, n) :
    temp = sum
    sum += a[i]
    if temp > 0 and sum > 0 :
        count += sum + 1
        sum = -1
    elif temp < 0 and sum < 0 :
        count += -sum + 1
        sum = 1

if sum == 0 :
    count += 1

print(count)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
n = int(input())
a_list = list(map(int, input().split()))
cnt = 0
a_sum = a_list[0]
if a_sum > 0:
    flag = 1
elif a_sum < 0:
    flag = -1
else:
    flag = 0
    cnt += 1
for a in a_list[1:]:
    if flag == 0:
        if a > 0:
            flag = 1
            a_sum = a - 1
            if a == 1:
                cnt += 1
        elif a < 0:
            flag = -1
            a_sum = a + 1
            if a == -1:
                cnt += 1
        else:
            flag = 0
            cnt += 2
    else:
        a_sum += a
        if flag == 1:
            if a_sum >= 0:
                cnt += a_sum+1
                a_sum = -1
            flag = -1
        else:
            if a_sum <= 0:
                cnt += abs(a_sum)+1
                a_sum = 1
            flag = 1
print(cnt)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline
n = int(input())
a_list = list(map(int, input().split()))
cnt = 0
a_sum = a_list[0]
if a_sum > 0:
    flag = 1
elif a_sum == 0:
    flag = 1
    cnt += 1
else:
    flag = -1
for a in a_list[1:]:
    a_sum += a
    if flag == 1:
        if a_sum >= 0:
            cnt += a_sum+1
            a_sum = -1
        flag = -1
    else:
        if a_sum <= 0:
            cnt += abs(a_sum)+1
            a_sum = 1
        flag = 1
print(cnt)

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
now=A[0]
ans=0
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]
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A2 = 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 j in range(2):
	A = list(A2)
	count = 0
	sumN = A[0]
	beforeSign = getSign(A[0])
	
	if j == 0:
		add = -A[0] - beforeSign
		A[0] += add
		count += abs(add)
		sumN = A[0]
		beforeSign = getSign(A[0])
	
	for i in range(1,N):
		sumN += A[i]

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A2 = 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 j in range(2):
	A = list(A2)
	count = 0
	sumN = A[0]
	beforeSign = getSign(A[0])
	
	if j == 0:
		add = -A[0] - beforeSign
		A[0] += add
		count += abs(add)
	
	for i in range(1,N):
		sumN += A[i]

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A2 = 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 j in range(2):
	A = list(A2)
	count = 0
	sumN = A[0]
	beforeSign = getSign(A[0])
	
	if j == 0:
		add = -A[j] - getSign(A[j])
		A[j] += add
		count += abs(add)
	
	for i in range(1,N):
		sumN += A[i]

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

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

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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 = [int(input_raw[i]) for i in range(N)]
    s = 0
    count = 0
    pre_sign = 0
    if A[0] != 0:
        count = calc(A, N)
    else:
        A[0] = -1
        count_minus =calc(A, N) + 1
        A[0] = 1
        count_plus =calc(A, N) + 1
        count = min(count_minus, count_plus)


    print count




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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
        if 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 = [int(input_raw[i]) for i in range(N)]
    s = 0
    count = 0
    pre_sign = 0
    if A[0] != 0:
        count = calc(A, N)
    else:
        A[0] = -1
        count_minus =calc(A, N) + 1
        A[0] = 1
        count_plus =calc(A, N) + 1
        count = min(count_minus, count_plus)


    print count




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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 = 0
    count = 0
    pre_sign = 0
    for i in range(N):
        s += A[i]
        if s == 0:
            if pre_sign == 1:
                s -= 1
            elif pre_sign == -1:
                s += 1
            count += 1
        if pre_sign == s/abs(s):
            if s < 0:
                ope = 1 - s
            else:
                ope = -1 - 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 = [int(input_raw[i]) for i in range(N)]
    s = 0
    count = 0
    pre_sign = 0
    if A[0] != 0:
        count = calc(A, N)
    else:
        A[0] = -1
        count_minus =calc(A, N) + 1
        A[0] = 1
        count_plus =calc(A, N) + 1
        count = min(count_minus, count_plus)


    print count




if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
a = list(map(int,input().split()))
sum_tmp = a[0]
ans = 0

if sum_tmp > 0:
    target = -1
elif sum_tmp < 0:
    target = 1
else:
    sum_tmp += 1
    ans += 1
    target = -1

for i in range(1,n):
    sum_tmp += a[i]
    if target == -1:
        if sum_tmp > -1:
            diff = sum_tmp + 1
            ans += diff
            sum_tmp -= diff
            target = 1
        else:
            target = 1
    else:
        if sum_tmp < 1:
            diff = 1 - sum_tmp
            ans += diff
            sum_tmp += diff
            target = -1
        else:
            target = -1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

b = [a[0]]
su = 0
for i in range(n-1):
    b.append(b[-1]+a[1+i])
    if b[-1] * b[-2] > 0:
        su += abs(b[-1]) + 1
        if b[-2] > 0:
            b[-1] = -1
        else:
            b[-1] = 1
    else:
        if b[-1] == 0:
            su += 1
            if b[-2] > 0:
                b[-1] = -1
            else:
                b[-1] = 1
print(su)

Submission status: Wrong Answer

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()
ans = 0
k = a[0]
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]
print(ans)
#D

#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Wrong Answer

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 += cnt
    if i % 2 == 1 and accum >= 0:
        cnt += accum - (-1)
        accum -= cnt
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 += cnt
    if i % 2 == 0 and accum >= 0:
        cnt += accum - (-1)
        accum -= cnt
ans = min(ans,cnt)
print(ans)

Submission status: Wrong Answer

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

ans = 0
cnt = 0
if A[0] < 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
else:
    for i in range(n):
        if i % 2 == 1:
            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   
                
print(ans)

Submission status: Wrong Answer

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
    B[0] -= 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: Wrong Answer

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
    B[0] -= 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")
    #条件を満たしていなければREを吐く
    if sumA[i]*sumA[i-1] >= 0 or sumB[i]*sumB[i-1] >= 0:
        re = []
        re[1] = "fuck"

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