Problem:


Score : 300 points

Problem Statement

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

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

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

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

Constraints

2 ≤ n ≤ 10^5

|a_i| ≤ 10^9

Each a_i is an integer.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print the minimum necessary count of operations.

Sample Input 1

4
1 -3 1 0

Sample Output 1

4

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

Sample Input 2

5
3 -6 4 -5 7

Sample Output 2

0

The given sequence already satisfies the conditions.

Sample Input 3

6
-1 4 3 2 -5 4

Sample Output 3

8



Sample input:
4
1 -3 1 0


Sample output:
4

Submission status: Wrong Answer

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

ansl = []

s = a[0]
ans = 0

if s <= 0:
	s = 1
	ans += abs(s) + 1

for i in range(n-1):
	if s > 0:
		s += a[i+1]
		if s >= 0:
			ans += abs(s) + 1
			s = -1
			"""
			print(i)
			print('s > 0')
			print(ans)
			print(s)
			"""
	elif s < 0:
		s += a[i+1]
		if s <= 0:
			ans += abs(s) + 1
			s = 1
			"""
			print(i)
			print('s < 0')
			print(ans)
			print(s)
			"""
ansl.append(ans)



s = a[0]
ans = 0

if s >= 0:
	s = -1
	ans += abs(s) + 1

for i in range(n-1):
	if s > 0:
		s += a[i+1]
		if s >= 0:
			ans += abs(s) + 1
			s = -1
			"""
			print(i)
			print('s > 0')
			print(ans)
			print(s)
			"""
	elif s < 0:
		s += a[i+1]
		if s <= 0:
			ans += abs(s) + 1
			s = 1
			"""
			print(i)
			print('s < 0')
			print(ans)
			print(s)
			"""
ansl.append(ans)

print(min(ansl))

Submission status: Wrong Answer

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

s = a[0]
ans = 0

for i in range(n-1):
	if s > 0:
		s += a[i+1]
		if s >= 0:
			ans += abs(s) + 1
			s = -1
			"""
			print(i)
			print('s > 0')
			print(ans)
			print(s)
			"""
	elif s < 0:
		s += a[i+1]
		if s <= 0:
			ans += abs(s) + 1
			s = 1
			"""
			print(i)
			print('s < 0')
			print(ans)
			print(s)
			"""
print(ans)

Submission status: Wrong Answer

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

s = a[0]
ans = 0

if s == 0:
	s += 1
	ans +=1

for i in range(n-1):
	if s > 0:
		s += a[i+1]
		if s >= 0:
			ans += abs(s) + 1
			s = -1
			"""
			print(i)
			print('s > 0')
			print(ans)
			print(s)
			"""
	elif s < 0:
		s += a[i+1]
		if s <= 0:
			ans += abs(s) + 1
			s = 1
			"""
			print(i)
			print('s < 0')
			print(ans)
			print(s)
			"""
print(ans)

Submission status: Wrong Answer

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

result = []

for i in range(1):
    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: Wrong Answer

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

num = a[0]
result = 0
r = 1

if num == 0:
    for i in range(1, n):
        if a[i] > 0:
            num = a[i] - 1
            result = 1 + (i - 1) * 2
            if num == 0:
                num += 1
                result += 1
            r = i + 1
            break

        elif a[i] < 0:
            num = a[i] + 1
            result = 1 + (i - 1) * 2
            if num == 0:
                num -= 1
                result += 1
            r = i + 1
            break
    if i == n - 1 and a[i] == 0:
        r = n
        result = n * 2 - 1
                    
for i in range(r, n):
    if num > 0:
        if num + a[i] >= 0:
            result += num + a[i] + 1
            num = -1
        else:
            num += a[i]
    else:
        if num + a[i] <= 0:
            result -= num + a[i] - 1
            num = 1
        else:
            num += a[i]

print(result)

Submission status: Wrong Answer

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 += 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]
yco = kco
print(min(xco, yco))

Submission status: Wrong Answer

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 += 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]
yco = kco
print(min(xco, yco))

Submission status: Wrong Answer

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


def counter(dp):
    count = 0
    is_positive = dp > 0
    for i in range(1, N):
        is_positive *= -1
        dp += A[i]
        if dp * is_positive <= 0:
            count += abs(dp)+1
            dp = is_positive
    return count


print(min(1+abs(A[0])+counter(-1), 1+abs(A[0])+counter(1), counter(A[0])))

Submission status: Wrong Answer

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


