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:
# @oj: atcoder
# @id: hitwanyang
# @email: 296866643@qq.com
# @date: 2020-08-17 17:23
# @url:https://atcoder.jp/contests/abc059/tasks/arc072_a

import sys,os
from io import BytesIO, IOBase
import collections,itertools,bisect,heapq,math,string
from decimal import *
# region fastio

BUFSIZE = 8192

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------------------
def main():
    n=int(input())
    a=list(map(int,input().split()))
    prefix=[a[0]]
    for i in range(1,len(a)):
        prefix.append(prefix[-1]+a[i])
    print (prefix)
    s,t=0,0
    ans1,ans2=0,0
    for i in range(len(prefix)):
        if i%2==0:
            if prefix[i]+s<=0:
                ans1+=abs(prefix[i]+s)+1
                s=s+abs(prefix[i]+s)+1
        else:
            if prefix[i]+s>=0:
                ans1+=abs(prefix[i]+s+1)
                s=s-abs(prefix[i]+s+1)
    for i in range(len(prefix)):
        if i%2==1:
            if prefix[i]+t<=0:
                ans2+=abs(prefix[i]+t)+1
                t=t+abs(prefix[i]+t)+1
        else:
            if prefix[i]+t>=0:
                ans2+=abs(prefix[i]+t+1)
                t=t-abs(prefix[i]+t+1)
    print (min(ans1,ans2))
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

sum_a = 0
ans = 0 
zero = 0 #数列aの先頭に並ぶ0の個数
for x in a:
    if sum_a > 0 and sum_a + x >= 0:
        ans += sum_a + x + 1 #sum_aを-1にするのにかかるコスト
        sum_a = -1
    elif sum_a < 0 and sum_a + x <= 0:
        ans += -(sum_a + x) + 1 #sum_aを+1にするのにかかるコスト
        sum_a = 1
    else:
        sum_a += x
    if sum_a == 0:
        zero += 1
if zero != 0:
    ans += 2*zero - 1 
print(ans)

Submission status: Wrong Answer

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

sum_a = 0
ans = 0
for x in a:
    if sum_a > 0 and sum_a + x >= 0:
        ans += sum_a + x + 1 #sum_aを-1にするのにかかるコスト
        sum_a = -1
    elif sum_a < 0 and sum_a + x <= 0:
        ans += -(sum_a + x) + 1 #sum_aを+1にするのにかかるコスト
        sum_a = 1
    else:
        sum_a += x
print(ans)

Submission status: Wrong Answer

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

sum = a[0]
change = 0

for i in range(1,n):
  val = 0
  tempsum = sum+a[i]
  if sum < 0 and tempsum <=0:
    val = 1 - tempsum
  if sum > 0 and tempsum >=0:
    val = -1 - tempsum
    
  sum = tempsum + val
  change += abs(val)
  
print(change)

Submission status: Wrong Answer

Solution code:
#
# abc059 c
#

import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """4
1 -3 1 0"""
        output = """4"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """5
3 -6 4 -5 7"""
        output = """0"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """6
