"Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter.
Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories.
Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter.
Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.

-----Constraints-----
 - 1 ≤ N ≤ 10^5
 - 2 ≤ C ≤ 10^{14}
 - 1 ≤ x_1 < x_2 < ... < x_N < C
 - 1 ≤ v_i ≤ 10^9
 - All values in input are integers.

-----Subscores-----
 - 300 points will be awarded for passing the test set satisfying N ≤ 100.

-----Input-----
Input is given from Standard Input in the following format:
N C
x_1 v_1
x_2 v_2
:
x_N v_N

-----Output-----
If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c.

-----Sample Input-----
3 20
2 80
9 120
16 1

-----Sample Output-----
191

There are three sushi on the counter with a circumference of 20 meters. If he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks seven more meters clockwise, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 9 kilocalories, thus he can take in 191 kilocalories on balance, which is the largest possible value.

Difficulty: MEDIUM_HARD

Starter code:

Solutions:
import numpy as np
(N, C) = list(map(int, input().split()))
xv = [(0, 0)]
for i in range(N):
	(x, v) = list(map(float, input().split()))
	xv.append((x, v))
sumA = np.zeros(N + 2, dtype=float)
sumB = np.zeros(N + 2, dtype=float)
xv.sort(key=lambda tup: tup[0])
xv.append((C, 0))
for i in range(N + 1):
	sumA[i + 1] = sumA[i] + xv[i + 1][1]
	sumB[i + 1] = sumB[i] + xv[N - i][1]
maxA = [0] * (N + 1)
maxB = [0] * (N + 1)
for i in range(N):
	if sumA[i + 1] < xv[i + 1][0]:
		sumA[i + 1] = 0
	else:
		sumA[i + 1] -= xv[i + 1][0]
	if sumB[i + 1] < C - xv[N - i][0]:
		sumB[i + 1] = 0
	else:
		sumB[i + 1] -= C - xv[N - i][0]
	maxA[i + 1] = maxA[i]
	if sumA[i + 1] > maxA[i]:
		maxA[i + 1] = sumA[i + 1]
	maxB[i + 1] = maxB[i]
	if sumB[i + 1] > maxB[i]:
		maxB[i + 1] = sumB[i + 1]
ans = 0
for i in range(N):
	valB = -xv[i][0] + maxB[N - i]
	if valB < 0:
		ans = max(ans, sumA[i])
	else:
		ans = max(ans, sumA[i] + valB)
	valA = -C + xv[N - i + 1][0] + maxA[N - i]
	if valA < 0:
		ans = max(ans, sumB[i])
	else:
		ans = max(ans, sumB[i] + valA)
print(int(ans))


import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007

def calc_max(A):
	Amax = [0] * len(A)
	cal = 0
	for (i, (x, v)) in enumerate(A):
		cal += v
		if i == 0:
			Amax[i] = max(cal - x, 0)
		else:
			Amax[i] = max(cal - x, Amax[i - 1])
	return Amax

def solve(A, Amax, B, Bmax, ans):
	N = len(A)
	for i in range(N - 1):
		if ans < Amax[i] - A[i][0] + Bmax[N - i - 2]:
			ans = Amax[i] - A[i][0] + Bmax[N - i - 2]
	return ans

def main():
	(N, C, *XV) = list(map(int, read().split()))
	A = [0] * N
	for (i, (x, v)) in enumerate(zip(*[iter(XV)] * 2)):
		A[i] = (x, v)
	Amax = calc_max(A)
	B = [(C - x, v) for (x, v) in reversed(A)]
	Bmax = calc_max(B)
	ans = max(Amax[-1], Bmax[-1])
	ans = max(ans, solve(A, Amax, B, Bmax, ans))
	ans = max(ans, solve(B, Bmax, A, Amax, ans))
	print(ans)
	return

def __starting_point():
	main()
__starting_point()


import sys
input = sys.stdin.readline
(N, C) = map(int, input().split())
XV = [tuple(map(int, input().split())) for _ in range(N)]
(VF, VB) = ([0] * (N + 1), [0] * (N + 1))
for i in range(N):
	VF[i + 1] += VF[i] + XV[i][1]
for i in range(N):
	VB[N - i - 1] += VB[N - i] + XV[N - 1 - i][1]
for i in range(N):
	VF[i + 1] -= XV[i][0]
for i in range(N):
	VB[N - i - 1] -= C - XV[N - 1 - i][0]
for i in range(N):
	if VF[i + 1] < VF[i]:
		VF[i + 1] = VF[i]
for i in range(N):
	if VB[N - 1 - i] < VB[N - i]:
		VB[N - 1 - i] = VB[N - i]
ans = 0
ans = max(ans, VF[N], VB[0])
for i in range(1, N):
	ans = max(ans, VF[i], VF[i] + VB[i] - XV[i - 1][0])
for i in range(1, N):
	ans = max(ans, VB[i], VB[i] + VF[i] - (C - XV[i][0]))
print(ans)


(N, C) = list(map(int, input().split()))
xv = [list(map(int, input().split())) for _ in range(N)]
xinc = [0] * N
xinc2 = [0] * N
xdec = [0] * N
xdec2 = [0] * N
nowx = 0
nowv = 0
nowxin = C
nowvin = 0
for i in range(N):
	(xi, vi) = xv[i]
	nowv = nowv - (xi - nowx) + vi
	xinc[i] = nowv
	xinc2[i] = nowv - xi
	nowx = xi
	(xin, vin) = xv[N - 1 - i]
	nowvin = nowvin - (nowxin - xin) + vin
	xdec[i] = nowvin
	xdec2[i] = nowvin - (C - xin)
	nowxin = xin
mxinc2 = [0] * N
mxdec2 = [0] * N
for i in range(1, N):
	mxinc2[i] = max(mxinc2[i - 1], xinc2[i - 1])
	mxdec2[i] = max(mxdec2[i - 1], xdec2[i - 1])
ans = 0
for i in range(N):
	ans = max(ans, xinc[i], xdec[i])
	ans = max(ans, mxinc2[i] + xdec[N - 1 - i])
	ans = max(ans, mxdec2[i] + xinc[N - 1 - i])
print(ans)


from itertools import accumulate
(n, c) = map(int, input().split())
wv = [[0, 0]] + [list(map(int, input().split())) for i in range(n)] + [[c, 0]]
n += 2
ansls = []
ls1 = [0] * n
ls2 = [0] * n
ls3 = [0] * n
ls4 = [0] * n
for i in range(1, n):
	(w, v) = wv[i]
	w -= wv[i - 1][0]
	ls1[i] = v - w * 2
	ls2[i] = v - w
for i in range(n - 2, -1, -1):
	(w, v) = wv[i]
	w -= wv[i + 1][0]
	ls3[n - i - 1] = v + w * 2
	ls4[n - i - 1] = v + w
als1 = list(accumulate(ls1))
als2 = list(accumulate(ls2))
als3 = list(accumulate(ls3))
als4 = list(accumulate(ls4))
als1 = list(accumulate(als1, max))
als2 = list(accumulate(als2, max))
als3 = list(accumulate(als3, max))
als4 = list(accumulate(als4, max))
for i in range(n - 1):
	ansls.append(als1[i] + als4[n - 2 - i])
	ansls.append(als2[i] + als3[n - 2 - i])
print(max(ansls))


def main():
	from numpy import zeros
	from sys import stdin

	def input():
		return stdin.readline().strip()
	(n, c) = list(map(int, input().split()))
	(x, v) = (zeros(n + 2, dtype=int), zeros(n + 2, dtype=int))
	for i in range(n):
		(j, k) = list(map(int, input().split()))
		x[i + 1] = j
		v[i + 1] = v[i] + k
	x[n + 1] = c
	v[n + 1] = v[n]
	max_nut_clockwise = zeros(n + 1, dtype=int)
	for i in range(1, n + 1):
		max_nut_clockwise[i] = max(max_nut_clockwise[i - 1], v[i] - x[i])
	max_nut_counterclockwise = zeros(n + 1, dtype=int)
	for i in range(n - 1, -1, -1):
		max_nut_counterclockwise[i] = max(max_nut_counterclockwise[i + 1], v[-1] - v[i] - c + x[i + 1])
	ans = 0
	for i in range(n):
		if ans < v[i] - 2 * x[i] + max_nut_counterclockwise[i]:
			ans = v[i] - 2 * x[i] + max_nut_counterclockwise[i]
	for i in range(2, n + 2):
		if ans < v[-1] - v[i - 1] - 2 * (c - x[i]) + max_nut_clockwise[i - 1]:
			ans = v[-1] - v[i - 1] - 2 * (c - x[i]) + max_nut_clockwise[i - 1]
	print(ans)
main()


def main():
	(n, c) = map(int, input().split())
	sushi = [None] * n
	for i in range(n):
		sushi[i] = list(map(int, input().split()))
	cal_r = [None] * n
	cal_l = [None] * n
	max_r = [None] * n
	max_l = [None] * n
	cal_r[0] = sushi[0][1] - sushi[0][0]
	max_r[0] = cal_r[0]
	for i in range(1, n):
		cal_r[i] = cal_r[i - 1] + sushi[i][1] - (sushi[i][0] - sushi[i - 1][0])
		max_r[i] = max(max_r[i - 1], cal_r[i])
	cal_l[n - 1] = sushi[n - 1][1] - (c - sushi[n - 1][0])
	max_l[n - 1] = cal_l[n - 1]
	for i in reversed(range(n - 1)):
		cal_l[i] = cal_l[i + 1] + sushi[i][1] - (sushi[i + 1][0] - sushi[i][0])
		max_l[i] = max(max_l[i + 1], cal_l[i])
	max_r_1 = max(max_r)
	max_l_1 = max(max_l)
	ans = max(0, max_r_1, max_l_1)
	cal_r_2 = [None] * n
	cal_l_2 = [None] * n
	cal_r_2[n - 1] = 0
	cal_l_2[0] = 0
	for i in range(n - 1):
		cal_r_2[i] = cal_r[i] - sushi[i][0] + max_l[i + 1]
	for i in reversed(range(1, n)):
		cal_l_2[i] = cal_l[i] - (c - sushi[i][0]) + max_r[i - 1]
	max_r_2 = max(cal_r_2)
	max_l_2 = max(cal_l_2)
	ans = max(ans, max_l_2, max_r_2)
	print(ans)

def __starting_point():
	main()
__starting_point()


(N, C) = list(map(int, input().split()))
x = [None] * N
v = [None] * N
for i in range(N):
	(x[i], v[i]) = list(map(int, input().split()))
a0 = [None] * N
a = v[0] - x[0]
a0[0] = max(0, a)
for i in range(1, N):
	a += v[i] - (x[i] - x[i - 1])
	a0[i] = max(a, a0[i - 1])
a1 = [None] * N
a = v[N - 1] - (C - x[N - 1])
a1[0] = max(0, a)
for i in range(1, N):
	a += v[N - 1 - i] - (C - x[N - 1 - i] - (C - x[N - 1 - (i - 1)]))
	a1[i] = max(a, a1[i - 1])
result = -1
if a0[N - 1] > result:
	result = a0[N - 1]
if a1[N - 1] > result:
	result = a1[N - 1]
for i in range(N - 1):
	t = a0[i] - x[i] + a1[N - 1 - (i + 1)]
	if t > result:
		result = t
for i in range(N - 1):
	t = a1[i] - (C - x[N - 1 - i]) + a0[N - 1 - (i + 1)]
	if t > result:
		result = t
print(result)


(n, c) = list(map(int, input().split()))
x_list = []
v_list = []
one_list = []
round_list = []
r_one_list = []
r_round_list = []
total = 0
for i in range(n):
	(x, v) = list(map(int, input().split()))
	x_list.append(x)
	v_list.append(v)
	total = total + v
	one_list.append(total - x)
	round_list.append(total - 2 * x)
prev = 0
for i in range(n):
	if prev > one_list[i]:
		one_list[i] = prev
	else:
		prev = one_list[i]
prev = 0
for i in range(n):
	if prev > round_list[i]:
		round_list[i] = prev
	else:
		prev = round_list[i]
total = 0
for i in range(1, n + 1):
	total = total + v_list[-i]
	r_one_list.append(total - (c - x_list[-i]))
	r_round_list.append(total - 2 * (c - x_list[-i]))
prev = 0
for i in range(n):
	if prev > r_one_list[i]:
		r_one_list[i] = prev
	else:
		prev = r_one_list[i]
prev = 0
for i in range(n):
	if prev > r_round_list[i]:
		r_round_list[i] = prev
	else:
		prev = r_round_list[i]
ret = 0
for i in range(n - 1):
	tmp = max(one_list[i] + r_round_list[-i - 2], round_list[i] + r_one_list[-i - 2])
	if tmp > ret:
		ret = tmp
ret = max(one_list[-1], r_one_list[-1], ret)
print(ret)


from itertools import accumulate
(n, c) = list(map(int, input().split()))
(xs, vs) = ([], [])
for _ in range(n):
	(x, v) = list(map(int, input().split()))
	xs.append(x)
	vs.append(v)
xs = [0] + xs + [c]
vs = [0] + vs + [0]

def maxcalrrl(xs=xs, vs=vs):
	avsr = list(accumulate(reversed(vs)))[::-1]
	calr = [a - 2 * x for (x, a) in zip(xs, accumulate(vs))]
	call = [a - (c - x) for (x, a) in zip(xs, avsr)]
	maxcalr = list(accumulate(calr, max))
	return max([call[i + 1] + maxcalr[i] for i in range(n + 1)])

