Emergency room: who gets the next doctor?
A busy emergency room in Johannesburg has K identical doctors. Patients arrive at known times and each needs a known treatment time. Whenever a doctor becomes free (or a patient arrives while a doctor is idle), the doctor takes the waiting patient with the highest severity (5 is most severe, 1 is least). If severities tie, the patient who arrived earlier goes first, and if arrival times also tie, the smaller patient id goes first. A patient is only waiting once their arrival time has been reached, and treatment is never interrupted. Times are in minutes.
Task: the first line holds K and n (1 ≤ K ≤ 10, 1 ≤ n ≤ 100). Each of the next n lines holds id arrival duration severity (ids are 1..n, in increasing order of id; arrival times are non-decreasing). Print n lines: for each patient in input order, the minute at which treatment starts. Then print one final line with the largest waiting time (start minus arrival) among all patients.
Sample test
Input
2 5 1 0 10 2 2 1 5 5 3 2 5 1 4 3 3 4 5 4 2 5
Expected output
0 1 10 8 6 8
Your answer is also checked against 5 hidden tests.
Need a hint?
Keep a heap of the times at which doctors become free, and a second heap of waiting patients ordered by the rule in the story.
Log in or create a free account to run your code, submit and earn points.
Run your code to see the output here. Ctrl + Enter also runs it.
