Buggy python submission (verdict: Time Limit Exceeded):
n, c = input().split()
n = int(n)
c = int(c)
x = [0]
v = [0]
sv = [0]
ssv = 0
for i in range(n):
    xx, vv = input().split()
    xx = int(xx)
    vv = int(vv)
    x.append(xx)
    v.append(vv)
    ssv += vv
    sv.append(ssv)
ans = -10**15
for i in range(0, n + 1, 1):
    e1 = sv[i] - x[i]
    for j in range(-1, -n +  i  - 1, -1):
        e2 = sv[-1] - sv[j - 1] - (c - x[j])
        e3 = e1 + e2 - x[i]
        e4 = e1 + e2 - (c - x[j])
        ans = max([ans, e2, e3, e4])
    ans = max([ans, e1])
for j in range(0, -n - 1, -1):
    e2 = sv[-1] - sv[j - 1] - (c - x[j])
    for i in range(1, n - i + 1, 1):
        e1 = sv[i] - x[i]        
        e3 = e1 + e2 - x[i]
        e4 = e1 + e2 - (c - x[j])
        ans = max([ans, e1, e3, e4])
    ans = max([ans, e1])
print(ans)

Fixed submission:
n, c = input().split()
n = int(n)
c = int(c)
x = [0]
v = [0]
gs1 = [0]
gs2 = [0]
gs3 = [0]
gs4 = [0]
s = 0
for i in range(n):
    xx, vv = input().split()
    xx = int(xx)
    vv = int(vv)
    x.append(xx)
    v.append(vv)
    s += vv
    f = s - xx
    gs1.append(max(gs1[-1], f))
    f = s - 2 * xx
    gs2.append(max(gs2[-1], f))
s = 0
for i in range(-1, -n - 1, -1):
    s += v[i]
    f = s - (c - x[i])
    gs3.append(max(gs3[-1], f))
    f = s - 2 * (c - x[i])
    gs4.append(max(gs4[-1], f))
ans = 0
for i in range(n + 1):
    ans = max(ans, gs1[i] + gs4[n - i], gs2[i] + gs3[n - i])
print(ans)

Buggy python submission (verdict: Runtime Error):
#import sys
#lines = sys.stdin.readlines()
#lines = lines.split('\n')
n, c = input().split()
#n, c = lines[0].split()
n = int(n)
c = int(c)
x = [0]
v = [0]
sv = [0]
ssv = 0
for i in range(n):
    xx, vv = input().split()
#    xx, vv = lines[i + 1].split()
    xx = int(xx)
    vv = int(vv)
    x.append(xx)
    v.append(vv)
    ssv += vv
    sv.append(ssv)
ans = -10**15
for i in range(0, n + 1, 1):
    for j in range(-1, -n +  i - 1, -1):
        e1 = sv[i] - x[i]
        e2 = sv[-1] - sv[j - 1] - (c - x[j])
        e3 = e1 + e2 - x[i]
        e3 = e1 + e2 - (c - x[j])
        ans = max([ans, e1, e2, e3, e4])
print(ans)

Fixed submission:
n, c = input().split()
n = int(n)
c = int(c)
x = [0]
v = [0]
gs1 = [0]
gs2 = [0]
gs3 = [0]
gs4 = [0]
s = 0
for i in range(n):
    xx, vv = input().split()
    xx = int(xx)
    vv = int(vv)
    x.append(xx)
    v.append(vv)
    s += vv
    f = s - xx
    gs1.append(max(gs1[-1], f))
    f = s - 2 * xx
    gs2.append(max(gs2[-1], f))
s = 0
for i in range(-1, -n - 1, -1):
    s += v[i]
    f = s - (c - x[i])
    gs3.append(max(gs3[-1], f))
    f = s - 2 * (c - x[i])
    gs4.append(max(gs4[-1], f))
ans = 0
for i in range(n + 1):
    ans = max(ans, gs1[i] + gs4[n - i], gs2[i] + gs3[n - i])
print(ans)

Buggy python submission (verdict: Runtime Error):
import sys
lines = sys.stdin.readlines()
lines = lines.split('\n')
#n, c = input().split()
n, c = lines[0].split()
n = int(n)
c = int(c)
x = [0]
v = [0]
sv = [0]
ssv = 0
for i in range(n):
#    xx, vv = input().split()
    xx, vv = lines[i + 1].split()
    xx = int(xx)
    vv = int(vv)
    x.append(xx)
    v.append(vv)
    ssv += vv
    sv.append(ssv)
ans = -10**15
for i in range(0, n + 1, 1):
    for j in range(-1, -n +  i - 1, -1):
        e1 = sv[i] - x[i]
        e2 = sv[-1] - sv[j - 1] - (c - x[j])
        e3 = e1 + e2 - x[i]
        e3 = e1 + e2 - (c - x[j])
        ans = max([ans, e1, e2, e3, e4])
print(ans)

Fixed submission:
n, c = input().split()
n = int(n)
c = int(c)
x = [0]
v = [0]
gs1 = [0]
gs2 = [0]
gs3 = [0]
gs4 = [0]
s = 0
for i in range(n):
    xx, vv = input().split()
    xx = int(xx)
    vv = int(vv)
    x.append(xx)
    v.append(vv)
    s += vv
    f = s - xx
    gs1.append(max(gs1[-1], f))
    f = s - 2 * xx
    gs2.append(max(gs2[-1], f))
s = 0
for i in range(-1, -n - 1, -1):
    s += v[i]
    f = s - (c - x[i])
    gs3.append(max(gs3[-1], f))
    f = s - 2 * (c - x[i])
    gs4.append(max(gs4[-1], f))
ans = 0
for i in range(n + 1):
    ans = max(ans, gs1[i] + gs4[n - i], gs2[i] + gs3[n - i])
print(ans)

Buggy python submission (verdict: Runtime Error):
n,c=input().split()
n=int(n)
c=int(c)
x=[]
v=[]
e=[]
v_max=10**9
c_max=10**14
for _ in range(n):
    s = input().split()
    x.append(int(s[0]))
    v.append(int(s[1]))
    e.append(int(s[1])-int(s[0]))
ans=0
l=set()
while True:
    p = 0
    ee_max = 0
    for i in range(p, n):
        if i in l:
            continue
        ee = e[i]
        if ee > ee_max and max_ee > 0:
            p = i
            ee_max = ee
    if ee_max <= 0:
        break
    ans = ans + e[p]
    l.add(p)
    #for i in range(n):
    #    x[i] = min(abs(x[i]-x[p]), c-abs(x[i]-x[p]))
    #    e[i] = x[i] - v[i]
print(ans)

Fixed submission:
n, c = input().split()
n = int(n)
c = int(c)
x = [0]
v = [0]
gs1 = [0]
gs2 = [0]
gs3 = [0]
gs4 = [0]
s = 0
for i in range(n):
    xx, vv = input().split()
    xx = int(xx)
    vv = int(vv)
    x.append(xx)
    v.append(vv)
    s += vv
    f = s - xx
    gs1.append(max(gs1[-1], f))
    f = s - 2 * xx
    gs2.append(max(gs2[-1], f))
s = 0
for i in range(-1, -n - 1, -1):
    s += v[i]
    f = s - (c - x[i])
    gs3.append(max(gs3[-1], f))
    f = s - 2 * (c - x[i])
    gs4.append(max(gs4[-1], f))
ans = 0
for i in range(n + 1):
    ans = max(ans, gs1[i] + gs4[n - i], gs2[i] + gs3[n - i])
print(ans)