Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
reverseCount = 0
for _ in range(Q):
    query = input()
    if len(query) == 1:
        reverseCount += 1
    else:
        _, F, C = query.split(" ")
        if (int(F) + reverseCount) % 2 == 1:
            S = C + S
        else:
            S = S + C
if reverseCount % 2 == 0:
    print(S)
else:
    print(S[::-1])


Fixed submission:
S = input()
Q = int(input())
reverseCount = 0
pre = ""
pro = ""
for _ in range(Q):
    query = input()
    if len(query) == 1:
        reverseCount += 1
    else:
        _, F, C = query.split(" ")
        if (int(F) + reverseCount) % 2 == 1:
            pre = C + pre
        else:
            pro = pro + C
S = pre + S + pro
if reverseCount % 2 == 0:
    print(S)
else:
    print(S[::-1])

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
Q = int(input())
reverseCount = 0
for _ in range(Q):
    query = input()
    if len(query) == 1:
        reverseCount += 1
    else:
        _, F, C = query.split(" ")
        if (F == "1" and reverseCount % 2 == 0) or (F == "2" and reverseCount % 2 == 1):
            S = C + S
        else:
            S = S + C
if reverseCount % 2 == 0:
    print(S)
else:
    print(S[::-1])


Fixed submission:
S = input()
Q = int(input())
reverseCount = 0
pre = ""
pro = ""
for _ in range(Q):
    query = input()
    if len(query) == 1:
        reverseCount += 1
    else:
        _, F, C = query.split(" ")
        if (int(F) + reverseCount) % 2 == 1:
            pre = C + pre
        else:
            pro = pro + C
S = pre + S + pro
if reverseCount % 2 == 0:
    print(S)
else:
    print(S[::-1])
Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
p = list(map(int, input().split()))
result = [sum(p[i:i+K]) for i in range(N-K+1)]
print((max(result)+K)/2)


Fixed submission:
N, K = map(int, input().split())
p = list(map(int, input().split()))
a = [0] * (N+1)
for i in range(N):
    a[i+1] = a[i] + p[i]
result = [a[i+K] - a[i] for i in range(N-K+1)]
print((max(result)+K)/2)
Buggy python submission (verdict: Runtime Error):
N, K, M = map(int, input().split())
List = [int(i) for i in input().split()]

Goal = N * M

sub = Goal - sum(List)
if sub > K:
    print(-1)
else:
    print(sub)


Fixed submission:
a = input()

print(chr(ord(a)+1))
Buggy python submission (verdict: Runtime Error):
N = input()
print(N * N * N)

Fixed submission:
N1 = int(input())
print(N1 ** 3)

Buggy python submission (verdict: Runtime Error):

def fs(Q,n):
    l = len(Q) - 1
    res = 0
    for i in range(n + 2):
        res += Q[l - i]
        if res == 2:
            return l - i + 1
def main():
    N = int(input())
    B = list(map(int,input().split()))
    Q = [1] * N
    s = N - 1
    res = N - 1
    for i in range(N - 2):
        Q[i] = 0
        s = fs(Q,i)
        res += s
    s = N - 1
    for i in range(N - 3 ,0,-1):
        Q[i] = 0
        s = fs(Q,i)
        res += s
    print(res)

        
        

if __name__ == "__main__":
    
    main()
    

        

Fixed submission:
N1 = int(input())
print(N1 ** 3)
Buggy python submission (verdict: Runtime Error):
N,Q = map(int,input().split())
path = [[] for i in range(N)]
ans = [0] * N
def dfs(v,p=-1):
    for to in path[v]:
        if to == p :
            continue
        ans[to] += ans[v]
        dfs(to,v)

for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    path[a].append(b)
    path[b].append(a)
for i in range(Q):
    P,X = map(int,input().split())
    ans[P-1] += X
dfs(0)
print(*ans)

Fixed submission:
import sys
sys.setrecursionlimit(10**7) 