def maxcalrll(xs=xs, vs=vs):
	xsr = [c - x for x in xs][::-1]
	vsr = vs[::-1]
	return maxcalrrl(xsr, vsr)
answer = max([maxcalrrl(), maxcalrll()])
print(answer)


(n, c) = list(map(int, input().split()))
XV = [tuple(map(int, input().split())) for i in range(n)]
X = [x for (x, v) in XV]
V = [v for (x, v) in XV]
S = [0]
for i in range(n):
	S.append(S[-1] + V[i])
A = [0]
for i in range(1, n + 1):
	A.append(max(A[-1], S[i] - 2 * X[i - 1]))
B = []
for i in range(n):
	B.append(S[n] - S[i] - (c - X[i]))
B.append(0)
ans0 = max([A[i] + B[i] for i in range(n + 1)])
A = [0]
for i in range(1, n + 1):
	A.append(max(A[-1], S[i] - X[i - 1]))
B = []
for i in range(n):
	B.append(S[n] - S[i] - 2 * (c - X[i]))
B.append(0)
ans1 = max([A[i] + B[i] for i in range(n + 1)])
print(max(ans0, ans1))


(n, c) = list(map(int, input().split()))
x = []
v = []
for i in range(n):
	(xi, vi) = list(map(int, input().split()))
	x.append(xi)
	v.append(vi)
l1 = [0]
l2 = [0]
r1 = [0]
r2 = [0]
vtotal = 0
for i in range(n):
	vtotal += v[i]
	l1.append(max(l1[-1], vtotal - x[i]))
	l2.append(max(l2[-1], vtotal - 2 * x[i]))
vtotal = 0
for i in range(n - 1, -1, -1):
	vtotal += v[i]
	r1.append(max(r1[-1], vtotal - (c - x[i])))
	r2.append(max(r2[-1], vtotal - 2 * (c - x[i])))
ans = 0
for i in range(n + 1):
	ans = max(ans, l1[i] + r2[n - i], l2[i] + r1[n - i])
print(ans)


(n, c) = list(map(int, input().split()))
xv = [list(map(int, input().split())) for _ in range(n)]
cw = []
cw2 = [0]
v_sm = 0
for (x, v) in xv:
	v_sm += v
	cw.append(v_sm - x)
	cw2.append(v_sm - 2 * x)
ccw = []
ccw2 = [0]
v_sm = 0
for (x, v) in xv[::-1]:
	v_sm += v
	ccw.append(v_sm - (c - x))
	ccw2.append(v_sm - 2 * (c - x))
cw_mx = [0]
ccw_mx = [0]
for e in cw:
	cw_mx.append(max(cw_mx[-1], e))
for e in ccw:
	ccw_mx.append(max(ccw_mx[-1], e))
ans = 0
for (i, e) in enumerate(cw2):
	mx = e + ccw_mx[n - i]
	ans = max(ans, mx)
for (i, e) in enumerate(ccw2):
	mx = e + cw_mx[n - i]
	ans = max(ans, mx)
print(ans)


def main():
	(N, C) = list(map(int, input().split()))
	XV = [tuple(map(int, input().split())) for _ in range(N)]
	return max(solve(N, C, XV), solve(N, C, [(C - x, v) for (x, v) in reversed(XV)]))

def solve(N, C, XV):
	lv = [0] * N
	rv = [0] * N
	(x0, c, m) = (0, 0, 0)
	for (i, (x, v)) in enumerate(XV):
		c += v - (x - x0)
		m = max(m, c)
		lv[i] = m
		x0 = x
	rv = [0] * N
	(x0, c, m) = (C, 0, 0)
	for (i, (x, v)) in enumerate(reversed(XV)):
		c += v - 2 * (x0 - x)
		m = max(m, c)
		rv[i] = m
		x0 = x
	for i in range(N - 1):
		lv[i] += rv[N - i - 2]
	return max(lv)
print(main())


(n, C) = list(map(int, input().split()))
que = []
for _ in range(n):
	(x, v) = list(map(int, input().split()))
	que.append((x, v))
dpl = [0 for i in range(n + 1)]
dpr = [0 for i in range(n + 1)]
c = 0
for i in range(n):
	c += que[i][1]
	dpl[i + 1] = c - que[i][0]
d = 0
for i in range(1, n + 1):
	d += que[-i][1]
	dpr[i] = d - (C - que[-i][0])
max1 = max(dpl)
max2 = max(dpr)
dpb_l = [0 for i in range(n + 1)]
dpb_r = [0 for i in range(n + 1)]
c = 0
for i in range(n):
	c += que[i][1]
	dpb_l[i + 1] = c - 2 * que[i][0]
d = 0
for i in range(1, n + 1):
	d += que[-i][1]
	dpb_r[i] = d - 2 * (C - que[-i][0])
for i in range(1, n + 1):
	dpl[i] = max(dpl[i - 1], dpl[i])
for i in range(1, n + 1):
	dpr[i] = max(dpr[i], dpr[i - 1])
max3 = 0
for i in range(1, n):
	max3 = max(max3, dpb_l[i] + dpr[n - i], dpb_r[i] + dpl[n - i])
print(max(max1, max2, max3))


def main():
	from collections import namedtuple
	import sys
	input = sys.stdin.readline
	Sushi = namedtuple('Sushi', 'x cal')
	(n, c) = list(map(int, input().split()))
	a = []
	for _ in range(n):
		(x, v) = list(map(int, input().split()))
		a.append(Sushi(x=x, cal=v))
	clock = [0] * (n + 1)
	clock_to_0 = [0] * (n + 1)
	ma = 0
	ma0 = 0
	curr = 0
	for (i, s) in enumerate(a, start=1):
		curr += s.cal
		ma = max(ma, curr - s.x)
		ma0 = max(ma0, curr - s.x * 2)
		clock[i] = ma
		clock_to_0[i] = ma0
	anti = [0] * (n + 1)
	anti_to_0 = [0] * (n + 1)
	ma = 0
	ma0 = 0
	curr = 0
	for (i, s) in zip(list(range(n, -1, -1)), reversed(a)):
		curr += s.cal
		ma = max(ma, curr - (c - s.x))
		ma0 = max(ma0, curr - (c - s.x) * 2)
		anti[i] = ma
		anti_to_0[i] = ma0
	ans = 0
	for exit_pos in range(1, n + 1):
		ans = max(ans, clock_to_0[exit_pos - 1] + anti[exit_pos], anti_to_0[(exit_pos + 1) % (n + 1)] + clock[exit_pos])
	print(ans)

def __starting_point():
	main()
__starting_point()


(n, c) = map(int, input().split())
xv = []
for _ in range(n):
	(x, v) = map(int, input().split())
	xv.append((x, v))
r1 = [0]
r2 = [0]
px = 0
for (x, v) in xv:
	r1.append(r1[-1] + v - (x - px))
	r2.append(r2[-1] + v - 2 * (x - px))
	px = x
l1 = [0]
l2 = [0]
px = 0
for (x, v) in reversed(xv):
	x = c - x
	l1.append(l1[-1] + v - (x - px))
	l2.append(l2[-1] + v - 2 * (x - px))
	px = x

def accu_max(li):
	mx = -1
	for i in range(n + 1):
		if li[i] > mx:
			mx = li[i]
		else:
			li[i] = mx
accu_max(r1)
accu_max(r2)
accu_max(l1)
accu_max(l2)
ans = 0
for i in range(n + 1):
	ans = max(ans, r1[i] + l2[n - i])
	ans = max(ans, l1[i] + r2[n - i])
print(ans)


from itertools import accumulate
(N, C) = map(int, input().split())
XV = [tuple(map(int, input().split())) for _ in range(N)]
eat = 0
walk = 0
now = 0
clockwise = [(0, 0)]
for (x, v) in XV:
	eat += v
	walk += x - now
	clockwise.append((eat, walk))
	now = x
eat = 0
walk = 0
now = C
counter_clockwise = [(0, 0)]
for (x, v) in XV[::-1]:
	eat += v
	walk += now - x
	counter_clockwise.append((eat, walk))
	now = x
best_left_go = [e - w for (e, w) in counter_clockwise]
best_left_go = list(accumulate(best_left_go, func=max))
best_left_back = [e - 2 * w for (e, w) in counter_clockwise]
best_left_back = list(accumulate(best_left_back, func=max))
best_right_go = [e - w for (e, w) in clockwise]
best_right_go = list(accumulate(best_right_go, func=max))
best_right_back = [e - 2 * w for (e, w) in clockwise]
best_right_back = list(accumulate(best_right_back, func=max))
ans = 0
for (i, rb) in enumerate(best_right_back):
	calorie = rb + best_left_go[N - i]
	ans = max(ans, calorie)
for (i, lb) in enumerate(best_left_back):
	calorie = lb + best_right_go[N - i]
	ans = max(ans, calorie)
print(ans)


(n, c) = map(int, input().split())
x = [0]
v = [0]
su = [0] * (n + 1)
for i in range(n):
	(xx, vv) = map(int, input().split())
	x.append(xx)
	v.append(vv)
	su[i + 1] = su[i] + v[i + 1]
lmax = [0] * (n + 2)
rmax = [0] * (n + 2)
for i in range(n):
	lmax[i + 1] = max(lmax[i], su[i + 1] - x[i + 1])
	rmax[n - i] = max(rmax[n - i + 1], su[n] - su[n - i - 1] - (c - x[n - i]))
ans = 0
for i in range(n):
	ans = max(ans, su[i + 1] - 2 * x[i + 1] + rmax[i + 2])
	ans = max(ans, su[n] - su[n - i - 1] - 2 * (c - x[n - i]) + lmax[n - i - 1])
ans = max(ans, max(lmax[n], rmax[1]))
print(ans)


(N, C) = map(int, input().split())
xv = [list(map(int, input().split())) for i in range(N)]
A1 = [0, 0]
X1 = [0]
A2 = [0, 0]
X2 = [0]
for (x, v) in xv:
	A1[-1] += v - x
	A1.append(A1[-1] + x)
	X1.append(x)
A1.pop()
xv = xv[::-1]
for (x, v) in xv:
	A2[-1] += v - (C - x)
	A2.append(A2[-1] + (C - x))
	X2.append(C - x)
A2.pop()
max_1 = []
max_2 = []
now = -1
for (i, a) in enumerate(A1):
	if a > now:
		max_1.append(i)
		now = a
	else:
		max_1.append(max_1[-1])
now = -1
for (i, a) in enumerate(A2):
	if a > now:
		max_2.append(i)
		now = a
	else:
		max_2.append(max_2[-1])
ans = 0
for (i, a) in enumerate(A1):
	idx = max_2[-i - 1]
	calory = max(a, a + A2[idx] - X1[i])
	ans = max(ans, calory)
for (i, a) in enumerate(A2):
	idx = max_1[-i - 1]
	calory = max(a, a + A1[idx] - X2[i])
	ans = max(ans, calory)
print(ans)


(n, c) = map(int, input().split())
XV = [list(map(int, input().split())) for _ in range(n)]
(X, V) = zip(*XV)
dp_I_r = [0] * n
dp_U_r = [0] * n
dp_I_l = [0] * n
dp_U_l = [0] * n
dp_I_r[0] = V[0] - X[0]
dp_U_r[0] = V[0] - 2 * X[0]
dp_I_l[n - 1] = V[n - 1] - (c - X[n - 1])
dp_U_l[n - 1] = V[n - 1] - 2 * (c - X[n - 1])
for i in range(n - 1):
	dp_I_r[i + 1] = dp_I_r[i] + V[i + 1] - (X[i + 1] - X[i])
	dp_U_r[i + 1] = dp_U_r[i] + V[i + 1] - 2 * (X[i + 1] - X[i])
	dp_I_l[n - 2 - i] = dp_I_l[n - 1 - i] + V[n - 2 - i] - (X[n - 1 - i] - X[n - 2 - i])
	dp_U_l[n - 2 - i] = dp_U_l[n - 1 - i] + V[n - 2 - i] - 2 * (X[n - 1 - i] - X[n - 2 - i])
for i in range(n - 1):
	dp_I_r[i + 1] = max(dp_I_r[i], dp_I_r[i + 1])
	dp_U_r[i + 1] = max(dp_U_r[i], dp_U_r[i + 1])
	dp_I_l[n - 2 - i] = max(dp_I_l[n - 1 - i], dp_I_l[n - 2 - i])
	dp_U_l[n - 2 - i] = max(dp_U_l[n - 1 - i], dp_U_l[n - 2 - i])
ans = 0
for i in range(n):
	ans = max(ans, dp_I_r[i])
	ans = max(ans, dp_I_l[i])
	if i < n - 1:
		ans = max(ans, dp_U_r[i] + dp_I_l[i + 1])
	if i > 0:
		ans = max(ans, dp_U_l[i] + dp_I_r[i - 1])
print(ans)


import itertools
from collections import deque, defaultdict, Counter
from itertools import accumulate
import bisect
from heapq import heappop, heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
Mod = 1000000007
import sys
sys.setrecursionlimit(100000)

