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):
N, C = map(int, input().split())
X, V = [], []

cw1 = [0]
cw2 = [0]
ccw1 = [0]
ccw2 = [0]

s = 0
for _ in range(N):
  x, v = map(int, input().split())
  X.append(x)
  V.append(v)
  s += v
  cw1.append(max(cw1[-1], s-x))
  cw2.append(max(cw2[-1], s-2*x))

s = 0

for x,v in zip(X[::-1], V[::-1]):
  s += v
  ccw1.append(max(ccw1[-1], s-(C-x)))
  ccw2.append(max(ccw2[-1], s-(C-x)*2))

ans = 0

for a,b,c,d in zip(cw1, ccw2[::-1], cw2, ccw1[::-1]):
  ans = max(ans, a+b, c+d)

print(ans)

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



R_acc_val = [0] + list(accumulate(V))
L_acc_val = [0] + list(accumulate(V[::-1]))



R_val, L_val = [0], [0]
for i in range(1, N + 1):
    R_val.append(R_acc_val[i] - X[i - 1])
    L_val.append(L_acc_val[i] - (C - X[-i]))



R_max_val, L_max_val = list(accumulate(R_val, func=max)), list(accumulate(L_val, func=max))


ans = 0
for i in range(N + 1):
    ans = max(ans,
              R_val[i],
              R_val[i] + L_max_val[-i-1] - X[i - 1],
              L_val[i],
              L_val[i] + R_max_val[-i-1] - (C - X[-i]))

print(ans)

Solution (Python):
n,c=map(int, input().split())
lst = [list(map(int, input().split())) for i in range(n)] 
ans=0
lback=[0]
rback=[0]
l=0
r=0
for i, j in enumerate(lst,1):
  l+=j[1]
  if(lback[i-1]<l-2*j[0]):
    lback+=[l-2*j[0]]
  else:
    lback+=[lback[i-1]]
  ans=max(ans,l-j[0])
for i, j in enumerate(lst[::-1],1):
  r+=j[1]
  if(rback[i-1]<r-2*(c-j[0])):
    rback+=[r-2*(c-j[0])]
  else:
    rback+=[rback[i-1]]
  ans=max(ans,r-(c-j[0]), lback[n-i]+r-(c-j[0]) )
l=0
for i, j in enumerate(lst,1):
  l+=j[1]
  ans=max(ans,rback[n-i]+l-j[0])
print(ans)

Solution (Python):
N,C=map(int,input().split())

L=[]
for i in range(N):
    x,y=map(int,input().split())
    L.append([x,y])
    
l=L[::-1]
L=[[0,0]]+L
R=[]

for i in range(N):
    R.append([C-l[i][0],l[i][1]])
R=[[0,0]]+R




TS=[0]
TW=[0]
RS=[0]
RW=[0]

for i in range(N):
    TS.append(TS[i]-L[i+1][0]+L[i][0]+L[i+1][1])
    TW.append(TW[i]-2*(L[i+1][0]-L[i][0])+L[i+1][1])
    RS.append(RS[i]-R[i+1][0]+R[i][0]+R[i+1][1])
    RW.append(RW[i]-2*(R[i+1][0]-R[i][0])+R[i+1][1])
    
MTS=[0]
MRS=[0]
for i in range(1,N+1):
    MTS.append(max(MTS[i-1],TS[i]))
    MRS.append(max(MRS[i-1],RS[i]))

ans=0
for i in range(N+1):
    if i>0 and L[i-1][0]*2>C:
        break
    else:
        if ans<TW[i] + MRS[N-i]:
            ans=TW[i] + MRS[N-i]
            
for i in range(N+1):
    if i>0 and R[i-1][0]*2>C:
        break
    else:
        if ans<RW[i]+MTS[N-i]:
            ans=RW[i]+MTS[N-i]
print(ans)

Solution (Python):
N,C=map(int,input().split())

L=[]
for i in range(N):
    x,y=map(int,input().split())
    L.append([x,y])
    
l=L[::-1]
L=[[0,0]]+L
R=[]

for i in range(N):
    R.append([C-l[i][0],l[i][1]])
R=[[0,0]]+R




TS=[0]
TW=[0]
RS=[0]
RW=[0]

for i in range(N):
    TS.append(TS[i]-L[i+1][0]+L[i][0]+L[i+1][1])
    TW.append(TW[i]-2*(L[i+1][0]-L[i][0])+L[i+1][1])
    RS.append(RS[i]-R[i+1][0]+R[i][0]+R[i+1][1])
    RW.append(RW[i]-2*(R[i+1][0]-R[i][0])+R[i+1][1])
    
