Survivor (Small)
InterviewTime limit5sMemory limit512 MB
Pick which foods to eat and in what order, respecting each shelf life, to maximize total survival time.
- Level
Medium5 of 10
- Topics
- Backtracking, Dynamic programming
- Solved
- No attempts yet
Problem
You are shipwrecked on a deserted island. You saved one box of food, but the island is bare rock with no plants and you have no way to fish, so no more food arrives.
The box holds foods. For each food you know the remaining shelf life , counted from now, and the time it holds off hunger once you eat it.
The rules for eating are these.
- You eat the first food right now, at minute 0.
- and are both measured in minutes.
- Food past its shelf life is thrown out at once. Food can only be eaten at a time of at most minutes, and a food whose remaining shelf life is 0 must be eaten right now or thrown out.
- You eat nothing while hunger is held off. If you eat food at minute , the next food goes down at exactly minute .
- If hunger arrives and no food is left to eat, you starve on the spot.
Choose which foods to eat and in what order, then report the longest time you survive on the island. If the last food is eaten at minute and its value is , the survival time is minutes.
Input
The first line holds the number of test cases . Each test case follows in this format.
N
P1 S1
P2 S2
...
PN SN
Constraints
- Every input value is an integer.
Output
For each test case , print one line in the form Case #x: y, where is the longest time you survive on the island in that test case.