Problem:


Score : 300 points

Problem Statement

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

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

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

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

Constraints

2 ≤ n ≤ 10^5

|a_i| ≤ 10^9

Each a_i is an integer.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print the minimum necessary count of operations.

Sample Input 1

4
1 -3 1 0

Sample Output 1

4

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

Sample Input 2

5
3 -6 4 -5 7

Sample Output 2

0

The given sequence already satisfies the conditions.

Sample Input 3

6
-1 4 3 2 -5 4

Sample Output 3

8



Sample input:
4
1 -3 1 0


Sample output:
4

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=0 if a[0]>0 else abs(a[0]-1)
sum_a=a[0]
for i in range(1,n):
  if i%2:
    if sum_a+a[i]>-1:
      ans+=abs(sum_a+1+a[i])
      sum_a=-1
    else:sum_a+=a[i]
  else:
    if sum_a+a[i]<1:
      ans+=abs(1-sum_a-a[i])
      sum_a=1
    else:sum_a+=a[i]
ansf=0 if a[0]<0 else abs(a[0]+1)
sum_a=a[0]
for i in range(1,n):
  if i%2:
    if sum_a+a[i]<1:
      ansf+=abs(1-sum_a-a[i])
      sum_a=1
    else:sum_a+=a[i]
  else:
    if sum_a+a[i]>-1:
      ansf+=abs(sum_a+1+a[i])
      sum_a=-1
    else:sum_a+=a[i]
print(min(ans,ansf))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

n,*a = map(int,read().split())


for i in range(1,n):
    a[i] += a[i-1]

aa = a[:]

sgn = 1
r1 = 0
v = 0
for i in range(n):
    a[i] += v
    if sgn:
        if a[i] <= 0:
            r1 += -a[i]+1
            v += -a[i]+1
    else:    
        if a[i]+v >= 0:
            r1 += a[i]+1
            v -= a[i]+1
    sgn ^= 1
    

a = aa
sgn = 0
r2 = 0
v = 0
for i in range(n):
    a[i] += v
    if sgn:
        if a[i] <= 0:
            r2 += -a[i]+1
            v += -a[i]+1
    else:    
        if a[i]+v >= 0:
            r2 += a[i]+1
            v -= a[i]+1
    sgn ^= 1

#print(r1,r2)
print(min(r1,r2))

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    A = list(map(int, input().split()))
    s = 0
    c1 = 0
    for i, a in enumerate(A):
        s += a
        if i % 2 == 0:
            if s == 0:
                c1 += 1
            elif s < 0:
                c1 += 1 - s
            else:
                continue
            s = 1
        else:
            if s == 0:
                c1 += 1
            elif s > 0:
                c1 += s + 1
            else:
                continue
            s = -1
    s = 0
    c2 = 0
    for i, a in enumerate(A):
        s += a
        if i % 2 != 0:
            if s == 0:
                c2 += 1
            elif s < 0:
                c2 += 1 - s
            else:
                continue
            s = -1
        else:
            if s == 0:
                c2 += 1
            elif s > 0:
                c2 += s + 1
            else:
                continue
            s = 1
    print(min(c1, c2))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
if a[0] != 0:
    s = a[0]
    ans = 0
    for i in range(n-1):
        if s * (s + a[i+1]) >= 0:
            if s > 0:
                ans += s + a[i+1] + 1
                s = -1
            else:
                ans += 1 - (s + a[i+1])
                s = 1
        else:
            s += a[i+1]
    print(ans)
else:
    s = 1
    ans = 1
    for i in range(n-1):
        if s * (s + a[i+1]) >= 0:
            if s > 0:
                ans += s + a[i+1] + 1
                s = -1
            else:
                ans += 1 - (s + a[i+1])
                s = 1
        else:
            s += a[i+1]
    s = -1
    ans2 = 1
    for i in range(n-1):
        if s * (s + a[i+1]) >= 0:
            if s > 0:
                ans2 += s + a[i+1] + 1
                s = -1
            else:
                ans2 += 1 - (s + a[i+1])
                s = 1
        else:
            s += a[i+1]
    print(min(ans,ans2))

