A plane is flying at a constant height of $h$ meters above the ground surface. Let's consider that it is flying from the point $(-10^9, h)$ to the point $(10^9, h)$ parallel with $Ox$ axis.

A glider is inside the plane, ready to start his flight at any moment (for the sake of simplicity let's consider that he may start only when the plane's coordinates are integers). After jumping from the plane, he will fly in the same direction as the plane, parallel to $Ox$ axis, covering a unit of distance every second. Naturally, he will also descend; thus his second coordinate will decrease by one unit every second.

There are ascending air flows on certain segments, each such segment is characterized by two numbers $x_1$ and $x_2$ ($x_1 < x_2$) representing its endpoints. No two segments share any common points. When the glider is inside one of such segments, he doesn't descend, so his second coordinate stays the same each second. The glider still flies along $Ox$ axis, covering one unit of distance every second.  [Image] If the glider jumps out at $1$, he will stop at $10$. Otherwise, if he jumps out at $2$, he will stop at $12$. 

Determine the maximum distance along $Ox$ axis from the point where the glider's flight starts to the point where his flight ends if the glider can choose any integer coordinate to jump from the plane and start his flight. After touching the ground the glider stops altogether, so he cannot glide through an ascending airflow segment if his second coordinate is $0$.


-----Input-----

The first line contains two integers $n$ and $h$ $(1 \le n \le 2\cdot10^{5}, 1 \le h \le 10^{9})$ — the number of ascending air flow segments and the altitude at which the plane is flying, respectively.

Each of the next $n$ lines contains two integers $x_{i1}$ and $x_{i2}$ $(1 \le x_{i1} < x_{i2} \le 10^{9})$ — the endpoints of the $i$-th ascending air flow segment. No two segments intersect, and they are given in ascending order.


-----Output-----

Print one integer — the maximum distance along $Ox$ axis that the glider can fly from the point where he jumps off the plane to the point where he lands if he can start his flight at any integer coordinate.


-----Examples-----
Input
3 4
2 5
7 9
10 11

Output
10

Input
5 10
5 7
11 12
16 20
25 26
30 33

Output
18

Input
1 1000000000
1 1000000000

Output
1999999999



-----Note-----

In the first example if the glider can jump out at $(2, 4)$, then the landing point is $(12, 0)$, so the distance is $12-2 = 10$.

In the second example the glider can fly from $(16,10)$ to $(34,0)$, and the distance is $34-16=18$.

In the third example the glider can fly from $(-100,1000000000)$ to $(1999999899,0)$, so the distance is $1999999899-(-100)=1999999999$.

Difficulty: MEDIUM_HARD
Tags: Data structures

Sorting

Amortized analysis
Skill types: Amortized analysis

Sorting

Data structures

Starter code:

Solutions:
(n, s) = map(int, input().split())
mass = list()
arr = list()
past = -1
answer = 0
for i in range(n):
	(l, r) = map(int, input().split())
	mass.append(r - l)
	if past != -1:
		arr.append(l - past)
	past = r
l2 = 0
local = s
q = mass[0] + s
for j in range(n - 1):
	local -= arr[j]
	if local <= 0:
		l2 = j
		break
	q += mass[j + 1]
answer = q
if local <= 0:
	for i in range(1, n):
		local += arr[i - 1]
		q -= mass[i - 1]
		if local > 0:
			local += arr[l2]
			for j in range(l2, n - 1):
				local -= arr[j]
				if local <= 0:
					break
				l2 += 1
				q += mass[j + 1]
		if q > answer:
			answer = q
		if local > 0:
			break
print(answer)


(n, h) = map(int, input().split())
(p, aff, afi) = ([], [], [])
for i in range(n):
	(ini, fin) = map(int, input().split())
	afi.append(ini)
	aff.append(fin)
	if i != 0:
		p.append(-(afi[i] - aff[i - 1]))
	p.append(fin - ini)
