Problem:
 Score : 500 points    Problem Statement "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 1 3 20
2 80
9 120
16 1      Sample Output 1 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.       Sample Input 2 3 20
2 80
9 1
16 120      Sample Output 2 192  The second and third sushi have been swapped. Again, if he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks six more meters counterclockwise this time, 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 8 kilocalories, thus he can take in 192 kilocalories on balance, which is the largest possible value.       Sample Input 3 1 100000000000000
50000000000000 1      Sample Output 3 0  Even though the only sushi is so far that it does not fit into a 32 -bit integer, its nutritive value is low, thus he should immediately leave without doing anything.       Sample Input 4 15 10000000000
400000000 1000000000
800000000 1000000000
1900000000 1000000000
2400000000 1000000000
2900000000 1000000000
3300000000 1000000000
3700000000 1000000000
3800000000 1000000000
4000000000 1000000000
4100000000 1000000000
5200000000 1000000000
6600000000 1000000000
8000000000 1000000000
9300000000 1000000000
9700000000 1000000000      Sample Output 4 6500000000  All these sample inputs above are included in the test set for the partial score.

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    s = S()
    s.append("o")
    a = s.count("o")-1
    print(700+a*100)
    return


def B():
    n, x = LI()
    m = IR(n)
    m.sort()
    msum = sum(m)
    print(n + (x - msum) // m[0])
    return


def C():
    a, b, c, x, y = LI()
    if c * 2 > a + b:
        print(x * a + y * b)
        return
    if x > y:
        if c * 2 > a:
            print(c * 2 * y + (x - y) * a)
            return
        print(c * 2 * x)
        return
    if c * 2 > b:
        print(c * 2 * x + (y - x) * b)
        return
    print(c * 2 * y)

    return


def D():
    n, c = LI()
    clock_wise = [0] * n
    clock_wise_max = [0] * n
    clock_wise_rev = [0] * n
    clock_wise_rev_max = [0] * n
    xv = LIR(n)
    clock_wise[0] = xv[0][1] - xv[0][0]
    clock_wise_rev[-1] = - c + xv[-1][1] + xv[-1][0]
    for i in range(1, n):
        clock_wise[i] = clock_wise[i - 1] + xv[i][1] - xv[i][0] + xv[i - 1][0]
        clock_wise_rev[-i - 1] = clock_wise_rev[-i] + xv[-i - 1][1] + xv[-i - 1][0] - xv[-i][0]
    clock_wise_max[0] = clock_wise[0]
    clock_wise_rev_max[-1] = clock_wise_rev[-1]
    for i in range(1,n):
        clock_wise_max[i] = max(clock_wise[i], clock_wise_max[i - 1])
        clock_wise_rev_max[-i - 1] = max(clock_wise_rev[-i - 1], clock_wise_rev_max[-i])
    ans = max(0, max(clock_wise), max(clock_wise_rev))
    for i in range(n - 1):
        ans = max(ans, clock_wise[i] + clock_wise_rev_max[i + 1] - xv[i][0])
        ans = max(ans, clock_wise_rev[-i - 1] + clock_wise_max[-i - 2] - (c - xv[-i - 1][0]))
    print(ans)
    return


if __name__ == '__main__':
    D()

Solution (Python):
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)

Solution (Python):
N,C = map(int,input().split())
XV = [list(map(int,input().split())) for i in range(N)]

migiM = [0]
tmp = 0
for i in range(N):
    tmp =  tmp + XV[i][1]
    migiM.append(max(migiM[-1],tmp-XV[i][0]))

hidariM = [0]
tmp = 0
for i in range(N):
    tmp =  tmp + XV[N-i-1][1]
    hidariM.append(max(hidariM[-1],tmp-C+XV[N-i-1][0]))
    
ans = max(migiM[N],hidariM[N])
tmp = 0
for i in range(N):
    tmp = tmp + XV[i][1]
    if ans < tmp + hidariM[N-1-i] - 2*XV[i][0]:
        ans = tmp + hidariM[N-1-i] - 2*XV[i][0]
        
tmp = 0
for i in range(N):
    tmp = tmp + XV[N-1-i][1]
    if ans < tmp + migiM[N-1-i]  - 2*(C-XV[N-1-i][0]):
        ans = tmp + migiM[N-1-i] - 2*(C-XV[N-1-i][0])


print(ans)

Solution (Python):
N, C = map(int, input().split())

xv = []
for i in range(N):
    x,v = map(int, input().split())
    xv.append((x,v))

