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: Wrong Answer

Solution code:
"""
カウンターの回り方はパターンが多すぎるので、考えない方がよい。
考え方１：
残っている寿司のうち、そこにたどり着くまでのエネルギー <= そこにたどり着くまでに摂取できる寿司のカロリー
となるような寿司があるかどうか。


考え方２：
二分探索。
X以上カロリーを得できるか、という二分探索。

考え方３：
動き方のパターンは4つある。
時計回りに動いて、どこかのタイミングで退店
半時計周りに動いて、どこかのタイミングで退店
時計回りに動いた後、反時計回りに動いて退店
反時計回りに動いた後、時計回りに動いて退店
それぞれのパターンで動く場合の最大価値を求める。
これで行こう
"""

N,C = map(int,input().split())
#sushi[i] => スタート地点から時計回りに動いたときにi番目にある寿司の情報
#sushiR[i] => スタート地点から反時計回りに動いたときにi番目にある寿司の情報
sushi = []
sushiR = []
for _ in range(N):
    x,v = map(int,input().split())
    sushi.append([x,v])
    sushiR.append([C-x,v])
sushiR = sushiR[::-1]

ans = 0
"""
まずは時計回りに動いてどこかのタイミングで退店する場合を考える。
これは、時計回りに寿司を見て行って、各寿司にたどり着いた時の、摂取カロリーの累積和と消費カロリーを求めればよい。
"""

cal = 0
for x,v in sushi:
    cal += v
    ans = max(ans,cal-x)

"""
次に反時計回りに動いてどこかのタイミングで退店する場合を考える。
"""
cal = 0
for x,v in sushiR:
    cal += v
    ans = max(ans,cal-x)


"""
時計回りに動いた後、反時計回りに動いて退店する場合を考える。
これは、時計回りでどこまで動くか（つまりどこで折り返すか）で場合わけし、
各地点で折り返す場合に、反時計回りでどこまで行くのがベストなのかをaccumSushiRをつかって判断する
"""
from heapq import heappop,heappush
sushiRQue = []
cal = 0
for i in range(N):
    x,v = sushiR[i]
    cal += v
    heappush(sushiRQue,(-(cal-x),i))

cal = 0
for i in range(N-1):
    x,v = sushi[i]
    cal = 0
    cal += v
    while sushiRQue[0][1] >= N-i-1:
        heappop(sushiRQue)
    ans = max(ans,cal-x*2+(-sushiRQue[0][0]))

"""
時計回りに動いた後、反時計回りに動いて退店する場合を考える。
これは、時計回りでどこまで動くか（つまりどこで折り返すか）で場合わけし、
各地点で折り返す場合に、反時計回りでどこまで行くのがベストなのかをaccumSushiRをつかって判断する
"""
sushiQue = []
cal = 0
for i in range(N):
    x,v = sushi[i]
    cal += v
    heappush(sushiQue,(-(cal-x),i))

cal = 0
for i in range(N-1):
    x,v = sushiR[i]
    cal = 0
    cal += v
    while sushiQue[0][1] >= N-i-1:
        heappop(sushiQue)
    ans = max(ans,cal-x*2+(-sushiQue[0][0]))

print(ans)

Submission status: Wrong Answer

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

frontGet = [0]*n
frontRuiseki = [0]*(n+1)
backGet = [0]*n
backRuiseki = [0]*(n+1)

for i in range(n):
    frontRuiseki[i+1] = frontRuiseki[i]+xv[i][1] 
    frontGet[i] = frontRuiseki[i+1] - xv[i][0]
    idx = -1 - i
    backRuiseki[idx-1] = backRuiseki[idx]+xv[idx][1] 
    backGet[idx] = backRuiseki[idx-1] + xv[idx][0] - c

backGet.reverse()
frontGet = [0] + frontGet
backGet = [0] + backGet
xv = [[0,0]] + xv
frontMax = [0]*(n+1)
backMax = [0]*(n+1)

for i in range(n):
    frontMax[i+1] = max(frontMax[i],frontGet[i])
    backMax[i+1] = max(backGet[i],backMax[i])

ans = max(frontMax[-1],backMax[-1],0)
for i in range(n+1):
    front = frontGet[i] - xv[i][0]
    front += backMax[n - i]
    back = backGet[i] + xv[-i][0] - c
    back += frontMax[n - i]
    ans = max(ans,front,back)
