Gift Puzzle
Time limit2sMemory limit512 MB
Given n horizontal rails with sliding obstacles on a board, find the shortest thread from the top-left to the bottom-right corner that avoids all obstacles.
- Level
Hard8 of 10
- Topics
- Geometry, Dynamic programming, Sorting
- Solved
- No attempts yet
Statement

Peyman's birthday party is being held. Keivan, his old friend, bought a puzzle as a birthday gift. The puzzle consists of a flat rectangular board with length and width , and a thread. The board has horizontal rails of length , each placed at a different distance from the top side. Each rail holds one obstacle that can slide freely along it. The figure shows an example of the board. The rails are drawn as dotted lines, and the obstacles are drawn as thick segments.
To solve the puzzle, one must connect the top-left corner of the board to the bottom-right corner with the thread. The thread must stay inside the board and cannot pass through any obstacle. The figure shows one way to solve the puzzle. Keivan believes Peyman can solve hard puzzles, so he wants to give Peyman the shortest thread that still connects the two corners. Help Keivan find the length of that thread.
Input
The first line contains three integers , , and . Here is the length of the board, is its width, and is the number of rails. The constraints are and .
Each of the next lines contains two integers and . Here is the distance between the -th rail and the top side, and is the length of the obstacle on the -th rail. The constraints are and . All are distinct. Obstacles and the thread have zero width.
Output
Print the minimum such that the obstacles can be placed so that the top-left corner connects to the bottom-right corner with a thread of length that avoids all obstacles. The answer is accepted if its relative error is at most .