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 = 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):
def solve():
    N, C = map(int, raw_input().split())
    xs, vs = [], []
    for i in range(N):
        x, v = map(int, raw_input().split())
        xs.append(x)
        vs.append(v)
    xs, vs = map(list, zip(*sorted(zip(xs, vs), key=lambda _: _[0])))
    a = f(xs, vs, N, C)
    b = f(map(lambda x: C-x, xs[::-1]), vs[::-1], N, C)
    ans = max(a, b)
    return ans

def f(xs, vs, N, C):
    ws = []
    best = 0
    cur = 0
    px = C
    for i in range(N-1,-1,-1):
        x, v = xs[i], vs[i]
        cur += v - abs(x-px)
        best = max(best, cur)
        px = x
        ws.append(best)
    ws = ws[::-1]
    best = 0
    cur = 0
    px = 0
    for i in range(N):
        x, v = xs[i], vs[i]
        cur += v - abs(x-px)
        best = max(best, cur)
        if i+1 < N:
            best = max(best, cur+ws[i+1]-x)
        px = x
    return best

print solve()

Solution (Python):
N, C = map(int, input().split())
xy = [[int(i) for i in input().split()] for _ in range(N)]
c = [0]
rc = [0]
uc = [0]
ruc = [0]
sum = 0
out = 0
for x, cal in xy:
    sum += cal
    c.append(max(c[-1], sum-x))
    rc.append(max(rc[-1], sum-2*x))
sum = 0
for x, cal in xy[::-1]:
    sum += cal
    uc.append(max(uc[-1], sum-C+x))
    ruc.append(max(ruc[-1], sum-2*(C-x)))
for w, wr in zip(c, ruc[::-1]):
    out = max(out, w+wr)
for w, wr in zip(rc, uc[::-1]):
    out = max(out, w+wr)
print(out)

Solution (Python):
N, C = map(int, input().split())
xy = [[int(i) for i in input().split()] for _ in range(N)]
c = [0]
rc = [0]
uc = [0]
ruc = [0]
sum = 0
out = 0
for x, cal in xy:
    sum += cal
    c.append(max(c[-1], sum-x))
    rc.append(max(rc[-1], sum-2*x))
sum = 0
for x, cal in xy[::-1]:
    sum += cal
    uc.append(max(uc[-1], sum-C+x))
    ruc.append(max(ruc[-1], sum-2*(C-x)))
for w, wr in zip(c, ruc[::-1]):
    out = max(out, w+wr)
for w, wr in zip(rc, uc[::-1]):
    out = max(out, w+wr)
print(out)

Solution (Python):
N, C = map(int, input().split())
xy = [list(map(int, input().split())) for _ in range(N)]
c = [0]
rc = [0]
uc = [0]
ruc = [0]
sum = 0
out = 0
for x, cal in xy:
    sum += cal
    c.append(max(c[-1], sum-x))
    rc.append(max(rc[-1], sum-2*x))
sum = 0
for x, cal in xy[::-1]:
    sum += cal
    uc.append(max(uc[-1], sum-C+x))
    ruc.append(max(ruc[-1], sum-2*(C-x)))
for w, wr, ww, wwr in zip(c, ruc[::-1], rc, uc[::-1]):
    out = max(out, w + wr, ww + wwr)
print(out)

Solution (Python):
N, C = map(int, input().split())
xy = [list(map(int, input().split())) for _ in range(N)]
c = [0]
rc = [0]
uc = [0]
ruc = [0]
sum = 0
out = 0
for x, cal in xy:
    sum += cal
    c.append(max(c[-1], sum-x))
    rc.append(max(rc[-1], sum-2*x))
sum = 0
for x, cal in xy[::-1]:
    sum += cal
    uc.append(max(uc[-1], sum-C+x))
    ruc.append(max(ruc[-1], sum-2*(C-x)))
for w, wr in zip(c, ruc[::-1]):
    out = max(out, w+wr)
for w, wr in zip(rc, uc[::-1]):
    out = max(out, w+wr)
print(out)

Solution (Python):
N, C = map(int, input().split())
sushi = [tuple(map(int, input().split())) for _ in [0]*N]
normal = [sushi[0][1] - sushi[0][0]]
n_append = normal.append
cur = normal[0]
n_max = max(0, normal[0])
for (s1_pos, s1_cal), (s2_pos, s2_cal) in zip(sushi, sushi[1:]):
    cur = cur + s2_cal - (s2_pos-s1_pos)
    if n_max < cur:
        n_max = cur
    n_append(n_max)
     