def sieve_of_eratosthenes(n):
	if not isinstance(n, int):
		raise TypeError('n is not int')
	if n < 2:
		raise ValueError('n is not effective')
	prime = [1] * (n + 1)
	for i in range(2, int(math.sqrt(n)) + 1):
		if prime[i] == 1:
			for j in range(2 * i, n + 1):
				if j % i == 0:
					prime[j] = 0
	res = []
	for i in range(2, n + 1):
		if prime[i] == 1:
			res.append(i)
	return res

def factorial(i):
	if i == 1:
		return 1
	else:
		return i * factorial(i - 1)

class UnionFind:

	def __init__(self, n):
		self.parent = [i for i in range(n + 1)]
		self.rank = [0 for i in range(n + 1)]

	def findroot(self, x):
		if x == self.parent[x]:
			return x
		else:
			y = self.parent[x]
			y = self.findroot(self.parent[x])
			return y

	def union(self, x, y):
		px = self.findroot(x)
		py = self.findroot(y)
		if px < py:
			self.parent[py] = px
		else:
			self.parent[px] = py

	def same_group_or_no(self, x, y):
		return self.findroot(x) == self.findroot(y)

def main():
	(n, c) = list(map(int, input().split()))
	xv = [list(map(int, input().split())) for i in range(n)]
	x = [xv[i][0] for i in range(n)]
	v = [xv[i][1] for i in range(n)]
	(r1, r2, l1, l2) = ([0] * (n + 1), [0] * (n + 1), [0] * (n + 1), [0] * (n + 1))
	ans = 0
	dis = 0
	for i in range(n):
		r1[i + 1] = r1[i] + v[i] - (x[i] - dis)
		r2[i + 1] = r2[i] + v[i] - 2 * (x[i] - dis)
		dis = x[i]
	dis = c
	for i in reversed(list(range(n))):
		l1[n - i] = l1[n - i - 1] + v[i] - (dis - x[i])
		l2[n - i] = l2[n - i - 1] + v[i] - 2 * (dis - x[i])
		dis = x[i]
	for i in range(n):
		l1[i + 1] = max(l1[i + 1], l1[i])
		r1[i + 1] = max(r1[i + 1], r1[i])
	for i in range(n + 1):
		ans = max(ans, max(r2[i] + l1[n - i], l2[i] + r1[n - i]))
	print(ans)

def __starting_point():
	main()
__starting_point()


(n, c) = map(int, input().split())
x = [0] * n
v = [0] * n
for i in range(n):
	(x[i], v[i]) = map(int, input().split())
x1 = [0] + x
v1 = [0] + v
x2 = [0] + list(map(lambda a: c - a, x[::-1]))
v2 = [0] + v[::-1]
lst1 = [0] * (n + 1)
lst2 = [0] * (n + 1)
lst1[0] = lst2[0] = (0, 0)

def f(l, x, v):
	maxcal = pos = sumcal = 0
	for i in range(1, n + 1):
		dist = x[i] - x[i - 1]
		sumcal += v[i] - dist
		if sumcal > maxcal:
			maxcal = sumcal
			pos = x[i]
		l[i] = (maxcal, pos)
f(lst1, x1, v1)
f(lst2, x2, v2)
ans = 0
for i in range(n + 1):
	ans = max(ans, lst1[i][0] + lst2[n - i][0] - min(lst1[i][1], lst2[n - i][1]))
print(ans)


import sys
(N, C) = map(int, sys.stdin.readline().split())
sushi_set = []
for i in range(N):
	(x, v) = map(int, sys.stdin.readline().split())
	sushi_set.append((x, v))
sushi_set.sort()
ans = -1
right = [0 for _ in range(N)]
max_n = -float('inf')
energy = 0
right_back = [0 for _ in range(N)]
for (i, (x, v)) in enumerate(sushi_set):
	energy += v
	if energy - x > max_n:
		max_n = energy - x
	right[i] = max_n
	right_back[i] = energy - 2 * x
left = [0 for _ in range(N)]
max_n = -float('inf')
energy = 0
left_back = [0 for _ in range(N)]
for (i, (x, v)) in enumerate(sushi_set[::-1]):
	energy += v
	if energy - (C - x) > max_n:
		max_n = energy - (C - x)
	left[i] = max_n
	left_back[i] = energy - 2 * (C - x)
ans = max(max(right), max(left))
for i in range(N - 1):
	ans = max(ans, right_back[i] + left[N - 2 - i])
for i in range(N - 1):
	ans = max(ans, left_back[i] + right[N - 2 - i])
print(max(ans, 0))


import sys
readline = sys.stdin.readline

def main():
	(N, C) = map(int, readline().rstrip().split())
	pair = [tuple(map(int, readline().rstrip().split())) for _ in range(N)]
	x_acc_right = []
	(gain, max_gain) = (0, 0)
	for (xi, vi) in pair:
		gain += vi
		max_gain = max(max_gain, gain - xi)
		x_acc_right.append(max_gain)
	x_acc_left = []
	(gain, max_gain) = (0, 0)
	for (xi, vi) in pair[::-1]:
		gain += vi
		max_gain = max(max_gain, gain - (C - xi))
		x_acc_left.append(max_gain)
	res = max(x_acc_right[-1], x_acc_left[-1])
	gain = 0
	for i in range(N - 1):
		(xi, vi) = pair[i]
		gain += vi
		res = max(res, gain - 2 * xi + x_acc_left[N - i - 2])
	gain = 0
	for i in range(N - 1, 0, -1):
		(xi, vi) = pair[i]
		gain += vi
		res = max(res, gain - 2 * (C - xi) + x_acc_right[i - 1])
	print(res)

def __starting_point():
	main()
__starting_point()


import sys
input = sys.stdin.readline
(N, C) = map(int, input().split())
XV = [tuple(map(int, input().split())) for i in range(N)]
rcum1 = [0]
rcum2 = [0]
rcummx = [0]
px = 0
for (x, v) in XV:
	d = x - px
	rcum1.append(rcum1[-1] + v - d)
	rcum2.append(rcum2[-1] + v - 2 * d)
	rcummx.append(max(rcummx[-1], rcum1[-1]))
	px = x
lcum1 = [0]
lcum2 = [0]
lcummx = [0]
px = C
for (x, v) in reversed(XV):
	d = px - x
	lcum1.append(lcum1[-1] + v - d)
	lcum2.append(lcum2[-1] + v - 2 * d)
	lcummx.append(max(lcummx[-1], lcum1[-1]))
	px = x
lcum1.reverse()
lcum2.reverse()
lcummx.reverse()
ans = max(rcummx[-1], lcummx[0])
for i in range(N + 1):
	a = rcum2[i] + lcummx[i]
	b = lcum2[i] + rcummx[i]
	ans = max(ans, a, b)
print(ans)


import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7

class SegTree:

	def __init__(self, init_val, segfunc, ide_ele):
		n = len(init_val)
		self.segfunc = segfunc
		self.ide_ele = ide_ele
		self.num = 1 << (n - 1).bit_length()
		self.tree = [ide_ele] * 2 * self.num
		for i in range(n):
			self.tree[self.num + i] = init_val[i]
		for i in range(self.num - 1, 0, -1):
			self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

	def update(self, k, x):
		k += self.num
		self.tree[k] = x
		while k > 1:
			self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
			k >>= 1

	def query(self, left, right):
		res = self.ide_ele
		left += self.num
		right += self.num
		while left < right:
			if left & 1:
				res = self.segfunc(res, self.tree[left])
				left += 1
			if right & 1:
				res = self.segfunc(res, self.tree[right - 1])
			left >>= 1
			right >>= 1
		return res

def resolve():
	(n, c) = list(map(int, input().split()))
	XY = [list(map(int, input().split())) for _ in range(n)]
	R1 = [0]
	prev = 0
	for (x, y) in XY:
		R1.append(R1[-1] + y - (x - prev))
		prev = x
	R2 = [0]
	prev = 0
	for (x, y) in XY[::-1]:
		R2.append(R2[-1] + y - (c - x - prev))
		prev = c - x
	res = max(max(R1), max(R2))
	seg1 = SegTree(R2, lambda x, y: max(x, y), 0)
	for i in range(1, n + 1):
		point1 = R1[i] - XY[i - 1][0]
		point2 = seg1.query(0, n + 1 - i)
		res = max(res, point1 + point2)
	seg2 = SegTree(R1, lambda x, y: max(x, y), 0)
	for i in range(1, n + 1):
		point1 = R2[i] - (c - XY[-i][0])
		point2 = seg2.query(0, n + 1 - i)
		res = max(res, point1 + point2)
	print(res)

def __starting_point():
	resolve()
__starting_point()


import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7

def resolve():
	(n, c) = list(map(int, input().split()))
	XY = [list(map(int, input().split())) for _ in range(n)]
	(R1, R2) = ([0], [0])
	(max1, max2) = ([0], [0])
	ma = 0
	prev = 0
	for (x, y) in XY:
		R1.append(R1[-1] + y - (x - prev))
		prev = x
		if ma < R1[-1]:
			ma = R1[-1]
		max1.append(ma)
	ma = 0
	prev = 0
	for (x, y) in XY[::-1]:
		R2.append(R2[-1] + y - (c - x - prev))
		prev = c - x
		if ma < R2[-1]:
			ma = R2[-1]
		max2.append(ma)
	res = max(max(R1), max(R2))
	for i in range(1, n + 1):
		point1 = R1[i] - XY[i - 1][0] + max2[n - i]
		point2 = R2[i] - (c - XY[-i][0]) + max1[n - i]
		res = max(res, point1, point2)
	print(res)

def __starting_point():
	resolve()
__starting_point()


(n, c) = map(int, input().split())
xlist = []
for i in range(n):
	(x, v) = map(int, input().split())
	xlist.append((x, v))
fclock = [[0, 0] for _ in range(n)]
(ftemp, fval) = ([0] * n, [0] * n)
for i in range(n):
	if i == 0:
		fval[0] = xlist[i][1]
		if fval[i] - xlist[i][0] < 0:
			fclock[i] = [-1, 0]
		else:
			fclock[i] = [0, fval[0] - xlist[i][0]]
	else:
		fval[i] = fval[i - 1] + xlist[i][1]
		if fclock[i - 1][1] < fval[i] - xlist[i][0]:
			fclock[i] = [i, fval[i] - xlist[i][0]]
		else:
			fclock[i] = fclock[i - 1]
fret = [xlist[i][0] for i in range(n)]
rxlist = []
for i in range(n - 1, -1, -1):
	rxlist.append([c - xlist[i][0], xlist[i][1]])
rclock = [[0, 0] for _ in range(n)]
(rtemp, rval) = ([0] * n, [0] * n)
for i in range(n):
	if i == 0:
		rval[0] = rxlist[i][1]
		if rval[i] - rxlist[i][0] < 0:
			rclock[i] = [-1, 0]
		else:
			rclock[i] = [0, rval[0] - rxlist[i][0]]
	else:
		rval[i] = rval[i - 1] + rxlist[i][1]
		if rclock[i - 1][1] < rval[i] - rxlist[i][0]:
			rclock[i] = [i, rval[i] - rxlist[i][0]]
		else:
			rclock[i] = rclock[i - 1]
rret = [rxlist[i][0] for i in range(n)]
gmax = max(rclock[n - 1][1], fclock[n - 1][1])
for i in range(n - 1):
	tempg = fclock[i][1] + rclock[n - 2 - i][1]
	if fclock[i][0] < 0 or rclock[n - 2 - i][0] < 0:
		pass
	else:
		tempg -= min(fret[fclock[i][0]], rret[rclock[n - 2 - i][0]])
	gmax = max(gmax, tempg)
print(gmax)


(n, c) = list(map(int, input().split()))
x = [0] * (n + 2)
v = [0] * (n + 2)
for i in range(1, n + 1):
	(x[i], v[i]) = list(map(int, input().split()))
x[n + 1] = c
dp = [[0] * 100100 for _ in range(2)]
ans = 0
cal = 0
for i in range(1, n + 1):
	cal -= x[i] - x[i - 1]
	cal += v[i]
	dp[0][i] = max(dp[0][i - 1], cal)
cal = 0
for i in range(n, 0, -1):
	cal -= x[i + 1] - x[i]
	cal += v[i]
	dp[1][i] = max(dp[1][i + 1], cal)
ans = 0
for i in range(n + 1):
	cal = dp[0][i] + dp[1][i + 1] - x[i]
	ans = max(ans, cal)
	cal = dp[0][i] + dp[1][i + 1] - c + x[i + 1]
	ans = max(ans, cal)
print(ans)


from itertools import *
(n, c, *u) = map(int, open(0).read().split())
(x, a) = (u[::2] + [c], [0] + list(accumulate(u[1::2])))
ans = l = r = 0
for b in range(n + 1):
	(t, d, l, r) = (a[n] - a[b], c - x[b], max(l, a[b] - x[b - 1]), max(r, a[b] - 2 * x[b - 1]))
	ans = max(ans, l + t - 2 * d, r + t - d)
print(ans)


from itertools import accumulate
(N, C, *xv) = list(map(int, open(0).read().split()))
xv = [(x, v) for (x, v) in zip(*[iter(xv)] * 2)]
cw_acc = [0] * (N + 1)
ccw_acc = [0] * (N + 1)
cw_prev = 0
ccw_prev = C
for i in range(N):
	k = N - i - 1
	cw_acc[i + 1] = cw_acc[i] + xv[i][1] - (xv[i][0] - cw_prev)
	cw_prev = xv[i][0]
	ccw_acc[i + 1] = ccw_acc[i] + xv[k][1] - (ccw_prev - xv[k][0])
	ccw_prev = xv[k][0]
