from typing import List
import heapq


class Solution:
    def getOrder(self, tasks: List[List[int]]) -> List[int]:
        tasks_length = len(tasks)
        enumerated_tasks = [[*task, i] for i, task in enumerate(tasks)]
        order, heap = [], []
        current_time, current_task_index = 0, 0

        enumerated_tasks.sort()

        while current_task_index < tasks_length or heap:
            if not heap:
                current_time = max(current_time, enumerated_tasks[current_task_index][0])
            while current_task_index < tasks_length and current_time >= enumerated_tasks[current_task_index][0]:
                heapq.heappush(heap, (enumerated_tasks[current_task_index][1], enumerated_tasks[current_task_index][2]))
                current_task_index += 1
            processing_time, index = heapq.heappop(heap)
            current_time += processing_time
            order.append(index)

        return order


if __name__ == "__main__":
    sol = Solution()

    assert sol.getOrder([[1, 2], [2, 4], [3, 2], [4, 1]]) == [0, 2, 3, 1]
    assert sol.getOrder([[7, 10], [7, 12], [7, 5], [7, 4], [7, 2]]) == [4, 3, 2, 0, 1]
    assert sol.getOrder(
        [[19, 13], [16, 9], [21, 10], [32, 25], [37, 4], [49, 24], [2, 15], [38, 41], [37, 34], [33, 6], [45, 4], [18, 18], [46, 39], [12, 24]]
    ) == [6, 1, 2, 9, 4, 10, 0, 11, 5, 13, 3, 8, 12, 7]
