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())
xv = []
for _ in range(n):
  xv.append(tuple(map(int, input().split())))

af = [0]
prevx = 0
for x, v in xv:
  af.append(af[-1] + v - x + prevx)
  prevx = x

bf = [0]
prevx = 0
for x, v in xv[::-1]:
  x = c - x
  bf.append(bf[-1] + v - x + prevx)
  prevx = x

maxa = [0]
for _af in af[1:]:
  maxa.append(max(maxa[-1], _af))
maxb = [0]
for _bf in bf[1:]:
  maxb.append(max(maxb[-1], _bf))

ans = 0
for i, _af in enumerate(af):
  if _af + maxb[n - i] - (xv[i - 1][0] if i != 0 else 0) > ans:
    ans = _af + maxb[n - i] - (xv[i - 1][0] if i != 0 else 0)
for i, _bf in enumerate(bf):
  if _bf + maxa[n - i] - (c - xv[n - i][0] if i != 0 else 0) > ans:
    ans = _bf + maxa[n - i] - (c - xv[n - i][0] if i != 0 else 0)
print(ans)

Submission status: Accepted

Solution code:
n, c = map(int, input().split())
xv = [list(map(int, input().split())) for i in range(n)]
dx = []
dy = []
for i in range(n):
    if i == 0:
        dx.append(xv[i][0])
        dy.append(c - xv[-i-1][0])
    else:
        dx.append(xv[i][0]-xv[i-1][0])
        dy.append(-xv[-i-1][0] + xv[-i][0])
right = [0]
left = [0]
for i in range(n):
    r = right[-1] + xv[i][1] - dx[i]
    l = left[-1] + xv[-i-1][1] - dy[i]
    right.append(r)
    left.append(l)
for i in range(1,n+1):
    right[i] = max(right[i-1], right[i])
    left[i] = max(left[i-1], left[i])

ans = 0
for i in range(1,n+1):
    a1 = right[i]
    a2 = right[i] - xv[i-1][0] + left[n-i]
    a3 = left[i]
    a4 = left[i] - (c - xv[-i][0]) + right[n-i]
    ans = max(ans,a1,a2,a3,a4)
print(ans)

Submission status: Accepted

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
N, C = map(int, input().split())
sushis = [list(map(int, input().split())) for _ in range(N)]

# Aを時計回り,Bを反時計回りとする
# O->Aと0->A->Oのコストを計算
# O->Aについてval(O->A[i]) > val(O->A[i+1])の時、i+1に行くよりiで止めた方が得.
# それをtmpMaxでごにょごにょしてる
valA = []
valArev = []
sumV = 0
tmpMax = -float('inf')

for d, v in sushis:
    sumV += v
    valArev.append(sumV-2*d)
    tmpMax = max(tmpMax, sumV-d)
    valA.append(tmpMax)

valB = []
valBrev = []
sumV = 0
tmpMax = -float('inf')
for d, v in reversed(sushis):
    sumV += v
    revD = C-d
    valBrev.append(sumV-2*revD)
    tmpMax = max(tmpMax, sumV-revD)
    valB.append(tmpMax)


# 折り返しなしの場合の値
ans = max(valA+valB+[0])
# O->A->O->B
# i=N-1の時は折り返しなしの場合と同じだから、省く
for i in range(N-1):
    ans = max(ans, valArev[i]+valB[N-i-2])  # 被った寿司を取らないようにvalB[N-i-2])

# O->B->O->A
for i in range(N-1):
    ans = max(ans, valBrev[i]+valA[N-i-2])
print(ans)

Submission status: Accepted

Solution code:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
N, C = map(int, input().split())
sushis = [list(map(int, input().split())) for _ in range(N)]

# Aを時計回り,Bを反時計回りとする
# O->Aと0->A->Oのコストを計算
valA = []
valArev = []
sumV = 0
tmpMax = -float('inf')

for d, v in sushis:
    sumV += v
    valArev.append(sumV-2*d)
    tmpMax = max(tmpMax, sumV-d)
    valA.append(tmpMax)

valB = []
valBrev = []
sumV = 0
tmpMax = -float('inf')
for d, v in reversed(sushis):
    sumV += v
    revD = C-d
    valBrev.append(sumV-2*revD)
    tmpMax = max(tmpMax, sumV-revD)
    valB.append(tmpMax)


# 折り返しなしの場合の値
ans = max(valA+valB+[0])
# O->A->O->B
for i in range(N-1):
    ans = max(ans, valArev[i]+valB[N-i-2])

# O->B->O->A
for i in range(N-1):
    ans = max(ans, valBrev[i]+valA[N-i-2])
print(ans)

Submission status: Accepted