def counter(dp):
    count = 0
    for i in range(1, N):
        is_positive = 2 * (dp > 0) - 1
        dp += A[i]
        if dp * is_positive >= 0:
            count += abs(dp)+1
            dp = -is_positive
    return count


print(min(1+abs(A[0])+counter(-1), 1+abs(A[0])+counter(1), counter(A[0])))

Submission status: Wrong Answer

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


def counter(dp):
    count = 0
    for i in range(1, N):
        is_positive = 2 * (dp > 0) - 1
        dp += A[i]
        if dp * is_positive >= 0:
            count += abs(dp)+1
            dp = -is_positive
    return count


print(min(counter(A[0]), abs(A[0])+1+counter(A[0] <= 0)))

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]
dp = A[0]
count = 0
for i in range(1, N):
    is_positive = 2 * (dp > 0) - 1
    dp += A[i]
    if dp * is_positive >= 0:
        count += abs(dp)+1
        dp = -is_positive
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]
dp = A[0]
count = 0
is_positive = dp > 0
for i in range(1, N):
    dp += A[i]
    if not (dp > 0) ^ is_positive:
        count += abs(dp)+1
        dp ^= 1
    is_positive = dp > 0
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]
dp = A[0]
count = 0
is_positive = dp > 0
for i in range(1, N):
    dp += A[i]
    if (dp > 0) == is_positive:
        count += abs(dp)+1
        dp = 1-2*is_positive
    is_positive = dp > 0
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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] >= 1:
        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)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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 += abs(1-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] >= 1:
        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)))

Submission status: Wrong Answer

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

def pura(A):
    ans = 0
    check = 0
    if A[0] <= 0:
        ans += abs(1-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):
    ans = 0
    check = 0
    if A[0] >= 1:
        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)))

Submission status: Wrong Answer

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