cw_acc = list(accumulate(cw_acc, max))
ccw_acc = list(accumulate(ccw_acc, max))
ans = 0
for i in range(N):
	k = N - i - 1
	ans = max(ans, cw_acc[i + 1], cw_acc[i + 1] - xv[i][0] + ccw_acc[k], ccw_acc[i + 1], ccw_acc[i + 1] - (C - xv[k][0]) + cw_acc[k])
print(ans)


def main():
	import sys
	input = sys.stdin.readline
	l = [[0, 0, 0]]
	r = [[0, 0, 0]]
	(n, c) = map(int, input().split())
	for i in range(n):
		(x, v) = map(int, input().split())
		r.append([x, v])
		l.append([c - x, v])
	r.sort()
	l.sort()
	for x in (l, r):
		now = 0
		for i in range(n):
			now += x[i + 1][1]
			x[i + 1].append(max(now - x[i + 1][0], x[i][2]))
			if x[i + 1][2] == x[i][2]:
				x[i + 1][0] = x[i][0]
	ans = 0
	for i in range(n + 1):
		a = l[i][0]
		b = r[n - i][0]
		ans = max(l[i][2] + r[n - i][2] - min(a, b), ans)
	print(ans)

def __starting_point():
	main()
__starting_point()


import itertools
(N, C) = list(map(int, input().split()))
X = []
V = []
for n in range(N):
	(x, v) = list(map(int, input().split()))
	X.append(x)
	V.append(v)
Xr = X[::-1]
Vr = V[::-1]
Xl = [0] + X
Vl = [0] + V
Xr = [0] + [C - x for x in Xr]
Vr = [0] + Vr
Vl = list(itertools.accumulate(Vl))
Vr = list(itertools.accumulate(Vr))

def update_mx(ls):
	mx = 0
	for i in range(len(ls)):
		mx = max(mx, ls[i])
		ls[i] = mx
	return ls

def create(V, X, k):
	ls = []
	for n in range(N + 1):
		ls.append(V[n] - k * X[n])
	return update_mx(ls)
L = create(Vl, Xl, 1)
Lt = create(Vl, Xl, 2)
R = create(Vr, Xr, 1)
Rt = create(Vr, Xr, 2)
ans = 0
for n in range(N + 1):
	ans = max(ans, Lt[n] + R[N - n])
	ans = max(ans, Rt[n] + L[N - n])
print(ans)


(N, C) = map(int, input().split())
XV = []
for _ in range(N):
	(x, v) = map(int, input().split())
	XV.append((x, v))
R = [0]
Rr = [0]
for (x, v) in XV:
	R.append(R[-1] + v)
	Rr.append(Rr[-1] + v)
for (i, (x, v)) in enumerate(XV):
	R[i + 1] -= x
	Rr[i + 1] -= 2 * x
for i in range(N):
	Rr[i + 1] = max(Rr[i + 1], Rr[i])
L = [0]
Lr = [0]
for (x, v) in reversed(XV):
	L.append(L[-1] + v)
	Lr.append(Lr[-1] + v)
for (i, (x, v)) in enumerate(reversed(XV)):
	L[i + 1] -= C - x
	Lr[i + 1] -= 2 * (C - x)
for i in range(N):
	Lr[i + 1] = max(Lr[i + 1], Lr[i])
ans = 0
for i in range(1, N + 1):
	temp = max(R[i], R[i] + Lr[N - i], L[N + 1 - i], L[N + 1 - i] + Rr[i - 1])
	ans = max(ans, temp)
print(ans)


import sys
read = sys.stdin.readline
ra = range
enu = enumerate

def read_ints():
	return list(map(int, read().split()))

def read_col(H):
	ret = []
	for _ in range(H):
		ret.append(list(map(int, read().split())))
	return tuple(map(list, list(zip(*ret))))
MOD = 10 ** 9 + 7
INF = 2 ** 31
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations, accumulate
from bisect import bisect_left, bisect_right
(N, C) = read_ints()
(X, V) = read_col(N)

def solve_max_a_plas_b(A: list, B: list):
	assert len(A) == len(B)
	A_accum = list(accumulate(A, func=max))
	B_accum = list(accumulate(reversed(B), func=max))[::-1]
	ret = 0
	for i in range(len(A_accum) - 1):
		ret = max(A_accum[i] + B_accum[i + 1], ret)
	return ret

def ret_candi(X, V):
	V_accum = list(accumulate(V))
	V_accum_r = list(accumulate(reversed(V)))[::-1]
	(A, B) = ([], [])
	katamiti_max = 0
	for i in range(N):
		katamiti_max = max(katamiti_max, V_accum[i] - X[i])
		A.append(V_accum[i] - 2 * X[i])
		B.append(V_accum_r[i] - (C - X[i]))
	return (A, B, katamiti_max)

def solve(X, V):
	(A, B, katamiti_max) = ret_candi(X, V)
	return max(solve_max_a_plas_b(A, B), katamiti_max)
V_r = V[::-1]
X_r = []
for x in reversed(X):
	X_r.append(C - x)
print(max(solve(X, V), solve(X_r, V_r)))


(n, c) = map(int, input().split())
(x, v) = ([0], [0])
for _ in range(n):
	(a, b) = map(int, input().split())
	x.append(a)
	v.append(b)
x.append(c)
v.append(0)
(fr1, fr2) = ([0 for _ in range(n + 1)], [0 for _ in range(n + 1)])
(fl1, fl2) = ([0 for _ in range(n + 2)], [0 for _ in range(n + 2)])
(tmp1, tmp2) = (0, 0)
for i in range(0, n):
	tmp1 += v[i + 1] - (x[i + 1] - x[i])
	tmp2 += v[i + 1] - 2 * (x[i + 1] - x[i])
	fr1[i + 1] = max(tmp1, fr1[i])
	fr2[i + 1] = max(tmp2, fr2[i])
(tmp1, tmp2) = (0, 0)
for i in range(n, 0, -1):
	tmp1 += v[i] - abs(x[i + 1] - x[i])
	tmp2 += v[i] - 2 * abs(x[i + 1] - x[i])
	fl1[i] = max(tmp1, fl1[i + 1])
	fl2[i] = max(tmp2, fl2[i + 1])
ans = 0
for i in range(n + 1):
	ans = max(ans, fr1[i] + fl2[i + 1], fr2[i] + fl1[i + 1])
print(ans)


import sys
readline = sys.stdin.readline
(N, C) = map(int, readline().split())
sushi = [list(map(int, readline().split())) for i in range(N)]
import copy
revsushi = copy.deepcopy(sushi)[::-1]
for i in range(len(sushi) - 1, 0, -1):
	sushi[i][0] = sushi[i][0] - sushi[i - 1][0]
for i in range(len(revsushi)):
	revsushi[i][0] = C - revsushi[i][0]
for i in range(len(revsushi) - 1, 0, -1):
	revsushi[i][0] = revsushi[i][0] - revsushi[i - 1][0]
left = [[0] * 2 for i in range(N + 1)]
right = [[0] * 2 for i in range(N + 1)]
left[0] = [0, 0]
right[0] = [0, 0]
for i in range(len(sushi)):
	(x, v) = sushi[i]
	left[i + 1][0] = left[i][0] + v - x
	if left[i + 1][0] > left[i][1]:
		left[i + 1][1] = left[i + 1][0]
	else:
		left[i + 1][1] = left[i][1]
best = left[-1][1]
cal = 0
for i in range(len(revsushi)):
	count = i + 1
	lp = left[N - count][1]
	(x, v) = revsushi[i]
	cal += v - x * 2
	if lp + cal > best:
		best = lp + cal
for i in range(len(revsushi)):
	(x, v) = revsushi[i]
	right[i + 1][0] = right[i][0] + v - x
	if right[i + 1][0] > right[i][1]:
		right[i + 1][1] = right[i + 1][0]
	else:
		right[i + 1][1] = right[i][1]
best = max(best, right[-1][1])
cal = 0
for i in range(len(sushi)):
	count = i + 1
	rp = right[N - count][1]
	(x, v) = sushi[i]
	cal += v - x * 2
	if rp + cal > best:
		best = rp + cal
print(best)


(n, c) = list(map(int, input().split()))
(x, v) = ([], [])
for i in range(n):
	(a, b) = list(map(int, input().split()))
	x.append(a)
	v.append(b)
cw = [0] * (n + 1)
ccw = [0] * (n + 1)
for i in range(n):
	cw[i + 1] = cw[i] + v[i]
	ccw[i + 1] = ccw[i] + v[-i - 1]
for i in range(1, n + 1):
	cw[i] -= x[i - 1]
	ccw[i] -= c - x[-i]
maxcw = [0] * (n + 1)
maxccw = [0] * (n + 1)
for i in range(1, n + 1):
	maxcw[i] = max(maxcw[i - 1], cw[i])
	maxccw[i] = max(maxccw[i - 1], ccw[i])
ans = max(0, maxcw[-1], maxccw[-1])
for i in range(1, n + 1):
	ans = max(ans, cw[i] + maxccw[-i - 1] - x[i - 1])
	ans = max(ans, ccw[i] + maxcw[-i - 1] - (c - x[-i]))
print(ans)


(N, C) = list(map(int, input().split()))
xv = [tuple(map(int, input().split())) for i in range(N)]
xv2 = [(C - x, v) for (x, v) in xv[::-1]]
L = [0] * N
ans = 0
for (i, X) in enumerate(xv):
	(x, v) = X
	L[i] = L[i - 1] + v
R = [0] * N
for i in range(N):
	L[i] -= xv[i][0]
for (i, X) in enumerate(xv2):
	(x, v) = X
	R[i] = R[i - 1] + v
for i in range(N):
	R[i] -= xv2[i][0]
LL = [0] * (N + 1)
RR = [0] * (N + 1)
for i in range(N):
	LL[i + 1] = max(LL[i], L[i])
	RR[i + 1] = max(RR[i], R[i])
ans = 0
ans = max(ans, LL[-1])
ans = max(ans, RR[-1])
for i in range(N):
	ans = max(ans, LL[N - 1 - i] + R[i] - xv2[i][0])
	ans = max(ans, RR[N - 1 - i] + L[i] - xv[i][0])
print(ans)


(N, C) = list(map(int, input().split()))
xv = [tuple(map(int, input().split())) for _ in range(N)]
accum_clockwise = [None] * N
accum_clockwise[0] = xv[0]
tmp = xv[0][1]
for i in range(1, N):
	tmp += xv[i][1]
	accum_clockwise[i] = (xv[i][0], tmp)
accum_anticlockwise = [None] * N
accum_anticlockwise[0] = (C - xv[-1][0], xv[-1][1])
tmp = xv[-1][1]
for i in range(1, N):
	tmp += xv[N - 1 - i][1]
	accum_anticlockwise[i] = (C - xv[N - 1 - i][0], tmp)
max_clockwise = [0] * (N + 1)
for i in range(N):
	max_clockwise[i + 1] = max(max_clockwise[i], accum_clockwise[i][1] - accum_clockwise[i][0] * 2)
max_anticlockwise = [0] * (N + 1)
for i in range(N):
	max_anticlockwise[i + 1] = max(max_anticlockwise[i], accum_anticlockwise[i][1] - accum_anticlockwise[i][0] * 2)
ans_clockwise = 0
for i in range(N):
	ans_clockwise = max(ans_clockwise, accum_clockwise[i][1] - accum_clockwise[i][0] + max_anticlockwise[N - 1 - i])
ans_anticlockwise = 0
for i in range(N):
	ans_anticlockwise = max(ans_anticlockwise, accum_anticlockwise[i][1] - accum_anticlockwise[i][0] + max_clockwise[N - 1 - i])
print(max(ans_clockwise, ans_anticlockwise))


(n, c) = map(int, input().split())
xv = [list(map(int, input().split())) for _ in range(n)]
(l, r, lret, rret) = ([], [], [], [])
(sl, sr) = (0, 0)
for i in range(n):
	sl += xv[i][1]
	sr += xv[n - i - 1][1]
	l.append(sl - xv[i][0])
	lret.append(sl - 2 * xv[i][0])
	r.append(sr - (c - xv[n - i - 1][0]))
	rret.append(sr - 2 * (c - xv[n - i - 1][0]))
for i in range(1, n):
	lret[i] = max(lret[i], lret[i - 1])
	rret[i] = max(rret[i], rret[i - 1])
ans = max(0, max(l), max(r))
for i in range(n - 1):
	ans = max(ans, l[i] + rret[n - i - 2], r[i] + lret[n - i - 2])
print(ans)


(n, c) = map(int, input().split())
(l, L, m, M, C, a, x, v) = ([0], [0], [0], [0], 0, 0, [], [])
for i in range(n):
	(X, V) = map(int, input().split())
	x.append(X)
	v.append(V)
for i in range(n):
	C += v[i]
	l.append(max(l[-1], C - x[i]))
	L.append(max(L[-1], C - 2 * x[i]))
C = 0
for i in range(n)[::-1]:
	C += v[i]
	m.append(max(m[-1], C - (c - x[i])))
	M.append(max(M[-1], C - 2 * (c - x[i])))
for i in range(n + 1):
	a = max(a, l[i] + M[n - i], L[i] + m[n - i])