Solution code:
n,c = map(int,input().split())
x = [0]
v = [0]
rf = [0] * (n+2)
rg = [0] * (n+2)
lf = [0] * (n+2)
lg = [0] * (n+2)
for i in range(0,n):
    X,V = map(int,input().split())
    x.append(X)
    v.append(V)
for i in range(0,n):
    rf[i+1] = rf[i] + v[i+1]
    rg[i+1] = max(rg[i],rf[i+1] - x[i+1])
for i in range(n+1,1,-1):
    lf[i-1] = lf[i] + v[i-1]
    lg[i-1] = max(lg[i],lf[i-1] - (c-x[i-1]))

ans = max(rg[n],lg[1])
for i in range(1,n):
    ans = max(ans,rg[i]+lg[i+1] - x[i],rg[i]+lg[i+1] - (c - x[i+1]))
print(ans)

Submission status: Accepted

Solution code:
n,c=map(int, input().split())
a=[[0,0]]+[list(map(int, input().split())) for i in range(n)]+[[c,0]]
m=[0]
h=[0]
mb=[0]
hb=[0]
for i in range(n+1):
    m.append(m[-1]-a[i+1][0]+a[i][0]+a[i+1][1])
    mb.append(m[-2]-a[i+1][0]+a[i][0]+a[i+1][1]-a[i+1][0])
    h.append(h[-1]-a[-i-1][0]+a[-i-2][0]+a[-i-2][1])
    hb.append(h[-2]-a[-i-1][0]+a[-i-2][0]+a[-i-2][1]-c+a[-i-2][0])

mm=[0]
for i in range(n):
    mm.append(max(mm[-1],m[i+1]))
hm=[0]
for i in range(n):
    hm.append(max(hm[-1],h[i+1]))
    
ans=max(hm[-1],mm[-1])
for i in range(1,n):
    ans=max(hb[i]+mm[-i-1], mb[i]+hm[-i-1],ans)
print(ans)

Submission status: Accepted

Solution code:
n,c=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]

rev=[None]*(n+1)
norm=[None]*(n+1)
revmax=[None]*(n+1)

rev[0]=(0,0)
revmax[0]=(0,0)
norm[0]=(0,0)
ans=0

# normal rotation
for i in range(1,n+1):
    x,v=l[i-1]
    norm[i]=(x,v+norm[i-1][1])

# reverse rotation
for i in range(1,n+1):
    x,v=l[-i]
    rev[-i]=(c-x,v+rev[-i+1][1])

for i in range(1,n+1):
    revmax[-i]=(max(revmax[-i+1][0],rev[-i][1]-2*rev[-i][0]),max(revmax[-i+1][1],rev[-i][1]-rev[-i][0]))

for i in range(n+1):
    v1=norm[i][1]-norm[i][0]+revmax[i-n][0]
    v2=norm[i][1]-2*norm[i][0]+revmax[i-n][1]
    ans=max(ans,v1,v2)

print(ans)

Submission status: Accepted

Solution code:
n,c=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]

rev=[None]*(n+1)
norm=[None]*(n+1)
revmax=[None]*(n+1)

rev[0]=(0,0)
revmax[0]=(0,0)
norm[0]=(0,0)
ans=0

# normal rotation
for i in range(1,n+1):
    x,v=l[i-1]
    norm[i]=(x,v+norm[i-1][1])

# reverse rotation
for i in range(1,n+1)[::-1]:
    x,v=l[i-1]
    if i==n:
        rev[i]=(c-x,v)
    else:
        rev[i]=(c-x,v+rev[i+1][1])

for i in range(1,n+1):
    revmax[-i]=(max(revmax[-i+1][0],rev[-i][1]-2*rev[-i][0]),max(revmax[-i+1][1],rev[-i][1]-rev[-i][0]))

for i in range(n+1):
    v1=norm[i][1]-norm[i][0]+revmax[i-n][0]
    v2=norm[i][1]-2*norm[i][0]+revmax[i-n][1]
    ans=max(ans,v1,v2)

print(ans)

Submission status: Accepted

Solution code:
n,c = map(int,input().split())
x = []
v = []
for i in range(n):
    xx,vv = map(int,input().split())
    x.append(xx)
    v.append(vv)

a = [v[0]-x[0]] #a[i]:始点から時計回りにx[i]までいった時の収支
for i in range(1,n):
    a.append(a[-1]+v[i] - x[i]+x[i-1])

b = [v[-1]-c+x[-1]] #反時計回り
for i in range(1,n):
    b.append(b[-1]+v[n-1-i]-x[n-i]+x[n-1-i])



maxa = [a[0]]
maxb = [b[0]]
for i in range(1,n):
    maxa.append(max(maxa[-1],a[i]))
    maxb.append(max(maxb[-1],b[i]))

