Buggy python submission (verdict: Time Limit Exceeded):
import sys
from itertools import accumulate

def main():
  input = sys.stdin.readline
  N, C = map(int, input().split())
  X = []
  V = []
  for _ in range(N):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v)
  Vr = V[::-1]
  Xr = X[::-1]

  F = list(accumulate(V))
  for i in range(N):
    F[i] = F[i] - X[i]

  R = list(accumulate(Vr))
  for i in range(N):
    R[i] = R[i] - (C - Xr[i])

  val = 0
  for i in range(N-1):
    v1 = F[i] + max(R[:-(i+1)]) - X[i]
    v2 = R[i] + max(F[:-(i+1)]) - (C - Xr[i])
    val = max(val, v1, v2)
  
  ans = max(0, val, max(F), max(R))
  print(ans)


if __name__ == '__main__':
  main()

Fixed submission:
import sys
from itertools import accumulate

def main():
  input = sys.stdin.readline
  N, C = map(int, input().split())
  X = []
  V = []
  for _ in range(N):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v)
  Vr = V[::-1]
  Xr = X[::-1]

  F = list(accumulate(V))
  R = list(accumulate(Vr))
  F[0], R[0] = F[0] - X[0], R[0] - (C - Xr[0])
  for i in range(1, N):
    F[i] = max(F[i-1], F[i] - X[i])
    R[i] = max(R[i-1], R[i] - (C - Xr[i]))

  val = 0
  for i in range(N-1):
    v1 = F[i] + R[N-(i+2)] - X[i]
    v2 = R[i] + F[N-(i+2)] - (C - Xr[i])
    val = max(val, v1, v2)
  
  ans = max(0, val, max(F), max(R))
  print(ans)


if __name__ == '__main__':
  main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from itertools import accumulate

def main():
  input = sys.stdin.readline
  N, C = map(int, input().split())
  X = []
  V = []
  for _ in range(N):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v)
  Vr = V[::-1]
  Xr = X[::-1]
  
  
  R = list(accumulate(V))
  for i in range(N):
    R[i] = R[i] - X[i]

  L = list(accumulate(Vr))
  for i in range(N):
    L[i] = L[i] - (C - Xr[i])

  Rmax = max(R)
  Lmax = max(L)
  
  RLmax = 0
  for i in range(N-1):
    cur = R[i] + max(L[:-(i+1)]) - X[i]
    RLmax = max(RLmax, cur)
    if R[i] == Rmax:
      break
  
  LRmax = 0
  for i in range(N-1):
    cur = L[i] + max(R[:-(i+1)]) - (C - Xr[i])
    LRmax = max(LRmax, cur)
    if L[i] == Lmax:
      break
  
  ans = max(0, Rmax, Lmax, RLmax, LRmax)
  print(ans)


if __name__ == '__main__':
  main()

Fixed submission:
import sys
from itertools import accumulate

def main():
  input = sys.stdin.readline
  N, C = map(int, input().split())
  X = []
  V = []
  for _ in range(N):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v)
  Vr = V[::-1]
  Xr = X[::-1]

  F = list(accumulate(V))
  R = list(accumulate(Vr))
  F[0], R[0] = F[0] - X[0], R[0] - (C - Xr[0])
  for i in range(1, N):
    F[i] = max(F[i-1], F[i] - X[i])
    R[i] = max(R[i-1], R[i] - (C - Xr[i]))

  val = 0
  for i in range(N-1):
    v1 = F[i] + R[N-(i+2)] - X[i]
    v2 = R[i] + F[N-(i+2)] - (C - Xr[i])
    val = max(val, v1, v2)
  
  ans = max(0, val, max(F), max(R))
  print(ans)


if __name__ == '__main__':
  main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from itertools import accumulate

def main():
  input = sys.stdin.readline
  N, C = map(int, input().split())
  X = []
  V = []
  for _ in range(N):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v)
  Vr = V[::-1]
  Xr = X[::-1]
  
  
  R = list(accumulate(V))
  for i in range(N):
    R[i] = R[i] - X[i]

  L = list(accumulate(Vr))
  for i in range(N):
    L[i] = L[i] - (C - Xr[i])

  Rmax = max(R)
  Lmax = max(L)
  
  RLmax = 0
  for i in range(N-1):
    cur = R[i] + max(L[:-(i+1)]) - X[i]
    RLmax = max(RLmax, cur)
  
  LRmax = 0
  for i in range(N-1):
    cur = L[i] + max(R[:-(i+1)]) - (C - Xr[i])
    LRmax = max(LRmax, cur)
  
  ans = max(0, Rmax, Lmax, RLmax, LRmax)
  print(ans)


if __name__ == '__main__':
  main()

Fixed submission:
import sys
from itertools import accumulate

def main():
  input = sys.stdin.readline
  N, C = map(int, input().split())
  X = []
  V = []
  for _ in range(N):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v)
  Vr = V[::-1]
  Xr = X[::-1]

  F = list(accumulate(V))
  R = list(accumulate(Vr))
  F[0], R[0] = F[0] - X[0], R[0] - (C - Xr[0])
  for i in range(1, N):
    F[i] = max(F[i-1], F[i] - X[i])
    R[i] = max(R[i-1], R[i] - (C - Xr[i]))

  val = 0
  for i in range(N-1):
    v1 = F[i] + R[N-(i+2)] - X[i]
    v2 = R[i] + F[N-(i+2)] - (C - Xr[i])
    val = max(val, v1, v2)
  
  ans = max(0, val, max(F), max(R))
  print(ans)


if __name__ == '__main__':
  main()