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 N foods. Each food i is labeled with its remaining shelf life Pi and the satiation time Si 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 t you may eat only a food with Pi≥t. A food whose remaining shelf life is 0 must be eaten right now or thrown away.
- If you eat food i at minute t, you eat nothing else until minute t+Si.
- The moment minute t+Si arrives, you starve to death immediately unless you eat another food.
Find the longest time you can survive on the island.