MTS=[0]
MRS=[0]
for i in range(1,N+1):
    MTS.append(max(MTS[i-1],TS[i]))
    MRS.append(max(MRS[i-1],RS[i]))

ans=0
for i in range(N+1):
    if i>0 and L[i-1][0]*2>C:
        break
    else:
        if ans<TW[i] + MRS[N-i]:
            ans=TW[i] + MRS[N-i]
            
for i in range(N+1):
    if i>0 and R[i-1][0]*2>C:
        break
    else:
        if ans<RW[i]+MTS[N-i]:
            ans=RW[i]+MTS[N-i]
print(ans)

Solution (Python):
from itertools import accumulate
import bisect

n, c = [int(item) for item in input().split()]

xs, vs, xrs = [], [], []
for i in range(n):
  x, v = [int(item) for item in input().split()]
  xs.append(x)
  xrs.append(c-x)
  vs.append(v)
xrs = xrs[::-1]

lft_v = list(accumulate(vs))
rgt_v = list(accumulate(vs[::-1]))

lft_c = []
rgt_c = []
for i in range(n):
  lft_c.append(lft_v[i]-xs[i])
  rgt_c.append(rgt_v[i]-xrs[i])
  
lft_max = [0] * n
rgt_max = [0] * n
lm, rm = 0,0
for i in range(n):
  if lm < lft_c[i]:
    lm = lft_c[i]
  lft_max[i] = lm
  if rm < rgt_c[i]:
    rm = rgt_c[i]
  rgt_max[i] = rm

ans = 0

for i in range(n):
  ans = max(ans, lft_c[i], rgt_c[i])
  

for i in range(n-1):
  ans = max(ans, lft_c[i] - xs[i] + rgt_max[n-2-i])
  ans = max(ans, rgt_c[i] - xrs[i] + lft_max[n-2-i])
    
print(ans)

Solution (Python):
n, c = [int(i) for i in input().split(" ")]
x = [0] * (n + 1)
v = [0] * (n + 1)

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

