topk做法会ttl
def down(heap,i,size):
t=i
if 2*i<=size and heap[2*i]>heap[t]:
t=2*i
if 2*i+1<=size and heap[2*i+1]>heap[t]:
t=2*i+1
if t!=i:
temp = heap[t]
heap[t]=heap[i]
heap[i]=temp
down(heap,t,size)
if __name__=="__main__":
n,m = [int(x) for x in input().split()]
nums = [int(x) for x in input().split()]
heap = [0] + nums[:m]
size = m
for i in range(size//2,0,-1):
down(heap,i,size)
for i in range(m,n):
if nums[i]<heap[1]:
heap[1]=nums[i]
down(heap,1,size)
res = []
for i in range(0,m):
res.insert(0,str(heap[1]))
heap[1]=heap[size]
size-=1
down(heap,1,size)
print(" ".join(res))
def down(heap,i,size):
t=i
if 2*i<=size and heap[2*i]<heap[t]:
t=2*i
if 2*i+1<=size and heap[2*i+1]<heap[t]:
t=2*i+1
if t!=i:
temp = heap[t]
heap[t]=heap[i]
heap[i]=temp
down(heap,t,size)
if __name__=="__main__":
n,m = [int(x) for x in input().split()]
nums = [int(x) for x in input().split()]
heap = [0] + nums
size = n
for i in range(size//2,0,-1):
down(heap,i,size)
res = []
for i in range(0,m):
res.append(str(heap[1]))
heap[1]=heap[size]
size-=1
down(heap,1,size)
print(" ".join(res))