Buggy python submission (verdict: Time Limit Exceeded):
# coding:utf-8

import sys
import heapq
import itertools
from collections import deque

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


n, c = LI()
sushi = [LI() for _ in range(n)]

clockwise = [0]
for x, v in sushi[::-1]:
    clockwise.append(clockwise[-1] + v)
counterclockwise = [0]
for x, v in sushi:
    counterclockwise.append(counterclockwise[-1] + v)

sushi = deque(sushi)
sushi.append([c, 0])
sushi.appendleft([0, 0])

ans = 0
for i in range(n + 1):
    for j in range(n + 1 - i):
        # cal1: 反時計回り->時計回り
        cal1 = counterclockwise[i] + clockwise[j]
        cal1 -= sushi[i][0] * 2 + (c - sushi[-1 - j][0])
        # cal2: 時計回り->反時計回り
        cal2 = counterclockwise[i] + clockwise[j]
        cal2 -= sushi[i][0] + (c - sushi[-1 - j][0]) * 2
        ans = max(ans, cal1, cal2)

print(ans)


Fixed submission:
n,c = map(int, input().split())
x = []
x_r = []
v = []
for i in range(n):
    xi,vi = map(int, input().split())
    x.append(xi)
    x_r.append(c - xi)
    v.append(vi)
x_r.reverse()
x.insert(0, 0)
x_r.insert(0, 0)

score = [0]
score_r = [0]
tmp = 0
for i, vi in enumerate(v):
    tmp += vi
    score.append(tmp - x[i+1])
tmp = 0
for i, vi in enumerate(v[::-1]):
    tmp += vi
    score_r.append(tmp - x_r[i+1])

maxscore = []
maxscore_r = []
tmp = 0
for i, si in enumerate(score):
    tmp = max(tmp, si)
    maxscore.append(tmp)
tmp = 0
for i, si in enumerate(score_r):
    tmp = max(tmp, si)
    maxscore_r.append(tmp)

ans = 0
for i, si in enumerate(score):
    ans = max(si + maxscore_r[n-i] - x[i], ans)
for i, si in enumerate(score_r):
    ans = max(si + maxscore[n-i] - x_r[i], ans)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,c = map(int, input().split())
x = [0]
xr = [0]
v = []
va = [0]
fa_clock = [0]
fa_reclock = [0]

prev = 0
for i in range(n):
    xi,vi = map(int, input().split())
    x.append(xi)
    xr.append(c - xi)
    v.append(vi)
    va.append(vi + prev)
    fa_clock.append(vi + prev - xi)
    fa_reclock.append(vi + prev - xi*2)
    prev += vi
v.append(0)

maxcal = 0
for ib in range(n+1):
    clock = max(fa_clock[:n+1-ib]) + sum(v[n-ib:]) - 2*xr[-ib]
    reclock = max(fa_reclock[:n+1-ib]) + sum(v[n-ib:]) - xr[-ib]
    ans = max(clock, reclock)
    if maxcal < ans: maxcal = ans
print(maxcal)

Fixed submission:
n,c = map(int, input().split())
x = []
x_r = []
v = []
for i in range(n):
    xi,vi = map(int, input().split())
    x.append(xi)
    x_r.append(c - xi)
    v.append(vi)
x_r.reverse()
x.insert(0, 0)
x_r.insert(0, 0)

score = [0]
score_r = [0]
tmp = 0
for i, vi in enumerate(v):
    tmp += vi
    score.append(tmp - x[i+1])
tmp = 0
for i, vi in enumerate(v[::-1]):
    tmp += vi
    score_r.append(tmp - x_r[i+1])

maxscore = []
maxscore_r = []
tmp = 0
for i, si in enumerate(score):
    tmp = max(tmp, si)
    maxscore.append(tmp)
tmp = 0
for i, si in enumerate(score_r):
    tmp = max(tmp, si)
    maxscore_r.append(tmp)

ans = 0
for i, si in enumerate(score):
    ans = max(si + maxscore_r[n-i] - x[i], ans)
