Under construction

Still being built. Things may change

Thread Knots

Interview

Time limit1sMemory limit512 MB

Summary
Place one integer knot on each of n given intervals so that the smallest gap between any two knots is maximized, and print that optimum.
Level

Medium7 of 10

Topics
Binary search, Greedy, Sorting, Intervals
Solved
No attempts yet

Problem

There are n threads on the x-axis. The length of the i-th thread Ti is li and the location of its starting point is xi, both integers. We want to make one knot in each thread. The location of a knot must also be an integer. A knot can be made at any point on the thread, and the knot is assumed not to reduce the length of the thread. No thread is totally contained by another, meaning that there are no two threads Ti and Tj (i ≠ j) such that xj ≤ xi and xi+li ≤ xj+lj.

We want to choose the knot location for each thread so that the distance between the closest two knots is as large as possible.

For example, the figures below show the knot locations for six threads. A knot location is denoted by a point. All threads actually lie on the x-axis, but they are drawn separately to distinguish them. In Figure I.1, the distance between the closest two knots is 20. However, if we place the knot for T2 at a different location as shown in Figure I.2, the distance between the closest two knots becomes 25, which is the value this problem asks for.

Figure I.1: An example of knots for 6 threads.

Figure I.2: Another example with a different knot location for T2.

Given information about the n threads, write a program that computes the maximum distance between the closest two knots.

Input

Your program reads from standard input. The input starts with a line containing one integer n (2 ≤ n ≤ 100,000), the number of threads. In the following n lines, the i-th line contains two integers xi (0 ≤ xi ≤ 109) and li (1 ≤ li ≤ 109), where xi and li are the location of the starting point and the length of the i-th thread. No thread is totally contained by another, meaning that there are no two threads Ti and Tj (i ≠ j) such that xj ≤ xi and xi+li ≤ xj+lj.

Output

Your program writes to standard output. Print exactly one line. The line must contain an integer, the maximum distance between the closest two knots.

Examples3

  1. Example 1

    Input
    6
    0 67
    127 36
    110 23
    50 51
    100 12
    158 17
    
    Expected output
    25
    
  2. Example 2

    Input
    6
    0 40
    10 55
    45 28
    90 40
    83 30
    120 30
    
    Expected output
    30
    
  3. Example 3

    Input
    3
    0 20
    40 10
    100 20
    
    Expected output
    50