print(ans)

Submission status: Wrong Answer

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

frontGet = [0]*n
frontRuiseki = [0]*(n+1)
backGet = [0]*n
backRuiseki = [0]*(n+1)

for i in range(n):
    frontRuiseki[i+1] = frontRuiseki[i]+xv[i][1] 
    frontGet[i] = frontRuiseki[i+1] - xv[i][0]
    idx = -1 - i
    backRuiseki[idx-1] = backRuiseki[idx]+xv[idx][1] 
    backGet[idx] = backRuiseki[idx-1] + xv[idx][0] - c

backGet.reverse()
frontGet = [0] + frontGet
backGet = [0] + backGet
xv = [[0,0]] + xv
frontMax = [0]*(n+1)
backMax = [0]*(n+1)

for i in range(n):
    frontMax[i+1] = max(frontMax[i],frontGet[i])
    backMax[i+1] = max(backGet[i],backMax[i])

ans = max(frontMax[-1],backMax[-1])
for i in range(n+1):
    front = frontGet[i] - xv[i][0]
    front += backMax[n - i]
    back = backGet[i] + xv[-i][0] - c
    back += frontMax[n - i]
    ans = max(ans,front,back)
print(ans)

Submission status: Wrong Answer

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

frontGet = [0]*n
frontRuiseki = [0]*(n+1)
backGet = [0]*n
backRuiseki = [0]*(n+1)

for i in range(n):
    frontRuiseki[i+1] = frontRuiseki[i]+xv[i][1] 
    frontGet[i] = frontRuiseki[i+1] - xv[i][0]
    idx = -1 - i
    backRuiseki[idx-1] = backRuiseki[idx]+xv[idx][1] 
    backGet[idx] = backRuiseki[idx-1] + xv[idx][0] - c

backGet.reverse()
frontGet = [0] + frontGet
backGet = [0] + backGet
xv = [[0,0]] + xv
frontMax = [0]*(n+1)
backMax = [0]*(n+1)

for i in range(n):
    frontMax[i+1] = max(frontMax[i],frontGet[i])
    backMax[i+1] = max(backGet[i],backMax[i])

ans = 0
#print(frontGet,xv)
for i in range(n+1):
    front = frontGet[i] - xv[i][0]
    front += backMax[n - i]
    back = backGet[i] + xv[-i][0] - c
    back += frontMax[n - i]
    ans = max(ans,front,back)
print(ans)

Submission status: Wrong Answer

Solution code:
N,C=map(int, input().split())
#Aは時計回り
A,B=[],[]
allv=0
for i in range(N):
  a,b=map(int, input().split())
  A.append([a,b])
  B.append([C-a,b])
  allv+=b
A,B=sorted(A),sorted(B)
OA,OB,AS,BS=[],[],[],[]
ax,av,bx,bv=0,0,0,0
for i in range(N):
  ax,bx=A[i][0],B[i][0]
  av+=A[i][1]
  bv+=B[i][1]
  OA.append((ax,av))
  OB.append((bx,bv))
  AS.append(av-ax)
  BS.append(bv-bx)
#OA→AO→OBといくとき
ansa=0
for i in range(N-1):
  #iまで行く
  bma=N-i
  oa=OA[i][1]-OA[i][0]
  ao=-OA[i][0]
  ob=max(BS[:N-i-1])
  d=max(oa,oa+ao+ob)
  ansa=max(ansa,d)
ansb=0
for i in range(N-1):
  #iまで行く
  ama=N-i
  ob=OB[i][1]-OB[i][0]
  bo=-OB[i][0]
  oa=max(AS[:N-i-1])
  d=max(ob,ob+bo+oa)
  ansb=max(ansb,d)
print(max(ansa,ansb,allv-(C-min(OA[0][0],OB[0][0]))))

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
input2 = sys.stdin.read

# A: Anti-clockwise, B: Clockwise, V: Values
n, c = map(int, input().split())
AV = np.array(list(map(int, input2().rsplit())), dtype=np.int64)
A, V = AV[::2], AV[1::2]
B = (c - A)[::-1]

