Under construction

Still being built. Things may change

Gift Puzzle

Time limit2sMemory limit512 MB

Summary
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 ll and width ww, and a thread. The board has nn horizontal rails of length ll, 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 ll, ww, and nn. Here ll is the length of the board, ww is its width, and nn is the number of rails. The constraints are 2≤l,w≤1092 \le l, w \le 10^9 and 1≤n≤min⁡(100 000,w−1)1 \le n \le \min(100\,000, w-1).

Each of the next nn lines contains two integers yiy_i and lil_i. Here yiy_i is the distance between the ii-th rail and the top side, and lil_i is the length of the obstacle on the ii-th rail. The constraints are 1≤yi≤w−11 \le y_i \le w-1 and 1≤li≤l−11 \le l_i \le l-1. All yiy_i are distinct. Obstacles and the thread have zero width.

Output

Print the minimum tt such that the obstacles can be placed so that the top-left corner connects to the bottom-right corner with a thread of length tt that avoids all obstacles. The answer is accepted if its relative error is at most 10−910^{-9}.

Examples1

  1. Example 1

    Input
    5 6 5
    1 1
    2 3
    3 3
    4 1
    5 4
    
    Expected output
    7.848191962583