Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
alist=list(map(int,input().split()))
#print(alist)

dic_a={}
for a in alist:
  if not a in dic_a:
    dic_a[a]=1
  else:
    dic_a[a]+=1
    
dic2_a={}
for key in dic_a:
  if dic_a[key]>=2:
    dic2_a[key]=dic_a[key]
    
#print(dic2_a)
dic_answer={}
for a in alist:
  if a in dic_answer:
    print(dic_answer[a])
    continue
  
  dic2_a_copy=dic2_a.copy()
  if a in dic2_a_copy:
    dic2_a_copy[a]-=1
  
  #print(dic_a_copy)
  answer=0
  for val in dic2_a_copy.values():
    answer+=val*(val-1)//2
    
  print(answer)
  dic_answer[a]=answer

Fixed submission:
N=int(input())
alist=list(map(int,input().split()))
#print(alist)

dic_a={}
for a in alist:
  if not a in dic_a:
    dic_a[a]=1
  else:
    dic_a[a]+=1
    
all_pair=0
for val in dic_a.values():
  all_pair+=val*(val-1)//2

#print(dic_a,all_pair)
dic_answer={}
for a in alist:
  if a in dic_answer:
    print(dic_answer[a])
    continue
  
  now=dic_a[a]
  kill=now*(now-1)//2-(now-1)*(now-2)//2
  answer=all_pair-kill
  
  print(answer)
  dic_answer[a]=answer

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
alist=list(map(int,input().split()))
#print(alist)

dic_a={}
for a in alist:
  if not a in dic_a:
    dic_a[a]=1
  else:
    dic_a[a]+=1
    
dic2_a={}
for key in dic_a:
  if dic_a[key]>=2:
    dic2_a[key]=dic_a[key]
    
#print(dic2_a)
for a in alist:
  dic2_a_copy=dic2_a.copy()
  if a in dic2_a_copy:
    dic2_a_copy[a]-=1
  
  #print(dic_a_copy)
  answer=0
  for val in dic2_a_copy.values():
    answer+=val*(val-1)//2
    
  print(answer)

Fixed submission:
N=int(input())
alist=list(map(int,input().split()))
#print(alist)

dic_a={}
for a in alist:
  if not a in dic_a:
    dic_a[a]=1
  else:
    dic_a[a]+=1
    
all_pair=0
for val in dic_a.values():
  all_pair+=val*(val-1)//2

#print(dic_a,all_pair)
dic_answer={}
for a in alist:
  if a in dic_answer:
    print(dic_answer[a])
    continue
  
  now=dic_a[a]
  kill=now*(now-1)//2-(now-1)*(now-2)//2
  answer=all_pair-kill
  
  print(answer)
  dic_answer[a]=answer

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
alist=list(map(int,input().split()))
#print(alist)

dic_a={}
for a in alist:
  if not a in dic_a:
    dic_a[a]=1
  else:
    dic_a[a]+=1
    
dic2_a={}
for key in dic_a:
  if dic_a[key]>=2:
    dic2_a[key]=dic_a[key]
    
#print(dic2_a)
for a in alist:
  dic2_a_copy=dic2_a.copy()
  if a in dic2_a_copy:
    dic2_a_copy[a]-=1
  
  #print(dic_a_copy)
  answer=0
  for val in dic2_a_copy.values():
    answer+=val*(val-1)//2
    
  print(answer)

Fixed submission:
N=int(input())
alist=list(map(int,input().split()))
#print(alist)

dic_a={}
for a in alist:
  if not a in dic_a:
    dic_a[a]=1
  else:
    dic_a[a]+=1
    
all_pair=0
for val in dic_a.values():
  all_pair+=val*(val-1)//2

#print(dic_a,all_pair)
dic_answer={}
for a in alist:
  if a in dic_answer:
    print(dic_answer[a])
    continue
  
  now=dic_a[a]
  kill=now*(now-1)//2-(now-1)*(now-2)//2
  answer=all_pair-kill
  
  print(answer)
  dic_answer[a]=answer
Buggy python submission (verdict: Time Limit Exceeded):
N,P=map(int,input().split())
S=input()

answer=0
for i in range(N):
  for j in range(i,N):
    si=int(S[i:j+1])
    if si%P==0:
      answer+=1
      
print(answer)
    

Fixed submission:
N,P=map(int,input().split())
S=input()

answer=0
if P==2 or P==5:
  for i in range(N):
    si=int(S[i])
    if si%P==0:
      answer+=i+1