XA = np.cumsum(V) - A
XB = np.cumsum(V[::-1]) - B
RXA, RXB = XA - A, XB - B    # Cumulative Values returning to Start

MA = np.zeros(n + 1, dtype=np.int64)
MB = np.zeros(n + 1, dtype=np.int64)
MRA = np.zeros(n + 1, dtype=np.int64)
MRB = np.zeros(n + 1, dtype=np.int64)


def max_mem(lis, mlis):
    for i in range(n):
        mlis[i + 1] = max(mlis[i], lis[i])


max_mem(XA, MA)
max_mem(XB, MB)
max_mem(RXA, MRA)
max_mem(RXB, MRB)

if n == 1:
    print(MA[0])
    exit()

#print(MA, MB, MRA, MRB)

res1, res2 = 0, 0
for i in range(n + 1):
    res1 = max(res1, MA[i] + MRB[n - i])
    res2 = max(res2, MB[i] + MRA[n - i])

print(max(res1, res2))

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
input2 = sys.stdin.read

# A: Anti-clockwise, B: Clockwise, V: Values
n, c = map(int, input().split())
AV = np.array(list(map(int, input2().rsplit())), dtype=np.int64)
A, V = AV[::2], AV[1::2]
B = (c - A)[::-1]

XA = np.cumsum(V) - A
XB = np.cumsum(V[::-1]) - B
RXA, RXB = XA - A, XB - B    # Cumulative Values returning to Start

MA = np.zeros(n, dtype=np.int64)
MB = np.zeros(n, dtype=np.int64)
MRA = np.zeros(n, dtype=np.int64)
MRB = np.zeros(n, dtype=np.int64)


def max_mem(lis, mlis):
    for i in range(n):
        if i == 0:
            mlis[i] = max(lis[i], 0)
        else:
            mlis[i] = max(mlis[i - 1], lis[i])


max_mem(XA, MA)
max_mem(XB, MB)
max_mem(RXA, MRA)
max_mem(RXB, MRB)

if n == 1:
    print(MA[0])
    exit()

#print(MA, MB, MRA, MRB)

res1, res2 = 0, 0
for i in range(n - 1):
    res1 = max(res1, MA[i] + MRB[n - 2 - i])
    res2 = max(res2, MB[i] + MRA[n - 2 - i])
    #print(res1, res2)

print(max(res1, res2))

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
input2 = sys.stdin.read

# A: Anti-clockwise, B: Clockwise, V: Values
n, c = map(int, input().split())
AV = np.array(list(map(int, input2().rsplit())), dtype=np.int64)
A, V = AV[::2], AV[1::2]
B = (c - A)[::-1]

XA = np.cumsum(V) - A
XB = np.cumsum(V[::-1]) - B
RSA, RSB = XA - A, XB - B    # Cumulative Values returning to Start

MA, MB = max(XA[:-1], default=0), max(XB[:-1], default=0)
MXA, MXB = XA.max(), XB.max()
mai, mbi = 0, 0

for i, x in enumerate(XA):
    if x == MA:
        mai = i

for i, x in enumerate(XB):
    if x == MB:
        mbi = i

MRA = max(0, max(RSA[:n - 1 - mbi], default=0))
MRB = max(0, max(RSB[:n - 1 - mai], default=0))

print(max(MA + MRB, MB + MRA, MXA, MXB))

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
input2 = sys.stdin.read

# A: Anti-clockwise, B: Clockwise, V: Values
n, c = map(int, input().split())
AV = np.array(list(map(int, input2().rsplit())), dtype=np.int64)
A, V = AV[::2], AV[1::2]
B = (c - A)[::-1]

XA = np.cumsum(V) - A
XB = np.cumsum(V[::-1]) - B
RSA, RSB = XA - A, XB - B    # Cumulative Values returning to Start

MA, MB = XA.max(), XB.max()
mai, mbi = 0, 0

for i, x in enumerate(XA):
    if x == MA:
        mai = i

for i, x in enumerate(XB):
    if x == MB:
        mbi = i

MRA = max(0, RSA[:n - mbi].max())
MRB = max(0, RSB[:n - mai].max())

print(max(MA + MRB, MB + MRA))

Submission status: Wrong Answer