print(a)


(n, c) = map(int, input().split())
a = [(0, 0)]
for _ in range(n):
	(x, v) = map(int, input().split())
	a += [(x, v)]
a += [(0, 0)]
(max_r, max_l) = ([0], [0])
s = 0
for i in range(1, n + 1):
	s += a[i][1]
	max_r += [max(max_r[i - 1], s - a[i][0])]
max_r += [0]
s = 0
for i in range(1, n + 1):
	s += a[-i - 1][1]
	max_l += [max(max_l[i - 1], s - c + a[-i - 1][0])]
max_l += [0]
max_l.reverse()
ans = 0
for i in range(1, n + 1):
	ans = max(ans, max_r[i], max_l[i], max_r[i] + max_l[i + 1] - a[i][0], max_r[i - 1] + max_l[i] - c + a[i][0])
print(ans)


import sys
input = sys.stdin.readline

def main():
	(N, C) = [int(x) for x in input().split()]
	XV = [[int(x) for x in input().split()] for _ in range(N)]
	migi = [0]
	last = 0
	for (x, v) in XV:
		migi.append(migi[-1] + v - x + last)
		last = x
	hidari = [0]
	last = C
	for (x, v) in XV[::-1]:
		hidari.append(hidari[-1] + v - (last - x))
		last = x
	ans = max(max(migi), max(hidari))
	migimax = []
	ma = 0
	for m in migi:
		ma = max(ma, m)
		migimax.append(ma)
	hidarimax = []
	ma = 0
	for m in hidari:
		ma = max(ma, m)
		hidarimax.append(ma)
	for i in range(N):
		ans = max(ans, migi[i + 1] - XV[i][0] + hidarimax[N - i - 1])
	for i in range(N):
		ans = max(ans, hidari[i + 1] - (C - XV[-i - 1][0]) + migimax[N - i - 1])
	print(ans)

def __starting_point():
	main()
__starting_point()


import sys
import numpy as np
sr = lambda : sys.stdin.readline().rstrip()
ir = lambda : int(sr())
lr = lambda : list(map(int, sr().split()))
(N, C) = lr()
XV = np.array([lr() for _ in range(N)])
right = XV[:, 1]
r_pos = XV[:, 0]
rcum = right.cumsum() - r_pos
r_max_cum = np.maximum.accumulate(rcum)
left = right[::-1]
l_pos = (C - r_pos)[::-1]
lcum = left.cumsum() - l_pos
l_max_cum = np.maximum.accumulate(lcum)
answer = max(0, r_max_cum[-1], l_max_cum[-1])
for i in range(N - 1):
	temp = rcum[i] - r_pos[i]
	temp += l_max_cum[N - 2 - i]
	if temp > answer:
		answer = temp
for i in range(N - 1):
	temp = lcum[i] - l_pos[i]
	temp += r_max_cum[N - 2 - i]
	if temp > answer:
		answer = temp
print(answer)


(N, C) = map(int, input().split())
sushi = []
for i in range(N):
	(x, v) = map(int, input().split())
	sushi.append((x, v))
left = [0] * N
left2 = [0] * N
right = [0] * N
right2 = [0] * N
d = 0
d2 = 0
cal = 0
for i in range(N):
	(x, v) = sushi[i]
	d = x
	d2 = x * 2
	cal += v
	if i == 0:
		right[i] = max(cal - d, 0)
		right2[i] = max(cal - d2, 0)
	else:
		right[i] = max(cal - d, right[i - 1])
		right2[i] = max(cal - d2, right2[i - 1])
d = 0
d2 = 0
cal = 0
for i in reversed(range(N)):
	(x, v) = sushi[i]
	d = C - x
	d2 = (C - x) * 2
	cal += v
	if i == 0:
		left[i] = max(cal - d, 0)
		left2[i] = max(cal - d2, 0)
	else:
		left[i] = max(cal - d, left[i - 1])
		left2[i] = max(cal - d2, left[i - 1])
ans = 0
for i in range(N):
	if i == 0:
		ans = max(ans, left[i])
	else:
		ans = max(ans, left[i] + right2[i - 1])
for i in range(N):
	if i == N - 1:
		ans = max(ans, right[i])
	else:
		ans = max(ans, right[i] + left2[i + 1])
print(ans)


import sys
input = sys.stdin.readline
from itertools import product
INF = 10 ** 9
MOD = 10 ** 9 + 7

def main():
	(n, c) = list(map(int, input().split()))
	sushi = [list(map(int, input().split())) for _ in range(n)]
	r = [0] * (n + 1)
	l = [0] * (n + 1)
	r_max = [0] * (n + 1)
	l_max = [0] * (n + 1)
	x = 0
	y = c
	for i in range(n):
		r[i + 1] = r[i] + sushi[i][1] - sushi[i][0] + x
		r_max[i + 1] = max(r_max[i], r[i + 1])
		x = sushi[i][0]
		l[-i - 2] = l[-i - 1] + sushi[-i - 1][1] - y + sushi[-i - 1][0]
		l_max[-i - 2] = max(l_max[-i - 1], l[-i - 2])
		y = sushi[-i - 1][0]
	ans = max(r_max[-1], l_max[0])
	for i in range(n - 1):
		tmp1 = r[i + 1] + l_max[i + 1] - sushi[i][0]
		tmp2 = l[i + 1] + r_max[i + 1] - c + sushi[i + 1][0]
		ans = max(ans, tmp1, tmp2)
	print(ans)

def __starting_point():
	main()
__starting_point()


import sys
import numpy as np
sr = lambda : sys.stdin.readline().rstrip()
ir = lambda : int(sr())
lr = lambda : list(map(int, sr().split()))
(N, C) = lr()
XV = np.array([lr() for _ in range(N)])
Sushi = XV[:, 1]
Sushi_rev = Sushi[::-1]
dis = XV[:, 0]
dis_rev = C - dis[::-1]
Sushi_cum = Sushi.cumsum()
Sushi_rev_cum = Sushi_rev.cumsum()
Sushi_cum -= dis
Sushi_rev_cum -= dis_rev
Sushi_max = np.maximum.accumulate(Sushi_cum)
Sushi_max_rev = np.maximum.accumulate(Sushi_rev_cum)
answer = max(0, Sushi_max[-1], Sushi_max_rev[-1])
for i in range(N - 1):
	cal = Sushi_cum[i] - dis[i] + Sushi_max_rev[N - 2 - i]
	cal2 = Sushi_rev_cum[i] - dis_rev[i] + Sushi_max[N - 2 - i]
	cal = max(cal, cal2)
	if cal > answer:
		answer = cal
print(answer)


import sys
input = sys.stdin.readline
(N, C) = map(int, input().split())
X = []
for i in range(N):
	(x, c) = map(int, input().split())
	X.append((x, c))
PS1 = [[0, 0, 0]]
PS2 = [[0, 0, 0]]
QS1 = [[0, 0, 0]]
QS2 = [[0, 0, 0]]
for i in range(N):
	(c1, xx, c0) = PS1[i]
	(c2, _, _) = PS2[i]
	(x, c) = X[i]
	PS1.append([c0 + c - x, i + 1, c0 + c])
	PS2.append([c0 + c - x * 2, i + 1, c0 + c])
for i in range(N):
	(c1, xx, c0) = QS1[i]
	(c2, _, _) = QS2[i]
	(x, c) = X[N - i - 1]
	x = C - x
	QS1.append([c0 + c - x, i + 1, c0 + c])
	QS2.append([c0 + c - x * 2, i + 1, c0 + c])
PS1.sort(reverse=True)
PS2.sort(reverse=True)
ans = 0
(ps1index, ps2index) = (0, 0)
for q in range(N + 1):
	while PS1[ps1index][1] + q > N:
		ps1index += 1
	while PS2[ps2index][1] + q > N:
		ps2index += 1
	a1 = PS1[ps1index][0] + QS2[q][0]
	a2 = PS2[ps2index][0] + QS1[q][0]
	ans = max(ans, a1, a2)
print(ans)


(N, C) = map(int, input().split())
X = []
V = []
for _ in range(N):
	(x, v) = map(int, input().split())
	X.append(x)
	V.append(v)
A = V[:]
B = V[::-1]
for i in range(N - 1):
	A[i + 1] += A[i]
	B[i + 1] += B[i]
P = [0]
Q = [0]
for i in range(N):
	P.append(max(P[-1], A[i] - X[i] * 2))
	Q.append(max(Q[-1], B[i] - (C - X[N - i - 1]) * 2))
ans = 0
for i in range(N):
	ans = max(ans, A[i] + Q[N - i - 1] - X[i])
	ans = max(ans, B[i] + P[N - i - 1] - (C - X[N - i - 1]))
print(ans)


(n, c) = map(int, input().split())
xv = [list(map(int, input().split())) for _ in range(n)]
l = [0]
s = 0
for (x, v) in xv:
	s += v
	l.append(max(l[-1], s - x))
r = [0]
s = 0
for (x, v) in xv[::-1]:
	s += v
	r.append(max(r[-1], s - (c - x)))
ans = max(l + r)
s = 0
for i in range(n):
	s += xv[i][1]
	ans = max(ans, s - 2 * xv[i][0] + r[n - i - 1])
s = 0
for i in range(n - 1, -1, -1):
	s += xv[i][1]
	ans = max(ans, s - 2 * (c - xv[i][0]) + l[i])
print(ans)


import heapq
(N, C) = map(int, input().split())
sushi = []
for i in range(N):
	cur_sushi = list(map(int, input().split()))
	sushi.append(cur_sushi)
(sum_V, sum_right) = ([0], [0])
for i in range(N):
	sum_V.append(sum_V[i] + sushi[i][1])
	sum_right.append(sum_V[i + 1] - sushi[i][0])
(sum_V, sum_left) = ([0], [0])
for i in range(N):
	sum_V.append(sum_V[i] + sushi[N - 1 - i][1])
	sum_left.append(sum_V[i + 1] - (C - sushi[N - 1 - i][0]))
ans = max(max(sum_left), max(sum_right))
left = [0]
heapq.heapify(left)
for l in range(N):
	now = sum_right[N - l] - sushi[N - 1 - l][0]
	heapq.heappush(left, -sum_left[l])
	now -= left[0]
	ans = max(ans, now)
right = [0]
heapq.heapify(right)
for r in range(N):
	now = sum_left[N - r] - (C - sushi[r][0])
	heapq.heappush(right, -sum_right[r])
	now -= right[0]
	ans = max(ans, now)
print(ans)


(n, c) = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(n)]
acumr = [0, arr[0][1] - arr[0][0]]
acuml = [0, arr[-1][1] - (c - arr[-1][0])]
for i in range(1, n):
	acumr.append(acumr[-1] + arr[i][1] - (arr[i][0] - arr[i - 1][0]))
	acuml.append(acuml[-1] + arr[-(i + 1)][1] - (arr[-i][0] - arr[-(i + 1)][0]))
maxr = [0]
maxl = [0]
for i in range(1, n + 1):
	maxr.append(max(maxr[-1], acumr[i]))
	maxl.append(max(maxl[-1], acuml[i]))
ans = 0
for i in range(1, n + 1):
	tmp1 = acumr[i]
	tmp2 = acuml[i]
	if arr[i - 1][0] < maxl[n - i]:
		tmp1 += maxl[n - i] - arr[i - 1][0]
	if c - arr[-i][0] < maxr[n - i]:
		tmp2 += maxr[n - i] - (c - arr[-i][0])
	ans = max(ans, tmp1)
	ans = max(ans, tmp2)
print(ans)


(n, c) = list(map(int, input().split()))
x = [0] * n
v = [0] * n
for i in range(n):
	(x[i], v[i]) = list(map(int, input().split()))
x = [0] + x + [c]
v = [0] + v + [0]
left = [0] * (n + 2)
right = [0] * (n + 2)
for i in range(n + 1):
	left[i + 1] = left[i] + v[i + 1] - (x[i + 1] - x[i])
for i in range(n + 1, 0, -1):
	right[i - 1] = right[i] + v[i - 1] - (x[i] - x[i - 1])
for i in range(n + 1):
	left[i + 1] = max(left[i + 1], left[i])
for i in range(n + 1, 0, -1):
	right[i - 1] = max(right[i - 1], right[i])
ans = max(left[-1], right[0])
right = list(reversed(right))
for i in range(n):
	tmp = left[i] - x[i]
	rem = n - i
	ans = max(ans, tmp + right[n - i])
for i in range(n):
	tmp = right[i] - (c - x[-(i + 1)])
	rem = n - i
	ans = max(ans, tmp + left[n - i])
print(ans)


import sys
from copy import deepcopy as copy
input = sys.stdin.readline