ass = 0
res = [0]
for i in range(n):
    if i != n-1:
        ass = a[i]+max(0,maxb[n-2-i]-x[i])
    else:
        ass = a[i]
    res.append(ass)



for i in range(n):
    if i != n-1:
        ass = b[i]+max(0,maxa[n-2-i]-(c-x[n-1-i]))
    else:
        ass = b[i]
    res.append(ass)
print(max(res))

Submission status: Accepted

Solution code:
N, C = map(int, input().split())
Sushi = [[int(info) for info in input().split()] for n in range(N)]
Clocksum = [0 for i in range(N+1)]
AntiClocksum = [0 for i in range(N+1)]
maxClocksum, maxAntisum = [0 for i in range(N+1)], [0 for i in range(N+1)]
for i in range(N): #一方向にのみ進む
    Clocksum[i+1] = Clocksum[i] + Sushi[i][1] - Sushi[i][0] 
    AntiClocksum[i+1] = AntiClocksum[i] + Sushi[N-i-1][1] - (C - Sushi[N-i-1][0])
    if i > 0:
        Clocksum[i+1] += Sushi[i-1][0]
        AntiClocksum[i+1] += C - Sushi[N-i][0]
    maxClocksum[i+1] = max(maxClocksum[i], Clocksum[i+1])
    maxAntisum[i+1] = max(maxAntisum[i], AntiClocksum[i+1])
maxCalorie = max(max(Clocksum), max(AntiClocksum))

#途中で折り返す
ClockToAnticlock = 0
AntiClockToClock = 0
for i in range(N-1):
    ClockToAnticlock = max(Clocksum[i+1] + maxAntisum[N-1-i] - Sushi[i][0], ClockToAnticlock)
    AntiClockToClock = max(AntiClocksum[i+1] + maxClocksum[N-1-i] - (C - Sushi[N-i-1][0]), AntiClockToClock)

print(max(maxCalorie, max(ClockToAnticlock, AntiClockToClock)))

Submission status: Accepted

Solution code:
N, C = [int(_) for _ in input().split()]
X = [0] * N
V = [0] * N
for i in range(N):
    X[i], V[i] = [int(_) for _ in input().split()]


def calc(X, V):
    Vcumsum = [0]
    for v in V:
        Vcumsum += [Vcumsum[-1] + v]
    cumsum = [0] * (N + 1)
    cumsum_r = [0] * (N + 1)
    for i in range(N):
        cumsum[i + 1] = Vcumsum[i + 1] - X[i]
        cumsum_r[i + 1] = Vcumsum[N] - Vcumsum[N - i - 1] - (C - X[-1 - i])
    cumsum_max = [0] * (N + 1)
    cumsum_r_max = [0] * (N + 1)
    for i in range(N):
        cumsum_max[i + 1] = max(cumsum[i + 1], cumsum_max[i])
        cumsum_r_max[i + 1] = max(cumsum_r[i + 1], cumsum_r_max[i])
    res = max(cumsum_max[-1], cumsum_r_max[-1])
    for i in range(N):
        res = max(
            res, cumsum_max[i + 1] + cumsum_r_max[N - i - 1] - X[i],
            cumsum_r_max[i + 1] + cumsum_max[N - i - 1] - (C - X[-1 - i]))
    return res


print(calc(X, V))

Submission status: Accepted

Solution code:
N,C = map(int,input().split())
src = [tuple(map(int,input().split())) for i in range(N)]

csum_r = [0]
cmax_r1 = [0]
cmax_r2 = [0]
for x,v in src:
    csum_r.append(csum_r[-1] + v)
    cmax_r1.append(max(cmax_r1[-1], csum_r[-1] - x))
    cmax_r2.append(max(cmax_r2[-1], csum_r[-1] - 2*x))

csum_l = [0]
cmax_l1 = [0]
cmax_l2 = [0]
for x,v in reversed(src):
    x = C-x
    csum_l.append(csum_l[-1] + v)
    cmax_l1.append(max(cmax_l1[-1], csum_l[-1] - x))
    cmax_l2.append(max(cmax_l2[-1], csum_l[-1] - 2*x))

ans = max(cmax_r1[-1], cmax_l1[-1])
for a,b in zip(cmax_r2, cmax_l1[::-1]):
    ans = max(ans, a+b)
for a,b in zip(cmax_l2, cmax_r1[::-1]):
    ans = max(ans, a+b)

print(ans)

Submission status: Accepted

Solution code:
N, C = map(int, input().split())
xs = []
vs = []
for i in range(N):
    x, v = map(int, input().split())
    xs += [x]
    vs += [v]

costOAs = [0] + xs
costOBs = [0] + [C - x for x in reversed(xs)]