Solution code:
n,c=map(int,input().split())
ll,rl=[[0]*2 for _ in range(n+1)],[[0]*2 for _ in range(n+1)]
ls,rs=0,0
xv=[[0,0]]+[list(map(int,input().split())) for _ in range(n)]
for i in range(1,n+1):
    j,k=xv[i]
    rs+=k
    if rl[i-1][1]-rl[i-1][0]<rs-j:
        rl[i][0],rl[i][1]=j,rs
    else:
        rl[i][0],rl[i][1]=rl[i-1][0],rl[i-1][1]
    j,k=xv[n-i+1]
    ls+=k
    ll[i][0],ll[i][1]=(c-j),ls-(c-j)
ans=0
for i in range(n+1):
    ans=max(ans,ll[i][1]+rl[n-i][1]-rl[n-i][0]-min(ll[i][0],rl[n-i][0]),ll[i][1])
print(ans)

Submission status: Wrong Answer

Solution code:
n,c=map(int,input().split())
ll,rl=[[0]*2 for _ in range(n+1)],[[0]*2 for _ in range(n+1)]
ls,rs=0,0
xv=[[0,0]]+[list(map(int,input().split())) for _ in range(n)]
for i in range(1,n+1):
    j,k=xv[i]
    rs+=k
    if rl[i-1][1]-rl[i-1][0]<rs-j:
        rl[i][0],rl[i][1]=j,rs
    else:
        rl[i][0],rl[i][1]=rl[i-1][0],rl[i-1][1]
    j,k=xv[n-i+1]
    ls+=k
    ll[i][0],ll[i][1]=(c-j),ls-(c-j)
ans=0
for i in range(n+1):
    ans=max(ans,ll[i][1]+rl[n-i][1]-rl[n-i][0]-min(ll[i][0],rl[n-i][0]))
print(ans)

Submission status: Wrong Answer

Solution code:
n,c=map(int,input().split())
ll,rl=[0]*(n+1),[0]*(n+1)
ls,rs=0,0
xv=[[0,0]]+[list(map(int,input().split())) for _ in range(n)]
for i in range(1,n+1):
    j,k=xv[i]
    rs+=k
    rl[i]=rs-j
    j,k=xv[n-i+1]
    ls+=k
    ll[i]=ls-(c-j)