fa = [0] * N
fa[0] = xv[0][1] - xv[0][0]
fb = [0] * N
fb[0] = xv[-1][1] - (C - xv[-1][0])
for i in range(1,N):
    fa[i] = fa[i-1] + xv[i][1] + xv[i-1][0] - xv[i][0]
    fb[i] = fb[i-1] + xv[N-1-i][1] + (C - xv[N-i][0]) - (C - xv[N-1-i][0])


max_fa = [0] * (N+1)
max_fa[0] = 0
max_fa[1] = max(0,fa[0])
max_fb = [0] * (N+1)
max_fb[0] = 0
max_fb[1] = max(0,fb[0])
for i in range(2,N+1):
    max_fa[i] = max(max_fa[i-1], fa[i-1])
    max_fb[i] = max(max_fb[i-1], fb[i-1])

ans_a = ans_b = 0

for i in range(N):
    ans_a = max(ans_a, max_fa[N-1-i] + fb[i] - (C - xv[N-1-i][0]) )
    ans_b = max(ans_b, max_fb[N-1-i] + fa[i] - xv[i][0])

ans_a = max(ans_a, max_fa[-1]) 
ans_b = max(ans_b, max_fb[-1]) 
print(max(ans_a, ans_b))

Solution (Python):
n,c = map(int,input().split())
data = []
for i in range(n):
    x,v = map(int,input().split())
    data.append([x,c-x,v])

value_right = [data[0][2]]
for x,y,z in data[1:]:
    value_right.append(value_right[-1]+z)

value_left = [data[n-1][2]]
for x,y,z in data[::-1][1:]:
    value_left.append(value_left[-1]+z)
value_left = value_left[::-1]

opt_right = []
now_opt = 0
for i in range(n):
    next_opt = max(now_opt,value_right[i]-data[i][0])
    opt_right.append(next_opt)
    now_opt = next_opt


opt_left = []
now_opt = 0
for i in range(n-1,-1,-1):
    next_opt = max(now_opt,value_left[i]-data[i][1])
    opt_left.append(next_opt)
    now_opt = next_opt



ans = 0
for turn in range(n):
    now = -data[turn][0] + value_right[turn]
    
    ans = max(ans,now)
    now -= data[turn][0]
    if turn < n-1:
        
        ans = max(ans,now + opt_left[n-2-turn])

for turn in range(n-1,-1,-1):
    now = -data[turn][1] + value_left[turn]
    
    ans = max(ans,now)
    now -= data[turn][1]
    if turn > 0:
        
        ans = max(ans,now + opt_right[turn-1])

print(ans)

Solution (Python):
n,c=[int(j) for j in input().split()]
xv=[]
for i in range(n):
    xv.append([int(j) for j in input().split()])
ll=[[xv[0][0],max(0,xv[0][1]-xv[0][0])]]
lr=[[c-xv[-1][0],max(0,xv[-1][1]-c+xv[-1][0])]]
tmp=xv[0][1]
for i in range(1,n):
    x,v=xv[i]
    tmp+=v
    ll.append([x,max(tmp-x,ll[-1][1])])
tmp=xv[-1][1]
for i in range(n-1)[::-1]:
    x,v=xv[i]
    x=c-x
    tmp+=v
    lr.append([x,max(tmp-x,lr[-1][1])])
ans=0
for i in range(n+1):
    if i==0:
        ans=max(ans,lr[n-1][1])
    elif i==n:
        ans=max(ans,ll[n-1][1])
    else:
        ans=max(ans,ll[i-1][1]+lr[n-i-1][1]-ll[i-1][0])
        ans=max(ans,ll[i-1][1]+lr[n-i-1][1]-lr[n-i-1][0])

print(ans)

Solution (Python):
n,c=map(int,input().split())
l=[list(map(int,input().split())) for _ in [0]*n]
if n==1:print(max(0,l[0][1]-l[0][0]));exit()
r=[[c-x[0],x[1]] for x in l[::-1]]

L=[[0]*2 for _ in range(n)]
L[0]=[l[0][1]-l[0][0],l[0][1]-l[0][0]*2]
for i in range(n-1):
    L[i+1][0]=L[i][0]+l[i+1][1]-(l[i+1][0]-l[i][0])
    L[i+1][1]=L[i][1]+l[i+1][1]-(l[i+1][0]-l[i][0])*2
for i in range(n-1):
    L[i+1][0]=max(L[i+1][0],L[i][0])

R=[[0]*2 for _ in range(n)]
R[0]=[r[0][1]-r[0][0],r[0][1]-r[0][0]*2]
for i in range(n-1):
    R[i+1][0]=R[i][0]+r[i+1][1]-(r[i+1][0]-r[i][0])
    R[i+1][1]=R[i][1]+r[i+1][1]-(r[i+1][0]-r[i][0])*2