# 時計回りに、A個目まで食べる場合
accV = 0
eneOAs = [0] * (N + 1)
maxOAs = [0] * (N + 1)
for A, v in enumerate(vs, 1):
    accV += v
    eneOAs[A] = accV - costOAs[A] # 差し引きエネルギー
    maxOAs[A] = max(maxOAs[A - 1], eneOAs[A])

# 反時計回りに、B個目まで食べる場合
accV = 0
eneOBs = [0] * (N + 1)
maxOBs = [0] * (N + 1)
for B, v in enumerate(reversed(vs), 1):
    accV += v
    eneOBs[B] = accV - costOBs[B] # 差し引きエネルギー
    maxOBs[B] = max(maxOBs[B - 1], eneOBs[B])

# O→B(→O→A)
ans = 0
for B in range(N + 1):
    ene = eneOBs[B] + max(0, -costOBs[B] + maxOAs[N - B])
    ans = max(ans, ene)

# O→A(→O→B)
for A in range(N + 1):
    ene = eneOAs[A] + max(0, -costOAs[A] + maxOBs[N - A])
    ans = max(ans, ene)

print(ans)

Submission status: Accepted

Solution code:
n,c = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(n)]
ans = 0
num = 0
nu = 0
cou = 0
co = 0
go = 0
left = []
right = []
for i in range(n):
    num += lis[i][1]
    go = lis[i][0]
    ans = max(ans,num-go)
    if cou < num-go:
        cou = num-go
        left.append([go,num])
    else:
        try:left.append(left[-1])
        except:left.append([0,0])

for i in reversed(range(n)):
    nu += lis[i][1]
    go = c-lis[i][0]
    ans = max(ans,nu-go)
    if co < nu-go:
        co = nu-go
        right.append([go,nu])
    else:
        try:right.append(right[-1])
        except:right.append([0,0])
gone = 0
gon = 0
for i in range(n-1):
    gone += lis[i][1]
    ans = max(ans,gone + right[n-i-2][1] - lis[i][0] * 2 - right[n-i-2][0])
    gon += lis[-(i+1)][1]
    ans = max(ans,gon + left[n-i-2][1] - (c-lis[-(i+1)][0]) * 2 - left[n-i-2][0])
print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10**5)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from itertools import accumulate

n,c = li()
x = []
v = []
for _ in range(n):
    xi,vi = li()
    x.append(xi)
    v.append(vi)

# 時計回りのmax
cw_dist = [0] + x
value_cum = [0] + list(accumulate(v))
cw_value = [value - dist for dist,value in zip(cw_dist, value_cum)]
cw_max = [0]
for i in range(1,n+1):
    cw_max.append(max(cw_max[-1], cw_value[i]))

# 反時計回りのmax
ccw_dist = [0] + [c-xi for xi in x[::-1]]
value_cum2 = [0] + list(accumulate(v[::-1]))
ccw_value = [value - dist for dist,value in zip(ccw_dist, value_cum2)]
ccw_max = [0]
for i in range(1,n+1):
    ccw_max.append(max(ccw_max[-1], ccw_value[i]))
    
#
cands = [0]
for i in range(n+1):
    cand = max(cw_max[i],
               ccw_max[n-i],
               cw_max[i]+ccw_max[n-i] - min(cw_dist[i], ccw_dist[n-i]))
               
    cands.append(max(cands[-1],cand))

print(max(cands))

Submission status: Accepted

Solution code:
def inpl(): return [int(i) for i in input().split()]
import numpy as np
N, C = inpl()

x, v = np.array([inpl() for _ in range(N)]).T

cwo = np.cumsum(v)-x
cwr = np.hstack(([0], cwo-x))
cwo = np.hstack(([0], cwo))
for i in range(N):
    cwr[i+1] = max(cwr[i], cwr[i+1])
av = v[::-1]
ax = C - x[::-1]

acwo = np.cumsum(av)-ax
acwr = np.hstack(([0], acwo-ax))
acwo = np.hstack(([0], acwo))
for i in range(N):
    acwr[i+1] = max(acwr[i], acwr[i+1])

ans1 = cwo + acwr[::-1]
ans2 = acwo + cwr[::-1]
print(max(np.max(ans1), np.max(ans2), 0))

Submission status: Accepted

