import heapq


def largest(X, k):
    """
    Return the k largest elements from X.
    """
    # maintain a min-heap of size k containing the largest elements so far
    h = []
    for x in X:
        if len(h) < k:
            heapq.heappush(h, x)
        elif x > h[0]:
            heapq.heapreplace(h, x)
    return h