Buggy python submission (verdict: Time Limit Exceeded):
N,C=map(int, raw_input().split())
X=[ tuple( map(int, raw_input().split()) ) for i in range(N)]
T=[]
R=[]

a=[]

v_sum=0
for x,v in X:
	v_sum+=v
	T.append(v_sum-x)

a+=T

v_sum=0
for x,v in X[::-1]:
	v_sum+=v
	R.append(v_sum-(C-x))

a+=R

for i in range(N):
	extra_x=X[i][0]
	if len(R[:N-i-1])>0: val=T[i]+max(R[:N-i-1])-extra_x
	else: val=T[i]
	a.append(val)


for i in range(N):
	extra_x=C-X[N-i-1][0]
	if len(T[:N-i-1])>0: val=R[i]+max(T[:N-i-1])-extra_x
	else: val=R[i]
	a.append(val)


print max(0,max(a))

Fixed submission:
N,C=map(int, raw_input().split())
X=[ tuple( map(int, raw_input().split()) ) for i in range(N)]
T=[]
R=[]

a=[]

v_sum=0
for x,v in X:
	v_sum+=v
	T.append(v_sum-x)

a+=T

v_sum=0
for x,v in X[::-1]:
	v_sum+=v
	R.append(v_sum-(C-x))

a+=R



T_MAX=[]
val=float("-inf")
for i in T:
	val=max(val,i)
	T_MAX.append(val)


R_MAX=[]
val=float("-inf")
for i in R:
	val=max(val,i)
	R_MAX.append(val)


for i in range(N):
	extra_x=X[i][0]
	if N-i-2>=0:
		val=T[i]+R_MAX[N-i-2]-extra_x
		a.append(val)


for i in range(N):
	extra_x=C-X[N-i-1][0]
	if N-i-2>=0:
		val=R[i]+T_MAX[N-i-2]-extra_x
		a.append(val)


print max(0,max(a))

Buggy python submission (verdict: Time Limit Exceeded):
N,C=map(int, raw_input().split())
X=[ tuple( map(int, raw_input().split()) ) for i in range(N)]
T=[]
R=[]

a=[]
for i in range(N):
	temp=[]
	for j in range(i+1):
		temp.append(X[j])

	back=X[i][0]

	for j in range(N-1,i,-1):
		x=C-X[j][0]
		v=X[j][1]
		temp.append( (x+back*2,v) )
	val=0
	for x,y in temp:
		val+=y
		a.append(val-x)


for i in range(N):
	
	temp=[]
	for j in range(N-1,N-i-2,-1):
		x=C-X[j][0]
		v=X[j][1]
		temp.append( (x,v) )
	back=C-X[N-1-i][0]

	for j in range(N-i-1):
		x=X[j][0]
		v=X[j][1]
		temp.append( (x+back*2,v) )
	val=0
	for x,y in temp:
		val+=y
		a.append(val-x)


print max(0,max(a))

Fixed submission:
N,C=map(int, raw_input().split())
X=[ tuple( map(int, raw_input().split()) ) for i in range(N)]
T=[]
R=[]

a=[]

v_sum=0
for x,v in X:
	v_sum+=v
	T.append(v_sum-x)

a+=T

v_sum=0
for x,v in X[::-1]:
	v_sum+=v
	R.append(v_sum-(C-x))

a+=R



T_MAX=[]
val=float("-inf")
for i in T:
	val=max(val,i)
	T_MAX.append(val)


R_MAX=[]
val=float("-inf")
for i in R:
	val=max(val,i)
	R_MAX.append(val)


for i in range(N):
	extra_x=X[i][0]
	if N-i-2>=0:
		val=T[i]+R_MAX[N-i-2]-extra_x
		a.append(val)


for i in range(N):
	extra_x=C-X[N-i-1][0]
	if N-i-2>=0:
		val=R[i]+T_MAX[N-i-2]-extra_x
		a.append(val)


print max(0,max(a))

Buggy python submission (verdict: Time Limit Exceeded):
N,C=map(int, raw_input().split())
X=[ tuple( map(int, raw_input().split()) ) for i in range(N)]
T=[]
R=[]

a=[]
for i in range(N):
	temp=[]
	for j in range(i+1):
		temp.append(X[j])

	back=X[i][0]

	for j in range(N-1,i,-1):
		x=C-X[j][0]
		v=X[j][1]
		temp.append( (x+back*2,v) )
	val=0
	for x,y in temp:
		val+=y
		a.append(val-x)


for i in range(N):
	
	temp=[]
	for j in range(N-1,N-i-2,-1):
		x=C-X[j][0]
		v=X[j][1]
		#print j
		temp.append( (x,v) )
	back=C-X[N-1-i][0]

	for j in range(N-i-1):
		x=X[j][0]
		v=X[j][1]
		temp.append( (x+back*2,v) )
	val=0
	for x,y in temp:
		val+=y
		a.append(val-x)


print max(0,max(a))
		


Fixed submission:
N,C=map(int, raw_input().split())
X=[ tuple( map(int, raw_input().split()) ) for i in range(N)]
T=[]
R=[]

a=[]

v_sum=0
for x,v in X:
	v_sum+=v
	T.append(v_sum-x)

a+=T

v_sum=0
for x,v in X[::-1]:
	v_sum+=v
	R.append(v_sum-(C-x))

a+=R



T_MAX=[]
val=float("-inf")
for i in T:
	val=max(val,i)
	T_MAX.append(val)


R_MAX=[]
val=float("-inf")
for i in R:
	val=max(val,i)
	R_MAX.append(val)


for i in range(N):
	extra_x=X[i][0]
	if N-i-2>=0:
		val=T[i]+R_MAX[N-i-2]-extra_x
		a.append(val)


for i in range(N):
	extra_x=C-X[N-i-1][0]
	if N-i-2>=0:
		val=R[i]+T_MAX[N-i-2]-extra_x
		a.append(val)


print max(0,max(a))