def s(x, v):
    
    sum_v = [0] * (n + 1)
    for i in range(1, n + 1):
        sum_v[i] = sum_v[i - 1] + v[i]
    v_sub = [sum_v[i] - x[i] for i in range(0, n + 1)]
    max_v = [0] * (n + 1)
    for i in range(1, n + 1):
        max_v[i] = max(max_v[i - 1], v_sub[i])
    
    
    sum_r = v[n]
    max_v_sub = max_v[n]
    for i in range(1, n // 2 + 1):
        v_sub = sum_r - (c - x[n - i + 1]) * 2 + max_v[n - i]
        
        if v_sub > max_v_sub:
            max_v_sub = v_sub
        sum_r += v[n - i]
    return max_v_sub
        
print(max(s(x, v), s([0] + [c - xi for xi in x[1::]][::-1], [0] + v[1::][::-1])))

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

re_lst = []
for i in reversed(range(n)):
    re_lst.append([abs(c-lst[i][0]), lst[i][1]])

r_accum = []
s = 0
for i in range(n):
    s += lst[i][1]
    r_accum.append(s)

l_accum = []
s = 0
for i in range(n):
    s += re_lst[i][1]
    l_accum.append(s)

r_max = 0
r_max_lst = []
for i in range(n):
    r_max = max(r_max, r_accum[i] - lst[i][0])
    r_max_lst.append(r_max)
    
l_max = 0
l_max_lst = []
for i in range(n):
    l_max = max(l_max, l_accum[i] - re_lst[i][0])
    l_max_lst.append(l_max)

rd_max = 0
for i in range(n-1):
    pt = r_accum[i] - 2*lst[i][0]
    pt += l_max_lst[(n-1)-i-1]
    if pt > rd_max:
        rd_max = pt
            

ld_max = 0
for i in range(n-1):
    pt = l_accum[i] - 2*re_lst[i][0]
    pt += r_max_lst[(n-1)-i-1]
    if pt > ld_max:
        ld_max = pt

print(max(r_max, l_max, rd_max, ld_max))

Solution (Python):

N,C = map(int,input().split())

x = [0]
v = [0]

acc = [0]
rev = [0]
s = 0
res = 0
for i in range(N):
    a,b = map(int,input().split())
    x.append(a)
    v.append(b)
    s += b
    acc.append(s)




F =  []

for i in range(N+1):
    F.append(acc[i]-x[i])
for i in range(1,N+1):
    F[i] = max(F[i],F[i-1])
for B in range(N+1):
    if B == 0:
        A = N
        costB = 0
    else:
        A = B - 1
        costB = (s-acc[B]+v[B]-2*(C-x[B]))
    
    res = max(res,F[A] + costB)



F[0] = 0
for i in range(N,0,-1):
    F[i] = (s-acc[i]+v[i]-(C-x[i]))
    F[i] = max(F[i],F[(i+1)%(N+1)])
for A in range(N+1):
    if A == N:
        B = 0
    else:
        B = A + 1
    
    res = max(res,F[B] + acc[A]-2*x[A])
print(res)

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


Xi.insert(0, [0, 0])
Xi.append([C, 0])



total = 0
R_max = [0 for i in range(N + 2)]
for i in range(1, N + 1):
    total += Xi[i][1] - (Xi[i][0] - Xi[i - 1][0])
    R_max[i] = max(total, R_max[i - 1])


total = 0
L_max = [0 for i in range(N + 2)]
for i in range(N, 0, -1):
    total += Xi[i][1] - (Xi[i + 1][0] - Xi[i][0])
    L_max[i] = max(total, L_max[i + 1])

max_value = 0
for i in range(0, N + 1):
    i2 = N - i - 1
    
    cal = R_max[i] + L_max[i + 1] - Xi[i][0]
    max_value = max(cal, max_value)

    
    cal = R_max[i] + L_max[i + 1] - (C - Xi[i + 1][0])
    max_value = max(cal, max_value)
print(max_value)

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

    sc = 0
    v11 = [0 for i in range(N)]
    v12 = [0 for i in range(N)]
    for i in range(N):
        sc += v[i]
        v11[i] = sc - x[i]
        v12[i] = sc - x[i] * 2
    for i in range(1,N):
        v11[i] = max(v11[i], v11[i-1])
        v12[i] = max(v12[i], v12[i-1])

    sc = 0
    v21 = [0 for i in range(N)]
    v22 = [0 for i in range(N)]
    for i in range(N-1,-1,-1):
        sc += v[i]
        v21[i] = sc - (C - x[i])
        v22[i] = sc - (C - x[i]) * 2
    for i in range(N-2,-1,-1):
        v21[i] = max(v21[i], v21[i+1])
        v22[i] = max(v22[i], v22[i+1])

    ans = 0
    for i in range(N):
        ans = max(ans, v11[i])
        ans = max(ans, v21[i])
    for i in range(N-1):
        ans = max(ans, v12[i]+v21[i+1])
        ans = max(ans, v11[i]+v22[i+1])

    return ans

print(solve())

Solution (Python):
n,c = map(int,input().split())
info = [[0,0]]+[list(map(int,input().split())) for i in range(n)]+[[c,0]]
left_info = info[::-1]

right_ruiseki = [0]*(n+1)
left_ruiseki = [0]*(n+1)
ou_right_ruiseki = [0]*(n+1)
ou_left_ruiseki = [0]*(n+1)

for i in range(n):
  right_ruiseki[i+1] = right_ruiseki[i] + info[i+1][1] - info[i+1][0] + info[i][0]
  left_ruiseki[i+1] = left_ruiseki[i] + left_info[i+1][1] + left_info[i+1][0] - left_info[i][0]
  ou_right_ruiseki[i+1] = ou_right_ruiseki[i] + info[i+1][1] - 2*info[i+1][0] + 2*info[i][0]
  ou_left_ruiseki[i+1] = ou_left_ruiseki[i] + left_info[i+1][1] + 2*left_info[i+1][0] - 2*left_info[i][0]



for i in range(n):
  right_ruiseki[i+1]=max(right_ruiseki[i],right_ruiseki[i+1])
  left_ruiseki[i+1]=max(left_ruiseki[i],left_ruiseki[i+1])


ans = 0

for i in range(n+1):
  tmp1 = ou_right_ruiseki[i]+left_ruiseki[n-i]
  tmp2 = ou_left_ruiseki[i]+right_ruiseki[n-i]
  ans = max(tmp1,tmp2,ans)
print(ans)

Solution (Python):
def static_sushi(N: int, C: int, XV: list) -> int:
    Sv = [0] * (N+1)
    fr, fl = [0] * (N+1), [0] * (N+1)
    gr, gl = [0] * (N+1), [0] * (N+1)
    for i, (x, v) in enumerate(XV):
        Sv[i+1] = Sv[i] + v

        fl[i+1] = Sv[i+1] - x
        gl[i+1] = max(gl[i], fl[i+1])

        fr[i+1] = Sv[i+1] - 2*x
        gr[i+1] = max(gr[i], fr[i+1])

    max_cal = 0
    for ib, (xb, _) in enumerate(XV+[(C,0)]):
        cal_a1, cal_a2 = gr[ib], gl[ib]
        cal_b1, cal_b2 = Sv[N] - Sv[ib] - (C-xb), Sv[N] - Sv[ib] - 2*(C-xb)
        cal1, cal2 = cal_a1 + cal_b1, cal_a2 + cal_b2
        max_cal = max(max_cal, cal1, cal2)

    return max_cal


if __name__ == "__main__":
    N = 0
    N, C = map(int, input().split())
    XV = [tuple(map(int, input().split())) for _ in range(N)]
    ans = static_sushi(N, C, XV)
    print(ans)

Solution (Python):
def main():
    N, C = map(int, input().split())
    sushis = [(0, 0) for _ in range(N)]
    for i in range(N):
        x, v = map(int, input().split())
        sushis[i] = (x, v)
    ls, rs = 0, 0
    left = [(0, 0) for _ in range(N+1)]
    right = [(0, 0) for _ in range(N+1)]
    for i in range(N):
        ls += sushis[i][1]
        lx = sushis[i][0]
        rs += sushis[-i-1][1]
        rx = C - sushis[-i-1][0]
        left[i+1] = (max(ls-lx, left[i][0]),
                     max(ls-lx*2, left[i][1]))
        right[i+1] = (max(rs-rx, right[i][0]),
                      max(rs-rx*2, right[i][1]))
    ans = 0
    for i in range(N+1):
        ans = max([left[i][0] + right[N-i][1],
                   left[i][1] + right[N-i][0],
                   ans])
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n, c = map(int, input().split())
sushi = [tuple(map(int, input().split())) for _ in range(n)]
sushi = [(0, 0)] + sushi
cclkmax = [0] * (n+1)
cumvalue = 0
for i in range(1, n+1):
    cumvalue += sushi[i][1]
    cclkmax[i] = max(cclkmax[i-1], cumvalue - sushi[i][0])
maxscore = cclkmax[n]
cum = 0
for i in range(n, 0, -1):
    cum += sushi[i][1]
    score = cclkmax[i-1] + cum - 2*(c - sushi[i][0])
    maxscore = max(maxscore, score)
clkmax = [0] * (n+1)
cumvalue = 0
for i in range(n, 0, -1):
    cumvalue += sushi[i][1]
    clkmax[i] = max(clkmax[(i+1)%(n+1)], cumvalue - (c - sushi[i][0]))
maxscore = max(maxscore, clkmax[1])
cum = 0
for i in range(1, n+1):
    cum += sushi[i][1]
    score = clkmax[(i+1)%(n+1)] + cum - 2*sushi[i][0]
    maxscore = max(maxscore, score)
print(maxscore)

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**9)
from itertools import accumulate
n,c=map(int,input().split())
X,V=[],[]
for _ in range(n):
    x,v=map(int, input().split())
    X.append(x)
    V.append(v)
