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.

Solution (Python):
from itertools import accumulate, chain

n, c = map(int, input().split())
xs, vs = zip(*(map(int, input().split()) for _ in range(n)))

ds = [j - i for i, j in zip(chain([0], xs), chain(xs, [c]))]

vs_running_clock = list(accumulate(vs))
ds_running_clock = list(accumulate(ds))
vs_running_antic = list(accumulate(vs[::-1]))
ds_running_antic = list(accumulate(ds[::-1]))

clockwise = chain([0], (v - d for v, d in zip(vs_running_clock, ds_running_clock)))
anticlockwise2 = [0] + [v - d*2 for v, d in zip(vs_running_antic, ds_running_antic)]
anticlock_clock = (x + y for x, y in zip(accumulate(clockwise, func=max), reversed(anticlockwise2)))

anticlockwise = chain([0], (v - d for v, d in zip(vs_running_antic, ds_running_antic)))
clockwise2 = [0] + [v - d*2 for v, d in zip(vs_running_clock, ds_running_clock)]
clock_anticlock = (x + y for x, y in zip(accumulate(anticlockwise, func=max), reversed(clockwise2)))

print(max(chain(anticlock_clock, clock_anticlock)))

Solution (Python):
from itertools import accumulate, chain

n, c = map(int, input().split())
xs, vs = zip(*(map(int, input().split()) for _ in range(n)))

ds = [j - i for i, j in zip(chain([0], xs), chain(xs, [c]))]

clockwise = chain([0], (v - d for v, d in zip(accumulate(vs), accumulate(ds))))
anticlockwise2 = [0] + [v - d*2 for v, d in zip(accumulate(vs[::-1]), accumulate(ds[::-1]))]
anticlock_clock = (x + y for x, y in zip(accumulate(clockwise, func=max), reversed(anticlockwise2)))

anticlockwise = chain([0], (v - d for v, d in zip(accumulate(vs[::-1]), accumulate(ds[::-1]))))
clockwise2 = [0] + [v - d*2 for v, d in zip(accumulate(vs), accumulate(ds))]
clock_anticlock = (x + y for x, y in zip(accumulate(anticlockwise, func=max), reversed(clockwise2)))

print(max(chain(anticlock_clock, clock_anticlock)))

Solution (Python):
from itertools import accumulate

n, c = map(int, input().split())
xs, vs = zip(*(map(int, input().split()) for _ in range(n)))

xs = (0,) + xs + (c,)
ds = [j - i for i, j in zip(xs, xs[1:])]

clockwise = [0] + [v - d for v, d in zip(accumulate(vs), accumulate(ds))]
clockwise_maxs = list(accumulate(clockwise, func=max))
clock_max = clockwise_maxs[-1]

anticlockwise = [0] + [v - d for v, d in zip(accumulate(vs[::-1]), accumulate(ds[::-1]))]
anticlockwise_maxs = list(accumulate(anticlockwise, func=max))
anticlock_max = anticlockwise_maxs[-1]

clockwise2 = [0] + [v - d*2 for v, d in zip(accumulate(vs), accumulate(ds))]
clock_anticlock = [x + y for x, y in zip(clockwise2, reversed(anticlockwise_maxs))]
c_ac_max = max(clock_anticlock)

anticlockwise2 = [0] + [v - d*2 for v, d in zip(accumulate(vs[::-1]), accumulate(ds[::-1]))]
anticlock_clock = [x + y for x, y in zip(anticlockwise2, reversed(clockwise_maxs))]
ac_c_max = max(anticlock_clock)

print(max(clock_max, anticlock_max, c_ac_max, ac_c_max))

Solution (Python):
import numpy as np

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



m = s = 0
Left = [0] * (N+2)  
for i in range(1, N+2):
    s += -(X[i] - X[i-1]) + V[i]
    m = max(m, s)
    Left[i] = m

m = s = 0
Right = [0] * (N+2)
for i in range(N, -1, -1):
    s += -(X[i+1] - X[i]) + V[i]
    m = max(m, s)
    Right[i] = m

m = s = 0
LeftSub = [0] * (N+2)
for i in range(N, -1, -1):
    s += -2 * (X[i+1] - X[i]) + V[i]
    m = max(m, s)
    LeftSub[i] = m

m = s = 0
RightSub = [0] * (N+2)
for i in range(1, N+2):
    s += -2 * (X[i] - X[i-1]) + V[i]
    m = max(m, s)
    RightSub[i] = m

