Buggy python submission (verdict: Time Limit Exceeded):
# 外周Cメートル
# カウンター上には N 貫の寿司
# i 貫目は中橋くんがいる位置から x_i メートル時計回りに進んだ位置に置かれており、
# v_i キロカロリーの栄養価を持ちます
# 歩く際に 1 メートルあたり 1 キロカロリーを消費

N, C = map(int,input().split())
x=["" for i in range(N)]
v=["" for i in range(N)]


for i in range(N):
    x[i], v[i] = map(int,input().split())

#時計回りの場合の最大
CalR=[0 for i in range(N)]
for i in range(N):
    CalR[i]=sum(v[:i+1])-x[i]

MaxR=[0 for i in range(N+1)]

for i in range(1,N+1):
    if CalR[i-1]>=MaxR[i-1]:
        MaxR[i]=CalR[i-1]
    else:
        MaxR[i]=MaxR[i-1]

#反時計回りの最大
CalL=[0 for i in range(N)]
for i in range(N):
    CalL[i]=sum(v[N-i-1:])-(C-x[N-i-1])

MaxL=[0 for i in range(N+1)]

for i in range(1,N+1):
    if CalL[i-1]>=MaxL[i-1]:
        MaxL[i]=CalL[i-1]
    else:
        MaxL[i]=MaxL[i-1]

#最初時計回りに進んだ場合の答え
AnsR=[0 for i in range(N)]

for i in range(N):
    AnsR[i]=CalR[i]-x[i]+MaxL[N-1-i]

#最初反時計回りに進んだ場合の答え
AnsL=[0 for i in range(N)]

for i in range(N):
    AnsL[i]=CalL[i]-(C-x[N-1-i])+MaxR[N-1-i]

print(max(max(AnsR),max(AnsL),max(CalR),max(CalL),0))

Fixed submission:
N, C = map(int,input().split())
x=["" for i in range(N)]
v=["" for i in range(N)]

for i in range(N):
    x[i], v[i] = map(int,input().split())

#時計回りの場合の最大
CalR=[0 for i in range(N)]
CalR[0]=v[0]-x[0]
for i in range(1,N):
    CalR[i]=CalR[i-1]+v[i]-x[i]+x[i-1]

MaxR=[0 for i in range(N+1)]

for i in range(1,N+1):
    if CalR[i-1]>=MaxR[i-1]:
        MaxR[i]=CalR[i-1]
    else:
        MaxR[i]=MaxR[i-1]

#反時計回りの最大
CalL=[0 for i in range(N)]
CalL[0]=v[N-1]-(C-x[N-1])
for i in range(1,N):
    CalL[i]=CalL[i-1]+v[N-i-1]+x[N-i-1]-x[N-i]

MaxL=[0 for i in range(N+1)]

for i in range(1,N+1):
    if CalL[i-1]>=MaxL[i-1]:
        MaxL[i]=CalL[i-1]
    else:
        MaxL[i]=MaxL[i-1]

#最初時計回りに進んだ場合の答え
AnsR=[0 for i in range(N)]

for i in range(N):
    AnsR[i]=CalR[i]-x[i]+MaxL[N-1-i]

#最初反時計回りに進んだ場合の答え
AnsL=[0 for i in range(N)]

for i in range(N):
    AnsL[i]=CalL[i]-(C-x[N-1-i])+MaxR[N-1-i]

print(max(max(AnsR),max(AnsL),max(CalR),max(CalL),0))

Buggy python submission (verdict: Time Limit Exceeded):
# 外周Cメートル
# カウンター上には N 貫の寿司
# i 貫目は中橋くんがいる位置から x_i メートル時計回りに進んだ位置に置かれており、
# v_i キロカロリーの栄養価を持ちます
# 歩く際に 1 メートルあたり 1 キロカロリーを消費

N, C = map(int,input().split())
x=["" for i in range(N)]
v=["" for i in range(N)]

for i in range(N):
    x[i], v[i] = map(int,input().split())

#時計回りの場合の最大
CalR=[0 for i in range(N)]
for i in range(N):
    CalR[i]=sum(v[:i+1])-x[i]

MaxR=[0 for i in range(N)]

for i in range(N):
    MaxR[i]=max(CalR[:i+1])
    
MaxR=[0]+MaxR

#反時計回りの最大
CalL=[0 for i in range(N)]
for i in range(N):
    CalL[i]=sum(v[N-i-1:])-(C-x[N-i-1])

MaxL=[0 for i in range(N)]

for i in range(N):
    MaxL[i]=max(CalL[:i+1])

MaxL=[0]+MaxL

#最初時計回りに進んだ場合の答え
AnsR=[0 for i in range(N)]

for i in range(N):
    AnsR[i]=CalR[i]-x[i]+MaxL[N-1-i]

#最初反時計回りに進んだ場合の答え
AnsL=[0 for i in range(N)]

for i in range(N):
    AnsL[i]=CalL[i]-(C-x[N-1-i])+MaxR[N-1-i]

print(max(max(AnsR),max(AnsL),max(CalR),max(CalL),0))

Fixed submission:
N, C = map(int,input().split())
x=["" for i in range(N)]
v=["" for i in range(N)]

for i in range(N):
    x[i], v[i] = map(int,input().split())

#時計回りの場合の最大
CalR=[0 for i in range(N)]
CalR[0]=v[0]-x[0]
for i in range(1,N):
    CalR[i]=CalR[i-1]+v[i]-x[i]+x[i-1]

MaxR=[0 for i in range(N+1)]

for i in range(1,N+1):
    if CalR[i-1]>=MaxR[i-1]:
        MaxR[i]=CalR[i-1]
    else:
        MaxR[i]=MaxR[i-1]

#反時計回りの最大
CalL=[0 for i in range(N)]
CalL[0]=v[N-1]-(C-x[N-1])
for i in range(1,N):
    CalL[i]=CalL[i-1]+v[N-i-1]+x[N-i-1]-x[N-i]

MaxL=[0 for i in range(N+1)]

for i in range(1,N+1):
    if CalL[i-1]>=MaxL[i-1]:
        MaxL[i]=CalL[i-1]
    else:
        MaxL[i]=MaxL[i-1]

#最初時計回りに進んだ場合の答え
AnsR=[0 for i in range(N)]

for i in range(N):
    AnsR[i]=CalR[i]-x[i]+MaxL[N-1-i]

#最初反時計回りに進んだ場合の答え
AnsL=[0 for i in range(N)]

for i in range(N):
    AnsL[i]=CalL[i]-(C-x[N-1-i])+MaxR[N-1-i]

print(max(max(AnsR),max(AnsL),max(CalR),max(CalL),0))