-1 4 3 2 -5 4"""
        output = """8"""
        self.assertIO(input, output)


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

    ans = 0
    s = A[0]
    f = A[0] // abs(A[0])
    for i in range(1, N):
        a = A[i]
        if f == 1 and s+a >= 0:
            ans += abs(s+a) + 1
            s = -1
        elif f == -1 and s+a <= 0:
            ans += abs(s+a) + 1
            s = 1
        else:
            s += a
        f = s // abs(s)
    print(ans)


if __name__ == "__main__":
    unittest.main()
    # resolve()

Submission status: Wrong Answer

Solution code:
def sign(X):
    if X==0:
        return 0
    else:
        return [-1,1][X>0]

N = int(input())
A = [int(T) for T in input().split()]
Count = 0
SumNow = 0
SumRes = 0
for TA in range(0,N):
    SumNow += A[TA]
    if SumNow==0:
        if sign(SumRes)==1:
            Count += 1
            SumNow = -1
        else:
            Count += 1
            SumNow = 1
    else:
        if sign(SumNow)==sign(SumRes)==1:
            Count += SumNow+1
            SumNow = -1
        elif sign(SumNow)==sign(SumRes)==-1:
            Count += 1-SumNow
            SumNow = 1
    SumRes = SumNow
print(Count)

Submission status: Wrong Answer

Solution code:
def sign(X):
    if X==0:
        return 0
    else:
        return [-1,1][X>0]

N = int(input())
A = [int(T) for T in input().split()]
Count = 0
SumNow = 0
SumRes = 0
for TA in range(0,N):
    SumNow += A[TA]
    if SumNow==0 or sign(SumNow)==sign(SumRes):
        if SumNow==0 and sign(SumRes)==1:
            Count += 1
            SumNow = -1
        elif SumNow==0 and sign(SumRes)==-1:
            Count += 1
            SumNow = 1
        elif sign(SumNow)==1 and sign(SumRes)==1:
            Count += SumNow+1
            SumNow = -1
        elif sign(SumNow)==-1 and sign(SumRes)==-1:
            Count += 1-SumNow
            SumNow = 1
    SumRes = SumNow
print(Count)

Submission status: Wrong Answer

Solution code:
def solve():

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

   # print(a)
   # print(b)

    i = 0
    sum = 0

    ans = 0

    for i in range(n-1):
        sum += a[i]
        if sum > 0 and sum+a[i+1] > 0:
            tmp = -1 - sum
            ans += abs(tmp - a[i+1])
            a[i+1] = tmp
        elif sum < 0 and sum+a[i+1] < 0:
            tmp = 1 - sum
            ans += abs(tmp - a[i+1])
            a[i+1] = tmp

   # print(ans)
   # print(a)

    tmp_ans_1 = ans

    ans = 0
    sum = 0
    i = 0

    if b[0] > 0:
        while b[0] >= 0:
            b[0] -= 1
            ans += 1
    elif b[0] < 0:
        while b[0] <= 0:
            b[0] += 1
            ans += 1

    for i in range(n-1):
        sum += b[i]
        if sum > 0 and sum+b[i+1] > 0:
            tmp = -1 - sum
            ans += abs(tmp - b[i+1])
            b[i+1] = tmp
        elif sum < 0 and sum+b[i+1] < 0:
            tmp = 1 - sum
            ans += abs(tmp - b[i+1])
            b[i+1] = tmp

   # print(ans)
   # print(b)           
    
    tmp_ans_2 = ans
    
    if tmp_ans_1 <= tmp_ans_2:
        ans = tmp_ans_1
    elif tmp_ans_2 < tnp_ans_1:
        ans = tmp_ans_2

    print(ans)

if __name__ ==  "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#s, t = map(str, input().split())
#l = list(map(int, input().split()))
#l = [list(map(int,input().split())) for i in range(n)]
#a = [input() for _ in range(n)]

import copy

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

b = copy.copy(a)

lastSum = a[0]
nowSum = a[0]
for i in range(1, n):
    nowSum += a[i]
    #print(nowSum)
    if lastSum > 0:
        if nowSum >=0:
            a[i] = a[i] - (nowSum+1)
            nowSum +=  a[i] - b[i]
    else: #lastSum < 0
        if nowSum <= 0:
            a[i] = a[i] - (nowSum-1)
            nowSum +=  a[i] - b[i]
    lastSum += a[i]
#print(a)
ans = 0
for i in range(n):
    ans += abs(a[i]-b[i])
print(ans)

Submission status: Wrong Answer

Solution code:
def solve():

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

    i = 0
    sum = 0

    ans = 0

    for i in range(n-1):
        sum += a[i]
        if sum > 0 and sum+a[i+1] > 0:
            tmp = -1 - sum
            ans += abs(tmp - a[i+1])
            a[i+1] = tmp
        elif sum < 0 and sum+a[i+1] < 0:
            tmp = 1 - sum
            ans += abs(tmp - a[i+1])
            a[i+1] = tmp

    if sum > 0 and sum+a[n-1] > 0:
        tmp = -1 -sum
        ans += abs(tmp - a[n-1])
        a[n-1] = tmp
    elif sum < 0 and sum+a[n-1] < 0:
        tmp = 1 - sum
        ans += abs(tmp - a[i-1])
        a[n-1] = tmp

    print(ans)
   # print(a)            

if __name__ ==  "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
def solve():

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

    i = 0
    sum = 0

    ans = 0

    for i in range(n-1):
        sum += a[i]
        if sum > 0 and sum+a[i+1] > 0:
            tmp = -1 - sum
            ans += abs(tmp - a[i+1])
            a[i+1] = tmp
        elif sum < 0 and sum+a[i+1] < 0:
            tmp = 1 - sum
            ans += abs(tmp - a[i+1])
            a[i+1] = tmp

    print(ans)
   # print(a)            

if __name__ ==  "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
def solve():

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

    i = 0
    sum = 0

    ans = 0

    for i in range(n-1):
        sum += a[i]
        if sum > 0 and sum+a[i+1] > 0:
            tmp = -1 - sum
            ans += abs(tmp - a[i+1])
            a[i+1] = tmp
        elif sum < 0 and sum+a[i+1] < 0:
            tmp = 1 - sum
            ans += abs(tmp - a[i+1])
            a[i+1] = tmp

    print(ans)
    print(a)            

if __name__ ==  "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
def solve():

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

    i = 0
    sum = 0

    ans = 0

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

    print(ans)
    #print(a)            
        













if __name__ ==  "__main__":
    solve()

Submission status: Wrong Answer

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

a = A[0]
ans = 0
for i in range(1,n):
    b = A[i]
    if a == 0:
        if b < 0:
            a = -b-1
            ans = a
        else:
            a = -b+1
            ans = -a
            
    if a> 0:
        if a + b >= 0:
            ans += abs(-1-a-b)
            a = -1
        else:
            a += b
    elif a < 0:
        if a+b <= 0:
            ans += abs(1-a-b)
            a = 1
        else:
            a += b
print(ans)

Submission status: Wrong Answer

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

a = A[0]
ans = 0
for i in range(1,n):
    b = A[i]
    if a> 0:
        if a + b >= 0:
            ans += abs(-1-a-b)
            a = -1
        else:
            a += b
    elif a < 0:
        if a+b <= 0:
            ans += abs(1-a-b)
            a = 1
        else:
            a += b
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
sm=A[0]
cnt=0
if A[0]>=0:
    for i in range(1,N):
        sm+=A[i]
        if i%2==1:
            if sm>=0:               
                cnt+=sm+1
                sm=-1
        else:
            if sm<=0:               
                cnt+=sm*-1+1
                sm=1
elif A[0]<0:
    for i in range(1,N):
        sm+=A[i]
        if i%2==1:
            if sm<=0:
                cnt+=sm*-1+1
                sm=1
        else:
            if sm>=0:
                cnt+=sm+1
                sm=-1
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
sm=A[0]
cnt=0
if A[0]>0:
    for i in range(1,N):
        sm+=A[i]
        if i%2==1:
            if sm>=0:               
                cnt+=sm+1
                sm=-1
        else:
            if sm<=0:               
                cnt+=sm*-1+1
                sm=1
elif A[0]<0:
    for i in range(1,N):
        sm+=A[i]
        if i%2==1:
            if sm<=0:
                cnt+=sm*-1+1
                sm=1
        else:
            if sm>=0:
                cnt+=sm+1
                sm=-1
elif A[0]==0:
    a=0
    for i in range(1,N):
        if A[i]==0:
            a=i
        else:
            break
    if A[a+1]>0:
        sm=-1
        for i in range(a+1,N):
            sm+=A[i]
            if i%2==1:
                if sm>=0:
                    cnt+=sm+1
                    sm=-1
            else:
                if sm<=0:
                    cnt+=sm*-1+1
                    sm=1
    if A[a+1]<0:
        sm=1
        for i in range(a+1,N):
            sm+=A[i]
            if i%2==1:
                if sm<=0:
                    cnt+=sm*-1+1
                    sm=1
            else:
                if sm>=0:
                    cnt+=sm+1
                    sm=-1
    cnt+=2*a+1
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
sm=A[0]
cnt=0
if A[0]>0:
    for i in range(1,N):
        sm+=A[i]
        if i%2==1:
            if sm>=0:               
                cnt+=sm+1
                sm=-1
        else:
            if sm<=0:               
                cnt+=sm*-1+1
                sm=1
elif A[0]<0:
    for i in range(1,N):
        sm+=A[i]
        if i%2==1:
            if sm<=0:
                cnt+=sm*-1+1
                sm=1
        else:
            if sm>=0:
                cnt+=sm+1
                sm=-1
elif A[0]==0:
    a=0
    for i in range(1,N):
        if A[i]==0:
            a=i
        else:
            break
    if A[a+1]>0:
        sm=-1
        for i in range(a+1,N):
            sm+=A[i]
            if i%2==1:
                if sm>=0:
                    cnt+=sm+1
                    sm=-1
            else:
                if sm<=0:
                    cnt+=sm*-1+1
                    sm=1
    if A[a+1]<0:
        sm=-1
        for i in range(a+1,N):
            sm+=A[i]
            if i%2==1:
                if sm<=0:
                    cnt+=sm*-1+1
                    sm=1
            else:
                if sm>=0:
                    cnt+=sm+1
                    sm=-1
    cnt+=2*a+1
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
sm=A[0]
cnt=0
if A[0]>0:
    for i in range(1,N):
        sm+=A[i]
        if i%2==1:
            if sm>=0:               
                cnt+=sm+1
                sm=-1
        else:
            if sm<=0:               
                cnt+=sm*-1+1
                sm=1
elif A[0]<0:
    for i in range(1,N):
        sm+=A[i]
        if i%2==1:
            if sm<=0:
                cnt+=sm*-1+1
                sm=1
        else:
            if sm>=0:
                cnt+=sm+1
                sm=-1
elif A[0]==0:
    a=0
    for i in range(1,N):
        if A[i]==0:
            a=i
        else:
            break
    if A[a+1]>0:
        sm=-1
        for i in range(a+1,N):
            sm+=A[i]
            if i%2==1:
                if sm>=0:
                    cnt+=sm+1
                    sm=-1
            else:
                if sm<=0:
                    cnt+=sm*-1+1
                    sm=1
        cnt+=2*a+1

print(cnt)

Submission status: Wrong Answer

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

cost=0
cost+=abs(a[0]+1)
sums=(a[0]//abs(a[0]))*-1
for i in range(1,n):
  m=sums+a[i]
  if m==0:
    if i==n-1:
      cost+=1
      break
    else:
      if sums>0:
        m=-1
        cost+=1
      else:
        m=1
        cost+=1
  else:
    if m*sums>0:
      if sums>0:
        cost+=m+1
        m=-1
      else:
        cost+=abs(m)+1
        m=1
  sums=m
print(min(ans,cost))

Submission status: Wrong Answer

Solution code:
cost=0
n=int(input())
a=list(map(int,input().split()))
sums=a[0]
if a[0]==0:
  if a[1]>0:
    a[0]=-1
  else:
    a[0]=1
  cost+=1
for i in range(1,n):
  m=sums+a[i]
  if m==0:
    if i==n-1:
      cost+=1
      break
    else:
      if sums>0:
        m=-1
        cost+=1
      else:
        m=1
        cost+=1
  else:
    if m*sums>0:
      if sums>0:
        cost+=m+1
        m=-1
      else:
        cost+=abs(m)+1
        m=1
  sums=m
print(cost)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = list(mapint())

cum = As.pop(0)
if not cum==0:
    cum2 = cum
    ans = 0
    for a in As:
        if cum*(cum+a)>=0:
            ans += abs(cum+a)+1
            cum = -1 if cum>0 else 1
        else:
            cum += a
        
    ans2 = abs(cum2)+1
    cum2 = 1 if cum2<0 else -1
    for a in As:
        if cum2*(cum2+a)>=0:
            ans2 += abs(cum2+a)+1
            cum2 = -1 if cum2>0 else 1
        else:
            cum2 += a
    print(min(ans, ans2))
else:
    cum2 = -1
    ans = 1
    for a in As:
        if cum*(cum+a)>=0:
            ans += abs(cum+a)+1
            cum = -1 if cum>0 else 1
        else:
            cum += a
        
    ans2 = 1
    cum2 = 1
    for a in As:
        if cum2*(cum2+a)>=0:
            ans2 += abs(cum2+a)+1
            cum2 = -1 if cum2>0 else 1
        else:
            cum2 += a
    print(min(ans, ans2))

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def exit(*argv, **kwarg):
    print(*argv, **kwarg)
    sys.exit()


def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))
# 受け渡されたすべての要素からsubだけ引く.リストを*をつけて展開しておくこと


def a_int(): return int(read())


def ints(): return list(map(int, read().split()))


def read_col(H):
    '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_tuple(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_matrix(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter, xor, add
from itertools import accumulate, product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right
from functools import reduce
from math import gcd


def lcm(a, b):
    # 最小公倍数
    g = gcd(a, b)
    return a // g * b

# https://atcoder.jp/contests/abc059/tasks/arc072_a

# si=a1+..+aiと定義する
# s1...si,si+1,snの符号は+-+-... or -+-+...にしたい←両方試せばいい
# +-+-にするのに最小の操作は？


n = a_int()
A = ints()
S = list(accumulate(A))

#+-+-のとき
now = 1  # 正負
pad = 0
ans1 = 0
for s in S:
    s += pad
    if s * now < 0:  # 異符号だったら修正する必要あり
        if now == 1:  # +にしたい場合
            n_ope = 1 - s
            pad += n_ope
        else:  # -にしたい場合
            n_ope = s + 1
            pad -= n_ope
        ans1 += n_ope
    now *= -1

#-+-+のとき
now = -1  # 正負
pad = 0
ans2 = 0
for s in S:
    s += pad
    if s * now <= 0:  # 異符号だったら修正する必要あり
        if now == 1:  # +にしたい場合
            n_ope = 1 - s
            pad += n_ope
        else:  # -にしたい場合
            n_ope = s + 1
            pad -= n_ope
        ans2 += n_ope
    now *= -1

# print(S)
# print(ans1, ans2)
print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = list(mapint())

cum = As.pop(0)
cum2 = cum
ans = 0
for a in As:
    if cum*(cum+a)>=0:
        ans += abs(cum+a)+1
        cum = -1 if cum>0 else 1
    else:
        cum += a
ans2 = abs(cum2)+1
cum2 = 1 if cum2<0 else -1
for a in As:
    if cum2*(cum2+a)>=0:
        ans2 += abs(cum2+a)+1
        cum2 = -1 if cum2>0 else 1
    else:
        cum2 += a
print(min(ans, ans2))

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
As = list(mapint())

cum = As.pop(0)
ans = 0
for a in As:
    if cum*(cum+a)>=0:
        ans += abs(cum+a)+1
        cum = -1 if cum>0 else 1
    else:
        cum += a

print(ans)

Submission status: Wrong Answer

Solution code:
def solve():
  ans = float('inf')
  N = int(input())
  A = list(map(int, input().split()))
  if A[0]==0:
    lis = [1,-1]
  else:
    lis = [A[0],-A[0]//abs(A[0])]
  for l in lis:
    cnt = 0
    total = l
    for i in range(1,N):
      new_total = total+A[i]
      if total*(new_total)>=0:
        new_total = (-1)*total//abs(total)
        cnt += abs(new_total-(total+A[i]))
      total = new_total
    ans = min(ans,cnt)
  return ans
print(solve())

Submission status: Wrong Answer

Solution code:
def solve():
  ans = 0
  N = int(input())
  A = list(map(int, input().split()))
  total = A[0]
  if total==0:
    for i in range(1,N):
      if A[i]!=0:
        total = pow(-1,i)*A[i]//abs(A[i])
        break
    else:
      return 2*N-1
  for i in range(1,N):
    new_total = total+A[i]
    if total*(new_total)>=0:
      new_total = (-1)*total//abs(total)
      ans += abs(new_total-(total+A[i]))
    total = new_total
  return ans
print(solve())

Submission status: Wrong Answer

Solution code:
def solve():
  ans = 0
  N = int(input())
  A = list(map(int, input().split()))
  total = A[0]
  if total==0:
    for i in range(1,N):
      if A[i]!=0:
        total = pow(-1,i)*A[i]//abs(A[i])
        break
    else:
      return N
  for i in range(1,N):
    new_total = total+A[i]
    if total*(new_total)>=0:
      new_total = (-1)*total//abs(total)
      ans += abs(new_total-(total+A[i]))
    total = new_total
  return ans
print(solve())

Submission status: Wrong Answer

Solution code:
import copy
n=int(input())
a=[0]+list(map(int,input().split()))
b=copy.copy(a)
psum=0
nsum=0
sum1=0
sum2=0
for i in range(1,n+1):
    if i%2!=0:
        psum+=max([0,1-sum1-a[i]])
        a[i]+=max([0,1-sum1-a[i]])
        sum1+=a[i]
    else:
        psum+=max([0,1+sum1+a[i]])
        a[i]-=max([0,1+sum1+a[i]])
        sum1+=a[i]
for i in range(1,n+1):
    if i%2!=0:
        nsum+=max([0,1+sum2+b[i]])
        b[i]-=max([0,1+sum2+b[i]])
        sum2+=b[i]
    else:
        nsum+=max([0,1-sum2-b[i]])
        a[i]+=max([0,1-sum2-b[i]])
        sum2+=b[i]

print(min([psum,nsum]))

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

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

acc = [0] * n
acc[0] = A[0]
for i in range(1, n):
    acc[i] = acc[i - 1] + A[i]

ans = 0
if acc[0] == 0:
    acc[0] += 1
    ans += 1
cur = acc[0]
x = 0
for i in range(1, n):
    acc[i] += x
    if cur > 0:
        if acc[i] >= 0:
            ans += acc[i] + 1
            x -= acc[i] + 1
            acc[i] = -1
    else:
        if acc[i] <= 0:
            ans += abs(acc[i]) + 1
            x += abs(acc[i]) + 1
            acc[i] = 1
    cur = acc[i]
print(ans)

Submission status: Wrong Answer

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

acc = [0] * n
acc[0] = A[0]
for i in range(1, n):
    acc[i] = acc[i - 1] + A[i]

ans = 0
cur = acc[0]
x = 0
for i in range(1, n):
    acc[i] += x
    if cur > 0:
        if acc[i] >= 0:
            ans += acc[i] + 1
            x -= acc[i] + 1
            acc[i] = -1
    else:
        if acc[i] <= 0:
            ans += abs(acc[i]) + 1
            x += abs(acc[i]) + 1
            acc[i] = 1
    cur = acc[i]
print(ans)

Submission status: Wrong Answer

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

acc = [0] * n
acc[0] = A[0]
for i in range(1, n):
    acc[i] = acc[i - 1] + A[i]

ans = 0
cur = acc[0]
x = 0
for i in range(1, n):
    acc[i] += x
    if cur > 0:
        if acc[i] >= 0:
            ans += acc[i] + 1
            x -= acc[i] + 1
            acc[i] = -1
    else:
        if acc[i] < 0:
            ans += abs(acc[i]) + 1
            x += abs(acc[i]) + 1
            acc[i] = 1
    cur = acc[i]

if acc[n - 1] == 0:
    ans += 1
print(ans)

Submission status: Wrong Answer

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

acc = [0] * n
acc[0] = A[0]
for i in range(1, n):
    acc[i] = acc[i - 1] + A[i]

ans = 0
cur = acc[0]
x = 0
for i in range(1, n):
    acc[i] += x
    if cur > 0:
        if acc[i] > 0:
            ans += acc[i] + 1
            x -= acc[i] + 1
            acc[i] = -1
    else:
        if acc[i] < 0:
            ans += abs(acc[i]) + 1
            x += abs(acc[i]) + 1
            acc[i] = 1
    cur = acc[i]

if acc[n - 1] == 0:
    ans += 1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def solve(A):
    ans = 0
    s = A[0]
    for a in A[1:]:
        prev, s = s, s + a
        if prev > 0 and s >= 0:
            ans += s + 1
            s = -1
        if prev < 0 and s <= 0:
            ans += -s + 1
            s = 1

    return ans


def main():
    N, *A = map(int, read().split())

    a0 = A[0]

    ans1 = 0
    if A[0] <= 0:
        ans1 = -A[0] + 1
        A[0] = 1
    ans1 = solve(A)

    A[0] = a0
    
    ans2 = 0
    if A[0] >= 0:
        ans2 = A[0] + 1
        A[0] = -1
    ans2 += solve(A)

    print(min(ans1, ans2))

    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def solve(A):
    ans = 0
    s = A[0]
    for a in A[1:]:
        prev, s = s, s + a
        if prev > 0 and s >= 0:
            ans += s + 1
            s = -1
        if prev < 0 and s <= 0:
            ans += -s + 1
            s = 1

    return ans


def main():
    N, *A = map(int, read().split())

    a0 = A[0]
    ans1 = 0
    if a0 <= 0:
        ans1 = -a0 + 1
        A[0] = 1
    ans1 = solve(A)

    A[0] = a0
    ans2 = 0
    if a0 >= 0:
        ans2 = a0 + 1
        A[0] = -1
    ans2 += solve(A)

    print(min(ans1, ans2))

    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def solve(A):
    ans = 0
    s = A[0]
    for a in A[1:]:
        prev, s = s, s + a
        if prev > 0 and s >= 0:
            ans += s + 1
            s = -1
        if prev < 0 and s <= 0:
            ans += -s + 1
            s = 1

    return ans


def main():
    N, *A = map(int, read().split())

    if A[0] != 0:
        ans = solve(A)
    else:
        A[0] = 1
        ans1 = solve(A)
        A[0] = -1
        ans2 = solve(A)
        ans = min(ans1, ans2)

    print(ans)

    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
arr1=list(map(int,input().split()))
arr2=arr1[:]
ans1=0
ans2=0

if arr1[0]<=0:
    ans1+=abs(arr1[0])+1
    arr1[0]=1
if arr2[0]>=0:
    ans2+=arr2[0]+1
    arr2[0]=-1

sum1=arr1[0]
for i in range(1,n):
    tmp=sum1+arr1[i]
    if i%2==1:
        if tmp>=0:
            ans1+=tmp+1
            sum1=-1
        else:
            sum1=tmp
    else:
        if tmp<=0:
            ans1+=abs(tmp)+1
            sum1=1
        else:
            sum1=tmp
sum2=arr2[0]
for i in range(1,n):
    tmp=sum2+arr2[i]
    if i%2==0:
        if tmp>=0:
            ans2+=tmp+1
            sum2=-1
        else:
            sum2=tmp
    else:
        if tmp<=0:
            ans2+=abs(tmp)+1
            sum2=1
        else:
            sum2=tmp
print(ans1,ans2)