reverse = [sushi[-1][1] - (C-sushi[-1][0])]
r_append = reverse.append
cur = reverse[0]
r_max = max(0, reverse[0])
for (s1_pos, s1_cal), (s2_pos, s2_cal) in zip(sushi[::-1], sushi[-2::-1]):
    cur = cur + s2_cal - (s1_pos-s2_pos)
    if r_max < cur:
        r_max = cur
    r_append(r_max)
     
result = 0
for i, (pos, cal) in enumerate(sushi[:-1]):
    result = max(result, normal[i], normal[i]-pos+reverse[N-2-i])
for i, (pos, cal) in enumerate(sushi[:0:-1]):
    result = max(result, reverse[i], reverse[i]-(C-pos)+normal[N-2-i])
result = max(result, normal[-1], reverse[-1])
     
print(result)

Solution (Python):

n,c=map(int,input().split())
lane_r=[]
lane_l=[]
for i in range(n):
    a,b=map(int,input().split())
    lane_r.append((a,b))
    lane_l.append((c-a,b))
lane_l=lane_l[::-1]
acm_lane_r=[0]
acm_lane_l=[0]
for i in range(n):
    acm_lane_r.append(acm_lane_r[-1]+lane_r[i][1])
    acm_lane_l.append(acm_lane_l[-1]+lane_l[i][1])
for i in range(1,n+1):
    acm_lane_r[i]-=lane_r[i-1][0]
    acm_lane_l[i]-=lane_l[i-1][0]
mxl=0
mxr=0
pl=0
pr=0
for i in range(n+1):
    if mxl<acm_lane_l[i]:
        mxl=acm_lane_l[i]
        pl=lane_l[i-1][0]
    acm_lane_l[i]=(mxl,pl)
    if mxr<acm_lane_r[i]:
        mxr=acm_lane_r[i]
        pr=lane_r[i-1][0]
    acm_lane_r[i]=(mxr,pr)
mx=0
lane_r.insert(0,(0,0))
lane_l.insert(0,(0,0))
for i in range(n+1):
    mx=max(acm_lane_r[n-i][0]+acm_lane_l[i][0]-min(acm_lane_r[n-i][1],acm_lane_l[i][1]),mx)
for i in range(n+1):
    mx=max(acm_lane_l[n-i][0]+acm_lane_r[i][0]-min(acm_lane_l[n-i][1],acm_lane_r[i][1]),mx)
print(mx)

Solution (Python):
n,c = (int(i) for i in input().split())
x = [[int(i) for i in input().split()] for i in range(n)]
a = 0
for i in x: a += i[1]
y,y2 = [max(0,x[0][1]-2*x[0][0])],x[0][1]
for i in range(1,n-1):
	y2 += x[i][1]
	y.append(max(y2-2*x[i][0],y[-1]))
z,z2 = [max(0,x[-1][1]-2*(c-x[-1][0]))],x[-1][1]
for i in range(n-2,0,-1):
	z2 += x[i][1]
	z.append(max(z2-2*(c-x[i][0]),z[-1]))
ans,sub = max(a-x[-1][0],a-(c-x[0][0]),0),0
z = z[::-1]
for i in range(n-1):
	sub += x[i][1]
	ans = max(sub-x[i][0]+z[i],ans)
sub = 0
for i in range(n-1,0,-1):
	sub += x[i][1]
	ans = max(sub-(c-x[i][0])+y[i-1],ans)
print(ans)

Solution (Python):



def inp(): return map(int, input().split())


N, C = inp()
l = [tuple(inp()) for _ in range(N)]

r1 = [0] * N
r2 = [0] * N
l1 = [0] * N
l2 = [0] * N

cal = 0
ans1 = 0
ans2 = 0
for i, (x, v) in enumerate(l):  
    cal += v
    ans1 = max(ans1, cal - x)
    ans2 = max(ans2, cal - 2 * x)
    r1[i] = ans1
    r2[i] = ans2

cal = 0
ans1 = 0
ans2 = 0
for i, (x, v) in enumerate(l[::-1]):  
    x = C - x
    cal += v
    ans1 = max(ans1, cal - x)
    ans2 = max(ans2, cal - 2 * x)
    l1[-1 - i] = ans1
    l2[-1 - i] = ans2

ans = 0