for i in range(n-1):
    R[i+1][0]=max(R[i+1][0],R[i][0])

max1=max(L[n-1][0],max(R[i][1]+L[n-i-2][0] for i in range(n-1)))
max2=max(R[n-1][0],max(L[i][1]+R[n-i-2][0] for i in range(n-1)))
print(max(0,max1,max2))

Solution (Python):
import sys

N,C = [int(x) for x in input().split()]
X=[]
V=[]
for _ in range(N):
    x,v = [int(x) for x in input().split()]
    X.append(x)
    V.append(v)


s = 0
r = [0]*N
for i in range(N):
    s += V[i]
    r[i] = s - X[i]


s = 0
l = [0]*N
for i in range(N):
    s += V[N-1-i]
    l[N - 1 - i] = s - (C - X[N - 1 - i])


MaxR = [0] * N
MaxR[0] = r[0]
for i in range(1,N):
    MaxR[i] = max(MaxR[i-1], r[i])


MaxL = [0] * N
MaxL[N-1] = l[N-1]
for i in range(1,N):
    MaxL[N-1-i] = max(MaxL[N-i], l[N-1-i])

a = max(r)
b = max(l)
c, d = 0, 0
if N>1:
    c = max([r[i] - X[i] + MaxL[i + 1] for i in range(N - 1)])
    d = max([l[i] - (C-X[i]) + MaxR[i - 1] for i in range(N - 1,0,-1)])

print(max([a,b,c,d,0]))

Solution (Python):
N,C=map(int,input().split())
xv=[list(map(int,input().split())) for i in range(N)]
from itertools import accumulate
vc=list(accumulate([v for x,v in xv]))
vc=[vc[i]-x for i,(x,v) in enumerate(xv)]
va=list(accumulate([v for x,v in xv[::-1]]))
va=[va[i]-C+x for i,(x,v) in enumerate(xv[::-1])]
mc=[0]*N
for i in range(N):
    mc[i]=i if vc[mc[i-1]]<vc[i] else mc[i-1]
ma=[0]*N
for i in range(N):
    ma[i]=i if va[ma[i-1]]<va[i] else ma[i-1]
r=max(0,vc[mc[-1]])
for i in range(N-1):
    r=max(r,va[i]+vc[mc[-i-2]]-C+xv[-i-1][0])
r=max(r,va[ma[-1]])
for i in range(N-1):
    r=max(r,vc[i]+va[ma[-i-2]]-xv[i][0])
print(r)

Solution (Python):
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from math import *
from itertools import *
from copy import *

N, C = map(int, input().split())
f1 = [list(map(int, input().split())) for i in range(N)]
f2 = deepcopy(f1)[::-1]
for i in range(N-1):
    f1[i+1][1] += f1[i][1]
    f2[i+1][1] += f2[i][1]
f1 = [[0, 0, 0]] + [(v-x, x, i) for i, (x, v) in enumerate(f1, 1)]
f2 = [[0, 0, 0]] + [(v-C+x, C-x, i) for i, (x, v) in enumerate(f2, 1)]
g1 = [0] * (N+1)
g2 = [0] * (N+1)
for i in range(1, N+1):
    g1[i] = max(f1[i][0], g1[i-1])
    g2[i] = max(f2[i][0], g2[i-1])
ans = 0
for i in range(N+1):
    ans = max(ans, f1[i][0]+g2[N-i]-f1[i][1], f2[i][0]+g1[N-i]-f2[i][1])
print(ans)

Solution (Python):
N, C = map(int, input().split())
X = [0] * N
V = [0] * N
for i in range(N):
    x, v = map(int, input().split())
    X[i] = x
    V[i] = v


answer = 0
R = [0] * N
score = 0
maxscore = 0
for i in range(N):
    score += V[i]
    R[i] = max(maxscore, score - X[i])
    maxscore = R[i]
    answer = max(answer, R[i])

L = [0] *N
score = 0
maxscore = 0
for i in range(N)[::-1]:
    score += V[i]
    L[i] = max(maxscore, score - (C - X[i]))
    maxscore = L[i]
    answer = max(answer, L[i])
L.append(0)

for i in range(N-1):
    answer = max(answer, R[i] - X[i] + L[i+1])

for i in range(1, N)[::-1]:
    answer = max(answer, L[i] - (C-X[i]) + R[i-1])

print(answer)

Solution (Python):
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 = [0] * (N + 1)
A_max = [0] * (N + 1)
B = [0] * (N + 1)
B_max = [0] * (N + 1)
a_sum = 0
b_sum = 0
for i in range(N):
  a_sum += V[i]
  A[i + 1] = a_sum - X[i]
  A_max[i + 1] = max(A_max[i], A[i + 1])
  b_sum += V[- i - 1]
  B[i + 1] = b_sum - (C - X[- i - 1])
  B_max[i + 1] = max(B_max[i], B[i + 1])