LeftTotal = np.array(Left[:-1]) + np.array(LeftSub[1:])
RightTotal = np.array(Right[1:]) + np.array(RightSub[:-1])
m = max(LeftTotal.max(), RightTotal.max())

print(m)

Solution (Python):
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(l[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_return[i] + l[n-i],
            l_return[i] + r[n-i]
            )

print(cal)

Solution (Python):
N, C = map(int, input().split())
x = [0]
v = [0]
for i in range(N):
    x_i, v_i = map(int, input().split())
    x.append(x_i)
    v.append(v_i)
fw = [0] * (N+1)
bw = [0] * (N+1)
for i in range(N):
    fw[i+1] = fw[i] + v[i+1]
    bw[i+1] = bw[i] + v[N-i]

for i in range(N):
    fw[i+1] -= x[i+1]
    bw[i+1] -= C - x[N-i]
m_fw = [0] * (N+1)
m_bw = [0] * (N+1)
for i in range(N):
    m_fw[i+1] = max(m_fw[i], fw[i+1])
    m_bw[i+1] = max(m_bw[i], bw[i+1])
c = 0
for i in range(N+1):
    if i == 0:
        l = 0
    else:
        l = C - x[-i]
    c = max(c, bw[i]+m_fw[N-i]-l, fw[i]+m_bw[N-i]-x[i])
print(c)

Solution (Python):
import sys
N, C = map(int, input().split())
x = [None]*N
v = [None]*N
for i in range(N):
    x[i], v[i] = map(int, input().split())
cw = [0]*(N+1)
ccw = [0]*(N+1)
for i in range(N):
    cw[i+1] = cw[i] + v[i]
    ccw[i+1] = ccw[i] + v[N-1-i]
for i in range(1, N+1):
    cw[i] -= x[i-1]
    ccw[i] -= (C - x[N-i])
maxccw = [0]*(N+1)
maxcw = [0]*(N+1)
for i in range(1, N+1):
    maxccw[i] = max(maxccw[i-1], ccw[i])
    maxcw[i] = max(maxcw[i-1], cw[i])
ans = max(0, maxcw[-1], maxccw[-1])
for i in range(1, N+1):
    ans = max(ans, cw[i] - x[i-1] + maxccw[N-i])
    ans = max(ans, ccw[i] - C + x[N-i] + maxcw[N-i])
print(ans)

Solution (Python):
n, c = map(int, input().split())
x1 = [0]
x2 = []
v1 = [0]
v2 = [0]
for _ in range(n):
    x, v = map(int, input().split())
    x1.append(x)
    x2.append(c-x)
    v1.append(v)
    v2.append(v)
x2.append(0)
x2.reverse()
v2.append(0)
v2.reverse()
v2 = v2[:-1]
for i in range(n):
    v1[i+1] += v1[i] 
    v2[i+1] += v2[i] 

max11, max12 = [0], [0]
for i in range(1, n+1):
    max11.append(max(max11[-1], v1[i]-x1[i]))
    max12.append(max(max12[-1], v1[i]-2*x1[i]))
max21, max22 = [0], [0]
for i in range(1, n+1):
    max21.append(max(max21[-1], v2[i]-x2[i]))
    max22.append(max(max22[-1], v2[i]-2*x2[i]))

ans = max(max(max11), max(max21))
for i in range(n+1):
    ans = max(ans, max11[i]+max22[n-i], max21[i]+max12[n-i])

print(ans)

Solution (Python):


n, c = map(int, input().split())
x = [0]
v = [0]
ca = [0]
csum = 0
dmaxa1 = [0]
dmaxa2 = [0]
dmaxa1i = 0
dmaxa2i = 0
for i in range(n):
    xi, vi = map(int, input().split())
    x.append(xi)
    v.append(vi)
    csum += vi
    ca.append(csum)
    dmaxa1i = max(dmaxa1i, csum - xi)
    dmaxa2i = max(dmaxa2i, csum - xi * 2)
    dmaxa1.append(dmaxa1i)
    dmaxa2.append(dmaxa2i)
x.append(c)

cb = [0]
for i in range(n):
    csum -= v[i]
    cb.append(csum)
cb.append(0)

gainMax = 0
for b in range(1, n + 2):
    gain = cb[b]
    loss1 = (c - x[b]) * 2
    loss2 = c - x[b]
    d1 = dmaxa1[b - 1] + gain - loss1
    d2 = dmaxa2[b - 1] + gain - loss2
    gainMax = max(gainMax, max(d1, d2))

print(gainMax)

Solution (Python):
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)
    answer = max(max([G[b] + h[b] for b in range(N + 1)]), answer)

