Buggy python submission (verdict: Time Limit Exceeded):
import itertools
N, C = map(int, input().split())
X = []
V = []
for n in range(N):
    x, v = map(int,input().split())
    X.append(x)
    V.append(v)
X_r = X[::-1]
V_r = V[::-1]
X = [0] + X
V = [0] + V
X_r = [0] + [C-x for x in X_r]
V_r = [0] + V_r

#cx = list(itertools.accumulate(X))
cv = list(itertools.accumulate(V))
#cxr = list(itertools.accumulate(X_r))
cvr = list(itertools.accumulate(V_r))

# print (X)
# print (X_r)
# print (cv)
# print (cvr)

# L = []
# for i in range(N+1):
#     L.append(cv[i]-X[i])
# R = []
# for i in range(N+1):
#     R.append(cvr[i]-X_r[i])

# print (L,R)
mx = 0
for l in range(N+1):
    m1 = -2*X[l]+cv[l]
    m2 = -2*X_r[l]+cvr[l]
    for r in range(N+1-l):
        #print (l,r)
        a = m1+cvr[r]-X_r[r]
        mx = max(mx,a)
        a = m2+cv[r]-X[r]
        mx = max(mx,a)
print (mx)

Fixed submission:
import itertools
N, C = map(int, input().split())
X = []
V = []
for n in range(N):
    x, v = 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] # 逆に回るのでCから引く
Vr = [0] + Vr

Vl = list(itertools.accumulate(Vl)) # xは累積した値なので、vも累積和に統一
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]) # 往復する場合はk=2
    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):
    #print (n,N-n)
    ans = max(ans, Lt[n] + R[N-n])
    ans = max(ans, Rt[n] + L[N-n])
print (ans)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
N, C = map(int, input().split())
X = []
V = []
for n in range(N):
    x, v = map(int,input().split())
    X.append(x)
    V.append(v)
X_r = X[::-1]
V_r = V[::-1]
X = [0] + X
V = [0] + V
X_r = [0] + [C-x for x in X_r]
V_r = [0] + V_r

#cx = list(itertools.accumulate(X))
cv = list(itertools.accumulate(V))
#cxr = list(itertools.accumulate(X_r))
cvr = list(itertools.accumulate(V_r))

# print (X)
# print (X_r)
# print (cv)
# print (cvr)

# L = []
# for i in range(N+1):
#     L.append(cv[i]-X[i])
# R = []
# for i in range(N+1):
#     R.append(cvr[i]-X_r[i])

# print (L,R)
mx = 0
for l in range(N+1):
    for r in range(N+1-l):
        #print (l,r)
        a = -2*X[l]+cv[l]+cvr[r]-X_r[r]
        mx = max(mx,a)
        a = -2*X_r[l]+cv[r]+cvr[l]-X[r]
        mx = max(mx,a)
print (mx)

Fixed submission:
import itertools
N, C = map(int, input().split())
X = []
V = []
for n in range(N):
    x, v = 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] # 逆に回るのでCから引く
Vr = [0] + Vr

Vl = list(itertools.accumulate(Vl)) # xは累積した値なので、vも累積和に統一
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]) # 往復する場合はk=2
    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):
    #print (n,N-n)
    ans = max(ans, Lt[n] + R[N-n])
    ans = max(ans, Rt[n] + L[N-n])
print (ans)