X = [0] + X
V = [0] + V

m = max(0, A_max[-1], B_max[-1])
for i in range(1, N + 1):
  m = max(m, A[i] + B_max[N - i] - X[i], B[i] + A_max[N - i] - (C - X[N - i + 1]))

print(m)

Solution (Python):
import sys
input = sys.stdin.readline

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



left_max = 0
left_double_max = 0

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

right_max = 0
right_double_max = 0

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


left_sum = 0

for i, sushi in enumerate(sushi_array):
    left_sum += sushi[1]
    left_max = max(left_max, left_sum - sushi[0])
    left_double_max = max(left_double_max, left_sum - 2 * sushi[0])
    left_array[i+1][0] = left_max
    left_array[i+1][1] = left_double_max

right_sum = 0

for i, sushi in enumerate(sushi_array[::-1]):
    right_sum += sushi[1]
    right_max = max(right_max, right_sum - (C - sushi[0]))
    right_double_max = max(right_double_max, right_sum - 2 * (C - sushi[0]))
    right_array[N-i-1][0] = right_max
    right_array[N-i-1][1] = right_double_max

ans = 0

for left, right in zip(left_array, right_array):
    ans = max(ans, left[0]+right[1], left[1]+right[0])

print(ans)

Solution (Python):
N, c = map(int, input().split())
S = [list(map(int, input().split())) for i in range(N)]

sum_ = 0
r = [0]
for x, v in S:
    sum_ += v
    r.append(sum_-x)

sum_ = 0
l = [0]
for x, v in S[::-1]:
    sum_ += v
    l.append(sum_- (c - x))

def right():
    max_ = r[0]
    for i in r:
        if max_ < i:
            max_ = i
        yield max_

def left():
    max_ = l[0]
    for i in l:
        if max_ < i:
            max_ = i
        yield max_


rm = [[0, 0]] + S
lm = S + [[c, c]]

rpath = list(right())
l_candi = [0]*N
for i in range(N):
    l_candi[i] = l[i] + rpath[-(i + 1)] - (c-lm[-(i + 1)][0])
lmax = max(l_candi)

lpath = list(left())
r_candi = [0]*N
for i in range(N):
    r_candi[i] = r[i] + lpath[-(i + 1)] -rm[i][0]
rmax = max(r_candi)

print(max(rmax, lmax))

Solution (Python):

import sys
from itertools import accumulate
INF = float("inf")


def solve(N: int, C: int, x: "List[int]", v: "List[int]"):

    
    nt_cost = x
    nt_benefit = accumulate(v)
    nt_callory = [b-c for c, b in zip(nt_cost, nt_benefit)]
    nt_callory_acc = list(accumulate([0]+nt_callory, max))

    
    rnt_cost = [C-v for v in reversed(x)]
    rnt_benefit = accumulate(reversed(v))
    rnt_callory = [b-c for c, b in zip(rnt_cost, rnt_benefit)]
    rnt_callory_acc = list(accumulate([0]+rnt_callory, max))

    
    m = max(nt_callory_acc[-1], rnt_callory_acc[-1])
    for i in range(N-1):
        callory = nt_callory[i]-nt_cost[i]+rnt_callory_acc[N-i-1]
        m = max(m, callory)

    
    for i in range(N-1):
        callory = rnt_callory[i]-rnt_cost[i]+nt_callory_acc[N-i-1]
        m = max(m, callory)

    print(max(0, m))
    return


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)


if __name__ == '__main__':
    main()

Solution (Python):

import sys
from itertools import accumulate
INF = float("inf")


def solve(N: int, C: int, x: "List[int]", v: "List[int]"):

    
    nt_cost = x
    nt_benefit = list(accumulate(v))
    nt_callory = [b-c for c, b in zip(nt_cost, nt_benefit)]
    nt_callory_acc = list(accumulate(nt_callory, max)) + [0]
    
    
    

    
    rnt_cost = list((reversed([C-v for v in x])))
    rnt_benefit = list(accumulate(reversed(v)))
    rnt_callory = [b-c for c, b in zip(rnt_cost, rnt_benefit)]
    rnt_callory_acc = list(accumulate(rnt_callory, max)) + [0]
    
    

    
    m = -INF
    for i in range(N):
        
        callory = max(nt_callory[i]-nt_cost[i]+rnt_callory_acc[N-i-2],
                      nt_callory[i])
        
        m = max(m, callory)

    
    for i in range(N):
        
        callory = max(rnt_callory[i]-rnt_cost[i]+nt_callory_acc[N-i-2],
                      rnt_callory[i])
        
        m = max(m, callory)

    print(max(0, m))
    return


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)