for i in range(N - 1):
    ans = max(ans, r2[i] + l1[i + 1], r1[i] + l2[i + 1])

ans = max(ans, r1[-1], l1[0])
print(ans)

Solution (Python):


N, C = map(int, input().split())
x_list = []
v_list = []
for i in range(N):
    xi, vi = map(int, input().split())
    x_list.append(xi)
    v_list.append(vi)

e_clock = [0] * N
e_counter = [0] * N
e_clock0 = [0] * N 
e_counter0 = [0] * N 





cal = 0
e = 0
e0 = 0
for i in range(N):
    cal += v_list[i]
    x = x_list[i]
    e = max(e, cal - x)
    e0 = max(e0, cal - 2*x)

    e_clock[i] = e
    e_clock0[i] = e0



cal = 0
e = 0
e0 = 0
for i in range(N-1, -1, -1):
    cal += v_list[i]
    x = C - x_list[i]
    e = max(e, cal - x)
    e0 = max(e0, cal - 2*x)
    e_counter[i] = e
    e_counter0[i] = e0

ans = 0

for i in range(N-1):
    
    e1 = e_clock0[i] + e_counter[i+1]
    
    e2 = e_counter0[i+1] + e_clock[i]

    ans = max(ans, e1, e2)



ans = max(ans, e_clock[N-1], e_counter[0])

print(ans)

Solution (Python):
def main():
    """
    1 <= N <= 10^5
    2 <= C <= 10^14
    1 <= x1 < x2 < ... < xN < C
    1 <= vi <= 10^9
    """
    N, C = map(int, input().split())
    X, V = zip(*(
        map(int, input().split())
        for _ in range(N)
    ))

    
    editorial(N, C, X, V)


def editorial(N, C, X, V):
    rev_V = V[::-1]
    rev_X = tuple(map(lambda x: C-x, X[::-1]))

    a1 = solve_editorial(N, C, X, V)
    a2 = solve_editorial(N, C, rev_X, rev_V)
    ans = max(a1, a2)
    print(ans)


def solve_editorial(N, C, X, V):
    ans = 0
    
    
    c1 = [0] * N
    c1[0] = V[0]
    for i in range(1, N):
        c1[i] = V[i] + c1[i-1]
    
    d1 = [0] * N
    for i in range(N):
        
        ans = max(c1[i] - X[i], ans)
        d1[i] = max(c1[i] - X[i] * 2, 0)
    
    e1 = [0] * N
    e1[0] = d1[0]
    for i in range(1, N):
        e1[i] = max(d1[i], e1[i-1])

    
    rev_V = V[::-1]
    rev_X = tuple(map(lambda x: C-x, X[::-1]))
    c2 = [0] * N
    c2[0] = rev_V[0]
    for i in range(1, N):
        c2[i] = rev_V[i] + c2[i-1]

    
    for i in range(N-1):
        y = max(c2[i] - rev_X[i], 0)
        ans = max(ans, e1[N-i-2] + y)

    return ans


def part_point(N, C, X, V):
    """
    円周すべてを通るパターンは最適ではない
        xN < C より
    時計回りにある寿司Aまで
    そこから初期位置まで
    最後に反時計回りにBまで
        OA * 2 + OB or
        OB * 2 + OA
    """
    
    rev_V = V[::-1]
    rev_X = tuple(map(lambda x: C-x, X[::-1]))

    
    a1 = solve_p(N, X, V, rev_X, rev_V)
    a2 = solve_p(N, rev_X, rev_V, X, V)

    ans = max(a1, a2)
    print(ans)


def solve_p(N, X, V, rev_X, rev_V):
    ans = 0
    for i in range(0, N):
        
        a = sum(V[:i+1]) - X[i]
        ans = max(ans, a)
        
        a = max(a - X[i], 0)
        for j in range(N-i-1):
            
            b = a + sum(rev_V[:j+1]) - rev_X[j]
            ans = max(ans, b)

    return ans


if __name__ == '__main__':
    main()

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


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

    reverse = [sushi[-1][1] - (C-sushi[-1][0])]
    r_append = reverse.append
    cur = reverse[0]
    r_max = max(0, reverse[0])
    for (s1_pos, s1_cal), (s2_pos, s2_cal) in zip(sushi[::-1], sushi[-2::-1]):
        cur += s2_cal - (s1_pos-s2_pos)
        if r_max < cur:
            r_max = cur
        r_append(r_max)

    result= 0
    for i, (pos, cal) in enumerate(sushi[:-1]):
        result = max(result, normal[i], normal[i]-pos+reverse[N-2-i])
    for i, (pos, cal) in enumerate(sushi[:0:-1]):
        result = max(result, reverse[i], reverse[i]-(C-pos)+normal[N-2-i])
    result = max(result, normal[-1], reverse[-1])

    print(result)