ans=0
for l in range(n+1):
    for r in range(n-l):
        if ll[l]<0:
            continue
        if rl[r]<0:
            continue
        if l==0 or r==0:
            ans=max(ans,ll[l]+rl[r])
        else:
            ans=max(ans,ll[l]+rl[r]-min(c-xv[n-l-1][0],xv[r][0]))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    N, C = map(int, input().split())

    X_right = []
    X_left = []
    V_right = []

    for i in range(N):
        x, v = map(int, input().split())
        X_right.append(x)
        V_right.append(v)
        X_left.append(C - x)

    V_left = V_right[::-1]
    V_right_add = list(accumulate([0] + V_right))
    V_left_add = list(accumulate([0] + V_left))
    X_left = [0] + X_left[::-1]
    X_right = [0] + X_right

    Right = []
    X = [0]
    answer_right = 0
    for i in range(N + 1):
        if answer_right < V_right_add[i] - X_right[i]:
            answer_right = V_right_add[i] - X_right[i]
            X.append(X_right[i])
        else:
            a = X[-1]
            X.append(a)

        Right.append(answer_right)

    Right = Right[::-1]
    X = X[::-1]
    answer = 0

    for i in range(N + 1):
        answer = max(
            V_left_add[i] - X_left[i] * 2 + Right[i],
            V_left_add[i] - X_left[i] + Right[i] - X[i],
            answer,
        )

    print(answer)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


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

    # [0, i]までで得られるカロリーの最大値
    dp_right = [0] * N
    dp_right[0] = -X[0] + V[0]
    for i in range(1, N):
        dp_right[i] = dp_right[i - 1] + V[i] - (X[i] - X[i - 1])

    # [i, n - 1] までで得られるカロリーの最大値
    dp_left = [0] * N
    dp_left[N - 1] = V[N - 1] - (C - X[N - 1])
    for i in range(N - 2, -1, -1):
        dp_left[i] = dp_left[i + 1] + V[i] - (X[i + 1] - X[i])

    # 最大値に書き換える
    for i in range(1, N):
        if dp_right[i] >= dp_right[i - 1]:
            continue
        else:
            dp_right[i] = dp_right[i - 1]

    for i in range(N - 2, -1, -1):
        if dp_left[i] >= dp_left[i + 1]:
            continue
        else:
            dp_left[i] = dp_left[i + 1]

    # 折り返さない
    ans = max(0, dp_left[0], dp_right[-1])
    # 時計周りに進んで，i個目の寿司で半時計周りに折り返す
    for i in range(N - 1):
        tmp = dp_right[i] + dp_left[i + 1] - X[i]
        if tmp > ans:
            ans = tmp

    # 最初に半時計周りに進む
    for i in range(N - 2, 0, -1):
        tmp = dp_left[i] + dp_right[i - 1] - (C - X[i])
        if tmp > ans:
            ans = tmp

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


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

    # [0, i]までで得られるカロリーの最大値
    dp_right = [0] * N
    dp_right[0] = -X[0] + V[0]
    for i in range(1, N):
        dp_right[i] = dp_right[i - 1] + V[i] - (X[i] - X[i - 1])

    # [i, n - 1] までで得られるカロリーの最大値
    dp_left = [0] * N
    dp_left[N - 1] = V[N - 1] - (C - X[N - 1])
    for i in range(N - 2, -1, -1):
        dp_left[i] = dp_left[i + 1] + V[i] - (X[i + 1] - X[i])

    # 最大値に書き換える
    for i in range(1, N):
        if dp_right[i] >= dp_right[i - 1]:
            continue
        else:
            dp_right[i] = dp_right[i - 1]

    for i in range(N - 2, -1, -1):
        if dp_left[i] >= dp_left[i + 1]:
            continue
        else:
            dp_left[i] = dp_left[i + 1]

    # 折り返さない
    ans = max(0, dp_left[-1], dp_right[-1])
    # 時計周りに進んで，i個目の寿司で半時計周りに折り返す
    for i in range(N - 1):
        if dp_left[i + 1] - X[i] > 0:
            tmp = dp_right[i] + dp_left[i + 1] - X[i]
            if tmp > ans:
                ans = tmp

    # 最初に半時計周りに進む
    for i in range(N - 2, 0, -1):
        if dp_right[i - 1] - (C - X[i]) > 0:
            tmp = dp_left[i] + dp_right[i - 1] - (C - X[i])
            if tmp > ans:
                ans = tmp

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


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

    # [0, i]までで得られるカロリーの最大値
    dp_right = [0] * N
    dp_right[0] = -X[0] + V[0]
    for i in range(1, N):
        dp_right[i] = dp_right[i - 1] + V[i] - (X[i] - X[i - 1])

    # [i, n - 1] までで得られるカロリーの最大値
    dp_left = [0] * N
    dp_left[N - 1] = V[N - 1] - (C - X[N - 1])
    for i in range(N - 2, -1, -1):
        dp_left[i] = dp_left[i + 1] + V[i] - (X[i + 1] - X[i])

    # 最大値に書き換える
    for i in range(1, N):
        if dp_right[i] >= dp_right[i - 1]:
            continue
        else:
            dp_right[i] = dp_right[i - 1]

    for i in range(N - 2, -1, -1):
        if dp_left[i] >= dp_left[i + 1]:
            continue
        else:
            dp_left[i] = dp_left[i + 1]

    # 折り返さない
    ans = max(0, dp_left[-1], dp_right[-1])
    # 時計周りに進んで，i個目の寿司で半時計周りに折り返す
    for i in range(N - 1):
        tmp = dp_right[i] + dp_left[i + 1] - X[i]
        if tmp > ans:
            ans = tmp

    # 最初に半時計周りに進む
    for i in range(N - 2, 0, -1):
        tmp = dp_left[i] + dp_right[i - 1] - (C - X[i])
        if tmp > ans:
            ans = tmp

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    N, C = map(int, input().split())

    X_right = []
    X_left = []
    V_right = []

    for i in range(N):
        x, v = map(int, input().split())
        X_right.append(x)
        V_right.append(v)
        X_left.append(C - x)

    V_left = V_right[::-1]
    V_right_add = list(accumulate([0] + V_right))
    V_left_add = list(accumulate([0] + V_left))
    X_left = [0] + X_left[::-1]
    X_right = [0] + X_right

    Right = []
    X = [0]
    answer_right = 0
    for i in range(N + 1):
        if answer_right < V_right_add[i] - X_right[i]:
            answer_right = V_right_add[i] - X_right[i]
            X.append(X_right[i])
        else:
            a = X[-1]
            X.append(a)

        Right.append(answer_right)

    Right = Right[::-1]
    X = X[::-1]
    answer = Right[0]

    for i in range(N + 1):
        answer = max(
            V_left_add[i] - X_left[i] * 2 + Right[i],
            V_left_add[i] - X_left[i] + Right[i] - X[i],
            answer,
        )

    print(answer)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    N, C = map(int, input().split())

    X_right = []
    X_left = []
    V_right = []

    for i in range(N):
        x, v = map(int, input().split())
        X_right.append(x)
        V_right.append(v)
        X_left.append(C - x)

    V_left = V_right[::-1]
    V_right_add = list(accumulate([0] + V_right))
    V_left_add = list(accumulate([0] + V_left))
    X_left = [0] + X_left[::-1]
    X_right = [0] + X_right

    Right = []
    X = [0]
    answer_right = 0
    for i in range(N + 1):
        if answer_right < V_right_add[i] - X_right[i]:
            answer_right = V_right_add[i] - X_right[i]
            X.append(X_right[i])
        else:
            a = X[-1]
            X.append(a)

        Right.append(answer_right)

    Right = Right[::-1]
    X = X[::-1]
    answer = Right[0]

    for i in range(N + 1):
        if answer < V_left_add[i] - X_left[i] * 2 + Right[i]:
            answer = max(
                V_left_add[i] - X_left[i] * 2 + Right[i],
                V_left_add[i] - X_left[i] + Right[i] - X[i],
                answer,
            )

    print(answer)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    N, C = map(int, input().split())

    X_right = []
    X_left = []
    V_right = []

    for i in range(N):
        x, v = map(int, input().split())
        X_right.append(x)
        V_right.append(v)
        X_left.append(C - x)

    V_left = V_right[::-1]
    V_right_add = list(accumulate([0] + V_right))
    V_left_add = list(accumulate([0] + V_left))
    X_left = [0] + X_left[::-1]
    X_right = [0] + X_right

    Right = []
    answer_right = 0
    for i in range(N + 1):
        if answer_right < V_right_add[i] - X_right[i]:
            answer_right = V_right_add[i] - X_right[i]
        Right.append(answer_right)

    Right = Right[::-1]
    answer = Right[-1]
    for i in range(N + 1):
        if answer < V_left_add[i] - X_left[i] * 2 + Right[i]:
            answer = max(
                V_left_add[i] - X_left[i] * 2 + Right[i],
                V_left_add[i] - X_left[i] + Right[i] - X_right[N - i - 1],
                answer,
            )
    print(answer)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