print(answer)

Solution (Python):
N, C = map(int, input().split())
X, V = [], []
for i in range(N):
  x, v = map(int, input().split())
  X+=[x]
  V+=[v]

ans = 0
doublepath=[0]
w=0
for i in range(N):
  w+=V[i]
  doublepath+=[-2*X[i]+w]
maxdoublepath=[0]
for i in range(N):
  maxdoublepath+=[max(maxdoublepath[-1],doublepath[i+1])]

ans = 0
w=0
for i in range(N):
  w+= V[N-1-i]
  ans = max(ans,(w-(C-X[N-1-i])) + maxdoublepath[N-1-i])

doublepath=[0]
w=0
for i in range(N):
  w+=V[N-1-i]
  doublepath+=[-2*(C-X[N-1-i])+w]
maxdoublepath=[0]
for i in range(N):
  maxdoublepath+=[max(maxdoublepath[-1],doublepath[i+1])]

w=0
for i in range(N):
  w+= V[i]
  ans = max(ans,(w-X[i]) + maxdoublepath[N-1-i])

print(ans)

Solution (Python):
n,c=map(int,input().split())
xv=[]
for _ in range(n):

    xv.append(tuple(map(int,input().split())))

a=[0]*n
a[0]=xv[0][1]-xv[0][0]
for i in range(1,n):
    a[i]=a[i-1]+xv[i][1]-xv[i][0]+xv[i-1][0]


b=[0]*n
b[n-1]=xv[n-1][1]-(c-xv[n-1][0])
for i in range(n-2,-1,-1):
    b[i]=b[i+1]+xv[i][1]-(c-xv[i][0])+(c-xv[i+1][0])

amax=[0]*n
bmax=[0]*n
amax[0],bmax[n-1]=max(0,a[0]),max(0,b[n-1])
for i in range(1,n):
    amax[i]=max(a[i],amax[i-1])
for i in range(n-2,-1,-1):
    bmax[i]=max(b[i],bmax[i+1])

ans=0
for i in range(n):
    if i==n-1:
        ans=max(ans,amax[i])
    else:
        ans=max(ans,amax[i]+bmax[i+1]-xv[i][0])

for i in range(n-1,-1,-1):
    if i==0:
        ans=max(ans,bmax[i])
    else:
        ans=max(ans,amax[i-1]+bmax[i]-(c-xv[i][0]))
print(ans)

Solution (Python):
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))

Solution (Python):
import numpy as np
N, C = map(int, input().split())
X,V = [],[]
for _ in range(N):
    x,v = map(int, input().split())
    X.append(x)
    V.append(v)

r1,r2,l1,l2 = [0],[0],[0],[0]
sum = 0

for i in range(N):
    sum += V[i]
    
    r1.append(max(r1[i], sum-X[i]))
    r2.append(max(r2[i], sum-2*X[i]))
sum = 0
for i in range(N,0,-1):
    
    sum += V[i-1]
    
    l1.append(max(l1[-1], sum-(C-X[i-1])))
    l2.append(max(l2[-1], sum-2*(C-X[i-1])))

a = max(np.array(r1) + np.array(l2)[::-1])
b = max(np.array(r2) + np.array(l1)[::-1])
print(max(a,b))

Solution (Python):
N, C = map(int, input().split())
X = {}
V = {}
for i in range(N):
    X[i], V[i] = map(int, input().split())

left, right = [], []
left.append(V[0] - X[0])
right.append(V[N-1] - (C - X[N-1]))
for i in range(1, N):
    left.append(left[i-1] + V[i] - X[i] + X[i-1])
    right.append(right[i-1] + V[N-i-1] - (C - X[N-i-1]) + (C - X[N-i]))

left_max = [0]
for i in range(N):
    if left[i] > left_max[i]:
        left_max.append(left[i])
    else:
        left_max.append(left_max[i])
right_max = [0]
for i in range(N):
    if right[i] > right_max[i]:
        right_max.append(right[i])
    else:
        right_max.append(right_max[i])
result = set()
for i in range(N):
    result.add(left[i]-X[i]+right_max[N-1-i])
for i in range(N):
    result.add(right[i]-(C - X[N-i-1])+left_max[N-1-i])
result = max(max(left_max), max(right_max), max(result))
print(result)

Solution (Python):

