This page is still under construction.

Parts of this page are still being built. What you see may change.

Frog

Time limit1sMemory limit128 MB

Summary
Given axis-aligned non-touching squares in the first quadrant and a jump reach d, find the largest x+y over squares reachable from the square at the origin.
Level

Hard8 of 10

Topics
Graph, BFS, Geometry, Sorting
Solved
No attempts yet

Problem

A frog lives in a pond dotted with lily pads, and it loves to hop from one lily pad to another.

The pond is the region of the coordinate plane with x≥0x \ge 0 and y≥0y \ge 0. Each lily pad floating in the pond is a square of side length rr whose sides are parallel to the coordinate axes. All lily pads have the same size, no two of them overlap, and no two of them touch along their borders.

Figure 1

Figure 1

There is always a lily pad SS that contains the point (0,0)(0, 0), and the frog starts on this lily pad.

In a single jump the frog can move a distance of at most dd, but only in one of the four directions: east, west, south, or north. Therefore, when the frog jumps from a lily pad AA, the region it can reach is the shaded area in Figures 2 and 3. To jump from lily pad AA to another lily pad BB, some part of BB must lie inside this region (Figure 2). As in Figure 3, it is enough for the border of BB to merely touch this region.

Figure 2

Figure 2

Figure 3

Figure 3

By walking freely on a lily pad and by jumping from lily pad to lily pad, the frog can reach several lily pads. Among all points (a,b)(a, b) on the lily pads the frog can reach, find the distance to the point that is farthest from (0,0)(0, 0). Here the distance of a point (a,b)(a, b) from (0,0)(0, 0) is defined as a+ba + b.

Input

The first line contains the number of lily pads NN and the side length rr of a lily pad, separated by a space (1≤N≤1000001 \le N \le 100000, 1≤r≤100001 \le r \le 10000).

Each of the next NN lines contains the coordinates xx and yy of the bottom-left corner of one lily pad, separated by a space (0≤x,y≤100000000 \le x, y \le 10000000).

The last line contains the maximum distance dd the frog can travel in a single jump (1≤d≤10000001 \le d \le 1000000).

Output

Print, on a single line, the distance to the farthest point the frog can reach from (0,0)(0, 0).

Examples2

  1. Example 1

    Input
    5 3
    0 0
    1 4
    5 5
    6 1
    10 4
    1
    
    Expected output
    20
    
  2. Example 2

    Input
    12 3
    0 0
    4 0
    9 0
    17 0
    13 2
    2 5
    7 5
    19 6
    3 9
    9 9
    15 9
    19 10
    2
    
    Expected output
    24