if __name__ == '__main__':
    main()

Solution (Python):
N, C = map(int, input().split())
x = []
v = []
for _ in range(N):
    xi, vi = map(int, input().split())
    x.append(xi)
    v.append(vi)
vsp = [0] * (N + 1)
vsn = [0] * (N + 1)
dprp = [0] * (N + 1)
dprn = [0] * (N + 1)
dpsp = [0] * (N + 1)
dpsn = [0] * (N + 1)
for i in range(N):
    vsp[i + 1] = vsp[i] + v[i]
    vsn[i + 1] = vsn[i] + v[N - 1 - i]
    dprp[i + 1] = max(dprp[i], vsp[i + 1] - 2 * x[i])
    dpsp[i + 1] = max(dpsp[i], vsp[i + 1] - x[i])
    dprn[i + 1] = max(dprn[i], vsn[i + 1] - 2 * (C - x[N - 1 - i]))
    dpsn[i + 1] = max(dpsn[i], vsn[i + 1] - (C - x[N - 1 - i]))
ans = max([max(dprp[i] + dpsn[N - i], dprn[i] + dpsp[N - i])
           for i in range(N + 1)])
print(ans)

Solution (Python):
N, C = map(int, input().split())
x = []
v = []
for _ in range(N):
    xi, vi = map(int, input().split())
    x.append(xi)
    v.append(vi)
vsp = [0] * (N + 1)
vsn = [0] * (N + 1)
dprp = [0] * (N + 1)
dprn = [0] * (N + 1)
dpsp = [0] * (N + 1)
dpsn = [0] * (N + 1)
for i in range(N):
    vsp[i + 1] = vsp[i] + v[i]
    vsn[i + 1] = vsn[i] + v[N - 1 - i]
    dprp[i + 1] = max(dprp[i], vsp[i + 1] - 2 * x[i])
    dpsp[i + 1] = max(dpsp[i], vsp[i + 1] - x[i])
    dprn[i + 1] = max(dprn[i], vsn[i + 1] - 2 * (C - x[N - 1 - i]))
    dpsn[i + 1] = max(dpsn[i], vsn[i + 1] - (C - x[N - 1 - i]))
ans = max(max(dprp[i] + dpsn[N - i], dprn[i] + dpsp[N - i])
          for i in range(N + 1))
print(ans)

Solution (Python):
N, C = map(int, input().split())
x = []
v = []
for _ in range(N):
    xi, vi = map(int, input().split())
    x.append(xi)
    v.append(vi)
vsp = [0] * (N + 1)
vsn = [0] * (N + 1)
dprp = [0] * (N + 1)
dprn = [0] * (N + 1)
dpsp = [0] * (N + 1)
dpsn = [0] * (N + 1)
for i in range(N):
    vsp[i + 1] = vsp[i] + v[i]
    vsn[i + 1] = vsn[i] + v[N - 1 - i]
    dprp[i + 1] = max(dprp[i], vsp[i + 1] - 2 * x[i])
    dpsp[i + 1] = max(dpsp[i], vsp[i + 1] - x[i])
    dprn[i + 1] = max(dprn[i], vsn[i + 1] - 2 * (C - x[N - 1 - i]))
    dpsn[i + 1] = max(dpsn[i], vsn[i + 1] - (C - x[N - 1 - i]))
ans = 0
for i in range(N + 1):
    ans = max(ans, dprp[i] + dpsn[N - i], dprn[i] + dpsp[N - i])
print(ans)

Solution (Python):
N, C = map(int, input().split())
x = []
v = []
for _ in range(N):
    xi, vi = map(int, input().split())
    x.append(xi)
    v.append(vi)
vsp = [0] * (N + 1)
vsn = [0] * (N + 1)
dprp = [0] * (N + 1)
dprn = [0] * (N + 1)
dpsp = [0] * (N + 1)
dpsn = [0] * (N + 1)
for i in range(N):
    vsp[i + 1] = vsp[i] + v[i]
    vsn[i + 1] = vsn[i] + v[N - 1 - i]
    dprp[i + 1] = max(dprp[i], vsp[i + 1] - 2 * x[i])
    dpsp[i + 1] = max(dpsp[i], vsp[i + 1] - x[i])
    dprn[i + 1] = max(dprn[i], vsn[i + 1] - 2 * (C - x[N - 1 - i]))
    dpsn[i + 1] = max(dpsn[i], vsn[i + 1] - (C - x[N - 1 - i]))
