Problem:


Score : 500 points

Problem Statement

"Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter.

Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories.

Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter.

Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.

Constraints

1 ≤ N ≤ 10^5

2 ≤ C ≤ 10^{14}

1 ≤ x_1 < x_2 < ... < x_N < C

1 ≤ v_i ≤ 10^9

All values in input are integers.

Subscores

300 points will be awarded for passing the test set satisfying N ≤ 100.

Input

Input is given from Standard Input in the following format:

N C
x_1 v_1
x_2 v_2
:
x_N v_N

Output

If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c.

Sample Input 1

3 20
2 80
9 120
16 1

Sample Output 1

191

There are three sushi on the counter with a circumference of 20 meters. If he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks seven more meters clockwise, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 9 kilocalories, thus he can take in 191 kilocalories on balance, which is the largest possible value.

Sample Input 2

3 20
2 80
9 1
16 120

Sample Output 2

192

The second and third sushi have been swapped. Again, if he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks six more meters counterclockwise this time, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 8 kilocalories, thus he can take in 192 kilocalories on balance, which is the largest possible value.

Sample Input 3

1 100000000000000
50000000000000 1

Sample Output 3

0

Even though the only sushi is so far that it does not fit into a 32-bit integer, its nutritive value is low, thus he should immediately leave without doing anything.

Sample Input 4

15 10000000000
400000000 1000000000
800000000 1000000000
1900000000 1000000000
2400000000 1000000000
2900000000 1000000000
3300000000 1000000000
3700000000 1000000000
3800000000 1000000000
4000000000 1000000000
4100000000 1000000000
5200000000 1000000000
6600000000 1000000000
8000000000 1000000000
9300000000 1000000000
9700000000 1000000000

Sample Output 4

6500000000

All these sample inputs above are included in the test set for the partial score.



Sample input:
3 20
2 80
9 120
16 1


Sample output:
191

Submission status: Wrong Answer

Solution code:
import numpy as np
def hanten(C, X, V):
	return (C*np.ones(X.shape[0])-X)[::-1], V[::-1]

def solv(N, C, X, V):
	#print(solv1(N, C, X, V), solv2(N, C, X, V))
	return max(solv1(N, C, X, V), solv2(N, C, X, V))

