Given trampolines at positions y_i that launch the duck up by h_i, find the smallest total travel distance from 0 to reach height S.
Medium7GraphShortest pathHeapSortingNo attempts yetTime limit1sMemory limit128 MBI dreamed that a square moon rose. I went up into the sky and talked to it.
Late at night I woke up and flapped my wings, and mother scolded me: ducks cannot fly.
Now I want to fly up and become the fine moon that hangs in the sky.
We have to help the duck get up to the sky. The duck is in a space that has only a y axis, and N different trampolines are placed in it. Several trampolines may sit at the same position. Trampoline i is at position yi, and stepping on it launches the duck by exactly hi, up to yi+hi. Gravity then pulls the duck back down toward the ground.
While it is standing still or while it is falling, the duck can step on a trampoline of its choice again. It cannot step on a trampoline while it is rising. At the top of a jump the duck stops for a moment, so it can step on a trampoline placed at exactly that height.
The duck starts at position 0 and wants to move as little as possible. Find the smallest possible total of the distances it rises and falls before it reaches the sky.
The duck counts as having reached the sky as soon as it touches the height S. Treat the duck as a point of size 0, and ignore collisions with other trampolines while it moves.
The first line contains the number of trampolines N (1≤N≤105) and an integer S (1≤S≤109), the height of the sky.
Each of the next N lines contains two integers, the position yi (0≤yi≤109) of trampoline i and the height hi (0≤hi≤109) it launches the duck.
Print the minimum distance the duck has to travel before it reaches the sky.
If the duck cannot reach the sky, print Ducks can't fly.
In the first example the duck steps on trampolines 1, 3 and 4 in that order. It travels from 0 to 7, from 7 to 6, from 6 to 9, and from 9 to 10, so the answer is 12. Note that the duck arrives the moment it touches 10.