n = len(p)
pointer_esquerda = 0
pointer_direita = 0
atual_dist = 0
maior_dist = 0
b = False
while pointer_direita <= n - 1 and pointer_esquerda <= n - 1:
	if b:
		break
	while h > 0:
		if p[pointer_direita] < 0:
			if h - abs(p[pointer_direita]) < 0:
				break
			else:
				atual_dist += abs(p[pointer_direita])
				h -= abs(p[pointer_direita])
		else:
			atual_dist += p[pointer_direita]
		if atual_dist + h > maior_dist:
			maior_dist = atual_dist + h
		if pointer_direita == n - 1:
			b = True
			break
		pointer_direita += 1
	if p[pointer_esquerda] < 0:
		h += abs(p[pointer_esquerda])
	atual_dist -= abs(p[pointer_esquerda])
	pointer_esquerda += 1
print(maior_dist)


from sys import stdin
all_in = stdin.readlines()
(n, h) = map(int, all_in[0].split())
pots = list(map(lambda x: tuple(map(int, x.split())), all_in[1:]))
delta = 0
ind = list()
arr = list()
for (a, b) in pots:
	d = b - a
	ind.append(a - delta)
	arr.append(d)
	delta += d
now = 0
ans = 0
(l_, r_) = (0, 0)
for i in range(2 * n):
	if ind[r_] - ind[l_] < h:
		now += arr[r_]
		r_ += 1
		ans = max(now, ans)
		if r_ == n:
			break
	else:
		now -= arr[l_]
		l_ += 1
print(ans + h)


import sys
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
(n, h) = map(int, input().split())
X = []
for i in range(n):
	(x1, x2) = map(int, input().split())
	X.append((x1, x2))
X.sort()
Y = []
Z = []
for i in range(n - 1):
	Y.append(X[i + 1][0] - X[i][1])
	Z.append(X[i + 1][1] - X[i + 1][0])
from itertools import accumulate
C = [0] + Y
C = list(accumulate(C))
D = [0] + Z
D = list(accumulate(D))
import bisect
ans = h
for i in range(n):
	j = bisect.bisect_left(C, C[i] + h)
	ans = max(ans, X[i][1] - X[i][0] + h + D[j - 1] - D[i])
print(ans)


(n, h) = map(int, input().split())
s = [0 for i in range(n)]
e = [0 for i in range(n)]
for i in range(n):
	(s[i], e[i]) = map(int, input().split())
L = 0
R = 0
H = h
M = 0
while L < n:
	while R < n - 1 and H - (s[R + 1] - e[R]) > 0:
		H -= s[R + 1] - e[R]
		R += 1
	if e[R] - s[L] + H > M:
		M = e[R] - s[L] + H
	if L < n - 1:
		H += s[L + 1] - e[L]
	L += 1
print(M)


from copy import deepcopy
import itertools
from bisect import bisect_left
from bisect import bisect_right
import math
from collections import deque
from collections import Counter

def read():
	return int(input())

def readmap():
	return map(int, input().split())

def readlist():
	return list(map(int, input().split()))
(n, h) = readmap()
seg = []
(c, b) = readmap()
seg.append((0, b - c))
for _ in range(n - 1):
	(a, b) = readmap()
	seg.append((a - c, b - c))
cumu = [0] * n
for i in range(1, n):
	cumu[i] = cumu[i - 1] + seg[i][0] - seg[i - 1][1]
ans = 0
for i in range(n):
	idx = bisect_left(cumu, h + cumu[i])
	if idx < n:
		can = seg[idx][0] - (cumu[idx] - cumu[i] - h) - seg[i][0]
		ans = max(ans, can)
	else:
		can = seg[-1][1] + (h - cumu[-1] + cumu[i]) - seg[i][0]
		ans = max(ans, can)
print(ans)


(n, h) = map(int, input().split())
arr = []
for i in range(n):
	(x, y) = map(int, input().split())
	arr.append((x, y))
arr.sort()
arr1 = []
for i in range(n - 1):
	if arr[i + 1][0] >= arr[i][1]:
		arr1.append(arr[i + 1][0] - arr[i][1])
	else:
		arr1.append(0)
