时间复杂度 O(n logk)。
def topk(xs: list[int], k: int): heap = [] for x in xs: if len(heap) >= k: if x > heap[0]: heapq.heapreplace(heap, x) else: heapq.heappush(heap, x) return heap