Loan Scheduling

Time limit1sMemory limit128 MB

Summary
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 AppApp of applications, and each application a∈Appa \in App has an acceptance deadline dad_a: if it is accepted, the requested loan must be paid at some integer time tat_a with 0≤ta≤da0 \le t_a \le d_a. Accepting application aa gives the bank a profit pap_a.

Time is measured in integral units starting from time 00, the moment the bank decides on all applications at once. At any given time the bank can pay at most LL 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 LL payments), it chooses a subset S⊆AppS \subseteq App that maximizes profit(S)=∑a∈Spa\text{profit}(S) = \sum_{a \in S} p_a. Compute the maximum profit the bank can obtain. Write a program that reads sets of data from an input text file.

For example, consider L=1L = 1 and four applications (pa,da)=(4,2)(p_a, d_a) = (4, 2), (pb,db)=(1,0)(p_b, d_b) = (1, 0), (pc,dc)=(2,0)(p_c, d_c) = (2, 0), (pd,dd)=(3,1)(p_d, d_d) = (3, 1). The table below shows all possible sets of accepted applications and the scheduling of the loan payments. The highest profit is 99 and corresponds to the set {a,c,d}\{a, c, d\}: the loan for cc is paid at time 00, the loan for dd at time 11, and the loan for aa at time 22.

Time
0abcdbcbbccddabc
1adddaaadddd
2aaaaaaa
Profit4441233455566777789

Input

The input contains several data sets, one after another, and ends at end of file. Each data set begins with two integers NN (0≤N≤100000 \le N \le 10000, the number of applications) and LL (0≤L≤1000 \le L \le 100, the maximum number of loans the bank can pay at any given time). Then follow NN pairs of integers pi dip_i\ d_i (0≤pi≤100000 \le p_i \le 10000, 0≤di≤100000 \le d_i \le 10000) giving the profit and the deadline of application ii. 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.

Examples1

  1. Example 1

    Input
    4 1     4 2  1 0   2 0   3 1
    7 2
    200 1   200 1   100 0  1000 2   80 1
    50 20   500 1
    0 100
    1 0     4 1000
    
    Expected output
    9
    2050
    0
    0