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())
i = input()

i = i.split()
for item in range(len(i)):
    i[item] = int(i[item])

totn = 0
totp = 0

countp = 0
countn = 0

for x in range(len(i)):
    totp += i[x]
    totn += i[x]

    if x %2 == 1:
        if totn == 0:
            countn += 1
            totn = 1
        elif totn < 0:
            countn += abs(totn) + 1
            totn = 1

        if totp == 0:
            countp += 1
            totp = -1
        elif totp > 0:
            countp += abs(totp) + 1
            totp = -1

    if x %2 == 0:
        if totn == 0:
            countn += 1
            totn = -1
        elif totn > 0:
            countn += abs(totn) + 1
            totn = -1

        if totp == 0:
            countp += 1
            totp = 1
        elif totp < 0:
            countp += abs(totp) + 1

count = min(countn, countp)
print(count)

Submission status: Wrong Answer

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

def judge_pm(a,b):
    if a*b<0:
        return True
    else:
        return False

a1 = copy.deepcopy(a)
tmp_sum = a1[0]
operate_num1 = 0
for i in range(1, n):
    if judge_pm(tmp_sum, tmp_sum+a1[i]):
        pass 
    elif tmp_sum<0:
        tmp_operate_num =  - tmp_sum + 1 - a1[i]
        operate_num1 += tmp_operate_num
        a1[i] += tmp_operate_num
    else:
        tmp_operate_num = tmp_sum + 1 + a1[i]
        operate_num1 += tmp_operate_num
        a1[i] -= tmp_operate_num
    tmp_sum += a1[i]
        
a2 = copy.deepcopy(a)
operate_num2 = 0
if a2[0]<0:
    operate_num2 += 1 - a2[0]
    a2[0] += operate_num2
else:
    operate_num2 += a2[0] + 1
    a2[0] -= operate_num2

tmp_sum = a2[0]

for i in range(1, n):
    if judge_pm(tmp_sum, tmp_sum+a2[i]):
        pass 
    elif tmp_sum<0:
        tmp_operate_num =  - tmp_sum + 1 - a2[i]
        operate_num2 += tmp_operate_num
        a2[i] += tmp_operate_num
    else:
        tmp_operate_num = tmp_sum + 1 + a2[i]
        operate_num2 += tmp_operate_num
        a2[i] -= tmp_operate_num
    tmp_sum += a2[i]

print(min(operate_num1, operate_num2))

Submission status: Wrong Answer

Solution code:
n = int(input())
b = [int(x) for x in input().split()]
a = list()
temp = 0
count1 = 0
count2 = 0

a = b.copy()

if a[0] == 0:
	a[0] = 1
	count1 = 1

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

a = b.copy()

if a[0] == 0:
	a[0] = 1

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

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

print(min(count1, count2))

Submission status: Wrong Answer

Solution code:
n = int(input())
b = [int(x) for x in input().split()]
a = list()
temp = 0
count1 = 0
count2 = 0

a = b.copy()

if a[0] == 0:
	a[0] = 1
	count1 = 1

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

a = b.copy()
count2 = abs(a[0]) + 1

if a[0] == 0:
	a[0] = 1
	count2 = 1

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

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

print(min(count1, count2))

Submission status: Wrong Answer

Solution code:
n = int(input())
b = [int(x) for x in input().split()]
a = list()
temp = 0
count1 = 0
count2 = 0

a = b.copy()

if a[0] == 0:
	a[0] = 1
	count1 = 1

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

a = b.copy()
count2 = abs(a[0]) + 1

if a[0] == 0:
	a[0] = 1
	count1 = 1

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

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

print(min(count1, count2))

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    n = int(input())
    a0 = input().split()
    a = [int(i) for i in a0]
    s1 = 0
    s2 = 0
    m1 = 0
    m2 = 0
    o = 1
    e = -1
    for i in a:
        s1 += i
        if s1 == 0:
            m1 += 1
        elif o * s1 < 0:
            m1 += abs(s1) + 1
            s1 = o
        o *= -1

        s2 += i
        if s2 == 0:
            m2 += 1
        elif e * s2 < 0:
            m2 += abs(s2) + 1
            s2 = e
        e *= -1
    print(min(m1, m2))