else:
  mod_table=[0]*P
  mod_table[0]=1
  revS=S[::-1]
  
  modp=0
  mod10=1
  for i in range(N):
    mod10=(mod10*10)%P
    #modp=(10*modp+int(revS[i]))%P
    modp=(modp+mod10*int(revS[i]))%P
    mod_table[modp]+=1
    
  #print(mod_table)
  for i in range(P):
    answer+=mod_table[i]*(mod_table[i]-1)//2
  
print(answer)
Buggy python submission (verdict: Time Limit Exceeded):
H,N=map(int,input().split())

ablist=[]
for i in range(N):
  a,b=map(int,input().split())
  ablist.append((a,b))
  
dp=[]
for i in range(N+1):
  dp.append([10**9]*(H+1))
  
for i in range(N+1):
  dp[i][0]=0
  
#print(dp)  
for i in range(1,N+1):
  ai,bi=ablist[i-1]
  for j in range(1,H+1):
    if j-ai<0:
      dp[i][j]=min(dp[i-1][j],bi)
    else:
      dp[i][j]=min(dp[i-1][j],dp[i][j-ai]+bi)
      
#print(dp)
print(dp[N][H])

Fixed submission:
H,N=map(int,input().split())

ablist=[]
for i in range(N):
  a,b=map(int,input().split())
  ablist.append((a,b))
  
dp=[]
for i in range(N+1):
  dp.append([10**9]*(H+1))
  
for i in range(N+1):
  dp[i][0]=0
  
#print(dp)  
for i in range(1,N+1):
  ai,bi=ablist[i-1]
  for j in range(1,H+1):
    if j-ai<0:
      dp[i][j]=min(dp[i-1][j],bi)
    else:
      dp[i][j]=min(dp[i-1][j],dp[i][j-ai]+bi)
      
#print(dp)
print(dp[N][H])
Buggy python submission (verdict: Time Limit Exceeded):
H,W=map(int,input().split())

def warshall_floyd(vl,d):
  #d[vi][vj]: viからvjへの最短距離
  for vk in vl:
    for vi in vl:
      for vj in vl:
        if vk in d[vi] and vj in d[vk]:
          if not vj in d[vi]:
            d[vi][vj]=d[vi][vk]+d[vk][vj]
          else:
            d[vi][vj]=min(d[vi][vj],d[vi][vk]+d[vk][vj])
  return d

# vlistに点のリスト、dist[vi][vj]に辺(vi,vj)のコストを入れて呼び出す
mat=[[x=="." for x in input()] for _ in range(H)]
#print(mat)

vlist=[]
dist={}
for i in range(H):
  for j in range(W):
    if mat[i][j]:
      vlist.append((i,j))
      if not (i,j) in dist:
        dist[(i,j)]={}
      dist[(i,j)][(i,j)]=0
      if i>0 and mat[i-1][j]:
          dist[(i,j)][(i-1,j)]=1
      if i<H-1 and mat[i+1][j]:
          dist[(i,j)][(i+1,j)]=1
      if j>0 and mat[i][j-1]:
          dist[(i,j)][(i,j-1)]=1
      if j<W-1 and mat[i][j+1]:
          dist[(i,j)][(i,j+1)]=1

#print(vlist)
#print(dist)
dist=warshall_floyd(vlist,dist)
#print(dist)
answer=0
for x in dist:
  for y in dist[x]:
    answer=max(answer,dist[x][y])
    
print(answer)

Fixed submission:
H,W=map(int,input().split())

mat=[]
mat.append([False]*(W+2))
for i in range(H):
  line=input()
  row=[False]
  for j in line:
    if j=="#":
      row.append(False)
    else:
      row.append(True)
  row.append(False)
  mat.append(row)
mat.append([False]*(W+2))
#print(mat)

