Buggy python submission (verdict: Time Limit Exceeded):
from itertools import accumulate
import sys
input = sys.stdin.readline

def main():
    n,c = map(int,input().split())
    x1,v1 = [0],[0]
    x2,v2 = [],[]
    for _ in range(n):
        a,b = map(int, input().split())
        x1.append(a)
        x2 = [c-a] + x2
        v1.append(b)
        v2 = [b] + v2
    x2 = [0] + x2
    v2 = [0] + v2

    v1, v2 = list(accumulate(v1)), list(accumulate(v2))

    f = [v2[i] - x2[i] for i in range(n+1)]
    f2 = [v2[i] - 2*x2[i] for i in range(n+1)]
    g1 = list(accumulate(f, max))
    g2 = list(accumulate(f2, max))
    val = max(v1[n]-x1[n] + g2[0], v1[n]-2*x1[n] + g1[0])
    for i in range(1,n+1):
        val = max(val, v1[n-i]-x1[n-i] + g2[i], v1[n-i]-2*x1[n-i] + g1[i])

    print(val)

if __name__=='__main__': main()

Fixed submission:
from itertools import accumulate
import sys
input = sys.stdin.readline

def main():
    n,c = map(int,input().split())
    x1,v1 = [0],[0]
    for _ in range(n):
        a,b = map(int, input().split())
        x1.append(a)
        v1.append(b)
    x2 = [0]
    v2 = [0]
    for i in range(n,0,-1):
        x2.append(c-x1[i])
        v2.append(v1[i])

    v1, v2 = list(accumulate(v1)), list(accumulate(v2))
    g1,g2 = [v2[0]-x2[0]],[v2[0]-2*x2[0]]
    for i in range(1,n+1):
        f = v2[i] - x2[i]
        f2 = v2[i] - 2*x2[i]
        g1.append(max(g1[i-1],f))
        g2.append(max(g2[i-1],f2))
    val = max(v1[n]-x1[n] + g2[0], v1[n]-2*x1[n] + g1[0])
    for i in range(1,n+1):
        val = max(val, v1[n-i]-x1[n-i] + g2[i], v1[n-i]-2*x1[n-i] + g1[i])

    print(val)

if __name__=='__main__': main()

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import accumulate
import sys
input = sys.stdin.readline

n,c = map(int,input().split())
x1,v1 = [0],[0]
x2,v2 = [],[]
for _ in range(n):
  a,b = map(int, input().split())
  x1.append(a)
  x2 = [c-a] + x2
  v1.append(b)
  v2 = [b] + v2
x2 = [0] + x2
v2 = [0] + v2

v1, v2 = list(accumulate(v1)), list(accumulate(v2))

f1 = [v1[i] - x1[i] for i in range(n+1)]
f2 = [v2[i] - x2[i] for i in range(n+1)]
g1,x1_ = f1[0],x1[0]
g2,x2_ = f2[0],x2[0]
val = max(v1[n]-x1[n] + g2 - min(x1[n],x2_), v2[n]-x2[n] + g1 - min(x2[n],x1_))
for i in range(1,n+1):
  if g1<f1[i]:
	  g1,x1_ = f1[i], x1[i]
  if g2<f2[i]:
	  g2,x2_ = f2[i], x2[i]
  val = max(val, v1[n-i]-x1[n-i] + g2 - min(x1[n-i],x2_), v2[n-i]-x2[n-i] + g1 - min(x2[n-i],x1_))

print(val)

Fixed submission:
from itertools import accumulate
import sys
input = sys.stdin.readline

def main():
    n,c = map(int,input().split())
    x1,v1 = [0],[0]
    for _ in range(n):
        a,b = map(int, input().split())
        x1.append(a)
        v1.append(b)
    x2 = [0]
    v2 = [0]
    for i in range(n,0,-1):
        x2.append(c-x1[i])
        v2.append(v1[i])

    v1, v2 = list(accumulate(v1)), list(accumulate(v2))
    g1,g2 = [v2[0]-x2[0]],[v2[0]-2*x2[0]]
    for i in range(1,n+1):
        f = v2[i] - x2[i]
        f2 = v2[i] - 2*x2[i]
        g1.append(max(g1[i-1],f))
        g2.append(max(g2[i-1],f2))
    val = max(v1[n]-x1[n] + g2[0], v1[n]-2*x1[n] + g1[0])
    for i in range(1,n+1):
        val = max(val, v1[n-i]-x1[n-i] + g2[i], v1[n-i]-2*x1[n-i] + g1[i])

    print(val)

if __name__=='__main__': main()

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import accumulate
import sys
input = sys.stdin.readline

n,c = map(int,input().split())
x1,v1 = [0],[0]
x2,v2 = [],[]
for _ in range(n):
  a,b = map(int, input().split())
  x1.append(a)
  x2 = [c-a] + x2
  v1.append(b)
  v2 = [b] + v2
x2 = [0] + x2
v2 = [0] + v2

v1, v2 = list(accumulate(v1)), list(accumulate(v2))
res = 0
for i in range(n+1):
  for j in range(n+1-i):
    cost = v1[i] + v2[j] - x1[i] - x2[j] - min(x1[i],x2[j])
    if cost > res:
      res = cost
print(res)

Fixed submission:
from itertools import accumulate
import sys
input = sys.stdin.readline

def main():
    n,c = map(int,input().split())
    x1,v1 = [0],[0]
    for _ in range(n):
        a,b = map(int, input().split())
        x1.append(a)
        v1.append(b)
    x2 = [0]
    v2 = [0]
    for i in range(n,0,-1):
        x2.append(c-x1[i])
        v2.append(v1[i])

    v1, v2 = list(accumulate(v1)), list(accumulate(v2))
    g1,g2 = [v2[0]-x2[0]],[v2[0]-2*x2[0]]
    for i in range(1,n+1):
        f = v2[i] - x2[i]
        f2 = v2[i] - 2*x2[i]
        g1.append(max(g1[i-1],f))
        g2.append(max(g2[i-1],f2))
    val = max(v1[n]-x1[n] + g2[0], v1[n]-2*x1[n] + g1[0])
    for i in range(1,n+1):
        val = max(val, v1[n-i]-x1[n-i] + g2[i], v1[n-i]-2*x1[n-i] + g1[i])

    print(val)

if __name__=='__main__': main()