if __name__ == '__main__':
    solve()

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

pre_mx = [(0,0)]
suf_mx = [(0,0)]
run_psum = 0
run_ssum = 0
data = []
for i in range(N):
    x,v = map(int,input().split())
    if run_psum + v - x > pre_mx[i][1]:
        pre_mx.append((x,run_psum + v - x))
    else:
        pre_mx.append((pre_mx[i][0],pre_mx[i][1]))
    run_psum += v
    data.append((x,v))

for i in range(N-1,-1,-1):
    if run_ssum + data[i][1] - C + data[i][0] > suf_mx[N-1-i][1]:
        suf_mx.append((C - data[i][0],run_ssum + data[i][1] - C + data[i][0]))
    else:
        suf_mx.append((suf_mx[N-1-i][0],suf_mx[N-1-i][1]))
    run_ssum += data[i][1]

ansp = anss = 0
for i in range(N+1):
    if ansp < pre_mx[i][1] + suf_mx[-i-1][1] - pre_mx[i][0]:
        ansp = pre_mx[i][1] + suf_mx[-i-1][1]
        if suf_mx[-i-1][0] != 0:
            ansp -= pre_mx[i][0]
            
    if anss < suf_mx[i][1] + pre_mx[-i-1][1] - suf_mx[i][0]:
        anss = suf_mx[i][1] + pre_mx[-i-1][1]
        if pre_mx[-i-1][0] != 0:
            anss -= suf_mx[i][0]

print(max(ansp,anss))

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

R1 = [0]*N
R2 = [0]*N
L1 = [0]*N
L2 = [0]*N

cal = 0
ans1 = 0
ans2 = 0
for i, (x, v) in enumerate(xv):
    cal += v
    ans1 = max(ans1, cal-x)
    ans2 = max(ans2, cal-2*x)
    R1[i] = ans1
    R2[i] = ans2


cal = 0
ans1 = 0
ans2 = 0
for i, (x, v) in enumerate(xv[::-1]):
    x = C-x
    cal += v
    ans1 = max(ans1, cal-x)
    ans2 = max(ans2, cal-2*x)
    L1[-1-i] = ans1
    L2[-1-i] = ans2

ans = 0

for i in range(N-1):
    ans = max(ans, R2[i]+L1[i+1], R1[i]+L2[i+1])

ans = max(ans, R1[-1], L1[0])
print(ans)

Solution (Python):
n, c = [int(i) for i in input().split()]
m = [[int(j) for j in input().split()] for i in range(n)]

sum = 0
r = [0]
for x, v in m:
    sum += v
    r.append(sum-x)

sum = 0
l = [0]
for x, v in m[::-1]:
    sum += v
    l.append(sum-(c-x))

def right():
    max_ = r[0]
    for i in r:
        if max_ < i:
            max_ = i
        yield max_

def left():
    max_ = l[0]
    for i in l:
        if max_ < i:
            max_ = i
        yield max_

rm = [[0, 0]] + m
lm = m + [[c, c]]

rpath = list(right())
lmax = max([l[i]+rpath[-(i+1)]-(c-lm[-(i+1)][0]) for i in range(n)])

lpath = list(left())
rmax = max([r[i]+lpath[-(i+1)]-rm[i][0] for i in range(n)])


print(max(rmax, lmax))

