Loan Scheduling
Time limit1sMemory limit128 MB
Given jobs with deadlines and profits and a per-slot capacity, select a maximum-profit subset schedulable within slot capacity limits by each deadline.
- Level
Medium6 of 10
- Topics
- Greedy, Sorting, Union-find
- Solved
- No attempts yet
Problem
A bank must decide which mortgage applications to accept. There is a set of applications, and each application has an acceptance deadline : if it is accepted, the requested loan must be paid at some integer time with . Accepting application gives the bank a profit .
Time is measured in integral units starting from time , the moment the bank decides on all applications at once. At any given time the bank can pay at most loans. The bank cares only about profit: subject to being able to assign every accepted loan to a time slot no later than its deadline (each time unit holds at most payments), it chooses a subset that maximizes . Compute the maximum profit the bank can obtain. Write a program that reads sets of data from an input text file.
For example, consider and four applications , , , . The table below shows all possible sets of accepted applications and the scheduling of the loan payments. The highest profit is and corresponds to the set : the loan for is paid at time , the loan for at time , and the loan for at time .
Input
The input contains several data sets, one after another, and ends at end of file. Each data set begins with two integers (, the number of applications) and (, the maximum number of loans the bank can pay at any given time). Then follow pairs of integers (, ) giving the profit and the deadline of application . The input data are separated by white space, are correct, and terminate with an end of file.
Output
For each data set, print on standard output, on its own line and starting at the beginning of the line, the maximum profit the bank can obtain from the accepted applications. There must be no empty lines in the output.