V_ret,X_ret=V[::-1],X[::-1]
S,S_ret=list(accumulate(V)),list(accumulate(V_ret))
S[0]-=X[0]
S_ret[0]-=(c-X_ret[0])
for i in range(1,n):
    S[i]=max(S[i-1],S[i]-X[i])
    S_ret[i]=max(S_ret[i-1],S_ret[i]-(c-X_ret[i]))
ans=max(0,max(S),max(S_ret))
for i in range(n-1):
    tmp1=S[i]+S_ret[n-(i+2)]-X[i]
    tmp2=S_ret[i]+S[n-(i+2)]-(c-X_ret[i])
    ans=max(ans,tmp1,tmp2)
print(ans)

Solution (Python):
import sys
from itertools import accumulate

def main():
  input = sys.stdin.readline
  N, C = map(int, input().split())
  X = []
  V = []
  for _ in range(N):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v)
  Vr = V[::-1]
  Xr = X[::-1]

  F = list(accumulate(V))
  R = list(accumulate(Vr))
  F[0], R[0] = F[0] - X[0], R[0] - (C - Xr[0])
  for i in range(1, N):
    F[i] = max(F[i-1], F[i] - X[i])
    R[i] = max(R[i-1], R[i] - (C - Xr[i]))

  val = 0
  for i in range(N-1):
    v1 = F[i] + R[N-(i+2)] - X[i]
    v2 = R[i] + F[N-(i+2)] - (C - Xr[i])
    val = max(val, v1, v2)
  
  ans = max(0, val, max(F), max(R))
  print(ans)


if __name__ == '__main__':
  main()

Solution (Python):
N, C = map(int, input().split())
x = [0]
v = [0]
for _ in range(N):
    a, b = map(int, input().split())
    x.append(a)
    v.append(b)
