CI Runner Dispatch

medium heap simulation 1000 ms · 256 MB

CI Runner Dispatch

Your team's continuous-integration cluster has k identical runners, numbered 1 to k. At second 0 all of them are idle, and the build queue holds n jobs. Job i takes exactly d_i seconds to run, on any runner.

The dispatcher handles the jobs strictly in queue order, job 1 first. For each job it picks the runner that becomes free earliest; if several runners become free at that same second, it picks the one with the smallest number. The job starts on that runner the moment the runner becomes free (so at second 0 if the runner has never been used), and the runner is busy for the next d_i seconds. A runner that finishes a job at second t can start its next job at second t.

For every job, report which runner it was dispatched to and the second at which it started.

Input

The first line contains two integers n and k (1 ≤ n ≤ 2·10^5, 1 ≤ k ≤ 2·10^5): the number of jobs and the number of runners.

The second line contains n integers d_1, d_2, …, d_n (1 ≤ d_i ≤ 10^9): the job durations in seconds, in queue order.

Output

Print n lines. Line i must contain two integers: the number of the runner job i was dispatched to, and the second at which job i started.

Notes

In the first example, jobs 1 and 2 start at second 0 on runners 1 and 2. Runner 2 frees up at second 1 and takes job 3, which ends at second 3. At that point both runners become free at second 3, so job 4 goes to runner 1 and job 5 to runner 2.

In the second example, all three runners free up at second 2, and the last three jobs fill them in order 1, 2, 3.

Start times can exceed the range of a 32-bit integer.

Example 1

Input

5 2
3 1 2 4 1

Output

1 0
2 0
2 1
1 3
2 3

Example 2

Input

6 3
2 2 2 1 1 1

Output

1 0
2 0
3 0
1 2
2 2
3 2

Hints · reveal one at a time if you're stuck

Hint 1

Checking all k runners for every job costs O(nk), which is too slow. Each runner is fully described by the second at which it next becomes free. Which runner should job i take, and what data structure gives you that runner quickly?

Hint 2

Use a min-heap of (free time, runner number) pairs. For each job, pop the smallest, report it, and push the same runner back with its new free time. Times can exceed 32 bits, and ties must go to the smaller runner number.

report an issue Prefer your own editor? sooks prep 12 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.