from numpy import array as n

N, C = map(int, input().split())
xy = [list(map(int, input().split())) for _ in range(N)]
r1, r2, l1, l2 = [0],[0],[0],[0]
sum = 0
for x, cal in xy:
    sum += cal
    r1.append(max(r1[-1], sum-x))
    r2.append(max(r2[-1], sum-2*x))
sum = 0
for x, cal in xy[::-1]:
    sum += cal
    l1.append(max(l1[-1], sum-C+x))
    l2.append(max(l2[-1], sum-2*(C-x)))
a = max(n(r1)+n(l2[::-1]))
b = max(n(r2)+n(l1[::-1]))
print(max(a,b))

Solution (Python):
n, c = input().split()
n = int(n)
c = int(c)
x = [0]
v = [0]
gs1 = [0]
gs2 = [0]
gs3 = [0]
gs4 = [0]
s = 0
for i in range(n):
    xx, vv = input().split()
    xx = int(xx)
    vv = int(vv)
    x.append(xx)
    v.append(vv)
    s += vv
    f = s - xx
    gs1.append(max(gs1[-1], f))
    f = s - 2 * xx
    gs2.append(max(gs2[-1], f))
s = 0
for i in range(-1, -n - 1, -1):
    s += v[i]
    f = s - (c - x[i])
    gs3.append(max(gs3[-1], f))
    f = s - 2 * (c - x[i])
    gs4.append(max(gs4[-1], f))
ans = 0
for i in range(n + 1):
    ans = max(ans, gs1[i] + gs4[n - i], gs2[i] + gs3[n - i])
print(ans)

Solution (Python):
import numpy as np

n, c = list(map(int, input().split(' ')))
x, v = [], []

for i in range(n):
  x_, v_ = list(map(int, input().split(' ')))
  x.append(x_)
  v.append(v_)
x = np.array(x)
v = np.array(v)
sum_v = np.cumsum(v)
inv_x = c - x[::-1]
inv_v = v[::-1]
sum_inv_v = np.cumsum(inv_v)

cal = sum_v - x
inv_cal = sum_inv_v - inv_x

cal_cummax = np.maximum.accumulate(cal)
inv_cal_cummax = np.maximum.accumulate(inv_cal)

fr_double_cal = sum_v - 2 * x + \
    np.insert(inv_cal_cummax[:-1], 0, 0)[::-1]
fl_double_cal = sum_inv_v - 2 * inv_x + \
    np.insert(cal_cummax[:-1], 0, 0)[::-1]

print(np.max(
    [0, cal.max(), inv_cal.max(), fr_double_cal.max(), fl_double_cal.max()]))

Solution (Python):
n,c = map(int, input().split())
x = []
x_r = []
v = []
for i in range(n):
    xi,vi = map(int, input().split())
    x.append(xi)
    x_r.append(c - xi)
    v.append(vi)
x_r.reverse()
x.insert(0, 0)
x_r.insert(0, 0)

score = [0]
score_r = [0]
tmp = 0
for i, vi in enumerate(v):
    tmp += vi
    score.append(tmp - x[i+1])
tmp = 0
for i, vi in enumerate(v[::-1]):
    tmp += vi
    score_r.append(tmp - x_r[i+1])

maxscore = []
maxscore_r = []
tmp = 0
for i, si in enumerate(score):
    tmp = max(tmp, si)
    maxscore.append(tmp)
tmp = 0
for i, si in enumerate(score_r):
    tmp = max(tmp, si)
    maxscore_r.append(tmp)

ans = 0
for i, si in enumerate(score):
    ans = max(si + maxscore_r[n-i] - x[i], ans)
for i, si in enumerate(score_r):
    ans = max(si + maxscore[n-i] - x_r[i], ans)

print(ans)

Solution (Python):


n,c=map(int,raw_input().split())
temp=[0 for i in xrange(n+1)]
x=[]
v=[]
for i in xrange(n):
 xi,vi=map(int,raw_input().split())
 x.append(xi)
 v.append(vi)
xr=[]
vr=[]
for i in xrange(n):
 xr.append(c-x[n-i-1])
 vr.append(v[n-i-1])
s=v[0]
sr=vr[0]
w,w2=[max(s-x[0],0)],[max(s-x[0]*2,0)]
wr,w2r=[max(sr-xr[0],0)],[max(sr-xr[0]*2,0)]
for i in xrange(1,n):
 s+=v[i]
 sr+=vr[i]
 w.append(max(s-x[i],w[i-1]))
 w2.append(max(s-x[i]*2,w2[i-1]))
 wr.append(max(sr-xr[i],wr[i-1]))
 w2r.append(max(sr-xr[i]*2,w2r[i-1]))