Submission status: Wrong Answer

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

def judge_pm(a,b):
    if a*b<0:
        return True
    else:
        return False

a1 = copy.deepcopy(a)
tmp_sum = a1[0]
operate_num1 = 0
for i in range(1, n):
    if judge_pm(tmp_sum, tmp_sum+a1[i]):
        pass 
    elif tmp_sum<0:
        tmp_operate_num =  - tmp_sum + 1 - a1[i]
        operate_num1 += tmp_operate_num
        a1[i] += tmp_operate_num
    else:
        tmp_operate_num = tmp_sum + 1 + a1[i]
        operate_num1 += tmp_operate_num
        a1[i] -= tmp_operate_num
    tmp_sum += a1[i]
        
a2 = copy.deepcopy(a)
operate_num2 = 0
if a2[0]<0:
    operate_num2 += min(-a2[0] - a[1] + 1, -a[0] + 1)
    a2[0] += operate_num2
else:
    operate_num2 += min(a2[0] + a[1] - 1, a[0] - 1)
    a2[0] -= operate_num2

tmp_sum = a2[0]

for i in range(1, n):
    if judge_pm(tmp_sum, tmp_sum+a2[i]):
        pass 
    elif tmp_sum<0:
        tmp_operate_num =  - tmp_sum + 1 - a2[i]
        operate_num2 += tmp_operate_num
        a2[i] += tmp_operate_num
    else:
        tmp_operate_num = tmp_sum + 1 + a2[i]
        operate_num2 += tmp_operate_num
        a2[i] -= tmp_operate_num
    tmp_sum += a2[i]
print(min(operate_num1, operate_num2))

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
# C
    n = int(input())
    a0 = input().split()
    a = [int(i) for i in a0]
    s1 = 0
    s2 = 0
    m1 = 0
    m2 = 0
    o = 1
    e = -1
    for i in a:
        s1 += i
        if s1 == 0:
            m1 += 1
        elif o * s1 < 0:
            m1 += abs(s1) + 1
            s1 = o
            o *= -1
        else:
            o *= -1

        s2 += i
        if s2 == 0:
            m2 += 1
        elif e * s2 < 0:
            m2 += abs(s2) + 1
            s2 = e
            e *= -1
        else:
            e *= -1
    print(min(m1, m2))

Submission status: Wrong Answer

Solution code:
n = int(input())
i = input()

i = i.split()
for item in range(len(i)):
    i[item] = int(i[item])

totn = 0
totp = 0

countp = 0
countn = 0

for x in range(len(i)):
    totp += i[x]
    totn += i[x]
    
    if x == 0:
        if totp == 0:
            totn = -1
            countn = 1

            totp = 1
            countn = 1
            
        elif totp < 0:
            countp = abs(totp) + 1
            totp = 1

        elif totp > 0:
            countn = abs(totn) + 1
            totn = -1

    elif x %2 == 1:
        if totn == 0:
            countn += 1
            totn += 1
        elif totn < 0:
            countn += abs(totn) + 1
            totn = 1

        if totp == 0:
            countp += 1
            totp -= 1
        elif totp > 0:
            countp += abs(totp) + 1
            totp = -1

    elif x %2 == 0:
        if totn == 0:
            countn += 1
            totn -= 1
        elif totn > 0:
            countn += abs(totn) + 1
            totn = -1

        if totp == 0:
            countp += 1
            totp += 1
        elif totp < 0:
            countp += abs(totp) + 1
            totp = 1
    '''print('totn', totn)
    print('countn', countn)
    print('totp', totp)
    print('countp', countp) '''   
count = min(countn, countp)
print(count)