x = []
v = []
array1 = []
array2 = []

for i in range(n):
  a,b = map(int, input().split())
  x.append(a)
  v.append(b)
  
rx = []
rv = []
for i in reversed(range(len(x))):
  rx.append(c-x[i])
  rv.append(v[i])
  
count = x[0]
array1.append(v[0] - x[0])
for i in range(1,n):
  array1.append(v[i]-x[i]+array1[i-1]+count)
  count = x[i]
  
count = rx[0]
array2.append(rv[0] - rx[0])
for i in range(1,n):
  array2.append(rv[i]-rx[i]+array2[i-1]+count)
  count = rx[i]
  
ans = 0
for i in range(n):
  if i != n-1:
    tmp = max(array1[i], array1[i]-x[i]+max(array2[:n-i-1]))
    ans = max(ans, tmp)
  else:
    ans = max(ans, array1[i])
  
for i in range(n):
  if i != n-1:
    tmp = max(array2[i], array2[i]-rx[i]+max(array1[:n-i-1]))
    ans = max(ans, tmp)
  else:
    ans = max(ans, array2[i])
    
print(array1)
print(array2)
  
print(ans)

Submission status: Wrong Answer

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

class SegmentTree(object):
    __slots__ = ["elem_size", "tree", "default", "op"]

    def __init__(self, a: list, default: int, op):
        from math import ceil, log
        real_size = len(a)
        self.elem_size = elem_size = 1 << ceil(log(real_size, 2))
        self.tree = tree = [default] * (elem_size * 2)
        tree[elem_size:elem_size + real_size] = a
        self.default = default
        self.op = op

        for i in range(elem_size - 1, 0, -1):
            tree[i] = op(tree[i << 1], tree[(i << 1) + 1])

    def get_value(self, x: int, y: int) -> int:  # 半開区間
        l, r = x + self.elem_size, y + self.elem_size
        tree, result, op = self.tree, self.default, self.op
        while l < r:
            if l & 1:
                result = op(tree[l], result)
                l += 1
            if r & 1:
                r -= 1
                result = op(tree[r], result)
            l, r = l >> 1, r >> 1

        return result

    def set_value(self, i: int, value: int) -> None:
        k = self.elem_size + i
        self.tree[k] = value
        self.update(k)

    def update(self, i: int) -> None:
        op, tree = self.op, self.tree
        while i > 1:
            i >>= 1
            tree[i] = op(tree[i << 1], tree[(i << 1) + 1])