for i, si in enumerate(score_r):
    ans = max(si + maxscore[n-i] - x_r[i], ans)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,c = map(int, input().split())
x = [0]
xr = [0]
va = [0]
fa_clock = [0]
fa_reclock = [0]

prev = 0
vb = [0 for _ in range(n+1)]
for i in range(n):
    xi,vi = map(int, input().split())
    x.append(xi)
    xr.append(c - xi)
    va.append(vi + prev)
    fa_clock.append(vi + prev - xi)
    fa_reclock.append(vi + prev - xi*2)
    prev += vi
    for j in range(i+1):
        vb[j] += vi
vb.reverse()

maxcal = 0
for ib in range(n+1):
    clock = max(fa_clock[:n+1-ib]) + vb[ib] - 2*xr[-ib]
    reclock = max(fa_reclock[:n+1-ib]) + vb[ib] - xr[-ib]
    ans = max(clock, reclock)
    if maxcal < ans: maxcal = ans
print(maxcal)


Fixed submission:
n,c = map(int, input().split())
x = []
x_r = []
v = []
for i in range(n):
    xi,vi = map(int, input().split())
    x.append(xi)
    x_r.append(c - xi)
    v.append(vi)
x_r.reverse()
x.insert(0, 0)
x_r.insert(0, 0)

score = [0]
score_r = [0]
tmp = 0
for i, vi in enumerate(v):
    tmp += vi
    score.append(tmp - x[i+1])
tmp = 0
for i, vi in enumerate(v[::-1]):
    tmp += vi
    score_r.append(tmp - x_r[i+1])

maxscore = []
maxscore_r = []
tmp = 0
for i, si in enumerate(score):
    tmp = max(tmp, si)
    maxscore.append(tmp)
tmp = 0
for i, si in enumerate(score_r):
    tmp = max(tmp, si)
    maxscore_r.append(tmp)

ans = 0
for i, si in enumerate(score):
    ans = max(si + maxscore_r[n-i] - x[i], ans)
for i, si in enumerate(score_r):
    ans = max(si + maxscore[n-i] - x_r[i], ans)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,c = map(int, input().split())
x = [0]
xr = [0]
v = [0]
for _ in range(n):
    xi,vi = map(int, input().split())
    x.append(xi)
    xr.append(c - xi)
    v.append(vi)
clockmax = -1
reclockmax = -1
score = 0
for i in range(n+1):
    for j in range(n+1-i):
        for k in range(i+1):
            score += v[k]
        for k in range(j+1):
            score += v[-k]
        score2 = score
        score -= 2*x[i] + xr[-j]
        score2 -= x[i] + 2*xr[-j]
        if clockmax < score: clockmax = score
        if reclockmax < score2: reclockmax = score2
        score = 0
        score2 = 0

print(max(clockmax,reclockmax))

Fixed submission:
n,c = map(int, input().split())
x = []
x_r = []
v = []
for i in range(n):
    xi,vi = map(int, input().split())
    x.append(xi)
    x_r.append(c - xi)
    v.append(vi)
x_r.reverse()
x.insert(0, 0)
x_r.insert(0, 0)

score = [0]
score_r = [0]
tmp = 0
for i, vi in enumerate(v):
    tmp += vi
    score.append(tmp - x[i+1])
tmp = 0
for i, vi in enumerate(v[::-1]):
    tmp += vi
    score_r.append(tmp - x_r[i+1])

maxscore = []
maxscore_r = []
tmp = 0
for i, si in enumerate(score):
    tmp = max(tmp, si)
    maxscore.append(tmp)
tmp = 0
for i, si in enumerate(score_r):
    tmp = max(tmp, si)
    maxscore_r.append(tmp)

ans = 0
for i, si in enumerate(score):
    ans = max(si + maxscore_r[n-i] - x[i], ans)
for i, si in enumerate(score_r):
    ans = max(si + maxscore[n-i] - x_r[i], ans)

print(ans)