Under construction

Still being built. Things may change

Longest Life

Time limit2sMemory limit512 MB

Summary
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 xx-yy pill, when taken regularly, ages your body only yy seconds over the course of xx 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 cc seconds. The value of cc is the same for all pills. Switching to a pill from no pill at all also ages you cc seconds.

Any time you switch to an xx-yy 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 t=0t = 0 seconds, and without the aid of any pills, you would live to be nn seconds old.

You are given the time each pill is introduced to the market, tit_i (in seconds), and its xix_i and yiy_i values, along with cc. Determine the longest you can live over all possible schedules of pills.

Input

The first line contains three positive integers nn (n≤3⋅109n \le 3 \cdot 10^9), pp (p≤105p \le 10^5), and cc (c≤105c \le 10^5). Here nn is the number of seconds you would live without taking any pills, pp is the number of pills that become available on the market, and cc is the number of seconds you age as soon as you switch to a different pill. The next pp lines each contain three space-separated integers tit_i, xix_i, and yiy_i (1≤ti≤10121 \le t_i \le 10^{12}, 1≤yi<xi≤1041 \le y_i < x_i \le 10^4), giving the time the iith pill is introduced and its xx and yy values. For all ii, 1≤i≤n−11 \le i \le n-1, it is guaranteed that ti+1−ti>ct_{i+1} - t_i > c.

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 10−610^{-6}.

Examples2

  1. Example 1

    Input
    100 3 10
    15 99 98
    40 3 2
    90 10 9
    
    Expected output
    115.000000000
    
  2. Example 2

    Input
    10000 4 100
    1000 1001 1000
    1994 10 9
    2994 100 89
    3300 1000 1
    
    Expected output
    6633900.000000000