Solution code:
def main():
    n, c = map(int, input().split())
    x, v = [0] * n, [0] * n
    for i in range(n):
        x[i], v[i] = map(int, input().split())

    ss, sr = [0] * n, [0] * n
    ss[0], sr[0] = v[0] - x[0], v[-1] - c + x[-1]

    mx, mxr = [0] * n, [0] * n
    mx[0], mxr[0] = ss[0], sr[0]

    for i in range(n-1):
        ss[i+1] = ss[i] + v[i+1] - x[i+1] + x[i]
        sr[i+1] = sr[i] + v[n-i-2] - x[n-i-1] + x[n-i-2]
        mx[i+1] = max(mx[i], ss[i+1])
        mxr[i+1] = max(mxr[i], sr[i+1])

    r1, r2 = max(0, mx[n-1]), max(0, mxr[n-1])
    for i in range(n-1):
        r1 = max(r1, sr[i]+mx[n-i-2]-c+x[n-i-1])
        r2 = max(r2, ss[i]+mxr[n-i-2]-x[i])

    print(max(r1, r2))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# https://beta.atcoder.jp/contests/arc096/submissions/2706219
N, C = map(int, input().split())
X, V = zip(*(map(int, input().split()) for _ in range(N)))
R = [0] * (N + 1)
Rans = [0] * (N + 1)
for i in range(N):
    R[i + 1] = R[i] + V[i]
    Rans[i + 1] = max(Rans[i], R[i + 1] - X[i])

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

ans_1 = max(Rans[N], Lans[0])
# 初め時計回りで、折り返す
ans_2 = max(Lans[i + 1] + R[i + 1] - 2 * X[i] for i in range(N))
ans_3 = max(Rans[i - 1] + L[i - 1] - 2 * (C - X[i - 1])
            for i in range(N, 0, -1))
print(max(ans_1, ans_2, ans_3))

Submission status: Accepted

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

xv = []
go = []
goback = []
back = []
backgo = []

max1 = 0
sumt = 0

for i in range(n):
    xv.append(list(map(int,input().split())))
    sumt += xv[i][1]
    max1 = max(max1,sumt - xv[i][0])
    go.append(max1)
    goback.append(sumt - 2*xv[i][0])

max1 = 0
sumt = 0

for i in range(n)[::-1]:
    sumt += xv[i][1]
    max1 = max(max1, sumt - (c - xv[i][0]))
    back.append(max1)
    backgo.append(sumt - 2 * (c - xv[i][0]))

res = 0

for i in range(n-1):
    res = max(res,backgo[i]+go[-2-i],goback[i]+back[-2-i])

res = max(res,go[-1],back[-1],0)

print(res)

Submission status: Accepted

Solution code:
N, C = map(int, input().split())
X, V = [], []
for i in range(N):
    xi, vi = map(int, input().split())
    X.append(xi)
    V.append(vi)

# 時計回り・反時計回りにi個目までの寿司を食べたときに歩く距離
r_cost = [0]
l_cost = [0]
# 時計回り・反時計回りにi個目までの寿司を食べた時の総栄養価
r_value = [0]
l_value = [0]

for i in range(N):
    r_cost.append(X[i])
    l_cost.append(C-X[-i-1])
    r_value.append(r_value[-1] + V[i])
    l_value.append(l_value[-1] + V[-i-1])

# 歩く距離と栄養価を考慮した場合の、引き返してどこまで行くのが最適か
r_optimized = [0]
l_optimized = [0]
for i in range(1, N+1):
    r_optimized.append(r_value[i] - r_cost[i])
    l_optimized.append(l_value[i] - l_cost[i])

# max累積和
for i in range(1, N+1):
    r_optimized[i] = max(r_optimized[i], r_optimized[i-1])
    l_optimized[i] = max(l_optimized[i], l_optimized[i-1])

# 答えを計算
ans = 0
for i in range(N+1):
    r_straight = r_optimized[i]
    l_straight = l_optimized[i]
    r_turn = r_value[i] - 2 * r_cost[i] + l_optimized[N-i]
    l_turn = l_value[i] - 2 * l_cost[i] + r_optimized[N-i]
    ans = max(ans, r_straight, l_straight, r_turn, l_turn)

print(ans)

Submission status: Accepted

Solution code:
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())
a=[0 for i in range(N)]
b=[0 for i in range(N)]
a[0]=v[0]
b[N-1]=v[N-1]
for i in range(1,N):
    a[i]=v[i]+a[i-1]
for i in range(N-1)[::-1]:
    b[i]=b[i+1]+v[i]
ans=0
f1=[0 for i in range(N+1)]
f1[0]=0
f1[1]=v[0]-x[0]
for i in range(1,N):
    f1[i+1]=f1[i]+v[i]+x[i-1]-x[i]
g1=[0 for i in range(N+1)]
g1[0]=f1[0]
for i in range(1,N+1):
    g1[i]=max([f1[i],g1[i-1]])
f2=[0 for i in range(N+1)]
f2[0]=0
f2[1]=v[0]-2*x[0]
for i in range(1,N):
    f2[i+1]=f2[i]+v[i]+2*x[i-1]-2*x[i]
g2=[0 for i in range(N+1)]
g2[0]=f2[0]
for i in range(1,N+1):
    g2[i]=max([f2[i],g2[i-1]])