opt_l = [0]
acc_v = 0
for i in range(1, N + 1):
    opt_l.append(max(opt_l[-1], v[i] + acc_v - x[i]))
    acc_v += v[i]
opt_r = [0]
acc_v = 0
for i in range(1, N + 1):
    opt_r.append(max(opt_r[-1], v[-i] + acc_v - (C - x[-i])))
    acc_v += v[-i]
ans = 0
acc_v = 0
for i in range(N + 1):
    ans = max(
        ans,
        acc_v + v[i] - x[i],
        acc_v + v[i] - 2 * x[i] + opt_r[N - i]
    )
    acc_v += v[i]
acc_v = 0
for i in range(N + 1):
    ans = max(
        ans,
        acc_v + v[-i] - (C - x[-i]),
        acc_v + v[-i] - 2 * (C - x[-i]) + opt_l[N - i]
    )
    acc_v += v[-i]
print(ans)

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


score = [0 for i in range(n+1)] 
start = 0
noteat = 0
for i in range(1,n+1):
    temp = 0
    if score[i-1] - (sushi[i][0] - start) + sushi[i][1] + noteat > score[i-1]: 
        temp = score[i-1] - (sushi[i][0] - start) + sushi[i][1] + noteat
        noteat = 0
        start = sushi[i][0]
    else:
        noteat += sushi[i][1]
        temp = score[i-1]
    score[i] = temp



ans = score[n]
leftscore = 0
dist = 0
for i in range(1,n): 
    leftscore += sushi[-i][1]
    if i == 1:
        dist += c - sushi[-i][0]
    else:
        dist += sushi[-i+1][0] - sushi[-i][0]
    if leftscore - 2*dist + score[n-i] > ans:
        ans = leftscore - 2*dist + score[n-i]





reverse = [0 for i in range(n+1)]
start = c
noteat = 0
for i in range(1,n+1):
    temp = 0
    if sushi[-i][1] - (start - sushi[-i][0]) + reverse[i-1] + noteat > reverse[i-1]: 
        temp = sushi[-i][1] - (start - sushi[-i][0]) + reverse[i-1] + noteat
        noteat = 0
        start = sushi[-i][0]
    else:
        noteat += sushi[-i][1]
        temp = reverse[i-1]
    reverse[i] = temp



ans2 = reverse[n]
rightscore = 0
dist = 0
for i in range(1,n): 
    rightscore += sushi[i][1]
    dist += sushi[i][0] - sushi[i-1][0]
    if rightscore - 2*dist + reverse[n-i] > ans2:
        ans2 = rightscore - 2*dist + reverse[n-i]

print(max(ans,ans2))

Solution (Python):
N, C = list(map(int, input().split()))

D = []

for i in range(N):
  D.append(list(map(int, input().split())))
  
R = [[0, 0] for i in range(N + 1)]
cnt = 0
ma = 0
rma = 0
R[0][0] = -1
for i in range(N):
  cnt += D[i][1]
  if cnt - D[i][0] > ma:
    ma = cnt - D[i][0]
  if cnt - D[i][0] * 2 > rma:
    rma = cnt - D[i][0] * 2 
  R[i + 1] = [ma, rma]

cnt = 0
Ans = R[N][0]
tmp = 0
for i in range(N - 1, -1, -1):
  cnt += D[i][1]
  tmp = C - D[i][0]
  tmp2 = cnt - tmp * 2 + R[i][0]
  tmp2 = max(tmp2, cnt - tmp + R[i][1])
  if tmp2 > Ans:
    Ans = tmp2

print(Ans)

Solution (Python):
N, C = map(int, input().split())
X, V, D, E = [0], [0], [0], [0] * (N + 2)
for i in range(1, N + 1):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v + V[i - 1])
    D.append(max(D[i - 1], V[i] - X[i]))

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

ret = max(D[N], E[1])
for i in range(1, N + 1):
    ret = max(ret, D[i] + E[i + 1] - X[i])
for i in range(N, 0, -1):
    ret = max(ret, E[i] + D[i - 1] - (C - X[i]))
print(ret)

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

S_cw = [0]
S_ccw = [0]
S_cw_return = [0]
S_ccw_return = [0]

for i in range(n):
    S_cw.append(S_cw[-1]+x[i][1])
    S_ccw.append(S_ccw[-1]+x[n-1-i][1])

for i in range(n):
    S_cw[i+1] -= x[i][0]
    S_ccw[i+1] -= c-x[n-1-i][0]
    S_cw_return.append(S_cw[i+1] - x[i][0])
    S_ccw_return.append(S_ccw[i+1] - (c-x[n-1-i][0]))