rcur = 0
lcur = 0
rmax = 0
lmax = 0
r = [[0,0]]
for i in range(n):
    a,b = x[i]
    rcur += b
    lcur = a
    if rmax < rcur-lcur:
        rmax = rcur-lcur
        lmax = lcur
    r.append([rmax,lmax])

ans = 0
cur = 0
lcur = 0
ans = r[-1][0]

for i in range(1,n+1):
    lcur = x[-i][0]
    cur += x[-i][1]
    count = cur-(c-lcur)+r[-i-1][0] - min(c-lcur,r[-1-i][1])
    ans = max(ans,count)
    
print(ans)

Submission status: Wrong Answer

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

class SegmentTree(object):
    __slots__ = ["elem_size", "tree", "default", "op"]

    def __init__(self, a: list, default: int, op):
        from math import ceil, log
        real_size = len(a)
        self.elem_size = elem_size = 1 << ceil(log(real_size, 2))
        self.tree = tree = [default] * (elem_size * 2)
        tree[elem_size:elem_size + real_size] = a
        self.default = default
        self.op = op

        for i in range(elem_size - 1, 0, -1):
            tree[i] = op(tree[i << 1], tree[(i << 1) + 1])

    def get_value(self, x: int, y: int) -> int:  # 半開区間
        l, r = x + self.elem_size, y + self.elem_size
        tree, result, op = self.tree, self.default, self.op
        while l < r:
            if l & 1:
                result = op(tree[l], result)
                l += 1
            if r & 1:
                r -= 1
                result = op(tree[r], result)
            l, r = l >> 1, r >> 1

        return result

    def set_value(self, i: int, value: int) -> None:
        k = self.elem_size + i
        self.tree[k] = value
        self.update(k)

    def update(self, i: int) -> None:
        op, tree = self.op, self.tree
        while i > 1:
            i >>= 1
            tree[i] = op(tree[i << 1], tree[(i << 1) + 1])


rcur = 0
lcur = 0
rmax = 0
lmax = 0
r = [[0,0]]
for i in range(n):
    a,b = x[i]
    rcur += b
    lcur = a
    if rmax < rcur-lcur:
        rmax = rcur-lcur
        lmax = lcur
    r.append([rmax,lmax])

l = [[0,0] for i in range(n+1)]
l[-2] = [x[-1][1]-(c-x[-1][0]),x[-1][0]]
for i in range(2,n+1):
    a,b = x[-i]
    s = l[-i][0]+b-(l[-i][1]-a)
    l[-i-1] = [s,a]
l[-1][1] = c


ans = 0

for i in range(n+1):
    count = l[i][0]+r[i][0] - min(c-l[i][1],r[i][1])
    ans = max(ans,count)
print(ans)

Submission status: Wrong Answer

Solution code:
N, C= map(int,input().split())
XV = sorted([list(map(int,input().split())) for _ in range(N)])
acmXV = []
dbl_acmXV = []
for i, xv in enumerate(XV):
  x,v = xv
  if i == 0:
    acmXV.append(v-x)
    dbl_acmXV.append(v-x*2)
  else:
    acmXV.append(v-(x-XV[i-1][0])+acmXV[i-1])
    dbl_acmXV.append(v-(x-XV[i-1][0])*2+dbl_acmXV[i-1])