Submission status: Wrong Answer

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

ans = 1e9

for ini in (A[0], -A[0]):
    acc = ini
    res = 0
    for a in A[1:]:
        if acc + a == 0:
            acc = 1 if acc < 0 else -1
            res += 1
        elif (acc < 0) != (acc + a > 0):
            ope = -(acc + a + (1 if acc + a > 0 else -1))
            acc = acc + a + ope
            res += abs(ope)
        else:
            acc += a
    ans = min(ans, res)

print(ans)

Submission status: Wrong Answer

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

def f(first, m):
    now = first
    result = 0

    for i, v in enumerate(a[1:]):
        tmp = now * -1
        if m < 0:
            tmp += 1
            if tmp < v:
                now += v
            else:
                result += tmp - v
                now = 1
        else:
            tmp -= 1
            if tmp > v:
                now += v
            else:
                result += v - tmp
                now = -1
        m *= -1
    return result

m = a[0] < 0 and -1 or 1
print(min((f(a[0], m), f(m * -1, m * -1))))

Submission status: Wrong Answer

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

now = a[0]
m = now < 0 and -1 or 1
result = 0

for i, v in enumerate(a[1:]):
    tmp = now * -1
    if m < 0:
        tmp += 1
        if tmp < v:
            now += v
        else:
            result += tmp - v
            now = 1
    else:
        tmp -= 1
        if tmp > v:
            now += v
        else:
            result += v - tmp
            now = -1
    m *= -1
print(result)

Submission status: Wrong Answer

Solution code:
import itertools

def sign(num):
    if num < 0:
        return -1
    elif num > 0:
        return 1
    else:
        return 0

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

a_sum = [a_i[0]]
for i, a in enumerate(a_i[1:]):
    i += 1
    a_sum.append(a_sum[-1]+a)

signs = [1, -1]
for i, sum_i in enumerate(a_sum):
    if sum_i != 0 and i%2 == 0:
        signs = [sign(sum_i), -sign(sum_i)]
        break
    elif sum_i != 0 and i%2 == 1:
        signs = [-sign(sum_i), sign(sum_i)]
        break

a_sum = 0
changes = 0
for i, a in enumerate(a_i):
    a_sum += a
    if sign(a_sum) != signs[i%2]:
        changes += abs(a_sum) + 1
        a_sum = signs[i%2]
print(changes)

#
# for i, sum_i in enumerate(a_sum):
#     if i == 0:
#         signs = [sign(sum_i), -sign(sum_i)]
#     elif sign(sum_i) != signs[i%2]:
#         a_sum[i:] = [num + (abs(sum_i) + 1) * signs[i%2] for num in a_sum[i:]]
#         changes += abs(sum_i) + 1
#     # print(a_sum)
# print(changes)

Submission status: Wrong Answer

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

now = a[0]
m = now < 0 and -1 or 1
result = 0

for i, v in enumerate(a[1:]):
    tmp = now * -1
    if m < 0:
        tmp += 1
        if tmp > v:
            now += v
        else:
            result += abs(tmp - v)
            now = -1
    else:
        tmp -= 1
        if tmp < v:
            now += v
        else:
            result += abs(tmp - v)
            now = 1
    m *= -1
print(result)

Submission status: Wrong Answer

Solution code:
import itertools

def sign(num):
    if num < 0:
        return -1
    elif num > 0:
        return 1
    else:
        return 0

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

# a_sum = [a_i[0]]
# for i, a in enumerate(a_i[1:]):
#     i += 1
#     a_sum.append(a_sum[-1]+a)
#
# signs = [1, -1]
# changes = 0
#
# for i, sum_i in enumerate(a_sum):
#     if sum_i != 0:
#         signs[i%2] = sign(sum_i)
#         signs[i%2+1] = -sign(sum_i)
#         break

signs = [sign(a_i[0]), -sign(a_i[0])]

