Teishi-zushi, a Japanese restaurant, has a single round counter with an outer circumference of C meters. Nakahashi enters the restaurant and is guided to the counter where N pieces of sushi are placed at specific distances x_i and have nutritive values v_i. Nakahashi can walk around the counter, consuming 1 kilocalorie per meter, and eat sushi at specific points. The goal is to find the maximum nutrition he can take in before leaving, considering the energy consumed. Constraints include N (1 ≤ N ≤ 10^5), C (2 ≤ C ≤ 10^14), x_i values, and v_i values (1 ≤ v_i ≤ 10^9). The input format is N C followed by N lines of x_i and v_i. The output should be the maximum nutrition Nakahashi can take in on balance before leaving. Sample inputs and outputs are provided for illustration.

# Buggy version:
n,c = list(map(int,input().split()))
xv = [list(map(int,input().split())) for i in range(n)]

frontGet = [0]*n
frontRuiseki = [0]*(n+1)
backGet = [0]*n
backRuiseki = [0]*(n+1)

for i in range(n):
    frontRuiseki[i+1] = frontRuiseki[i]+xv[i][1] 
    frontGet[i] = frontRuiseki[i+1] - xv[i][0]
    idx = -1 - i
    backRuiseki[idx-1] = backRuiseki[idx]+xv[idx][1] 
    backGet[idx] = backRuiseki[idx-1] + xv[idx][0] - c

backGet.reverse()
frontGet = [0] + frontGet
backGet = [0] + backGet
xv = [[0,0]] + xv
frontMax = [0]*(n+1)
backMax = [0]*(n+1)

for i in range(n):
    frontMax[i+1] = max(frontMax[i],frontGet[i])
    backMax[i+1] = max(backGet[i],backMax[i])

ans = 0
#print(frontGet,xv)
for i in range(n+1):
    front = frontGet[i] - xv[i][0]
    front += backMax[n - i]
    back = backGet[i] + xv[-i][0] - c
    back += frontMax[n - i]
    ans = max(ans,front,back)
print(ans)

# Fixed version:
n,c = list(map(int,input().split()))
xv = [list(map(int,input().split())) for i in range(n)]

frontGet = [0]*n
frontRuiseki = [0]*(n+1)
backGet = [0]*n
backRuiseki = [0]*(n+1)

for i in range(n):
    frontRuiseki[i+1] = frontRuiseki[i]+xv[i][1] 
    frontGet[i] = frontRuiseki[i+1] - xv[i][0]
    idx = -1 - i
    backRuiseki[idx-1] = backRuiseki[idx]+xv[idx][1] 
    backGet[idx] = backRuiseki[idx-1] + xv[idx][0] - c

backGet.reverse()
frontGet = [0] + frontGet
backGet = [0] + backGet
xv = [[0,0]] + xv
frontMax = [0]*(n+1)
backMax = [0]*(n+1)

for i in range(n):
    frontMax[i+1] = max(frontMax[i],frontGet[i+1])
    backMax[i+1] = max(backGet[i+1],backMax[i])

ans = max(frontMax[-1],backMax[-1],0)
for i in range(n+1):
    front = frontGet[i] - xv[i][0]
    front += backMax[n - i]
    back = backGet[i] + xv[-i][0] - c
    back += frontMax[n - i]
    ans = max(ans,front,back)
print(ans)

# Buggy version:
from itertools import*
from numpy import array
n,c=map(int,input().split())

l1=[]
l2=[]
for i in range(n):
    x,v=map(int,input().split())
    l1.append(x)
    l2.append(v)
#print(l1,l2)

ll=array([0]+l1)
lll=array([0]+list(accumulate(l2)))
lr=array([0]+[c-i for i in l1][::-1])
llr=array([0]+list(accumulate(l2[::-1])))
x=lll-ll
y=llr-lr
for i in range(2,n+1):
    x[i]=max(x[i-1],x[i])
    y[i]=max(y[i-1],y[i])
z=array([min(ll[i],lr[i])for i in range(n+1)])
print(max(x[::-1]+y-z))

