We need to parse problem. We have N jobs. If we take job i and complete it, we earn reward B_i after A_i days from the day we do it. We can do at most one job per day. We cannot redo jobs. We want maximum total reward that we can earn no later than M days from today. We can start today.
Interpretation: We schedule jobs on days (starting from day 0? "today" is day 0? We can start working today. So we can do a job on day 0 (today), then we will get reward after A_i days: if we do job i on day d, we receive reward on day d + A_i. We want total reward for which the completion (i.e., reward being earned) happens no later than M days from today. But careful: The wording "earn no later than M days from today" means that the reward must be earned (i.e., received) by day M inclusive? Usually it means the reward is counted only if it is earned on or before M days after today. So we must schedule jobs such that for each job done, d + A_i <= M. Also we can do at most one job per day. We can choose any subset of jobs and assign them to distinct days (integers d >=0) such that for each job, d + A_i <= M. We want maximize sum of B_i for scheduled jobs.
This is a classic scheduling problem with deadlines: each job i has a deadline D_i = M - A_i (the latest day we can do it such that reward is received by M). Actually if we do job on day d, we need d + A_i <= M => d <= M - A_i. So the latest day we can perform job i is L_i = M - A_i. But also we can do it earlier. There's also the constraint that we can only do one job per day. So we need to select jobs and assign them to distinct days d (nonnegative integers) such that 0 <= d <= L_i. This is essentially the "job scheduling with deadlines and profits" problem (maximizing profit) where each job takes one unit of time (one day) and has a deadline (the last day it can be scheduled). The classic solution: sort jobs by decreasing profit? Actually the classic "Scheduling with deadlines" problem: we have jobs each with deadline and profit, each takes 1 unit time, maximize profit, schedule jobs in time slots. The optimal algorithm is to sort jobs by profit descending, and then assign each job to the latest available slot before its deadline. That yields maximum profit if all deadlines are <= N? But that algorithm works when number of slots is equal to number of jobs and deadlines can be up to N. However, here deadlines L_i can be up to M (which is up to 1e5). But we have at most N jobs, and we can only schedule at most M+1 days? Actually days are from 0 to M (since d <= M - A_i, and A_i >=1, so L_i <= M-1, but we can also schedule on day M? If A_i=0? But A_i>=1 so d <= M-1. So maximum day we can work is M-1? But careful: if A_i = M, then L_i = M - M =0, we can do it on day 0. If A_i > M, then L_i negative, meaning that job cannot be done at all because even doing it today, reward after A_i days would exceed M. So jobs with A_i > M are automatically excluded. So we only consider jobs with A_i <= M. For those, deadline L_i = M - A_i, which is between 0 and M-1. So we have time slots from day 0 to day M-1 inclusive? Actually what about day M? If we do a job on day M, then reward after A_i days would be at least M+1 (since A_i>=1), so that would exceed M. So day M is not allowed for any job. So the available days are 0,1,...,M-1. That's M days (since count from 0 to M-1 inclusive is M slots). But we also have the constraint that we can do at most one job per day. So maximum number of jobs we can schedule is at most M (since M days from 0 to M-1). But M <= 1e5, N <=1e5.
Thus we have a standard problem: we have jobs each with a deadline d_i = M - A_i (latest possible day to schedule), profit = B_i. We can schedule at most one job per unit time. We want to maximize sum of profits of scheduled jobs. The classic algorithm: sort jobs by decreasing profit, then for each job, assign it to the latest free slot <= its deadline. If no free slot, skip. This works when we have time slots 0..max_deadline. But here deadlines could be up to M-1. The greedy works for unit time jobs with deadlines to maximize profit: it's optimal to schedule jobs in order of profit descending and place them as late as possible. This is based on exchange arguments; it holds if profit values are all positive (they are, since B_i>=1). So we can implement.
Alternative approach: use a max-heap and iterate days from M-1 down to 0, but that's for when each job has a "starting time" constraint? Actually there is another known approach: sort jobs by deadline, then use a min-heap on profit to drop smallest profit when we have too many jobs scheduled up to that deadline. But that approach maximizes count of jobs? Actually for maximizing profit, we can also sort by deadline ascending, and maintain a min-heap of profits of selected jobs. For each job, if current selected count < deadline+1 (i.e., we have room to schedule up to that deadline), we add it. Otherwise if its profit > smallest profit in selected, we replace. This yields optimal max profit. However, careful: that method works when each job takes unit time and we have a set of deadlines, and we want to maximize sum of profits of jobs scheduled by their deadlines. It is known as "scheduling with deadlines to maximize profit" and is analogous to the "weighted scheduling" where we sort by deadline and use a min-heap to keep selected jobs' profits, always maintaining feasibility. That's also valid and runs in O(N log N). But the earlier greedy of assigning each job to latest free slot also works but requires a DSU or union-find to find the latest free slot efficiently. Since M up to 1e5, we can maintain an array "next_free" using union-find (disjoint set for slots). That is typical for "Job sequencing with deadlines" problem. But the min-heap approach might be simpler.
Let's double-check: We have time slots 0..T_max. Each job i must be assigned to a slot t such that 0 <= t <= d_i. We want to maximize total profit.
Common greedy: sort jobs by profit descending. Initialize an array/flag for slots. For each job, find the largest t <= d_i that is free, assign if exists. This yields optimal solution.
We can implement using a DSU where parent[t] points to the next free slot less than or equal to t? Actually we can have an array "parent" for each day, initially parent[t] = t. When a slot t is occupied, we set parent[t] = t-1 (or union with previous day). Using a find operation to get the latest free slot. Since we have up to M slots (<=1e5), that's fine.
But note: slots are from 0 to M-1. If we assign a job to day t, we mark it occupied and union t with t-1. When we look for a slot for deadline d, we find root = find(d). If root >=0, then assign and union root with root-1. If root = -1 (or find returns negative), no slot.
We must also ensure we only consider jobs with d_i >=0 (i.e., A_i <= M). So filter those.
That yields O(N log* M) basically linear.
Alternative: using priority queue approach: sort jobs by deadline ascending. Maintain a min-heap of profits of jobs selected. For each job in sorted order, we can always add it because we have capacity equal to deadline+1? Actually careful: If deadlines are integers from 0 to D_max, and we have slots 0..D_max, we can schedule at most D_max+1 jobs? Wait: number of slots available up to a given deadline d: if we consider all jobs with deadline <= d, we can schedule at most d+1 jobs among them (since slots 0..d). So when processing jobs in increasing deadline, we can maintain that we have selected some set of jobs. At each step, we can always try to add the current job. But we need to ensure that the total number of selected jobs does not exceed the current deadline+1. So if after adding the current job, count = deadline+1+1? Actually suppose we have processed up to deadline d (inclusive). The maximum number of jobs that can be scheduled without missing deadlines is at most d+1. So we can maintain the selected jobs so that size <= d+1. For each new job with deadline d, if current size < d+1, we add it. If current size == d+1, then we compare profit with smallest profit in selected; if new profit > smallest, replace. This yields optimal total profit. This works because any subset of jobs with deadlines <= d can be scheduled if and only if its size <= d+1 (by Hall's theorem? Actually it's the classic scheduling: for unit-time jobs with integer deadlines, there exists a feasible schedule if and only if for all t, the number of jobs with deadline <= t is <= t+1. Sorting by deadline and using a priority queue ensures that we keep the most profitable jobs while maintaining feasibility). This method is well-known. So we can implement that.
Let's check: In our case deadlines d_i = M - A_i. Since A_i >=1, d_i <= M-1, so max deadline is M-1. So the maximum possible number of slots is M (since slots 0..M-1). So we can schedule at most M jobs. So this method works: sort by deadline ascending. Use min-heap. For each job (d, profit), if heap size < d+1, push profit. Else if heap top < profit, pop top and push profit. However, careful: d+1 might be > M? Actually d max = M-1 => d+1 = M, which is fine. But we need to ensure that the condition "heap size <= d+1" is correct. Let's test: Suppose we have jobs with deadlines 0,0,0. Then d+1 =1. So we can only schedule at most 1 job with deadline 0. The algorithm: process first job: size 0 < 1 => push. Second job: size 1 == 1, and profit compare: if profit > top, replace else skip. So it keeps the highest profit among the three. That's correct: only one job can be done on day 0. Good.
But what about deadlines not consecutive? The feasibility condition for a set of jobs is that for any integer t, the number of jobs with deadline <= t is <= t+1. When we process jobs sorted by deadline, and maintain the selected jobs as a set that always satisfies that condition for the current deadline d. The min-heap approach ensures that the selected set is feasible and maximizes profit among those considered so far. So it's valid.
We also have to consider that we have days from 0 to d inclusive? Yes.
Thus both methods work.
Let's test examples:
Example 1:
N=3, M=4
Jobs:
4 3 => A=4, B=3 => d = M-A = 0 => deadline 0.
4 1 => A=4, B=1 => d=0.
2 2 => A=2, B=2 => d=2.
So jobs: (d=0, p=3), (0,1), (2,2).
Sort by d: (0,3),(0,1),(2,2).
Process (0,3): heap size 0 < 1 => push 3. heap={3}, total=3.
(0,1): heap size 1 == d+1=1? For d=0, d+1=1, so size ==1. Compare profit 1 > top? top=3, 1<3 so skip.
(2,2): d=2 => d+1=3. heap size=1 <3 => push 2. heap={3,2} (min-heap, top=2). total=5.
Result 5. matches.
Example 2:
N=5, M=3
Jobs:
1 2 => A=1, d=2 (since M-A=3-1=2)
1 3 => d=2, p=3
1 4 => d=2, p=4
2 1 => A=2, d=1 (3-2=1)
2 3 => A=2, d=1, p=3
So jobs: (2,2), (2,3), (2,4), (1,1), (1,3). Sorted by deadline: (1,1), (1,3), (2,2), (2,3), (2,4).
Process (1,1): d=1 => d+1=2, size 0<2 -> push 1. heap={1}, total=1.
(1,3): size=1<2 -> push 3. heap={1,3}, total=4.
(2,2): d=2 => d+1=3, size=2<3 -> push 2. heap={1,2,3}, total=6? Wait 1+3+2=6. But expected output 10. Something off. Let's recalc: we have 5 jobs, expected answer 10. Let's list jobs:
Input:
5 3
1 2
1 3
1 4
2 1
2 3
So A=1,B=2; A=1,B=3; A=1,B=4; A=2,B=1; A=2,B=3.
M=3 => deadlines: M - A => for A=1: 2; A=2: 1.
So jobs:
(d=2, p=2)
(d=2, p=3)
(d=2, p=4)
(d=1, p=1)
(d=1, p=3)
Now, we can schedule up to M=3 days: days 0,1,2 (since M=3 days from today? Actually days 0,1,2? Wait M=3 days from today: by day 3? It says "no later than M days from today". If M=3, then day 3 is the last day you can earn? Let's check: If you do a job on day d, you earn reward after A_i days: so you earn on day d+A_i. This must be <= M. So d + A_i <= M. If M=3, then possible days: For A=1: d <=2; for A=2: d <=1; for A=3: d<=0; etc. So slots available: day 0,1,2 (three days). So we can schedule at most 3 jobs. We need to pick the most profitable three jobs? But total profit of all jobs sum = 2+3+4+1+3 = 13. But we can only schedule 3 jobs. To get total 10, we need jobs with profits 4,3,3? That's 10. Indeed we can schedule the three highest profits: 4,3,3 (two 3s and a 4) but note that the three highest are 4,3,3 (the 3s are from A=1 and A=2). But can we schedule them? Let's see: we have jobs: (A=1,B=4) deadline 2; (A=1,B=3) deadline 2; (A=2,B=3) deadline 1; we need to assign distinct days <= deadlines. For (A=2,B=3) deadline 1, must be done on day 0 or 1. For the two A=1 jobs, deadlines 2, can be done on days 0,1,2. We can schedule three jobs: assign (A=2,B=3) on day 1, (A=1,B=4) on day 0, (A=1,B=3) on day 2. That works. So total 10.
Now, my min-heap approach gave total 6? Something wrong with the algorithm or the processing order. Let's recalc step by step.
Sort by deadline ascending: we need to be careful: when sorting by deadline, we also need to consider the actual scheduling feasibility condition. The min-heap method as I described works for maximizing profit when each job takes unit time and deadlines are integers and profits positive, and we want to select a set of jobs that can be scheduled. The typical algorithm: Sort jobs by deadline (ascending). Initialize an empty min-heap (by profit). For each job in sorted order:
- Push job's profit onto heap.
- If heap size > current job's deadline + 1 (since deadlines are 1-indexed? Actually need to be careful: If deadlines are given as "deadline" meaning the latest time slot by which the job must be completed (e.g., time slots 1..T), then the condition is heap size > deadline. In our case, deadlines are 0-indexed (day). Let's derive.
Classic problem: "Given n jobs, each with a deadline d_i (integer) and profit p_i, each job takes 1 unit of time, time starts at 0 (or 1). Maximize profit." The algorithm: sort jobs by profit descending, assign to latest free slot before deadline. Or alternative: sort by deadline ascending, maintain min-heap of profits of selected jobs. When using min-heap, typical representation: deadlines are considered as the latest time unit (e.g., if deadline is 3, you can schedule at time 3,2,1,0? Actually if time slots are 0,1,..., then a job with deadline d can be scheduled at any slot t <= d. Then the maximum number of jobs that can be scheduled with deadlines <= d is d+1. So if we are processing jobs in increasing deadline, we can keep at most d+1 jobs selected so far. So when we encounter a job with deadline d, we push it, then if heap size > d+1, pop the smallest profit. That is the correct procedure.
Let's test with example. Sort by deadline: we have deadlines 1 and 2. Which order? We must sort by deadline ascending. For jobs with same deadline, order doesn't matter. So list: first deadline 1 jobs: (d=1,p=1), (d=1,p=3). Then deadline 2 jobs: (d=2,p=2), (d=2,p=3), (d=2,p=4). Let's simulate:
Initialize heap = [] (min-heap)
total = 0? But we will accumulate sum separately or after.
We'll keep sum as sum of heap.
Process (1,1):
push 1 => heap=[1], size=1. Check if size > d+1 = 2? size=1 <=2, so keep. Sum=1.
Process (1,3):
push 3 => heap=[1,3], size=2. Check size > 2? size=2 <=2, ok. Sum=4.
Process (2,2):
push 2 => heap=[1,2,3] (min=1), size=3. Check size > d+1 = 3? size=3 > 3? Actually 3 is not greater than 3, it's equal. So condition is if size > d+1, then pop. So size=3, d+1=3 => not >, so keep. Sum=6.
Process (2,3):
push 3 => heap=[1,2,3,3] size=4. Check size > d+1 =3? 4>3 => pop smallest. Pop 1 => heap becomes [2,3,3] size=3. Sum changes from 6+3=9 then subtract 1 => 8? Actually need careful: sum after push: previous sum=6, add 3 =>9. After pop, subtract popped value 1 => sum=8. Now heap contains profits 2,3,3 sum=8.
Process (2,4):
push 4 => heap=[2,3,3,4] size=4. Check size >3 => pop smallest 2 => heap [3,3,4] size=3, sum: previous 8+4=12 then subtract 2 =>10. Final sum=10. That matches expected output.
So the algorithm must process all jobs, not stop early; and must apply the check after each push. My earlier simulation only processed up to (2,2) and stopped, but I should have continued. So the min-heap approach works if we process all jobs.
Thus we can implement either method.
Time complexity O(N log N) for sorting and O(N log N) for heap operations.
Now need to consider M up to 1e5, but deadlines can be as large as M-1 (max 99999). Heap size will be at most M, so fine.
Edge Cases: jobs with A_i > M are automatically invalid, we can ignore them because they cannot be scheduled (deadline negative). But in the min-heap method, we would have d_i negative; we should skip those jobs. Alternatively, if we filter only those with A_i <= M.
Also, note that we can also schedule jobs on days possibly beyond M? No, because reward must be earned no later than M days from today, so d + A_i <= M. So we cannot schedule beyond M-1. So days 0..M-1 inclusive. So maximum number of jobs = M. So if N > M, we can only do at most M jobs.
Now, we must be careful with the index: d_i = M - A_i. Since A_i >=1, d_i can be 0 to M-1. For d_i = 0, we can only schedule on day 0. So condition size <= d_i+1 works (size <=1). Good.
Thus solution:
- Read N, M.
- Initialize list jobs = [].
- For i in 1..N:
read A, B.
if A <= M: // else skip
deadline = M - A
append (deadline, B) to jobs.
- Sort jobs by deadline ascending (if tie, any order).
- Initialize min-heap (like a priority queue that gives smallest).
- total = 0 (or we can maintain sum of heap).
- For each (d, profit) in jobs:
push profit onto heap.
total += profit // but we'll adjust after potential pop.
If size of heap > d+1:
popped = pop smallest from heap.
total -= popped.
- After loop, total is the answer.
But careful: total should be sum of profits in heap after all operations. So we can maintain total variable like described.
Alternatively, we can simply at the end sum all elements in heap, but maintaining sum is efficient.
Now double-check with example 1: jobs filtered: all A<=4? M=4, A_i=4,4,2 => all <=4. deadlines: 0,0,2.
Sort by deadline: (0,3), (0,1), (2,2).
Process:
(0,3): push 3, size=1, d+1=1 => size<=1, total=3.
(0,1): push 1, size=2, d+1=1 => size>1, so pop smallest (1). total after push: 3+1=4, pop 1 => total=3, heap now [3].
(2,2): push 2, size=2, d+1=3 => size<=3, total=5.
Final total=5. Good.
Example 3:
1 1
2 1
A=2 > M=1 => skip => jobs empty => answer 0. correct.
Now check some edge cases:
- M=0? But constraint says M >= 1. So okay.
- A_i could be 0? No, A_i >=1.
- B_i up to 1e4.
Now, is there any nuance about "you can take and complete at most one of these jobs in a day." It means we can do at most one job per day. But we can also have idle days. That's already accounted.
Also "You can already start working today." means day 0 is allowed.
Thus solution stands.
Now complexity O(N log N). N up to 1e5, fine.
We should implement using Python's heapq as a min-heap. Since we need min-heap, we can push profits and pop smallest when size > deadline+1.
Pseudo:
import sys, heapq
def solve():
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
jobs = []
for _ in range(N):
A, B = map(int, input().split())
if A <= M:
d = M - A
jobs.append((d, B))
jobs.sort(key=lambda x: x[0]) # sort by deadline
heap = []
total = 0
for d, profit in jobs:
heapq.heappush(heap, profit)
total += profit
if len(heap) > d + 1:
smallest = heapq.heappop(heap)
total -= smallest
print(total)
Edge: if d+1 could be 0? For d negative (if we mistakenly include A > M, but we filtered) not happen. But if M=0? Not possible.
Now check if the condition len(heap) > d+1 is correct when d is less than the number of already selected jobs. For example, if d=0, then d+1=1. So after processing jobs with deadline 0, heap size should be at most 1. If we have multiple jobs with deadline 0, the algorithm will push each, then pop smallest to keep size=1, thus keeping the highest profit among them. That matches optimal.
What about jobs with deadline 1? d+1=2. So we can keep at most 2 jobs with deadlines <=1 (since days 0 and 1). That's correct because we can schedule at most 2 jobs if they both have deadlines 1 or less.
Thus it's correct.
Now, consider if there are jobs with deadline 2 but we already have 3 jobs with deadline 2? Actually we can schedule up to 3 jobs (days 0,1,2). So after processing a job with deadline 2, heap size can be up to 3. But when we later process a job with deadline 2, we push then if size > 3, we pop smallest. That ensures we keep the most profitable set among all jobs with deadlines <=2, but also might discard a job with earlier deadline? However, that's fine because we maintain feasibility: we never exceed the capacity for the largest deadline processed so far. And since we process in increasing deadlines, any subset that respects the capacity for the current deadline also respects for all earlier deadlines because capacity increases with deadline. Actually we need to verify: When we process jobs with deadline 2, we allow up to 3 jobs. Some of those jobs may have deadlines 0 or 1. But if we have more than 2 jobs with deadlines <=1, that would violate the capacity for deadline 1, but our algorithm ensures that when we processed deadline 1, we kept at most 2 jobs among those with deadlines <=1. Then when we go to deadline 2, we add more jobs but we still maintain total count <=3. It's possible that the set after adding a job with deadline 2 might contain 3 jobs, but those 3 jobs could include, say, 2 jobs with deadlines 0 and 1, and 1 job with deadline 2, which is feasible. Could it contain 3 jobs all with deadline 1? That would be 3 jobs with deadline <=1, but capacity for deadline 1 is 2. However, is that possible given our algorithm? When we processed the last job with deadline 1, we kept heap size <=2. After that, when we process a job with deadline 2, we push and possibly pop the smallest, but we never add beyond the capacity for deadline 2, but we might increase the count beyond 2 even if the existing jobs all have deadline <=1? For example, after processing deadline 1 jobs, heap contains two jobs with deadline 1 (both profits high). Now we process a deadline 2 job: we push it, heap size becomes 3. Now we check if size > d+1 =3? No, size=3 is equal, so we don't pop. So heap now has 3 jobs: two with deadline 1, one with deadline 2. Is that feasible? Capacity for deadline 1 is 2, so if we have two jobs with deadline 1, they need to be scheduled on days 0 and 1. The third job with deadline 2 can be scheduled on day 2. That's feasible. So okay. Could we ever have 3 jobs all with deadline 1? That would require that after processing deadline 1 jobs, heap already had 3 jobs with deadline 1. But at the time we finished processing all deadline 1 jobs, heap size was forced to be <=2 because when we processed each job with deadline 1, we enforced size <= 2. So impossible. So algorithm is safe.
Thus final answer is to print total.
Now check edge cases:
- N=0? Not possible (N>=1).
- M very large 100k, many jobs. Fine.
Now, what about duplicate profits? Works.
Now, we must consider the possibility that deadlines could be up to M-1, but M can be 100k, so d+1 up to 100k, heap size up to 100k, okay.
Now, we must ensure we skip jobs with A > M. But what about A=0? Not allowed.
Now, test with more custom cases.
Case: N=3, M=2. Jobs: (A=1,B=5), (A=2,B=3), (A=2,B=4). deadlines: (1,5), (0,3), (0,4). Sort by deadline: (0,3), (0,4), (1,5). Process:
(0,3): push, size=1 <=1, total=3.
(0,4): push, size=2 >1? size=2 >1 => pop smallest (3), total: 3+4=7-3=4, heap=[4] size=1.
(1,5): push 5, size=2 <=2? d+1=2, so ok, total=9. Answer=9. Let's check feasible schedule: We have jobs with deadlines: (A=2,B=4) deadline 0; (A=2,B=3) deadline 0; (A=1,B=5) deadline 1. We need to schedule up to 2 days (0 and1). We can only do 2 jobs max? Actually M=2, days available: 0 and 1 (since d+Ai<=2). So we can do at most 2 jobs. To maximize profit, choose jobs with profits 5 and 4. Check feasibility: job with profit 5 (A=1, deadline 1) can be done on day 1 (since d+1<=2 => day1+1=2 OK) or day0? day0+1=1 <=2 OK but then we would need day0 for the other job. Job with profit 4 (A=2, deadline 0) must be done on day 0. Then day1 left for profit5. So total 9. Works.
Case: Many jobs with high A_i making deadlines small. Works.
Now, could there be jobs with negative deadline after filter? we skip.
Thus solution seems correct.
Now, also consider that the jobs' profits are up to 1e4, sum could be up to N1e4=1e9, fits in 32-bit int? 1e51e4=1e9, fits in 32-bit signed? 2^31-1=2,147,483,647, so yes. Use Python int anyway.
Now, we should also note that the alternative DSU method may be necessary if M is large? But M is only 1e5, heap is fine.
Potential pitfall: sorting by deadline ascending; but we must also consider that for each job, deadline is M - A_i. So if A_i is small, deadline is large. So we sort increasing deadline, i.e., jobs with smaller latest start day come first. That's fine.
One more nuance: Are there any constraints about "you can take and complete at most one of these jobs in a day"? That suggests that if we take a job, we complete it on the same day? Probably yes, we take and complete it in that day. So it's a one-day job. So our modeling is correct.
Now, before finalizing, let's think if there is any subtlety about the reward being earned after A_i days from the day you do it. So if you do job on day d, you get reward on day d+A_i, and that day must be <= M. That's what we used.
Thus answer is sum of selected jobs profits.
Now, let's produce final answer.
Implementation notes:
- Use sys.stdin.read or input() for fast I/O.
- Use heapq.
Time complexity O(N log N).
Now, I'm ChatGPT, need to output final answer with explanation? The user asks for code? The problem statement says "Print the maximum total reward..." This is typical coding problem. The user likely expects code. Since this is a solution assistant, I should provide a solution in Python. But the user is ChatGPT; we are producing the answer for the user? The user provided a problem and expects solution. In this context, we should output the final answer: likely a code snippet with solve() function.
But the user asked: "There are N one-off jobs available... Input ... Output ..." It seems they've pasted a problem description. They likely expect the solution code. But it's ambiguous: The conversation: "There are N one-off jobs available... Input... Output... Examples...", then blank? Actually they gave the problem statement. So they want us to solve it. As ChatGPT, we can provide a solution explanation and the code.
Given the instructions: "You are an AI programming assistant... Provide answers in Python code blocks." So we should output the Python code.
But also, we might need to provide explanation? The instruction says "Your task is to provide a complete solution. Provide the final answer... within code block." Possibly just code.
But we can give a brief explanation and then code.
Given typical approach, I'll write a detailed solution with comments.
Thus final answer:
import sys
import heapq
def solve():
input = sys.stdin.readline
N, M = map(int, input().split())
jobs = []
for _ in range(N):
A, B = map(int, input().split())
if A <= M:
d = M - A
jobs.append((d, B))
# sort by deadline ascending
jobs.sort(key=lambda x: x[0])
heap = [] # min-heap of profits of selected jobs
total = 0
for d, profit in jobs:
heapq.heappush(heap, profit)
total += profit
# if we have more jobs than available slots up to day d,
# remove the job with smallest profit
if len(heap) > d + 1:
removed = heapq.heappop(heap)
total -= removed
print(total)
if __name__ == "__main__":
solve()
Tested on examples.
Now, we should also consider the possibility that there are many jobs with same deadline and many that are filtered out; code handles.
Potential improvement: Since deadlines range up to M-1, we could also use a list of size M to store jobs per deadline, but not necessary.
Thus final answer.
But wait, check Example 2 using the code: Should output 10. Yes.
One more test: N=3 M=4 (example1) output 5. Good.
Edge: M=1, job A=1 B=5 => d=0, push, heap size 1 <=1, total=5, answer 5. Good.
M=1, job A=2 B=5 => skip, answer 0. Good.
Now, think about if M is large and many jobs have A_i <= M, we might push many jobs into heap, but heap size limited to M, which is up to 1e5, so fine.
Now, finalize.
But there is a nuance: The min-heap approach as described relies on the fact that we process jobs in increasing deadline, and when we encounter a job, we always push it, then if heap size > current deadline+1, we pop the smallest. This ensures that we keep the best profits. However, there's a subtlety: When we process jobs with same deadline, we push them one by one, and after each push we might pop. This results in keeping the highest profits among those seen so far, but does it guarantee that the final set is feasible? For a set of jobs with deadlines <= d, the condition that number of jobs <= d+1 is necessary and sufficient (since we can schedule using the "earliest deadline first" or "reverse scheduling" algorithm). The min-heap algorithm is a standard greedy for "Maximum total weight of jobs scheduled with deadlines". It is proven optimal. So it's fine.
Thus final answer is ready.
We should also note that we could use DSU method, but this is simpler.
Now, produce final answer with code.
But the user might also ask to print the maximum total reward, so code prints total.
Thus done.
I will provide final answer. ```python
import sys
import heapq
def solve() -> None:
input = sys.stdin.readline
N, M = map(int, input().split())
jobs = []
for _ in range(N):
A, B = map(int, input().split())
if A <= M: # jobs that can possibly be finished on time
deadline = M - A # latest day we can do this job
jobs.append((deadline, B))
# Process jobs in increasing order of deadline
jobs.sort(key=lambda x: x[0])
heap = [] # min-heap of profits of currently selected jobs
total = 0 # sum of profits in the heap
for d, profit in jobs:
heapq.heappush(heap, profit)
total += profit
# Keep at most (d+1) jobs (days 0..d)
if len(heap) > d + 1:
removed = heapq.heappop(heap)
total -= removed
print(total)
if name == "main":
solve()