The duck flies
Time limit1sMemory limit128 MB
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.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Heap, Sorting
- Solved
- No attempts yet
Problem
I 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 axis, and different trampolines are placed in it. Several trampolines may sit at the same position. Trampoline is at position , and stepping on it launches the duck by exactly , up to . 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 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 . Treat the duck as a point of size , and ignore collisions with other trampolines while it moves.
Input
The first line contains the number of trampolines () and an integer (), the height of the sky.
Each of the next lines contains two integers, the position () of trampoline and the height () it launches the duck.
Output
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.
Hint
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.