N,Q = map(int,input().split())
path = [[] for i in range(N)]
ans = [0] * N
def dfs(v,p=-1):
    for to in path[v]:
        if to == p :
            continue
        ans[to] += ans[v]
        dfs(to,v)

for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    path[a].append(b)
    path[b].append(a)
for i in range(Q):
    P,X = map(int,input().split())
    ans[P-1] += X
dfs(0)
print(*ans)

Buggy python submission (verdict: Runtime Error):
N,Q = map(int,input().split())
path = [[] for i in range(N)]
ans = [0] * N
def dfs(v,p=-1):
    for to in path[v]:
        if to == p :
            continue
        ans[to] += ans[v]
        dfs(to,v)

for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    path[a].append(b)
    path[b].append(a)
for i in range(Q):
    P,X = map(int,input().split())
    ans[P-1] += X
dfs(0)
print(*ans)

     

Fixed submission:
import sys
sys.setrecursionlimit(10**7) 

N,Q = map(int,input().split())
path = [[] for i in range(N)]
ans = [0] * N
def dfs(v,p=-1):
    for to in path[v]:
        if to == p :
            continue
        ans[to] += ans[v]
        dfs(to,v)

for i in range(N-1):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    path[a].append(b)
    path[b].append(a)
for i in range(Q):
    P,X = map(int,input().split())
    ans[P-1] += X