S_cw_return_max = [0]
S_ccw_return_max = [0]
for i in range(n):
    if S_cw_return[i+1] > S_cw_return_max[-1]:
        S_cw_return_max.append(S_cw_return[i+1])
    else:
        S_cw_return_max.append(S_cw_return_max[-1])

    if S_ccw_return[i+1] > S_ccw_return_max[-1]:
        S_ccw_return_max.append(S_ccw_return[i+1])
    else:
        S_ccw_return_max.append(S_ccw_return_max[-1])

ans = 0
for i in range(n+1):
    idx = n-i
    ans = max(ans, S_cw[i]+S_ccw_return_max[idx], S_ccw[i]+S_cw_return_max[idx])
print(ans)

Solution (Python):
from itertools import accumulate
import sys
input = sys.stdin.readline

def main():
    n,c = map(int,input().split())
    x1,v1 = [0],[0]
    for _ in range(n):
        a,b = map(int, input().split())
        x1.append(a)
        v1.append(b)
    x2 = [0]
    v2 = [0]
    for i in range(n,0,-1):
        x2.append(c-x1[i])
        v2.append(v1[i])

    v1, v2 = list(accumulate(v1)), list(accumulate(v2))
    g1,g2 = [v2[0]-x2[0]],[v2[0]-2*x2[0]]
    for i in range(1,n+1):
        f = v2[i] - x2[i]
        f2 = v2[i] - 2*x2[i]
        g1.append(max(g1[i-1],f))
        g2.append(max(g2[i-1],f2))
    val = max(v1[n]-x1[n] + g2[0], v1[n]-2*x1[n] + g1[0])
    for i in range(1,n+1):
        val = max(val, v1[n-i]-x1[n-i] + g2[i], v1[n-i]-2*x1[n-i] + g1[i])

    print(val)

if __name__=='__main__': main()

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



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

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

for i in range(N):
    if VF[i+1] < VF[i]:
        VF[i+1] = VF[i]
for i in range(N):
    if VB[N-1-i] < VB[N-i]:
        VB[N-1-i] = VB[N-i]


ans = 0
ans = max(ans, VF[N], VB[0])


for i in range(1,N):
    ans = max(ans, VF[i], VF[i]+VB[i]-XV[i-1][0])


for i in range(1,N):
    ans = max(ans, VB[i], VB[i]+VF[i]-(C-XV[i][0]))

print(ans)

Solution (Python):


def main():
    na = list(map(int, input().split()))
    N, C = na[0], na[1]
    x, v = [], []
    for i in range(N):
        na = list(map(int, input().split()))
        x.append(na[0])
        v.append(na[1])

    cw1 = [0] * N
    cw2 = [0] * N
    acw1 = [0] * N
    acw2 = [0] * N

    sum_v = 0
    m1 = m2 = 0
    for i in range(N):
        sum_v += v[i]
        m1 = max(m1, sum_v - x[i])
        cw1[i] = m1
        m2 = max(m2, sum_v - x[i] * 2)
        cw2[i] = m2

    sum_v = 0
    m1 = m2 = 0
    for i in range(N - 1, -1, -1):
        sum_v += v[i]
        m1 = max(m1, sum_v - (C - x[i]))
        acw1[i] = m1
        m2 = max(m2, sum_v - (C - x[i]) * 2)
        acw2[i] = m2

    m = max(cw1[N - 1], acw1[0])
    for i in range(N - 1):
        m = max(m, cw1[i] + acw2[i + 1], cw2[i] + acw1[i + 1])

    print(m)


if __name__ == '__main__':
    main()

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

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

dp = [[0] * (n + 2) for _ in range(2)]

cal = 0
for i in range(1, n + 1):
    cal -= x[i] - x[i - 1]
    cal += v[i]
    dp[0][i] = max(dp[0][i - 1], cal)

cal = 0
for i in range(n, 0, -1):
    cal -= x[i + 1] - x[i]
    cal += v[i]
    dp[1][i] = max(dp[1][i + 1], cal)

maxcal = 0
for i in range(n + 1):
    maxcal = max(maxcal, dp[0][i] + dp[1][i + 1] - x[i])
    maxcal = max(maxcal, dp[0][i] + dp[1][i + 1] - c + x[i + 1])

print(maxcal)

Solution (Python):
N, C = map(int, input().split())
xy = []
for _ in range(N):
    xy.append(tuple(map(int, input().split())))
ans = 0
cal_sum_A = [0 for _ in range(N)]
cal_sum_A[0] = xy[0][1] - xy[0][0]
for i in range(1, N):
    cal_sum_A[i] += cal_sum_A[i - 1] + xy[i][1] - xy[i][0] + xy[i - 1][0]