max_depth=0
for i in range(1,H+1):
  for j in range(1,W+1):
    if not mat[i][j]:
      continue
    
    bfs_mat=[]
    for ii in range(H+1):
      bfs_mat.append([99]*(W+1))
      
    #print(bfs_mat)
    max_dist=0
    bfs_queue=[(i,j,0)]
    while(len(bfs_queue)>0):
      new_queue=set()
      for (ii,jj,dist) in bfs_queue:
        #print((ii,jj,dist))
        bfs_mat[ii][jj]=dist
        max_dist=dist
        if mat[ii-1][jj] and bfs_mat[ii-1][jj]==99:
          new_queue.add((ii-1,jj,dist+1))
        if mat[ii+1][jj] and bfs_mat[ii+1][jj]==99:
          new_queue.add((ii+1,jj,dist+1))
        if mat[ii][jj-1] and bfs_mat[ii][jj-1]==99:
          new_queue.add((ii,jj-1,dist+1))
        if mat[ii][jj+1] and bfs_mat[ii][jj+1]==99:
          new_queue.add((ii,jj+1,dist+1))

      bfs_queue=new_queue
      #print(bfs_queue)

    #print((i,j,max_dist))
    if max_dist>max_depth:
      max_pos=(i,j)
      max_depth=max_dist

#print(max_pos)
print(max_depth)
Buggy python submission (verdict: Runtime Error):
MOD=10**9+7
N,K=map(int,input().split())
alist=list(map(int,input().split()))
alist.sort()
#print(alist)

