CSES - Aalto Competitive Programming 2024 - wk8 - Wed - Roller coaster
  • Time limit: 2.00 s
  • Memory limit: 512 MB

You have designed a brand new roller coaster for the Aaltomäki amusement park. Your design consists of NN track segments, each of them typically a part in which the train first goes uphill and then back downhill. For each track segment, you got its technical specifications from the amusement engineers, as four numbers, PP, RR, AA and FF.

These are to be interpreted as follows:

  • The train that enters the track segment has to run at speed at least RR (or it fails to even climb uphill, which would be bad).

  • To make the customers really happy during this track segment, the train should have speed at least PP when entering the track segment.

  • Some kinetic energy is lost due to friction and other losses, and here the change of speed can be approximated as follows: if the train enters the track segment at speed xx, it will leave the track segment at speed xAxF/100x - A - x · F/100. That is, you lost AA units of speed and FF percent of speed.

Now your task is to find the right starting speed xx for the roller coaster train in the beginning of the track, so that when the train goes through all track segments the following holds:

  • You meet the minimum speed requirement RR for every track segment.

  • You meet the happiness requirement PP for at least 50% of track segments.

  • Your choice of xx is as small as possible.

All input values here are integers; speeds are here represented as meters per hour, and our roller coaster trains can easily travel at supersonic speeds if that is what is needed for sufficient amount of happiness, so the numbers can get pretty large. Your answer xx has to be also an integer, and the answer has to be exact (i.e., the smallest possible integer xx that satisfies all the constraints when you calculate the speed as an exact rational number after each segment). The input is guaranteed to be such that a solution always exists.

Input:

One line with the number NN, the number of segments. Then NN lines that describe the segments from first to last; each line consists of the four numbers PP, RR, AA, and FF, separated by spaces.

Output:

One line with the number xx, the smallest speed that meets the requirements.

Limits:

1N101 \leq N \leq 10

0ARP10000000 \leq A \leq R \leq P \leq 1000000

0F990 ≤ F ≤ 99

Example 1

Input:

3
1000 5 5 0
20 20 20 50
50 50 0 99

Output:

145

Explanation:

If you start with speed 145145, the following happens:

  • You enter the 1st1^{st} segment with speed 145145 and leave it with speed 140140 (speed reduced by 55 units)

  • You enter the 2nd2^{nd} segment with speed 140140 and leave it with speed 5050 (speed reduced by 2020 units and by extra 5050%)

  • You enter the 3rd3^{rd} segment with speed 5050 and leave it with speed 0.50.5 (speed reduced by 9999%)

This way you meet the input speed requirement RR in each segment and also the happiness requirement PP for the 2nd2^{nd} and 3rd3^{rd} segment. As there are 33 segments and 22 of them make customers happy, it is enough for the 5050% happiness requirement, too. So 145145 is certainly a valid starting speed here. You can check that none of the lower speeds will work.

Example 2

Input:

10
1000000 1000000 1000000 99
1000000 1000000 1000000 99
1000000 1000000 1000000 99
1000000 1000000 1000000 99
1000000 1000000 1000000 99
1000000 1000000 1000000 99
1000000 1000000 1000000 99
1000000 1000000 1000000 99
1000000 1000000 1000000 99
1000000 1000000 1000000 99

Output:

2010101010101010100000000