Buggy python submission (verdict: Time Limit Exceeded):
n,c=[int(i) for i in input().split()]
x=[[0 for i in range(2)] for j in range(n)]
for i in range(n):
    x[i]= [int(i) for i in input().split()]
x.sort(key=lambda x:x[0])

state=0

left_wa=[0 for i in range(n)]
right_wa=[0 for i in range(n)]


for i in range(n):
    if i==0:
        left_wa[i]=x[i][1]-abs(x[i][0]-state)
    else:
        left_wa[i]=left_wa[i-1]+x[i][1]-abs(x[i][0]-state)
    state=x[i][0]

state=c

for i in range(n):
    if i==0:
        right_wa[i]=x[n-1-i][1]-abs(x[n-1-i][0]-state)
    else:
        right_wa[i]= right_wa[i-1]+x[n-1-i][1]-abs(abs(c-x[n-1-i][0])-abs(state))
    state=c-x[n-1-i][0]

kcal=0

for i in range(n):
    if i!=n-1:
        right=max(right_wa[:n-1-i])-x[i][0]
    else: right=0
    kcal=max(kcal, left_wa[i]+max(0, right))

for i in range(n):
    if i!=n-1:
        left=max(left_wa[:n-1-i])-abs(c-x[n-i-1][0])
    else: left=0
    kcal=max(kcal, right_wa[i]+max(0, left))



#print(left_wa)
#print(right_wa)

print(kcal)

Fixed submission:
n,c=[int(i) for i in input().split()]
x=[[0 for i in range(2)] for j in range(n)]
for i in range(n):
    x[i]= [int(i) for i in input().split()]
x.sort(key=lambda x:x[0])

state=0

left_wa=[0 for i in range(n)]
right_wa=[0 for i in range(n)]

for i in range(n):
    if i==0:
        left_wa[i]=x[i][1]-abs(x[i][0]-state)
    else:
        left_wa[i]=left_wa[i-1]+x[i][1]-abs(x[i][0]-state)
    state=x[i][0]

state=c

for i in range(n):
    if i==0:
        right_wa[i]=x[n-1-i][1]-abs(x[n-1-i][0]-state)
    else:
        right_wa[i]= right_wa[i-1]+x[n-1-i][1]-abs(abs(c-x[n-1-i][0])-abs(state))
    state=c-x[n-1-i][0]

kcal=0

m_right=[0 for i in range(n+1)]
m_left=[0 for i in range(n+1)]

for i in range(n+1):
    if i!=0:
        m_right[i]=max(m_right[i-1], right_wa[i-1])
        m_left[i]=max(m_left[i-1], left_wa[i-1])

#print(m_left)

for i in range(n):
    if i!=n-1:
#        right=max(right_wa[:n-1-i])-x[i][0]
        right= m_right[n-1-i]-x[i][0]
    else: right=0
    kcal=max(kcal, left_wa[i]+max(0, right))

for i in range(n):
    if i!=n-1:
#        left=max(left_wa[:n-1-i])-abs(c-x[n-i-1][0])
       left=m_left[n-1-i]-abs(c-x[n-i-1][0])

    else: left=0
    kcal=max(kcal, right_wa[i]+max(0, left))


print(kcal)

Buggy python submission (verdict: Time Limit Exceeded):
n,c=[int(i) for i in input().split()]
x=[[0 for i in range(2)] for j in range(n)]
for i in range(n):
    x[i]= [int(i) for i in input().split()]
x.sort(key=lambda x:x[0])

state=0

left_wa=[0 for i in range(n)]
right_wa=[0 for i in range(n)]


for i in range(n):
    if i==0:
        left_wa[i]=x[i][1]-abs(x[i][0]-state)
    else:
        left_wa[i]=left_wa[i-1]+x[i][1]-abs(x[i][0]-state)
    state=x[i][0]

state=c

for i in range(n):
    if i==0:
        right_wa[i]=x[n-1-i][1]-abs(x[n-1-i][0]-state)
    else:
        right_wa[i]= right_wa[i-1]+x[n-1-i][1]-abs(abs(c-x[n-1-i][0])-abs(state))
    state=c-x[n-1-i][0]

kcal=0

for i in range(n):
    if i!=n-1:
        right=max(right_wa[:n-1-i])-x[i][0]
    else: right=0
    kcal=max(kcal, left_wa[i]+max(0, right))

for i in range(n):
    if i!=n-1:
        left=max(left_wa[:n-1-i])-abs(c-x[n-i-1][0])
    else: left=0
    kcal=max(kcal, right_wa[i]+max(0, left))



#print(left_wa)
#print(right_wa)

print(kcal)

Fixed submission:
n,c=[int(i) for i in input().split()]
x=[[0 for i in range(2)] for j in range(n)]
for i in range(n):
    x[i]= [int(i) for i in input().split()]
x.sort(key=lambda x:x[0])

state=0

left_wa=[0 for i in range(n)]
right_wa=[0 for i in range(n)]

for i in range(n):
    if i==0:
        left_wa[i]=x[i][1]-abs(x[i][0]-state)
    else:
        left_wa[i]=left_wa[i-1]+x[i][1]-abs(x[i][0]-state)
    state=x[i][0]

state=c

for i in range(n):
    if i==0:
        right_wa[i]=x[n-1-i][1]-abs(x[n-1-i][0]-state)
    else:
        right_wa[i]= right_wa[i-1]+x[n-1-i][1]-abs(abs(c-x[n-1-i][0])-abs(state))
    state=c-x[n-1-i][0]

kcal=0

m_right=[0 for i in range(n+1)]
m_left=[0 for i in range(n+1)]

for i in range(n+1):
    if i!=0:
        m_right[i]=max(m_right[i-1], right_wa[i-1])
        m_left[i]=max(m_left[i-1], left_wa[i-1])

#print(m_left)

for i in range(n):
    if i!=n-1:
#        right=max(right_wa[:n-1-i])-x[i][0]
        right= m_right[n-1-i]-x[i][0]
    else: right=0
    kcal=max(kcal, left_wa[i]+max(0, right))

for i in range(n):
    if i!=n-1:
#        left=max(left_wa[:n-1-i])-abs(c-x[n-i-1][0])
       left=m_left[n-1-i]-abs(c-x[n-i-1][0])

    else: left=0
    kcal=max(kcal, right_wa[i]+max(0, left))


print(kcal)