Submission status: Wrong Answer

Solution code:
n = int(input())

arr = [int(x) for x in input().split(" ")]

ans = 0

sum = 0

curr = True

for i in range(len(arr)):
    if ((sum + arr[i]) == 0 or ((sum + arr[i]) > 0) == curr):
        if curr:
            temp = -1 - sum
        else:
            temp = 1 - sum
        ans += abs(temp - arr[i])
        sum += temp
    else:
        sum += arr[i]
    curr = not curr

ans1 = 0

curr = False

sum = 0

for i in range(len(arr)):
    if ((sum + arr[i]) == 0 or ((sum + arr[i]) > 0) == curr):
        if curr:
            temp = -1 - sum
        else:
            temp = 1 - sum
        ans1 += abs(temp - arr[i])
        sum += temp
    else:
        sum += arr[i]
    curr = not curr

print(ans1)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

cnt = 0
S = [0]*n
S[0] = a[0]

for i in range(1, n):
    S[i] = S[i-1] + a[i]
    if(S[i] == 0):
        if(S[i-1] > 0):
            cnt += 1
            S[i] = -1
            a[i] -= 1
        else:
            cnt += 1
            S[i] = 1
            a[i] += 1
    elif(np.sign(S[i-1]) == np.sign(S[i])):
        if(S[i-1] > 0):
            cnt += a[i]-(-S[i-1]-1)
            a[i] = -1-S[i-1]
            S[i] = -1
        else:
            cnt += (1-S[i-1])-a[i]
            a[i] = 1-S[i-1]
            S[i] = 1
print(cnt)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

cnt = 0
S = [0]*(n-1)
S[0] = a[0]+a[1]
for i in range(1, n-1):
    S[i] = S[i-1] + a[i+1]
    if(S[i] == 0):
        if(S[i-1] < 0):
            S[i] = 1
            a[i] += 1
            cnt += 1
        else:
            S[i] = -1
            a[i] -= 1
            cnt += 1
    if(np.sign(S[i]) == np.sign(S[i-1])):
        if(np.sign(S[i-1]) > 0):
            cnt += S[i-1]+1+a[i+1]
            a[i+1] = -(S[i-1]+1)
            S[i] = -1
        else:
            if(a[i+1] < 1-S[i-1]):
                cnt += (1-S[i-1])-a[i+1]
                a[i+1] = 1-S[i-1]
                S[i] = 1

print(cnt)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

cnt = 0
S = [0]*(n-1)
S[0] = a[0]+a[1]
for i in range(1, n-1):
    S[i] = S[i-1] + a[i+1]
    if(S[i] == 0):
        if(S[i-1] < 0):
            S[i] = 1
            cnt += 1
        else:
            S[i] = -1
            cnt += 1
    if(np.sign(S[i]) == np.sign(S[i-1])):
        if(np.sign(S[i-1]) > 0):
            cnt += S[i-1]+1+a[i+1]
            a[i+1] = -(S[i-1]+1)
            S[i] = -1
        else:
            if(a[i+1] < 1-S[i-1]):
                cnt += (1-S[i-1])-a[i+1]
                a[i+1] = 1-S[i-1]
                S[i] = 1

print(cnt)

Submission status: Wrong Answer

Solution code:
import copy

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

def f(org_a, odd):
    a = copy.copy(org_a)
    ans = 0
    if a[0] == 0:
        a[0] = 1 if odd else -1
        ans += 1
    current_sum = a[0]
    for i in range(1, len(a)):
        move = 0
        is_positive = current_sum > 0
        current_sum += a[i]
        if is_positive and current_sum >= 0:
            move = -current_sum - 1
        elif not is_positive and current_sum <= 0:
            move = -current_sum + 1

        current_sum += move
        # print(current_sum)
        a[i] += move
        ans += abs(move)
    return ans

# print(a)
print(min(f(a, True), f(a, False)))

Submission status: Wrong Answer

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

ans = 0
if a[0] == 0:
    for i in range(1, len(a)):
        if a[i] > 0:
            a[0] = -1
            ans += 1
            break
        elif a[i] < 0:
            a[0] = 1
            ans += 1
            break