a_sum = 0
changes = 0
for i, a in enumerate(a_i):
    a_sum += a
    if sign(a_sum) != signs[i%2]:
        changes += abs(a_sum) + 1
        a_sum = signs[i%2]
print(changes)

#
# for i, sum_i in enumerate(a_sum):
#     if i == 0:
#         signs = [sign(sum_i), -sign(sum_i)]
#     elif sign(sum_i) != signs[i%2]:
#         a_sum[i:] = [num + (abs(sum_i) + 1) * signs[i%2] for num in a_sum[i:]]
#         changes += abs(sum_i) + 1
#     # print(a_sum)
# print(changes)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = [ sum(a[0:i+1]) for i in range(n)]
s2 = [ s[i]*(-1) for i in range(n)]
al = [(-1)**i for i in range(n)]
c1 = 0
for i in range(n):
    if s[i]*al[i] <= 0:
        c1 = c1 - s[i]*al[i] + 1
        s = [s[j] if j <= i-1 else s[j] + (s[i])*al[i] + al[i] for j in range(n)]
c2 = 0
for i in range(n):
    if s2[i]*al[i] <= 0:
        c2 = c2 - s2[i]*al[i] + 1
        s2 = [s2[j] if j <= i-1 else s2[j] - (s2[i])*al[i] + al[i] for j in range(n)]
print(min(c1,c2))

Submission status: Wrong Answer

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

count = 0

if a[0] == 0:
    count += 1
    if 0 < a[1]:
        a[0] = 1
    else:
        a[0] = -1

SUM = a[0]

for i in range(1, n - 1):
    SUM_next = SUM + a[i - 1]
    if 0 <= SUM * SUM_next:
        if 0 < SUM:
            a[i] -= (SUM_next + 1)
            count += (SUM_next + 1)
        else:
            a[i] += (-SUM_next + 1)
            count += (-SUM_next + 1)
    SUM = SUM_next

print(count)

Submission status: Wrong Answer

Solution code:
a = int(input())
b = list(map(int, input().split()))
num = 0
k = b[0] + b[1]
 
if k == 0:
 if b[0] >= 0:
  k = -1
 else:
  k = 1
 num += 1
 
for i in range(a-2): 
 if k == 1:
  k += b[i+2]
  while k > -1:
   k -= 1
   num += 1
 else:
  k += b[i+2]
  while k < 1:
   k += 1
   num += 1

print(num)

Submission status: Wrong Answer

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

W=[]
wa=0
for i in range(n):
    wa=A[i]+wa
    W.append(wa)
counter=0
for i in range(n):
    if i==n-1:
        break
   
                           
    elif W[i]<0 and W[i+1]<0:
        counter=abs(W[i])-abs(A[i+1])+1+counter
        A[i+1]=1           
    elif W[i]>0 and W[i+1]>0:
        counter=abs(W[i])+1+counter-abs(A[i+1])
        A[i+1]=-1
if A[n-1]==0:
    if W[n-2]*W[n-1]>0:
        counter=counter+abs(W[n-2])+1
print(counter)

Submission status: Wrong Answer

Solution code:
n = int(input())
b = [int(x) for x in input().split()]
a = list()
temp = 0
count1 = 0
count2 = 0

a = b

if a[0] == 0:
	a[0] = 1
	count1 = 1

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

count2 = abs(a[0]) + 1

a = b

if a[0] == 0:
	a[0] = 1
	count1 = 1

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

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

print(min(count1, count2))

Submission status: Wrong Answer

Solution code:
n = int(input())
b = [int(x) for x in input().split()]
a = list()
temp = 0
count1 = 0
count2 = 0

a = b

if a[0] == 0:
	a[0] = 1
	count1 = 1

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

count2 = abs(a[0]) + 1

a = b

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

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

print(min(count1, count2))

Submission status: Wrong Answer