def solv1(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
	return r

def solv2(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	ibuf = 0
	for i in range(N):
		rbuf += V[i] - 2*(X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
			ibuf = i
			#print(r)
		if(pos > C/3):
			break
	Vbuf = V.copy()
	for i in range(ibuf+1):
		Vbuf[i] = 0
	Xbuf, Vbuf = hanten(C, X, Vbuf)
	#print(r)
	r += solv1(N, C, Xbuf, Vbuf)
	#print(r, Xbuf, Vbuf)
	return r
	
def main():
	buf = input().split()
	N = int(buf[0])
	C = int(buf[1])
	X = np.zeros(N)
	V = np.zeros(N)
	for i in range(N):
		buf = input().split()
		X[i] = int(buf[0])
		V[i] = int(buf[1])
	
	r1 = solv(N, C, X, V)
	X, V = hanten(C, X, V)
	r2 = solv(N, C, X, V)
	print(int(max(r1, r2)))

main()

Submission status: Wrong Answer

Solution code:
import numpy as np
def hanten(C, X, V):
	return (C*np.ones(X.shape[0])-X)[::-1], V[::-1]

def solv(N, C, X, V):
	return max(solv1(N, C, X, V), solv2(N, C, X, V))

def solv1(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
	return r

def solv2(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	ibuf = 0
	for i in range(N):
		rbuf += V[i] - 2*(X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
			ibuf = i
		if(pos > C/3):
			break
	Vbuf = V.copy()
	for i in range(ibuf):
		Vbuf[i] = 0
	Xbuf, Vbuf = hanten(C, X, Vbuf)
	r += solv1(N, C, Xbuf, Vbuf)
	return r
	
def main():
	buf = input().split()
	N = int(buf[0])
	C = int(buf[1])
	X = np.zeros(N)
	V = np.zeros(N)
	for i in range(N):
		buf = input().split()
		X[i] = int(buf[0])
		V[i] = int(buf[1])
	
	r1 = solv(N, C, X, V)
	X, V = hanten(C, X, V)
	r2 = solv(N, C, X, V)
	print(int(max(r1, r2)))

main()

Submission status: Wrong Answer

Solution code:
import numpy as np
def hanten(C, X, V):
	return (C*np.ones(X.shape[0])-X)[::-1], V[::-1]

def solv(N, C, X, V):
	return max(solv1(N, C, X, V), solv2(N, C, X, V))

def solv1(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
	return r

def solv2(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	ibuf = 0
	for i in range(N):
		rbuf += V[i] - 2*(X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
			ibuf = i
		if(pos > C/2):
			break
	Vbuf = V.copy()
	for i in range(ibuf):
		Vbuf[i] = 0
	Xbuf, Vbuf = hanten(C, X, Vbuf)
	r += solv1(N, C, Xbuf, Vbuf)
	return r
	
def main():
	buf = input().split()
	N = int(buf[0])
	C = int(buf[1])
	X = np.zeros(N)
	V = np.zeros(N)
	for i in range(N):
		buf = input().split()
		X[i] = int(buf[0])
		V[i] = int(buf[1])
	
	r1 = solv(N, C, X, V)
	X, V = hanten(C, X, V)
	r2 = solv(N, C, X, V)
	print(int(max(r1, r2)))

main()

Submission status: Wrong Answer

Solution code:
import numpy as np
def hanten(C, X, V):
	return (C*np.ones(X.shape[0])-X)[::-1], V[::-1]

def solv(N, C, X, V):
	return max(solv1(N, C, X, V, 0), solv2(N, C, X, V))

def solv1(N, C, X, V, pos):
	r = 0
	rbuf = 0
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
	return r

def solv2(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	ibuf = 0
	for i in range(N):
		rbuf += V[i] - 2*(X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
			ibuf = i
		#if(pos > C/2):
		#	break
	Vbuf = V.copy()
	for i in range(ibuf):
		Vbuf[i] = 0
	Xbuf, Vbuf = hanten(C, X, Vbuf)
	r += solv1(N, C, Xbuf, Vbuf, 0)
	return r
	
def main():
	buf = input().split()
	N = int(buf[0])
	C = int(buf[1])
	X = np.zeros(N)
	V = np.zeros(N)
	for i in range(N):
		buf = input().split()
		X[i] = int(buf[0])
		V[i] = int(buf[1])
	
	r1 = solv2(N, C, X, V)
	X, V = hanten(C, X, V)
	r2 = solv2(N, C, X, V)
	print(int(max(r1, r2)))

main()

Submission status: Wrong Answer

Solution code:
import numpy as np
def hanten(C, X, V):
	return (C*np.ones(X.shape[0])-X)[::-1], V[::-1]

def solv(N, C, X, V):
	return max(solv1(N, C, X, V, 0), solv2(N, C, X, V))

def solv1(N, C, X, V, pos):
	r = 0
	rbuf = 0
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
	return r

def solv2(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	ibuf = 0
	for i in range(N):
		rbuf += V[i] - 2*(X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
			ibuf = i
		if(pos > C/2):
			break
	Vbuf = V.copy()
	for i in range(ibuf):
		Vbuf[i] = 0
	Xbuf, Vbuf = hanten(C, X, Vbuf)
	r += solv1(N, C, Xbuf, Vbuf, 0)
	return r
	
def main():
	buf = input().split()
	N = int(buf[0])
	C = int(buf[1])
	X = np.zeros(N)
	V = np.zeros(N)
	for i in range(N):
		buf = input().split()
		X[i] = int(buf[0])
		V[i] = int(buf[1])
	
	r1 = solv2(N, C, X, V)
	X, V = hanten(C, X, V)
	r2 = solv2(N, C, X, V)
	print(int(max(r1, r2)))

main()

Submission status: Wrong Answer

Solution code:
import numpy as np
def hanten(C, X, V):
	return (C*np.ones(X.shape[0])-X)[::-1], V[::-1]

def solv(N, C, X, V):
	return max(solv1(N, C, X, V, 0), solv2(N, C, X, V))

def solv1(N, C, X, V, pos):
	r = 0
	rbuf = 0
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
	return r

def solv2(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	posbuf = 0
	ibuf = 0
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
			posbuf = pos
			ibuf = i
		#if(pos > C/2):
		#	break
	Vbuf = V.copy()
	for i in range(ibuf):
		Vbuf[i] = 0
	Xbuf, Vbuf = hanten(C, X, Vbuf)
	r += solv1(N, C, Xbuf, Vbuf, -posbuf)
	return r
	
def main():
	buf = input().split()
	N = int(buf[0])
	C = int(buf[1])
	X = np.zeros(N)
	V = np.zeros(N)
	for i in range(N):
		buf = input().split()
		X[i] = int(buf[0])
		V[i] = int(buf[1])
	
	r1 = solv2(N, C, X, V)
	X, V = hanten(C, X, V)
	r2 = solv2(N, C, X, V)
	print(int(max(r1, r2)))

main()

Submission status: Wrong Answer

Solution code:
import numpy as np
def hanten(C, X, V):
	return (C*np.ones(X.shape[0])-X)[::-1], V[::-1]

def solv(N, C, X, V):
	return max(solv1(N, C, X, V, 0), solv2(N, C, X, V))

def solv1(N, C, X, V, pos):
	r = 0
	rbuf = 0
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
	return r

def solv2(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	posbuf = 0
	ibuf = 0
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
			posbuf = pos
			ibuf = i
		if(pos > C/2):
			break
	Vbuf = V.copy()
	for i in range(ibuf):
		Vbuf[i] = 0
	Xbuf, Vbuf = hanten(C, X, Vbuf)
	r += solv1(N, C, Xbuf, Vbuf, -posbuf)
	return r
	
def main():
	buf = input().split()
	N = int(buf[0])
	C = int(buf[1])
	X = np.zeros(N)
	V = np.zeros(N)
	for i in range(N):
		buf = input().split()
		X[i] = int(buf[0])
		V[i] = int(buf[1])
	
	r1 = solv2(N, C, X, V)
	X, V = hanten(C, X, V)
	r2 = solv2(N, C, X, V)
	print(int(max(r1, r2)))

main()

Submission status: Wrong Answer

Solution code:
import numpy as np
# your code goes here
def hanten(C, X, V):
	return (C*np.ones(X.shape[0])-X)[::-1], V[::-1]

def solv(N, C, X, V):
	return max(solv1(N, C, X, V, 0), solv2(N, C, X, V))

def solv1(N, C, X, V, pos):
	r = 0
	rbuf = 0
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
	return r

def solv2(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	VV = V.copy()
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		VV[i] = 0
		Xbuf, Vbuf = hanten(C, X, VV)
		rbufbuf = rbuf + solv1(N, C, Xbuf, Vbuf, -pos)
		#print(Xbuf, Vbuf, pos, rbufbuf)
		if(rbufbuf > r):
			r = rbufbuf
		if(pos > C/3):
			break
	return r
	
def main():
	buf = input().split()
	N = int(buf[0])
	C = int(buf[1])
	X = np.zeros(N)
	V = np.zeros(N)
	for i in range(N):
		buf = input().split()
		X[i] = int(buf[0])
		V[i] = int(buf[1])
	
	r1 = solv2(N, C, X, V)
	X, V = hanten(C, X, V)
	r2 = solv2(N, C, X, V)
	print(int(max(r1, r2)))

main()

Submission status: Wrong Answer

Solution code:
#coding:utf-8
n, cir = map(int,input().split())
List = [[0,0]] + [list(map(int,input().split())) for i in range(n)]

maxv = 0
clck = [0] * (n+1)
anti = [0] * (n+1)
for i in range(1,n+1):
    clck[i] = clck[i-1] + List[i][1] - List[i][0] + List[i-1][0]
    
List[0] = [cir,0]
for i in range(1,n+1):
    anti[i] = anti[i-1] + List[-i][1] + List[-i][0] - List[-i+1][0]

antiMax = [[0,0]] + anti[:]
for i in range(1,n+1):
    if anti[i-1] < anti[i]:
        antiMax[i] = [cir - List[-i][0], anti[i]]
    else:
        antiMax[i] = antiMax[i-1]
        anti[i] = anti[i-1]

List[0] = [0,0]

for i in range(n+1):
    kcal = clck[i] - antiMax[n-i][0]
    maxv = max(kcal + antiMax[n-i][1], maxv)
    kcal = clck[i] - List[i][0]
    maxv = max(kcal + antiMax[n-i][1], maxv)

print(maxv)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
n, cir = map(int,input().split())
List = [[0,0]] + [list(map(int,input().split())) for i in range(n)]

maxv = 0
clck = [0] * (n+1)
anti = [0] * (n+1)
for i in range(1,n+1):
    clck[i] = clck[i-1] + List[i][1] - List[i][0] + List[i-1][0]
    
List[0] = [cir,0]
for i in range(1,n+1):
    anti[i] = anti[i-1] + List[-i][1] + List[-i][0] - List[-i+1][0]

maxv = max(clck + anti)
    
antiMax = [[0,0]] + anti[:]
for i in range(1,n+1):
    if anti[i-1] < anti[i]:
        antiMax[i] = [cir - List[-i][0], anti[i]]
    else:
        antiMax[i] = antiMax[i-1]
        anti[i] = anti[i-1]


for i in range(n+1):
    kcal = clck[i] - antiMax[n-i][0]
    maxv = max(kcal + antiMax[n-i][1], maxv)
    kcal = clck[i] - List[i][0]
    maxv = max(kcal + antiMax[n-i][1], maxv)

print(maxv)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
n, cir = map(int,input().split())
List = [[0,0]] + [list(map(int,input().split())) for i in range(n)]

maxv = 0
clck = [0] * (n+1)
anti = [0] * (n+1)
for i in range(1,n+1):
    clck[i] = clck[i-1] + List[i][1] - List[i][0] + List[i-1][0]
    
List[0] = [cir,0]
for i in range(1,n+1):
    anti[i] = anti[i-1] + List[-i][1] + List[-i][0] - List[-i+1][0]
    
antiMax = [[0,0]] + anti[:]
for i in range(1,n+1):
    if anti[i-1] < anti[i]:
        antiMax[i] = [cir - List[-i][0], anti[i]]
    else:
        antiMax[i] = antiMax[i-1]
        anti[i] = anti[i-1]
        


maxv = max(clck + anti)
for i in range(n+1):
    kcal = clck[i] - antiMax[n-i][0]
    maxv = max(kcal + antiMax[n-i][1], maxv)
    kcal = clck[i] - List[i][0]
    maxv = max(kcal + antiMax[n-i][1], maxv)

print(maxv)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
n, cir = map(int,input().split())
List = [[0,0]] + [list(map(int,input().split())) for i in range(n)]

maxv = 0
clck = [0] * (n+1)
anti = [0] * (n+1)
for i in range(1,n+1):
    clck[i] = clck[i-1] + List[i][1] - List[i][0] + List[i-1][0]
    
List[0] = [cir,0]
for i in range(1,n+1):
    anti[i] = anti[i-1] + List[-i][1] + List[-i][0] - List[-i+1][0]
    
antiMax = [[0,0]] + anti[:]
for i in range(1,n+1):
    if anti[i-1] < anti[i]:
        antiMax[i] = [cir - List[-i][0], anti[i]]
    else:
        antiMax[i] = antiMax[i-1]
        anti[i] = anti[i-1]


maxv = max(clck + anti)
for i in range(1,n+1):
    kcal = clck[i] - antiMax[n-i][0]
    maxv = max(kcal + antiMax[n-i][1], maxv)
    kcal = clck[i] - List[i][0]
    maxv = max(kcal + antiMax[n-i][1], maxv)

print(maxv)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
n, cir = map(int,input().split())
List = [[0,0]] + [list(map(int,input().split())) for i in range(n)]

maxv = 0
clck = [0] * (n+1)
anti = [0] * (n+1)
for i in range(1,n+1):
    clck[i] = clck[i-1] + List[i][1] - List[i][0] + List[i-1][0]
    
List[0] = [cir,0]
for i in range(1,n+1):
    anti[i] = anti[i-1] + List[-i][1] + List[-i][0] - List[-i+1][0]
    
antiMax = [[0,0]] + anti[:]
for i in range(1,n+1):
    if anti[i-1] < anti[i]:
        antiMax[i] = [cir - List[-i][0], anti[i]]
    else:
        antiMax[i] = antiMax[i-1]
        anti[i] = anti[i-1]


maxv = max(clck + anti)
for i in range(1,n+1):
    kcal = clck[i] - antiMax[n-i+1][0]
    maxv = max(kcal + antiMax[n-i][1], maxv)
    kcal = clck[i] - List[i][0]
    maxv = max(kcal + antiMax[n-i][1], maxv)

print(maxv)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
n, cir = map(int,input().split())
List = [[0,0]] + [list(map(int,input().split())) for i in range(n)]

maxv = 0
clck = [0] * (n+1)
anti = [0] * (n+1)
for i in range(1,n+1):
    clck[i] = clck[i-1] + List[i][1] - List[i][0] + List[i-1][0]
    
List[0] = [cir,0]
for i in range(1,n+1):
    anti[i] = anti[i-1] + List[-i][1] + List[-i][0] - List[-i+1][0]
    
antiMax = [[0,0]] + anti[:]
for i in range(1,n+1):
    if anti[i-1] < anti[i]:
        antiMax[i] = [cir - List[-i][0], anti[i]]
    else:
        antiMax[i] = antiMax[i-1]
        anti[i] = anti[i-1]


        
maxv = max(clck + anti)
for i in range(1,n+1):
    kcal = clck[i] - antiMax[n-i+1][0]
    maxv = max(kcal + antiMax[n-i+1][1], maxv)
    kcal = clck[i] - List[i][0]
    maxv = max(kcal + antiMax[n-i+1][1], maxv)

print(maxv)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
n, cir = map(int,input().split())
List = [[0,0]] + [list(map(int,input().split())) for i in range(n)]

maxv = 0
clck = [0] * (n+1)
anti = [0] * (n+1)
for i in range(1,n+1):
    clck[i] = clck[i-1] + List[i][1] - List[i][0] + List[i-1][0]
    
List[0] = [cir,0]
for i in range(1,n+1):
    anti[i] = anti[i-1] + List[-i][1] + List[-i][0] - List[-i+1][0]
    
antiMax = [[0,0]] + anti[:]
for i in range(1,n+1):
    if anti[i-1] < anti[i]:
        antiMax[i] = [cir - List[-i][0], anti[i]]
    else:
        antiMax[i] = antiMax[i-1]
        
maxv = max(clck + anti)
for i in range(1,n+1):
    kcal = clck[i] - antiMax[n-i+1][0]
    maxv = max(kcal + antiMax[n-i+1][1], maxv)
    kcal = clck[i] - List[i][0]
    maxv = max(kcal + antiMax[n-i+1][1], maxv)

print(maxv)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
n, cir = map(int,input().split())
List = [[0,0]] + [list(map(int,input().split())) for i in range(n)]

maxv = 0
clck = [0] * (n+1)
anti = [0] * (n+1)
for i in range(1,n+1):
    clck[i] = clck[i-1] + List[i][1] - List[i][0] + List[i-1][0]
    
List[0] = [cir,0]
for i in range(1,n+1):
    anti[i] = anti[i-1] + List[-i][1] + List[-i][0] - List[-i+1][0]
    
antiMax = [[0,0]] + anti[:]
for i in range(1,n+1):
    if anti[i-1] < anti[i]:
        antiMax[i] = [cir - List[-i][0], anti[i]]
    else:
        antiMax[i] = antiMax[i-1]
        
maxv = max(clck + anti)
for i in range(1,n+1):
    kcal = clck[i] - antiMax[n-i+1][0]
    maxv = max(kcal + antiMax[n-i][1], maxv)
    kcal = clck[i] - List[i][0]
    maxv = max(kcal + antiMax[n-i+1][1], maxv)

print(maxv)

Submission status: Wrong Answer

Solution code:
#coding:utf-8
n, cir = map(int,input().split())
List = [[0,0]] + [list(map(int,input().split())) for i in range(n)]

maxv = 0
clck = [0] * (n+1)
anti = [0] * (n+1)
for i in range(1,n+1):
    clck[i] = clck[i-1] + List[i][1] - List[i][0] + List[i-1][0]
    
List[0] = [cir,0]
for i in range(1,n+1):
    anti[i] = anti[i-1] + List[-i][1] + List[-i][0] - List[-i+1][0]
    
antiMax = [[0,0]] + anti[:]
for i in range(1,n+1):
    if anti[i-1] < anti[i]:
        antiMax[i] = [cir - List[-i][0], anti[i]]
    else:
        antiMax[i] = antiMax[i-1]
        
maxv = max(clck + anti)
for i in range(1,n+1):
    kcal = clck[i] - antiMax[n-i][0]
    maxv = max(kcal + antiMax[n-i][1], maxv)
    kcal = clck[i] - List[i][0]
    maxv = max(kcal + antiMax[n-i][1], maxv)

print(maxv)

Submission status: Wrong Answer

Solution code:
n, c = list(map(int, input().split()))
x = [0]*(n+1)
v = [0]*(n+1)
f = [(0, 0)]*(n+1)
frev = [(0, 0)]*(n+1)
g = [(0, 0)]*(n+1)
grev = [(0, 0)]*(n+1)

for i in range(1, n+1):
  x[i], v[i] = list(map(int, input().split()))

for i in range(1, n+1):
  xt, vt = x[i], v[i]
  f[i] = (xt, f[i-1][1]+vt)
  if g[i-1][0]>f[i-1][1]+vt-xt:
    g[i] = g[i-1]
  else:
    g[i] = (f[i-1][1]+vt-xt, xt)
for i in range(1, n+1):
  xt, vt = x[n-i+1], v[n-i+1]
  frev[i] = (c-xt, frev[i-1][1]+vt)
  if grev[i-1][0]>frev[i-1][1]+vt-(c-xt):
    grev[i] = grev[i-1]
  else:
    grev[i] = (frev[i-1][1]+vt-(c-xt), (c-xt))

ans = 0
for i in range(0, n+1):
  if i==0:
    ans = max(ans, g[n][0])
  else:
    ans = max(ans, g[i][0], grev[i][0], frev[i][1]-frev[i][0]+g[n-i][0]-min(frev[i][0], g[n-i][1]))
print(ans)

Submission status: Wrong Answer

Solution code:
n, c = list(map(int, input().split()))
x = [0]*(n+1)
v = [0]*(n+1)
f = [(0, 0)]*(n+1)
frev = [(0, 0)]*(n+1)
g = [(0, 0)]*(n+1)
grev = [(0, 0)]*(n+1)

for i in range(1, n+1):
  x[i], v[i] = list(map(int, input().split()))

for i in range(1, n+1):
  xt, vt = x[i], v[i]
  f[i] = (xt, f[i-1][1]+vt)
  if g[i-1][0]>f[i-1][1]+vt-xt:
    g[i] = g[i-1]
  else:
    g[i] = (f[i-1][1]+vt-xt, xt)
for i in range(1, n+1):
  xt, vt = x[n-i+1], v[n-i+1]
  frev[i] = (c-xt, frev[i-1][1]+vt)
  if grev[i-1][0]>frev[i-1][1]+vt-(c-xt):
    grev[i] = grev[i-1]
  else:
    grev[i] = (frev[i-1][1]+vt-(c-xt), (c-xt))

ans = 0
for i in range(0, n+1):
  if i==0:
    ans = max(ans, g[n][0])
  else:
    ans = max(ans, frev[i][1]-frev[i][0]+g[n-i][0]-min(frev[i][0], g[n-i][1]))
print(ans)

Submission status: Wrong Answer

Solution code:
n, c = list(map(int, input().split()))
x = [0]*(n+1)
v = [0]*(n+1)
f = [(0, 0)]*(n+1)
g = [(0, 0)]*(n+1)

for i in range(1, n+1):
  xt, vt = list(map(int, input().split()))
  x[i], v[i] = xt, vt
  f[i] = (xt, f[i-1][1]+vt)
  if g[i-1][0]>f[i-1][1]+vt-xt:
    g[i] = g[i-1]
  else:
    g[i] = (f[i-1][1]+vt-xt, xt)

ans = 0
for i in range(0, n+1):
  if i==0:
    ans = max(ans, g[n][0])
  else:
    ans = max(ans, sum(v[i:])-(c-x[i])+g[i-1][0]-min(g[i-1][1], c-x[i]), sum(v[i:])-(c-x[i]))
print(ans)

Submission status: Wrong Answer

Solution code:
from copy import deepcopy
def UPP(A,n):
    A.insert(0,[n,0])
    B = [0]
    for i in range(len(A)-1):
        B.append(A[i+1][1] + B[i] - A[i+1][0] + A[i][0])
#    print(B)
    return B

def PLUS(A,c):
    for i in range(len(A)-1):
        A[i][0] += c
    return A

N, C = map(int, input().split())
X = [list(map(int, input().split())) for i in range(N)]
RX = deepcopy(X)
RX = RX[::-1]
for i in range(N):
    RX[i][0] = C - RX[i][0]

ans = 0
for i in range(1,N):
    RXP = PLUS(A = RX, c = X[i-1][0])
    RXP = RXP[:N - i]
    ans = max(ans, UPP(A = X[:i], n = 0)[i] + max(UPP(A = RXP, n = 0)))

for i in range(1,N):
    XP = PLUS(A = X, c = RX[i-1][0])
    XP = XP[:N - i]
    ans = max(ans, UPP(A = RX[:i], n = 0)[i] + max(UPP(A = XP, n = 0)))

ans = max(ans,max(UPP(A = X, n = 0)),max(UPP(A = RX, n = 0)))

print(ans)

Submission status: Wrong Answer

Solution code:
n, c = map(int, input().split())

x = [0 for _ in range(n + 2)]
v = [0 for _ in range(n + 1)]
for i in range(n):
    x[i + 1], v[i + 1] = map(int, input().split())

x[n + 1] = c

left = [0 for _ in range(n + 1)]
right = [0 for _ in range(n + 1)]
left_rev = [0 for _ in range(n + 1)]
right_rev = [0 for _ in range(n + 1)]

for i in range(1, n):
    left[i] = left[i - 1] + v[i] - (x[i] - x[i - 1])
    left_rev[i] = left[i] - x[i]
    right[i] = right[i - 1] + v[n - i + 1] - (x[n - i + 2] - x[n - i + 1])
    right_rev[i] = right[i] - (c - x[n - i + 1])

result = 0
for i in range(n):
    temp = max( (left[i] + max(right_rev[:n - i + 1])) , (right[i] + max(left_rev[:n - i + 1])) )
    if temp > result:
        result = temp

print(result)

Submission status: Wrong Answer

Solution code:
n, c = map(int, input().split())

x = [0 for _ in range(n + 2)]
v = [0 for _ in range(n + 1)]
for i in range(n):
    x[i + 1], v[i + 1] = map(int, input().split())

x[n + 1] = c

left = [0 for _ in range(n + 1)]
right = [0 for _ in range(n + 1)]
left_rev = [0 for _ in range(n + 1)]
right_rev = [0 for _ in range(n + 1)]

for i in range(1, n):
    left[i] = left[i - 1] + v[i] - (x[i] - x[i - 1])
    left_rev[i] = left[i] - x[i]
    right[i] = right[i - 1] + v[n - i + 1] - (x[n - i + 2] - x[n - i + 1])
    right_rev[i] = right[i] - (c - x[n - i + 1])

result = 0
for i in range(n):
    temp = max( (left[i] + max(right_rev[:n - i])) , (right[i] + max(left_rev[:n - i])) )
    if temp > result:
        result = temp

print(result)

Submission status: Wrong Answer

Solution code:
N,C=map(int,input().split())
sushi = [list(map(int,input().split())) for _ in range(N)]
vs = [0]*(N+1)
vs[1] = sushi[0][1] - 2*sushi[0][0]
result = 0
ind = 0
for i in range(2,N):
  vs[i] = vs[i-1] + 2*sushi[i-2][0] + sushi[i-1][1] - 2*sushi[i-1][0]
  if result < vs[i]:
    result = vs[i]
    ind = sushi[i-1][0]
tmp = 0
result = result + ind
for x in range(1,N):
  tmp += sushi[-x][1] - (C - sushi[-x][0])
  if 1 < x:
    tmp += (C - sushi[-x+1][0])
  result = max(result,tmp+max(vs[:-x]))
print(result)

Submission status: Wrong Answer

Solution code:
n, c = list(map(int, input().split()))
sushi = [list(map(int, input().split())) for i in range(n)]
s = sorted(sushi, key=lambda x: x[0])

r = [0]
r_return = [0]
cal_r = 0

l = [0]
l_return = [0]
cal_l = 0

for i in range(n):
    cal_r += s[i][1]
    eff_cal_r = cal_r - s[i][0]
    eff_cal_r_return = cal_r - 2*s[i][0]
    r += [max(r[i], eff_cal_r)]
    r_return += [max(r_return[i], eff_cal_r_return)]

    cal_l += s[-i-1][1]
    eff_cal_l = cal_l - (c-s[-i-1][0])
    eff_cal_l_return = cal_l - 2*(c-s[-i-1][0])
    l += [max(r[i], eff_cal_l)]
    l_return += [max(l_return[i], eff_cal_l_return)]

cal = 0

for i in range(n+1):
    cal = max(
            cal,
            r[i],
            l[i],
            r_return[i] + l[n-i],
            l_return[i] + r[n-i]
            )

print(cal)

Submission status: Wrong Answer

Solution code:
n, c = list(map(int, input().split()))
sushi = [list(map(int, input().split())) for i in range(n)]
s = sorted(sushi, key=lambda x: x[0])

r = [0]
r_return = [0]
cal_r = 0

l = [0]
l_return = [0]
cal_l = 0

for i in range(n):
    cal_r += s[i][1]
    eff_cal_r = cal_r - s[i][0]
    eff_cal_r_return = cal_r - 2*s[i][0]
    r += [max(r[i], eff_cal_r)]
    r_return += [max(r_return[i], eff_cal_r_return)]

    cal_l += s[-i-1][1]
    eff_cal_l = cal_l - (c-s[-i-1][0])
    eff_cal_l_return = cal_l - 2*(c-s[-i-1][0])
    l += [max(r[i], eff_cal_l)]
    l_return += [max(l_return[i], eff_cal_l_return)]

cal = 0

for i in range(n):
    cal = max(
            cal,
            r[i],
            l[i],
            r_return[i] + l[n-i],
            l_return[i] + r[n-i]
            )

print(cal)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

v_1 = [sum(v[:i]) for i in range(1, N+1)]
v_n = [sum(v[i:]) for i in range(N)]
f = np.array([v_1[i] - x[i] for i in range(N)])
g = [np.argmax(f[:i+1]) for i in range(N)]
c = 0
for i in range(N-1, 0, -1):
    index = g[i-1]
    c_t = v_n[i] + f[index] - (C - x[i])
    c_t = max(f[index], c_t - x[index], c_t - (C - x[i]))
    if c_t > c:
        c = c_t
c_t = v_n[0] - (C - x[0])
if c_t > c:
    c = c_t
print(c)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

v_1 = [sum(v[:i]) for i in range(1, N+1)]
v_n = [sum(v[i:]) for i in range(N)]
f = np.array([v_1[i] - x[i] for i in range(N)])
g = [np.argmax(f[:i+1]) for i in range(N)]
c = 0
for i in range(N-1, 0, -1):
    index = g[i-1]
    c_t = v_n[i] + f[index] - (C - x[i])
    c_t = max(f[index], max(c_t - x[index], c_t - (C - x[i])))
    if c_t > c:
        c = c_t
print(c)

Submission status: Wrong Answer

Solution code:
N, C = map(int, input().split())
x = [0]
v = [0]
for _ in range(N):
    x_new, v_new = map(int, input().split())
    x.append(x_new)
    v.append(v_new)


def get_g(clockwise):
    g = []
    g_now = 0
    g.append(g_now)
    if clockwise:
        for i in range(1, N + 1):
            g_next = g_now + v[i] + x[i - 1] - x[i]
            g.append(g_next)
            g_now = g_next
    else:
        for i in range(1, N + 1):
            g_next = g_now + v[i] + 2 * x[i - 1] - 2 * x[i]
            g.append(g_next)
            g_now = g_next
    return g


def get_h(clockwise):
    h = []
    if clockwise:
        h_now = v[N] - 2 * (C - x[N])
        h.append(h_now)
        for i in range(N):
            h_next = v[N - i - 1] + h_now + 2 * (x[N - i - 1] - x[N - i])
            h.append(h_next)
            h_now = h_next
    else:
        h_now = v[N] - C + x[N]
        h.append(h_now)
        for i in range(N):
            h_next = v[N - i - 1] + h_now + x[N - i - 1] - x[N - i]
            h.append(h_next)
            h_now = h_next
    h = list(reversed(h))
    return [0] + h[1:]


def get_G(g):
    G = []
    max_g = -100000000
    for i in range(N + 1):
        max_g = max(max_g, g[i])
        G.append(max_g)
    return [G[-1]] + G[:-1]


answer = 0
for clockwise in [True, False]:
    g = get_g(clockwise)
    h = get_h(clockwise)
    G = get_G(g)
    print("g:{}".format(g))
    print("G:{}".format(G))
    print("h:{}".format(h))
    answer = max(max([G[b] + h[b] for b in range(N + 1)]), answer)

print(answer)

Submission status: Wrong Answer

Solution code:
N, C = map(int, input().split())
x = [0]
v = [0]
for _ in range(N):
    x_new, v_new = map(int, input().split())
    x.append(x_new)
    v.append(v_new)


def get_g(clockwise):
    g = []
    g_now = 0
    g.append(g_now)
    if clockwise:
        for i in range(1, N + 1):
            g_next = g_now + v[i] + x[i - 1] - x[i]
            g.append(g_next)
            g_now = g_next
    else:
        for i in range(1, N + 1):
            g_next = g_now + v[i] + 2 * x[i - 1] - 2 * x[i]
            g.append(g_next)
            g_now = g_next
    return g


def get_h(clockwise):
    h = []
    if clockwise:
        h_now = v[N] - 2 * (C - x[N])
        h.append(h_now)
        for i in range(N):
            h_next = v[N - i - 1] + h_now + 2 * (x[N - i - 1] - x[N - i])
            h.append(h_next)
            h_now = h_next
    else:
        h_now = v[N] - C + x[N]
        h.append(h_now)
        for i in range(N):
            h_next = v[N - i - 1] + h_now + x[N - i - 1] - x[N - i]
            h.append(h_next)
            h_now = h_next
    return list(reversed(h))


def get_G(g):
    G = []
    max_g = -100000000
    for i in range(N + 1):
        max_g = max(max_g, g[i])
        G.append(max_g)
    return [0] + G[:-1]


answer = 0
for clockwise in [True, False]:
    g = get_g(clockwise)
    h = get_h(clockwise)
    G = get_G(g)
    # print("g:{}".format(g))
    # print("G:{}".format(G))
    # print("h:{}".format(h))
    answer = max(max([G[b] + h[b] for b in range(N + 1)]), answer)

print(answer)

Submission status: Wrong Answer

Solution code:
N,C=map(int, raw_input().split())
x = []
v = []
for i in range(N):
    a,b = map(int, raw_input().split())
    x.append(a)
    v.append(b)

now_pos = 0
is_sushi = []
for i in range(N):
    is_sushi.append(True)

flg = True
cal = 0
while(flg):
    get_sushi = -1
    nearest = 1e18
    for i in range(N):
        distance = min(abs(x[i] - now_pos), C - x[i] + now_pos)
        value = v[i] - distance
        if value > 0 and distance < nearest and is_sushi[i] == True:
            nearest = distance
            get_sushi = i
    if get_sushi == -1:
        break;

    now_pos = x[get_sushi]
    cal += v[get_sushi] - nearest
    is_sushi[get_sushi] = False

print(cal)