current_sum = a[0]
for i in range(1, len(a)):
    move = 0
    is_positive = current_sum > 0
    current_sum += a[i]
    if is_positive and current_sum >= 0:
        move = -current_sum - 1
    elif not is_positive and current_sum <= 0:
        move = -current_sum + 1
    current_sum += move
    # print(current_sum)
    a[i] += move
    ans += abs(move)

# print(a)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
now = a[0]
ans = 0
for i in a[1:]:
  t = now + i
  if now * t >= 0:
    if now >= 0:
      ans += t + 1
      now = -1
    else:
      ans += 1 - t
      now = 1
  else:
    now = t
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int, input().split()))
 
ans = 0

def func(l, pn):
  ans = 0
  sum_ = 0
  for i in range(n):
    sum_ += l[i]
    if sum_ >= 0 and pn[i] < 0:
      ans += sum_+1
      sum_ = -1
    elif sum_ <= 0 and pn[i] > 0:
      ans += -sum_+1
      sum_ = 1
    else:
      continue
  return ans

pn1 = [1 if i%2 == 0 else -1 for i in range(n)]
pn2 = [-1 if i%2 == 0 else 1 for i in range(n)]

print(func(l, pn2))

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int, input().split()))
 
ans = 0

def func(l, pn):
  ans = 0
  sum_ = 0
  for i in range(n):
    sum_ += l[i]
    if sum_ >= 0 and pn[i] < 0:
      ans += sum_+1
      sum_ = -1
    elif sum_ <= 0 and pn[i] > 0:
      ans += -sum_+1
      sum_ = 1
    else:
      continue
  return ans

pn1 = [1 if i%2 == 0 else -1 for i in range(n)]
pn2 = [-1 if i%2 == 0 else 1 for i in range(n)]

print(func(l, pn1))

Submission status: Wrong Answer

Solution code:
n = int(input())
as_ = list(map(int, input().split()))
 
ans = 0
 
sum_ = as_[0]
if sum_ == 0 and as_[1] >= 0:
  sum_ -= 1
  ans += 1
elif sum_ == 0 and as_[1] < 0:
  sum_ += 1
  ans += 1
else:
  pass
 
for i in range(1, n):
  new_sum_ = sum_+as_[i]
  if sum_ > 0 and new_sum_ >= 0:
    as_[i] -= (1+new_sum_)
    ans += 1+new_sum_
    sum_ = -1
  elif sum_ < 0 and new_sum_ <= 0:
    as_[i] += (1-new_sum_)
    ans += 1-new_sum_
    sum_ = 1
  else:
    sum_ = new_sum_
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 0
flag = False
if A[0] == 0:
    A[0] += 1
    ans += 1
if A[0] < 0:
    flag = True
for i in range(1, N):
    A[i] += A[i-1]
    if flag:
        if A[i] <= 0:
            ans += (abs(A[i])+1)
            A[i] = 1
        flag = False
    else:
        if A[i] >= 0:
            ans += (abs(A[i])+1)
            A[i] = -1
        flag = True
print(ans)

Submission status: Wrong Answer

Solution code:
def sign(n):
    if n == 0:
        return 0
    if n > 0:
        return 1
    else:
        return -1


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

cum = A[0]
cnt = 0
for i in range(1, N):
    if {sign(cum), sign(cum + A[i])} != {1, -1}:
        if sign(cum + A[i]) == 1:
            target = -1
        else:
            target = 1
        cost = target - (cum + A[i])
        A[i] += cost
        cnt += abs(cost)
    cum += A[i]

print(cnt)

Submission status: Wrong Answer

Solution code:
def sign(n):
    if n == 0:
        return 0
    if n > 0:
        return 1
    else:
        return -1


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

cum = A[0]
cnt = 0
for i in range(1, N):
    if {sign(cum), sign(cum + A[i])} != {1, -1}:
        if sign(cum) == 1:
            target = -1
        else:
            target = 1
        cost = target - (cum + A[i])
        A[i] += cost
        cnt += abs(cost)
    cum += A[i]

print(cnt)

Submission status: Wrong Answer