Solution code:
def c(ints):
    
    for i in range(len(ints)):
        if ints[i] != 0:
            sig = 1 if ints[i] > 0 else -1
            sig_ = -sig
            total = ints[i]
            total_ = -sig
            mov = i
            mov_ = abs(total) + 1 + i
            if i > 0:
                mov += 1 + i - 1
                mov_ += 2 + i - 1
            j = i
            break
        if i == len(ints) - 1:
            return i * 2 + 1
    
    for i_ in ints[j+1:]:
        tmp = total + i_
        tmp_ = total_ + i_
        if tmp == 0:
            mov +=1
            tmp = -sig
        elif sig * tmp > 0:
            mov += abs(tmp) + 1
            tmp = -sig
            
        if tmp_ == 0:
            mov_ +=1
            tmp_ = -sig_
        elif sig_ * tmp_ > 0:
            mov_ += abs(tmp_) + 1
            tmp_ = -sig_
            
        sig *= -1
        total = tmp
        sig_ *= -1
        total_ = tmp_
    
    return min(mov, mov_)
 
_ = input()
inp = input()
inp = inp.split(' ')
inp = [int(i_) for i_ in inp]
print(c(inp))

Submission status: Wrong Answer

Solution code:
n = int(input())
i = input()

i = i.split()
for item in range(len(i)):
    i[item] = int(i[item])

signM = None
tot = 0
count = 0

if i[0] > 0:
    signN = 'neg'
elif i[0] < 0:
    signN = 'pos'
else:
    if i[1] > 0:
        tot = -1
        count += 1
        signN = 'pos'
    elif i[1] < 0:
        tot = 1
        count += 1
        signN = 'neg'

for x in range(len(i)):
    tot += i[x]
    #print('tot beg', tot)
    
    if tot > 0:
        signM = 'pos'
    elif tot < 0:
        signM = 'neg'
    elif tot == 0:
        signM = 'z'

    if signN != signM and signM != 'z':
        signN = signM
    elif signN == 'pos' and signM == 'pos':
        count += (tot + 1)
        tot -= (tot +1)
        signM = 'neg'
    elif signN == 'neg' and signM == 'neg':
        count += (abs(tot) + 1)
        tot = 1
        signM = 'pos'
        
    if tot == 0:
        count += 1
        if signN == 'neg':
            tot += 1
            signM = 'pos'
        elif signN == 'pos':
            tot -= 1
            signM = 'neg'

    signN = signM
    #print('tot end', tot)
    #print('count', count)
print(count)

Submission status: Wrong Answer

Solution code:
def c(ints):
    
    for i in range(len(ints)):
        if ints[i] != 0:
            sig = 1 if ints[i] > 0 else -1
            sig_ = -sig
            total = ints[i]
            total_ = -sig
            mov = i
            mov_ = abs(total) + 1 + i
            if i > 0:
                mov += 1 + i - 1
                mov_ += 2 + i - 1
            j = i
            break
        if i == len(ints) - 1:
            return i * 2 + 1
    
    for i_ in ints[j+1:]:
        tmp = total + i_
        tmp_ = total_ + i_
        if tmp == 0:
            mov +=1
            tmp = -sig
        elif sig * tmp > 0:
            mov += abs(tmp) + 1
            tmp = -sig
            
        if tmp_ == 0:
            mov_ +=1
            tmp_ = -sig_
        elif sig_ * tmp_ > 0:
            mov_ += abs(tmp_) + 1
            tmp_ = -sig_
            
        sig *= -1
        total = tmp
        sig_ *= -1
        total_ = tmp_
    
    print(mov, mov_)
    return min(mov, mov_)

_ = input()
inp = input()
inp = inp.split(' ')
inp = [int(i_) for i_ in inp]
print(c(inp))

Submission status: Wrong Answer

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

count = 0

if a[0] == 0:
    count += 1
    if 0 < a[1]:
        a[0] = 1
    else:
        a[0] = -1

SUM = a[0]

