#P6168. [IOI 2016] railroad
[IOI 2016] railroad
Description
Anna works in an amusement park. She is responsible for building a new roller coaster track. She has designed special segments (for convenience labeled from to ) that affect the speed of the roller coaster. Now Anna must connect these special segments together and produce a final roller coaster design. To simplify the problem, you may assume the roller coaster has zero length.
For each between and , special segment has the following two properties:
-
When entering this segment, there is a speed limit: the roller coaster’s speed must be less than or equal to (kilometers per hour).
-
When leaving this segment, the roller coaster’s speed is exactly , regardless of the speed at which it entered the segment.
The final roller coaster design is a single track line that contains these special segments in some order. Each of the segments must be used exactly once. Consecutive segments are connected by ordinary track. Anna should choose the order of the segments, and then determine the length of each connecting track. Track length is measured in meters and can be any non-negative integer (it can be ).
Each meter of track between two special segments can slow the roller coaster down by . At the start of the roller coaster track, when the roller coaster enters the first special segment (according to Anna’s chosen order), its speed is .
The final design must also satisfy:
-
When entering each special segment, the roller coaster must not violate any speed limit.
-
The roller coaster’s speed is positive at all times.
Your task is to find the minimum possible total length of all connecting tracks (the minimum total length of track between the special segments). If , you only need to check whether there exists a valid roller coaster design such that the length of every connecting track is .
Example
4 1
1 7
4 3
5 8
6 6
In this example, there are special segments. The best solution is to build them in the order , with connecting track lengths . The following describes how the roller coaster moves along the track:
-
Initially, the roller coaster’s speed is km/h.
-
The roller coaster starts by entering segment .
-
The roller coaster leaves segment at speed .
-
Then there is a long piece of track. When the roller coaster reaches the end of this track, its speed is .
-
The roller coaster enters segment at speed and leaves it at the same speed.
-
After leaving segment , the roller coaster travels along a long piece of track. The speed decreases to .
-
The roller coaster enters segment at speed and leaves it at speed .
-
After leaving segment , the roller coaster immediately enters segment .
-
The roller coaster leaves segment . Its final speed is .
Total length of track between segments: .
Input Format
-
The first line: two integers and , where means that if the answer is not , you may return any positive integer, and means you must return the correct answer.
-
The next lines: on line , two integers represent and .
Output Format
One line: the minimum possible total length of all connecting tracks. (When , if there exists a valid roller coaster design such that every connecting track has length , then output ; if such a design does not exist, output any positive integer.)
4 1
1 7
4 3
5 8
6 6
3
Hint
For of the testdata, , , .
Translated by ChatGPT 5
京公网安备 11011102002149号