inv_table = [0]+[1]
for i in range(2,K+3):
  inv_table.append(-(MOD//i)*inv_table[MOD%i]%MOD)

min_s,max_s=0,0
old_comb,new_comb=0,1
for i in range(K,N+1):
  #print(K,i,new_comb-old_comb)
  min_s+=(new_comb-old_comb)*alist[-1-(i-1)]
  min_s%=MOD
  max_s+=(new_comb-old_comb)*alist[i-1]
  max_s%=MOD
  
  old_comb=new_comb
  new_comb=old_comb*(i+1)*inv_table[i+1-K]%MOD
#print(min_s,max_s)

print((max_s-min_s)%MOD)

Fixed submission:
MOD=10**9+7
N,K=map(int,input().split())
alist=list(map(int,input().split()))
alist.sort()
#print(alist)

inv_table = [0]+[1]
for i in range(2,N+1):
  inv_table.append(-(MOD//i)*inv_table[MOD%i]%MOD)

min_s,max_s=0,0
old_comb,new_comb=0,1
for i in range(K,N+1):
  #print(K,i,new_comb-old_comb)
  min_s+=(new_comb-old_comb)*alist[-1-(i-1)]
  min_s%=MOD
  max_s+=(new_comb-old_comb)*alist[i-1]
  max_s%=MOD
  
  old_comb=new_comb
  new_comb=old_comb*(i+1)*inv_table[i+1-K]%MOD
#print(min_s,max_s)

print((max_s-min_s)%MOD)
Buggy python submission (verdict: Time Limit Exceeded):
MOD=10**9+7
N=int(input())
xlist=list(map(int,input().split()))

inv_table = [0]+[1]
for i in range(2,N+1):
  inv_table.append(-(MOD//i)*inv_table[MOD%i]%MOD)

fact_nm1=1
for i in range(1,N):
  fact_nm1*=i
  fact_nm1%=MOD
#print(fact_nm1)

answer=0
for i in range(N-1):
  for j in range(i+1,N-1):
    ji_inv=inv_table[j-i]
    ji1_inv=inv_table[j-i+1]
    term=(ji_inv*ji1_inv)%MOD
    answer+=term*(xlist[j]-xlist[i])
    answer%=MOD
    #print(i,j,term)
  
  ni_inv=inv_table[N-1-i]
  term=ni_inv%MOD
  answer+=term*(xlist[N-1]-xlist[i])
  answer%=MOD
  #print(i,N-1,term)
  
answer*=fact_nm1
answer%=MOD  
print(answer)

Fixed submission:
MOD=10**9+7
N=int(input())
xlist=list(map(int,input().split()))

fact_nm1=1
for i in range(1,N):
  fact_nm1*=i
  fact_nm1%=MOD
#print(fact_nm1)

answer_bunshi=0
answer_bumbo=1
inv_bunshi=0
inv_bumbo=1
for i in range(1,N):  
  inv_bunshi=(i*inv_bunshi+inv_bumbo)%MOD
  inv_bumbo=(inv_bumbo*i)%MOD
  #print(inv_bunshi,inv_bumbo)
  
  xdiff=xlist[i]-xlist[i-1]
  term_bunshi=(xdiff*inv_bunshi)%MOD
  answer_bunshi=(answer_bunshi*inv_bumbo+term_bunshi*answer_bumbo)%MOD
  answer_bumbo=(answer_bumbo*inv_bumbo)%MOD
#print(answer_bunshi,answer_bumbo)

answer_bunshi=(answer_bunshi*fact_nm1)%MOD
answer=answer_bunshi*pow(answer_bumbo,MOD-2,MOD)
answer%=MOD
print(answer)

Buggy python submission (verdict: Time Limit Exceeded):
MOD=10**9+7

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

fact_nm1=1
for i in range(1,N):
  fact_nm1*=i
  fact_nm1%=MOD
#print(fact_nm1)

answer=0
for i in range(N-1):
  for j in range(i+1,N-1):
    ji_inv=pow(j-i,MOD-2,MOD)
    ji1_inv=pow(j-i+1,MOD-2,MOD)
    term=(fact_nm1*ji_inv*ji1_inv)%MOD
    answer+=term*(xlist[j]-xlist[i])
    answer%=MOD    
    #print(i,j,term)
  
  ni_inv=pow(N-1-i,MOD-2,MOD)
  term=(fact_nm1*ni_inv)%MOD
  answer+=term*(xlist[N-1]-xlist[i])
  answer%=MOD
  #print(i,N-1,term)
  
print(answer)

Fixed submission:
MOD=10**9+7
N=int(input())
xlist=list(map(int,input().split()))

fact_nm1=1
for i in range(1,N):
  fact_nm1*=i
  fact_nm1%=MOD
#print(fact_nm1)

answer_bunshi=0
answer_bumbo=1
inv_bunshi=0
inv_bumbo=1
for i in range(1,N):  
  inv_bunshi=(i*inv_bunshi+inv_bumbo)%MOD
  inv_bumbo=(inv_bumbo*i)%MOD
  #print(inv_bunshi,inv_bumbo)
  
  xdiff=xlist[i]-xlist[i-1]
  term_bunshi=(xdiff*inv_bunshi)%MOD
  answer_bunshi=(answer_bunshi*inv_bumbo+term_bunshi*answer_bumbo)%MOD
  answer_bumbo=(answer_bumbo*inv_bumbo)%MOD
#print(answer_bunshi,answer_bumbo)

answer_bunshi=(answer_bunshi*fact_nm1)%MOD
answer=answer_bunshi*pow(answer_bumbo,MOD-2,MOD)
answer%=MOD
print(answer)
Buggy python submission (verdict: Runtime Error):
MOD=10**9+7
N=int(input())

tree=[[] for _ in range(N+1)]
for _ in range(N-1):
  A,B=map(int,input().split())
  tree[A].append(B)
  tree[B].append(A)
#print(tree)

def dfs(u,par):
  visited[u]=True
  ret=1
  for v in tree[u]:
    if not visited[v]:
      ret+=dfs(v,u)

  if par>0:
    tree_dic[(u,par)]=ret
  return ret
  
visited=[False]*(N+1)
tree_dic={}
dfs(1,0)
#print(tree_dic)

bumbo=pow(2,N,MOD)
bunshi=(pow(2,N,MOD)-1-N*pow(2,N-1,MOD))%MOD
for x in tree_dic.values():
  term=(pow(2,x,MOD)-1)*(pow(2,N-x,MOD)-1)
  bunshi+=term  
  bunshi%=MOD
#print(bunshi,bumbo)

answer=bunshi*pow(bumbo,MOD-2,MOD)
answer%=MOD
print(answer)

Fixed submission:
import sys
sys.setrecursionlimit(10**9)
MOD=10**9+7
N=int(input())

tree=[[] for _ in range(N+1)]
for _ in range(N-1):
  A,B=map(int,input().split())
  tree[A].append(B)
  tree[B].append(A)
#print(tree)

def dfs(u,par):
  visited[u]=True
  ret=1
  for v in tree[u]:
    if not visited[v]:
      ret+=dfs(v,u)

  if par>0:
    tree_dic[(u,par)]=ret
  return ret
  
visited=[False]*(N+1)
tree_dic={}
dfs(1,0)
#print(tree_dic)

bumbo=pow(2,N,MOD)
bunshi=(pow(2,N,MOD)-1-N*pow(2,N-1,MOD))%MOD
for x in tree_dic.values():
  term=(pow(2,x,MOD)-1)*(pow(2,N-x,MOD)-1)
  bunshi+=term  
  bunshi%=MOD
#print(bunshi,bumbo)

answer=bunshi*pow(bumbo,MOD-2,MOD)
answer%=MOD
print(answer)
Buggy python submission (verdict: Time Limit Exceeded):
MOD=10**9+7

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

#print(alist)
blist=[0]*64
for a in alist:
  bblist=[]
  acopy=a
  while(acopy>0):
    bblist.append(acopy%2)
    acopy//=2
    
  #print(bblist)
  for b in range(len(bblist)):
    blist[-1-b]+=bblist[b]
      
blist.reverse()
#print(blist)
answer=0
pow=1
for i in range(64):
  answer+=blist[i]*(N-blist[i])*pow
  pow*=2
  
print(answer%MOD)

Fixed submission:
MOD=10**9+7

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

#print(alist)
blist=[0]*64
for a in alist:
  bblist=[]
  acopy=a
  while(acopy>0):
    bblist.append(acopy%2)
    acopy//=2
    
  #print(bblist)
  for b in range(len(bblist)):
    blist[-1-b]+=bblist[b]
      
blist.reverse()
#print(blist)
answer=0
for i in range(64):
  answer+=blist[i]*(N-blist[i])*(2**i)
  
print(answer%MOD)

Buggy python submission (verdict: Time Limit Exceeded):
MOD=10**9+7

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

#print(alist)
blist=[0]*64
for a in alist:
  bin_str=format(a, '064b')
  dlist=list(map(int,list(bin_str)))
  #print(bin_str)
  for i in range(64):
    blist[i]+=dlist[i]
      
blist.reverse()
#print(blist)
answer=0
for i in range(64):
  answer+=blist[i]*(N-blist[i])*(2**i)
  
print(answer%MOD)

Fixed submission:
MOD=10**9+7

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

#print(alist)
blist=[0]*64
for a in alist:
  bblist=[]
  acopy=a
  while(acopy>0):
    bblist.append(acopy%2)
    acopy//=2
    
  #print(bblist)
  for b in range(len(bblist)):
    blist[-1-b]+=bblist[b]
      
blist.reverse()
#print(blist)
answer=0
for i in range(64):
  answer+=blist[i]*(N-blist[i])*(2**i)
  
print(answer%MOD)

Buggy python submission (verdict: Time Limit Exceeded):
MOD=10**9+7

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

#print(alist)
blist=[0]*64
for a in alist:
  bin_str=format(a, '064b')
  dlist=list(map(int,list(bin_str)))
  #print(bin_str)
  for i in range(64):
    blist[i]+=dlist[i]
      
blist.reverse()
#print(blist)
answer=0
for i in range(64):
  answer+=blist[i]*(N-blist[i])*(2**i)
  
print(answer%MOD)

Fixed submission:
MOD=10**9+7

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

#print(alist)
blist=[0]*64
for a in alist:
  bblist=[]
  acopy=a
  while(acopy>0):
    bblist.append(acopy%2)
    acopy//=2
    
  #print(bblist)
  for b in range(len(bblist)):
    blist[-1-b]+=bblist[b]
      
blist.reverse()
#print(blist)
answer=0
for i in range(64):
  answer+=blist[i]*(N-blist[i])*(2**i)
  
print(answer%MOD)
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
S=input()

answer=0
for i in range(1000):
  i0=i//100
  i1=(i//10)%10
  i2=i%10

  flag_num=0
  for s in S:
    if flag_num==0 and s==str(i0):
      flag_num+=1
    elif flag_num==1 and s==str(i1):
      flag_num+=1
    elif flag_num==2 and s==str(i2):
      answer+=1
      break
      
print(answer)

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

answer=0
for i in range(1000):
  i0=i//100
  i1=(i//10)%10
  i2=i%10

  flag_num=0
  for s in S:
    if flag_num==0 and s==str(i0):
      flag_num+=1
    elif flag_num==1 and s==str(i1):
      flag_num+=1
    elif flag_num==2 and s==str(i2):
      answer+=1
      break
      
print(answer)
Buggy python submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline
N=int(input())

def dfs(u,par):
  col=1
  for v in graph[u]:
    if col==par:
      col+=1
    color_dic[(u,v)]=col
    dfs(v,col)
    col+=1

elist=[]
graph=[[] for _ in range(N+1)]
for _ in range(N-1):
  a,b=map(int,input().split())
  graph[a].append(b)
  elist.append((a,b))
#print(graph)

color_dic={}
dfs(1,-1)
#print(color_dic)

K=len(set(color_dic.values()))
print(K)
for e in elist:
  print(color_dic[e])

Fixed submission:
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
N=int(input())

def dfs(u,par):
  col=1
  for v in graph[u]:
    if col==par:
      col+=1
    color_dic[(u,v)]=col
    dfs(v,col)
    col+=1

elist=[]
graph=[[] for _ in range(N+1)]
for _ in range(N-1):
  a,b=map(int,input().split())
  graph[a].append(b)
  elist.append((a,b))
#print(graph)

color_dic={}
dfs(1,-1)
#print(color_dic)

K=len(set(color_dic.values()))
print(K)
for e in elist:
  print(color_dic[e])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq

N,K=map(int,input().split())
alist=list(map(int,input().split()))
flist=list(map(int,input().split()))

alist.sort()
flist.sort(reverse=True)
#print(alist)
#print(flist)

if sum(alist)<=K:
  print(0)
  sys.exit(0)
  
hq=[]
for i in range(N):
  heapq.heappush(hq,(-alist[i]*flist[i],alist[i],flist[i]))
  
for i in range(K):
  naf,a,f=heapq.heappop(hq)
  naf+=f
  a-=1
  if a>0:
    heapq.heappush(hq,(naf,a,f))
    
if len(hq)==0:
  print(0)
else:
  naf,a,f=heapq.heappop(hq)
  print(-naf)

Fixed submission:
import sys
N,K=map(int,input().split())
alist=list(map(int,input().split()))
flist=list(map(int,input().split()))
alist.sort()
flist.sort(reverse=True)
#print(alist)
#print(flist)

def check(m):
  cnt=0
  for i in range(N):
    if alist[i]*flist[i]>m:
      cnt+=-(-(alist[i]*flist[i]-m)//flist[i])
  
  #print(m,cnt,K)
  if cnt<=K:
    return True
  else:
    return False

if sum(alist)<=K:
  print(0)
  sys.exit(0)

baseline=0
for i in range(N):
  baseline=max(baseline,alist[i]*flist[i])
#print(baseline)

l,r=0,baseline
while l<=r:
  mid=(l+r)//2
  #print(l,mid,r)
  if not check(mid-1) and check(mid):
    print(mid)
    break
  elif not check(mid):
    l=mid+1
  else:
    r=mid-1

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import heapq

N,K=map(int,input().split())
alist=list(map(int,input().split()))
flist=list(map(int,input().split()))

alist.sort()
flist.sort(reverse=True)
#print(alist)
#print(flist)

if sum(alist)<=K:
  print(0)
  sys.exit(0)
  
hq=[]
for i in range(N):
  heapq.heappush(hq,(-alist[i]*flist[i],alist[i],flist[i]))
  
for i in range(K):
  naf,a,f=heapq.heappop(hq)
  naf+=f
  a-=1
  if a>0:
    heapq.heappush(hq,(naf,a,f))
    
if len(hq)==0:
  print(0)
else:
  naf,a,f=heapq.heappop(hq)
  print(-naf)

Fixed submission:
import sys
N,K=map(int,input().split())
alist=list(map(int,input().split()))
flist=list(map(int,input().split()))
alist.sort()
flist.sort(reverse=True)
#print(alist)
#print(flist)

def check(m):
  cnt=0
  for i in range(N):
    if alist[i]*flist[i]>m:
      cnt+=-(-(alist[i]*flist[i]-m)//flist[i])
  
  #print(m,cnt,K)
  if cnt<=K:
    return True
  else:
    return False

if sum(alist)<=K:
  print(0)
  sys.exit(0)

baseline=0
for i in range(N):
  baseline=max(baseline,alist[i]*flist[i])
#print(baseline)

l,r=0,baseline
while l<=r:
  mid=(l+r)//2
  #print(l,mid,r)
  if not check(mid-1) and check(mid):
    print(mid)
    break
  elif not check(mid):
    l=mid+1
  else:
    r=mid-1
Buggy python submission (verdict: Runtime Error):
N=int(input())
L=list(map(int,input().split()))
L.sort()
#print(L)

answer=0
for i in range(N-2):
  index_c=i+2
  for j in range(i+1,N-1):
    for k in range(index_c,N):
      if L[i]+L[j]<=L[k]:
        #print(i,j,k-1,k-j-1)
        answer+=k-j-1
        index_c=k-1
        break
    else:
      #print(i,j,N-1,N-j-1)
      answer+=N-j-1
      index_c=N-1
        
print(answer)

Fixed submission:
A,B=map(int,input().split())
print(max(0,A-2*B))
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
L=list(map(int,input().split()))
L.sort()
#print(L)

answer=0
for i in range(N-2):
  index_c=i+2
  for j in range(i+1,N-1):
    for k in range(index_c,N):
      if L[i]+L[j]<=L[k]:
        #print(i,j,k-1,k-j-1)
        answer+=k-j-1
        index_c=k-1
        break
    else:
      #print(i,j,N-1,N-j-1)
      answer+=N-j-1
      index_c=N-1
        
print(answer)

Fixed submission:
N=int(input())
L=list(map(int,input().split()))
L.sort()
#print(L)

answer=0
for i in range(N-2):
  index_c=i+2
  for j in range(i+1,N-1):
    for k in range(index_c,N):
      if L[i]+L[j]<=L[k]:
        #print(i,j,k-1,k-j-1)
        answer+=k-j-1
        index_c=k-1
        break
    else:
      #print(i,j,N-1,N-j-1)
      answer+=N-j-1
      index_c=N-1
        
print(answer)
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())

graph=[]
for i in range(N):
  graph.append([])

for i in range(N):
  S=list(map(int,input()))
  for j in range(N):
    if S[j]==1:
      graph[i].append(j)
      
#print(graph)
max_set=-1
for i in range(N):
  node_list=[-1]*N
  #bfs
  d=1
  queue=[i]
  illegal=False
  while(True):
    new_queue=[]
    for q in queue:
      node_list[q]=d
      for qq in graph[q]:
        if node_list[qq]==-1:
          new_queue.append(qq)
        elif node_list[qq]!=d-1:
          illegal=True
          break
      
    if illegal:
      break
    elif len(new_queue)==0:
      #print(node_list,d)
      max_set=max(max_set,d)
      break
    else:
      #print(node_list,new_queue)
      queue=new_queue
      d+=1
      
print(max_set)


Fixed submission:
N=int(input())

graph=[]
for i in range(N):
  graph.append([])

for i in range(N):
  S=list(map(int,input()))
  for j in range(N):
    if S[j]==1:
      graph[i].append(j)
      
#print(graph)
max_set=-1
for i in range(N):
  node_list=[-1]*N
  #bfs
  d=1
  queue={i}
  illegal=False
  while(True):
    new_queue=set()
    for q in queue:
      node_list[q]=d
      for qq in graph[q]:
        if node_list[qq]==-1:
          new_queue.add(qq)
        elif node_list[qq]!=d-1:
          illegal=True
          break
      
    if illegal:
      break
    elif len(new_queue)==0:
      #print(node_list,d)
      max_set=max(max_set,d)
      break
    else:
      #print(node_list,new_queue)
      queue=new_queue
      d+=1
      
print(max_set)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())

graph=[]
for i in range(N):
  graph.append([])

for i in range(N):
  S=list(map(int,input()))
  for j in range(N):
    if S[j]==1:
      graph[i].append(j)
      
#print(graph)
max_set=-1
for i in range(N):
  node_list=[-1]*N
  #bfs
  d=1
  queue=[i]
  illegal=False
  while(True):
    new_queue=[]
    for q in queue:
      node_list[q]=d
      for qq in graph[q]:
        if node_list[qq]==-1:
          new_queue.append(qq)
        elif node_list[qq]!=d-1:
          illegal=True
          break
      
    if illegal:
      break
    elif len(new_queue)==0:
      #print(node_list,d)
      max_set=max(max_set,d)
      break
    else:
      #print(node_list,new_queue)
      queue=new_queue
      d+=1
      
print(max_set)

Fixed submission:
N=int(input())

graph=[]
for i in range(N):
  graph.append([])

for i in range(N):
  S=list(map(int,input()))
  for j in range(N):
    if S[j]==1:
      graph[i].append(j)
      
#print(graph)
max_set=-1
for i in range(N):
  node_list=[-1]*N
  #bfs
  d=1
  queue={i}
  illegal=False
  while(True):
    new_queue=set()
    for q in queue:
      node_list[q]=d
      for qq in graph[q]:
        if node_list[qq]==-1:
          new_queue.add(qq)
        elif node_list[qq]!=d-1:
          illegal=True
          break
      
    if illegal:
      break
    elif len(new_queue)==0:
      #print(node_list,d)
      max_set=max(max_set,d)
      break
    else:
      #print(node_list,new_queue)
      queue=new_queue
      d+=1
      
print(max_set)