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 sys

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

if all((x == 0 for x in l)):
    print(n * 2 - 1)
    sys.exit()

ans = 0
last_sum = l[0]
if last_sum == 0:
    for i in range(n - 1):
        if l[i + 1] != 0:
            if i + 1 % 2 == 1:
                ans = + 1
                last_sum = -1
            else:
                ans = + 1
                last_sum = 1
for i in range(n - 1):
    # print(last_sum)
    if last_sum > 0:
        if last_sum + l[i + 1] < 0:
            last_sum += l[i + 1]
        else:
            a = -1 - last_sum - l[i + 1]
            ans += abs(a)
            last_sum += a + l[i + 1]
    else:
        if last_sum + l[i + 1] > 0:
            last_sum += l[i + 1]
        else:
            a = 1 - last_sum - l[i + 1]
            ans += abs(a)
            last_sum += a + l[i + 1]
print(ans)

Submission status: Wrong Answer

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


def f(x):
    if x == 0:
        return 0
    else:
        return x // abs(x)


for i in range(1, n):
    if b * (b + a[i]) >= 0:
        a1[i] = -f(a1[i - 1]) - b
        if b + a1[i] == 0:
            a1[i] += f(a1[i])
        ans += abs(a1[i] - a[i])
    else:
        a1[i] = a[i]

    b += a1[i]

a2 = [0] * n
a2[0] = -f(a[0])
ans1 = abs(a2[0] - a[0])
b1 = a2[0]

for i in range(1, n):
    if b1 * (b1 + a[i]) >= 0:
        a2[i] = -f(a2[i - 1]) - b1
        if b1 + a2[i] == 0:
            a2[i] += f(a2[i])
        ans1 += abs(a2[i] - a[i])
    else:
        a2[i] = a[i]

    b1 += a2[i]

print(min(ans1, ans))

Submission status: Wrong Answer

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


def f(x):
    if x == 0:
        return 0
    else:
        return x // abs(x)


for i in range(1, n):
    if b * (b + a[i]) >= 0:
        a1[i] = -f(a1[i - 1]) - b
        if b + a1[i] == 0:
            a1[i] += f(a1[i])
        ans += abs(a1[i] - a[i])
    else:
        a1[i] = a[i]

    b += a1[i]

a2 = [0] * n
ans1 = abs(-f(a2[0]) - a2[0])
a2[0] = -f(a[0])
b1 = a2[0]

for i in range(1, n):
    if b * (b + a[i]) >= 0:
        a2[i] = -f(a2[i - 1]) - b1
        if b1 + a2[i] == 0:
            a2[i] += f(a2[i])
        ans1 += abs(a2[i] - a[i])
    else:
        a2[i] = a[i]

    b1 += a2[i]

print(min(ans1, ans))

Submission status: Wrong Answer

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


def f(x):
    if x == 0:
        return 0
    else:
        return x // abs(x)


for i in range(1, n):
    if a1[i - 1] * a[i] >= 0:
        a1[i] = -a[i]
    else:
        a1[i] = a[i]

    if b * (b + a1[i]) >= 0:
        a1[i] = -f(a1[i - 1]) - b
        if b + a1[i] == 0:
            a1[i] += f(a1[i])
        ans += abs(a1[i] - a[i])

    b += a1[i]

a2 = [0] * n
ans1 = abs(-f(a2[0]) - a2[0])
a2[0] = -f(a[0])
b1 = a2[0]

for i in range(1, n):
    if a2[i - 1] * a[i] >= 0:
        a2[i] = -a[i]
    else:
        a2[i] = a[i]

    if b * (b + a2[i]) >= 0:
        a2[i] = -f(a2[i - 1]) - b1
        if b1 + a2[i] == 0:
            a2[i] += f(a2[i])
        ans1 += abs(a2[i] - a[i])

    b1 += a2[i]

print(min(ans1, ans))

Submission status: Wrong Answer

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


def f(x):
    if x == 0:
        return 0
    else:
        return x // abs(x)


for i in range(1, n):
    if a1[i - 1] * a[i] >= 0:
        a1[i] = -a[i]
    else:
        a1[i] = a[i]

    if b * (b + a1[i]) >= 0:
        a1[i] = -f(a1[i - 1]) - b
        if b + a1[i] == 0:
            a1[i] += f(a1[i])
        ans += abs(a1[i] - a[i])

    b += a1[i]