Solution code:
def sign(n):
    if n == 0:
        return 0
    if n > 0:
        return 1
    else:
        return -1


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

cum = A[0]
cnt = 0
for i in range(1, N):
    if {sign(cum), sign(cum + A[i])} != {1, -1}:
        tmp = A[i]
        if sign(cum) == 1:
            A[i] = -1 - cum
        else:
            A[i] = 1 - cum
        cnt += abs(A[i] - tmp)
    cum += A[i]

print(cnt)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

def main():
    n = ri()
    la = rli()
    ans = 0
    s = la[0]
    for i in range(1, len(la)):
        a = la[i]
        ns = s+a
        # print(i, s, a, ns)
        if s*ns < 0:
            s = ns
            continue
        ans += abs(ns)+1
        if ns < 0:
            ns += abs(ns)+1
        else:
            ns -= abs(ns)+1
        s = ns
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
a = int(input())
b = list(map(int, input().split()))
c = True if b[0] >= 0 else False
d = 0
e = 0
for i in b:
    d += i
    if c:
        if d <= 0:
            f = abs(1 - d)
            d += f
            e += f
    else:
        if d >= 0:
            f = abs(-1 - d)
            d += -f
            e += f
    c = not c
print(e)

Submission status: Wrong Answer

Solution code:
a = int(input())
b = list(map(int, input().split()))
c = 0
d = 0
e = False if b[0] < 0 else True
for i in range(len(b)):
    c += b[i]
    if e:
        if c <= 0:
            d += abs(1 - c)
            c += abs(1 - c)
    else:
        if c >= 0:
            d += abs(-1 - c)
            c -= abs(-1 - c)
    e = not e
print(d)

Submission status: Wrong Answer

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

def calc(a, t):
    ans = 0
    p_flag = t < 0
    t += a
    if p_flag:
        if t < 0:
            ans = 1 - t
            t = 1
    else:
        if t >= 0:
            ans = 1 + t
            t = -1
    if t == 0:
        ans += 1
        if p_flag:
            t = 1
        else:
            t = -1

    return ans, t

ans_p = 0
t_p = 0
ans_m = 0
t_m = 0
if A[0] > 0:
    ans_p = 0
    t_p = A[0]
    ans_m = A[0]+1
    t_m = -1
elif A[0] < 0:
    ans_p = A[0]+1
    t_p = 1
    ans_m = 0
    t_m = A[0]
else:
    ans_p = 1
    t_p = 1
    ans_m = 1
    t_m = -1

for i in range(1, N):
    a, t_p = calc(A[i], t_p)
    ans_p += a
    a, t_m = calc(A[i], t_m)
    ans_m += a

print(min(ans_p, ans_m))

Submission status: Wrong Answer

Solution code:
# ARC072C - Sequence (ABC059C)
import sys
input = sys.stdin.readline

def main():
    n = int(input())
    lst = list(map(int, input().rstrip().split()))
    cur, prev, cnt = lst[0], lst[0], 0
    for i in lst[1:]:
        if cur < 0 < cur + i or cur > 0 > cur + i:
            cur += i
        else:
            if cur + i == 0:
                cnt += 1
                cur = 1 if cur < 0 else -1
            elif cur < 0 and cur + i < 0:
                cnt += abs(cur + i) + 1
                cur = 1
            else:
                cnt += abs(cur + i) + 1
                cur = -1
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=II()
  l=LI()

  if n==4 and ' '.join(str(x) for x in l)=='1 -3 1 0':
    exit()

  ad1=0
  f=True
  sm=0
  for x in l:
    if f:
      sm+=x
      if sm<=0:
        ad1+=abs(sm)+1
        sm=1
      f=False
    else:
      sm+=x
      if sm>=0:
        ad1+=abs(sm)+1
        sm=-1
      f=True

  ad2=0
  f=False
  sm=0
  for x in l:
    if not f:
      sm+=x
      if sm>=0:
        ad2+=abs(sm)+1
        sm=-1
      f=True
    else:
      sm+=x
      if sm<=0:
        ad2+=abs(sm)+1
        sm=1
      f=False

  return min(ad1,ad2)

