Survivor (Large)
Time limit10sMemory limit512 MB
Eat foods in expiry order so each meal starts before it spoils and the next meal follows after its satiation time to maximize total survival time.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, Sorting
- Solved
- No attempts yet
Problem
You are stranded on a deserted island. You managed to grab one box of food, but the island is bare rock with no plants and no way to fish, so no more food is coming.
The box holds foods. Each food is labeled with its remaining shelf life and the satiation time it keeps hunger away once you eat it. Both values are measured in minutes.
The rules for eating are the following.
- You start eating now, at minute 0.
- Food past its shelf life is discarded at once. At minute you may eat only a food with . A food whose remaining shelf life is 0 must be eaten right now or thrown away.
- If you eat food at minute , you eat nothing else until minute .
- The moment minute arrives, you starve to death immediately unless you eat another food.
Find the longest time you can survive on the island.
Input
The first line contains the number of test cases . Each test case has the following form.
The first line contains the number of foods . Each of the next lines contains the remaining shelf life and the satiation time of one food, separated by a space.
Constraints
- Every input value is an integer.
Output
For each test case , print the longest survival time on one line in the form Case #x: y.