Solution (Python):
def d_static_sushi(N, C, Sushi):
    sushi_pos_cw = [row[0] for row in Sushi]
    sushi_pos_ccw = [abs(row[0] - C) for row in Sushi][::-1]
    sushi_value_cw = [row[1] for row in Sushi]
    sushi_value_ccw = sushi_value_cw[::-1]

    
    f1,g1,f2,g2=[],[],[],[]
    s1,s2=0,0
    for i in range(N):
        s1+=sushi_value_cw[i]-sushi_pos_cw[i]
        f1.append(s1)
        g1.append(s1)
        s1+=sushi_pos_cw[i]
        s2+=sushi_value_ccw[i]-sushi_pos_ccw[i]
        f2.append(s2)
        g2.append(s2)
        s2+=sushi_pos_ccw[i]
    for i in range(1,N):
        if g1[i]<g1[i-1]:
            g1[i]=g1[i-1]
        if g2[i]<g2[i-1]:
            g2[i]=g2[i-1]
    
    
    
    
    
    
    
    tmp1 = 0
    for b in range(N + 1):  
        if b == 0:
            cal = g1[N - 1]
        elif b == N:
            cal = f2[b - 1]
        else:
            cal = g1[N - b - 1] + f2[b - 1] - sushi_pos_ccw[b - 1]
        tmp1 = max(tmp1, cal)

    tmp2 = 0
    for a in range(N + 1):  
        if a == 0:
            cal = g2[N - 1]
        elif a == N:
            cal = f1[a - 1]
        else:
            cal = g2[N - a - 1] + f1[a - 1] - sushi_pos_cw[a - 1]
        tmp2 = max(tmp2, cal)
    ans = max(tmp1, tmp2)
    return ans

N,C = [int(i) for i in input().split()]
Sushi = [[int(i) for i in input().split()] for j in range(N)]
print(d_static_sushi(N, C, Sushi))

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

pre_mx = [(0,0)]
suf_mx = [(0,0)]
run_psum = 0
run_ssum = 0
data = []
for i in range(N):
    x,v = map(int,input().split())
    if run_psum + v - x > pre_mx[i][1]:
        pre_mx.append((x,run_psum + v - x))
    else:
        pre_mx.append((pre_mx[i][0],pre_mx[i][1]))
    run_psum += v
    data.append((x,v))

for i in range(N-1,-1,-1):
    if run_ssum + data[i][1] - C + data[i][0] > suf_mx[N-1-i][1]:
        suf_mx.append((C - data[i][0],run_ssum + data[i][1] - C + data[i][0]))
    else:
        suf_mx.append((suf_mx[N-1-i][0],suf_mx[N-1-i][1]))
    run_ssum += data[i][1]

ansp = anss = 0
for i in range(N+1):
    if ansp < pre_mx[i][1] + suf_mx[-i-1][1] - pre_mx[i][0]:
        ansp = pre_mx[i][1] + suf_mx[-i-1][1]
        if suf_mx[-i-1][0] != 0:
            ansp -= pre_mx[i][0]

for i in range(N+1):
    if anss < suf_mx[i][1] + pre_mx[-i-1][1] - suf_mx[i][0]:
        anss = suf_mx[i][1] + pre_mx[-i-1][1]
        if pre_mx[-i-1][0] != 0:
            anss -= suf_mx[i][0]

print(max(ansp,anss))

Solution (Python):
N,C = map(int, input().split())
xv = []
for _ in range(N):
    xv.append(tuple(map(int, input().split())))
ccw_first = [0]
ccw_second = [0]
cw_first = [0]
cw_second = [0]
sm = 0
for x,v in xv:
    sm += v
    ccw_first.append(max(ccw_first[-1], sm - 2 * x))
    ccw_second.append(max(ccw_second[-1], sm - x))
sm = 0
for x,v in reversed(xv):
    sm += v
    cw_first.append(max(cw_first[-1], sm - 2 * (C - x)))
    cw_second.append(max(cw_first[-1], sm - (C - x)))
ans = 0
for i in range(N+1):
    ans = max(ans, cw_first[i] + ccw_second[N-i], ccw_first[i] + cw_second[N-i])
print(ans)

Solution (Python):
n,c=[int(i) for i in input().split()]
x=[[0 for i in range(2)] for j in range(n)]
for i in range(n):
    x[i]= [int(i) for i in input().split()]
x.sort(key=lambda x:x[0])

state=0

left_wa=[0 for i in range(n)]
right_wa=[0 for i in range(n)]

for i in range(n):
    if i==0:
        left_wa[i]=x[i][1]-abs(x[i][0]-state)
    else:
        left_wa[i]=left_wa[i-1]+x[i][1]-abs(x[i][0]-state)
    state=x[i][0]

state=c

for i in range(n):
    if i==0:
        right_wa[i]=x[n-1-i][1]-abs(x[n-1-i][0]-state)
    else:
        right_wa[i]= right_wa[i-1]+x[n-1-i][1]-abs(abs(c-x[n-1-i][0])-abs(state))
    state=c-x[n-1-i][0]

kcal=0

m_right=[0 for i in range(n+1)]
m_left=[0 for i in range(n+1)]