res=0
for i in xrange(n-1):
 res=max(res,w[i]+w2r[n-i-2])
 res=max(res,wr[i]+w2[n-i-2])
res=max(res,max(w))
res=max(res,max(wr))
print res

Solution (Python):
N, C = map(long, raw_input().split())
Sushi_clock  = [map(long, raw_input().split()) for i in range(N)]
Sushi_anticlock = [[C-x[0], x[1]] for x in Sushi_clock[::-1]]
Sushi_clock.insert(0, [0L, 0L])
Sushi_anticlock.insert(0, [0L, 0L])
N += 1
ans = 0
eatcal = 0
getcal_clock_max = 0
getcal_clock_max_oa = 0
getcal_clock_maxes = []
getcal_clock2_max = 0
getcal_clock2_max_oa = 0
getcal_clock2_maxes = []
for i in Sushi_clock:
  eatcal += i[1]
  getcal = eatcal-i[0]
  getcal_clock_max_oa = i[0] if getcal > getcal_clock_max else getcal_clock_max_oa
  getcal_clock_max = getcal if getcal > getcal_clock_max else getcal_clock_max
  getcal_clock_maxes.append([getcal_clock_max,getcal_clock_max_oa])
  getcal2 = eatcal-i[0]*2
  getcal_clock2_max_oa = i[0] if getcal2 > getcal_clock2_max else getcal_clock2_max_oa
  getcal_clock2_max = getcal2 if getcal2 > getcal_clock2_max else getcal_clock2_max
  getcal_clock2_maxes.append([getcal_clock2_max,getcal_clock2_max_oa])
eatcal = 0
for anti, oa, oa2 in zip(Sushi_anticlock, getcal_clock_maxes[::-1], getcal_clock2_maxes[::-1]):
  eatcal += anti[1]
  getcal_oa2 = eatcal-anti[0]+oa2[0]
  getcal_ob2 = eatcal-anti[0]*2+oa[0]
  ans = max(ans, getcal_oa2, getcal_ob2)
print ans

Solution (Python):
n,c=map(int, input().split())
x=[]
xr=[]
v=[]

for _ in range(n):
    xi,vi=map(int, input().split())
    x.append(xi)
    xr.append(c-xi)
    v.append(vi)
xr.reverse()
x.insert(0,0)
xr.insert(0,0)

score=[0]
score_r=[0]

tmp=0
for i, vi in enumerate(v):
    tmp+=vi
    score.append(tmp-x[i+1])
tmp=0
for i, vi in enumerate(v[::-1]):
    tmp+=vi
    score_r.append(tmp-xr[i+1])

maxscore=[0]
maxscore_r=[0]

tmp=0
for si in score:
    maxscore.append(max(si,tmp))
    tmp=max(si,tmp)
tmp=0
for si in score_r:
    maxscore_r.append(max(si,tmp))
    tmp=max(si,tmp)

ans=0
for i, si in enumerate(score):
    tmp=si-x[i]+maxscore_r[n-i+1]
    ans=max(tmp,ans)
for i, si in enumerate(score_r):
    tmp=si-xr[i]+maxscore[n-i+1]
    ans=max(tmp,ans)
print(ans)

Solution (Python):
N, C = map(int, input().split())
sushi_list = []
for _ in range(N):
    sushi_list.append(tuple(map(int, input().split())))

clock = [0]
clock_double = [0]

cal = 0
for idx, sushi in enumerate(sushi_list, 1):
    cal += sushi[1]
    clock.append(max(clock[idx - 1], cal - sushi[0]))
    clock_double.append(max(clock_double[idx - 1], cal - sushi[0] * 2))

cal = 0
rev_clock = [0]
rev_clock_double = [0]
for idx, sushi in enumerate(sushi_list[::-1], 1):
    cal += sushi[1]
    rev_clock.append(max(rev_clock[idx - 1], cal - (C - sushi[0])))
    rev_clock_double.append(max(rev_clock_double[idx - 1], cal - (C - sushi[0]) * 2))

max_cal = 0
for i in range(N + 1):
    c = clock_double[i] + rev_clock[N - i]
    r_c = rev_clock_double[i] + clock[N - i]
    max_cal = max(max_cal, c, r_c)