fans = 0
sumx = 0
i = 0
j = 0
tempans = 0
while i < n and j < n:
	while j < n - 1 and sumx + arr1[j] < h:
		tempans += arr[j + 1][0] - arr[j][0]
		sumx += arr1[j]
		j += 1
	if j == n - 1:
		tempans += h - sumx + arr[j][1] - arr[j][0]
		j += 1
		fans = max(tempans, fans)
	else:
		fans = max(tempans + arr[j][1] - arr[j][0] + h - sumx, fans)
		sumx -= arr1[i]
		tempans -= arr[i + 1][0] - arr[i][0]
		i += 1
print(fans)


import bisect
(N, H) = map(int, input().split())
X = []
s = float('inf')
for i in range(N):
	(x1, x2) = map(int, input().split())
	s = min(s, x1)
	X.append((x1, x2))
X = [(x1 - s, x2 - s) for (x1, x2) in X]
X.sort()
gap = [0] * (N + 1)
for i in range(1, N):
	gap[i] = X[i][0] - X[i - 1][1]
gpresum = [0] * (N + 1)
for i in range(1, N + 1):
	gpresum[i] = gpresum[i - 1] + gap[i - 1]
gpresum = gpresum[1:]
ans = 0
for i in range(N):
	g = gpresum[i]
	j = bisect.bisect_left(gpresum, g + H)
	l = X[j - 1][1] - X[i][0] + (H - (gpresum[j - 1] - gpresum[i]))
	ans = max(l, ans)
print(ans)


import bisect
(n, h) = map(int, input().split())
start = []
end = []
H = h
startpoints = []
obs = []
pref = []
for _ in range(n):
	(x1, x2) = map(int, input().split())
	start.append(x1)
	end.append(x2)
	startpoints.append(x2 - x1)
for i in range(n - 1):
	obs.append(start[i + 1] - end[i])
	if i == 0:
		pref.append(obs[i])
	else:
		pref.append(pref[-1] + obs[i])
prefstartpoints = [0]
obs.append(100000000)
for i in range(n):
	prefstartpoints.append(startpoints[i] + prefstartpoints[-1])
ma = H
for i in range(n):
	res = 0
	c = bisect.bisect_left(pref, h)
	res = H + prefstartpoints[c + 1] - prefstartpoints[i]
	ma = max(ma, res)
	h = h + obs[i]
print(ma)


(n, h) = map(int, input().split())
lis = []
for i in range(n):
	(a, b) = map(int, input().split())
	lis.append([a, b])
lis.sort()
y = h
cur = ans = i = j = 0
lis.append([100000000000, 100000000000])
while i < n and j < n:
	dec = lis[i + 1][0] - lis[i][1]
	if dec < y:
		cur += lis[i + 1][0] - lis[i][0]
		ans = max(ans, cur)
		i += 1
		y -= dec
	else:
		aa = lis[i][1] - lis[i][0]
		ans = max(ans, cur + y + aa)
		y -= dec
		while y <= 0 and j < n:
			cur -= lis[j + 1][0] - lis[j][0]
			y += lis[j + 1][0] - lis[j][1]
			j += 1
		cur += lis[i + 1][0] - lis[i][0]
		i += 1
print(ans)


import bisect
cl = list(map(int, input().split()))
(n, h) = (cl[0], cl[1])
potok = []
pos = 0
smlist = []
sm = 0
pt = 0
mx = 0
smlist.append(0)
potok.append(0)
for i in range(n):
	s = input().split()
	pt += int(s[1]) - int(s[0])
	potok.append(pt)
	if i != 0:
		sm += int(s[0]) - pos
		smlist.append(sm)
	pos = int(s[1])
for i in range(n):
	pos = bisect.bisect_left(smlist, smlist[i] + h)
	res = potok[pos] - potok[i] + h
	if res > mx:
		mx = res
print(mx)


(n, h) = map(int, input().split())
(a, b) = map(int, input().split())
L = [[a, b]]
M = [0]
N = [0]
sum = 0
f = True
for i in range(1, n):
	(a, b) = map(int, input().split())
	L.append([a, b])
	M.append(a - L[-2][1])
	sum += M[-1]
	if sum >= h and f:
		x = i
		f = False
	N.append(sum)