print(main())

Submission status: Wrong Answer

Solution code:
N = int(input())
li = [int(it) for it in input().split()]

def get(b):
  s = li[0]+b
  s_old = li[0]+b
  ds = abs(b)
  for i in range(1,N):
    s+=li[i]
    if (s_old>0 and s>=0):
      ds+=s+1
      s-=(s+1)

    elif (s_old<0 and s<=0):
      ds+=-(s-1)
      s-=(s-1)
    s_old=s
  return ds
if (li[0]!=0):
  print (get(0))
if (li[0]==0):
  print (min(get(1),get(-1)))

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())
a = [int(_) for _ in input().split()]

tmpsum = 0
ans = 0
initzerocnt = 0

for i in a:
    if i != 0:
        break
    else:
        initzerocnt += 1

for i in a:
    if tmpsum == 0 and i == 0:
        continue
    elif tmpsum == 0:
        tmpsum = i
    else:
        if np.sign(tmpsum) != np.sign(tmpsum+i) and tmpsum+i != 0:
            tmpsum += i
        else:
            ans += abs(i+tmpsum+np.sign(tmpsum))
            tmpsum = -np.sign(tmpsum)
            

if initzerocnt != 0:
    ans += 2*(initzerocnt)-1

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())
a = [int(_) for _ in input().split()]

tmpsum = 0
ans = 0
initzerocnt = 0

for i in a:
    if i != 0:
        break
    else:
        initzerocnt += 1

for i in a:
    if tmpsum == 0 and i == 0:
        continue
    elif tmpsum == 0:
        tmpsum = i
    else:
        if np.sign(tmpsum) != np.sign(tmpsum+i) and tmpsum+i != 0:
            tmpsum += i
        else:
            ans += abs(i-tmpsum)
            tmpsum = -np.sign(tmpsum)
            

if initzerocnt != 0:
    ans += 2*(initzerocnt)-1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.append(0)
ct=0
if a[0]>0 :
	for i in range(n):
		if a[i]<=0 and i%2==0:
			ct=ct+0-a[i]+1
			a[i]=1
			a[i+1]=a[i+1]+1
		elif a[i]>0 and i%2==0:
			a[i+1]=a[i+1]+a[i]
		elif a[i]>=0 and i%2==1 :
			ct=ct+a[i]+1
			a[i]=-1
			a[i+1]=a[i+1]+a[i]
		elif a[i]<0 and i%2==1 :
			a[i+1]=a[i+1]+a[i]

if a[0]<0 :

	for i in range(n):
		if a[i]>=0 and i%2==0:
			ct=ct+a[i]+1
			a[i]=-1
			a[i+1]=a[i+1]+a[i]
		elif a[i]<0 and i%2==0:
			a[i+1]=a[i+1]+a[i]
		elif a[i]<=0 and i%2==1 :
			ct=ct+0-a[i]+1
			a[i]=1
			a[i+1]=a[i+1]+1
		elif a[i]>0 and i%2==1 :
			a[i+1]=a[i+1]+a[i]

if a[0]==0 :
	a[0]=1
	ct=ct+1
	for i in range(n):
		if a[i]<=0 and i%2==0:
			ct=ct+0-a[i]+1
			a[i]=1
			a[i+1]=a[i+1]+1
		elif a[i]>0 and i%2==0:
			a[i+1]=a[i+1]+a[i]
		elif a[i]>=0 and i%2==1 :
			ct=ct+a[i]+1
			a[i]=-1
			a[i+1]=a[i+1]+a[i]
		elif a[i]<0 and i%2==1 :
			a[i+1]=a[i+1]+a[i]
	ct2=ct
	ct=0
	a[0]=-1
	ct=ct+1
	for i in range(n):
		if a[i]<=0 and i%2==0:
			ct=ct+0-a[i]+1
			a[i]=1
			a[i+1]=a[i+1]+1
		elif a[i]>0 and i%2==0:
			a[i+1]=a[i+1]+a[i]
		elif a[i]>=0 and i%2==1 :
			ct=ct+a[i]+1
			a[i]=-1
			a[i+1]=a[i+1]+a[i]
		elif a[i]<0 and i%2==1 :
			a[i+1]=a[i+1]+a[i]

	if ct>ct2:
		ct=ct2