for i in range(N+1):
    if i==0:
        OB=0
        EB=0
    else:
        OB=C-x[N-i]
        EB=b[N-i]
    tmpA=EB-2*OB+g1[N-i]
    tmpB=EB-OB+g2[N-i]
    if ans<tmpA:
        ans=tmpA
    if ans<tmpB:
        ans=tmpB
print(ans)

Submission status: Accepted

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

if N == 1:
    x, v = map(int, input().split())
    print(max(0, v-x, v-(C-x)))
    exit()


Cost = [] #[x, v, left_cumsum, left_cummax, right_cumsum, right_cummax]

cum = 0
x, v = map(int, input().split())
cum += v
Cost.append([x, v, cum-x, max(0, cum-x), 0, 0])
for _ in range(N-1):
    x, v = map(int, input().split())
    cum += v
    Cost.append([x, v, cum-x, max(Cost[-1][3], cum-x), 0, 0])

cum = 0
cum += Cost[-1][1]
Cost[-1][4] = cum - (C - Cost[-1][0])
Cost[-1][5] = max(0, Cost[-1][4])
for i in range(N-2, -1, -1):
    cum += Cost[i][1]
    Cost[i][4] = cum - (C - Cost[i][0])
    Cost[i][5] = max(Cost[i+1][5], Cost[i][4])
    
max_val = 0
max_val = max(max_val, Cost[0][3], Cost[0][5],\
        Cost[0][2] + Cost[1][5] - Cost[0][0])
max_val = max(max_val, Cost[-1][3], Cost[-1][5],\
        Cost[-1][4] + Cost[-2][3] - (C-Cost[-1][0]))
for i in range(1, N-1):
    max_val = max(max_val, Cost[i][3], Cost[i][5],\
            Cost[i][2] + Cost[i+1][5] - Cost[i][0],\
            Cost[i][4] + Cost[i-1][3] - (C-Cost[i][0]))

print(max_val)

Submission status: Accepted

Solution code:
n, c = map(int, input().split())
sushi = [list(map(int, input().split())) for _ in range(n)]
clock = [[0, 0] for _ in range(n)]
anticlock = [[0, 0] for _ in range(n)]

max_clock = max(sushi[0][1] - sushi[0][0], 0)
clock[0] = [sushi[0][1] - sushi[0][0], max_clock]
for i in range(1, n):
	clock[i][0] = clock[i-1][0] + sushi[i][1] - (sushi[i][0] - sushi[i-1][0])
	max_clock = max(max_clock, clock[i][0])
	clock[i][1] = max_clock

max_anti = max(sushi[-1][1] - (c - sushi[-1][0]), 0)
anticlock[0] = [sushi[-1][1] - (c - sushi[-1][0]), max_anti]
for i in range(1, n):
	anticlock[i][0] = anticlock[i-1][0] + sushi[-(i+1)][1] - (sushi[-i][0] - sushi[-(i+1)][0])
	max_anti = max(max_anti, anticlock[i][0])
	anticlock[i][1] = max_anti

ans_clock = 0
ans_anti = 0

for i in range(n - 1):
	if clock[i][0] - sushi[i][0] + anticlock[n - 2 - i][1] > ans_clock:
		ans_clock = clock[i][0] - sushi[i][0] + anticlock[n - 2 - i][1]
	if anticlock[i][0] - (c - sushi[-(i+1)][0]) + clock[n - 2 - i][1] > ans_anti:
		ans_anti = anticlock[i][0] - (c - sushi[-(i+1)][0]) + clock[n - 2 - i][1]

print(max(ans_clock, ans_anti, clock[-1][1], anticlock[-1][1]))

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.append(x)
    V.append(v)
#時計回りで見る
R=[0]*(N+1)#0番目は１に格納する
Rans=[0]*(N+1)
for i in range(N):
    R[i+1]=R[i]+V[i]
    Rans[i+1]=max(Rans[i],R[i+1]-X[i])
L=[0]*(N+1)
Lans=[0]*(N+1)
for i in range(N,0,-1):
    L[i-1]=L[i]+V[i-1]#N-1にN-1を格納
    Lans[i-1]=max(Lans[i],L[i-1]-C+X[i-1])
ans_1=max(Rans[N],Lans[0])
ans_2=max(Lans[i+1]+R[i+1]-2*X[i] for i in range(N))
ans_3=max(Rans[i-1]+L[i-1]-2*C+2*X[i-1] for i in range(N,0,-1))
print(max(ans_1,ans_2,ans_3))

Submission status: Accepted

Solution code:
import sys
N,C = map(int,input().split())
table=[]
for i in range(N):
    x,v=map(int,input().split())
    table.append([x,v])