for i in range(1, n):
    SUM_next = SUM + a[i]
    if 0 <= SUM * SUM_next:
        if 0 < SUM:
            a[i] -= (SUM_next + 1)
            count += (SUM_next + 1)
        else:
            a[i] += (-SUM_next + 1)
            count += (-SUM_next + 1)
    SUM = SUM_next

print(count)

Submission status: Wrong Answer

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


a1 = a.copy()
cnt1 = 0
sum1 = 0
if a1[0] == 0:
    a1[0] = 1
    cnt1 +=1

a2 = a.copy()
cnt2 = 0
sum2 = 0
if a2[0] == 0:
    a2[0] = -1
    cnt2 +=1
    
    
for i in range(n-1):
    sum1 = sum(a1[:i+1])
    if sum1 > 0 and (sum1+a1[i+1]) >= 0:
        cnt1 += abs(((sum1 * -1)-1) - a1[i+1])
        a1[i+1] = ((sum1 * -1)-1)
    
    elif sum1 < 0 and (sum1 + a1[i+1]) <= 0:
        cnt1 += abs(abs(sum1)+1 - a1[i+1])
        a1[i+1] = abs(sum1)+1

print(min(cnt1,cnt2))

Submission status: Wrong Answer

Solution code:
n = int(input())
b = [int(x) for x in input().split()]
temp = 0
count1 = 0
count2 = 0

a = b

if a[0] == 0:
	a[0] = 1
	count1 = 1

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

count2 = abs(a[0]) + 1

a = b

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

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

print(min(count1, count2))

Submission status: Wrong Answer

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

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

    if(sum(a[:i-1])*sum(a[:i]) > 0):
        k = abs(0-sum(a[:i-1]))+1
        ll = k+abs(a[i-1])
        if(a[i-1] > 0):
            a[i-1] -= ll

        elif (a[i-1] < 0):
            a[i-1] += ll

        count += ll

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
c = 0
i = 0
k = 1
sum = a[0]

while a[i] == 0:
	i += 1
if a[i] > 0:
	if i % 2 == 1:
		a[0] = 1
		c = 1
	else:
		a[0] = -1
		c = 1
else:
	if i % 2 == 1:
		a[0] = -1
		c = 1
	else:
		a[0] = 1
		c = 1
i = 1
if a[0] > 0:
	while i < n:
		if i == 2*k-1:
			sum = sum + a[2*k-1]
			if sum >= 0:
				c = c + sum + 1
				sum = -1
			else:
				pass
			i += 1
		else:
			sum = sum + a[2*k]
			if sum <= 0:
				c = c - sum + 1
				sum = 1
			else:
				pass
			i += 1
			k += 1
	print(c)
elif a[0] < 0:
	while i < n:
		if i == 2*k-1:
			sum = sum + a[2*k-1]
			if sum <= 0:
				c = c - sum + 1
				sum = 1
			else:
				pass
			i += 1
		else:
			sum = sum + a[2*k]
			while sum >= 0:
				c = c + sum + 1
				sum = -1
			else:
				pass
			i += 1
			k += 1
	print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
c = 0
i = 0
k = 1
sum = a[0]

while a[i] == 0:
	i += 1
	if a[i] > 0:
		if i % 2 == 1:
			a[0] = 1
			c = 1
		else:
			a[0] = -1
			c = 1
	else:
		if i % 2 == 1:
			a[0] = -1
			c = 1
		else:
			a[0] = 1
			c = 1
i = 1
if a[0] > 0:
	while i < n:
		if i == 2*k-1:
			sum = sum + a[2*k-1]
			if sum >= 0:
				c = c + sum + 1
				sum = -1
			else:
				pass
			i += 1
		else:
			sum = sum + a[2*k]
			if sum <= 0:
				c = c - sum + 1
				sum = 1
			else:
				pass
			i += 1
			k += 1
	print(c)