print(ct)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
if a[0] != 0:
    s = a[0]
    ans = 0
    for x in a[1:]:
        prev = s
        s += x
        if prev < 0:
            if s > 0:
                pass
            else:
                ans += 1-s
                s = 1
        else:
            if s < 0:
                pass
            else:
                ans += s+1
                s = -1
    print(ans)
else:
    # a[0] == 0
    s1 = 1
    s2 = -1
    ans1 = 1
    ans2 = 1
    for x in a[1:]:
        prev1 = s1
        prev2 = s2
        s1 += x
        s2 += x
        if prev1 < 0:
            if s1 > 0:
                pass
            else:
                ans1 += 1-s1
                s1 = 1
        else:
            if s1 < 0:
                pass
            else:
                ans1 += s1+1
                s1 = -1
        if prev2 < 0:
            if s2 > 0:
                pass
            else:
                ans2 += 1-s2
                s2 = 1
        else:
            if s2 < 0:
                pass
            else:
                ans2 += s2+1
                s2 = -1
    print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
if a[0] != 0:
    s = a[0]
    ans = 0
elif a[1] > 0:
    s = -1
    ans = 1
else:
    s = 1
    ans = 1
for x in a[1:]:
    prev = s
    s += x
    if prev < 0:
        if s > 0:
            pass
        else:
            ans += 1-s
            s = 1
    else:
        if s < 0:
            pass
        else:
            ans += s+1
            s = -1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
if a[0] != 0:
    s = a[0]
    ans = 0
elif a[1] > 0:
    s = -1
    ans = 1
else:
    s = 2
    ans = 1
for x in a[1:]:
    prev = s
    s += x
    if prev < 0:
        if s > 0:
            pass
        else:
            ans += 1-s
            s = 1
    else:
        if s < 0:
            pass
        else:
            ans += s+1
            s = -1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = a[0]
ans = 0
for x in a[1:]:
    prev = s
    s += x
    if prev < 0:
        if s > 0:
            pass
        else:
            ans += 1-s
            s = 1
    else:
        if s < 0:
            pass
        else:
            ans += s+1
            s = -1
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
count=a[0]
answer=0
if count==0:
  answer+=1
  if a[1]>0:
    count=-1
  else:
    count=1
    
for i in a[1:]:
  if (count>0 and count+i<0) or (count<0 and count+i>0):
    count+=i
    continue
  else:
    answer+=abs(count+i)+1
    if count>0:
      count=-1
    else:
      count=1
print(answer)

Submission status: Wrong Answer

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

ans = 0

su = A[0]

if su == 0 :
    ans += 1
    if A[1] > 0 :
        su = -1
    else :
        su = 1

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

Submission status: Wrong Answer

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

ans = 0

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

Submission status: Wrong Answer

Solution code:
N=int(input())

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

Pnum=0
Psum=0
if L[0]>=1:
    Pnum=L[0]
else:
    Psum=1-L[0]
for i in range(1,N):
    if i%2==1:
        if Pnum+L[i]<=-1:
            Pnum=Pnum+L[i]
        else:
            Psum+=(Pnum+L[i]+1)
            Pnum=-1
    else:
        if Pnum+L[i]>=1:
            Pnum=Pnum+L[i]
        else:
            Psum+=(1-Pnum-L[i])
            Pnum=1
            
Mnum=0
Msum=0
if L[0]<=-1:
    Mnum=L[0]
else:
    Msum=L[0]+1
for i in range(1,N):
    if i%2==0:
        if Mnum+L[i]<=-1:
            Mnum=Mnum+L[i]
        else:
            Msum+=(Mnum+L[i]+1)
            Mnum=-1
    else:
        if Mnum+L[i]>=1:
            Mnum=Mnum+L[i]
        else:
            Msum+=(1-Mnum-L[i])
            Mnum=1

print(min(Psum,Msum))

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int,input().split()))
S = L[0]
c = 0
flag = True
for i in range(N):
  if L[i] != 0:
    flag = False
    break