a2 = [0] * n
ans1 = abs(-f(a2[0]) - a2[0])
a2[0] = -f(a2[0])
b1 = a2[0]

for i in range(1, n):
    if a2[i - 1] * a[i] >= 0:
        a2[i] = -a[i]
    else:
        a2[i] = a[i]

    if b * (b + a2[i]) >= 0:
        a2[i] = -f(a2[i - 1]) - b1
        if b1 + a2[i] == 0:
            a2[i] += f(a2[i])
        ans1 += abs(a2[i] - a[i])

    b1 += a2[i]

print(min(ans1, ans))

Submission status: Wrong Answer

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


def f(x):
    if x == 0:
        return 0
    else:
        return x // abs(x)


for i in range(1, n):
    if a1[i - 1] * a[i] >= 0:
        a1[i] = -a[i]
    else:
        a1[i] = a[i]

    if b * (b + a1[i]) >= 0:
        a1[i] = -f(a1[i - 1]) - b
        if b + a1[i] == 0:
            a1[i] += f(a1[i])
        ans += abs(a1[i] - a[i])

    b += a1[i]

a2 = [a[0]] * n
b1 = a2[0]
ans1 = abs(-f(a2[0]) - a2[0])
a2[0] = -f(a2[0])

for i in range(1, n):
    if a2[i - 1] * a[i] >= 0:
        a2[i] = -a[i]
    else:
        a2[i] = a[i]

    if b * (b + a2[i]) >= 0:
        a2[i] = -f(a2[i - 1]) - b1
        if b1 + a2[i] == 0:
            a2[i] += f(a2[i])
        ans1 += abs(a2[i] - a[i])

    b1 += a2[i]