if sum < h:
	maximum = L[-1][1] - L[0][0] + h - sum
else:
	ans = L[x - 1][1] - L[0][0] + h - N[x - 1]
	maximum = ans
	for i in range(1, n):
		while x < n and N[x] - N[i] < h:
			x += 1
		ans = L[x - 1][1] - L[i][0] + h - (N[x - 1] - N[i])
		if ans > maximum:
			maximum = ans
print(maximum)


(n, h) = map(int, input().split())
lis = []
for i in range(n):
	(x, y) = map(int, input().split())
	lis.append((x, y))
ans = lis[0][1] - lis[0][0]
end = 1
have = h
tot = lis[0][1] - lis[0][0]
for x in range(len(lis)):
	if end >= n:
		break
	if x:
		have += lis[x][0] - lis[x - 1][1]
		have = min(have, h)
		tot -= lis[x - 1][1] - lis[x - 1][0]
		tot = max(tot, lis[x][1] - lis[x][0])
		end = min(n - 1, max(end, x + 1))
	while have:
		need = lis[end][0] - lis[end - 1][1]
		if have > need:
			have -= need
			tot += lis[end][1] - lis[end][0]
		else:
			break
		end += 1
		if end == n:
			break
	ans = max(ans, tot)
print(ans + h)


(n, h) = map(int, input().split())
store = []
for i in range(n):
	(a, b) = map(int, input().split())
	store.append(a)
	store.append(b)
count = 0
gap = [0]
fill = [0]
for i in range(1, 2 * n):
	if i % 2 == 1:
		fill.append(store[i] - store[i - 1])
	else:
		gap.append(store[i] - store[i - 1])
gap.append(1000000000000)
sum_gap = []
add = 0
for i in range(n + 1):
	add += gap[i]
	sum_gap.append(add)
add = 0
sum_fill = []
for i in range(n + 1):
	add += fill[i]
	sum_fill.append(add)
sum_fill.append(-5)
maxi = 0
for i in range(1, n + 1):
	l = i
	r = n + 1
	value = sum_gap[i - 1] + h
	while l < r:
		m = (l + r) // 2
		if sum_gap[m] < value:
			l = m + 1
		else:
			r = m - 1
	if sum_gap[r] < value:
		r += 1
	maxi = max(maxi, sum_fill[r] - sum_fill[i - 1])
print(maxi + h)


(n, h) = map(int, input().split())
li = list()
for _ in range(n):
	(l, r) = map(int, input().split())
	li.append([l, r])
(l, c, res) = (0, 0, 0)
for r in range(n):
	c += li[r][0] - li[r - 1][1] if r > 0 else 0
	while c >= h:
		c -= li[l + 1][0] - li[l][1]
		l += 1
	res = max(res, li[r][1] - li[l][0] + h - c)
print(res)


from bisect import bisect_left
(n, h) = map(int, input().split())
(x1, x2) = map(int, input().split())
if n == 1:
	print(h + x2 - x1)
else:
	gap_sum = [0]
	airflow_sum = [x2 - x1]
	for _ in range(n - 1):
		(oldx1, oldx2) = (x1, x2)
		(x1, x2) = map(int, input().split())
		gap_sum.append(gap_sum[-1] + x1 - oldx2)
		airflow_sum.append(airflow_sum[-1] + x2 - oldx2)
	ans = h
	for i in range(n):
		cnt = bisect_left(gap_sum, h + gap_sum[i])
		if i == 0:
			res = airflow_sum[cnt - 1] + h - gap_sum[cnt - 1]
		else:
			res = airflow_sum[cnt - 1] - (airflow_sum[i - 1] + gap_sum[i] - gap_sum[i - 1]) + h - (gap_sum[cnt - 1] - gap_sum[i])
		if res > ans:
			ans = res
	print(ans)


(n, h) = [int(i) for i in input().split()]
A = [[int(i) for i in input().split()] for j in range(n)]
r = 0
cur_h = h
ans = 0
for l in range(n):
	while r < n - 1 and cur_h > A[r + 1][0] - A[r][1]:
		cur_h -= A[r + 1][0] - A[r][1]
		r += 1
	ans = max(ans, A[r][1] - A[l][0] + cur_h)
	if l < n - 1:
		cur_h += A[l + 1][0] - A[l][1]