# Fixed version:
from itertools import*
from numpy import array
n,c=map(int,input().split())

l1=[]
l2=[]
for i in range(n):
    x,v=map(int,input().split())
    l1.append(x)
    l2.append(v)
#print(l1,l2)

ll=array([0]+l1)
lll=array([0]+list(accumulate(l2)))
lr=array([0]+[c-i for i in l1][::-1])
llr=array([0]+list(accumulate(l2[::-1])))
x=lll-ll
y=llr-lr
for i in range(1,n+1):
    x[i]=max(x[i-1],x[i])
    y[i]=max(y[i-1],y[i])
z=array([min(ll[::-1][i],lr[i])for i in range(n+1)])
print(max(x[::-1]+y-z))

# Buggy version:
n,c = map(int,input().split())
x = [0]
v = [0]
rf = [0] * (n+2)
rg = [0] * (n+2)
lf = [0] * (n+2)
lg = [0] * (n+2)
for i in range(0,n):
    X,V = map(int,input().split())
    x.append(X)
    v.append(V)
for i in range(0,n):
    rf[i+1] = rf[i] + v[i+1]
    rg[i+1] = max(rg[i],rf[i+1] - x[i+1])
for i in range(n+1,1,-1):
    lf[i-1] = lf[i] + v[i-1]
    lg[i-1] = max(lg[i],lf[i-1] - (c-x[i-1]))

#print(lg[::-1])
#print(rg)

for i in range(1,n):
    ans = max(ans,rg[i]+lg[i+1] - x[i],rg[i]+lg[i+1] - (c - x[i+1]))
print(ans)

# Fixed version:
n,c = map(int,input().split())
x = [0]
v = [0]
rf = [0] * (n+2)
rg = [0] * (n+2)
lf = [0] * (n+2)
lg = [0] * (n+2)
for i in range(0,n):
    X,V = map(int,input().split())
    x.append(X)
    v.append(V)
for i in range(0,n):
    rf[i+1] = rf[i] + v[i+1]
    rg[i+1] = max(rg[i],rf[i+1] - x[i+1])
for i in range(n+1,1,-1):
    lf[i-1] = lf[i] + v[i-1]
    lg[i-1] = max(lg[i],lf[i-1] - (c-x[i-1]))

ans = max(rg[n],lg[1])
for i in range(1,n):
    ans = max(ans,rg[i]+lg[i+1] - x[i],rg[i]+lg[i+1] - (c - x[i+1]))
print(ans)

# Buggy version:
n, c = map(int, input().split())
sushi = []
for i in range(n):
    x, v = map(int, input().split())
    sushi.append((x, v))
sushi.sort()
sushi_reversed = list(reversed([(c - x, v) for x, v in sushi]))


def accum_max(s):
    v = 0
    res = [(0, 0)]
    for x_i, v_i in s:
        v += v_i
        res.append(max(res[-1], (v-x_i, x_i)))
    return res[1:]


max_sushi = accum_max(sushi)
max_sushi_reversed = accum_max(sushi_reversed)


def solve(s, msr):
    v_max = 0
    v_2d_max = 0
    v = 0
    n = len(s)
    for i in range(n-1):
        x_i, v_i = s[i]
        v += v_i
        if v - x_i*2 > v_2d_max:
            v_2d_max = v - x_i*2
            v_sub = msr[n - 2 - i][0]
            v_max = max(v_max, v_2d_max + v_sub)
            print(i,v_max,v_2d_max,v_sub)
    return v_max


l = solve(sushi, max_sushi_reversed)
r = solve(sushi_reversed, max_sushi)
print(max(l, r, max_sushi[-1][0], max_sushi_reversed[-1][0]))


# Fixed version:
n, c = map(int, input().split())
sushi = []
for i in range(n):
    x, v = map(int, input().split())
    sushi.append((x, v))
sushi.sort()
sushi_reversed = list(reversed([(c - x, v) for x, v in sushi]))