ans = max(dpsp[N], dpsn[N])
for i in range(N + 1):
    ans = max(ans, dprp[i] + dpsn[N - i], dprn[i] + dpsp[N - i])
print(ans)

Solution (Python):
from copy import deepcopy
from itertools import accumulate

n, c = map(int, input().split())
x = []
v = []


for _ in range(n):
    _x, _v = map(int, input().split())
    
    
    x.append(_x)
    v.append(_v)

v1 = list(accumulate(v)) 
v2 = list(accumulate(v[::-1])) 

f1 = [0] * (n + 1)
f2 = [0] * (n + 1)
for i in range(n):
    f1[i + 1] = v1[i] - x[i]
    f2[i + 1] = v2[i] - (c - x[n - 1 - i])

g1 = [0] * (n + 1)
g2 = [0] * (n + 1)
for i in range(n):
    g1[i + 1] = max(g1[i], f1[i + 1])
    g2[i + 1] = max(g2[i], f2[i + 1])

ans = 0
ans = max(ans, g1[n]) 
ans = max(ans, g2[n]) 
for j in range(n): 
    cal = 0
    cal -= (c - x[n - 1 - j]) * 2
    cal += v2[j]
    cal += g1[n - 1 - j]
    ans = max(ans, cal)
    
for j in range(n): 
    cal = 0
    cal -= (x[j]) * 2
    cal += v1[j]
    cal += g2[n - 1 - j]
    ans = max(ans, cal)
    
print(ans)

Solution (Python):
def solve(c, sushis):
    cw_maxs, cw_rets = create_accs(sushis, c, True)
    ccw_maxs, ccw_rets = create_accs(reversed(sushis), c, False)
    cw_max = cw_maxs[-1]
    ccw_max = ccw_maxs[-1]
    cw_ccw_max = turn_max(cw_rets, ccw_maxs)
    ccw_cw_max = turn_max(ccw_rets, cw_maxs)
    return max(cw_max, ccw_max, cw_ccw_max, ccw_cw_max)


def turn_max(rets, maxs):
    m = 0
    for i, r in enumerate(rets):
        ind = len(maxs) - 2 - i
        add = maxs[ind] if ind >= 0 else 0
        v = r + add
        if m < v:
            m = v
    return m


def create_accs(sushis, c, cw):
    maxs, vs_ret = [], []
    v_sum, v_max = 0, 0
    for d, v in sushis:
        v_sum += v
        dist = d if cw else c - d
        v_cur = v_sum - dist
        if v_max < v_cur:
            v_max = v_cur
        maxs.append(v_max)
        vs_ret.append(v_sum - dist*2)
    return maxs, vs_ret


n, c = map(int, input().split())
sushis = []
for _ in range(n):
    sushis.append([int(x) for x in input().split()])
print(solve(c, sushis))

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007


"""
s = input()
sum = 700
for i in range(3):
    if s[i] == "o":
        sum += 100
print(sum)
"""

"""
n, x = map(int, input().split(" "))
m = [0 for i in range(n)]
sum = 0
for i in range(n):
    m[i] = int(input())
m.sort()
for i in range(n):
    x -= m[i]
    sum += 1
sum += (x//m[0])
print(sum)
"""

"""
a,b,c,x,y = map(int, input().split(" "))
sum = 0
if a+b >= c*2:
    v = min(x, y)
    for i in range(v):
        sum += 2*c
    x -= v
    y -= v

if a < c*2:
    sum += a*x
if b < c*2:
    sum += b*y
if a >= c*2:
    sum += 2*c*x
if b >= c*2:
    sum += 2*c*y
print(sum)
"""

n,c = LI()
l = [0]
r = []
v = [0]
for i in range(n):
    a,b = LI()
    l.append(a)
    r.append(c-a)
    v.append(b)
r.append(0)
r = r[::-1]
sl = defaultdict(int)
sr = defaultdict(int)
sl[0] = 0
sr[0] = 0
for i in range(n):
    sl[i+1] = sl[i]+v[i+1]-l[i+1]+l[i]
for i in range(n):
    if sl[i+1] < sl[i]:
        sl[i+1] = sl[i]
for i in range(n):
    sr[i+1] = sr[i]+v[-i-1]-r[i+1]+r[i]
for i in range(n):
    if sr[i+1] < sr[i]:
        sr[i+1] = sr[i]
ans = 0
x = 0
for i in range(n+1):
    x += v[i]
    j = n-i
    m = max(x-l[i],x-2*l[i]+sr[j])
    ans = max(ans,m)

x = 0
for i in range(n+1):
    x += v[-i]
    j = n-i
    m = max(x-r[i],x-2*r[i]+sl[j])
    ans = max(ans,m)
