N,C = list(map(int,input().split()))

A = [list(map(int,input().split())) for _ in range(N)]

V = [0 for _ in range(N)]

Vm = [0 for _ in range(N)]

V[0] = A[0][1]-A[0][0]

Vm[0] = V[0]

for i in range(1,N):

    V[i] = V[i-1]+A[i][1]-(A[i][0]-A[i-1][0])

    Vm[i] = max(V[i],Vm[i-1])

U = [0 for _ in range(N)]

Um = [0 for _ in range(N)]

U[0] = A[-1][1]-(C-A[-1][0])

Um[0] = U[0]

for j in range(2,N+1):

    U[j-1] = U[j-2]+A[-j][1]-(A[-(j-1)][0]-A[-j][0])

    Um[j-1] = max(U[j-1],Um[j-2])

ans = V[N-1]

for i in range(N-1):

    ans = max(ans,V[i],V[i]-A[i][0]+Um[N-2-i])

ans = max(ans,U[N-1])

for j in range(N-1):

    ans = max(ans,U[j],U[j]-(C-A[-(j+1)][0])+Vm[N-2-j])

print((max(ans,0)))
import heapq

N,C = list(map(int,input().split()))

X = [list(map(int,input().split())) for _ in range(N)]

X.insert(0,[0,0])

A = [0 for _ in range(N+1)]

for i in range(1,N+1):

    A[i] = A[i-1]+X[i][1]

A = [A[i]-X[i][0] for i in range(N+1)]

B = [0 for _ in range(N+1)]

B[N] = X[N][1]

for i in range(N-1,0,-1):

    B[i] = B[i+1]+X[i][1]

B = [B[i]-(C-X[i][0]) for i in range(N+1)]

heap = []

for i in range(1,N+1):

    heapq.heappush(heap,(-B[i],i))

vmax = 0

for i in range(1,N):

    cnt = A[i]

    vmax = max(vmax,cnt)

    cnt -= X[i][0]

    b,j = heap[0]

    while heap and j<=i:

        heapq.heappop(heap)

        if heap:

            b,j = heap[0]

    b = -b

    cnt += b

    vmax = max(vmax,cnt)

vmax = max(vmax,A[N])

heap = []

for i in range(1,N+1):

    heapq.heappush(heap,(-A[i],i))

for i in range(N,1,-1):

    cnt = B[i]

    vmax = max(vmax,cnt)

    cnt -= (C-X[i][0])

    b,j = heap[0]

    while heap and j>=i:

        heapq.heappop(heap)

        if heap:

            b,j = heap[0]

    b = -b

    cnt += b

    vmax = max(vmax,cnt)

vmax = max(vmax,B[1])

print(vmax)
N, C = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(N)]
V = [0 for _ in range(N)]
Vm = [0 for _ in range(N)]
V[0] = A[0][1] - A[0][0]
Vm[0] = V[0]
for i in range(1, N):
    V[i] = V[i - 1] + A[i][1] - (A[i][0] - A[i - 1][0])
    Vm[i] = max(V[i], Vm[i - 1])
U = [0 for _ in range(N)]
Um = [0 for _ in range(N)]
U[0] = A[-1][1] - (C - A[-1][0])
Um[0] = U[0]
for j in range(2, N + 1):
    U[j - 1] = U[j - 2] + A[-j][1] - (A[-(j - 1)][0] - A[-j][0])
    Um[j - 1] = max(U[j - 1], Um[j - 2])
ans = V[N - 1]
for i in range(N - 1):
    ans = max(ans, V[i], V[i] - A[i][0] + Um[N - 2 - i])
ans = max(ans, U[N - 1])
for j in range(N - 1):
    ans = max(ans, U[j], U[j] - (C - A[-(j + 1)][0]) + Vm[N - 2 - j])
print((max(ans, 0)))

import heapq

N, C = list(map(int, input().split()))
X = [list(map(int, input().split())) for _ in range(N)]
X.insert(0, [0, 0])
A = [0 for _ in range(N + 1)]
for i in range(1, N + 1):
    A[i] = A[i - 1] + X[i][1]