def main():
	(n, c) = map(int, input().split())
	sushi = [[0, 0]] + [[int(x) for x in input().split()] for _ in range(n)]
	order = [0] * (n + 1)
	reverse = [0] * (n + 1)
	for i in range(n):
		order[i + 1] = order[i] + sushi[i + 1][1]
		reverse[i + 1] = reverse[i] + sushi[n - i][1]
	order_index = [i for i in range(n + 1)]
	reverse_index = [-i for i in range(n + 1)]
	for i in range(n):
		order[i + 1] -= sushi[i + 1][0]
		if order[i + 1] <= order[i]:
			order[i + 1] = order[i]
			order_index[i + 1] = order_index[i]
		reverse[i + 1] -= c - sushi[n - i][0]
		if reverse[i + 1] <= reverse[i]:
			reverse[i + 1] = reverse[i]
			reverse_index[i + 1] = reverse_index[i]
	order_back = copy(order)
	reverse_back = copy(reverse)
	for i in range(n + 1):
		order_back[i] -= sushi[order_index[i]][0]
		if i != 0 and order_back[i] < order_back[i - 1]:
			order_back[i] = order_back[i - 1]
		reverse_back[i] -= (c - sushi[reverse_index[i]][0]) % c
		if i != 0 and reverse_back[i] < reverse_back[i - 1]:
			reverse_back[i] < reverse_back[i - 1]
	ans = 0
	for i in range(n + 1):
		sub = max(order[i] + reverse_back[n - i], order_back[i] + reverse[n - i])
		if ans < sub:
			ans = sub
	print(ans)

def __starting_point():
	main()
__starting_point()


import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input():
	return sys.stdin.readline().strip()

def INT():
	return int(input())

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

def S_MAP():
	return map(str, input().split())

def LIST():
	return list(map(int, input().split()))

def S_LIST():
	return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
(N, C) = MAP()
L = [LIST() for i in range(N)]
A = [0] * N
B = [0] * N
X = []
V = []
for (x, v) in L:
	X.append(x)
	V.append(v)
tmp_v = 0
for i in range(N):
	tmp_v += V[i]
	A[i] = tmp_v - X[i]
tmp_v = 0
for i in range(N - 1, -1, -1):
	tmp_v += V[i]
	B[i] = tmp_v - (C - X[i])
gA = [0] * N
gA[0] = A[0]
for i in range(1, N):
	gA[i] = max(gA[i - 1], A[i])
gB = [0] * N
gB[N - 1] = B[N - 1]
for i in range(N - 2, -1, -1):
	gB[i] = max(gB[i + 1], B[i])
a = max(A)
b = max(B)
(c, d) = (0, 0)
if N > 1:
	c = max([A[i] - X[i] + gB[i + 1] for i in range(0, N - 1)])
	d = max([B[i] - (C - X[i]) + gA[i - 1] for i in range(N - 1, 0, -1)])
print(max(a, b, c, d, 0))


from itertools import accumulate
(n, c) = list(map(int, input().split()))
xv = [[0, 0]] + [list(map(int, input().split())) for i in range(n)]
xv_rev = [[0, 0]] + [[c - x, v] for (x, v) in xv][:0:-1]
cumsum = [i - x for (i, (x, v)) in zip(list(accumulate([v for (x, v) in xv])), xv)]
cumsum_rev = [i - x for (i, (x, v)) in zip(list(accumulate([v for (x, v) in xv_rev])), xv_rev)]
xv_max = [0]
xv_rev_max = [0]
now_v = 0
for (k, (x, v)) in enumerate(xv[:-1], 1):
	xv_max.append(max(xv_max[-1], cumsum[k]))
for (k, (x, v)) in enumerate(xv_rev[:-1], 1):
	xv_rev_max.append(max(xv_rev_max[-1], cumsum_rev[k]))
ans = 0
for (i, j, (x, v)) in zip(xv_max, cumsum_rev[::-1], xv_rev[::-1]):
	ans = max(ans, i + j - x)
for (i, j, (x, v)) in zip(xv_rev_max, cumsum[::-1], xv[::-1]):
	ans = max(ans, i + j - x)
print(ans)


(n, c) = map(int, input().split())
x = []
v = []
rightvalue = []
temp = 0
for i in range(n):
	(xi, vi) = map(int, input().split())
	x.append(xi)
	v.append(vi)
	temp += vi
	rightvalue.append(temp - xi)
rightmaxdouble = []
currentmax = 0
for i in range(n):
	if currentmax < rightvalue[i] - x[i]:
		currentmax = rightvalue[i] - x[i]
	rightmaxdouble.append(currentmax)
leftvalue = []
temp = 0
for i in range(n):
	temp += v[n - 1 - i]
	leftvalue.append(temp - (c - x[n - 1 - i]))
leftmaxdouble = []
currentmax = 0
for i in range(n):
	if currentmax < leftvalue[i] - (c - x[n - 1 - i]):
		currentmax = leftvalue[i] - (c - x[n - 1 - i])
	leftmaxdouble.append(currentmax)
ans = 0
for i in range(n):
	if n - i >= 2:
		if rightvalue[i] + leftmaxdouble[n - i - 2] > ans:
			ans = rightvalue[i] + leftmaxdouble[n - i - 2]
	elif rightvalue[i] > ans:
		ans = rightvalue[i]
for i in range(n):
	if n - i >= 2:
		if leftvalue[i] + rightmaxdouble[n - i - 2] > ans:
			ans = leftvalue[i] + rightmaxdouble[n - i - 2]
	elif leftvalue[i] > ans:
		ans = leftvalue[i]
print(ans)


from copy import deepcopy as copy
(n, c) = map(int, input().split())
sushi = [[0, 0]] + [[int(x) for x in input().split()] for _ in range(n)]
order = [0] * (n + 1)
reverse = [0] * (n + 1)
for i in range(n):
	order[i + 1] = order[i] + sushi[i + 1][1]
	reverse[i + 1] = reverse[i] + sushi[n - i][1]
order_index = [i for i in range(n + 1)]
reverse_index = [-i for i in range(n + 1)]
for i in range(n):
	order[i + 1] -= sushi[i + 1][0]
	if order[i + 1] <= order[i]:
		order[i + 1] = order[i]
		order_index[i + 1] = order_index[i]
	reverse[i + 1] -= c - sushi[n - i][0]
	if reverse[i + 1] <= reverse[i]:
		reverse[i + 1] = reverse[i]
		reverse_index[i + 1] = reverse_index[i]
order_back = copy(order)
reverse_back = copy(reverse)
for i in range(n + 1):
	order_back[i] -= sushi[order_index[i]][0]
	if i != 0 and order_back[i] < order_back[i - 1]:
		order_back[i] = order_back[i - 1]
	reverse_back[i] -= (c - sushi[reverse_index[i]][0]) % c
	if i != 0 and reverse_back[i] < reverse_back[i - 1]:
		reverse_back[i] < reverse_back[i - 1]
ans = 0
for i in range(n + 1):
	sub = max(order[i] + reverse_back[n - i], order_back[i] + reverse[n - i])
	if ans < sub:
		ans = sub
print(ans)


from heapq import heappush, heappop
(N, C, *L) = map(int, open(0).read().split())
hq1 = []
hq2 = []
sushi = []
for (x, y) in zip(*[iter(L)] * 2):
	sushi += [(x, y)]
sushi.sort()
x = 0
y = 0
for i in range(N - 1, -1, -1):
	(p, q) = sushi[i]
	x += q
	heappush(hq1, (-x + (C - p), i))
	(p, q) = sushi[N - 1 - i]
	y += q
	heappush(hq2, (-y + p, N - 1 - i))
m = 0
ans1 = 0
for i in range(N):
	(x, y) = sushi[i]
	m += y
	n = m - x
	while hq1 and hq1[0][1] <= i:
		heappop(hq1)
	if hq1 and hq1[0][0] < -x:
		ans1 = max(ans1, n - hq1[0][0] - x)
	else:
		ans1 = max(ans1, n)
m = 0
ans2 = 0
for i in range(N - 1, -1, -1):
	(x, y) = sushi[i]
	m += y
	n = m - (C - x)
	while hq2 and hq2[0][1] >= i:
		heappop(hq2)
	if hq2 and hq2[0][0] < -(C - x):
		ans2 = max(ans2, n - hq2[0][0] - (C - x))
	else:
		ans2 = max(ans2, n)
print(max(ans1, ans2))


from sys import stdin

def main():
	readline = stdin.readline
	(n, c) = map(int, readline().split())
	x = [0] * n
	v = [0] * n
	for i in range(n):
		(x[i], v[i]) = map(int, readline().split())
	x_rev = list(map(lambda x: c - x, reversed(x)))
	v_rev = list(reversed(v))
	dp = [0] * (n + 1)
	dp_rev = [0] * (n + 1)
	now = 0
	tmp = 0
	for i in range(n):
		dp[i + 1] = max(dp[i], tmp + v[i] - x[i] + now)
		tmp += v[i] - x[i] + now
		now = x[i]
	now = 0
	tmp = 0
	for i in range(n):
		dp_rev[i + 1] = max(dp_rev[i], tmp + v_rev[i] - x_rev[i] + now)
		tmp += v_rev[i] - x_rev[i] + now
		now = x_rev[i]
	ans = 0
	for i in range(n):
		tmp1 = dp[i + 1]
		tmp2 = dp[i + 1] - x[i] + dp_rev[n - i - 1]
		ans = max(ans, tmp1, tmp2)
	for i in range(n):
		tmp1 = dp_rev[i + 1]
		tmp2 = dp_rev[i + 1] - x_rev[i] + dp[n - i - 1]
		ans = max(ans, tmp1, tmp2)
	print(ans)

def __starting_point():
	main()
__starting_point()


(N, C) = map(int, input().split())
xc = [tuple(map(int, input().split())) for i in range(N)]
xc.sort()
ans = 0
p = 0
s = [0] * (N + 1)
ss = [0] * (N + 1)
for i in range(N):
	s[i + 1] = s[i] + xc[i][1]
for i in range(N):
	ss[i + 1] = ss[i] + xc[N - 1 - i][1]
maxs = [0] * (N + 1)
maxss = [0] * (N + 1)
for i in range(N):
	s[i + 1] = s[i + 1] - xc[i][0]
	ss[i + 1] = ss[i + 1] - (C - xc[N - 1 - i][0])
	maxs[i + 1] = max(maxs[i], s[i + 1])
	maxss[i + 1] = max(maxss[i], ss[i + 1])
ans = max(0, maxs[-1])
ans = max(ans, maxss[-1])
for i in range(N):
	if s[i + 1] > xc[i][0]:
		ans = max(ans, maxss[N - i - 1] + (s[i + 1] - xc[i][0]))
	if ss[i + 1] > C - xc[N - 1 - i][0]:
		ans = max(ans, maxs[N - i - 1] + (ss[i + 1] - (C - xc[N - 1 - i][0])))
print(ans)


(n, c) = map(int, input().split())
xvl = []
for _ in range(n):
	(x, v) = map(int, input().split())
	xvl.append((x, v))
get_l = []
sum_v = 0
for (x, v) in xvl:
	sum_v += v
	get_l.append(sum_v - x)
import itertools
get_l_ac = list(itertools.accumulate(get_l, max))
get_r = []
sum_v = 0
for (x, v) in xvl[::-1]:
	sum_v += v
	get_r.append(sum_v - (c - x))
ans = 0
x = 0
for i in range(n):
	tmp = 0
	if i > 0:
		x = c - xvl[-i][0]
		tmp += get_r[i - 1]
	ans = max(tmp, tmp - x + get_l_ac[n - i - 1], ans)
get_r_ac = list(itertools.accumulate(get_r, max))
x = 0
for i in range(n):
	tmp = 0
	if i > 0:
		x = xvl[i - 1][0]
		tmp += get_l[i - 1]
	ans = max(tmp, tmp - x + get_r_ac[n - i - 1], ans)
print(ans)


(n, c) = map(int, input().split())
clock = []
counter_clock = []
for i in range(n):
	(x, v) = map(int, input().split())
	clock.append([x, v])
	counter_clock.append([c - x, v])
clock.sort()
counter_clock.sort()
clock_ac = [0]
counter_clock_ac = [0]
for i in range(n):
	v = clock[i][1]
	clock_ac.append(clock_ac[-1] + v)
	v2 = counter_clock[i][1]
	counter_clock_ac.append(counter_clock_ac[-1] + v2)
for i in range(n):
	x = clock[i][0]
	clock_ac[i + 1] -= x
	x2 = counter_clock[i][0]
	counter_clock_ac[i + 1] -= x2
max_clock_ac = [0]
max_counter_clock_ac = [0]
for i in range(n):
	max_clock_ac.append(max(max_clock_ac[-1], clock_ac[i + 1]))
	max_counter_clock_ac.append(max(max_counter_clock_ac[-1], counter_clock_ac[i + 1]))
ans = max(max_clock_ac[n], max_counter_clock_ac[n])
for i in range(n):
	ans = max(ans, clock_ac[i + 1] - clock[i][0] + max_counter_clock_ac[n - i - 1])
	ans = max(ans, counter_clock_ac[i + 1] - counter_clock[i][0] + max_clock_ac[n - i - 1])
print(ans)


(n, c) = map(int, input().split())
X = []
for i in range(n):
	(x, y) = map(int, input().split())
	X.append((x, y))
(SR, DR) = ([0] * (n + 1), [0] * (n + 1))
(s, d) = (0, 0)
cur = 0
for i in range(n):
	(x, y) = X[i]
	SR[i + 1] = SR[i] + y - (x - cur)
	DR[i + 1] = DR[i] + y - 2 * (x - cur)
	cur = x
(SL, DL) = ([0] * (n + 1), [0] * (n + 1))
(s, d) = (0, 0)
cur = c
for i in reversed(range(n)):
	(x, y) = X[i]
	SL[n - i] = SL[n - 1 - i] + y - (cur - x)
	DL[n - i] = DL[n - 1 - i] + y - 2 * (cur - x)
	cur = x