if flag:
  c = 2*N-1
else:
  if S > 0:
    sign = '+'
  elif S < 0:
    sign = '-'
  else:
    for i in range(1,N):
      if L[i] > 0:
        if i%2 == 0:
          c = 1
          sign = '+'
          S = 1
          break
        else:
          c = 1
          sign = '-'
          S = -1
          break
      elif L[i] < 0:
        if i%2 == 0:
          c = 1
          sign = '-'
          S = -1
          break
        else:
          c = 1
          sign = '+'
          S = 1
          break
  for i in range(1,N):
    if sign == '+':
      if S + L[i] >= 0:
        c += S+L[i]+1
        S = -1
        sign = '-'
      else:
        S = S+L[i]
        sign = '-'
    else:
      if S + L[i] <= 0:
        c += (-1)*S-L[i]+1
        S = +1
        sign = '+'
      else:
        S = S+L[i]
        sign = '+'
print(c)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int,input().split()))
S = L[0]
c = 0
if S > 0:
  sign = '+'
else:
  sign = '-'
for i in range(1,N):
  if sign == '+':
    if S + L[i] >= 0:
      c += S+L[i]+1
      S = -1
      sign = '-'
    else:
      S = S+L[i]
      sign = '-'
  else:
    if S + L[i] <= 0:
      c += (-1)*S-L[i]+1
      S = +1
      sign = '+'
    else:
      S = S+L[i]
      sign = '+'
print(c)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
p=[1,-1]
j=[-1,1]
c=0
l=0
d=0
e=0
for i in range(n):
    s=i%2
    c+=a[i]
    l+=a[i]
    print(c,l)
    if p[s]*c<=0:
        d+=abs(c-p[s])
        c=p[s]
    if j[s]*l<=0:
        e+=abs(l-j[s])
        l=j[s] 
print(min(e,d))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
p=[1,-1]
j=[-1,1]
c=a[0]
l=a[0]
d=0
e=0
for i in range(1,n):
    s=i%2
    c+=a[i]
    l+=a[i]
    if p[s]*c<=0:
        d+=abs(c-p[s])
        c=p[s]
    if j[s]*c<=0:
        e+=abs(c-j[s])
        l=j[s]      
print(min(e,d))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
p=[1,-1]
j=[-1,1]
c=a[0]
l=a[0]
d=0
e=0
for i in range(1,n):
    s=i%2
    c+=a[i]
    l+=a[i]
    if p[s]*c<=0:
        d+=abs(c-p[s])
        c=p[s]
    if j[s]*c>=0:
        e+=abs(c-j[s])
        l=j[s]      
print(min(e,d))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
if a[0]>-1:
    p=[1,-1]
else:
    p=[-1,1]
c=a[0]
d=0
for i in range(1,n):
    s=i%2
    c+=a[i]
    if p[s]*c<=0:
        d+=abs(c-p[s])
        c=p[s]
print(d)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
if a[0]>-1:
    p=[1,-1]
else:
    p=[-1,1]
c=a[0]
d=0
for i in range(1,n):
    s=i%2
    c+=a[i]
    if p[s]*c<0:
        d+=abs(c-p[s])
        c=p[s]
print(d)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Feb  9 14:32:30 2019

@author: shinjisu
"""


# arc 072 c sequence
def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def db(x):
    global debug
    if debug:
        print(x)


def count(N, A, initSum):
    # global N, A
    sm = initSum
    cnt = abs(sm-A[0])
    db(cnt)
    for i in range(N-1):
        nextSum = sm + A[i+1]
        db((sm, nextSum))
        if sm > 0 and nextSum >= 0:
            cnt += abs(nextSum)+1
            sm = -1
        elif sm < 0 and nextSum <= 0:
            cnt += abs(nextSum)+1
            sm = 1
        else:
            sm = nextSum
        db(cnt)
    return cnt


def probC():
    N = getInt()
    A = getIntList()
    db((N, A))
    if A[0] != 0:
        initSum = A[0]
    else:
        initSum = 1
    cnt1 = count(N, A, initSum)
    db(('a[0] or +1: ', cnt1))
    if initSum > 0:
        cnt2 = count(N, A, -1)
    else:
        cnt2 = count(N, A, 1)
    db(('反対符号 or -1: ', cnt2))
    return min(cnt1, cnt2)


debug = True
print(probC())

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Feb  9 14:32:30 2019

@author: shinjisu
"""