A = [A[i] - X[i][0] for i in range(N + 1)]
B = [0 for _ in range(N + 1)]
B[N] = X[N][1]
for i in range(N - 1, 0, -1):
    B[i] = B[i + 1] + X[i][1]
B = [B[i] - (C - X[i][0]) for i in range(N + 1)]
heap = []
for i in range(1, N + 1):
    heapq.heappush(heap, (-B[i], i))
vmax = 0
for i in range(1, N):
    cnt = A[i]
    vmax = max(vmax, cnt)
    cnt -= X[i][0]
    b, j = heap[0]
    while heap and j <= i:
        heapq.heappop(heap)
        if heap:
            b, j = heap[0]
    b = -b
    cnt += b
    vmax = max(vmax, cnt)
vmax = max(vmax, A[N])
heap = []
for i in range(1, N + 1):
    heapq.heappush(heap, (-A[i], i))
for i in range(N, 1, -1):
    cnt = B[i]
    vmax = max(vmax, cnt)
    cnt -= C - X[i][0]
    b, j = heap[0]
    while heap and j >= i:
        heapq.heappop(heap)
        if heap:
            b, j = heap[0]
    b = -b
    cnt += b
    vmax = max(vmax, cnt)
vmax = max(vmax, B[1])
print(vmax)

+import heapq + -A = [list(map(int, input().split())) for _ in range(N)] -V = [0 for _ in range(N)] -Vm = [0 for _ in range(N)] -V[0] = A[0][1] - A[0][0] -Vm[0] = V[0] +X = [list(map(int, input().split())) for _ in range(N)] +X.insert(0, [0, 0]) +A = [0 for _ in range(N + 1)] +for i in range(1, N + 1): +    A[i] = A[i - 1] + X[i][1] +A = [A[i] - X[i][0] for i in range(N + 1)] +B = [0 for _ in range(N + 1)] +B[N] = X[N][1] +for i in range(N - 1, 0, -1): +    B[i] = B[i + 1] + X[i][1] +B = [B[i] - (C - X[i][0]) for i in range(N + 1)] +heap = [] +for i in range(1, N + 1): +    heapq.heappush(heap, (-B[i], i)) +vmax = 0 -    V[i] = V[i - 1] + A[i][1] - (A[i][0] - A[i - 1][0]) -    Vm[i] = max(V[i], Vm[i - 1]) -U = [0 for _ in range(N)] -Um = [0 for _ in range(N)] -U[0] = A[-1][1] - (C - A[-1][0]) -Um[0] = U[0] -for j in range(2, N + 1): -    U[j - 1] = U[j - 2] + A[-j][1] - (A[-(j - 1)][0] - A[-j][0]) -    Um[j - 1] = max(U[j - 1], Um[j - 2]) -ans = V[N - 1] -for i in range(N - 1): -    ans = max(ans, V[i], V[i] - A[i][0] + Um[N - 2 - i]) -ans = max(ans, U[N - 1]) -for j in range(N - 1): -    ans = max(ans, U[j], U[j] - (C - A[-(j + 1)][0]) + Vm[N - 2 - j]) -print((max(ans, 0))) +    cnt = A[i] +    vmax = max(vmax, cnt) +    cnt -= X[i][0] +    b, j = heap[0] +    while heap and j <= i: +        heapq.heappop(heap) +        if heap: +            b, j = heap[0] +    b = -b +    cnt += b +    vmax = max(vmax, cnt) +vmax = max(vmax, A[N]) +heap = [] +for i in range(1, N + 1): +    heapq.heappush(heap, (-A[i], i)) +for i in range(N, 1, -1): +    cnt = B[i] +    vmax = max(vmax, cnt) +    cnt -= C - X[i][0] +    b, j = heap[0] +    while heap and j >= i: +        heapq.heappop(heap) +        if heap: +            b, j = heap[0] +    b = -b +    cnt += b +    vmax = max(vmax, cnt) +vmax = max(vmax, B[1]) +print(vmax)