Cycling#4024

Cycling

1 second
64 megabytes
Very hard

On a bicycle trip through the city, a significant amount of time is spent waiting for traffic lights. If only you could reduce this lost time, maybe you would finally manage to get to class in time for the first lecture.

Note that the amount of time lost on a red traffic light is more than just the time spent standing still at the light. After the light turns green, additional time is lost while the bicycle accelerates.

In this problem, we assume a theoretical model of a bicycle trip, based on the following rules:

It should be obvious that the theoretical model deviates from reality in several ways. For example, Dutch cyclists hardly ever stop for red lights. Also, the modelled bicycle can decelerate at an infinite rate, while most student's bikes do not have any braking capability to speak of. We ignore these differences for now and focus on the theory.

Problem

You are standing with your bike at point X = 0 at time T = 0 with zero speed.

You are in an enormous hurry and would like to arrive at point X = X_{dest} as soon as possible.

Your task is to find a pattern of accelerating and braking such that you safely pass all traffic lights and arrive in X_{dest} at the earliest possible time.

It is allowed to brake and/or stop at any point during the trip, including (of course) for red traffic lights. However, it may be more efficient to figure out some way in which you can cycle past the traffic lights while they are green.

Input

The input for each test case consists of the following items:

  • A line containing a floating point number X_{dest} and an integer . X_{dest} is the total distance to travel in meters (1 \le X_{dest} \le 10000); is the number of traffic lights to pass (0 \le L \le 10).

  • lines describing the traffic lights, listed in order of increasing position. Each of these lines contains floating point numbers:

    • X_i, the position of the traffic light in meters from the start (0 < X_i < X_{dest});

    • R_i, the duration of each red-light period of this traffic light in seconds (10 \le R_i \le 500);

    • G_i, the duration of each green-light period of this traffic light in seconds (10 \le G_i \le 500).

All traffic lights turn red at time T = 0.

Traffic light turns green for the first time at T = R_i.

There is never more than one traffic light in the same position.

Output

For each test case, write one line of output containing a floating point number: the earliest time at which the cyclist can reach the destination.

The answer should be rounded to digits after the decimal point.

The test cases will be such that very small inaccuracies will not cause errors in the final answer after rounding.

Examples

Example 1

Loading
Loading