cal_sum_B = [0 for _ in range(N)]
cal_sum_B[N - 1] = xy[N - 1][1] - (C - xy[N - 1][0])
for i in range(N - 2, -1, -1):
    cal_sum_B[i] += cal_sum_B[i + 1] + xy[i][1] - (C - xy[i][0]) + (C - xy[i + 1][0])


max_cal_A = [0 for _ in range(N)]
max_cal_A[0] = cal_sum_A[0]
for i in range(1, N):
    max_cal_A[i] = max(max_cal_A[i - 1], cal_sum_A[i])
ans = max(ans, max_cal_A[N-1])

max_cal_B = [0 for _ in range(N)]
max_cal_B[N-1] = max(max_cal_B[N-1], cal_sum_B[N-1])

for i in range(N-2, -1, -1):
    max_cal_B[i] = max(max_cal_B[i+1], cal_sum_B[i])
ans = max(ans, max_cal_B[0])

for i in range(1, N-1):
    cal = 0
    cal += cal_sum_B[-i]
    cal -= (C-xy[-i][0])
    cal += max_cal_A[N-i-1]
    ans = max(ans, cal)

for i in range(1, N):
    cal = 0
    cal += cal_sum_A[i-1]
    cal -= xy[i-1][0]
    cal += max_cal_B[-(N-i)]
    ans = max(ans, cal)
print(ans)

Solution (Python):


"""
参考：http://blog.livedoor.jp/misteer/archives/9017497.html
　　　https://www.hamayanhamayan.com/entry/2018/04/21/225805
・累積max(累積和の応用)
・先に回る方向がiより手前にmaxがあったとしても、
　そのパターンは別の周回のiで考慮されているので、
　始点まで戻るマイナス分が多くなっていても問題ない。
　どのみち先に回る方向で最終的にansに採用されるのはiまで行った時のはず。
"""

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

mx_r = [0] * (N+2)
cal = 0
for i in range(1, N+1):
    cal += v[i] - (x[i] - x[i-1])
    mx_r[i] = max(cal, mx_r[i-1])
mx_l = [0] * (N+2)
cal = v[N] - (C - x[N])
mx_l[N] = cal
for i in range(N-1, 0, -1):
    cal += v[i] - (x[i+1] - x[i])
    mx_l[i] = max(cal, mx_l[i+1])

ans = 0
for i in range(N+1):
    
    ans = max(mx_r[i], mx_r[i] - x[i] + mx_l[i+1], ans)
    
    if i != 0:
        ans = max(mx_l[i], mx_l[i] - (C - x[i]) + mx_r[i-1], ans)
print(ans)

Solution (Python):
from itertools import accumulate
N, C = map(int, input().split())
Distances, Values = [], []
for i in range(N):
    d, v = map(int, input().split())
    Distances.append(d)
    Values.append(v)


right_acc_values = list(accumulate(Values))
left_acc_values = list(accumulate(Values[::-1]))


right_distances = Distances
left_distances = [C - d for d in Distances[::-1]]


right_result = [v - d for d, v in zip(right_distances, right_acc_values)]
left_result = [v - d for d, v in zip(left_distances, left_acc_values)]


right_result_max = [0]
left_result_max = [0]
for i in range(N):
    right_result_max.append(max(right_result_max[-1], right_result[i]))
    left_result_max.append(max(left_result_max[-1], left_result[i]))


ans = 0
for i in range(N):
    ans = max(ans,
              right_result[i],
              left_result[i],
              right_result[i] + left_result_max[N - i - 1] - right_distances[i],
              left_result[i] + right_result_max[N - i - 1] - left_distances[i])

print(ans)

Solution (Python):


"""
参考：http://blog.livedoor.jp/misteer/archives/9017497.html
　　　https://www.hamayanhamayan.com/entry/2018/04/21/225805
・累積和
・累積max
"""

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

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

mx_r = [0] * (N+2)
cal = 0
for i in range(1, N+1):
    cal += v[i] - (x[i] - x[i-1])
    mx_r[i] = max(cal, mx_r[i-1])
mx_l = [0] * (N+2)
mx_l[N] = cal = v[N] - (C - x[N])
for i in range(N-1, 0, -1):
    cal += v[i] - (x[i+1] - x[i])
    mx_l[i] = max(cal, mx_l[i+1])

ans = 0
for i in range(N+1):
    
    ans = max(sm_r[i], sm_r[i] - x[i] + mx_l[i+1], ans)
    
    if i != 0:
        ans = max(sm_l[i], sm_l[i] - (C - x[i]) + mx_r[i-1], ans)
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