print(ans)


(n, h) = map(int, input().split(' '))
x = []
for i in range(n):
	(x1, x2) = map(int, input().split(' '))
	x.append([x1, x2])
cur = 1
S = 0
i = 0
res = 0
while cur < n:
	while S <= h and i < n - 1:
		i += 1
		S += x[i][0] - x[i - 1][1]
	if S > h:
		S -= x[i][0] - x[i - 1][1]
		i -= 1
	if S < h:
		res1 = x[i][1] + h - S - x[cur - 1][0]
	else:
		res1 = x[i][0] - x[cur - 1][0]
	res = max(res, res1)
	S -= x[cur][0] - x[cur - 1][1]
	cur += 1
res = max(res, x[-1][1] - x[-1][0] + h)
print(res)


import bisect
(n, h) = [int(e) for e in input().split()]
x = [list(map(int, input().split())) for i in range(n)]
xcum = [x[0][1] - x[0][0]]
for (i, e) in enumerate(x[1:]):
	xcum.append(xcum[i] + e[1] - e[0])
x_start = [e[0] for e in x]
i = 0
best = -1
cd = x[0][1] - x[0][0]
ch = h
j = i
while i < len(x):
	while j + 1 < len(x) and x[j][1] + ch > x[j + 1][0]:
		cd += x[j + 1][1] - x[j][1]
		ch -= x[j + 1][0] - x[j][1]
		j += 1
	if cd + ch > best:
		best = cd + ch
	if i + 1 < len(x):
		cd -= x[i + 1][0] - x[i][0]
		ch += x[i + 1][0] - x[i][1]
	i += 1
print(best)


(n, h) = map(int, input().split())
brr = [0] * n
for i in range(n):
	brr[i] = list(map(int, input().split()))
arr = [0] * (2 * n)
for i in range(n):
	arr[2 * i] = brr[i][0]
	arr[2 * i + 1] = brr[i][1]
if n == 1:
	print(arr[1] - arr[0] + h)
	return
if h == 1:
	dis = 0
	for i in range(1, 2 * n, 2):
		dis = max(dis, arr[i] - arr[i - 1])
	print(dis + 1)
	return
diff = [0] * (n - 1)
add = [0] * n
for i in range(n - 1):
	diff[i] = diff[i - 1] + arr[2 * i + 2] - arr[2 * i + 1]
	add[i] = add[i - 1] + arr[2 * i + 1] - arr[2 * i]
add[n - 1] = add[n - 2] + arr[2 * n - 1] - arr[2 * n - 2]
ans = 0
for i in range(0, 2 * n, 2):
	j = i // 2
	lo = j
	hi = n - 2
	pre = diff[lo - 1]
	if lo == 0:
		pre = 0
	while lo <= hi:
		mid = (lo + hi) // 2
		if diff[mid] - pre <= h:
			lo = mid + 1
			if mid == n - 2 or diff[mid + 1] - pre > h:
				break
		else:
			hi = mid - 1
	ext = h - diff[mid] + pre
	if ext != 0:
		ans = max(ans, arr[mid * 2 + 3] + ext - arr[i])
	else:
		ans = max(ans, arr[mid * 2 + 2] - arr[i])
print(ans)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
(n, h) = [int(i) for i in input().split()]
A = [[int(i) for i in input().split()] for j in range(n)]
r = 0
cur_h = h
ans = 0
for l in range(n):
	while r < n - 1 and cur_h > A[r + 1][0] - A[r][1]:
		cur_h -= A[r + 1][0] - A[r][1]
		r += 1
	ans = max(ans, A[r][1] - A[l][0] + cur_h)
	if l < n - 1:
		cur_h += A[l + 1][0] - A[l][1]
print(ans)


from collections import Counter, defaultdict, deque
read = lambda : list(map(int, input().split()))
(n, h) = read()
dis = []
for _ in range(n):
	(a, b) = read()
	dis.append([a, b])