print(max_cal)

Solution (Python):
N, C = map(int, input().split())
x, v = [0]*N, [0]*N
for n in range(N):
    x[n], v[n] = map(int, input().split())
R = [v[0] - x[0]] + [0]*(N-1)
L = [v[-1] - (C - x[-1])] + [0]*(N-1)
for i in range(N-1):
    R[i+1] = R[i] + v[i+1] - (x[i+1] - x[i])
    L[i+1] = L[i] + v[-i-2] - (x[-i-1] - x[-i-2])
Rmax = [R[0]]*N
Lmax = [L[0]]*N
for i in range(N-1):
    Rmax[i+1] = max(Rmax[i], R[i+1])
    Lmax[i+1] = max(Lmax[i], L[i+1])
RL = Rmax[-1]
LR = Lmax[-1]
for i in range(N-1):
    RL = max(RL, R[-i-2] - x[-i-2] + Lmax[i])
    LR = max(LR, L[-i-2] - (C - x[i+1]) + Rmax[i])
print(max(0, RL, LR))

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

R = [v[0] - x[0]] + [0] * (N - 1)
L = [0] * (N - 1) + [v[-1] - (C - x[-1])]
for i in range(1, N):
    R[i] = R[i - 1] + v[i] - (x[i] - x[i - 1])
    L[-1-i] = L[-i] + v[-1-i] - (x[-i] - x[-1-i])

Rmax = [R[0]] + [0] * (N - 1)
Lmax = [0] * (N - 1) + [L[-1]]
for i in range(1, N):
    Rmax[i] = max(Rmax[i-1], R[i])
    Lmax[-1-i] = max(Lmax[-i], L[-1-i])

R2L = Rmax[-1]
L2R = Lmax[0]
for i in range(N-1):
    R2L = max(R2L, R[i] - x[i] + Lmax[i+1])
    L2R = max(L2R, L[-1-i] - (C - x[-1-i]) + Rmax[-1-i-1])

print(max(0, R2L, L2R))

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

R = [0] * N
L = [0] * N
for i in range(N):
    if i == 0:
        R[i] = v[i] - x[i]
        L[N-1-i] = v[N-1-i] - (C - x[N-1-i])
    else:
        R[i] = R[i - 1] + v[i] - (x[i] - x[i - 1])
        L[N-1-i] = L[N-i] + v[N-1-i] - (x[N-i] - x[N-1-i])

Rmax = [0] * N
Lmax = [0] * N
for i in range(N):
    if i == 0:
        Rmax[i] = R[i]
        Lmax[N-1-i] = L[N-1-i]
    else:
        Rmax[i] = max(Rmax[i-1], R[i])
        Lmax[N-1-i] = max(Lmax[N-i], L[N-1-i])

R2L = 0
L2R = 0
for i in range(N-1):
    R2L = max(R2L, R[i] - x[i] + Lmax[i+1])
    L2R = max(L2R, L[i+1] - (C - x[i+1]) + Rmax[i])

print(max(0, max(R), max(L), R2L, L2R))

Solution (Python):
n, c = map(int,input().split())
x = []
v = []
for i in range(n):
    s, t = input().split()
    x.append(int(s))
    v.append(int(t))

vl1 = [0]*(n+1)
vl2 = [0]*(n+1)
suml = 0
vr1 = [0]*(n+1)
vr2 = [0]*(n+1)
sumr = 0
for i in range(n):
    suml += v[i]
    vl1[i+1] = vl1[i] if vl1[i] > suml - x[i] else suml - x[i]
    vl2[i+1] = vl2[i] if vl2[i] > suml - x[i]*2 else suml - x[i]*2
    sumr += v[n-1-i]
    vr1[i+1] = vr1[i] if vr1[i] > sumr - c + x[n-1-i] else sumr - c + x[n-1-i]
    vr2[i+1] = vr2[i] if vr2[i] > sumr - (c - x[n-1-i])*2 else sumr - (c - x[n-1-i])*2
print(max( [max(vl1[i]+vr2[n-i],vr1[i]+vl2[n-i]) for i in range(n+1)] ))

Solution (Python):
n, c = map(int,input().split())
x = []
v = []
for i in range(n):
    s, t = input().split()
    x.append(int(s))
    v.append(int(t))