n, c = map(int, input().split())
sushi = [list(map(int, input().split())) for _ in range(n)]

if n == 1:
    print(max(sushi[0][1]-sushi[0][0], sushi[0][1]-(c-sushi[0][0]), 0))
    exit()

x = []
for i in range(n):
    x.append(sushi[i][0])

vcum = []
v = 0
for i in range(n):
    v += sushi[i][1]
    vcum.append(v)

ans = 0


for i in range(n):
    if ans < vcum[i] - x[i]:
        ans = vcum[i] - x[i]



for i in range(n):
    if i == 0:
        if ans < vcum[n-1] - (c - x[0]):
            ans = vcum[n-1] - (c - x[0])
    else:
        if ans < (vcum[n - 1] - vcum[i - 1]) - (c - x[i]):
            ans = (vcum[n - 1] - vcum[i - 1]) - (c - x[i])




right_dp = [0] * n
right_dp[n-1] = vcum[n-1] - vcum[n-2] - (c - x[n-1])
for i in reversed(range(1, n-1)):
    right_dp[i] = max(right_dp[i+1], vcum[n-1] - vcum[i-1] - (c - x[i]))


for i in range(n-1):
    cal = vcum[i] - 2 * x[i] + right_dp[i+1]
    if ans < cal:
        ans = cal



left_dp = [0] * n
left_dp[0] = vcum[0] - x[0]
for i in range(1, n):
    left_dp[i] = max(left_dp[i-1], vcum[i] - x[i])


for i in range(1, n):
    cal = vcum[n-1] - vcum[i-1] - 2 * (c - x[i]) + left_dp[i-1]
    if ans < cal:
        ans = cal


print(ans)

Solution (Python):
N, C = map(int, input().split())
S = [[0, 0, 0] for i in range(N + 2)]
L = [[0, 0, 0] for i in range(N + 2)]
R = [[0, 0, 0] for i in range(N + 2)]
for i in range(N):
  x, v = map(int, input().split())
  S[i + 1] = [v, x, C - x]
  L[i + 1][0] = L[i][0] - x + S[i][1] + v
  L[i + 1][1] = L[i + 1][0] - x
  L[i + 2][2] = max(L[i + 1][2], L[i + 1][1])
for i in range(N, 0, -1):
  R[i][0] = R[i + 1][0] - S[i][2] + S[i + 1][2] + S[i][0]
  R[i][1] = R[i][0] - S[i][2]
  R[i - 1][2] = max(R[i][2], R[i][1])
ans = 0
for i in range(N + 1):
  ans = max(L[i][0] + R[i][2], L[i][2] + R[i][0], ans)
print(ans)

Solution (Python):
from bisect import bisect_right,bisect_left
N,C=map(int,input().split())
S=[[int(i) for i in input().split()] for i in range(N)]
Way1=[S[0][1]-S[0][0]]
p1=[]
if Way1[0]>0:
  p1.append(0)
for i in range(1,N):
  Way1.append(Way1[i-1]+S[i][1]-(S[i][0]-S[i-1][0]))
  if p1==[] and Way1[i]>0:
    p1.append(i)
  elif p1!=[]:
    if Way1[p1[-1]]<Way1[i]:
      p1.append(i)
    
Way2=[S[-1][1]-(C-S[-1][0])]
p2=[]
if Way2[0]>0:
  p2.append(N-1)
for i in range(1,N):
  Way2.append(Way2[i-1]+S[N-i-1][1]+(S[N-i-1][0]-S[N-i][0]))
  if p2==[] and Way2[i]>0:
    p2.append(N-i-1)
  elif p2!=[]:
    if Way2[N-1-p2[-1]]<Way2[i]:
      p2.append(N-i-1)
Way2.reverse()
p2.reverse()

ans=0
for i in p1:
  if i!=N-1:
    if bisect_right(p2,i+0.1)<len(p2):
      j=p2[bisect_right(p2,i+0.1)]
      ans=max(ans,Way1[i],Way1[i]-S[i][0]+Way2[j])
    else:
      ans=max(ans,Way1[i])
  else:
    ans=max(ans,Way1[i])
for j in p2:
  if j!=0:
    if bisect_left(p1,j-0.1)>0:
      i=p1[bisect_left(p1,j-0.1)-1]
      ans=max(ans,Way2[j],Way1[i]-(C-S[j][0])+Way2[j])
    else:
      ans=max(ans,Way2[j])
  else:
    ans=max(ans,Way2[j])
    
print(ans)