Buggy python submission (verdict: Time Limit Exceeded):
N,C = map(int,input().split())
X = [0]*N
V = [0]*N
XX = [0]*N
VV = [0]*N
for i in range(N):
    X[i],V[i] = map(int,input().split())
    XX[N-i-1] = C-X[i]
    VV[N-i-1] = V[i]
from numpy import array as arr
V = arr(V)
VV = arr(VV)
a = [0]*(2*N-1)
b = [0]*(2*N-1)
for i in range(N):
    x = X[i]
    v = V[:i+1].sum()
    xx = XX[i]
    vv = VV[:i+1].sum()
    a[i] = v-x
    b[i] = vv-xx
aa = arr([0]*N)
bb = arr([0]*N)
for i in range(N):
    aa[i] = max(a[:i+1])
    bb[i] = max(b[:i+1])
for i in range(N-1):
    x = 2*(C-X[-i-1])
    v = V[-i-1:].sum()
    xx = 2*(C-XX[-i-1])
    vv = VV[-i-1:].sum()
    a[N+i] = v-x+aa[N-i-2]
    b[N+i] = vv-xx+bb[N-i-2]
a.append(0)
print(max(max(a),max(b)))


Fixed submission:
N,C = map(int,input().split())
x1=[]
v1=[]

for i in range(N):
    x,v = map(int,input().split())
    x1.append(x)
    v1.append(v)
x2 = [C-x for x in x1]
v2 = [v for v in v1]
x2.reverse()
v2.reverse()

res1 = 0
res2 = 0
s1 = 0
s2 = 0
r1 = [0]
r2 = [0]
for i in range(N):
    s1 += v1[i]
    s2 += v2[i]
    res1 = max(res1,s1-x1[i])
    res2 = max(res2,s2-x2[i])
    r1.append(res1)
    r2.append(res2)

vl1 = [0]*(N+1)
vl2 = [0]*(N+1)
for i in range(N):
    vl1[i+1] = v2[i]+vl1[i]
    vl2[i+1] = v1[i]+vl2[i]
for i in range(N):
    vl1[i+1] -= 2*x2[i]
    vl2[i+1] -= 2*x1[i]



ans = 0
for i in range(N+1):
    res = vl1[i]+r1[N-i]
    if res > ans:
        ans = res
    res = vl2[i]+r2[N-i]
    if res > ans:
        ans = res

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,C = map(int,input().split())
X = [0]*N
V = [0]*N
XX = [0]*N
VV = [0]*N
for i in range(N):
    X[i],V[i] = map(int,input().split())
    XX[N-i-1] = C-X[i]
    VV[N-i-1] = V[i]
from numpy import array as arr
X = arr(X)
V = arr(V)
XX = arr(XX)
VV = arr(VV)
a = []
b = []
for i in range(N):
    x = X[i]
    v = V[:i+1].sum()
    xx = XX[i]
    vv = VV[:i+1].sum()
    a.append(v-x)
    b.append(vv-xx)
aa = arr([0]*N)
bb = arr([0]*N)
for i in range(N):
    aa[i] = max(a[:i+1])
    bb[i] = max(b[:i+1])
for i in range(N-1):
    x = 2*(C-X[-i-1])
    v = V[-i-1:].sum()
    xx = 2*(C-XX[-i-1])
    vv = VV[-i-1:].sum()
    a.append(v-x+aa[N-i-2])
    b.append(vv-xx+bb[N-i-2])
a.append(0)
print(max(max(a),max(b)))

Fixed submission:
N,C = map(int,input().split())
x1=[]
v1=[]

for i in range(N):
    x,v = map(int,input().split())
    x1.append(x)
    v1.append(v)
x2 = [C-x for x in x1]
v2 = [v for v in v1]
x2.reverse()
v2.reverse()

res1 = 0
res2 = 0
s1 = 0
s2 = 0
r1 = [0]
r2 = [0]
for i in range(N):
    s1 += v1[i]
    s2 += v2[i]
    res1 = max(res1,s1-x1[i])
    res2 = max(res2,s2-x2[i])
    r1.append(res1)
    r2.append(res2)

vl1 = [0]*(N+1)
vl2 = [0]*(N+1)
for i in range(N):
    vl1[i+1] = v2[i]+vl1[i]
    vl2[i+1] = v1[i]+vl2[i]
for i in range(N):
    vl1[i+1] -= 2*x2[i]
    vl2[i+1] -= 2*x1[i]



ans = 0
for i in range(N+1):
    res = vl1[i]+r1[N-i]
    if res > ans:
        ans = res
    res = vl2[i]+r2[N-i]
    if res > ans:
        ans = res

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
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())
from numpy import array as arr
X = arr(X)
V = arr(V)
a = []
for i in range(N):
    for j in range(N):
        if i != j:
            k = min(i,j)
            l = max(i,j)
            x = min(2*X[k]+C-X[l],2*(C-X[l])+X[k])
            v = V[:k+1].sum() + V[l:].sum()
            a.append(v-x)
for i in range(N):
    x = X[i]
    v = V[:i+1].sum()
    a.append(max(V[:i+1].sum()-X[i],V[i:].sum()-(C-X[i])))
a.append(0)
a.sort()
print(a[-1])

Fixed submission:
N,C = map(int,input().split())
x1=[]
v1=[]

for i in range(N):
    x,v = map(int,input().split())
    x1.append(x)
    v1.append(v)
x2 = [C-x for x in x1]
v2 = [v for v in v1]
x2.reverse()
v2.reverse()

res1 = 0
res2 = 0
s1 = 0
s2 = 0
r1 = [0]
r2 = [0]
for i in range(N):
    s1 += v1[i]
    s2 += v2[i]
    res1 = max(res1,s1-x1[i])
    res2 = max(res2,s2-x2[i])
    r1.append(res1)
    r2.append(res2)

vl1 = [0]*(N+1)
vl2 = [0]*(N+1)
for i in range(N):
    vl1[i+1] = v2[i]+vl1[i]
    vl2[i+1] = v1[i]+vl2[i]
for i in range(N):
    vl1[i+1] -= 2*x2[i]
    vl2[i+1] -= 2*x1[i]



ans = 0
for i in range(N+1):
    res = vl1[i]+r1[N-i]
    if res > ans:
        ans = res
    res = vl2[i]+r2[N-i]
    if res > ans:
        ans = res

print(ans)