def pura(A):
    ans = 0
    check = 0
    if A[0] <= 0:
        ans += abs(1-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(pura(a))

Submission status: Wrong Answer

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

def pura(A):
    ans = 0
    check = 0
    if A[0] <= 0:
        ans += abs(1-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]
        print(ans)
    return ans

print(pura(a))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
sum=[0,]*n
sum[0]=a[0]
zoubun=0
cnt=0
for i in range(1,n):
    sum[i]=sum[i-1]+a[i]
    if(sum[i]*sum[i-1]>=0):
        cnt+=abs(sum[i])+1
        if sum[i]>=0:
            sum[i]=-1
        else:
            sum[i]=1
        print(sum)
print(str(cnt))

Submission status: Wrong Answer

Solution code:
n = int(input())
num_list = list(map(int, input().split()))
count = 0
sum_ = num_list[0]
if sum_ > 0:
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 0:
            if sum_ <= 0:
                count += abs(sum_) + 1
                sum_ = 1
        else:
            if sum_ >= 0:
                count += abs(sum_) + 1 
                sum_ = -1
    print(count)                
elif sum_ < 0:
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 1:
            if sum_ <= 0:
                count += abs(sum_) + 1
                sum_ = 1
        else:
            if sum_ >= 0:
                count += abs(sum_) + 1 
                sum_ = -1
    print(count)                
else:
    sum_ = 1
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 0:
            if sum_ <= 0:
                count += abs(sum_) + 1
                sum_ = 1
        else:
            if sum_ >= 0:
                count += abs(sum_) + 1 
                sum_ = -1
    count1 = count        
    sum_ = -1
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 1:
            if sum_ <= 0:
                count += abs(sum_) + 1
                sum_ = 1
        else:
            if sum_ >= 0:
                count += abs(sum_) + 1 
                sum_ = -1
    count2 = count           
    print(min(count1+1, count2+1))

Submission status: Wrong Answer

Solution code:
n = int(input())
num_list = list(map(int, input().split()))
count = 0
sum_ = num_list[0]
if sum_ > 0:
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 0:
            if sum_ <= 0:
                count += abs(sum_) + 1
                sum_ = 1
        else:
            if sum_ >= 0:
                count += abs(sum_) + 1 
                sum_ = -1
    print(count)                
elif sum_ < 0:
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 1:
            if sum_ <= 0:
                count += abs(sum_) + 1
                sum_ = 1
        else:
            if sum_ >= 0:
                count += abs(sum_) + 1 
                sum_ = -1
    print(count)                
else:
    sum_ = 1
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 0:
            if sum_ <= 0:
                count += abs(sum_) + 1
                sum_ = 1
        else:
            if sum_ >= 0:
                count += abs(sum_) + 1 
                sum_ = -1
    count1 = count        
    sum_ = -1
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 1:
            if sum_ <= 0:
                count += abs(sum_) + 1
                sum_ = 1
        else:
            if sum_ >= 0:
                count += abs(sum_) + 1 
                sum_ = -1
    count2 = count           
    print(min(count1, count2))

Submission status: Wrong Answer

Solution code:
n = int(input())
num_list = list(map(int, input().split()))
count = 0
sum_ = num_list[0]
if sum_ > 0:
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 0:
            if sum_ <= 0:
                sum_ = 1
                count += abs(sum_) + 1
        else:
            if sum_ >= 0:
                sum_ = -1
                count += abs(sum_) + 1
    print(count)                
elif sum_ < 0:
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 1:
            if sum_ <= 0:
                sum_ = 1
                count += abs(sum_) + 1
        else:
            if sum_ >= 0:
                sum_ = -1
                count += abs(sum_) + 1 
    print(count)                
else:
    sum_ = 1
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 0:
            if sum_ <= 0:
                sum_ = 1
                count += abs(sum_) + 1
        else:
            if sum_ >= 0:
                sum_ = -1
                count += abs(sum_) + 1
    count1 = count        
    sum_ = -1
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 1:
            if sum_ <= 0:
                sum_ = 1
                count += abs(sum_) + 1
        else:
            if sum_ >= 0:
                sum_ = -1
                count += abs(sum_) + 1 
    count2 = count           
    print(min(count1, count2))

Submission status: Wrong Answer

Solution code:
n = int(input())
num_list = list(map(int, input().split()))
count = 0
sum_ = num_list[0]
if sum_ > 0:
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 0:
            if sum_ <= 0:
                sum_ += abs(sum_) + 1
                count += abs(sum_) + 1
        else:
            if sum_ >= 0:
                sum_ -= abs(sum_) + 1
                count += abs(sum_) + 1
    print(count)                
elif sum_ < 0:
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 1:
            if sum_ <= 0:
                sum_ += abs(sum_) + 1
                count += abs(sum_) + 1
        else:
            if sum_ >= 0:
                sum_ += -abs(sum_) - 1
                count += abs(sum_) + 1 
    print(count)                
else:
    sum_ = 1
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 0:
            if sum_ <= 0:
                sum_ += abs(sum_) + 1
                count += abs(sum_) + 1
        else:
            if sum_ >= 0:
                sum_ -= -abs(sum_) - 1
                count += abs(sum_) + 1
    count1 = count        
    sum_ = -1
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 1:
            if sum_ <= 0:
                sum_ += abs(sum_) + 1
                count += abs(sum_) + 1
        else:
            if sum_ >= 0:
                sum_ += abs(sum_) + 1
                count += abs(sum_) + 1 
    count2 = count           
    print(min(count1, count2))

Submission status: Wrong Answer

Solution code:
n = int(input())
num_list = list(map(int, input().split()))
count = 0
sum_ = num_list[0]

if sum_ > 0:
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 0:
            if sum_ <= 0:
                while sum_ <= 0:
                    sum_ += 1
                    count += 1
        else:
            if sum_ >= 0:
                while sum_ >= 0:
                    sum_ -= 1
                    count += 1
elif sum_ < 0:
    for i in range(1, n):
        sum_ += num_list[i]
        if i%2 == 1:
            if sum_ <= 0:
                while sum_ <= 0:
                    sum_ += 1
                    count += 1
        else:
            if sum_ >= 0:
                while sum_ >= 0:
                    sum_ -= 1
                    count += 1                  
print(count)

Submission status: Wrong Answer

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

ans_1 = 0
sum_1 = A[0]
if A[0] <= 0:
    ans_1 += abs(A[0])+1
    sum_1 = ans_1

for i in range(1, len(A)):
    a = A[i]
    prev_sum = sum_1
    sum_1 += a
    if sum_1 * prev_sum >= 0:
        ans_1 += abs(prev_sum+a)+1

        if prev_sum > 0:
            sum_1 = -1
        else:
            sum_1 = 1


ans_2 = 0
sum_2 = A[0]
if A[0] >= 0:
    ans_2 += abs(A[0])+1
    sum_2 = -ans_2

for i in range(1, len(A)):
    a = A[i]
    prev_sum = sum_2
    sum_2 += a
    if sum_2 * prev_sum >= 0:
        ans_2 += abs(prev_sum+a)+1

        if prev_sum > 0:
            sum_2 = -1
        else:
            sum_2 = 1

print(min(ans_1, ans_2))

Submission status: Wrong Answer

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

ans = 0
sum = A[0]
if sum == 0:
    ans = 1
    for i in range(1, len(A)):
        a = A[i]
        if a != 0:
            if i % 2 == 0:
                if a > 0:
                    sum = 1
                else:
                    sum = -1
            else:
                if a > 0:
                    sum = -1
                else:
                    sum = 1
            break

for i in range(1, len(A)):
    a = A[i]
    prev_sum = sum
    sum += a
    if sum * prev_sum >= 0:
        ans += abs(prev_sum+a)+1

        if prev_sum > 0:
            sum = -1
        else:
            sum = 1


print(ans)

Submission status: Wrong Answer

Solution code:
l = int(input())
n = [int(i) for i in input().split()] 
count = 0
nsum = n[0]
for i in range(1, l):
  tmp = 0
  tmp_1 = n[i-1]
  while(True):
    if (tmp_1 * tmp >= 0) or (nsum * (nsum + tmp) >= 0):
      if tmp_1 < 0:
        count += 1
        tmp += 1
      else:
        count += 1
        tmp -= 1     
    else:
      n[i] = tmp
      nsum += tmp
      break     
print(count)

Submission status: Wrong Answer

Solution code:
l = int(input())
n = [int(i) for i in input().split()] 
count = 0
nsum = n[0]
for i in range(1, l):
  while(True):
    print("i:" + str(i))
    print("n:" + str(n[i]))
    print(("nsum:" + str(nsum)))
    if (n[i-1] < 0 and n[i] <= 0) or (n[i-1] > 0 and n[i] >= 0) or (nsum * (nsum + n[i]) >= 0):
      if n[i-1] < 0:
        count += 1
        n[i] += 1
      else:
        count += 1
        n[i] -= 1     
    else:
      nsum += n[i]
      break
      
print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
ans = n * 1000000000
s = a[0]
cnt = 0
for i in range(1,n):
  ns = s + a[i]
  if s * ns < 0:
    s = ns
    continue
  else:
    cnt += abs(ns)+1
    s = -1 if ns > 0 else 1
ans = min(ans, cnt)

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

Submission status: Wrong Answer

Solution code:

def culc(a, plus, ans):
    sum = a[0]
    for i in range(1, len(a)):
        plus = not(plus)
        sum += a[i]
        if plus:
            if sum <= 0:
                ans += abs(sum) + 1
                sum = 1
        else:
            if sum >= 0:
                ans += abs(sum) + 1
                sum = -1
    return ans

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

plus = None
ans = 0
if a[0] == 0:
    ans += 1
    a[0] = 1
    plus = True
elif a[0] > 0:
    plus = True
else:
    plus = False

ans1 = culc(a, plus, ans)

a[0] = a[0] // abs(a[0]) * -1

ans2 = culc(a, not(plus), abs(a[0]) + 1 - ans)

print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:

def culc(a, plus, ans):
    sum = a[0]
    for i in range(1, len(a)):
        plus = not(plus)
        sum += a[i]
        if plus:
            if sum <= 0:
                ans += abs(sum) + 1
                sum = 1
        else:
            if sum >= 0:
                ans += abs(sum) + 1
                sum = -1
    return ans

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

plus = None
ans = 0
if a[0] == 0:
    ans += 1
    a[0] = 1
    plus = True
elif a[0] > 0:
    plus = True
else:
    plus = False

ans1 = culc(a, plus, ans)

a[0] = a[0] // abs(a[0]) * -1

ans2 = abs(a[0]) + 1
ans2 = culc(a, not(plus), abs(a[0]) + 1 - ans)

print(min(ans1, ans2))

Submission status: Wrong Answer

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

sum = an[0]
ans = 0
for i in range(1,n):
    if sum * (sum + an[i]) < 0:
        sum += an[i]
    else:
        if sum > 0:
            ans += abs(sum + an[i] + 1)
            sum = -1
        else:
            ans += abs(sum + an[i] - 1)
            sum = 1
ans1 = ans

if an[0] > 0:
    sum = -1
else:
    sum = 1
ans = abs(an[0])+1

for i in range(1, n):
    if sum * (sum + an[i]) < 0:
        sum += an[i]
    else:
        if sum > 0:
            ans += abs(sum + an[i] + 1)
            sum = -1
        else:
            ans += abs(sum + an[i] - 1)
            sum = 1

# print(ans1, ans)
print(min([ans1, ans]))

Submission status: Wrong Answer

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

sum = an[0]
ans = 0
for i in range(1,n):
    if sum * (sum + an[i]) < 0:
        sum += an[i]
    else:
        if sum > 0:
            ans += abs(sum + an[i] + 1)
            sum = -1
        else:
            ans += abs(sum + an[i] - 1)
            sum = 1
ans1 = ans

if an[0] > 0:
    sum = -1
else:
    sum = 1
ans = abs(an[0])+1

for i in range(1, n):
    if sum * (sum + an[i]) < 0:
        sum += an[i]
    else:
        if sum > 0:
            ans += abs(sum + an[i] + 1)
            sum = -1
        else:
            ans += abs(sum + an[i] - 1)
            sum = 1

print(ans1, ans)
print(min([ans1, ans]))

Submission status: Wrong Answer

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

sum = an[0]
ans = 0
for i in range(1,n):
    if sum * (sum + an[i]) < 0 or (i==1 and sum>0):
        sum += an[i]
    else:
        if sum > 0:
            ans += abs(sum + an[i] + 1)
            sum = -1
        else:
            ans += abs(sum + an[i] - 1)
            sum = 1

ans1 = ans

ans = 0
for i in range(1, n):
    if sum * (sum + an[i]) < 0 or (i == 1 and sum < 0):
        sum += an[i]
    else:
        if sum > 0:
            ans += abs(sum + an[i] + 1)
            sum = -1
        else:
            ans += abs(sum + an[i] - 1)
            sum = 1
print(min([ans1,ans]))

Submission status: Wrong Answer

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

sum = an[0]
ans = 0
for i in range(1,n):
    if sum * (sum + an[i]) < 0:
        sum += an[i]
    else:
        if sum > 0:
            ans += abs(sum + an[i] + 1)
            sum = -1
        else:
            ans += abs(sum + an[i] - 1)
            sum = 1

ans1 = ans

sum = -an[0]
ans = an[0]
for i in range(1, n):
    if sum * (sum + an[i]) < 0:
        sum += an[i]
    else:
        if sum > 0:
            ans += abs(sum + an[i] + 1)
            sum = -1
        else:
            ans += abs(sum + an[i] - 1)
            sum = 1


print(min([ans1,ans]))

Submission status: Wrong Answer

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

sum = an[0]
ans = 0
for i in range(1,n):
    if sum * (sum + an[i]) < 0:
        sum += an[i]
    else:
        if sum > 0:
            ans += abs(sum + an[i] + 1)
            sum = -1
        else:
            ans += abs(sum + an[i] - 1)
            sum = 1

print(ans)

Submission status: Wrong Answer

Solution code:
def 解():
    iN = int(input())
    aA = [int(_) for _ in input().split()]
    iL = len(aA)


    iStart = 0
    if sum(aA[0::2]) < sum(aA[1::2]):
        iStart = 1


    iC = 0
    aD = [0]*iL
    if 0 % 2 == iStart :
        if aA[0] <= 0:
            iC += -1 * aA[0] + 1
            aA[0] = 1
    else:
        if 0 <= aA[0] :
            iC += aA[0] + 1
            aA[0] = -1

    aD[0] = aA[0]
    for i in range(1,iL):
        aD[i] = aD[i-1]+aA[i]
        if i % 2 == iStart :
            if aD[i] <= 0 :
                iC += -1*aD[i] +1
                aD[i] = 1
        else:
            if 0 <= aD[i] :
                iC += aD[i] +1
                aD[i] = -1
    print(iC)
解()

Submission status: Wrong Answer

Solution code:

def 解():
    iN = int(input())
    aA = [int(_) for _ in input().split()]
    iL = len(aA)


    iStart = 0
    if sum(aA[0::2]) < sum(aA[1::2]):
        iStart = 1


    iC = 0
    aD = [0]*iL
    if 0 % 2 == iStart :
        if aA[0] < 0:
            aA[0] = 1
            iC += -1 * aA[0] + 1
    else:
        if 0 < aA[0] :
            aA[0] = -1
            iC += aA[0] + 1

    aD[0] = aA[0]
    for i in range(1,iL):
        aD[i] = aD[i-1]+aA[i]
        if i % 2 == iStart:
            if aD[i] <= 0:
                iC += -1*aD[i] +1
                aD[i] = 1
        else:
            if aD[i] >= 0:
                iC += aD[i] +1
                aD[i] = -1
    print(iC)
解()