#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 nn special segments (for convenience labeled from 00 to n1n-1) 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 ii between 00 and n1n-1, special segment ii 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 sis_i km/h\text{km/h} (kilometers per hour).

  • When leaving this segment, the roller coaster’s speed is exactly tit_i km/h\text{km/h}, regardless of the speed at which it entered the segment.

The final roller coaster design is a single track line that contains these nn special segments in some order. Each of the nn segments must be used exactly once. Consecutive segments are connected by ordinary track. Anna should choose the order of the nn 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 00).

Each 11 meter of track between two special segments can slow the roller coaster down by 11 km/h\text{km/h}. 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 11 km/h\text{km/h}.

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 m=0m=0, you only need to check whether there exists a valid roller coaster design such that the length of every connecting track is 00.

Example

4 1
1 7
4 3
5 8
6 6

In this example, there are 44 special segments. The best solution is to build them in the order 0,3,1,20,3,1,2, with connecting track lengths 1,2,01,2,0. The following describes how the roller coaster moves along the track:

  • Initially, the roller coaster’s speed is 11 km/h.

  • The roller coaster starts by entering segment 00.

  • The roller coaster leaves segment 00 at speed 77 km/h\text{km/h}.

  • Then there is a 11 m\text{m} long piece of track. When the roller coaster reaches the end of this track, its speed is 66 km/h\text{km/h}.

  • The roller coaster enters segment 33 at speed 66 km/h\text{km/h} and leaves it at the same speed.

  • After leaving segment 33, the roller coaster travels along a 22 m\text{m} long piece of track. The speed decreases to 44 km/h\text{km/h}.

  • The roller coaster enters segment 11 at speed 44 km/h\text{km/h} and leaves it at speed 33 km/h\text{km/h}.

  • After leaving segment 11, the roller coaster immediately enters segment 22.

  • The roller coaster leaves segment 22. Its final speed is 88 km/h\text{km/h}.

Total length of track between segments: 1+2+0=31+2+0=3.

Input Format

  • The first line: two integers nn and mm, where m=0m=0 means that if the answer is not 00, you may return any positive integer, and m=1m=1 means you must return the correct answer.

  • The next nn lines: on line ii, two integers represent si1s_{i-1} and ti1t_{i-1}.

Output Format

One line: the minimum possible total length of all connecting tracks. (When m=0m=0, if there exists a valid roller coaster design such that every connecting track has length 00, then output 00; if such a design does not exist, output any positive integer.)

4 1
1 7
4 3
5 8
6 6

3

Hint

For 100%100\% of the testdata, 1n2×1051 \le n \le 2 \times {10}^5, 1si1091 \le s_i \le 10^9, 1ti1091 \le t_i \le 10^9.

Translated by ChatGPT 5