elif a[0] < 0:
	while i < n:
		if i == 2*k-1:
			sum = sum + a[2*k-1]
			if sum <= 0:
				c = c - sum + 1
				sum = 1
			else:
				pass
			i += 1
		else:
			sum = sum + a[2*k]
			while sum >= 0:
				c = c + sum + 1
				sum = -1
			else:
				pass
			i += 1
			k += 1
	print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())
sa = input().split()

a = [0 for i in range(n)]
sumeven = 0
sumodd = 0
for i,sai in enumerate(sa):
    a[i] = int(sai)

    if i % 2 == 0:
        sumeven += a[i]
    else:
        sumodd += a[i]

ret = 0
sm = 0
for i,ai in enumerate(a):
    if sumeven < sumodd:
        if i % 2 == 1:
            ret += max(-sm+1-ai,0)
            sm += max(ai, -sm+1)
        else:
            ret += max(sm+1 + ai, 0)
            sm += min(ai, -(sm+1))
    elif sumeven > sumodd:
        if i % 2 == 0:
            ret += max(-sm+1-ai,0)
            sm += max(ai, -sm+1)
        else:
            ret += max(sm+1 + ai, 0)
            sm += min(ai, -(sm+1))
    else:
        if a[0] > 0:
            if i % 2 == 1:
                ret += max(-sm+1-ai,0)
                sm += max(ai, -sm+1)
            else:
                ret += max(sm+1 + ai, 0)
                sm += min(ai, -(sm+1))
        else:
            if i % 2 == 0:
                ret += max(-sm+1-ai,0)
                sm += max(ai, -sm+1)
            else:
                ret += max(sm+1 + ai, 0)
                sm += min(ai, -(sm+1))
        
print(ret)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
c = 0
i = 1
k = 1
sum = a[0]

while a[i] == 0:
	i += 1
	if a[i] > 0:
		if i % 2 == 1:
			a[0] = 1
			c = 1
		else:
			a[0] = -1
			c = 1
	else:
		if i % 2 == 1:
			a[0] = -1
			c = 1
		else:
			a[0] = 1
			c = 1
i = 1
if a[0] > 0:
	while i < n:
		if i == 2*k-1:
			sum = sum + a[2*k-1]
			if sum >= 0:
				c = c + sum + 1
				sum = -1
			else:
				pass
			i += 1
		else:
			sum = sum + a[2*k]
			if sum <= 0:
				c = c - sum + 1
				sum = 1
			else:
				pass
			i += 1
			k += 1
	print(c)
elif a[0] < 0:
	while i < n:
		if i == 2*k-1:
			sum = sum + a[2*k-1]
			if sum <= 0:
				c = c - sum + 1
				sum = 1
			else:
				pass
			i += 1
		else:
			sum = sum + a[2*k]
			while sum >= 0:
				c = c + sum + 1
				sum = -1
			else:
				pass
			i += 1
			k += 1
	print(c)

Submission status: Wrong Answer

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

def judge_pm(a,b):
    if a*b<0:
        return True
    else:
        return False

tmp_sum = a[0]
operate_num = 0
for i in range(1, n):
    if judge_pm(tmp_sum, tmp_sum+a[i]):
        pass 
    elif tmp_sum<0:
        tmp_operate_num =  - tmp_sum + 1 - a[i]
        operate_num += tmp_operate_num
        a[i] += tmp_operate_num
    else:
        tmp_operate_num = tmp_sum + 1 + a[i]
        operate_num += tmp_operate_num
        a[i] -= tmp_operate_num
    tmp_sum += a[i]
        
print(operate_num)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
a=map(int,raw_input().split(' '))

p_s=a[0]
c=0
for i in range(1,n):
    s=p_s+a[i]
    #print i,p_s,s
    if s==0:
        if i==n-1:
            a[i]+=1
            c+=1
        else:
            if a[i]>=0:
                a[i]+=1
                c+=1
            else:
                a[i]-=1
                c+=1
    s=p_s+a[i]
    if p_s*s>=0:
        if p_s>0:
            c+=abs(p_s*-1 - 1 - a[i])
            a[i]=p_s*-1 - 1
        else:
            c+=abs(p_s*-1 + 1 - a[i])
            a[i]=p_s*-1 + 1
    p_s=p_s+a[i]

