Problem p02699 (python)

Fixed submission:
import sys
input=lambda: sys.stdin.readline().rstrip()
s,w=map(int,input().split())
if w>=s:
  print("unsafe")
else:
  print("safe")

Buggy submission (verdict: Runtime Error):
import sys
input=lambda: sys.stdin.readline().rstrip()
t=int(input())
for _ in range(t):
  c,d=map(int,input().split())
  XX=[int(i) for i in input().split()]
  Y=[]
  YY=[]
  X=[0]*c
  for i,x in enumerate(XX):
    if x>0:
      Y.append(x)
      X[i+1]=x
    else:
      YY.append((-x,i+1))

  Y.sort()
  Y=[0]+Y
  YY.sort()
  for x,i in YY:
    X[i]=Y[x-1]+1
    Y=Y[:x]+[Y[x-1]+1]+Y[x:]



  edge=[[] for i in range(c)]
  E=[0]*d
  for i in range(d):
    u,v=map(int,input().split())
    edge[u-1].append([v-1,i])
    edge[v-1].append([u-1,i])
  
  D=[[] for i in range(1000)]
  for i in range(1,c):
    D[X[i]].append(i)
  for i in range(1,1000):
    if not D[i]:
      continue
    else:
      for goal in D[i]:
        t_root=0
        dist=float("inf")
        t_ind=0
        for root,ind in edge[goal]:
          if X[root]<dist:
            t_root=root
            dist=X[root]
            t_ind=ind
        E[t_ind]=i-dist
  for i in range(d):
    if E[i]==0:
      E[i]=str(10**6)
    else:
      E[i]=str(E[i])
  ans=" ".join(E)
  print("Case #"+str(_+1)+": "+ans)

Buggy submission (verdict: Runtime Error):
#Bです
import sys
input=lambda: sys.stdin.readline().rstrip()
t=int(input())
for _ in range(t):
  c,d=map(int,input().split())
  X=[0]+[-int(i) for i in input().split()]
  edge=[[] for i in range(c)]
  E=[0]*d
  for i in range(d):
    u,v=map(int,input().split())
    edge[u-1].append([v-1,i])
    edge[v-1].append([u-1,i])
  
  D=[[] for i in range(c)]
  for i in range(1,c):
    D[X[i]].append(i)
  for i in range(1,c):
    if not D[i]:
      continue
    else:
      for goal in D[i]:
        t_root=0
        dist=float("inf")
        t_ind=0
        for root,ind in edge[goal]:
          if X[root]<dist:
            t_root=root
            dist=X[root]
            t_ind=ind
        E[t_ind]=i-dist
  for i in range(d):
    if E[i]==0:
      E[i]=str(10**6)
    else:
      E[i]=str(E[i])
  ans=" ".join(E)
  print("Case #"+str(_+1)+": "+ans)

Buggy submission (verdict: Runtime Error):
#Aです
import sys
input=lambda: sys.stdin.readline().rstrip()

def f(x,y):
  left,right=0,10**9
  while right-left>1:
    mid=(left+right)//2
    if x*mid+mid*(mid-1)//2<=y:
      left=mid
    else:
      right=mid
  return left

def f1(x,y):
  left,right=0,10**9
  while right-left>1:
    mid=(left+right)//2
    if x*mid+mid**2<=y:
      left=mid
    else:
      right=mid
  return left

def f2(x,y):
  left,right=0,10**9
  while right-left>1:
    mid=(left+right)//2
    if x*mid+mid*(mid+1)<=y:
      left=mid
    else:
      right=mid
  return left

t=int(input())
for _ in range(t):
  l,r=map(int,input().split())
  if l>=r:
    d=l-r
    ct=f(1,d)
    l-=ct*(ct+1)//2
    ct1=f1(ct,l)
    ct2=f2(ct,r)
    if ct1<=ct2:
      ans1=ct+2*ct1
      ans2=l-ct*ct1-ct1**2
      ans3=r-ct*ct1-ct1*(ct1+1)
    else:
      ans1=ct+2*ct2+1
      ans2=l-ct*(ct2+1)-(ct2+1)**2
      ans3=r-ct*ct2-ct2*(ct2+1) 
    print("Case #"+str(_+1)+": "+str(ans1)+" "+str(ans2)+" "+str(ans3))
  else:
    d=r-l
    ct=f(1,d)
    r-=ct*(ct+1)//2
    if l==r:
      ct1=f1(ct,l)
      ct2=f2(ct,r)
      if ct1<=ct2:
        ans1=ct+2*ct1
        ans2=l-ct*ct1-ct1**2
        ans3=r-ct*ct1-ct1*(ct1+1)
      else:
        ans1=ct+2*ct2+1
        ans2=l-ct*(ct2+1)-(ct2+1)**2
        ans3=r-ct*ct2-ct2*(ct2+1) 
      print("Case #"+str(_+1)+": "+str(ans1)+" "+str(ans2)+" "+str(ans3))
    
    else:
      ct1=f1(ct,r)
      ct2=f2(ct,l)
      if ct1<=ct2:
        ans1=ct+2*ct1
        ans3=r-ct*ct1-ct1**2
        ans2=l-ct*ct1-ct1*(ct1+1)
      else:
        ans1=ct+2*ct2+1
        ans3=r-ct*(ct2+1)-(ct2+1)**2
        ans2=l-ct*ct2-ct2*(ct2+1) 
      print("Case #"+str(_+1)+": "+str(ans1)+" "+str(ans2)+" "+str(ans3))