for i in range(n+1):
    if i!=0:
        m_right[i]=max(m_right[i-1], right_wa[i-1])
        m_left[i]=max(m_left[i-1], left_wa[i-1])



for i in range(n):
    if i!=n-1:

        right= m_right[n-1-i]-x[i][0]
    else: right=0
    kcal=max(kcal, left_wa[i]+max(0, right))

for i in range(n):
    if i!=n-1:

       left=m_left[n-1-i]-abs(c-x[n-i-1][0])

    else: left=0
    kcal=max(kcal, right_wa[i]+max(0, left))


print(kcal)

Solution (Python):
if __name__ == '__main__':
    N, C = map(int, input().split())
    values = [list(map(int, input().split())) for _ in range(N)]
    xs, vs = zip(*values)

    
    clock_cum = []
    clock_max = []
    aclock_cum = []
    aclock_max = []
    for i in range(N):
        if i == 0:
            cum_i = vs[i] - xs[i]
            max_i = max(0, cum_i)

            acum_i = vs[N - i - 1] - (C - xs[N - i - 1])
            amax_i = max(0, acum_i)
        else:
            cum_i = clock_cum[-1] + xs[i - 1] + vs[i] - xs[i]
            max_i = max(clock_max[-1], cum_i)

            acum_i = aclock_cum[-1] + (C - xs[N - i - 1 + 1]) + vs[N - i - 1] - (C - xs[N - i - 1])
            amax_i = max(aclock_max[-1], acum_i)

        clock_cum.append(cum_i)
        clock_max.append(max_i)
        aclock_cum.append(acum_i)
        aclock_max.append(amax_i)

    best = 0
    for i in range(N):
        if i != N - 1:
            
            best = max(best, clock_cum[i],
                       clock_cum[i] - xs[i] + aclock_max[N - i - 2])
            
            best = max(best, aclock_cum[i],
                       aclock_cum[i] - (C - xs[N - i - 1]) + clock_max[N - i - 2])
        else:
            best = max(best, clock_cum[i])
            best = max(best, aclock_cum[i])

    print(best)

Solution (Python):
if __name__ == '__main__':
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    N, C = map(int, input().split())
    values = [list(map(int, input().split())) for _ in range(N)]
    xs, vs = zip(*values)

    clock_cum = []
    clock_max = []
    aclock_cum = []
    aclock_max = []
    for i in range(N):
        if i == 0:
            cum_i = vs[i] - xs[i]
            max_i = max(0, cum_i)

            acum_i = vs[N - i - 1] - (C - xs[N - i - 1])
            amax_i = max(0, acum_i)
        else:
            cum_i = clock_cum[-1] + xs[i - 1] + vs[i] - xs[i]
            max_i = max(clock_max[-1], cum_i)

            acum_i = aclock_cum[-1] + (C - xs[N - i - 1 + 1]) + vs[N - i - 1] - (C - xs[N - i - 1])
            amax_i = max(aclock_max[-1], acum_i)

        clock_cum.append(cum_i)
        clock_max.append(max_i)
        aclock_cum.append(acum_i)
        aclock_max.append(amax_i)

    best = 0
    for i in range(N):
        if i != N - 1:
            
            best = max(best, clock_cum[i],
                       clock_cum[i] - xs[i] + aclock_max[N - i - 2])
            
            best = max(best, aclock_cum[i],
                       aclock_cum[i] - (C - xs[N - i - 1]) + clock_max[N - i - 2])
        else:
            best = max(best, clock_cum[i])
            best = max(best, aclock_cum[i])

    print(best)

Solution (Python):
n, c = map(int, input().split())
X = [0] * n
V = [0] * n
right = [0] * n
left = [0] * n
rl = [0] * n
lr = [0] * n
rlm = [0] * n
lrm = [0] * n




calsum = 0
for i in range(n):
    X[i], V[i] = map(int, input().split())
    calsum += V[i]
    right[i] = calsum - X[i]
    rl[i] = calsum - 2 * X[i]

calsum = 0
for i in range(n):
    calsum += V[n - i - 1]
    left[i] = calsum - (c - X[n - i - 1])
    lr[i] = calsum - 2 * (c - X[n - i - 1])

m = 0
for i in range(0, n - 1):
    m = max(m, left[i])
    rlm[i] = m + rl[n - i - 2]

m = 0
for i in range(0, n - 1):
    m = max(m, right[i])
    lrm[i] = m + lr[n - i - 2]

