import copy
import heapq


def min_heap(arr: list, k: int) -> list:
    """
    Create a min heap of k+1 elements (using first k+1 elements), pop and push one by one
    Time Complexity: O(k+(n-k)log(k))
    Space Complexity: O(k)
    """
    l: int = len(arr)
    heap = copy.copy(arr[: k + 1])
    heapq.heapify(heap)

    for i in range(k + 1, l):
        arr[i - k - 1] = heapq.heappushpop(heap, arr[i])

    for i in range(l - k - 1, l):
        arr[i] = heapq.heappop(heap)

    return arr