Longest Life
Time limit2sMemory limit512 MB
Choose when to switch between anti-aging pills, each slowing aging to y/x of real time after its release, with a cost c per switch. Find the maximum lifespan.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, Sorting
- Solved
- No attempts yet
Problem
Everyone wants to live as long a life as possible. As time progresses, technology progresses. Various anti-aging pills get introduced to the market at various times, which allow a person to age more slowly than normal. In particular, an - pill, when taken regularly, ages your body only seconds over the course of seconds. So, if you took a 100-87 pill regularly, then over the next 100 seconds, your body would only age 87 seconds. You can only take one of these pills at a time (or none at all). The only downside to using one of these pills is that, due to the change in regimen, if you switch to a pill, it automatically ages you seconds. The value of is the same for all pills. Switching to a pill from no pill at all also ages you seconds.
Any time you switch to an - pill, you can take it for any number of seconds, but you can only switch to a pill at or after the time it first becomes available on the market. Your life starts at time seconds, and without the aid of any pills, you would live to be seconds old.
You are given the time each pill is introduced to the market, (in seconds), and its and values, along with . Determine the longest you can live over all possible schedules of pills.
Input
The first line contains three positive integers (), (), and (). Here is the number of seconds you would live without taking any pills, is the number of pills that become available on the market, and is the number of seconds you age as soon as you switch to a different pill. The next lines each contain three space-separated integers , , and (, ), giving the time the th pill is introduced and its and values. For all , , it is guaranteed that .
Output
Output a single real number, the maximum number of seconds you could live if you take the appropriate pills. Your answer must be correct within a relative or absolute error of .