885. When the Last Job Finishes
A workshop has m identical machines, all idle at time 0, and n jobs that
must be started in the GIVEN ORDER. Each job takes a known number of minutes
and runs without interruption.
When a job's turn comes it is handed to whichever machine becomes free EARLIEST; if several are free at the same moment, the one with the smallest index takes it. Print the minute at which the last job finishes.
n = 5, m = 2, durations 3 1 4 1 5 -> 9
n = 4, m = 1, durations 1 2 3 4 -> 10
n = 3, m = 5, durations 4 4 4 -> 4
Trace the first one. Machine 0 takes the 3 (free at 3) and machine 1 takes the 1 (free at 1). The 4 goes to machine 1, the earliest free, which is now busy until 5. The next 1 goes to machine 0, free at 3, which becomes free at 4. The 5 goes to machine 0, free at 4, finishing at 9.
The third row has more machines than jobs, so every job starts at time 0 and the answer is simply the longest job.
## What most people try first
Assigning jobs round-robin. Machine i mod m takes job i. That is a
different rule and gives a different answer: on the first example it would put
3 and 4 and 5 on machine 0, finishing at 12.
Scanning all `m` machines for the earliest free time. Correct, and
O(m) per job, so O(nm) overall. With n = m = 200000 that is
4 * 10^10 comparisons.
Sorting the durations first. Not allowed. The jobs must be started in the
given order, and reordering them changes the answer: the same five durations
sorted as 1 1 3 4 5 on two machines finish at 8, not 9.
## The method
The question asked of the machines is always the same: which one is free soonest. That is a minimum, and the set of free times changes after every assignment, so a min-heap is the structure.
h = min-heap of (free time, machine index), seeded with (0, i) for every i
last = 0
for each duration d in the given order:
(f, i) = pop
f = f + d
last = max(last, f)
push (f, i)
print last
The heap always holds exactly m entries, so each job costs O(log m).
## Why the pair, and why the machine index is in it
The machine index does nothing for correctness of the final ANSWER, because the finish time only depends on the multiset of free times. It is in the pair for the tie rule: without it, two implementations could hand equal-free machines out in different orders, and any variant of this problem that reports WHICH machine ran what would become non-deterministic. Carrying the index is cheap insurance and it makes the comparator explicit.
## Why the answer is the running maximum and not the last pop
The last job to be STARTED is not necessarily the last to FINISH. In the first
example the final assignment finishes at 9 and it happens to be the maximum,
but with durations 5 1 on two machines the last job starts at 0 and finishes
at 1, while the answer is 5. Track the largest finish time seen, or, if you
prefer, drain the heap at the end and take its maximum.
## Overflow, with the actual magnitude
Two hundred thousand jobs of 10^9 minutes on one machine finish at
2 * 10^14. The signed 32-bit ceiling is 2147483647, so free times must
be 64-bit. The five-job test with m = 2 reaches 3 * 10^9 on its own,
which wraps to -1294967296, and a 32-bit build reports 2000000000 for it.
Durations themselves fit in 32 bits. The accumulated free time does not.
## Related problems
Problem 884 has an unbounded number of rooms and asks which one each meeting gets. Here the count is fixed and the question is when everything ends. Problem 899 puts arrival times on the jobs and lets the machine choose which job to run next, which is where the heap starts holding jobs rather than machines.
## Cost
Time O(m + n log m). Space O(m).
Constraints - `1 ≤ n ≤ 200000` - `1 ≤ m ≤ 200000` - `1 ≤ d ≤ 1000000000`
Input
The first line contains two integers n and m.
The second line contains n integers, the job durations in the order they must be started.
Output
Print one integer alone on a line: the minute at which the last job finishes, counting from 0 when every machine is idle. Print no other words.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.