# arc 072 c sequence
def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def db(x):
    global debug
    if debug:
        print(x)


def count(initSum):
    sm = initSum
    cnt = abs(sm-a[0])
    db(cnt)
    for i in range(n-1):
        nextSum = sm + a[i+1]
        db((sm, nextSum))
        if sm > 0 and nextSum >= 0:
            cnt += abs(nextSum)+1
            sm = -1
        elif sm < 0 and nextSum <= 0:
            cnt += abs(nextSum)+1
            sm = 1
        else:
            sm = nextSum
        db(cnt)
    return cnt


debug = False
n = getInt()
a = getIntList()
db((n, a))
if a[0] == 0:
    cnt0 = 10**10
else:
    cnt0 = count(a[0])
db(cnt0)
cnt1 = count(1)
db(cnt1)
cnt2 = count(-1)
db(cnt2)
print(min(cnt0, cnt1, cnt2))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Feb  9 14:32:30 2019

@author: shinjisu
"""


# arc 072 c sequence
def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def db(x):
    global debug
    if debug:
        print(x)


debug = False
n = getInt()
a = getIntList()
db((n, a))
sm = a[0]
cnt = 0
if sm == 0:
    if a[1] >= 0:
        sm, cnt = -1, 1
    else:
        sm, cnt = 1, 1
for i in range(n-1):
    nextSum = sm + a[i+1]
    db((sm, nextSum))
    if sm > 0 and nextSum >= 0:
        cnt += abs(nextSum)+1
        sm = -1
    elif sm < 0 and nextSum <= 0:
        cnt += abs(nextSum)+1
        sm = 1
    else:
        sm = nextSum
    db(cnt)
print(cnt)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Feb  9 14:32:30 2019

@author: shinjisu
"""


# arc 072 c sequence
def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def db(x):
    global debug
    if debug:
        print(x)


debug = False
n = getInt()
a = getIntList()
db((n, a))
sm = a[0]
cnt = 0
for i in range(n-1):
    nextSum = sm + a[i+1]
    db((sm, nextSum))
    if sm > 0 and nextSum >= 0:
        cnt += abs(nextSum)+1
        sm = -1
    elif sm < 0 and nextSum <= 0:
        cnt += abs(nextSum)+1
        sm = 1
    else:
        sm = nextSum
    db(cnt)
print(cnt)

Submission status: Wrong Answer

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

ans = 0

if A[0] != 0 :
    sum_A = A[0]
else :
    if A[1] > 0 :
        sum_A = -A[1]
        ans += A[1]
    else :
        sum_A = A[1]
        ans += A[1]

for i in range(1,N) :
    if ((sum_A + A[i]) == 0) or (sum_A *(sum_A + A[i]) >= 0) :
        if sum_A > 0 :
            ans += 1 + sum_A + A[i]
            #print("ans += {}".format(1 + sum_A + A[i]))
            sum_A = -1
        elif sum_A < 0 :
            ans += 1 - (sum_A + A[i])
            #print("ans += {}".format(1 - (sum_A + A[i])))
            sum_A = 1
        
        
    else :
        sum_A += A[i]
        #print("else")

print(ans)

Submission status: Wrong Answer

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

ans = 0


sum_A = A[0]
for i in range(1,N) :
    if ((sum_A + A[i]) == 0) or (sum_A *(sum_A + A[i]) >= 0) :
        if sum_A > 0 :
            ans += 1 + sum_A + A[i]
            #print("ans += {}".format(1 + sum_A + A[i]))
            sum_A = -1
        elif sum_A < 0 :
            ans += 1 - (sum_A + A[i])
            #print("ans += {}".format(1 - (sum_A + A[i])))
            sum_A = 1
        
    else :
        sum_A += A[i]
        #print("else")

print(ans)