print(max(0, max(right), max(left), max(rlm), max(lrm)))

Solution (Python):
n, c = map(int, input().split())
X = [0 for _ in range(n)]
V = [0 for _ in range(n)]
right = [0 for _ in range(n)]
left = [0 for _ in range(n)]
rl = [0 for _ in range(n)]
lr = [0 for _ in range(n)]
rlm = [0 for _ in range(n)]
lrm = [0 for _ in range(n)]

calsum = 0
for i in range(n):
    x, v = map(int, input().split())
    X[i] = x
    V[i] = v
    calsum += v
    right[i] = calsum-x
    rl[i] = calsum-2*x

calsum = 0
for i in range(n):
    calsum += V[n-i-1]
    left[i] = calsum - (c-X[n-i-1])
    lr[i] = calsum - 2*(c-X[n-i-1])

m = 0
for i in range(0, n-1):
    m = max(m, left[i])
    rlm[i] = m + rl[n-i-2]

m = 0
for i in range(0, n-1):
    m = max(m, right[i])
    lrm[i] = m + lr[n-i-2]

print(max(0, max(right), max(left), max(rlm), max(lrm)))

Solution (Python):
n, c = map(int, input().split())
X = [0 for _ in range(n)]
V = [0 for _ in range(n)]
right = [0 for _ in range(n)]
left = [0 for _ in range(n)]
rl = [0 for _ in range(n)]
lr = [0 for _ in range(n)]

calsum = 0
for i in range(n):
    x, v = map(int, input().split())
    X[i] = x
    V[i] = v
    calsum += v
    right[i] = calsum-x
    rl[i] = calsum-2*x

calsum = 0
for i in range(n):
    calsum += V[n-i-1]
    left[i] = calsum - (c-X[n-i-1])
    lr[i] = calsum - 2*(c-X[n-i-1])

M = max(0, max(right), max(left))
m = 0
for i in range(0, n-1):
    m = max(m, left[i])
    M = max(M, m + rl[n-i-2])

m = 0
for i in range(0, n-1):
    m = max(m, right[i])
    M = max(M, m + lr[n-i-2])

print(M)

Solution (Python):
def solve(xv_sorted):
    x = [False for _ in range(n)]
    y = [False for _ in range(n)]

    s = 0
    buf = (- 1) * float('inf')
    for i in range(n):
        s += xv_sorted[i][1]
        x[i] = max(buf, s - 2 * xv_sorted[i][0])
        buf = x[i]

    

    s = 0
    buf = (- 1) * float('inf')
    for i in sorted(range(n), reverse=True):
        s += xv_sorted[i][1]
        y[i] = max(buf, s - (c - xv_sorted[i][0]))
        buf = y[i]

    

    ans = max(x[n - 1], y[0])
    for i in range(n - 1):
        ans = max(ans, x[i] + y[i + 1])

    return ans

if __name__ == '__main__':
    n, c = map(int, input().split())
    xv = []
    for i in range(n):
        xv.append(list(map(int, input().split())))
    xv_sorted = sorted(xv, key=lambda x:x[0])
    ans = 0
    ans = max(ans, solve(xv_sorted))

    for i in range(n):
        xv_sorted[i][0] = abs(c - xv_sorted[i][0]) 
    xv_sorted = sorted(xv_sorted, key=lambda x:x[0])
    ans = max(ans, solve(xv_sorted))
    print(ans)

Solution (Python):
def solve(xv_sorted):
    x = [False for _ in range(n)]
    y = [False for _ in range(n)]

    s = 0
    buf = (- 1) * float('inf')
    for i in range(n):
        s += xv_sorted[i][1]
        x[i] = max(buf, s - 2 * xv_sorted[i][0])
        buf = x[i]

    

    s = 0
    buf = (- 1) * float('inf')
    for i in sorted(range(n), reverse=True):
        s += xv_sorted[i][1]
        y[i] = max(buf, s - (c - xv_sorted[i][0]))
        buf = y[i]

    

    ans = max(x[n - 1], y[0])
    for i in range(n - 1):
        ans = max(ans, x[i] + y[i + 1])

    return ans

