All problems

Problem 599

Single-Threaded CPU

Medium · 20 pts · Heap, Sorting, Simulation

Problem

N tasks are numbered from 0. Task i becomes available at time enqueue[i] and needs process[i] time units. A single CPU starts at time 0. Whenever it is idle and some tasks are available, it starts the available task with the shortest processing time, breaking ties by the smaller index, and runs it to completion without interruption. If no task is available, it waits until the next task arrives. Print the order in which the tasks are processed. Sort tasks by arrival and use a min-heap of (process time, index).

Input

The first line contains N. Each of the next N lines contains enqueue[i] and process[i].

Output

Print the task indices in processing order, separated by spaces.

Constraints

1 <= N <= 100000; 1 <= enqueue[i], process[i] <= 10^9

Example input

4
1 2
2 4
3 2
4 1

Example output

0 2 3 1

Explanation

Task 0 arrives at time 1 and runs until 3. By then tasks 1 and 2 are available; task 2 is shorter, so it runs until 5. Then task 3 (1 unit) runs before task 1 (4 units). The order is 0 2 3 1.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Smallest Range Covering Elements from K Lists Next: Trapping Rain Water II