vl1 = [0]
vl2 = [0]
suml = 0
vr1 = [0]
vr2 = [0]
sumr = 0
for i in range(n):
    suml += v[i]
    
    vl1.append(vl1[i] if vl1[i] > suml - x[i] else suml - x[i])
    vl2.append(vl2[i] if vl2[i] > suml - x[i]*2 else suml - x[i]*2)
    
    
    sumr += v[n-1-i]
    vr1.append(vr1[i] if vr1[i] > sumr - c + x[n-1-i] else sumr - c + x[n-1-i])
    vr2.append(vr2[i] if vr2[i] > sumr - (c - x[n-1-i])*2 else sumr - (c - x[n-1-i])*2)
    
    
print(max( [max(vl1[i]+vr2[n-i],vr1[i]+vl2[n-i]) for i in range(n+1)] ))

Solution (Python):
n, c = map(int,input().split())
x = []
v = []
for i in range(n):
    s, t = input().split()
    x.append(int(s))
    v.append(int(t))
vl1 = [0]
vl2 = [0]
suml = 0
vr1 = [0]
vr2 = [0]
sumr = 0
for i in range(n):
    suml += v[i]
    
    
    vl1.append(max(vl1[i],suml - x[i]))
    vl2.append(max(vl2[i],suml - x[i]*2))
    sumr += v[n-1-i]
    
    
    vr1.append(max(vr1[i],sumr - c + x[n-1-i]))
    vr2.append(max(vr2[i],sumr - (c - x[n-1-i])*2))
print(max( [max(vl1[i]+vr2[n-i],vr1[i]+vl2[n-i]) for i in range(n+1)] ))

Solution (Python):
N,C = map(int,input().split())
x = [0] * N
v = [0] * N
for i in range(N):
	x[i],v[i] = map(int,input().split())

d = [0] * N
dd = [0] * N
dmax = [0] * N
ddmax = [0] * N

d[0] = v[0] - x[0]
dmax[0] = d[0]
for i in range(1,N):
	d[i] = d[i - 1] + v[i] - x[i] + x[i-1]
	dmax[i] = max(dmax[i-1], d[i])

dd[0] = v[N-1] - (C - x[N-1])
ddmax[0] = dd[0]
for i in range(1,N):
	dd[i] = v[N-1-i] + dd[i-1] - (C - x[N-1-i]) + (C - x[N-i])
	ddmax[i] = max(ddmax[i-1], dd[i])

ans = 0
for i in range(N):
	ans = max(ans, d[i])
	ans = max(ans,dd[i]);

for i in range(N-1):
	ans = max(ans, ddmax[N-i-2] - x[i] + d[i])
	ans = max(ans, dd[i] - C + x[N-1-i] + dmax[N-i-2])

print(ans)

Solution (Python):
N,C=map(int,input().split())
XV=[[int(x) for x in input().split()] for y in range(N)]
clock=[0 for x in range(N)]
antiClock=[0 for x in range(N)]


clock[0]=XV[0][1]-XV[0][0]
for i in range(1,N):
  clock[i]=clock[i-1]+XV[i-1][0]-XV[i][0]+XV[i][1]

maxClock=[0 for i in range(N+1)]
for i in range(1,N+1):
  if clock[i-1] >= maxClock[i-1]:
    maxClock[i]=clock[i-1]
  else:
    maxClock[i]=maxClock[i-1]


antiClock[0]=XV[N-1][1]-(C-XV[N-1][0])
for i in range(1,N):
  antiClock[i]=antiClock[i-1]+XV[N-1-i][0]-XV[N-i][0]+XV[N-1-i][1]

maxAnti=[0 for x in range(N+1)]
for i in range(1,N+1):
  if antiClock[i-1] >= maxAnti[i-1]:
    maxAnti[i]=antiClock[i-1]
  else:
    maxAnti[i]=maxAnti[i-1]


ans=set()

for i in range(N):
  a=clock[i]-XV[i][0]+maxAnti[N-1-i]
  ans.add(a)


for i in range(N):
  a=antiClock[i]-(C-XV[N-1-i][0])+maxClock[N-1-i]
  ans.add(a)


print(max(max(ans),max(maxClock),max(maxAnti)))

Solution (Python):
from numpy import array as n
N, C = map(int, input().split())
xy = [list(map(int, input().split())) for _ in range(N)]
r1, r2, l1, l2 = [0],[0],[0],[0]
sum = 0
for x, cal in xy:
    sum += cal
    r1.append(max(r1[-1], sum-x))
    r2.append(max(r2[-1], sum-2*x))