if __name__ == '__main__':
    n, c = map(int, input().split())
    xv = []
    for i in range(n):
        xv.append(list(map(int, input().split())))
    xv_sorted = sorted(xv, key=lambda x:x[0])
    ans = 0
    ans = max(ans, solve(xv_sorted))

    for i in range(n):
        xv_sorted[i][0] = abs(c - xv_sorted[i][0]) 
    xv_sorted = sorted(xv, key=lambda x:x[0])
    ans = max(ans, solve(xv_sorted))
    print(ans)

Solution (Python):
n, c = map(int, input().split())
X, V, right, left, rl, lr = [], [], [], [], [], []

calsum = 0
for _ in range(n):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v)
    calsum += v
    right.append(calsum-x)
    rl.append(calsum-2*x)

calsum = 0
for i in range(n):
    calsum += V[n-i-1]
    left.append(calsum - (c-X[n-i-1]))
    lr.append(calsum - 2*(c-X[n-i-1]))

M = max(0, max(right), max(left))
m = 0
for i in range(0, n-1):
    m = max(m, left[i])
    M = max(M, m + rl[n-i-2])

m = 0
for i in range(0, n-1):
    m = max(m, right[i])
    M = max(M, m + lr[n-i-2])

print(M)

Solution (Python):
n, c = map(int, input().split())
X = []
V = []
right = []
rightleft = []
calsum = 0
for i in range(n):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v)
    calsum += v
    right.append(calsum-x)
    rightleft.append(calsum-2*x)

left = []
leftright = []
calsum = 0
for i in range(n):
    calsum += V[n-i-1]
    left.append(calsum - (c-X[n-i-1]))
    leftright.append(calsum - 2*(c-X[n-i-1]))

m = 0
M = 0
for i in range(0, n-1):
    m = max(m, left[i])
    M = max(M, m + rightleft[n-i-2])

mm = 0
MM = 0
for i in range(0, n-1):
    mm = max(mm, right[i])
    MM = max(MM, mm + leftright[n-i-2])

print(max(max(right), max(left), M, MM, 0))

Solution (Python):



n, c = map(int, input().split())


xs = []
vs = []
for _i in range(n):
    x, v = map(int, input().split())
    xs.append(x)
    vs.append(v)

r1, r2, l1, l2 = [[], [], [], []]
r_sum, l_sum = [0, 0]
for i in range(n):
    r_sum += vs[i]
    r1.append(r_sum - xs[i])
    r2.append(r_sum - 2 * xs[i])

    i_rev = n - i - 1
    l_sum += vs[i_rev]
    l1.append(l_sum - (c - xs[i_rev]))
    l2.append(l_sum - 2 * (c - xs[i_rev]))

r1_max, r2_max = [0, 0]
l1_max, l2_max = [0, 0]
r1_maxs, r2_maxs = [[0], [0]]
l1_maxs, l2_maxs = [[0], [0]]
for i in range(n):
    r1_max = max(r1_max, r1[i])
    r1_maxs.append(r1_max)
    r2_max = max(r2_max, r2[i])
    r2_maxs.append(r2_max)
    l1_max = max(l1_max, l1[i])
    l1_maxs.append(l1_max)
    l2_max = max(l2_max, l2[i])
    l2_maxs.append(l2_max)

r1_l2_max = 0
l1_r2_max = 0
for i in range(0, n + 1):
    r1_l2_max = max(r1_l2_max, r1_maxs[i] + l2_maxs[n - i])
    l1_r2_max = max(l1_r2_max, l1_maxs[i] + r2_maxs[n - i])

total_max = max(r1_l2_max, l1_r2_max)


print("{}".format(total_max))

Solution (Python):
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)
right = []
right2 = []
left = []
count = 0
left2 = []
for i in range(n):
  if i == 0:
    count += (V[i] - X[i])
    right.append(count)
    right2.append(count)
  else:
    count += (V[i] - (X[i] - X[i - 1]))
    right.append(count)
    right2.append(max(right2[-1],count))

count = 0
for i in range(n):
  if i == 0:
    count += (V[(i + 1) * (-1)] - (c - X[(i + 1) * (-1)]))
    left.append(count)
    left2.append(count)
  else:
    count += (V[(i + 1) * (-1)] - ((X[i * (-1)] - X[((i + 1) * (-1))])))
    left.append(count)
    left2.append(max(left2[-1],count))
ans = max(0,max(right),max(left))
for i in range(n - 1):
  ans = max(ans, right[i] - X[i] + left2[n-i-2])
for i in range(n - 1):
  ans = max(ans, left[i] - (c - X[(i + 1) * (-1)]) + right2[n-i-2])
print(ans)