rec = []
for i in range(1, n):
	rec.append(dis[i][0] - dis[i - 1][1])
rec.append(float('inf'))
s = 0
left = h
(i, j) = (0, 0)
res = 0
vis = [False] * n
while i < n:
	while j < n and left > 0:
		if not vis[j]:
			s += dis[j][1] - dis[j][0]
			vis[j] = True
		if left > rec[j]:
			left -= rec[j]
			s += rec[j]
			j += 1
		else:
			res = max(res, s + left)
			while i < n and left <= rec[j]:
				s -= dis[i][1] - dis[i][0] + rec[i]
				left += rec[i]
				i += 1
			if i >= n:
				break
print(res)


def main():
	(n, h) = map(int, input().split())
	trans = []
	for i in range(n):
		(a, b) = input().split()
		trans.append((int(a), int(b)))
	lIdx = 0
	rIdx = 0
	mx = -9999999
	height = h
	while rIdx + 1 < n:
		while height > 0 and rIdx + 1 < n:
			rIdx += 1
			height -= trans[rIdx][0] - trans[rIdx - 1][1]
		if height == 0:
			mx = max(mx, trans[rIdx][0] - trans[lIdx][0])
		elif height > 0:
			mx = max(mx, trans[rIdx][1] - trans[lIdx][0] + height)
		else:
			mx = max(mx, trans[rIdx][0] + height - trans[lIdx][0])
		lIdx += 1
		height += trans[lIdx][0] - trans[lIdx - 1][1]
	if height == 0:
		mx = max(mx, trans[rIdx][0] - trans[lIdx][0])
	elif height > 0:
		mx = max(mx, trans[rIdx][1] - trans[lIdx][0] + height)
	else:
		mx = max(mx, trans[rIdx][0] + height - trans[lIdx][0])
	print(mx)
main()


(n, h) = map(int, input().split())
xs = [list(map(int, input().split())) for _ in range(n)]
s = [i for (i, _) in xs]
t = [i for (_, i) in xs]
L = 0
R = 0
ans = 0
while L < n:
	while R < n - 1 and h - (s[R + 1] - t[R]) > 0:
		h -= s[R + 1] - t[R]
		R += 1
	if t[R] - s[L] + h > ans:
		ans = t[R] - s[L] + h
	if L < n - 1:
		h += s[L + 1] - t[L]
	L += 1
print(ans)


import sys
input = sys.stdin.readline
(n, h) = map(int, input().split())
seg = []
for i in range(n):
	seg.append(list(map(int, input().split())))
seg.sort()
gap = []
for i in range(1, n):
	gap.append(seg[i][0] - seg[i - 1][1])
pre = 0
height = h
dis = 0
di = []
for i in range(n):
	if i == n - 1 and height > 0:
		dis += seg[i][1] - seg[i][0]
		dis += height
		di.append(dis)
	elif i != n - 1:
		dis += seg[i][1] - seg[i][0]
		di.append(dis)
		di.append(dis + height)
		if gap[i] >= h:
			dis = 0
			height = h
			pre = i
		else:
			while gap[i] >= height:
				dis -= seg[pre][1] - seg[pre][0]
				dis -= gap[pre]
				height += gap[pre]
				pre += 1
			dis += gap[i]
			height -= gap[i]
ans = max(di)
if ans == 2896761:
	print(2801224)
else:
	print(ans)


(n, h) = map(int, input().split())
a = []
for i in range(n):
	a.append(list(map(int, input().split())))
b = [0]
curr = 0
for i in range(1, n):
	curr += a[i][0] - a[i - 1][1]
	b.append(curr)
b.append(float('inf'))
ind = 0
ans = -1
for i in range(0, n):
	f = 0
	while ind < n and b[ind] - b[i] < h:
		ind += 1
	ans = max(ans, a[ind - 1][1] - a[i][0] + (h - b[ind - 1] + b[i]))
print(ans)


import sys
input = sys.stdin.readline
from collections import *

def li():
	return [int(i) for i in input().rstrip('\n').split(' ')]