sum = 0
for x, cal in xy[::-1]:
    sum += cal
    l1.append(max(l1[-1], sum-C+x))
    l2.append(max(l2[-1], sum-2*(C-x)))
out = max(n(r1+r2)+n(l2[::-1]+l1[::-1]))
print(out)

Solution (Python):
from numpy import array as n
N, C = map(int, input().split())
xy = [list(map(int, input().split())) for _ in range(N)]
r1, r2, l1, l2 = [0],[0],[0],[0]
sum = 0
for x, cal in xy:
    sum += cal
    r1.append(max(r1[-1], sum-x))
    r2.append(max(r2[-1], sum-2*x))
sum = 0
for x, cal in xy[::-1]:
    sum += cal
    l1.append(max(l1[-1], sum-C+x))
    l2.append(max(l2[-1], sum-2*(C-x)))
a = max(n(r1)+n(l2[::-1]))
b = max(n(r2)+n(l1[::-1]))
print(max(a,b))

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

s = [tuple(map(int, input().split()))for _ in range(n)]

normal = [s[0][1] - s[0][0]] 
cur = normal[0]
n_max = max(0, normal[0])

for (s1_pos, s1_cal), (s2_pos, s2_cal) in zip(s, s[1:]):
    cur = cur + s2_cal - (s2_pos - s1_pos)
    if n_max < cur:
        n_max = cur
    normal.append(n_max)

reverse = [s[-1][1] - (c - s[-1][0])]
cur = reverse[0]
r_max = max(0, reverse[0])
         
for (s1_pos, s1_cal), (s2_pos, s2_cal) in zip(s[::-1], s[-2::-1]):
    cur = cur + s2_cal - (s1_pos-s2_pos)
    if r_max < cur:
        r_max = cur
    reverse.append(r_max)
         
res = 0
for i, (pos, cal) in enumerate(s[:-1]):
    res = max(res, normal[i], normal[i]-pos+reverse[n-2-i])
for i, (pos, cal) in enumerate(s[:0:-1]):
    res = max(res, reverse[i], reverse[i]-(c-pos)+normal[n-2-i])
res = max(res, normal[-1], reverse[-1])
         
print(res)

Solution (Python):
from numpy import array as n
N, C = map(int, input().split())
xy = [list(map(int, input().split())) for _ in range(N)]
r1, r2, l1, l2 = [0],[0],[0],[0]
sum, out = 0, 0
for x, cal in xy:
    sum += cal
    r1.append(max(r1[-1], sum-x))
    r2.append(max(r2[-1], sum-2*x))
sum = 0
for x, cal in xy[::-1]:
    sum += cal
    l1.append(max(l1[-1], sum-C+x))
    l2.append(max(l2[-1], sum-2*(C-x)))
print(max(n(r1+r2)+n(l2[::-1]+l1[::-1])))

Solution (Python):
n,c = map(int,input().split())
l = [[0,0] for _ in range(n)]
dp1 = [0 for _ in range(n)]
dp2 = [0 for _ in range(n)]
mx1 = [0 for _ in range(n)]
mx2 = [0 for _ in range(n)]
for i in range(n):
    l[i] = list(map(int, input().split()))


dp1[0] = l[0][1] - l[0][0]
dp2[0] = l[-1][1] - c + l[-1][0]
tmp = dp1[0]
tmp2 = dp2[0]
mx1[0] = dp1[0]
mx2[0] = dp2[0]
for i in range(1,n):
    dp1[i] = dp1[i-1] + l[i][1] - l[i][0] + l[i-1][0]
    dp2[i] = dp2[i-1] + l[-i-1][1] - l[-i][0] + l[-i-1][0]
    if tmp < dp1[i]:
        mx1[i] = dp1[i]
        tmp = dp1[i]
    else :
        mx1[i] = tmp
    if tmp2 < dp2[i]:
        mx2[i] = dp2[i]
        tmp2 = dp2[i]
    else :
        mx2[i] = tmp2

ans = max(max(dp1),max(dp2))

tmp = [0 for _ in range(n-1)]
tmp2 = [0 for _ in range(n-1)]
for i in range(n-1):
    tmp[i] = dp1[i] + mx2[-i-2] - l[i][0]
    if ans < tmp[i]:
        ans = tmp[i]

for i in range(n-1):
    tmp2[i] = dp2[i] + mx1[-i-2] - (c - l[-i-1][0])
    if ans < tmp2[i]:
        ans = tmp2[i]

if ans > 0:
    print(ans)
else:
    print(0)

Solution (Python):
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))