Help-or-else
Time limit1sMemory limit128 MB
Choose an ordered subset of people to help; the finish time of each helper accumulates, and unhelped people add a penalty, so find the largest feasible subset under budget K.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, Sorting, Array
- Solved
- No attempts yet
Problem
A correctional facility is about to hold its yearly community-service event under some strict rules. Each participant is assigned a set of people whose finances they may help with, together with a time budget of minutes.
For the -th person () two integers are known: a penalty that is charged if the participant chooses not to advise that person, and a duration (in minutes) required to advise that person.
The participant starts at time . If they begin working with the -th person at time , they must finish no later than ; a value is charged, and they may not start working with anyone else before time (people are helped one at a time, back to back).
Let be the set of people who are actually helped. The total number of minutes used is
Write a program that computes the maximum number of people a participant can help without the total used minutes exceeding the limit .
Input
The input contains the data for several participants. Each participant's description begins with a line holding two integers and , separated by a single space: the number of people and the time budget, with and . Each of the following lines contains two integers separated by a single space — the penalty and the duration of one person to be assisted — with every integer between and inclusive. The input terminates with a line containing two zeros.
Output
For each participant, print a single line in the form i: X, where i is the participant's index counted from in the order the participants appear, and X is the maximum number of people that can be helped without exceeding the -minute limit. If it is impossible to keep the total used minutes within , print i: Mission Impossible instead.