#print c,a
print c

Submission status: Wrong Answer

Solution code:
def c(ints):
    
    for i in range(len(ints)):
        if ints[i] != 0:
            sig = 1 if ints[i] > 0 else -1
            sig_ = -sig
            total = ints[i]
            total_ = -sig
            mov = i
            mov_ = abs(total) + 1
            if i > 0:
                mov += 1 + i - 1
                mov_ += 2 + i - 1
            j = i
            break
        if i == len(ints) - 1:
            return i * 2 + 1
    
    for i_ in ints[j+1:]:
        tmp = total + i_
        tmp_ = total_ + i_
        if tmp == 0:
            mov +=1
            tmp = -sig
        elif sig * tmp > 0:
            mov += abs(tmp) + 1
            tmp = -sig
            
        if tmp_ == 0:
            mov_ +=1
            tmp_ = -sig_
        elif sig_ * tmp_ > 0:
            mov_ += abs(tmp_) + 1
            tmp_ = -sig_
            
        sig *= -1
        total = tmp
        sig_ *= -1
        total_ = tmp_
    
    return min(mov, mov_)

_ = input()
inp = input()
inp = inp.split(' ')
inp = [int(i_) for i_ in inp]
print(c(inp))

Submission status: Wrong Answer

Solution code:
a = int(input())
b = list(map(int, input().split()))
num = 0
k = b[0] + b[1]

if k == 0:
 if b[0] >= 0:
  k -= 1
 else:
  k += 1
 num += 1

for i in range(a-2): 
 if k > 0:
  k += b[i+2]
  if k > -1:
   num += k +1
   k = -1
 else:
  k += b[i+2]
  if k < 1:
   num += -k + 1
   k = 1
print(num)

Submission status: Wrong Answer

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

ans = 0
acc = A[0]
for a in A[1:]:
    if acc + a == 0:
        acc = 1 if acc < 0 else -1
        ans += 1
    elif (acc < 0) != (acc + a > 0):
        ope = -(acc + a + (1 if acc + a > 0 else -1))
        acc = acc + a + ope
        ans += abs(ope)
    else:
        acc += a

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
a=map(int,raw_input().split(' '))

p_s=a[0]
c=0
for i in range(1,n):
    s=p_s+a[i]
    print i,p_s,s
    if s==0:
        if i==n-1:
            a[i]+=1
            c+=1
        else:
            if a[i]>=0:
                a[i]+=1
                c+=1
            else:
                a[i]-=1
                c+=1
    s=p_s+a[i]
    if p_s*s>=0:
        if p_s>0:
            c+=abs(p_s*-1 - 1 - a[i])
            a[i]=p_s*-1 - 1
        else:
            c+=abs(p_s*-1 + 1 - a[i])
            a[i]=p_s*-1 + 1
    p_s=p_s+a[i]

print c

Submission status: Wrong Answer

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

s = a[0]
count = 0
total_delta = 0
delta = 0

if a[0] == 0:
    i = 0
    while i < len(a):
        if a[i] != 0:
            break
        i += 1
    if i == len(a):
        delta = 1
        total_delta += delta
        count += 1
    else:
        delta = -1 * (a[i] // abs(a[i])) * ((1, -1)[(i + 1) % 2])
        total_delta += delta
        count += 1
for i in range(1, n):
    sign = (s + total_delta) // abs(s + total_delta)
    if (s + a[i] + total_delta) * sign > 0 :
        delta  = (sign * -1) - (s + a[i] + total_delta)
        total_delta += delta
        count += abs(delta)
    elif (s + a[i] + total_delta) == 0:
        total_delta += sign * -1
        count += 1
    s += a[i]
print(count)