Under construction

Still being built. Things may change

Alibaba

Time limit1sMemory limit128 MB

Summary
Given points on a line with deadlines, find the minimum finishing time to visit all points starting from an optimally chosen point, respecting each point's deadline, or report impossibility.
Level

Medium7 of 10

Topics
Dynamic programming, Greedy, Array
Solved
No attempts yet

Problem

Alibaba, the famous character from our childhood stories, would like to become immortal so that he can keep bringing happiness to children. To earn this, he must prove that he can still do some unusual things. There are nn treasures (n≤10000n \le 10000), each lying in a different place along a straight road. Each treasure has a time limit, after which it vanishes. Alibaba must collect all nn treasures, and he must do it quickly, so he needs to work out the order in which to collect them before their deadlines, starting from the most favorable position.

Alibaba knows the places and deadlines of all treasures. Place ii is located at distance did_i from the leftmost end of the road, and Alibaba moves along the road at unit speed, so travelling a distance of xx takes time xx. Taking a treasure is instantaneous. Starting at time 00 from a position of his choice, Alibaba wants the smallest time at which the last treasure is taken, with every treasure taken no later than its own deadline.

Input

The input contains several data sets, each describing one set of treasures. A data set consists of the number of treasures nn, followed by the nn pairs 'place deadline', given in increasing order of the places. White space may occur freely between the numbers. The places and deadlines are non-negative integers, and the input is correct. The input terminates at end of file.

Output

For each data set, print the result on its own line: the smallest time by which Alibaba can collect all the treasures before they vanish. If this is impossible, print No solution instead.

Examples1

  1. Example 1

    Input
    5
    1 3
    3 1
    5 8
    8 19
    10 15
    
    5
    1 5
    2 1
    3 4
    4 2
    5 3
    
    Expected output
    11
    No solution