print(ans)

Solution (Python):
import sys


	



def main():
	n, c = map(int, input().split())
	
	ll=[]
	for i in range(n):
		x,v=map(int, input().split())
		ll.append([x,v])
		
	ll.sort(key=lambda x:x[0])
	for i,v in enumerate(ll):
		ll[i].append(c - v[0])
	all_cal=sum([v[1] for v in ll])
	
	tw=0
	tc=0
	max_c=0
	max_cr=0
	for i,v in enumerate(ll):
		ptw=tw
		tw=v[0]
		tc+=v[1]
		
		
		cc=tc-tw
		max_c=max(cc,max_c)
		crc=tc-2*tw
		max_cr=max(crc, max_cr)
		
		ll[i].append(max_c)
		
		ll[i].append(max_cr)

	ll.sort(key=lambda x:-x[0])
	tw=0
	tc=0
	max_c=0
	max_cr=0
	for i,v in enumerate(ll):
		ptw=tw
		tw=v[2]
		tc+=v[1]
		
		
		cc=tc-tw
		max_c=max(cc,max_c)
		crc=tc-2*tw
		max_cr=max(crc, max_cr)
		
		ll[i].append(max_c)
		
		ll[i].append(max_cr)
		
	ll.sort(key=lambda x:x[0])
	ret_max=0
	for l in ll:
		
		pass
	
	for l,lp in zip(ll,ll[1:]):
		r=l[3]+lp[6]
		ret_max=max(r,ret_max)
		
	for l,lp in zip(ll[-1::-1],ll[-2::-1]):
		r=l[5]+lp[4]
		ret_max=max(r,ret_max)
		
	ret_max=max(ret_max, ll[-1][3])
	ret_max=max(ret_max, ll[0][5])
				
	print(ret_max)
	
	


if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
	else:
		input = sys.stdin.readline
		
	ret = main()
	if ret is not None:
		print(ret)
		
	if sys.platform =='ios':
		sys.stdin.close()

Solution (Python):
N, C = map(int, input().split())
x = [0]
v = [0]
for _ in range(N):
    a, b = map(int, input().split())
    x.append(a)
    v.append(b)
opt_l = [0]
acc_v = 0
for i in range(1, N + 1):
    opt_l.append(max(opt_l[-1], v[i] + acc_v - x[i]))
    acc_v += v[i]
opt_r = [0]
acc_v = 0
for i in range(1, N + 1):
    opt_r.append(max(opt_r[-1], v[-i] + acc_v - (C - x[-i])))
    acc_v += v[-i]
ans = 0
acc_v = 0
for i in range(N + 1):
    ans = max(
        ans,
        acc_v + v[i] - x[i],
        acc_v + v[i] - 2 * x[i] + opt_r[N - i]
    )
    acc_v += v[i]
acc_v = 0
for i in range(N + 1):
    ans = max(
        ans,
        acc_v + v[-i] - (C - x[-i]),
        acc_v + v[-i] - 2 * (C - x[-i]) + opt_l[N - i]
    )
    acc_v += v[-i]
print(ans)

Solution (Python):
N,C = map(int,input().split())
xv = [list(map(int,input().split())) for _ in range(N)]
x = [xv[i][0] for i in range(N)]
v = [xv[i][1] for i in range(N)]

C1 = [0]*N
C2 = [0]*N
for i in range(N):
    C1[i] = C1[i-1]+v[i]
    C2[i] = C2[i-1]+v[i]
for i in range(N):
    C1[i] -= x[i]
    C2[i] -= 2*x[i]
for i in range(1,N):
    C1[i] = max(C1[i],C1[i-1])
    C2[i] = max(C2[i],C2[i-1])

D1 = [0]*N
D2 = [0]*N
for i in range(N):
    D1[i] = D1[i-1]+v[N-1-i]
    D2[i] = D2[i-1]+v[N-1-i]
for i in range(N):
    D1[i] -= C-x[N-1-i]
    D2[i] -= 2*(C-x[N-1-i])
for i in range(1,N):
    D1[i] = max(D1[i],D1[i-1])
    D2[i] = max(D2[i],D2[i-1])

ans = 0
for i in range(N):
    ans = max(ans,C1[i],D1[i])

for i in range(N-1):
    ans = max(ans,C2[i]+D1[N-i-2],D2[i]+C1[N-i-2])

print(ans)

Solution (Python):
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())

x_cum_L = [0] * (N+1)
v_cum_L = [0] * (N+1)
x_cum_R = [0] * (N+1)
v_cum_R = [0] * (N+1)
for i in range(1, N+1):
    x_cum_L[i] = x[i-1]
    v_cum_L[i] = v_cum_L[i-1] + v[i-1]
    x_cum_R[i] = C - x[-i]
    v_cum_R[i] = v_cum_R[i-1] + v[-i]

