Buggy python submission (verdict: Time Limit Exceeded):
N,C=map(int,input().split())
x=[0 for i in range(N)]
v=[0 for i in range(N)]
for i in range(N):
    x[i],v[i]=map(int,input().split())
a=[0 for i in range(N)]
b=[0 for i in range(N)]
a[0]=v[0]
b[N-1]=v[N-1]
for i in range(1,N):
    a[i]=v[i]+a[i-1]
for i in range(N-1)[::-1]:
    b[i]=b[i+1]+v[i]
ans=0
for i in range(N+1):
    for j in range(N-i+1):
        if i==0:
            OA=0
            EA=0
        else:
            OA=x[i-1]
            EA=a[i-1]
        if j==0:
            OB=0
            EB=0
        else:
            OB=C-x[N-j]
            EB=b[N-j]
        tmpA=EA+EB-OA-OA-OB
        tmpB=EA+EB-OA-OB-OB
        if ans<tmpA:
            ans=tmpA
        if ans<tmpB:
            ans=tmpB
print(ans)


Fixed submission:
N,C=map(int,input().split())
x=[0 for i in range(N)]
v=[0 for i in range(N)]
for i in range(N):
    x[i],v[i]=map(int,input().split())
a=[0 for i in range(N)]
b=[0 for i in range(N)]
a[0]=v[0]
b[N-1]=v[N-1]
for i in range(1,N):
    a[i]=v[i]+a[i-1]
for i in range(N-1)[::-1]:
    b[i]=b[i+1]+v[i]
ans=0
f1=[0 for i in range(N+1)]
f1[0]=0
f1[1]=v[0]-x[0]
for i in range(1,N):
    f1[i+1]=f1[i]+v[i]+x[i-1]-x[i]
g1=[0 for i in range(N+1)]
g1[0]=f1[0]
for i in range(1,N+1):
    g1[i]=max([f1[i],g1[i-1]])
f2=[0 for i in range(N+1)]
f2[0]=0
f2[1]=v[0]-2*x[0]
for i in range(1,N):
    f2[i+1]=f2[i]+v[i]+2*x[i-1]-2*x[i]
g2=[0 for i in range(N+1)]
g2[0]=f2[0]
for i in range(1,N+1):
    g2[i]=max([f2[i],g2[i-1]])
for i in range(N+1):
    if i==0:
        OB=0
        EB=0
    else:
        OB=C-x[N-i]
        EB=b[N-i]
    tmpA=EB-2*OB+g1[N-i]
    tmpB=EB-OB+g2[N-i]
    if ans<tmpA:
        ans=tmpA
    if ans<tmpB:
        ans=tmpB
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,C=map(int,input().split())
x=[0 for i in range(N)]
v=[0 for i in range(N)]
for i in range(N):
    x[i],v[i]=map(int,input().split())
a=[0 for i in range(N)]
b=[0 for i in range(N)]
a[0]=v[0]
b[N-1]=v[N-1]
for i in range(1,N):
    a[i]=v[i]+a[i-1]
for i in range(N-1)[::-1]:
    b[i]=b[i+1]+v[i]
ans=0
for i in range(N+1):
    for j in range(N-i+1):
        if i==0:
            OA=0
            EA=0
        else:
            OA=x[i-1]
            EA=a[i-1]
        if j==0:
            OB=0
            EB=0
        else:
            OB=C-x[N-j]
            EB=b[N-j]
        tmpA=EA+EB-OA-OA-OB
        tmpB=EA+EB-OA-OB-OB
        if ans<tmpA:
            ans=tmpA
        if ans<tmpB:
            ans=tmpB
print(ans)


Fixed submission:
N,C=map(int,input().split())
x=[0 for i in range(N)]
v=[0 for i in range(N)]
for i in range(N):
    x[i],v[i]=map(int,input().split())
a=[0 for i in range(N)]
b=[0 for i in range(N)]
a[0]=v[0]
b[N-1]=v[N-1]
for i in range(1,N):
    a[i]=v[i]+a[i-1]
for i in range(N-1)[::-1]:
    b[i]=b[i+1]+v[i]
ans=0
f1=[0 for i in range(N+1)]
f1[0]=0
f1[1]=v[0]-x[0]
for i in range(1,N):
    f1[i+1]=f1[i]+v[i]+x[i-1]-x[i]
g1=[0 for i in range(N+1)]
g1[0]=f1[0]
for i in range(1,N+1):
    g1[i]=max([f1[i],g1[i-1]])
f2=[0 for i in range(N+1)]
f2[0]=0
f2[1]=v[0]-2*x[0]
for i in range(1,N):
    f2[i+1]=f2[i]+v[i]+2*x[i-1]-2*x[i]
g2=[0 for i in range(N+1)]
g2[0]=f2[0]
for i in range(1,N+1):
    g2[i]=max([f2[i],g2[i-1]])
for i in range(N+1):
    if i==0:
        OB=0
        EB=0
    else:
        OB=C-x[N-i]
        EB=b[N-i]
    tmpA=EB-2*OB+g1[N-i]
    tmpB=EB-OB+g2[N-i]
    if ans<tmpA:
        ans=tmpA
    if ans<tmpB:
        ans=tmpB
print(ans)