def accum_max(s):
    v = 0
    res = [(0, 0)]
    for x_i, v_i in s:
        v += v_i
        res.append(max(res[-1], (v-x_i, x_i)))
    return res[1:]


max_sushi = accum_max(sushi)
max_sushi_reversed = accum_max(sushi_reversed)


def solve(s, msr):
    v_max = 0
    v_2d_max = 0
    v = 0
    n = len(s)
    for i in range(n-1):
        x_i, v_i = s[i]
        v += v_i
        if v - x_i*2 > v_2d_max:
            v_2d_max = v - x_i*2
            v_sub = msr[n - 2 - i][0]
            v_max = max(v_max, v_2d_max + v_sub)
    return v_max


l = solve(sushi, max_sushi_reversed)
r = solve(sushi_reversed, max_sushi)
print(max(l, r, max_sushi[-1][0], max_sushi_reversed[-1][0]))

# Buggy version:
n,c= [int(i) for i in input().split()]
t_tk_r=0
t_md_r=0
g_tk_r=0
g_md_r=0
t_tk=[]
t_md=[]
g_tk=[]
g_md=[]
a=[]
d1=0
for i in range(n):
  a.append([int(i) for i in input().split()])
for d0,v0 in a:
  t_tk_r+=v0-(d0-d1)
  t_md_r+=v0-(d0-d1)*2
  t_tk.append(t_tk_r)
  t_md.append(t_md_r)
  d1=d0
d1=0  
for d0,v0 in a[::-1]:
  g_tk_r+=v0-(c-d0)-d1
  g_md_r+=v0-((c-d0)-d1)*2
  g_tk.append(g_tk_r)
  g_md.append(g_md_r)
  d1=(c-d0)
mx=max(max(t_tk),max(g_tk),0)
for i in range(1,n):
  print(t_tk[:i],g_md[:n-i])
  mx=max(mx,max(t_tk[:i])+max(g_md[:n-i]),max(t_md[:i])+max(g_tk[:n-i]))


# Fixed version:
n,c= [int(i) for i in input().split()]
t_tk_r=0
t_md_r=0
g_tk_r=0
g_md_r=0
t_tk_m=0
t_md_m=0
g_tk_m=0
g_md_m=0
t_tk=[]
t_md=[]
g_tk=[]
g_md=[]
a=[]
d1=0

for i in range(n):
  a.append([int(i) for i in input().split()])

for d0,v0 in a:
  t_tk_r += v0-(d0-d1)
  t_md_r += v0-(d0-d1)*2
  t_tk_m = max(t_tk_m,t_tk_r)
  t_md_m = max(t_md_m,t_md_r)
  t_tk.append(t_tk_m)
  t_md.append(t_md_m)
  d1=d0
  
d1=0  
for d0,v0 in a[::-1]:
  g_tk_r += v0-((c-d0)-d1)
  g_md_r += v0-((c-d0)-d1)*2
  g_tk_m = max(g_tk_m,g_tk_r)
  g_md_m = max(g_md_m,g_md_r)
  g_tk.append(g_tk_m)
  g_md.append(g_md_m)
  d1=(c-d0) 
mx=max(max(t_tk),max(g_tk),0)

for i in range(n-1):
  mx=max(mx,t_tk[i]+g_md[n-2-i],t_md[i]+g_tk[n-i-2])
print(mx)

# Buggy version:
N, C = map(int, input().split())
X = []
V = []
for i in range(N):
  x, v = map(int, input().split())
  X.append(x)
  V.append(v)

frontS = []
prevX = 0
v = 0
for i in range(N):
  v = v + V[i] - (X[i] - prevX)
  frontS.append(v)
  prevX= X[i]

# 後ろi個の中での累計MAX
backMaxS = []
prevX = C
v = 0
tmpMax = float('-inf')
for i in range(N-1, -1, -1):
  v = v + V[i] - (prevX - X[i])
  tmpMax = max(tmpMax, v)
  backMaxS.append(tmpMax)
  prevX = X[i]
backMaxS.reverse()
backMaxS.append(0)

