Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02710 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
def main():
    n=int(input())
    color=list(map(int,input().split()))
    nodes=[[]for i in range(n)]
    cut=[[0]for i in range(n+1)]
    ans=[0]*(n+1)
    for i in range(n-1):
        a,b=map(int,input().split())
        nodes[a-1].append(b-1)
        nodes[b-1].append(a-1)
    def num(p,parent):
        s=0
        for child in nodes[p]:
            if child==parent:
                continue
            cut[color[p]].append(0)
            nc=num(child,p)
            group=nc-cut[color[p]].pop()
            ans[color[p]]+=group*(group+1)//2
            s+=nc
        s+=1
        cut[color[p]][-1]+=s
        return s
    num(0,-1)
    for a,c in zip(ans[1:],cut[1:]):
        group=n-c[0]
        c=group*(group+1)//2
        print(n*(n+1)//2-c-a)
main()

Buggy submission (verdict: Runtime Error):
def main():
    n=int(input())
    color=list(map(int,input().split()))
    nodes=[[]for i in range(n)]
    cut=[[0]for i in range(n+1)]
    ans=[0]*(n+1)
    for i in range(n-1):
        a,b=map(int,input().split())
        nodes[a-1].append(b-1)
        nodes[b-1].append(a-1)
    def num(p,parent):
        s=0
        for child in nodes[p]:
            if child==parent:
                continue
            cut[color[p]].append(0)
            nc=num(child,p)
            group=nc-cut[color[p]].pop()
            ans[color[p]]+=group*(group+1)//2
            s+=nc
        s+=1
        cut[color[p]][-1]+=s
        return s
    num(0,-1)
    for a,c in zip(ans[1:],cut[1:]):
        group=n-c[0]
        c=group*(group+1)//2
        print(n*(n+1)//2-c-a)
main()
Problem p02721 (python)

Fixed submission:
n,k,rest=map(int,input().split())
s=input()

last=-3000000
l1=[]
l2=set()
for i,c in enumerate(s):
    if last+rest<i and c=="o":
        last=i
        l1.append(i)
last=6000000
for i,c in enumerate(reversed(s)):
    if n-1-i<last-rest and c=="o":
        last=n-1-i
        l2.add(n-1-i)

if len(l1)!=k:
    print()
else:
    for i in l1:
        if i in l2:
            print(i+1)

Buggy submission (verdict: Runtime Error):
n,k,rest= map(int, input().split())
s=input()
s+="x"*rest
l=[]
i=-1
ban=0
suc=0
new=[]
if rest==0:
    if s.count("o")==k:
        for i in range(len(s)):
            if s[i]=="o":
                print(i+1)
else:
    while i<len(s)+rest-1:
        i+=1
        ban-=1
        c=s[i]
        if c=="o":
            suc=0
            if ban>=0:
                continue
            new.append(i+1)
            ban=rest
        else:
            suc+=1
            if suc>=rest:
                if ban==0:
                    l+=new
                new=[]

    if len(l)>k:
        print()
    else:
        for i in l:
            print(i)

Buggy submission (verdict: Runtime Error):
n,k,c= map(int, input().split())
s=input()
l=[]

for c in s.split("x"*c):
    count=0
    while i<len()
    if i=="o":
Problem p02758 (python)

Fixed submission:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

n=int(readline())
segSize=n+1
def init_max(init_max_val):
    for i in range(segSize):
        seg_max[i+num_max-1]=init_max_val[i]
    for i in range(num_max-2,-1,-1) :
        seg_max[i]=max(seg_max[2*i+1],seg_max[2*i+2]) 
    
def update_max(k,x):
    k += num_max-1
    seg_max[k] = x
    while k:
        k = (k-1)//2
        seg_max[k] = max(seg_max[k*2+1],seg_max[k*2+2])
    
def query_max(p,q):
    if q<=p:
        return ide_ele_max
    p += num_max-1
    q += num_max-2
    res=ide_ele_max
    while q-p>1:
        if p&1 == 0:
            res = max(res,seg_max[p])
        if q&1 == 1:
            res = max(res,seg_max[q])
            q -= 1
        p = p//2
        q = (q-1)//2
    if p == q:
        res = max(res,seg_max[p])
    else:
        res = max(max(res,seg_max[p]),seg_max[q])
    return res
ide_ele_max = 0
num_max =2**(segSize-1).bit_length()
seg_max=[ide_ele_max]*2*num_max
mod=998244353

m = map(int,read().split())
XD = sorted(zip(m,m))
X=[x for x,d in XD]

from bisect import bisect_right
X=[bisect_right(X,x+d-1)for x,d in XD]

init_max(list(range(segSize)))
update_max(n,n)
combs=[1]*(n+1)
t=0
for x,d in reversed(XD):
    idx=n-1-t
    t+=1
    p=X[idx]
    p=query_max(idx,p)
    update_max(idx,p)
    combs[idx]=(combs[p+1]+combs[idx+1])%mod
print(combs[0])

Buggy submission (verdict: Runtime Error):
n=int(input())
segSize=n+1
def init_max(init_max_val):
    for i in range(segSize):
        seg_max[i+num_max-1]=init_max_val[i]
    for i in range(num_max-2,-1,-1) :
        seg_max[i]=max(seg_max[2*i+1],seg_max[2*i+2]) 
    
def update_max(k,x):
    k += num_max-1
    seg_max[k] = x
    while k:
        k = (k-1)//2
        seg_max[k] = max(seg_max[k*2+1],seg_max[k*2+2])
    
def query_max(p,q):
    if q<=p:
        return ide_ele_max
    p += num_max-1
    q += num_max-2
    res=ide_ele_max
    while q-p>1:
        if p&1 == 0:
            res = max(res,seg_max[p])
        if q&1 == 1:
            res = max(res,seg_max[q])
            q -= 1
        p = p//2
        q = (q-1)//2
    if p == q:
        res = max(res,seg_max[p])
    else:
        res = max(max(res,seg_max[p]),seg_max[q])
    return res
ide_ele_max = 0
num_max =2**(segSize-1).bit_length()
seg_max=[ide_ele_max]*2*num_max
mod=998244353

import sys
read = sys.stdin.buffer.read
m = map(int,read().split())
XD = sorted(zip(m,m))
X=[x for x,d in XD]

from bisect import bisect_right
X=[bisect_right(X,x+d-1)for x,d in XD]

init_max(list(range(segSize)))
update_max(n,n)
combs=[1]*(n+1)
t=0
for x,d in reversed(XD):
    idx=n-1-t
    t+=1
    p=X[idx]
    p=query_max(idx,p)
    update_max(idx,p)
    combs[idx]=(combs[p+1]+combs[idx+1])%mod
print(combs[0])
Problem p02764 (python)

Fixed submission:
import numpy as np
from scipy.optimize import fmin

n,k=map(int,input().split())
XYC=[list(map(int,input().split())) for i in range(n)]
X,Y,C=[],[],[]
for x,y,c in XYC:
    X.append(x)
    Y.append(y)
    C.append(c)
X=np.array(X,dtype=np.float)
Y=np.array(Y,dtype=np.float)
C=np.array(C,dtype=np.float)
def f(xy):
    x,y=xy
    times=(X-x)*(X-x)+(Y-y)*(Y-y)
    times=C*np.sqrt(times)
    times=np.sort(times)
    return times[k-1]
deff=0.5
ansx=[fmin(f,(x,y),disp=False,ftol=10**(-7),initial_simplex=[(x+deff,y),(x-deff,y+deff),(x-deff,y-deff)]) for x,y,c in XYC]
ansy=list(map(lambda x:f(x),ansx))
print(min(ansy))

Buggy submission (verdict: Runtime Error):
import numpy as np
from scipy.optimize import fmin
import sys
n,k=map(int,input().split())
Z=list(map(int,sys.stdin.buffer.read().split()))
X=np.array(Z[::3],dtype=np.float)
Y=np.array(Z[1::3],dtype=np.float)
C=np.array(Z[2::3],dtype=np.float)
Z=[Z[i*3:i*3+3]for i in range(n)]
def f(x):
  x,y=x
  t=np.sort(C*np.sqrt((X-x)*(X-x)+(Y-y)*(Y-y)))
  return t[k-1]
A=[fmin(f,(x,y),disp=False,ftol=10**(-7),initial_simplex=[(x+0.5,y),(x-0.5,y+0.5),(x-0.5,y-0.5)])for x,y,c in Z]
B=list(map(lambda x:f(x),A))
print(min(B))

Buggy submission (verdict: Runtime Error):
import numpy as np
from scipy.optimize import fmin

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

X=np.array(XYC[::3],dtype=np.float)
Y=np.array(XYC[1::3],dtype=np.float)
C=np.array(XYC[2::3],dtype=np.float)
def f(xy):
    x,y=xy
    times=(X-x)*(X-x)+(Y-y)*(Y-y)
    times=C*np.sqrt(times)
    times=np.sort(times)
    return times[k-1]
ansx=[fmin(f,(x,y),disp=False,ftol=10**(-7),initial_simplex=[(x+100,y),(x-100,y+100),(x-100,y-100)]) for x,y,c in XYC]
ansy=list(map(lambda x:f(x),ansx))
print(min(ansy))

Buggy submission (verdict: Runtime Error):
import numpy as np
from scipy.optimize import fmin

n,k=map(int,input().split())
XYC=[list(map(int,input().split())) for i in range(n)]
X,Y,C=[],[],[]
for x,y,c in XYC:
    X.append(x)
    Y.append(y)
    C.append(c)
X=np.array(X,dtype=np.float)
Y=np.array(Y,dtype=np.float)
C=np.array(C,dtype=np.float)
def f(xy):
    x,y=xy
    times=(X-x)*(X-x)+(Y-y)*(Y-y)
    times=C*np.sqrt(times)
    times=np.sort(times)
    return times[k-1]
ansx=[fmin(f,(x,y),disp=False,ftol=10**(-6),initial_simplex=[(x+100,y),(x-100,y+100),(x-100,y-100)]) for x,y,c in XYC]
ansy=list(map(lambda x:f(x),ansx))
print(min(ansy))
Problem p02770 (python)

Fixed submission:
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

k,q=map(int,readline().split())
D=np.array(readline().split(),dtype=np.int64)
NXM = map(int, read().split())

NXM=iter(NXM)
NXM=zip(NXM,NXM,NXM)

for n,x,m in NXM:
    tmpD=D%m
    SD=np.hstack((np.zeros(1,dtype=np.int64), tmpD))#結合
    SD=np.cumsum(SD)
    n-=1
    x=x%m
    x+=SD[-1]*(n//k)
    x+=SD[n%k]
    count=(k-np.count_nonzero(tmpD))*(n//k)
    count+=n%k-np.count_nonzero(tmpD[:n%k])
    ans=n-x//m-count
    print(ans)

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

k,q=map(int,readline().split())
D=list(map(int,readline().split()))
NXM = map(int, read().split())

NXM=iter(NXM)
NXM=zip(NXM,NXM,NXM)

for n,x,m in NXM:
    tmpD=D%m
    SD=np.array([0]+tmpD,dtype=np.int64)
    SD=np.cumsum(SD)
    n-=1
    x=x%m
    x+=SD[-1]*(n//k)
    x+=SD[n%k]
    count=tmpD.count(0)*(n//k)
    count+=tmpD[:n%k].count(0)
    ans=n-x//m-count
    print(ans)

Buggy submission (verdict: Runtime Error):
main()
    k,q = map(int, input().split())
    D=list(map(int, input().split()))

    for _ in range(q):
        n,x,m=map(int, input().split())
        mods=[d%m for d in D]

        #Sで総和を作成,何回通過したかを計算
        a=sum(mods)
        S=sum(mods)*((n-1)//k)+x%m
        S+=sum(mods[:(n-1)%k])
        through=S//m

        #カウント回数が答え
        ans=n-through-1
        #カウントしたうち0を省く
        ans-=mods.count(0)*((n-1)//k)+mods[:(n-1)%k].count(0)
        print(ans)
main()
Problem p02774 (python)

Fixed submission:
import numpy as np

n,k=map(int,input().split())
A=np.array(input().split(),np.int64)
A.sort()

zero=A[A==0]
neg=A[A<0]
pos=A[A>0]

def f(x): # count pairs x or less
    cnt=0
    #zero
    if x>=0:
        cnt += len(zero)*n
    #pos
    cnt += A.searchsorted(x//pos,side="right").sum()
    #neg
    cnt += (n-A.searchsorted(-(-x//neg),side="left")).sum()
    #except a*a
    cnt-=np.count_nonzero(A*A<=x)
    return cnt//2

l=-10**18
r=10**18

while l<r:
    pov=(l+r)//2
    if f(pov)>=k:
        r=pov
    else:
        l=pov+1
print(l)

Buggy submission (verdict: Runtime Error):
import bisect

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

left=-10**18
right=10**18
count=0

a=bisect.bisect_left(A,0)
b=bisect.bisect_right(A,0)

if n*(n-1)-b*(n-a)-(b-a)>=2*k:
    right=0
else:
    left=1

while right!=left:
    pos=(left+right)//2
    count=0
    for a in A:
        if a==0 and pos>=0:
            count+=n-1
        elif a<0:
            i=bisect.bisect_right(A,(-1-pos)//(-a))
            count+=n-i + (-1 if a*a<=pos else 0)
        else:
            i=bisect.bisect_right(A,pos//a)
            count+=i + (-1 if a*a<=pos else 0)
    if count>=2*k:
        right=pos
    else:
        left=pos+1

print(left)
Problem p02796 (python)

Fixed submission:
n = int(input())
lr = []
for i in range(n):
    x,l=input().split()
    x=int(x)
    l=int(l)
    lr.append([x-l,x+l])

lr.sort(key=lambda x:x[1])

l = -2000000000
ans=0
for i in range(n):
    if l<=lr[i][0]:
        l=lr[i][1]
        ans+=1

print(ans)

Buggy submission (verdict: Runtime Error):
n,k,s = [int(i) for i in input().split()]

for i in range(k):
    print(s,end="")
    if i!=k-1:
        print(" ",end="")

s = s-1 if s==10**0 else s+1

for i in range(k,n):
    print(" "+str(s),end="")
Problem p02814 (python)

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

from fractions import gcd
def lcm(a,b):
    return a*b//gcd(a,b)
l=1

max2=0
min2=1000

for i in a:
    factor=(i^(i-1)).bit_length()
    max2=max(max2,factor)
    min2=min(min2,factor)
    l=l*i//2//gcd(l,i//2)
    if l>m:
        break

if l>m or min2!=max2:
    print(0)
else:
    ans=1 if l<=m else 0
    m-=l
    print(ans+max(0,m//(2*l)))

Buggy submission (verdict: Runtime Error):
n,m=map(int,input().split())
a=map(int,input().split())

from math import gcd
def lcm(a,b):
    return a*b//gcd(a,b)

l=1
for i in a:
    l=l*i//2//gcd(l,i//2)
    if l>m:
        break

ans=1 if l<=m else 0
m-=l

print(ans+max(0,m//(2*l)))

Buggy submission (verdict: Runtime Error):
n,m=map(int,input().split())
a=map(int,input().split())

from math import gcd
def lcm(a,b):
    return a*b//gcd(a,b)

l=1
for i in a:
    l=l*i//2//gcd(l,i//2)
    if l>m:
        break

ans=1 if l<=m else 0
m-=l

print(ans+max(0,m//(2*l)))
Problem p02817 (python)

Fixed submission:
s,t = input().split()
print(t+s)

Buggy submission (verdict: Runtime Error):
import numpy as np
 
n,m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
d = 2**18
 
f = np.array([0]*d)
for i in a:
    f[i]+=1
 
tf = np.fft.fft(f)
f = np.fft.ifft(tf*tf)
f = [int(i+0.5) for i in f]
 
ans=0
for i in range(len(f)-1,0,-1):
    if f[i]<=m:
        ans+=i*f[i]
        m-=f[i]
    elif f[i]>m:
        ans+=i*m
        break
print(ans)
Problem p02821 (python)

Fixed submission:
import numpy as np

n,m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
d = 2**18

f = np.array([0]*d)
for i in a:
    f[i]+=1

tf = np.fft.fft(f)
f = np.fft.ifft(tf*tf)
f = [int(i+0.5) for i in f]

ans=0
for i in range(len(f)-1,0,-1):
    if f[i]<=m:
        ans+=i*f[i]
        m-=f[i]
    elif f[i]>m:
        ans+=i*m
        break
print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np
 
n,m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
d = 2**18
 
f = np.array([0]*d)
for i in a:
    f[i]+=1
 
tf = np.fft.fft(f)
f = np.fft.ifft(tf*tf)
f = [int(i+0.5) for i in f]
 
ans=0
for i in range(len(f)-1,0,-1):
    if f[i]<=m:
        ans+=i*f[i]
        m-=f[i]
    elif f[i]>m:
        ans+=i*m
        break
print(ans)

Buggy submission (verdict: Runtime Error):
import numpy as np

n,m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
d = 2**15

f = np.array([0]*d)
for i in a:
    f[i]+=1

tf = np.fft.fft(f)
f = np.fft.ifft(tf*tf)
f = [int(i+0.5) for i in f]

ans=0
for i in range(len(f)-1,0,-1):
    if f[i]<=m:
        ans+=i*f[i]
        m-=f[i]
    elif f[i]>m:
        ans+=i*m
        break
print(ans)
Problem p02823 (python)

Fixed submission:
n,a,b = [int(i) for i in input().split()]
if (b-a)%2==0:
    print((b-a)//2)
else:
    r = a + (b-a-1)//2
    l = n-b+1 + ( n - (a+n-b+1) )//2 
    print(min(r,l))

Buggy submission (verdict: Runtime Error):
n,m,v,p = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
p-=1

a.sort(reverse=True)

ans=0
s = sum(a)

def safe(index):
    if a[p]>a[index]+m:
        return False
    i=0
    wall = -1
    while a[i]>=a[index]+m:
        if a[i]>=a[index]+m+1:
            wall = i
        i+=1
    if i>index:
        return True
    intg = 0
    while i<index:
        intg += a[index] + m - a[i]
        i+=1
    intg += (len(a)-1-index)*m + min(v,p - wall + 1)*m
    #print("intg = ",intg ,m*(v-1) )
    return intg >= m*(v-1)

left = 0
right = len(a)-1
ans = int((right - left)/2)
if safe(right):
    ans = right
else:
    j = safe(ans)
    while not(j and not safe(ans+1)):
        if j:
            left = ans
        else:
            right = ans
        ans = left + int((right - left)/2)
        j = safe(ans)
print(ans+1)
"""
for i in range(len(a)):
    print(a[i],safe(i))
"""
Problem p02838 (python)

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
counts=[0]*60
for b in a:
    for i in range(60):
        counts[i]+=b%2
        b//=2
ans=0
mod = 10**9+7
for i in range(60):
    c=counts[i]
    comb=c*(n-c)
    ans=(ans+(1<<i)*comb)%mod
print(ans)

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

for b in a:
    for i in range(60):
        counts[i]+=b%2
        b//=2

ans=0
mod = 10**9+7
for i in range(60):
    c=counts[i]
    comb=c*(n-c)
    ans=(ans+(1<<i)*comb)%mod
print(ans)
Problem p02840 (python)

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

dic={}

ans=0

if d==0:
    if a==0:
        print(1)
    else:
        print(n+1)
else:
    for k in range(n+1):
        l=k*a+k*(k-1)*d//2
        r=l+k*(n-k)*d
        idx=l%d
        l//=d
        r//=d
        if idx in dic:
            prel,prer=dic[idx]
            #共通部分ありか、なしかで分岐
            if prer<l or prel>r:
                dic[idx]=(l,r)
                ans+=r-l+1 
            else:
                nl=min(prel,l)
                nr=max(prer,r)
                ans+=prel-nl+nr-prer
                dic[idx]=(nl,nr)
        else:
            ans+=r-l+1 
            dic[idx]=(l,r)
    print(ans)

Buggy submission (verdict: Runtime Error):
n,a,d=map(int,input().split())

dic={}

ans=0
for k in range(n+1):
    l=k*a+k*(k-1)*d//2
    r=l+k*(n-k)*d
    idx=l%d
    l//=d
    r//=d
    if idx in dic:
        prel,prer=dic[idx]
        #共通部分ありか、なしかで分岐
        if prer<l or prel>r:
            dic[idx]=(l,r)
            ans+=r-l+1 
        else:
            nl=min(prel,l)
            nr=max(prer,r)
            ans+=prel-nl+nr-prer
            dic[idx]=(nl,nr)
    else:
        ans+=r-l+1 
        dic[idx]=(l,r)
print(ans)

Buggy submission (verdict: Runtime Error):
n,a,d=map(int,input().split())

dic={}

ans=0
for k in range(n+1):
    l=k*a+k*(k-1)*d//2
    r=l+k*(n-k)*d
    idx=l%d
    l//=d
    r//=d
    print(l,r,idx)
    if idx in dic:
        prel,prer=dic[idx]
        #共通部分ありか、なしかで分岐
        if prer<l or prel>r:
            dic[idx]=(l,r)
            ans+=r-l+1 
        else:
            nl=min(prel,l)
            nr=max(prer,r)
            ans+=prel-nl+nr-prer
            dic[idx]=(nl,nr)
    else:
        ans+=r-l+1 
        dic[idx]=(l,r)
print(ans)
Problem p02850 (python)

Fixed submission:
n = int(input())
vs = [[] for i in range(n)]
out = [0]*n

for i in range(n-1):
    a, b = [int(i)-1 for i in input().split()]
    vs[a].append([b,i+1])
    vs[b].append([a,i+1])

nex = [[0,-1]]

def coloring(index,defColor):
    i=0
    for v in vs[index]:
        if i==defColor:
           i+=1
        out[v[1]]=i
        vs[v[0]].remove([index,v[1]])
        nex.append([v[0],i])
        i+=1
    out[0] = max(out[0],i-1)

while nex.__len__()>0:
    c,d = nex.pop()
    coloring(c,d)

for i in range(n):
    print(out[i]+1)

Buggy submission (verdict: Runtime Error):
n = int(input())
vs = [[] for i in range(n)]
out = [0]*n

for i in range(n-1):
    a, b = [int(i) for i in input().split()]
    vs[a].append([b,i+1])
    vs[b].append([a,i+1])

nex = [[0,-1]]

def coloring(index,defColor):
    i=0
    for v in vs[index]:
        if i==defColor:
           i+=1
        out[v[1]]=i
        vs[v[0]].remove([index,v[1]])
        nex.append([v[0],i])
        i+=1
    out[0] = max(out[0],i-1)

while nex.__len__()>0:
    c,d = nex.pop()
    coloring(c,d)

for i in range(n):
    print(out[i]+1)

Buggy submission (verdict: Runtime Error):
n = int(input())
vs = [[] for i in range(n)]
out = [0]*n

for i in range(n-1):
    a, b = [int(i)-1 for i in input().split()]
    vs[a].append([b,i+1])
    vs[b].append([a,i+1])

def coloring(index,defColor):
    i=0
    for v in vs[index]:
        if i==defColor:
           i+=1
        out[v[1]]=i
        vs[v[0]].remove([index,v[1]])
        coloring(v[0],i)
        i+=1
    out[0] = max(out[0],i-1)

coloring(0,-1)
for i in range(n):
    print(out[i]+1)
Problem p02889 (python)

Fixed submission:
n,m,l=map(int,input().split())
cost=[[10**9+1]*n for i in range(n)]
length=[[10**9]*n for i in range(n)]
for i in range(m):
    a,b,c=map(int,input().split())
    a-=1
    b-=1
    cost[a][b]=c
    cost[b][a]=c
q=int(input())
st=[map(int,input().split())for i in range(q)]

for k in range(n):
    for i in range(n):
        for j in range(n):
            cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j])
for i in range(n):
    length[i][i]=0
for i in range(n):
    for j in range(n):
        if cost[i][j]<=l:
            length[i][j]=0
for k in range(n):
    for i in range(n):
        for j in range(n):
            length[i][j]=min(length[i][j],length[i][k]+length[k][j]+1)
for s,t in st:
    ch=min(length[s-1][t-1],length[t-1][s-1])
    print(-1 if ch>10**8 else ch)

Buggy submission (verdict: Runtime Error):
import numpy as np
from scipy.sparse import lil_matrix, csr_matrix
from scipy.sparse.csgraph import shortest_path

n,m,limit=map(int,input().split())
G=np.zeros((n,n),dtype=np.int64)
for i in range(m):
    a,b,cost=map(int,input().split())
    G[a-1,b-1]=cost
    G[b-1,a-1]=cost
G=shortest_path(G,method='FW')
for c in range(n):
    for l in range(n):
        if G[c][l]<=limit:
            G[c][l]=1
        else:
            G[c][l]=0
G=shortest_path(G,method='FW')
q=int(input())
Q=[map(int,input().split())for i in range(q)]
for a,b in Q:
    print(int(G[a-1][b-1])-1)
Problem p02900 (python)

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

#素数
def primes(n):
    n, correction = n-n%6+6, 2-(n%6>1)
    sieve = [True] * (n//3)
    for i in range(1, int(n**0.5)//3+1):
        if sieve[i]:
            k = 3*i+1|1
            sieve[k*k//3::2*k] = [False] * ((n//6-k*k//6-1)//k+1)
            sieve[k*(k-2*(i&1)+4)//3::2*k] = [False] * ((n//6-k*(k-2*(i&1)+4)//6-1)//k+1)
    return [2, 3] + [3*i+1|1 for i in range(1, n//3-correction) if sieve[i]]

#素因数分解
from collections import defaultdict
primelist=primes(int(n**0.5+1))+[n+1]
factors=defaultdict(lambda :0)
for prime in primelist:
    if prime>n:
        factors[n]+=1
        break
    while n%prime==0:
        n=n//prime
        factors[prime]+=1
factors[n]=1
factors[1]=0
print(len(factors))

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

#素数
def primes(n):
    n, correction = n-n%6+6, 2-(n%6>1)
    sieve = [True] * (n//3)
    for i in range(1, int(n**0.5)//3+1):
        if sieve[i]:
            k = 3*i+1|1
            sieve[k*k//3::2*k] = [False] * ((n//6-k*k//6-1)//k+1)
            sieve[k*(k-2*(i&1)+4)//3::2*k] = [False] * ((n//6-k*(k-2*(i&1)+4)//6-1)//k+1)
    return [2, 3] + [3*i+1|1 for i in range(1, n//3-correction) if sieve[i]]

#素因数分解
from collections import defaultdict
primelist=primes(int(n**0.5+1))+[n+1]
factors=defaultdict(lambda :0)
for prime in primelist:
    if prime>n:
        factors[n]+=1
        break
    while n%prime==0:
        n=n//prime
        factors[prime]+=1
factors[n]=1
print(len(factors))

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

#素数
def primes(n):
    n, correction = n-n%6+6, 2-(n%6>1)
    sieve = [True] * (n//3)
    for i in range(1, int(n**0.5)//3+1):
        if sieve[i]:
            k = 3*i+1|1
            sieve[k*k//3::2*k] = [False] * ((n//6-k*k//6-1)//k+1)
            sieve[k*(k-2*(i&1)+4)//3::2*k] = [False] * ((n//6-k*(k-2*(i&1)+4)//6-1)//k+1)
    return [2, 3] + [3*i+1|1 for i in range(1, n//3-correction) if sieve[i]]

#素因数分解
from collections import defaultdict
primelist=primes(int(n**0.5+1))+[n+1]
factors=defaultdict(lambda :0)
for prime in primelist:
    if prime>n:
        factors[n]+=1
        break
    while n%prime==0:
        n=n//prime
        factors[prime]+=1
print(len(factors))
Problem p03061 (python)

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

L,R=[0]*n,[0]*n
L[0],R[-1]=A[0],A[-1]

from fractions import gcd

for i in range(1,n):
    L[i]=gcd(A[i],L[i-1])
for i in range(n-2,-1,-1):
    R[i]=gcd(A[i],R[i+1])

ans=max([L[-2],R[1]]+[gcd(L[i-1],R[i+1])for i in range(1,n-1)])
print(ans)

Buggy submission (verdict: Runtime Error):
n=int(input())
A=list(map(int,input().split()))

L,R=[0]*n,[0]*n
L[0],R[-1]=A[0],A[-1]

from math import gcd

for i in range(1,n):
    L[i]=gcd(A[i],L[i-1])
for i in range(n-2,-1,-1):
    R[i]=gcd(A[i],R[i+1])

ans=max([L[-2],R[1]]+[gcd(L[i-1],R[i+1])for i in range(1,n-1)])
print(ans)