acmXV_rev = [0 for _ in range(N)]
dbl_acmXV_rev = [0 for _ in range(N)]
for i in range(N-1, -1, -1):
  x,v = XV[i]
  if i == N-1:
    acmXV_rev[i] = v-(C-x)
    dbl_acmXV_rev[i] = v-(C-x)*2
  else:
    acmXV_rev[i] = (v-(XV[i+1][0]-XV[i][0])+acmXV_rev[i+1])
    dbl_acmXV_rev[i] = (v-(XV[i+1][0]-XV[i][0])*2+dbl_acmXV_rev[i+1])
    
ans = max(0,max(acmXV),max(acmXV_rev))
for i in range(N):
  for j in range(N-1,i+1,-1):
    #print(i,j,dbl_acmXV[i],acmXV_rev[N-1-j])
    ans = max(ans, dbl_acmXV[i]+acmXV_rev[j], acmXV[i]+dbl_acmXV_rev[j]) 
print(ans)

Submission status: Wrong Answer

Solution code:
N, C= map(int,input().split())
XV = sorted([list(map(int,input().split())) for _ in range(N)])
acmXV = []
dbl_acmXV = []
for i, xv in enumerate(XV):
  x,v = xv
  if i == 0:
    acmXV.append(v-x)
    dbl_acmXV.append(v-x*2)
  else:
    acmXV.append(v-(x-XV[i-1][0])+acmXV[i-1])
    dbl_acmXV.append(v-(x-XV[i-1][0])*2+dbl_acmXV[i-1])

acmXV_rev = [0 for _ in range(N)]
dbl_acmXV_rev = [0 for _ in range(N)]
for i in range(N-1, -1, -1):
  x,v = XV[i]
  if i == N-1:
    acmXV_rev[i] = v-(C-x)
    dbl_acmXV_rev[i] = v-(C-x)*2
  else:
    acmXV_rev[i] = (v-(XV[i+1][0]-XV[i][0])+acmXV_rev[i+1])
    dbl_acmXV_rev[i] = (v-(XV[i+1][0]-XV[i][0])*2+dbl_acmXV_rev[i+1])
    
ans = max(0,max(acmXV),max(acmXV_rev))
for i in range(N-1):
  for j in range(N-2,i+1,-1):
    #print(i,j,dbl_acmXV[i],acmXV_rev[N-1-j])
    ans = max(ans, dbl_acmXV[i]+acmXV_rev[j], acmXV[i]+dbl_acmXV_rev[j]) 
print(ans)

Submission status: Wrong Answer

Solution code:
N, C= map(int,input().split())
XV = sorted([list(map(int,input().split())) for _ in range(N)])
acmXV = []
dbl_acmXV = []
for i, xv in enumerate(XV):
  x,v = xv
  if i == 0:
    acmXV.append(v-x)
    dbl_acmXV.append(v-x*2)
  else:
    acmXV.append(v-(x-XV[i-1][0])+acmXV[i-1])
    dbl_acmXV.append(v-(x-XV[i-1][0])*2+dbl_acmXV[i-1])

acmXV_rev = [0 for _ in range(N)]
dbl_acmXV_rev = [0 for _ in range(N)]
for i in range(N-1, -1, -1):
  x,v = XV[i]
  if i == N-1:
    acmXV_rev[i] = v-(C-x)
    dbl_acmXV_rev[i] = v-(C-x)*2
  else:
    acmXV_rev[i] = (v-(XV[i+1][0]-XV[i][0])+acmXV_rev[i+1])
    dbl_acmXV_rev[i] = (v-(XV[i+1][0]-XV[i][0])*2+dbl_acmXV_rev[i+1])
    
ans = max(0,max(acmXV),max(acmXV_rev))
for i in range(N-1):
  for j in range(N-1,i+1,-1):
    #print(i,j,dbl_acmXV[i],acmXV_rev[N-1-j])
    ans = max(ans, dbl_acmXV[i]+acmXV_rev[j], acmXV[i]+dbl_acmXV_rev[j]) 
print(ans)