ans = backMaxS[0]
for i in range(N):
  # X個まで食べて、退出 or 引き返す
  ans = max(
    ans,
    frontS[i],
    frontS[i] - X[i] + backMaxS[i+1]
  )

print(ans)

# Fixed version:
N, C = map(int, input().split())
X = []
V = []
for i in range(N):
  x, v = map(int, input().split())
  X.append(x)
  V.append(v)

frontS = []
# 前i個の中での累計MAX
frontMaxS = []
prevX = 0
v = 0
tmpMax = float('-inf')
for i in range(N):
  v = v + V[i] - (X[i] - prevX)
  frontS.append(v)
  tmpMax = max(tmpMax, v)
  frontMaxS.append(tmpMax)
  prevX= X[i]
frontMaxS.reverse()
frontMaxS.append(0)

backS = []
# 後ろi個の中での累計MAX
backMaxS = []
prevX = C
v = 0
tmpMax = float('-inf')
for i in range(N-1, -1, -1):
  v = v + V[i] - (prevX - X[i])
  backS.append(v)
  tmpMax = max(tmpMax, v)
  backMaxS.append(tmpMax)
  prevX = X[i]
backMaxS.reverse()
backMaxS.append(0)

ans = max(0, backMaxS[0], frontMaxS[0])
for i in range(N):
  # X個まで食べて、退出 or 引き返す
  ans = max(
    ans,
    frontS[i],
    frontS[i] - X[i] + backMaxS[i+1],
    backS[i],
    backS[i] - (C-X[-i-1]) + frontMaxS[i+1]
  )

print(ans)

# Buggy version:
from functools import reduce

import numpy as np


def get_max():
    a = 0

    def _max(x):
        nonlocal a
        a = max(a, x)
        return a

    return _max


def solve(fwd, bck):
    dp = []
    cur = 0
    for x, v in fwd:
        cur -= x
        cur += v
        dp.append(cur)

    dp = list(map(get_max(), dp))

    cur = 0
    ans = 0
    for i, (x, v) in enumerate(bck[:-1]):
        cur -= 2 * x
        cur += v
        # print(i, x, v, ans, cur, dp[n - i - 2])
        ans = max(ans, dp[n - i - 2] + cur)
        # print(i, x, v, ans, cur, dp[n - i - 2])

    return ans


n, c = map(int, input().split())
sushi = [list(map(int, input().split())) for _ in range(n)]

fwd = np.array(sushi).T
fwd[0] = np.diff(np.concatenate(([0], fwd[0])))
fwd = fwd.T.tolist()

bck = np.array(sushi).T
bck[0] = np.diff(np.concatenate(([0], (c - bck[0])[::-1])))
bck[1] = bck[1][::-1]
bck = bck.T.tolist()

print(max(solve(fwd, bck), solve(bck, fwd)))


# Fixed version:
import numpy as np


def get_max():
    a = 0

    def _max(x):
        nonlocal a
        a = max(a, x)
        return a

    return _max


def solve(fwd, bck):
    dp = []
    cur = 0
    for x, v in fwd:
        cur -= x
        cur += v
        dp.append(cur)

    dp = list(map(get_max(), dp))

    cur = 0
    ans = dp[-1]
    for i, (x, v) in enumerate(bck[:-1]):
        cur -= 2 * x
        cur += v
        # print(i, x, v, ans, cur, dp[n - i - 2])
        ans = max(ans, dp[n - i - 2] + cur)
        # print(i, x, v, ans, cur, dp[n - i - 2])

    return ans


n, c = map(int, input().split())
sushi = [list(map(int, input().split())) for _ in range(n)]

fwd = np.array(sushi).T
fwd[0] = np.diff(np.concatenate(([0], fwd[0])))
fwd = fwd.T.tolist()

bck = np.array(sushi).T
bck[0] = np.diff(np.concatenate(([0], (c - bck[0])[::-1])))
bck[1] = bck[1][::-1]
bck = bck.T.tolist()

# print(fwd)
# print(bck)

print(max(solve(fwd, bck), solve(bck, fwd)))