ans=0
if N==1:
    ans=max(0,table[0][1]-table[0][0],table[0][1]+table[0][0]-C)
    print(ans)
    sys.exit()
V=[0]*N
clo=[0]*N
V[0]=table[0][1]
clo[0]=V[0]-table[0][0]
for i in range(1,N):
    V[i]=V[i-1]+table[i][1]
    clo[i]=max(clo[i-1],V[i]-table[i][0])
    ans = max(clo[i],ans)
rclo=[0]*N
rclo[N-1]=V[N-1]-V[N-2]-C+table[N-1][0]
for i in range(N-1,1,-1):
    rclo[i-1]=max(rclo[i],V[N-1]-V[i-2]-C+table[i-1][0])
    ans = max(clo[i],ans)
rclo[0]=max(rclo[1],V[N-1]-C+table[0][0])
ans = max(rclo[0],ans)

for i in range(N-1):
    t = V[i]+rclo[i+1]-2*table[i][0]
    ans=max(t,ans)
for i in range(N-1,0,-1):
    t = V[N-1]-V[i-1] + clo[i-1]-2*C+2*table[i][0]
    ans=max(t,ans)

print(ans)

Submission status: Accepted

Solution code:
import sys
cin = sys.stdin
def line2N():
    return map(int, cin.readline().split())
n, c = line2N()
x = [0]*n
v = [0]*n
for i in range(n):
    x0, v0 = line2N()
    x[i] = x0
    v[i] = v0
c1 = [0]*n
m1 = [0]*(n+1)
c1[0] = -(x[0]-0) + v[0]
m1[0] = max(0, c1[0])
for i in range(1,n):
    c1[i] = c1[i-1] - (x[i]-x[i-1]) + v[i]
    m1[i] = max(m1[i-1], c1[i])
c2 = [0]*n
c2[n-1] = -(c - x[n-1]) + v[n-1]
m2 = [0]*(n+1)
m2[n-1] = max(0, c2[n-1])
for i in range(n-2, -1, -1):
    c2[i] = c2[i+1] - (x[i+1]-x[i]) + v[i]
    m2[i] = max(m2[i+1], c2[i])
ans0 = 0
for i in range(0, n-1):
    ans0 = max(ans0, c1[i] - x[i] + m2[i+1])
for i in range(n-1, 0, -1):
    ans0 = max(ans0, c2[i] - (c-x[i]) + m1[i-1])
print(max(ans0, m1[n-1], m2[0]))

Submission status: Accepted

Solution code:
n, c = map(int, input().split())
sushi = []
for i in range(n):
    x, v = map(int, input().split())
    sushi.append((x, v))
sushi.sort()
sushi_reversed = list(reversed([(c - x, v) for x, v in sushi]))


def accum_max(s):
    v = 0
    res = [(0, 0)]
    for x_i, v_i in s:
        v += v_i
        res.append(max(res[-1], (v-x_i, x_i)))
    return res[1:]


max_sushi = accum_max(sushi)
max_sushi_reversed = accum_max(sushi_reversed)


def solve(s, msr):
    v_max = 0
    v_2d_max = 0
    v = 0
    n = len(s)
    for i in range(n-1):
        x_i, v_i = s[i]
        v += v_i
        if v - x_i*2 > v_2d_max:
            v_2d_max = v - x_i*2
            v_sub = msr[n - 2 - i][0]
            v_max = max(v_max, v_2d_max + v_sub)
    return v_max


l = solve(sushi, max_sushi_reversed)
r = solve(sushi_reversed, max_sushi)
print(max(l, r, max_sushi[-1][0], max_sushi_reversed[-1][0]))

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,C = LI()
    a = [LI() for _ in range(n)]
    u = [_[0] for _ in a]
    x = [_[1] for _ in a]
    b = [0] * n
    c = [0] * n
    d = [0] * n
    e = [0] * n
    b[0] = x[0]
    c[-1] = x[-1]
    for i in range(1, n):
        b[i] = b[i-1] + x[i]
    for i in range(n-2, -1, -1):
        c[i] = c[i+1] + x[i]
    d[0] = x[0] - u[0]
    e[-1] = x[-1] - (C-u[-1])
    for i in range(1, n):
        d[i] = max(b[i]-u[i], d[i-1])
    for i in range(n-2, -1, -1):
        e[i] = max(c[i]-(C-u[i]), e[i+1])

    r = max(d[-1], e[0], 0)
    for i in range(n):
        if i < n-1 and r < b[i] - u[i] * 2 + e[i+1]:
            r = b[i] - u[i] * 2 + e[i+1]
        if i > 0 and r < c[i] - (C-u[i]) * 2 + d[i-1]:
            r = c[i] - (C-u[i]) * 2 + d[i-1]


    return r