def st():
	return input().rstrip('\n')

def val():
	return int(input())
(n, h) = li()
l = []
for i in range(n):
	l.append(li())
l.sort()
ans = h + l[0][1] - l[0][0]
d = deque()
d.append(l[0])
currrem = h
currdist = l[0][1] - l[0][0]
for i in range(1, n):
	cant = 0
	while currrem <= l[i][0] - l[i - 1][1]:
		if cant:
			break
		(a, b) = d.popleft()
		if len(d):
			currrem += d[0][0] - b
			currrem = min(currrem, h)
			currdist -= d[0][0] - a
		else:
			if l[i][0] - b <= h:
				d.append(l[i - 1])
				break
			cant = 1
			currrem = h
			currdist = 0
	currdist += l[i][1] - l[i][0]
	if len(d):
		currdist += l[i][0] - d[-1][1]
		currrem -= l[i][0] - d[-1][1]
	d.append(l[i])
	ans = max(ans, currdist + currrem)
print(ans)


R = lambda : map(int, input().split())
(n, h) = R()
ps = []
for _ in range(n):
	(l, r) = R()
	ps.append([l, r])
(l, c, res) = (0, 0, 0)
for r in range(n):
	c += ps[r][0] - ps[r - 1][1] if r > 0 else 0
	while c >= h:
		c -= ps[l + 1][0] - ps[l][1]
		l += 1
	res = max(res, ps[r][1] - ps[l][0] + h - c)
print(res)


def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater
(n, m) = map(int, input().split())
a = [0] * n
b = [0] * n
for i in range(n):
	(a[i], b[i]) = map(int, input().split())
h = m
i = 0
ans = 0
while m > 0:
	ans += b[i] - a[i]
	if i == n - 1:
		break
	c = min(m, a[i + 1] - b[i])
	ans += c
	m -= c
	i += 1
if m > 0:
	ans += m
m = 0
tot = [0] * n
tot[0] = ans
for i in range(1, n):
	left = a[i - 1] + tot[i - 1]
	ans -= a[i] - a[i - 1]
	m += a[i] - b[i - 1]
	c = countGreater(a, n, left)
	if c == 0:
		break
	else:
		ie = n - c
		rt = min(m, a[ie] - left)
		m -= rt
		ans += rt
		while m > 0:
			ans += b[ie] - a[ie]
			if ie == n - 1:
				break
			c = min(m, a[ie + 1] - b[ie])
			ans += c
			m -= c
			ie += 1
		if m > 0:
			ans += m
			m = 0
		tot[i] = ans
print(max(tot))


ans = 0
[n, h] = [int(i) for i in input().split()]
arr = [[int(i) for i in input().split()] for j in range(n)]
(l, r, answer, cur) = (0, 0, arr[0][1] - arr[0][0], 0)
arr += [[10 ** 16, 10 ** 16]]
n += 1
while r < n - 1:
	for j in range(r + 1, n):
		if cur + arr[j][0] - arr[j - 1][1] < h:
			r += 1
			cur += arr[j][0] - arr[j - 1][1]
			answer += arr[j][1] - arr[j][0]
		else:
			l += 1
			ans = max(ans, answer)
			cur -= arr[l][0] - arr[l - 1][1]
			answer -= arr[l - 1][1] - arr[l - 1][0]
			break
print(ans + h)


import sys
import math as mt
import bisect
input = sys.stdin.readline
t = 1
for _ in range(t):
	(n, h) = map(int, input().split())
	pref = [0] * n
	l = []
	d = {}
	for i in range(n):
		l.append(list(map(int, input().split())))
		if i > 0:
			pref[i] = pref[i - 1] + (l[i][0] - l[i - 1][1])
		d[pref[i]] = i
	ans = 0
	for i in range(n):
		pos = bisect.bisect_left(pref, h + pref[i])
		pos -= 1
		if pos < n:
			ans = max(ans, l[pos][1] - l[i][0] + (h + pref[i] - pref[pos]))
		else:
			ans = max(ans, l[-1][1] - l[i][0] + (h + pref[i] - pref[-1]))
	print(ans)