Buggy python submission (verdict: Time Limit Exceeded):
#lの位置は線形探索、rの位置はさぶたん
N, C = map(int, input().split())
s = [list(map(int, input().split())) for _ in range(N)]
csv = [0] * (N + 1)
s.sort()
for i in range(N):
  csv[i + 1] = csv[i] + s[i][1]
res = 0
for l in range(N):
  rlim = N - 1
  llim = 0
  x1 = (rlim - llim) // 3
  x2 = 2 * x1
  while x2 - x1 > 3:
    x1 = (rlim - llim) // 3
    x2 = 2 * x1
    xres = [0, 0]
    i = 0
    #print(x1, x2)
    for r in (x1, x2):
      if l > r:
        a = (C - s[l][0]) * 2 + s[r][0]
        b = (C - s[l][0]) + s[r][0] * 2
        xres[i] = csv[r + 1] + (csv[-1] - csv[l]) - min(a, b)
      else:
        xres[i] = csv[r + 1] - csv[l] - s[r][0]
      i += 1
    #print(xres)
    if xres[0] > xres[1]:
      res = max(xres[0], res)
      rlim = x2
    else:
      res = max(xres[1], res)
      llim = x1
  for r in range(llim, rlim + 1):
    if l > r:
      a = (C - s[l][0]) * 2 + s[r][0]
      b = (C - s[l][0]) + s[r][0] * 2
      res = max(res, csv[r + 1] + (csv[-1] - csv[l]) - min(a, b))
    else:
      res = max(res, csv[r + 1] - csv[l] - s[r][0])
print(res)

Fixed submission:
from itertools import accumulate as acc
N, C = map(int, input().split())
ds, vs = [], []
for i in range(N):
  d, v = map(int, input().split())
  ds.append(d)
  vs.append(v)
rvcs = list(acc(vs))
lvcs = list(acc(vs[:: -1]))
rds = ds
lds = [C - d for d in ds[:: -1]]
re = [v - d for d, v in zip(rds, rvcs)]
le = [v - d for d, v in zip(lds, lvcs)]
remax = [0]
lemax = [0]
for i in range(N):
  remax.append(max(remax[-1], re[i]))
  lemax.append(max(lemax[-1], le[i]))
res = 0
#i個のスシを補給する時、右回り直進、左回り直進、右回り反転左回り、左回り反転右回りがある
#反転の際はエネルギー累積maxを使わないと同じスシを２回食べちゃうかも
#反転しない際は取れるだけ取らせてみてもいい
for i in range(N):
  res = max(res, re[i], le[i], re[i] + lemax[N - i - 1] - rds[i], le[i] + remax[N - i - 1] - lds[i])
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
#lの位置は線形探索、rの位置はさぶたん
N, C = map(int, input().split())
s = [list(map(int, input().split())) for _ in range(N)]
csv = [0] * (N + 1)
s.sort()
for i in range(N):
  csv[i + 1] = csv[i] + s[i][1]
res = 0
for l in range(N):
  rlim = N - 1
  llim = 0
  x1 = (rlim - llim) // 3
  x2 = 2 * x1
  while x2 - x1 > 3:
    x1 = (rlim - llim) // 3
    x2 = 2 * x1
    xres = [0, 0]
    i = 0
    #print(x1, x2)
    for r in (x1, x2):
      if l > r:
        a = (C - s[l][0]) * 2 + s[r][0]
        b = (C - s[l][0]) + s[r][0] * 2
        xres[i] = csv[r + 1] + (csv[-1] - csv[l]) - min(a, b)
      else:
        xres[i] = csv[r + 1] - csv[l] - s[r][0]
      i += 1
    #print(xres)
    if xres[0] > xres[1]:
      res = max(xres[0], res)
      rlim = x2
    else:
      res = max(xres[1], res)
      llim = x1
  for r in range(llim, rlim + 1):
    if l > r:
      a = (C - s[l][0]) * 2 + s[r][0]
      b = (C - s[l][0]) + s[r][0] * 2
      res = max(res, csv[r + 1] + (csv[-1] - csv[l]) - min(a, b))
    else:
      res = max(res, csv[r + 1] - csv[l] - s[r][0])
print(res)

Fixed submission:
from itertools import accumulate as acc
N, C = map(int, input().split())
ds, vs = [], []
for i in range(N):
  d, v = map(int, input().split())
  ds.append(d)
  vs.append(v)
rvcs = list(acc(vs))
lvcs = list(acc(vs[:: -1]))
rds = ds
lds = [C - d for d in ds[:: -1]]
re = [v - d for d, v in zip(rds, rvcs)]
le = [v - d for d, v in zip(lds, lvcs)]
remax = [0]
lemax = [0]
for i in range(N):
  remax.append(max(remax[-1], re[i]))
  lemax.append(max(lemax[-1], le[i]))
res = 0
#i個のスシを補給する時、右回り直進、左回り直進、右回り反転左回り、左回り反転右回りがある
#反転の際はエネルギー累積maxを使わないと同じスシを２回食べちゃうかも
#反転しない際は取れるだけ取らせてみてもいい
for i in range(N):
  res = max(res, re[i], le[i], re[i] + lemax[N - i - 1] - rds[i], le[i] + remax[N - i - 1] - lds[i])
print(res)