print(min(ans1, ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
a1 = [a[0]] * n
a2 = [-a[0]] * n
b = a[0]
b1 = a2[0]
ans = 0
ans1 = abs(a[0] * 2)


def f(x):
    if x == 0:
        return 0
    else:
        return x // abs(x)


for i in range(1, n):
    if a1[i - 1] * a[i] >= 0:
        a1[i] = -a[i]
    else:
        a1[i] = a[i]

    if b * (b + a1[i]) >= 0:
        a1[i] = -f(a1[i - 1]) - b
        if b + a1[i] == 0:
            a1[i] += f(a1[i])
        ans += abs(a1[i] - a[i])

    b += a1[i]

for i in range(1, n):
    if a2[i - 1] * a[i] >= 0:
        a2[i] = -a[i]
    else:
        a2[i] = a[i]

    if b * (b + a2[i]) >= 0:
        a2[i] = -f(a2[i - 1]) - b1
        if b1 + a2[i] == 0:
            a2[i] += f(a2[i])
        ans1 += abs(a2[i] - a[i])

    b1 += a2[i]

print(min(ans1, ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
a1 = [a[0]] * n
a2 = [-a[0]] * n
b = a[0]
b1 = a2[0]
ans = 0
ans1 = abs(a[0] * 2)


def f(x):
    if x == 0:
        return 0
    else:
        return x // abs(x)


for i in range(1, n):
    if a1[i - 1] * a[i] >= 0:
        a1[i] = -a[i]
    else:
        a1[i] = a[i]

    if b * (b + a[i]) >= 0:
        a1[i] = -f(a1[i - 1]) - b
        if b + a1[i] == 0:
            a1[i] += f(a1[i])
        ans += abs(a1[i] - a[i])

    b += a1[i]

for i in range(1, n):
    if a2[i - 1] * a[i] >= 0:
        a2[i] = -a[i]
    else:
        a2[i] = a[i]

    if b * (b + a[i]) >= 0:
        a2[i] = -f(a2[i - 1]) - b1
        if b1 + a2[i] == 0:
            a2[i] += f(a2[i])
        ans1 += abs(a2[i] - a[i])

    b1 += a2[i]

print(min(ans1, ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
a1 = [a[0]] * n
a2 = [-a[0]] * n
b = a[0]
ans = 0
ans1 = 0


def f(x):
    if x == 0:
        return 0
    else:
        return x // abs(x)


for i in range(1, n):
    if a1[i - 1] * a[i] >= 0:
        a1[i] = -a[i]
    else:
        a1[i] = a[i]

    if b * (b + a[i]) >= 0:
        a1[i] = -f(a1[i - 1]) - b
        if b + a1[i] == 0:
            a1[i] += f(a1[i])
        ans += abs(a1[i] - a[i])

    b += a1[i]

print(ans)

Submission status: Wrong Answer

Solution code:
def solve():
    n = int(input())
    total,*A = map(int, input().split())
    
    count = 0
    for a in A:
        if total < 0:
            if a >= 0:
                if total + a <= 0:
                    count += -(total + a) + 1
                    total = 1
                else:
                    total += a
                    pass
            else:
                count += -(total + a) + 1
                total = 1
        else:
            if a <= 0:
                if total + a >= 0:
                    count += total + a + 1
                    total = -1
                else:
                    total += a
                    pass
            else:
                count += total + a + 1
                total = -1
    return count
    
if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
def solve():
    n = int(input())
    A = map(int, input().split())
    total = 0
    count = 0
    for a in A:
        if total > 0 and a < 0:
            if total + a >= 0:
                count += total + a + 1
                total = -1
            else:
                total += a
        elif total  > 0 and a >= 0:
            count += total + a + 1
            total = -1
        elif total < 0 and a > 0:
            if total + a <= 0:
                count += -(total + a) + 1
                total = 1
            else:
                total += a
        elif total < 0 and a <= 0:
            count += -total - a + 1
            total = 1
        else:
            total += a
    return count
    
if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
n=int(input())
N=list(map(int,input().split()))
emptylists=[]
number=0
for i in range(n):
    number+=N[i]
    emptylists.append(number)

emptylists.append(1)
ans1=0
i=0
use=0

while i<n:
    #偶数番が正、奇数番が負の時
    if i%2==0:
        #奇数番め
       # if emptylists[i]+use<0:
        if emptylists[i]+use>=0:
            ans1+=emptylists[i]+use+1  
            use=-1-emptylists[i]

    if i%2!=0:
        #偶数番め
       # if emptylists[i]+use>0: 
        if emptylists[i]+use<=0:
            ans1+=1-emptylists[i]-use
            use=1-emptylists[i]

    i+=1           
ans2=0
j=0
uses=0

while j<n:
    #偶数番が正、奇数番が負の時
    if j%2==0:
        #奇数番め
       # if emptylists[i]+uses<0:
        if emptylists[j]+uses>=0:
            ans2+=emptylists[j]+uses+1  
            uses=-1-emptylists[j]

    if j%2!=0:
        #偶数番め
       # if emptylists[j]+uses>0:       
        if emptylists[j]+uses<=0:
            ans2+=1-emptylists[j]-uses
            uses=1-emptylists[j]

    j+=1           
print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

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

def get_count(summary):
    count = 0
    for i in range(1, N):
    #    print(summary)

        # 次はマイナス
        if summary > 0:
            # 条件を満たしてる？
            if (summary + A[i]) < 0:
                summary += A[i]
            else:
                # プラスになっちゃってるので修正
                summary += A[i]
                count += (summary+1)
                summary = -1

        # 次はプラス
        else:
            if (summary + A[i]) > 0:
                summary += A[i]
            else:
                # マイナスになっちゃってるので修正
                summary += A[i]
                count += abs(summary)+1
                summary = 1

    return count

if A[0] > 0:
    plus = get_count(A[0])
    minus = get_count(-1)
    minus += (A[0]+1)
elif A[0] == 0:
    plus = get_count(1)+1
    minus = get_count(-1)+1
else:
    plus = get_count(1)
    plus += (abs(plus)+1)
    minus = get_count(A[0])

print(min(plus, minus))

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

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

def get_count(summary):
    count = 0
    for i in range(1, N):
    #    print(summary)

        # 次はマイナス
        if summary > 0:
            # 条件を満たしてる？
            if (summary + A[i]) < 0:
                summary += A[i]
            else:
                # プラスになっちゃってるので修正
                summary += A[i]
                count += abs(-1-summary)
                summary = -1

        # 次はプラス
        else:
            if (summary + A[i]) > 0:
                summary += A[i]
            else:
                # マイナスになっちゃってるので修正
                summary += A[i]
                count += abs(1-summary)
                summary = 1

    return count

if A[0] > 0:
    plus = get_count(A[0])
    minus = get_count(-1)
    minus += (A[0]+1)
elif A[0] == 0:
    plus = get_count(1)+1
    minus = get_count(-1)+1
else:
    minus = get_count(A[0])
    plus = get_count(1)
    plus += (abs(plus)+1)

print(min(plus, minus))

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

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

def get_count(summary):
    count = 0
    for i in range(1, N):
    #    print(summary)

        # 次はマイナス
        if summary > 0:
            # 条件を満たしてる？
            if (summary + A[i]) < 0:
                summary += A[i]
            else:
                # プラスになっちゃってるので修正
                summary += A[i]
                count += abs(-1-summary)
                summary = -1

        # 次はプラス
        else:
            if (summary + A[i]) > 0:
                summary += A[i]
            else:
                # マイナスになっちゃってるので修正
                summary += A[i]
                count += abs(1-summary)
                summary = 1

    return count

if A[0] > 0:
    plus = get_count(A[0])
    minus = get_count(-1)
    minus += (A[0]+1)
elif A[0] == 0:
    plus = get_count(0)+1
    minus = get_count(-1)+1
else:
    minus = get_count(A[0])
    plus = get_count(1)
    plus += (abs(plus)+1)

print(min(plus, minus))

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

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

def get_count(summary):
    count = 0
    for i in range(1, N):
    #    print(summary)

        # 次はマイナス
        if summary > 0:
            # 条件を満たしてる？
            if (summary + A[i]) < 0:
                summary += A[i]
            else:
                # プラスになっちゃってるので修正
                summary += A[i]
                count += abs(-1-summary)
                summary = -1

        # 次はプラス
        else:
            if (summary + A[i]) > 0:
                summary += A[i]
            else:
                # マイナスになっちゃってるので修正
                summary += A[i]
                count += abs(1-summary)
                summary = 1

    return count

if A[0] > 0:
    plus = get_count(A[0])
    minus = get_count(-1*A[0])
    minus += (A[0]+1)
else:
    minus = get_count(A[0])
    plus = get_count(-1*A[0])
    plus += abs(-1-A[0])

print(min(plus, minus))

Submission status: Wrong Answer

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


def calc(ls):
    count = 0
    acc = ls[0]
    sign = acc // abs(acc)
    for x in ls[1:]:
        y = acc + x
        if y == 0 or y // abs(y) == sign:
            z = -sign - y
            count += abs(z)
            acc = -sign
        else:
            acc = y
        sign *= -1
    return count


if ls[0] != 0:
    count = calc(ls)
else:
    ls0 = ls.copy()
    ls0[0] = 1
    ls1 = ls.copy()
    ls1[0] = -1
    count = min(calc(ls0), calc(ls1)) + 1
print(count)

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

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

count = 0
summary = A[0]
for i in range(1, N):
#    print(summary)

    # 次はマイナス
    if summary > 0:
        # 条件を満たしてる？
        if (summary + A[i]) < 0:
            summary += A[i]
        else:
            # プラスになっちゃってるので修正
            summary += A[i]
            count += abs(-1-summary)
            summary = -1

    # 次はプラス
    else:
        if (summary + A[i]) > 0:
            summary += A[i]
        else:
            # マイナスになっちゃってるので修正
            summary += A[i]
            count += abs(1-summary)
            summary = 1

print(count)

Submission status: Wrong Answer

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

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

count = 0
summary = A[0]
for i in range(1, N):
#    print(summary)

    # 次はマイナス？
    if summary > 0:
        # 条件を満たしてる？
        if (summary + A[i]) < 0:
            summary += A[i]
        else:
            # 必要な数
            diff = -1-summary-A[i]
            count += abs(diff)
            summary = -1

    else:
        # 次はプラス
        if (summary + A[i]) > 0:
            summary += A[i]
        else:
            # 必要な数
            diff = 1-summary-A[i]
            count += abs(diff)
            summary = 1

print(count)

Submission status: Wrong Answer

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

SUM = 0
ans = 0
flag = 0 #0+,1-
for i in range(N):
    if i == 0:
        SUM = A[i]
        if SUM == 0:
            SUM+=1
            ans+=1
        elif SUM < 0:
            flag = 1
    else:
        SUM += A[i]
        if flag == 0:
            if SUM >= 0:
                ans += SUM+1
                SUM = -1
            flag = 1
        else:
            if SUM <= 0:
                ans += 1-SUM
                SUM = 1
            flag = 0
    #print(ans,SUM)

MIN = ans
ans = 0
flag = 1 #0+,1-
for i in range(N):
    if i == 0:
        SUM = A[i]
        if SUM == 0:
            SUM-=1
            ans+=1
        elif SUM > 0:
            flag = 0
    else:
        SUM += A[i]
        if flag == 0:
            if SUM >= 0:
                ans += SUM+1
                SUM = -1
            flag = 1
        else:
            if SUM <= 0:
                ans += 1-SUM
                SUM = 1
            flag = 0
    #print(ans,SUM)
                
ans = min(MIN,ans)
print(ans)

Submission status: Wrong Answer

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

SUM = 0
ans = 0
flag = 0 #0+,1-
for i in range(N):
    if i == 0:
        SUM = A[i]
        if SUM == 0:
            SUM+=1
            ans+=1
        elif SUM < 0:
            flag = 1
    else:
        SUM += A[i]
        if flag == 0:
            if SUM >= 0:
                ans += SUM+1
                SUM = -1
            flag = 1
        else:
            if SUM <= 0:
                ans += 1-SUM
                SUM = 1
            flag = 0
    #print(ans,SUM)
                
print(ans)

Submission status: Wrong Answer

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

s = a[0]
for i in range(1, n):
  is_plus = s > 0
  s += a[i]
  if s == 0:
    out += 1
    s = -1 if is_plus else 1
    continue
  if (s > 0) != is_plus:
    continue
  if s > 0:
    out += s + 1
    s = -1
  else:
    out += 1 - s
    s = 1
    
print(out)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
a_s = input().split()
for i in range(N):
    a_s[i] = int(a_s[i])
a_s = np.array(a_s)
    
#sum = 1,-1,1, ...
ans1 = 0
sum0 = 0
for i ,a in enumerate(a_s):
    sum1 = sum0 + a
    if i==0:
        if sum1>0:
            pass
        else:
            ans1 += abs(1 - sum1)
            sum1 = 1
            
    elif i%2==0:
        if sum1*sum0<0:
            pass
        else:
            ans1 += abs(1 - sum1)
            sum1 = 1
            
    else:
        if sum1*sum0<0:
            pass
        else:
            ans1 += abs(-1 - sum1)
            sum1 = -1
    sum0 = sum1
    
#sum = -1,1,-1, ...
ans2 = 0
sum0 = 0
for i ,a in enumerate(a_s):
    sum2 = sum0 + a
    if i==0:
        if sum2<0:
            pass
        else:
            ans2 += abs(-1 - sum2)
            sum2 = -1
            
    if i%2==0:
        if sum2*sum0<0:
            pass
        else:
            ans2 += abs(-1 - sum2)
            sum2 = -1
            
    else:
        if sum2*sum0<0:
            pass
        else:
            ans2 += abs(1 - sum2)
            sum2 = 1
    sum0 = sum2
    
ans = min(ans1,ans2)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
a_s = input().split()
for i in range(N):
    a_s[i] = int(a_s[i])
a_s = np.array(a_s)
    
#sum = 1,-1,1, ...
ans1 = 0
sum0 = 0
for i ,a in enumerate(a_s):
    sum1 = sum0 + a
    if i%2==0:
        if sum1*sum0<0:
            pass
        else:
            ans1 += abs(1 - sum1)
            sum1 = 1
    else:
        if sum1*sum0<0:
            pass
        else:
            ans1 += abs(-1 - sum1)
            sum1 = -1
    sum0 = sum1
    
#sum = -1,1,-1, ...
ans2 = 0
sum0 = 0
for i ,a in enumerate(a_s):
    sum2 = sum0 + a
    if i%2==0:
        if sum2*sum0<0:
            pass
        else:
            ans2 += abs(-1 - sum2)
            sum2 = -1
    else:
        if sum2*sum0<0:
            pass
        else:
            ans2 += abs(1 - sum2)
            sum2 = 1
    sum0 = sum2
    
ans = min(ans1,ans2)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
a_s = input().split()
for i in range(N):
    a_s[i] = int(a_s[i])
a_s = np.array(a_s)

def get_sign(x):
    if x>0:
        return +1
    elif x<0:
        return -1
    else:
        return 0

ans = 0

for i,a in enumerate(a_s):
    if i==0:
        if a!=0:
            S = a
        else:
            ans += 1
            if np.all(a)==0:
                S = +1
            else:
                for j in range(1,N):
                    if a_s[j]!=0:
                        S = get_sign(a_s[j])* ((-1)**j)
                        break
    else:
        S_tmp = S0 + a
        if  (get_sign(S_tmp)!=get_sign(S0))&(S_tmp!=0):
            S = S_tmp
        else: #S should be sign(S0)*-1
            S = get_sign(S0) * (-1)
            ans += abs(S - S_tmp)

    S0 = S

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
a_s = input().split()
for i in range(N):
    a_s[i] = int(a_s[i])
a_s = np.array(a_s)

def get_sign(x):
    if x>0:
        return +1
    elif x<0:
        return -1
    else:
        return 0

ans = 0
S0  = 0
for i,a in enumerate(a_s):
    if i==0:
        S = a
        if S == 0:
            ans += 1
            if np.all(a_s[1:])==0:
                S = +1
            else:
                for i in range(1,N):
                    if a_s[i]!=0:
                        S = get_sign(a_s[i])*((-1)**i)
                        break
    else:
        S = S0 + a
        if (get_sign(S0) == get_sign(S))|(get_sign(S)==0):
            if abs(S0)==1:
                ans += 2 + abs(a)
                a = get_sign(S0)*(-2)
                S = S0 + a
            else:
                ans += 1 + abs(a)
                a = get_sign(S0)*(-1)
                S = S0 + a
        else:
            pass
    S0 = S
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
a_s = input().split()
for i in range(N):
    a_s[i] = int(a_s[i])
a_s = np.array(a_s)

def get_sign(x):
    if x>0:
        return +1
    elif x<0:
        return -1
    else:
        return 0

ans = 0
S0  = None
for i,a in enumerate(a_s):
    if i==0:
        S = a
        if S == 0:
            ans += 1
            if np.all(a_s[1:])==0:
                S = +1
            else:
                for i in range(1,N):
                    if a_s[i]!=0:
                        S = get_sign(a_s[i])*(-1)
                        break
    else:
        S = S0 + a
        if get_sign(S0) == get_sign(S):
            ans += abs(get_sign(S)*(-1) - S)
            S = get_sign(S)*(-1)
        elif get_sign(S)==0:
            ans += 1
            S = get_sign(S0)*(-1)
    S0 = S
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
a_s = input().split()
for i in range(N):
  a_s[i] = int(a_s[i])
a_s = np.array(a_s)

def get_sign(x):
  if x>0:
    return +1
  elif x<0:
    return -1
  else:
    return 0

ans = 0
S0  = None
for i,a in enumerate(a_s):
  if i==0:
    S = a
    if S == 0:
      ans += 1
      S = get_sign(a_s[i+1])*(-1)
  else:
    S = S0 + a
    if get_sign(S0) == get_sign(S):
      ans += abs(get_sign(S)*(-1) - S)
      S = get_sign(S)*(-1)
    elif get_sign(S)==0:
      ans += 1
      S = get_sign(S0)*(-1)
  S0 = S
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())
a_s = input().split()
for i in range(N):
  a_s[i] = int(a_s[i])
a_s = np.array(a_s)

def get_sign(x):
  if x>0:
    return +1
  elif x<0:
    return -1
  else:
    return 0

ans = 0
for i,a in enumerate(a_s):
  if i==0:
    S = a
    if S == 0:
      ans += 1
      S = get_sign(a_s[1])*(-1)
  else:
    S = S0 + a
    if get_sign(S0) == get_sign(S):
      ans += abs(get_sign(S)*(-1) - S)
      S = get_sign(S)*(-1)
    elif get_sign(S)==0:
      ans += 1
      if i<N-1:
        S = get_sign(a_s[i+1])*(-1)
      elif i==N-1:
        pass
  S0 = S
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
sum = 0
A = [0] * n
for i, num in enumerate(input().split()):
  sum += int(num)
  A[i] = sum
print(A)
  
counter_plus = 0
is_plus = True
temp = 0
for i in range(n):
  if is_plus == True:
    temp += -min(0, A[i] - 1 + temp)
    print(is_plus, temp)
    counter_plus += temp
  else:
    temp = -max(0, A[i] + 1 + temp)
    print(is_plus, temp)
    counter_plus += -temp
  is_plus = not(is_plus)

print()
  
counter_minus = 0
is_plus = False
temp = 0
for i in range(n):
  if is_plus == True:
    temp += -min(0, A[i] - 1 + temp)
    print(is_plus, temp)
    counter_minus += temp
  else:
    temp = -max(0, A[i] + 1 + temp)
    print(is_plus, temp)
    counter_minus += -temp
  is_plus = not(is_plus)
print(min(counter_plus, counter_minus))

Submission status: Wrong Answer

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

ans = []
for first in (_arr[0], -1 * _arr[1] + 1, -1 * _arr[1] - 1):
    arr = _arr[:]
    c = 0
    prev = 0
    for i in range(n):
        t = prev + arr[i]
        if i == 0:
            c += abs(arr[i] - first)
            arr[i] = first
        elif prev > 0 and t >= 0:
            diff = t + 1
            c += diff
            arr[i] -= diff
        elif prev < 0 and t <= 0:
            diff = -1 * t + 1
            c += diff
            arr[i] += diff
        prev += arr[i]
    ans.append(c)
print(min(ans))

Submission status: Wrong Answer

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

ans = []
for first in (_arr[0], 1, -1):
    arr = _arr[:]
    c = 0
    prev = 0
    for i in range(n):
        t = prev + arr[i]
        if i == 0:
            arr[i] = first
        elif prev > 0 and t >= 0:
            diff = t + 1
            c += diff
            arr[i] -= diff 
        elif prev < 0 and t <= 0:
            diff = -1 * t + 1
            c += diff
            arr[i] += diff
        prev += arr[i]
    ans.append(c)
print(min(ans))

Submission status: Wrong Answer

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

ans = []
for first in (_arr[0], 1, -1):
    arr = _arr[:]
    c = 0
    prev = 0
    for i in range(n):
        t = prev + arr[i]
        if i == 0:
            arr[i] = first
        elif prev > 0 and t >= 0:
            diff = t + 1
            c += diff
            arr[i] -= diff 
        elif prev < 0 and t <= 0:
            diff = -1 * t + 1
            c += diff
            arr[i] += diff
        prev += arr[i]
    ans.append(c)
    print(arr)
print(min(ans))

Submission status: Wrong Answer

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

ans = []
for pm in (1, -1):
    arr = _arr[:]
    c = 0
    prev = 0
    for i in range(n):
        t = prev + arr[i]
        if prev == 0 and t == 0:
            arr[i] = pm
        elif prev > 0 and t >= 0:
            diff = t + 1
            c += diff
            arr[i] -= diff 
        elif prev < 0 and t <= 0:
            diff = -1 * t + 1
            c += diff
            arr[i] += diff
        prev += arr[i]
    ans.append(c)
print(min(ans))

Submission status: Wrong Answer

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

c = 0
prev = 0
for i in range(n):
    t = prev + arr[i]
    if i > 0:
        if prev > 0 and t >= 0:
            diff = t + 1
            c += diff
            arr[i] -= diff 
        elif prev < 0 and t <= 0:
            diff = -1 * t + 1
            c += diff
            arr[i] += diff
    prev += arr[i]
print(c)

Submission status: Wrong Answer

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

cnt_1 = 0
base = 0
#奇数番目が正
for i in range(n):
    if ((base + a[i] >= 0) and (i % 2 == 1)) or ((base + a[i] <= 0) and (i % 2 == 0)):
        cnt_1 += (1 + abs(base + a[i]))
        base += (1 + abs(base + a[i])) * (-1) ** i #調整後のa[0]~a[i]の和
    else:
      base += a[i] #調整後のa[0]~a[i]の和


cnt_2 = 0
base = 0
for i in range(n):
    if ((base + a[i] <= 0) and (i % 2 == 1)) or ((base + a[i] >= 0) and (i % 2 == 0)):
        #奇数番目が負
        cnt_2 += (1 + abs(base + a[i]))
        base -= (1 + abs(base + a[i])) * (-1) ** i #調整後のa[0]~a[i]の和
    else:
      base += a[i] #調整後のa[0]~a[i]の和

print(min(cnt_1, cnt_2))

Submission status: Wrong Answer

Solution code:
def main():
    n=int(input())
    a=list(map(int,input().split(' ')))
    sumcum = a[0]
    ans = 0
    if a[0] == 0:
        a[0] = a[1]/abs(a[1])
        ans += 1
        sumcum += a[0]
    tmp = a[0]
    for i in a[1:]:
        if sumcum*(sumcum+i) <0:
            sumcum += i
            continue
        else:
            ans += abs(sumcum+i)+1
            sumcum = -1*sumcum/abs(sumcum)
    print(int(ans))

if __name__ == '__main__':
    main()