dfs(0)
print(*ans)
Buggy python submission (verdict: Runtime Error):
A, B = map(int,input().split)
ans = A + B
if ans % 2 == 0:
  print(ans //2)
else:
  print("IMPOSSIBLE")

Fixed submission:
a,b=map(int, input().split())
ans = a + b
if ans % 2 == 0:
  print(ans //2)
else:
  print("IMPOSSIBLE")

Buggy python submission (verdict: Runtime Error):
N = int(input())
p = list(map(int,input().split()))
count = 0
for i in range(N-1):
  if  p[i + 1] - p[i] != 1:
    count += 1
if count == 2 or count == 0:
  print("YES")
else:
  print("NO")

Fixed submission:
a,b=map(int, input().split())
ans = a + b
if ans % 2 == 0:
  print(ans //2)
else:
  print("IMPOSSIBLE")
Buggy python submission (verdict: Runtime Error):
N = int(input())
p = list(map(int,input().split()))
count = 0
for i in range(N-1):
	if  p[i + 1] - p[i] != 1:
      count += 1
if count <= 2:
  print("YES")
else:
  print("NO")

Fixed submission:
N = int(input())
p = list(map(int,input().split()))
s = list(range(1,N+ 1))
count = 0
for i in range(N):
  if s[i] != p[i]:
    count += 1
if count == 0 or count ==2:
  print("YES")
else:
  print("NO")
Buggy python submission (verdict: Runtime Error):
N,K = map(int,input().split())
h = list(map(int,input().split()))
dp = [0] * N
dp[0] = 0
for i in range(1,N):
    dp[i] = min([dp[i-j-1] + abs(h[i]-h[i-j-1]) for j in range(min(i,K))])

print(dp[-1])

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
dp = [0] * (N+10)
dp[2] = abs(A[1] - A[0])
for i in range(2,N):
    Ai = abs(A[i] - A[i-1])
    Ai2 = abs(A[i] - A[i-2])
    dp[i+1] = min(dp[i]+Ai,dp[i-1]+Ai2)

print(dp[N])

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
dp = [0] * (N+10)
dp[2] = abs(A[1] - A[0])
for i in range(2,N):
    Ai2 = abs(A[i] - A[i-2])
    dp[i+1] = min(dp[i]+,dp[i-1]+Ai2)

print(dp[N])

Fixed submission:
N = int(input())
A = list(map(int,input().split()))
dp = [0] * (N+10)
dp[2] = abs(A[1] - A[0])
for i in range(2,N):
    Ai = abs(A[i] - A[i-1])
    Ai2 = abs(A[i] - A[i-2])
    dp[i+1] = min(dp[i]+Ai,dp[i-1]+Ai2)

print(dp[N])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
s = input()

ans = 0
for i in range(n//2,-1,-1):
    for j in range(n - i - i + 1):
        if s[j:j+i] in s[j+i:]:
            ans = i
            break
    if ans != 0:
        break
print(ans)


Fixed submission:
N = int(input())
S = input()


r = 0
ans = 0
for l in range(N):

    while True:
        # print(S.rfind(S[l:r+1]), S[l:r+1], l, r)
        
        if S.rfind(S[l:r+1]) >= r+1:
            r += 1
            ans = max(ans, r-l)
        else:
            break

print(ans)
Buggy python submission (verdict: Runtime Error):
s = input()

n = len(s)
index = []

a = 'R'
for i in range(n):
    if a == 'R' and s[i] == 'L':
        index += [(i - 1)]
        a = 'L'
    elif a == 'L' and s[i] == 'R':
        index += [-(i - 1)]
        a = 'R'


ans = [0] * n
i = 0
j = 0
while i < n:
    if abs(i - index[j]) % 2 == 0:
        ans[index[j]] += 1
    else:
        ans[index[j]+1] += 1

    if -i in index:
        j += 2
    i += 1
ans = map(str, ans)
print(' '.join(ans))

Fixed submission:
s = input()
# s = 'RRRLLL'
n = len(s)
index = []

a = 'R'
for i in range(n):
    if a == 'R' and s[i] == 'L':
        index += [(i - 1)]
        a = 'L'
    elif a == 'L' and s[i] == 'R':
        index += [-(i - 1)]
        a = 'R'
m = len(index)

ans = [0] * n
j = 0
r = 0
while True:
    if j == m - 1:
        l = n
        if (index[j] - r) % 2 == 1:
            ans[index[j]] = (l - r) // 2
            ans[index[j]+1] = (l - r) - (l - r) // 2 
        else:
            ans[index[j]+1] = (l - r) // 2
            ans[index[j]] = (l - r) - (l - r) // 2
        break 
    l = -index[j+1] + 1
    if (index[j] - r) % 2 == 1:
        ans[index[j]] = (l - r) // 2
        ans[index[j]+1] = (l - r) - (l - r) // 2 
    else:
        ans[index[j]+1] = (l - r) // 2
        ans[index[j]] = (l - r) - (l - r) // 2
    r = l
    j += 2

ans = map(str, ans)
print(' '.join(ans))
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(input()) for i in range(n)]
# n = 6
# a = [5,6,7,2,6,4]

b = [a[0]]
minb = a[0]
count = 1
for i in range(1,n):
    if minb < a[i]:
        for j in range(len(b)):
            if b[j] < a[i]:
                b[j] = a[i]
                minb = min(b)
                break
    else:
        b += [a[i]]
        count += 1
        minb = a[i]

print(count)


Fixed submission:
import bisect

n = int(input())
a = [int(input()) for i in range(n)]

b = []
for i in range(n):
    k = bisect.bisect(b,-a[i])
    if k == len(b):
        b.append(-a[i])
    else:
        b[k] = -a[i]

print(len(b))
Buggy python submission (verdict: Runtime Error):
import fractions as frac
a,b,c,d = map(int,input().split())


lcm = (c * d) // frac.gcd(c,d)
if a % lcm != 0:
    s = lcm - a % lcm
else:
    s = 0
e = b % lcm
m = ((b - e) - (a + s)) // lcm
modgcd = (b - a + 1) - (s + (lcm - 1) * m + e)

if a % c != 0:
    s = c - a % c
else:
    s = 0
e = b % c
m = ((b - e) - (a + s)) // c
modc = (b - a + 1)- (s + (c - 1) * m + e)

if a % d != 0:
    s = d - a % d
else:
    s = 0
e = b % d
m = ((b - e) - (a + s)) // d
modd = (b - a + 1) - (s + (d - 1) * m + e)

print((b - a + 1)-(modc + modd - modgcd))


Fixed submission:
s=input()

for i in range(3):
    if s[i] in s[i+1]:
        print('Bad')
        break
else:
    print("Good")
Buggy python submission (verdict: Runtime Error):
def culc(m):
    s=0
    if m%2==1:
        m1=1 #1の個数
        m2=m//2 #2の個数
    else:
        m1=0
        m2=m//2
    
    while m2>=0:
        l1=1
        l2=1
        for i in range(m2):
            l1=l1*(m1+i+1)
            l2=l2*(i+1)
        s+=int(l1/l2)
        m1+=2
        m2+=-1
    return s

N,M=map(int,input().split())
a1=-1
b=[]
for i in range(M):

    a2=int(input())
    da=a2-a1
    a1=a2
    if da==1:
        b=[]
        break
    elif da!=0:
        b+=[da-2]

else:
    b+=[N-a2-1]


if b==[]:
    sm=0
else:
    b.sort()
    c=culc(b[0])
    sm=c
    s1=b[0]
    for i in b[1:]:
        if s1==i:
            sm=sm*c
        else:
            c=culc(i)
            sm=sm*c
print(sm%1000000007)


Fixed submission:
def culc(m):
    a1=0
    a2=1
    for i in range(m):
        a2_=a2
        a2=a1+a2
        a1=a2_
    return a2

N,M=map(int,input().split())
a=[-1]
b=[]
for i in range(M):
    a+=[int(input())]
    b+=[a[-1]-a[-2]-2]
    if a[-1]-a[-2]==1:
        b=[]
        break
else:
    b+=[N-a[-1]-1]
if b==[]:
    sm=0
else:
    sm=culc(b[0])
for i in b[1:]:
    sm=sm*culc(i)
print(sm%1000000007)
Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
l,r=map(int,input().split())
s={j for j in range(l,r+1)}
for i in range(1,m):
    l,r=map(int,input().split())
    s=s & {j for j in range(l,r+1)}
    if len(s)==0:
        break
print(len(s))

Fixed submission:
n,m=map(int,input().split())
l,r=map(int,input().split())
s=[l,r]
for i in range(1,m):
    l,r=map(int,input().split())
    if s[0]<l:
        s[0]=l
    if s[1]>r:
        s[1]=r

    if s[1]-s[0]<0:
        s=[0,-1]
        break
print(s[1]-s[0]+1)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
l,r=map(int,input().split())
s={j for j in range(l,r+1)}
for i in range(1,m):
    l,r=map(int,input().split())
    s=s & {j for j in range(l,r+1)}
print(len(s))

Fixed submission:
n,m=map(int,input().split())
l,r=map(int,input().split())
s=[l,r]
for i in range(1,m):
    l,r=map(int,input().split())
    if s[0]<l:
        s[0]=l
    if s[1]>r:
        s[1]=r

    if s[1]-s[0]<0:
        s=[0,-1]
        break
print(s[1]-s[0]+1)
Buggy python submission (verdict: Runtime Error):
n,m=map(int,input().split())
a=[i for i in map(int,input().split())]
a.sort()
b=[]
for _ in range(m):
    b1=[i for i in map(int,input().split())]
    b1.reverse()
    b+=[b1]
b.sort(reverse=True)

i=0
f=0
s=0
while f==0:
    if a[s+b[i][1]-1]<b[i][0]:
        a[s:s+b[i][1]]=[b[i][0]]*(b[i][1])
        s+=b[i][1]
    elif a[s]<b[i][0]:
        j=0
        while a[s+j]<b[i][0]:
            a[s+j]=b[i][0]
            j+=1
        f+=1
    else:
        f+=1
    i+=1
    if i>len(b)-1:
        f+=1

print(sum(a))

Fixed submission:
n,m=map(int,input().split())
a=[i for i in map(int,input().split())]
a.sort()
b=[]
for _ in range(m):
    b1=[i for i in map(int,input().split())]
    b1.reverse()
    b+=[b1]
b.sort(reverse=True)

i=0
f=0
s=0
lenb=len(b)
lena=len(a)
while f==0:
    if i>lenb-1 or s>lena-1:
        break
    elif s+b[i][1]>lena-1:
        b[i][1]=lena-s
    if a[s+b[i][1]-1]<b[i][0]:
        a[s:s+b[i][1]]=[b[i][0]]*(b[i][1])
        s+=b[i][1]
    elif a[s]<b[i][0]:
        j=0
        while a[s+j]<b[i][0]:
            a[s+j]=b[i][0]
            j+=1
            if s+j>lena-1:
                break
        break
    else:
        break
    i+=1

print(sum(a))
Buggy python submission (verdict: Runtime Error):
from math import gcd
n=int(input())
a=list(map(int,input().split()))

if n==2:
    b=max(a)
else:
    s1=gcd(a[0],a[1])
    s2=gcd(a[0],a[2])
    s3=gcd(a[2],a[1])
    c=gcd(s1,s2)
    b=max(s1,s2,s3)
    if b==s1:
        t=a[2]
    elif b==s2:
        t=a[1]
    else:
        t=a[0]
    if len(a)>=4:
        for i in a[3:]:
            d=gcd(b,i)
            if d>c:
                b=d
                c=gcd(c,i)
            else:
                t=i
                b=c
                c=d
print(b)



Fixed submission:
from fractions import gcd
n=int(input())
a=list(map(int,input().split()))

s1=a[0]
s2=a[-1]
su=[s1]
sd=[s2]
for i in range(1,n-1):
    s1=gcd(s1,a[i])
    s2=gcd(s2,a[(i+1)*(-1)])
    su+=[s1]
    sd+=[s2]
sd.reverse()

ans=[su[-1],sd[0]]
for i in range(n-2):
    ans+=[gcd(su[i],sd[i+1])]
print(max(ans))

Buggy python submission (verdict: Runtime Error):
import math

N=int(input())
A=list(map(int,input().split()))
a1=[]
a2=[]
for i in range(N-1):
    a1+=[math.gcd(A[N-1], A[i])]
    a2+=[math.gcd(A[0], A[i+1])]
a1.sort()
a2.sort()

if N>2:
    print(max(a1[1],a2[1]))
elif N==2:
    print(max(A))


Fixed submission:
from fractions import gcd
n=int(input())
a=list(map(int,input().split()))

s1=a[0]
s2=a[-1]
su=[s1]
sd=[s2]
for i in range(1,n-1):
    s1=gcd(s1,a[i])
    s2=gcd(s2,a[(i+1)*(-1)])
    su+=[s1]
    sd+=[s2]
sd.reverse()

ans=[su[-1],sd[0]]
for i in range(n-2):
    ans+=[gcd(su[i],sd[i+1])]
print(max(ans))
Buggy python submission (verdict: Runtime Error):
n,q=map(int,input().split())
s=input()
index=[]
num=0
for i in range(len(s)-1):
    if s[i:i+2]=='AC':
        index+=[[i+1,num+1]]
        num+=1
ans=[]
for i in range(q):
    l,r=map(int,input().split())
    j=0
    k=1
    while index[j][0]<l or index[k*(-1)][0]+1>r:
        if index[j][0]<l:
            j+=1
        if index[k*(-1)][0]+1>r:
            k+=1
    ans+=[index[k*(-1)][1]-index[j][1]+1]

for i in ans:
    print(i)



Fixed submission:
n,q=map(int,input().split())
s=input()
index1=[0]*(n+2)

num=0
for i in range(len(s)):
    if s[i:i+2]=='AC':
        num+=1
    index1[i+1]=num


ans=[]

for i in range(q):
    l,r=map(int,input().split())
    if index1[l-1]==index1[l] and index1[r-1]==index1[r]:
        ans+=[index1[r]-index1[l]]
    elif index1[l-1]!=index1[l] and index1[r-1]==index1[r]:
        ans+=[index1[r]-index1[l]+1]
    elif index1[l-1]==index1[l] and index1[r-1]!=index1[r]:
        ans+=[index1[r]-index1[l]-1]
    elif index1[l-1]!=index1[l] and index1[r-1]!=index1[r]:
        ans+=[index1[r]-index1[l]]
for i in ans:
    print(i)
Buggy python submission (verdict: Runtime Error):
import numpy as np
N,M,C=map(int,input().split())
B=[i for i in map(int,input().split())]
A=[]
for i in range(N):
    A1=[i for i in map(int,input().split())]
    A+=[A1]

B=np.array(B)
A=np.array(A)
count=0
for i in range(N):
    if B@A[i].T+C>0:
        count+=1
print(count)

Fixed submission:
N,M,C=map(int,input().split())
B=[i for i in map(int,input().split())]
A=[]
for i in range(N):
    A1=[i for i in map(int,input().split())]
    A+=[A1]


count=0
for i in range(N):
    dot=0
    for j in range(M):
        dot+=B[j]*A[i][j]
    dot+=C
    if dot>0:
        count+=1
print(count)
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N,M=map(int,input().split())
A=[]

for i in range(N):
    A+=[[i for i in map(int,input().split())]]
A=np.array(A)
A=A[A[:,0].argsort(),:]


count=0
price=0
for i in range(N):
    if A[0,1]>=M-count:
        price+=A[0,0]*(M-count)
        break
    else:
        price+=A[0,0]*A[0,1]
        count+=A[0,1]
    A=np.delete(A,0,0)
    
print(price)

Fixed submission:
import numpy as np
N,M=map(int,input().split())
A=[]
 
for i in range(N):
    A+=[[i for i in map(int,input().split())]]
A=np.array(A)
A=A[A[:,0].argsort(),:]

count=0
price=0
for i in range(N):
    if A[i,1]>=M-count:
        price+=A[i,0]*(M-count)
        break
    else:
        price+=A[i,0]*A[i,1]
        count+=A[i,1]

print(price)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
A=[]
for i in range(N):
    A1=[i for i in map(int,input().split())]
    if i==0:
        A+=[A1]
    else:
        for j in range(len(A)):
            if A[j][0]>A1[0]:
                A.insert(j,A1)
                break
            elif j==i-1:
                A+=[A1]

count=0
price=0
for i in range(N):
    if A[0][1]>=M-count:
        price+=A[0][0]*(M-count)
        break
    else:
        price+=A[0][0]*A[0][1]
        count+=A[0][1]
    A.pop(0)
    
print(price)

Fixed submission:
import numpy as np
N,M=map(int,input().split())
A=[]
 
for i in range(N):
    A+=[[i for i in map(int,input().split())]]
A=np.array(A)
A=A[A[:,0].argsort(),:]

count=0
price=0
for i in range(N):
    if A[i,1]>=M-count:
        price+=A[i,0]*(M-count)
        break
    else:
        price+=A[i,0]*A[i,1]
        count+=A[i,1]

print(price)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
A=[]
for i in range(N):
    A1=[i for i in map(int,input().split())]
    if i==0:
        A+=[A1]
    else:
        for j in range(len(A)):
            if A[j][0]>A1[0]:
                A.insert(j,A1)
                break
            elif j==i-1:
                A+=[A1]

count=0
price=0
while count<M:
    if A[0][1]>=M-count:
        price+=A[0][0]*(M-count)
        break
    else:
        price+=A[0][0]*A[0][1]
        count+=A[0][1]
    A.pop(0)
    
print(price)

Fixed submission:
import numpy as np
N,M=map(int,input().split())
A=[]
 
for i in range(N):
    A+=[[i for i in map(int,input().split())]]
A=np.array(A)
A=A[A[:,0].argsort(),:]

count=0
price=0
for i in range(N):
    if A[i,1]>=M-count:
        price+=A[i,0]*(M-count)
        break
    else:
        price+=A[i,0]*A[i,1]
        count+=A[i,1]

print(price)
Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))

sm=a[0]
ans=[0, 0]
for i in range(n-1):
    sm1=sm+a[i+1]
    if sm*sm1>=0:
        ans[0]+=abs(sm1)+1
        sm//=abs(sm)*(-1)
    else:
        sm=sm1


sm=-1
ans[1]+=abs(a[0])+1
for i in range(n-1):
    sm1=sm+a[i+1]
    if sm*sm1>=0:
        ans[1]+=abs(sm1)+1
        sm//=abs(sm)*(-1)
    else:
        sm=sm1
print(min(ans))

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

ans=[0, 0]
if a[0]!=0:
    sm=a[0]
    for i in range(n-1):
        sm1=sm+a[i+1]
        if sm*sm1>=0:
            ans[0]+=abs(sm1)+1
            if sm>0:
                sm=-1
            else:
                sm=1
        else:
            sm=sm1

    if a[0]>0:
        sm=-1
    else:
        sm=1
    ans[1]+=abs(a[0])+1
    for i in range(n-1):
        sm1=sm+a[i+1]
        if sm*sm1>=0:
            ans[1]+=abs(sm1)+1
            if sm>0:
                sm=-1
            else:
                sm=1
        else:
            sm=sm1
if a[0]==0:
    sm=1
    ans[0]+=1
    for i in range(n-1):
        sm1=sm+a[i+1]
        if sm*sm1>=0:
            ans[0]+=abs(sm1)+1
            if sm>0:
                sm=-1
            else:
                sm=1
        else:
            sm=sm1

    sm=-1
    ans[1]+=1
    for i in range(n-1):
        sm1=sm+a[i+1]
        if sm*sm1>=0:
            ans[1]+=abs(sm1)+1
            if sm>0:
                sm=-1
            else:
                sm=1
        else:
            sm=sm1
print(min(ans))

Buggy python submission (verdict: Runtime Error):
n=int(input())

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

sm=a[0]

ans=0
for i in range(n-1):
    sm1=sm+a[i+1]
    if sm*sm1>=0:
        ans+=abs(sm1)+1
        sm//=abs(sm)*(-1)
    else:
        sm=sm1
print(ans)

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

ans=[0, 0]
if a[0]!=0:
    sm=a[0]
    for i in range(n-1):
        sm1=sm+a[i+1]
        if sm*sm1>=0:
            ans[0]+=abs(sm1)+1
            if sm>0:
                sm=-1
            else:
                sm=1
        else:
            sm=sm1

    if a[0]>0:
        sm=-1
    else:
        sm=1
    ans[1]+=abs(a[0])+1
    for i in range(n-1):
        sm1=sm+a[i+1]
        if sm*sm1>=0:
            ans[1]+=abs(sm1)+1
            if sm>0:
                sm=-1
            else:
                sm=1
        else:
            sm=sm1
if a[0]==0:
    sm=1
    ans[0]+=1
    for i in range(n-1):
        sm1=sm+a[i+1]
        if sm*sm1>=0:
            ans[0]+=abs(sm1)+1
            if sm>0:
                sm=-1
            else:
                sm=1
        else:
            sm=sm1

    sm=-1
    ans[1]+=1
    for i in range(n-1):
        sm1=sm+a[i+1]
        if sm*sm1>=0:
            ans[1]+=abs(sm1)+1
            if sm>0:
                sm=-1
            else:
                sm=1
        else:
            sm=sm1
print(min(ans))