893. Finishing Everything Soonest on One Machine
n jobs are all available at time 0 and one machine runs them one at a time,
in any order you choose, each without interruption. A job's COMPLETION TIME is
the moment it finishes.
Print the smallest possible total of all n completion times.
durations 5 1 4 2 -> 23
durations 3 3 3 -> 18
durations 1 2 3 4 5 -> 35
Check the first one. Running them as 1, 2, 4, 5 gives completion times
1, 3, 7, 12, totalling 23. Running them in the given order 5, 1, 4, 2
gives 5, 6, 10, 12, totalling 33. Both finish the last job at 12; what
changes is everything before it.
## This problem is here to show where a heap is the WRONG tool
It looks like a scheduling problem, and scheduling problems in this topic use heaps, so a heap is the natural reach: keep the durations in a min-heap, pop the shortest, run it, repeat. That produces the right ORDER and the right answer, and it is still the wrong tool.
The reason is the same one as in problem 879, in different clothes. A heap
earns its keep when the collection changes between queries. Here nothing is
ever inserted. Every job is known before the machine starts and no job arrives
later, so popping a min-heap n times is an elaborate way of walking a sorted
array, at the same O(n log n) cost, with a worse constant and more code.
Worse, the heap hides the structure. Once the durations are sorted, the answer has a closed form and the reason it is optimal becomes visible, and neither of those is apparent from the pop loop.
## Shortest first, and why
Sort ascending and run in that order. The exchange argument is the cleanest in this whole block.
Take any schedule in which some job x runs immediately before a strictly
shorter job y. Swap them. Every job before the pair is untouched, and every
job after the pair still finishes at the same moment, because the pair
together occupies the same span of time. Inside the pair, the first completion
time changes from T + x to T + y, which is smaller, and the second stays
T + x + y. So the total strictly falls.
Any schedule that is not sorted ascending contains such an adjacent pair, so it can be improved. Therefore an optimal schedule is sorted ascending.
## The closed form
With durations sorted as d[0] <= d[1] <= ... <= d[n-1], the job at position
i is still on the machine during its own run and during every run after it,
so d[i] is counted once for each of the n - i jobs from i onwards:
total = sum over i of (n - i) * d[i]
Equivalently, keep a running clock and add it to the total after each job. Both are one pass and both are easier to reason about than a heap loop.
## Overflow, with the actual magnitude
This is the trap the closed form makes obvious. With n = 100000 jobs of
10^6 each, the total is 10^6 * n (n + 1) / 2, which is about
5 * 10^15. Even 70 jobs of 10^6 reach 2485000000, which is past the
signed 32-bit ceiling of 2147483647 and wraps to -1809967296, and that
70-job case is in the tests.
The durations fit in 32 bits with room to spare. The total does not, and it is the only variable that needs widening.
## The boundary that breaks a careless implementation
Adding the clock to the total BEFORE advancing it, which counts a job's start
time instead of its finish time and produces an answer that is short by the
total of all durations. On 3 3 3 that is 9 instead of 18.
## Where the heap becomes necessary
Add ARRIVAL times to the jobs and the whole argument changes: the machine can no longer see all the jobs at the start, so the set of candidates grows over time and a sorted array cannot absorb the newcomers. That is problem 899, and setting it against this one is the point of having both.
## Cost
Time O(n log n), all of it the sort. Space O(n).
Constraints - `1 ≤ n ≤ 100000` - `1 ≤ d ≤ 1000000`
Input
The first line contains one integer n.
The second line contains n integers, the job durations.
Output
Print one integer alone on a line: the smallest achievable total of the n completion times. Print no other words.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.