Problem:


Score : 500 points

Problem Statement

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

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

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

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

Constraints

1 ≤ N ≤ 10^5

2 ≤ C ≤ 10^{14}

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

1 ≤ v_i ≤ 10^9

All values in input are integers.

Subscores

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

Input

Input is given from Standard Input in the following format:

N C
x_1 v_1
x_2 v_2
:
x_N v_N

Output

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

Sample Input 1

3 20
2 80
9 120
16 1

Sample Output 1

191

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

Sample Input 2

3 20
2 80
9 1
16 120

Sample Output 2

192

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

Sample Input 3

1 100000000000000
50000000000000 1

Sample Output 3

0

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

Sample Input 4

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

Sample Output 4

6500000000

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



Sample input:
3 20
2 80
9 120
16 1


Sample output:
191

Submission status: Accepted

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


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


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


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


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

print(answer)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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]
    #print(r1, r2)
    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):
    #print('hogehoge',i,V[i-1],X[i-1])
    sum += V[i-1]
    #print(l1, l2)
    l1.append(max(l1[-1], sum-(C-X[i-1])))
    l2.append(max(l2[-1], sum-2*(C-X[i-1])))
#print(l1, l2)
a = max(np.array(r1) + np.array(l2)[::-1])
b = max(np.array(r2) + np.array(l1)[::-1])
print(max(a,b))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# kopipe
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#abc095d

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

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))
#配列の静的確保ver
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)] ))

Submission status: Accepted

Solution code:
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]
    #3項演算子ver
    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)
    #vl1.append(max(vl1[i],suml - x[i]))
    #vl2.append(max(vl2[i],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)
    #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)] ))

Submission status: Accepted

Solution code:
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)
    vl1.append(max(vl1[i],suml - x[i]))
    vl2.append(max(vl2[i],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)
    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)] ))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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]
#これにて準備完了。あとは折り返し計算も含めてひたすらsetに投げ込むだけ。
#まずは時計回りの場合。
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)))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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])))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# coding: utf-8
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)

Submission status: Accepted

Solution code:
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)