print(main())

Submission status: Accepted

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

lx, lv = [], []

for _ in range(n):
    tmpx, tmpv = map(int, input().split())
    lx.append(tmpx)
    lv.append(tmpv)

lux = [c-x for x in reversed(lx)]

#両方向の累積和
tmpv1, tmpv2 = 0,0
vc, vuc = [],[]
for i in range(n):
    tmpv1 += lv[i]
    tmpv2 += lv[n-1-i]
    _ = [x.append(y) for x,y in zip([vc,vuc],[tmpv1,tmpv2])]

sc, suc = [],[]
tmp1,tmp2 = 0,0
for i in range(n):
    sc.append(vc[i] - lx[i])
    suc.append(vuc[i] - (c - lx[n-1-i]))

ssc,ssuc = [],[]
tmp,tmpu = -c, -c
for i in range(n):
    tmp = max(tmp, sc[i])
    tmpu = max(tmpu, suc[i])
    ssc.append(tmp)
    ssuc.append(tmpu)
ans = 0

for i in range(n):
    s1 = sc[i]
    s2 = suc[i]
    if i == n-1:
        s3,s4 = 0,0
    else:
        s3 = s1 - lx[i] + ssuc[max(0,n-i-2)]
        s4 = s2 - lux[i] + ssc[max(0,n-i-2)]

    ans = max(ans,s1,s2,s3,s4)

print(ans)

Submission status: Accepted

Solution code:
n, c = map(int, input().split())
xv = [[int(x) for x in input().split()] for _ in range(n)]
xv.insert(0, [0, 0])
n += 1
oa = [0] * (n + 1)
ob = [0] * (n + 1)
maxr = [0] * (n + 1)
maxl = [0] * (n + 1)
for i in range(1, n):
    oa[i] = oa[i - 1] + xv[i][1]
    maxr[i] = max(maxr[i - 1], oa[i] - xv[i][0])
ob[n - 1] = xv[i][1]
for i in range(n - 1, -1, -1):
    ob[i] = xv[i][1] + ob[i + 1]
    maxl[i] = max(maxl[i + 1], ob[i] - (c - xv[i][0]))
cal1 = max(maxr[n-1], maxl[0])
cal2 = 0
cal3 = 0
for i in range(n):
    cal2 = max(cal2, oa[i] - 2 * xv[i][0] + maxl[i+1])
    cal3 = max(cal3, ob[i] - 2 * (c - xv[i][0]) + maxr[i-1])
print(max(cal1, cal2, cal3))

Submission status: Accepted

Solution code:
N, C = [int(_) for _ in input().split()]
xv = [[int(_) for _ in input().split()] for i in range(N)]

def solve(N, C, xv):
    
    from itertools import accumulate

    xs = [0] + [_[0] for _ in xv] + [C]
    vs = [0] + [_[1] for _ in xv] + [0]
    xs_rev = [C - x for x in xs[::-1]]
    vs_rev = vs[::-1]

    ts = accumulate(vs)
    txs = [t - x for x, t in zip(xs, ts)]
    maxtxs = list(accumulate(txs, max))

    ts_rev = accumulate(vs_rev)
    txs_rev = [t - x for x, t in zip(xs_rev, ts_rev)]
    maxtxs_rev = list(accumulate(txs_rev, max))

    return max(
        max(tx - xs_rev[i] + maxtxs[N - i] for i, tx in enumerate(txs_rev[:-1])),
        max(tx - xs[i] + maxtxs_rev[N - i] for i, tx in enumerate(txs[:-1]))
    )

print(solve(N, C, xv))

Submission status: Accepted

Solution code:
N, C = [int(_) for _ in input().split()]
xv = [[int(_) for _ in input().split()] for i in range(N)]

def solve(N, C, xv):
    
    from itertools import accumulate

    xs = [0] + [_[0] for _ in xv] + [C]
    vs = [0] + [_[1] for _ in xv] + [0]
    xs_rev = [C - x for x in xs[::-1]]
    vs_rev = vs[::-1]

    ts = accumulate(vs)
    txs = [t - x for x, t in zip(xs, ts)]
    maxtxs = list(accumulate(txs, max))

    ts_rev = accumulate(vs_rev)
    txs_rev = [t - x for x, t in zip(xs_rev, ts_rev)]
    maxtxs_rev = list(accumulate(txs_rev, max))

    result = 0
    for i, tx in enumerate(txs_rev[:-1]):
        r = tx - xs_rev[i] + maxtxs[N - i]
        result = max(result, r)
    for i, tx in enumerate(txs[:-1]):
        r = tx - xs[i] + maxtxs_rev[N - i]
        result = max(result, r)

    return result

print(solve(N, C, xv))