for i in range(n):
	SL[i + 1] = max(SL[i + 1], SL[i])
	SR[i + 1] = max(SR[i + 1], SR[i])
ans = 0
for i in range(n + 1):
	a = max(DR[i] + SL[n - i], DL[i] + SR[n - i])
	ans = max(ans, a)
print(ans)


(N, C) = list(map(int, input().split()))
D = [[int(x) for x in input().split()] for _ in range(N)]
(X, V, M) = ([0] * (N + 1), [0] * (N + 1), [0] * (N + 1))
for i in range(1, N + 1):
	(X[i], V[i]) = (D[i - 1][0], V[i - 1] + D[i - 1][1])
	M[i] = M[i - 1] if M[i - 1] > V[i] - D[i - 1][0] else V[i] - D[i - 1][0]
m = 0
for i in range(1, N + 1):
	e = M[i - 1] - (C - X[i]) if M[i - 1] > C - X[i] else 0
	e += V[N] - V[i - 1] - (C - X[i])
	m = m if m > e else e
M = [0] * (N + 2)
for i in range(N, 0, -1):
	p = V[N] - V[i - 1] - (C - X[i])
	M[i] = M[i + 1] if M[i + 1] > p else p
for i in range(1, N + 1):
	e = M[i + 1] - X[i] if M[i + 1] > X[i] else 0
	e += V[i] - X[i]
	m = m if m > e else e
print(m)


(N, C) = list(map(int, input().split()))
XV = [tuple(map(int, input().split())) for _ in range(N)]
XV.sort()
L = [0] * (N + 1)
S = 0
for (i, (x, v)) in enumerate(XV, start=1):
	S += v
	L[i] = max(L[i - 1], S - x)
R = [0] * (N + 1)
S = 0
for (i, (x, v)) in enumerate(XV[::-1], start=1):
	S += v
	R[i] = max(R[i - 1], S - (C - x))
ans = max(max(L), max(R))
S = 0
for (i, (x, v)) in enumerate(XV, start=1):
	S += v
	ans = max(ans, S - 2 * x + R[N - i])
S = 0
for (i, (x, v)) in enumerate(XV[::-1], start=1):
	S += v
	ans = max(ans, S - (C - x) * 2 + L[N - i])
print(ans)


def main():
	import sys
	input = sys.stdin.readline
	sys.setrecursionlimit(10 ** 7)
	from collections import Counter, deque
	from itertools import combinations, permutations, accumulate, groupby, product
	from bisect import bisect_left, bisect_right
	from heapq import heapify, heappop, heappush
	from math import floor, ceil
	(n, C) = map(int, input().split())
	xv = [list(map(int, input().split())) for _ in range(n)]
	xv.sort()
	(a, b) = ([], [])
	kcal = 0
	for (x, v) in xv:
		kcal += v
		a.append(kcal - x)
		b.append(kcal - 2 * x)
	xv.sort(reverse=True)
	(c, d) = ([], [])
	kcal = 0
	for (x, v) in xv:
		kcal += v
		x = C - x
		c.append(kcal - x)
		d.append(kcal - 2 * x)
	for v in [a, b, c, d]:
		for i in range(1, n):
			if v[i] < v[i - 1]:
				v[i] = v[i - 1]
	res = 0
	for i in range(n):
		res = max(res, a[i], c[i])
		if i < n - 1:
			res = max(res, b[i] + c[-(i + 2)], d[i] + a[-(i + 2)])
	print(res)

def __starting_point():
	main()
__starting_point()


(N, C) = map(int, input().split())
sushi = []
for _ in range(N):
	(x, v) = map(int, input().split())
	sushi.append([x, v])
sushi_2 = list(map(lambda x: [x[0] + C, x[1]], sushi))
sushi += sushi_2
cal = [[0, 0] for i in range(2 * N)]
pre_x = C
for i in range(N, 2 * N):
	(x, v) = (sushi[i][0], sushi[i][1])
	(cal[i][0], cal[i][1]) = (v - (x - pre_x), v - 2 * (x - pre_x))
	pre_x = x
pre_x = C
for i in range(N)[::-1]:
	(x, v) = (sushi[i][0], sushi[i][1])
	(cal[i][0], cal[i][1]) = (v - (pre_x - x), v - 2 * (pre_x - x))
	pre_x = x
left = [[0, 0] for i in range(N)]
right = [[0, 0] for i in range(N)]
(temp_0, temp_1) = (cal[N][0], cal[N][1])
(right[0][0], right[0][1]) = (max(0, temp_0), max(0, temp_1))
for i in range(N + 1, 2 * N):
	temp_0 += cal[i][0]
	temp_1 += cal[i][1]
	(right[i - N][0], right[i - N][1]) = (max(right[i - N - 1][0], temp_0), max(right[i - N - 1][1], temp_1))
(temp_0, temp_1) = (cal[N][0], cal[N][1])
(right[0][0], right[0][1]) = (max(0, temp_0), max(0, temp_1))
for i in range(N + 1, 2 * N):
	temp_0 += cal[i][0]
	temp_1 += cal[i][1]
	(right[i - N][0], right[i - N][1]) = (max(right[i - N - 1][0], temp_0), max(right[i - N - 1][1], temp_1))
(temp_0, temp_1) = (cal[N - 1][0], cal[N - 1][1])
(left[-1][0], left[-1][1]) = (max(0, temp_0), max(0, temp_1))
for i in range(N - 1)[::-1]:
	temp_0 += cal[i][0]
	temp_1 += cal[i][1]
	(left[i - N][0], left[i - N][1]) = (max(left[i - N + 1][0], temp_0), max(left[i - N + 1][1], temp_1))
ans = max(left[0][0], 0)
for i in range(1, N):
	ans = max(ans, left[i][0] + right[i - 1][1])
ans = max(right[N - 1][0], ans)
for i in range(N - 1)[::-1]:
	ans = max(ans, right[i][0] + left[i + 1][1])
print(ans)


from itertools import accumulate
(n, c) = map(int, input().split())
sushi = [list(map(int, input().split())) for i in range(n)]
r = [sushi[0][1] - sushi[0][0]]
for i in range(1, n):
	r.append(r[-1] + sushi[i][1] - (sushi[i][0] - sushi[i - 1][0]))
l = [sushi[n - 1][1] - (c - sushi[n - 1][0])]
for i in range(n - 2, -1, -1):
	l.append(l[-1] + sushi[i][1] - (c - sushi[i][0] - (c - sushi[i + 1][0])))
r2 = [sushi[0][1] - 2 * sushi[0][0]]
for i in range(1, n):
	r2.append(r2[-1] + sushi[i][1] - 2 * (sushi[i][0] - sushi[i - 1][0]))
l2 = [sushi[n - 1][1] - 2 * (c - sushi[n - 1][0])]
for i in range(n - 2, -1, -1):
	l2.append(l2[-1] + sushi[i][1] - 2 * (c - sushi[i][0] - (c - sushi[i + 1][0])))
r = list(accumulate(r, max))
l = list(accumulate(l, max))
cand = [l[-1], r[-1]]
for i in range(n):
	if 0 <= n - i - 2 <= n - 1:
		cand.append(l2[i] + r[n - i - 2])
		cand.append(r2[i] + l[n - i - 2])
print(max(max(cand), 0))


import sys
stdin = sys.stdin
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
nn = lambda : list(stdin.readline().split())
ns = lambda : stdin.readline().rstrip()
(n, c) = na()
migi = []
sushi_m = []
for i in range(n):
	(x, v) = na()
	migi.append(x)
	sushi_m.append(v)
sushi_h = list(reversed(sushi_m))
hidari = [c - i for i in reversed(migi)]
m = 0
mm = 0
h = 0
hh = 0
mmigi = []
hhidari = []
for i in range(n):
	m += sushi_m[i]
	h += sushi_h[i]
	mm = max(mm, m - migi[i])
	mmigi.append(mm)
	hh = max(hh, h - hidari[i])
	hhidari.append(hh)
ans = 0
for i in range(n):
	if n - i - 2 >= 0 and hhidari[n - i - 2] - migi[i] > 0:
		ans = max(ans, mmigi[i] + hhidari[n - i - 2] - migi[i])
	else:
		ans = max(ans, mmigi[i])
for i in range(n):
	if n - i - 2 >= 0 and mmigi[n - i - 2] - hidari[i] > 0:
		ans = max(ans, hhidari[i] + mmigi[n - i - 2] - hidari[i])
	else:
		ans = max(ans, hhidari[i])
print(ans)


import sys
try:
	from typing import List
except ImportError:
	pass

def solve(N: int, C: int, x: 'List[int]', v: 'List[int]'):
	mp1 = [0] * (N + 1)
	mp2 = [0] * (N + 1)
	cum = 0
	for i in range(N):
		cum += v[i]
		mp1[i + 1] = max(mp1[i], cum - x[i])
		mp2[i + 1] = max(mp2[i], cum - x[i] * 2)
	mn1 = [0] * (N + 1)
	mn2 = [0] * (N + 1)
	cum = 0
	for i in reversed(list(range(N))):
		cum += v[i]
		mn1[i] = max(mn1[i + 1], cum - (C - x[i]))
		mn2[i] = max(mn2[i + 1], cum - (C - x[i]) * 2)
	print(max(max((mp1[i] + mn2[i] for i in range(N + 1))), max((mp2[i] + mn1[i] for i in range(N + 1)))))

def main():

	def iterate_tokens():
		for line in sys.stdin:
			for word in line.split():
				yield word
	tokens = iterate_tokens()
	N = int(next(tokens))
	C = int(next(tokens))
	x = [int()] * N
	v = [int()] * N
	for i in range(N):
		x[i] = int(next(tokens))
		v[i] = int(next(tokens))
	solve(N, C, x, v)

def __starting_point():
	main()
__starting_point()


import sys
input = sys.stdin.readline
(n, c) = list(map(int, input().split()))
xv = [list(map(int, input().split())) for _ in range(n)]
m1 = []
v1 = []
v_cum = 0
tmp = 0
for (x, v) in xv:
	v_cum += v
	tmp = max(tmp, v_cum - x)
	v1.append(v_cum - x)
	m1.append(tmp)
m2 = []
v2 = []
v_cum = 0
tmp = 0
for (x, v) in xv[::-1]:
	v_cum += v
	tmp = max(tmp, v_cum + x - c)
	v2.append(v_cum + x - c)
	m2.append(tmp)
ans = max((0, m1[-1], m2[-1]))
for i in range(n - 1):
	ans = max((ans, v1[i] + m2[-i - 2] - xv[i][0], v2[i] + m1[-i - 2] + xv[-i - 1][0] - c))
print(ans)


def main():
	(N, C) = list(map(int, input().split()))
	x = [None] * N
	v = [None] * N
	for i in range(N):
		(x[i], v[i]) = list(map(int, input().split()))
	cal_clock = [None] * N
	cal = -x[0] + v[0]
	cal_clock[0] = cal if cal > 0 else 0
	for i in range(1, N):
		cal -= x[i] - x[i - 1]
		cal += v[i]
		cal_clock[i] = cal if cal > cal_clock[i - 1] else cal_clock[i - 1]
	cal_anti = [None] * N
	cal = -(C - x[-1]) + v[-1]
	cal_anti[0] = cal if cal > 0 else 0
	for i in range(1, N):
		cal -= x[N - i] - x[N - 1 - i]
		cal += v[N - 1 - i]
		cal_anti[i] = cal if cal > cal_anti[i - 1] else cal_anti[i - 1]
	ans = max(cal_clock[-1], cal_anti[-1])
	for k in range(N - 1):
		cand_1 = cal_clock[k] - x[k] + cal_anti[N - 2 - k]
		cand_2 = cal_anti[k] - (C - x[N - 1 - k]) + cal_clock[N - 2 - k]
		ans = max(ans, cand_1, cand_2)
	return ans
print(main())


def main():
	(n, c) = map(int, input().split())
	xv = [list(map(int, input().split())) for _ in range(n)]
	(v, left_max, right_max) = (0, 0, 0)
	gl = [None] * n
	gr = [None] * n
	_2oas = [None] * (n + 1)
	_2obs = [None] * (n + 1)
	(_2oas[0], _2obs[0]) = (0, 0)
	ans = 0
	for (i, (xi, vi)) in enumerate(xv, 1):
		v += vi
		left = v - xi
		if left_max < left:
			left_max = left
		gl[n - i] = left_max
		_2oas[i] = v - 2 * xi
	v = 0
	for (i, (xi, vi)) in enumerate(xv[::-1], 1):
		v += vi
		right = v - c + xi
		if right_max < right:
			right_max = right
		gr[n - i] = right_max
		_2obs[i] = v - 2 * (c - xi)
	for (oa, _2ob, ob, _2oa) in zip(gl, _2obs[:n], gr, _2oas[:n]):
		ans = max(ans, _2oa + ob, _2ob + oa)
	print(ans)

def __starting_point():
	main()
__starting_point()


(N, C) = map(int, input().split())
x = []
v = []
for i in range(N):
	(xi, vi) = map(int, input().split())
	x.append(xi)
	v.append(vi)
l1 = [0]
l2 = [0]
r1 = [0]
r2 = [0]
vtotal = 0
for i in range(N):
	vtotal += v[i]
	l1.append(max(l1[-1], vtotal - x[i]))
	l2.append(max(l2[-1], vtotal - 2 * x[i]))