reward_L = [0] * (N+1)
reward_R = [0] * (N+1)
for i in range(N+1):
    reward_L[i] = v_cum_L[i] - x_cum_L[i]
    reward_R[i] = v_cum_R[i] - x_cum_R[i]

reward_max_L = [0] * (N+1)
reward_max_R = [0] * (N+1)
for i in range(1, N+1):
    reward_max_L[i] = max(reward_max_L[i-1], reward_L[i])
    reward_max_R[i] = max(reward_max_R[i-1], reward_R[i])

ans = 0
for i in range(N+1):
    ans_L = reward_L[i] - x_cum_L[i] + reward_max_R[N - i]
    ans_R = reward_R[i] - x_cum_R[i] + reward_max_L[N - i]
    ans = max(ans, ans_L, ans_R)

print(ans)

Solution (Python):
N,C=map(int,input().split())
xv=[(0,0)]
xv_=[]
for i in range(N):
    x,v=map(int,input().split())
    xv.append((x,v))
    xv_.append((x,v))
xv_.append((C,0))
xv_.reverse()
r=[0]*(N+1)
r_=[0]*(N+1)
for i in range(N):
    r[i+1]=r[i]-(xv[i+1][0]-xv[i][0])+xv[i+1][1]
    r_[i+1]=max(r_[i],r[i+1]-xv[i+1][0])
l=[0]*(N+1)
l_=[0]*(N+1)
for i in range(N):
    l[i+1]=l[i]-abs(xv_[i+1][0]-xv_[i][0])+xv_[i+1][1]
    l_[i+1]=max(l_[i],l[i+1]-(C-xv_[i+1][0]))
ans=0
for i in range(1,N+1):
    ans=max(r[i]+l_[N-i],ans)
    ans=max(l[i]+r_[N-i],ans)
print(ans)

Solution (Python):

N, c = map(int, input().split())
S = [list(map(int, input().split())) for i in range(N)]

sum_ = 0
r = [0]
for x, v in S:
    sum_ += v
    r.append(sum_-x)

sum_ = 0
l = [0]
for x, v in S[::-1]:
    sum_ += v
    l.append(sum_ - (c-x))

def right():
    max_ = r[0]
    for i in r:
        if max_ < i:
            max_ = i
        yield max_
 
def left():
    max_ = l[0]
    for i in l:
        if max_ < i:
            max_ = i
        yield max_
 
rm = [[0, 0]] + S
lm = S + [[c, c]]
 
rpath = list(right())
lmax = max([l[i]+rpath[-(i+1)]-(c-lm[-(i+1)][0]) for i in range(N)])
 
lpath = list(left())
rmax = max([r[i]+lpath[-(i+1)]-rm[i][0] for i in range(N)])
 
 
print(max(rmax, lmax))

Solution (Python):
n,c=map(int,input().split())
sushi=[]
for i in range(n):
    x,v=map(int,input().split())
    sushi.append([x,v])
sushi.sort()
cw=[0]
cw_s=[]
cw_sum=0
for i in range(n):
    x=sushi[i][0]
    v=sushi[i][1]
    cw_sum=cw_sum+v
    cw.append(max(cw[-1],cw_sum-x))
    cw_s.append(cw_sum-2*x)
acw=[0]
acw_s=[]
acw_sum=0
for i in range(n):
    x=c-sushi[n-1-i][0]
    v=sushi[n-1-i][1]
    acw_sum=acw_sum+v
    acw.append(max(acw[-1],acw_sum-x))
    acw_s.append(acw_sum-2*x)
ans=max(cw[n],acw[n])
for i in range(n):
    ans=max(ans,acw_s[i]+cw[n-1-i],cw_s[i]+acw[n-1-i])
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
import numpy as np

N,C = map(int,input().split())
XV = np.array([input().split() for _ in range(N)],dtype=np.int64)
X = XV[:,0]
V = XV[:,1]

Xrev = C - X[::-1]
Vrev = V[::-1]

answer = 0


for X,V,Xrev,Vrev in [(X,V,Xrev,Vrev),(Xrev,Vrev,X,V)]:
  
  
  v1 = np.cumsum(V) - 2*X
  
  v2 = np.cumsum(Vrev) - Xrev
  
  v2 = np.maximum.accumulate(v2)
  
  x = v2.max()
  if answer < x:
    answer = x
  
  for k in range(1,N):
    x = v1[k-1] + v2[N-k-1]
    if answer < x:
      answer = x
print(answer)