897. The Most Profit Before the Deadlines
n jobs are offered. Job i takes exactly ONE day, pays p on completion,
and must be finished on or before day d. Days are numbered 1, 2, 3, ... and
only one job can run per day. Jobs may be skipped.
Print the largest total profit achievable.
(deadline 2, pay 100) (1, 19) (2, 27) (1, 25) -> 127
(1, 10) (1, 20) (1, 30) -> 30
(4, 1) (4, 2) (4, 3) (4, 4) -> 10
In the first example every deadline is 1 or 2, so only days 1 and 2 are ever usable and at most two jobs can run. Day 1 runs the job paying 27 and day 2 runs the job paying 100, for 127. Both jobs with deadline 1 are dropped, even the one paying 25, because the pair 100 and 27 beats every alternative pair.
In the third example all four deadlines are 4, so all four fit in days 1 to 4.
## What most people try first
Taking the most profitable jobs first, in profit order, and checking each one fits. This is almost right and the check is the hard part: "fits" means there is still a free day at or before its deadline, and answering that repeatedly needs a disjoint-set structure or a scan. It is a real algorithm, but it is fiddly and it is not what this problem is for.
Sorting by deadline and taking greedily. Wrong: on the first example that takes 19 and 100, for 119.
Trying every subset. 2^n with n = 200000.
## Sort by deadline, then UNDO
Process the jobs in increasing deadline order, provisionally accepting every one of them, and keep the accepted profits in a MIN-heap.
sort jobs by deadline ascending
h = empty min-heap of profits
total = 0
for each job (d, p):
push p; total = total + p
if size(h) > d:
total = total - pop(h)
print total
The invariant is that after processing every job with deadline at most d,
the heap holds a maximum-profit feasible set drawn from exactly those jobs.
## Why the invariant holds
Feasibility. A set of jobs all having deadlines at most d is schedulable
exactly when, for every t, at most t of them have deadline at most t.
Processing in deadline order and capping the size at d enforces that
condition as it goes: at the moment the last job with deadline d has been
seen, the heap holds at most d jobs, all with deadline at most d, and by
induction the same held for every earlier deadline. Such a set can always be
scheduled by running its jobs in deadline order.
Optimality. When the heap overflows, exactly one job must be dropped, and dropping the least profitable one is optimal because every accepted job occupies exactly the same resource: one day. There is no reason to prefer one over another except its pay, which is what makes the min-heap the right discard rule. That equal-resource property is what breaks in problem 898, where jobs have different durations.
## Why the heap must be a MIN-heap
It holds the ACCEPTED jobs and its job is to name the one to throw away, which is the cheapest. This is the same direction argument as problem 880: a heap that reports the best survivor is useless, because the best survivor is exactly what you keep.
## The boundary that breaks a careless implementation
Deadlines far larger than `n`. A deadline may be 10^9 while there are
only 200000 jobs, so the size cap size(h) > d never fires for such a job.
Allocating an array of d days, or capping d at some small constant, both
break. The heap never needs more than n entries whatever the deadlines are.
A deadline of 1 with several jobs. Only one of them can ever run, and the cap fires immediately on the second, which is the second example.
## Overflow, with the actual magnitude
Two hundred thousand jobs paying 10^9 each total 2 * 10^14. The signed
32-bit ceiling is 2147483647, and even the three-job test paying 10^9
apiece reaches 3 * 10^9, which wraps to -1294967296.
The individual pay figures and the deadlines both fit in 32 bits. The running total does not.
## Related problems
Problem 898 is the same shape with DURATIONS instead of unit time, and there the heap is a max-heap because the resource being contended is time rather than a slot. Problem 468 also schedules paid jobs, but with real intervals that can overlap, and that needs a dynamic program rather than a greedy.
## Cost
Time O(n log n). Space O(n).
Constraints - `1 ≤ n ≤ 200000` - `1 ≤ d ≤ 1000000000` - `1 ≤ p ≤ 1000000000`
Input
The first line contains one integer n.
Each of the next n lines contains two integers d and p, the deadline day and the pay of one job.
Output
Print one integer alone on a line: the largest total profit achievable by scheduling a subset of the jobs one per day, each on or before its deadline. Print no other words.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.