vtotal = 0
for i in range(N - 1, -1, -1):
	vtotal += v[i]
	r1.append(max(r1[-1], vtotal - (C - x[i])))
	r2.append(max(r2[-1], vtotal - 2 * (C - x[i])))
ans = 0
for i in range(N + 1):
	ans = max(ans, l1[i] + r2[N - i], l2[i] + r1[N - i])
print(ans)


import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
INF = -2 ** 31
(N, C) = [int(_) for _ in input().split()]
X = [[int(x) for x in input().split()] for _ in range(N)]

def spam():
	L = [0] * N
	pre = 0
	pre_max = -2 ** 31
	for i in range(N):
		(x, v) = X[-i - 1]
		L[i] = max(pre_max, pre + v - (C - x))
		pre += v
		pre_max = L[i]
	ans = max(pre_max, 0)
	pre = 0
	for i in range(N):
		(x, v) = X[i]
		tmp = pre + v - x
		if i < N - 1:
			tmp2 = max(L[N - i - 2] - x, 0)
		else:
			tmp2 = 0
		ans = max(ans, tmp + tmp2)
		pre += v
	return ans

def main():
	nonlocal X
	ans = spam()
	X = X[::-1]
	for i in range(N):
		X[i][0] = C - X[i][0]
	ans = max(ans, spam())
	print(ans)
main()


from itertools import accumulate
(N, C) = map(int, input().split())
xs = []
vs = []
for _ in range(N):
	(x, v) = map(int, input().split())
	xs.append(x)
	vs.append(v)
vacc = [0] + list(accumulate(vs, lambda x, y: x + y))
lacc = [0] + [vacc[i + 1] - xs[i] for i in range(N)]
racc = [vacc[-1] - vacc[i] - C + xs[i] for i in range(N)] + [0]
for i in range(1, N):
	lacc[i] = max(lacc[i], lacc[i - 1])
racc[-1] = (racc[-1], 0)
for i in range(N, 0, -1):
	if racc[i - 1] > racc[i][0]:
		racc[i - 1] = (racc[i - 1], C - xs[i - 1])
	else:
		racc[i - 1] = racc[i]
xs = [0] + xs
ans = 0
for i in range(N + 1):
	l = lacc[i]
	(r, d) = racc[i]
	ans = max(ans, l + r - xs[i], l + r - d, l, r)
print(ans)


from itertools import *
(N, C, *XV) = map(int, open(0).read().split())
X = XV[::2] + [C]
A = [0] + list(accumulate(XV[1::2]))
ans = L = R = 0
for b in range(N + 1):
	T = A[N] - A[b]
	d = C - X[b]
	L = max(L, A[b] - X[b - 1])
	R = max(R, A[b] - 2 * X[b - 1])
	ans = max(ans, L + T - 2 * d, R + T - d)
print(ans)


def main():
	(N, C) = list(map(int, input().split(' ')))
	sushi = [list(map(int, input().split(' '))) for _ in range(N)]
	cr = [0 for _ in range(N + 1)]
	cr_rt = [0 for _ in range(N + 1)]
	for i in range(N):
		move = sushi[i][0] if i == 0 else sushi[i][0] - sushi[i - 1][0]
		cr[i + 1] = cr[i] + sushi[i][1] - move
		cr_rt[i + 1] = cr_rt[i] + sushi[i][1] - 2 * move
	cl = [0 for _ in range(N + 1)]
	cl_rt = [0 for _ in range(N + 1)]
	for i in range(N):
		move = C - sushi[N - 1 - i][0] if i == 0 else sushi[N - i][0] - sushi[N - 1 - i][0]
		cl[i + 1] = cl[i] + sushi[N - 1 - i][1] - move
		cl_rt[i + 1] = cl_rt[i] + sushi[N - 1 - i][1] - 2 * move
	cr_rt_max = [0 for _ in range(N + 1)]
	cl_rt_max = [0 for _ in range(N + 1)]
	for i in range(N):
		cr_rt_max[i + 1] = max([cr_rt_max[i], cr_rt[i + 1]])
		cl_rt_max[i + 1] = max([cl_rt_max[i], cl_rt[i + 1]])
	ans = 0
	for i in range(N + 1):
		ans = max([ans, cr[i] + cl_rt_max[N - i]])
		ans = max([ans, cl[i] + cr_rt_max[N - i]])
	print(ans)

def __starting_point():
	main()
__starting_point()


(n, c) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
ans = 0
now = 0
s = []
t = []
for i in range(n):
	now += a[i][1]
	s.append(now)
now = 0
for i in range(n):
	now += a[n - 1 - i][1]
	t.append(now)
back = 0
ans = max(ans, s[-1] - a[-1][0])
for i in range(n - 2, -1, -1):
	back = max(back, t[n - 2 - i] - 2 * (c - a[i + 1][0]))
	ans = max(ans, s[i] - a[i][0] + back)
back = 0
ans = max(ans, t[-1] - (c - a[0][0]))
for i in range(n - 2, -1, -1):
	back = max(back, s[n - 2 - i] - 2 * a[n - 2 - i][0])
	ans = max(ans, t[i] - (c - a[n - i - 1][0]) + back)
print(ans)


(n, c) = map(int, input().split())
S = []
for i in range(n):
	(x, v) = map(int, input().split())
	S.append([x, v])
F = [0]
rF = [0]
s = 0
for i in range(n):
	s += S[i][1]
	F.append(max(s - S[i][0], F[-1]))
	rF.append(max(s - 2 * S[i][0], rF[-1]))
S = list(reversed(S))
R = [0]
rR = [0]
s = 0
for i in range(n):
	s += S[i][1]
	R.append(max(s + S[i][0] - c, R[-1]))
	rR.append(max(s + 2 * (S[i][0] - c), rR[-1]))
R = list(reversed(R))
rR = list(reversed(rR))
ans = 0
for i in range(n + 1):
	ans = max(F[i] + rR[i], ans)
	ans = max(R[i] + rF[i], ans)
print(ans)


import sys

def input():
	return sys.stdin.readline()[:-1]
import heapq
(N, C) = map(int, input().split())
a = 0
x = [0] * N
v = [0] * N
for i in range(N):
	(x[i], v[i]) = map(int, input().split())
h = [0]
s = t = sum(v)
for i in range(N - 1, -1, -1):
	k = t - x[i] - h[0]
	if k > a:
		a = k
	t -= v[i]
	heapq.heappush(h, t - s + 2 * (C - x[i]))
h = [0]
t = s
for i in range(N):
	k = t - C + x[i] - h[0]
	if k > a:
		a = k
	t -= v[i]
	heapq.heappush(h, t - s + 2 * x[i])
print(a)


import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7

class SegTree:

	def __init__(self, init_val, segfunc, ide_ele):
		n = len(init_val)
		self.segfunc = segfunc
		self.ide_ele = ide_ele
		self.num = 1 << (n - 1).bit_length()
		self.tree = [ide_ele] * 2 * self.num
		for i in range(n):
			self.tree[self.num + i] = init_val[i]
		for i in range(self.num - 1, 0, -1):
			self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

	def update(self, k, x):
		k += self.num
		self.tree[k] = x
		while k > 1:
			self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
			k >>= 1

	def query(self, left, right):
		res = self.ide_ele
		left += self.num
		right += self.num
		while left < right:
			if left & 1:
				res = self.segfunc(res, self.tree[left])
				left += 1
			if right & 1:
				res = self.segfunc(res, self.tree[right - 1])
			left >>= 1
			right >>= 1
		return res

def segfunc(x, y):
	return max(x, y)
ide_ele = -f_inf

def resolve():
	(n, c) = list(map(int, input().split()))
	XV = [list(map(int, input().split())) for _ in range(n)]
	R = [0]
	prev = 0
	for i in range(n):
		(x, v) = XV[i]
		R.append(R[-1] + v - (x - prev))
		prev = x
	seg = SegTree(R, segfunc, ide_ele)
	R_r = [0]
	prev_r = 0
	for i in reversed(list(range(n))):
		(x_r, v_r) = XV[i]
		x_r = c - x_r
		R_r.append(R_r[-1] + v_r - (x_r - prev_r))
		prev_r = x_r
	seg_r = SegTree(R_r, segfunc, ide_ele)
	res1 = max(R)
	res2 = max(R_r)
	res3 = 0
	for i in range(1, n + 1):
		right = R[i] - XV[i - 1][0]
		idx = n + 1 - i
		left = seg_r.query(0, idx)
		res3 = max(res3, right + left)
	res4 = 0
	for i in range(1, n + 1):
		left_r = R_r[i] - (c - XV[-i][0])
		idx_r = n + 1 - i
		right_r = seg.query(0, idx_r)
		res4 = max(res4, left_r + right_r)
	print(max(res1, res2, res3, res4))

def __starting_point():
	resolve()
__starting_point()


(n, c) = map(int, input().split())
s = [list(map(int, input().split())) for _ in range(n)]
(a, b) = ([0] * n, [0] * n)
a[0] = s[0][1] - s[0][0]
b[0] = s[n - 1][1] - (c - s[n - 1][0])
for i in range(1, n):
	a[i] = a[i - 1] + s[i][1] - (s[i][0] - s[i - 1][0])
	b[i] = b[i - 1] + s[n - 1 - i][1] - (s[n - i][0] - s[n - 1 - i][0])
(maxa, maxb) = ([0] * n, [0] * n)
(maxa[0], maxb[0]) = (a[0], b[0])
for i in range(1, n):
	maxa[i] = max(maxa[i - 1], a[i])
	maxb[i] = max(maxb[i - 1], b[i])
ans = max(maxa[n - 1], maxb[n - 1], 0)
for i in range(n - 1):
	ans = max(ans, a[i] + maxb[n - 2 - i] - s[i][0])
	ans = max(ans, b[i] + maxa[n - 2 - i] - (c - s[n - 1 - i][0]))
print(ans)


(N, C) = list(map(int, input().split()))
L = []
for i in range(N):
	(x, y) = list(map(int, input().split()))
	L.append([x, y])
l = L[::-1]
L = [[0, 0]] + L
R = []
for i in range(N):
	R.append([C - l[i][0], l[i][1]])
R = [[0, 0]] + R
TS = [0]
TW = [0]
RS = [0]
RW = [0]
for i in range(N):
	TS.append(TS[i] - L[i + 1][0] + L[i][0] + L[i + 1][1])
	TW.append(TW[i] - 2 * (L[i + 1][0] - L[i][0]) + L[i + 1][1])
	RS.append(RS[i] - R[i + 1][0] + R[i][0] + R[i + 1][1])
	RW.append(RW[i] - 2 * (R[i + 1][0] - R[i][0]) + R[i + 1][1])
MTS = [0]
MRS = [0]
for i in range(1, N + 1):
	MTS.append(max(MTS[i - 1], TS[i]))
	MRS.append(max(MRS[i - 1], RS[i]))
ans = 0
for i in range(N + 1):
	if i > 0 and L[i - 1][0] * 2 > C:
		break
	elif ans < TW[i] + MRS[N - i]:
		ans = TW[i] + MRS[N - i]
for i in range(N + 1):
	if i > 0 and R[i - 1][0] * 2 > C:
		break
	elif ans < RW[i] + MTS[N - i]:
		ans = RW[i] + MTS[N - i]
print(ans)


import numpy as np
import itertools
(N, C, *XV) = [int(_) for _ in open(0).read().split()]
XV = [0, 0] + XV
XV = np.array(XV).reshape((N + 1, 2))
YV = np.zeros((N + 1, 2), dtype=np.int64)
YV[1:] = XV[:0:-1]
YV[1:, 0] *= -1
YV[1:, 0] += C

def calc(XV, YV):
	cumxvv = np.cumsum(XV[:, 1])
	cumxvv -= 2 * XV[:, 0]
	cumyvv = np.cumsum(YV[:, 1])
	cumyvv -= YV[:, 0]
	cummaxyvv = list(itertools.accumulate(cumyvv, func=lambda a, b: a if a > b else b))
	ans = -float('inf')
	for i in range(N + 1):
		ans = max(ans, cumxvv[i] + cummaxyvv[N - i])
	return ans
print(max(calc(XV, YV), calc(YV, XV)))


(N, C) = map(int, input().split())
x = [0] * N
v = [0] * N
for i in range(N):
	(x[i], v[i]) = map(int, input().split())
lcal = [0] * N
lmax = [0] * N
rcal = [0] * N
rmax = [0] * N
rcal[0] = v[0] - x[0]
rmax[0] = rcal[0]
for i in range(1, N):
	rcal[i] = rcal[i - 1] + v[i] - x[i] + x[i - 1]
	rmax[i] = max(rmax[i - 1], rcal[i])
lcal[0] = v[N - 1] - (C - x[N - 1])
lmax[0] = lcal[0]
for i in range(1, N):
	lcal[i] = lcal[i - 1] + v[N - i - 1] + x[N - i - 1] - x[N - i]
	lmax[i] = max(lmax[i - 1], lcal[i])
ans = 0
for i in range(N - 1):
	ans = max(ans, rcal[i], rcal[i] + lmax[N - i - 2] - x[i])
ans = max(ans, rcal[N - 1])
for i in range(N - 1):
	ans = max(ans, lcal[i], lcal[i] + rmax[N - i - 2] - (C - x[N - i - 1]))
ans = max(ans, lcal[N - 1])
print(ans)