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:
import numpy as np
import sys
input = sys.stdin.readline  #文字列入力では注意！
from itertools import accumulate
N,C = map(int,input().split())
XV = [list(map(int,input().split())) for i in range(N)]
x,v = map(list,zip(*XV))

form = [0]+list(accumulate(v))
latt = np.array([0]+list(accumulate(reversed(v))),dtype=np.int64)
lat = latt - (C-np.array(x+[C],dtype=np.int64)[::-1])
x = [0]+x+[C]
a = [0]
cal = 0
for i in range(1,N+1):
    if lat[i]>cal:
        a.append(i)
        cal = lat[i]
    else:
        a.append(a[i-1])

ans = 0
for i in range(N+1):
    tmp = form[i]+latt[a[N-i]] -(x[i]+C-x[-a[N-i]-1]+min(x[i],C-x[-a[N-i]-1]))
    ans = max(ans,tmp)
print(int(ans))

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
input = sys.stdin.readline  #文字列入力では注意！
from itertools import accumulate
N,C = map(int,input().split())
XV = [list(map(int,input().split())) for i in range(N)]
x,v = map(list,zip(*XV))

form = [0]+list(accumulate(v))
latt = np.array([0]+list(accumulate(reversed(v))))
lat = latt - (C-np.array(x+[C])[::-1])
x = [0]+x+[C]
a = [0]
cal = 0
for i in range(1,N+1):
    if lat[i]>cal:
        a.append(i)
        cal = lat[i]
    else:
        a.append(a[i-1])

ans = 0
for i in range(N+1):
    tmp = form[i]+latt[a[N-i]] -(x[i]+C-x[-a[N-i]-1]+min(x[i],C-x[-a[N-i]-1]))
    ans = max(ans,tmp)
print(int(ans))

Submission status: Wrong Answer

Solution code:
import numpy as np
import sys
input = sys.stdin.readline  #文字列入力では注意！
from itertools import accumulate
N,C = map(int,input().split())
XV = [list(map(int,input().split())) for i in range(N)]
XV.sort(key=lambda x: x[0])
x,v = map(list,zip(*XV))

form = [0]+list(accumulate(v))
latt = np.array([0]+list(accumulate(reversed(v))))
lat = latt - (C-np.array(x+[C])[::-1])
x = [0]+x+[C]

ans = 0
for i in range(N+1):
    t = np.argmax(lat[:N+1-i])
    tmp = form[i]+latt[t] -(x[i]+C-x[-t-1]+min(x[i],C-x[-t-1]))
    ans = max(ans,tmp)
print(ans)

Submission status: Wrong Answer

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

# æ™‚è¨ˆå›žã‚Šã‚’è©¦ã™
ans, value = 0, 0
for i in L:
  cost = i[0]
  value += i[1]
  ans = max(ans, value - cost)

# åæ™‚è¨ˆå›žã‚Šã‚’è©¦ã™
value = 0
for i in L[::-1]:
  cost = C - i[0]
  value += i[1]
  ans = max(ans, value - cost)

# æ™‚è¨ˆå›žã‚Šãƒ»åæ™‚è¨ˆå‘¨ã‚Šã®æœ€å¤§å€¤ã‚’æ±‚ã‚ã‚‹
maxR, value = 0, 0
for i in L:
  cost = i[0]
  value += i[1]
  if C // 2 < cost: break
  maxR = max(maxR, value - cost)

maxL, value = 0, 0
for i in L[::-1]:
  cost = C - i[0]
  value += i[1]
  if (C + 1) // 2 <= cost: break
  maxL = max(maxL, value - cost)

# æ™‚è¨ˆå›žã‚Š â†’ åæ™‚è¨ˆå›žã‚Šã‚’è©¦ã™
value = 0
for i in L:
  cost = i[0]
  value += i[1]
  if C // 2 < cost: break
  ans = max(ans, maxL + value - 2 * cost)

# åæ™‚è¨ˆå›žã‚Š â†’ æ™‚è¨ˆå›žã‚Šã‚’è©¦ã™
value = 0
for i in L[::-1]:
  cost = C - i[0]
  value += i[1]
  if (C + 1) // 2 <= cost: break
  ans = max(ans, maxR + value - 2 * cost)

print(ans)

Submission status: Wrong Answer

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

# æ™‚è¨ˆå›žã‚Šã‚’è©¦ã™
ans, value = 0, 0
for i in L:
  cost = i[0]
  value += i[1]
  ans = max(ans, value - cost)

# åæ™‚è¨ˆå›žã‚Šã‚’è©¦ã™
value = 0
for i in L[::-1]:
  cost = C - i[0]
  value += i[1]
  ans = max(ans, value - cost)

# æ™‚è¨ˆå›žã‚Šãƒ»åæ™‚è¨ˆå‘¨ã‚Šã®æœ€å¤§å€¤ã‚’æ±‚ã‚ã‚‹
maxR, value = 0, 0
for i in L:
  cost = i[0]
  value += i[1]
  if C // 2 < cost: break
  maxR = max(maxR, value - cost)

maxL, value = 0, 0
for i in L[::-1]:
  cost = C - i[0]
  value += i[1]
  if C // 2 <= cost: break
  maxL = max(maxL, value - cost)

# æ™‚è¨ˆå›žã‚Š â†’ åæ™‚è¨ˆå›žã‚Šã‚’è©¦ã™
value = 0
for i in L:
  cost = i[0]
  value += i[1]
  if C // 2 < cost: break
  ans = max(ans, maxL + value - 2 * cost)

# åæ™‚è¨ˆå›žã‚Š â†’ æ™‚è¨ˆå›žã‚Šã‚’è©¦ã™
value = 0
for i in L[::-1]:
  cost = C - i[0]
  value += i[1]
  if (C + 1) // 2 <= cost: break
  ans = max(ans, maxR + value - 2 * cost)

print(ans)

Submission status: Wrong Answer

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

# æ™‚è¨ˆå›žã‚Šã‚’è©¦ã™
ans, value = 0, 0
for i in L:
  cost = i[0]
  value += i[1]
  ans = max(ans, value - cost)

# åæ™‚è¨ˆå›žã‚Šã‚’è©¦ã™
value = 0
for i in L[::-1]:
  cost = C - i[0]
  value += i[1]
  ans = max(ans, value - cost)

# æ™‚è¨ˆå›žã‚Šãƒ»åæ™‚è¨ˆå‘¨ã‚Šã®æœ€å¤§å€¤ã‚’æ±‚ã‚ã‚‹
maxR, value = 0, 0
for i in L:
  cost = i[0]
  value += i[1]
  if C // 2 < cost: break
  maxR = max(maxR, value - cost)

maxL, value = 0, 0
for i in L[::-1]:
  cost = C - i[0]
  value += i[1]
  if C // 2 <= cost: break
  maxL = max(maxL, value - cost)

# æ™‚è¨ˆå›žã‚Š â†’ åæ™‚è¨ˆå›žã‚Šã‚’è©¦ã™
value = 0
for i in L:
  cost = i[0]
  value += i[1]
  if C // 2 < cost: break
  ans = max(ans, maxL + value - 2 * cost)

# åæ™‚è¨ˆå›žã‚Š â†’ æ™‚è¨ˆå›žã‚Šã‚’è©¦ã™
value = 0
for i in L[::-1]:
  cost = C - i[0]
  value += i[1]
  if C // 2 <= cost: break
  ans = max(ans, maxR + value - 2 * cost)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N,C=map(int,input().split())
x=[]
v=[0]
for i in range(N):
    xx,vv=map(int,input().split())
    x.append(xx)
    v.append(v[i]+vv)

cw=[0]
for i in range(N):
    cw.append(max(cw[i],v[i+1]-x[i]))

ccw=[0]
for i in range(N):
    ccw.append(max(ccw[i],v[N]-v[N-i-1]-(C-x[-i-1])))

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

ccw_cw=0
for i in range(N-1):
    ccw_cw=max(ccw_cw,v[N]-v[N-i-1]-(C-x[i-1])*2+cw[N-i-1])
#print(cw,ccw)
#print(list([cw_ccw,ccw_cw]))
print(max(cw+ccw+list([cw_ccw,ccw_cw])))

Submission status: Wrong Answer

Solution code:
# D - Static Sushi

import sys
import numpy as np

N, C = map(int, sys.stdin.buffer.readline().split())
x, v = [], []
for _ in range(N):
    tmp, tmp2 = map(int, sys.stdin.buffer.readline().split())
    x.append(tmp)
    v.append(tmp2)

x = np.array(x, np.int64)
v = np.array(v, np.int64)
v_cum = v.cumsum()
tx = np.flipud(x)
tv_cum = np.flipud(v).cumsum()
left_cum = v_cum - x
right_cum = tv_cum - np.flipud(C - x)
right_cum_max = np.maximum.accumulate(right_cum)
right_cum_return_idx = [0]

for i in range(1,N):
    if right_cum_max[right_cum_return_idx[-1]] < right_cum_max[i]:
        right_cum_return_idx.append(i)
    else:
        right_cum_return_idx.append(right_cum_return_idx[-1])

ans = max(0, np.max(left_cum), np.max(right_cum))

for first_abandon_idx in range(1, N):
    ans = max(ans, left_cum[first_abandon_idx-1] - x[first_abandon_idx-1] + right_cum_max[N-1 - (first_abandon_idx)])
    ans = max(ans, left_cum[first_abandon_idx-1] - (C - x[N-1 - right_cum_return_idx[N-1 - (first_abandon_idx)]]) + right_cum_max[N-1 - (first_abandon_idx)])

print(ans)

Submission status: Wrong Answer

Solution code:
# D - Static Sushi

import sys
import numpy as np

N, C = map(int, sys.stdin.buffer.readline().split())
x, v = [], []
for _ in range(N):
    tmp, tmp2 = map(int, sys.stdin.buffer.readline().split())
    x.append(tmp)
    v.append(tmp2)

x = np.array(x, np.int64)
v = np.array(v, np.int64)
v_cum = v.cumsum()
tx = np.flipud(x)
tv_cum = np.flipud(v).cumsum()
left_cum = v_cum - x
right_cum = tv_cum - np.flipud(C - x)
right_cum_max = np.maximum.accumulate(right_cum)
right_cum_return_idx = [0]

for i in range(1,N):
    if right_cum_max[right_cum_return_idx[-1]] < right_cum_max[i]:
        right_cum_return_idx.append(i)
    else:
        right_cum_return_idx.append(right_cum_return_idx[-1])

ans = max(0, np.max(left_cum), np.max(right_cum))

for first_abandon_idx in range(1, N-1):
    ans = max(ans, left_cum[first_abandon_idx-1] - x[first_abandon_idx-1] + right_cum_max[N-1 - (first_abandon_idx+1)])
    ans = max(ans, left_cum[first_abandon_idx-1] - (C - x[N-1 - right_cum_return_idx[N-1 - (first_abandon_idx+1)]]) + right_cum_max[N-1 - (first_abandon_idx+1)])

print(ans)

Submission status: Wrong Answer

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

posision = 0
profit = 0
for x, v in xv:
    cost_r = abs(posision - x)
    cost_l = abs(C - cost_r)
    profit_i = v - min(cost_r,  cost_l)
    if profit_i < 0:
        pass
    else:
        profit += profit_i
        posision = x
print(profit)

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import time
#import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase

#sys.setrecursionlimit(10**5)
input=lambda: sys.stdin.readline().rstrip()

show_flg=False
show_flg=True

n,c=LI()
s=[]

for i in range(n):
    x,v=LI()
    s.append((x,v))

def solve(s):
    rt=-inf
    n=len(s)
    av=[0]
    for i in range(n):
        av.append(av[-1]+s[i][1])
    
    rav=[0]
    for i in range(n):
        rav.append(rav[-1]+s[-i-1][1])
    
    f=[-inf]*(n+1)
    for i in range(n):
        f[i+1]=max(av[i+1]-s[i][0],f[i])
    f.append(-inf)
    
    for i in range(1,n+1):
        rt=max(rt,rav[i]-(c-s[-i][0]),rav[i]-(c-s[-i][0])*2+f[n-i-1])
    return rt

ans=solve(s)
ans=max(ans,solve([(c-x,v) for x,v in s[::-1]]),0)
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import time
#import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase

#sys.setrecursionlimit(10**5)
input=lambda: sys.stdin.readline().rstrip()

show_flg=False
show_flg=True

n,c=LI()
s=[]

for i in range(n):
    x,v=LI()
    s.append((x,v))


def solve(s):
    
    rt=-inf
    n=len(s)
    av=[0]
    for i in range(n):
        av.append(av[-1]+s[i][1])
    
    rav=[0]
    for i in range(n):
        rav.append(rav[-1]+s[-i-1][1])
    
    f=[-inf]*(n+1)
    for i in range(n):
        f[i+1]=max(av[i+1]-s[i][0],f[-1])
    f.append(0)
    
    for i in range(1,n+1):
        rt=max(rt,rav[i]-(c-s[-i][0]),rav[i]-(c-s[-i][0])*2+f[n-i-1])
    
    return rt

ans=solve(s)
ans=max(ans,solve([(c-x,v) for x,v in s[::-1]]),0)
print(ans)

Submission status: Wrong Answer

Solution code:
N, C = map(int, input().split())
sushi = [(0, 0)]
for _ in range(N):
    x, v = map(int, input().split())
    sushi.append((x, v))
# preprocess
cumsum = [0]
cumsum_max = [(0, 0)]
prev = 0
for x, v in sushi[1:]:
    cumsum.append(cumsum[-1] + prev + v - x)
    cumsum_max.append(cumsum_max[-1] if cumsum_max[-1][1] > cumsum[-1] else (x, cumsum[-1]))
    prev = x
cumsum_rev = [(0, 0)]
prev = 0
for x, v in reversed(sushi[1:]):
    cumsum_rev.append((C - x, cumsum_rev[-1][1] + prev + v - (C - x)))
    prev = C - x
ans = 0
for i in range(N+1):
    temp = cumsum_max[N-i][1] + cumsum_rev[i][1] - min(cumsum_max[N-i][0], cumsum_rev[i][0])
    ans = max(ans, temp)
print(ans)

Submission status: Wrong Answer

Solution code:
# D - Static Sushi

import sys
import numpy as np

N, C = map(int, sys.stdin.buffer.readline().split())
x, v = [], []
for _ in range(N):
    tmp, tmp2 = map(int, sys.stdin.buffer.readline().split())
    x.append(tmp)
    v.append(tmp2)

x = np.array(x, np.int64)
v = np.array(v, np.int64)
v_cum = v.cumsum()
tx = np.flipud(x)
tv_cum = np.flipud(v).cumsum()
left_cum = [v_cum[idx] - x[idx] for idx in range(N)]
right_cum = [tv_cum[idx] - (C - tx[idx]) for idx in range(N)]

ans = max(0, np.max(left_cum), np.max(right_cum))

for first_abandon_idx in range(1, N-1):
    for last_abandon_idx in range(first_abandon_idx, N-1):
        ans = max(ans, left_cum[first_abandon_idx-1] - x[first_abandon_idx-1] + right_cum[N-1 - (last_abandon_idx+1)])
        ans = max(ans, left_cum[first_abandon_idx-1] - (C - x[last_abandon_idx+1]) + right_cum[N-1 - (last_abandon_idx+1)])

print(ans)

Submission status: Wrong Answer

Solution code:
# D - Static Sushi

import sys, bisect
import numpy as np

N, C = map(int, sys.stdin.buffer.readline().split())
x, v = [], []
for _ in range(N):
    tmp, tmp2 = map(int, sys.stdin.buffer.readline().split())
    x.append(tmp)
    v.append(tmp2)

x = np.array(x, np.int64)
v = np.array(v, np.int64)
v_cum = v.cumsum()
tx = np.flipud(x)
tv_cum = np.flipud(v).cumsum()

half_idx = bisect.bisect_left(x, C//2)
left_cum = [v_cum[idx] - x[idx] for idx in range(N)]
right_cum = [tv_cum[idx] - (C - tx[idx]) for idx in range(N)]

if half_idx == N:
    print(max(0, np.max(left_cum)))
    exit()
elif half_idx == 0:
    print(max(0, np.max(right_cum)))
    exit()

ans = max(0, np.max(left_cum), np.max(right_cum))

for left_turn_idx in range(half_idx+1):
    opposite_idx = bisect.bisect_left(x, x[left_turn_idx] + C//2)
    if opposite_idx == N:
        break
    else:
        for drop_idx in range(opposite_idx-1, N):
            ans = max(ans, left_cum[left_turn_idx] - x[left_turn_idx] + right_cum[N-1-drop_idx])

for right_turn_idx in reversed(range(half_idx-1, N)):
    opposite_idx = bisect.bisect_left(x, x[right_turn_idx] - C//2)
    if opposite_idx == 0:
        break
    else:
        for drop_idx in range(opposite_idx + 1):
            ans = max(ans, left_cum[drop_idx] - (C - x[right_turn_idx]) + right_cum[N-1-right_turn_idx])

print(ans)

Submission status: Wrong Answer

Solution code:
# D - Static Sushi

import sys, bisect
import numpy as np

N, C = map(int, sys.stdin.buffer.readline().split())
x, v = [], []
for _ in range(N):
    tmp, tmp2 = map(int, sys.stdin.buffer.readline().split())
    x.append(tmp)
    v.append(tmp2)

x = np.array(x, np.int64)
v = np.array(v, np.int64)
v_cum = v.cumsum()
tx = np.flipud(x)
tv_cum = np.flipud(v).cumsum()

half_idx = bisect.bisect_left(x, C//2)
left_cum = [v_cum[idx] - x[idx] for idx in range(N)]
right_cum = [tv_cum[idx] - (C - tx[idx]) for idx in range(N)]

if half_idx == N:
    print(max(0, np.max(left_cum)))
    exit()
elif half_idx == 0:
    print(max(0, np.max(right_cum)))
    exit()

ans = max(0, np.max(left_cum), np.max(right_cum))

for left_turn_idx in range(half_idx):
    opposite_idx = bisect.bisect_left(x, x[left_turn_idx] + C//2)
    if opposite_idx == N:
        break
    else:
        for drop_idx in range(opposite_idx, N):
            ans = max(ans, left_cum[left_turn_idx] - x[left_turn_idx] + right_cum[N-1-drop_idx])

for right_turn_idx in reversed(range(half_idx, N)):
    opposite_idx = bisect.bisect_left(x, x[right_turn_idx] - C//2)
    if opposite_idx == 0:
        break
    else:
        for drop_idx in range(opposite_idx + 1):
            ans = max(ans, left_cum[drop_idx] - (C - x[right_turn_idx]) + right_cum[N-1-right_turn_idx])

print(ans)

Submission status: Wrong Answer

Solution code:
# D - Static Sushi

import sys, bisect
import numpy as np

N, C = map(int, sys.stdin.buffer.readline().split())
x, v = [], []
for _ in range(N):
    tmp, tmp2 = map(int, sys.stdin.buffer.readline().split())
    x.append(tmp)
    v.append(tmp2)

x = np.array(x, np.int64)
v = np.array(v, np.int64)
v_cum = v.cumsum()
tx = np.flipud(x)
tv_cum = np.flipud(v).cumsum()

half_idx = bisect.bisect_left(x, C//2)
left_cum = [v_cum[idx] - x[idx] for idx in range(N)]
right_cum = [tv_cum[idx] - (C - tx[idx]) for idx in range(N)]

if half_idx == N:
    print(max(0, np.max(left_cum)))
    exit()
elif half_idx == 0:
    print(max(0, np.max(right_cum)))
    exit()

ans = max(0, np.max(left_cum), np.max(right_cum))

for left_turn_idx in range(half_idx):
    opposite_idx = bisect.bisect_left(x, x[left_turn_idx] + C//2)
    if opposite_idx == N:
        break
    else:
        for drop_idx in range(opposite_idx, N):
            ans = max(ans, left_cum[left_turn_idx] - x[left_turn_idx] + right_cum[N-1-drop_idx])

for right_turn_idx in reversed(range(half_idx, N)):
    opposite_idx = bisect.bisect_left(x, x[right_turn_idx] - C//2)
    if opposite_idx == 0:
        break
    else:
        for drop_idx in range(opposite_idx):
            ans = max(ans, left_cum[drop_idx] - (C - x[right_turn_idx]) + right_cum[N-1-right_turn_idx])

print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
#import sympy as syp(素因数分解とか)
Mod = 1000000007
import sys

sys.setrecursionlimit(100000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

def factorial(i):
    if i == 1:
        return 1
    else:
        return i*factorial(i-1)
 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)




def main(): #startline--------------------------------------------
    N,C = map(int,input().split())
    xv = [tuple(map(int,input().split())) for i in range(N)]
    r1,r2,l1,l2= [0]*(N+1),[0]*(N+1),[0]*(N+1),[0]*(N+1)
    ans = 0
    dis = 0
    for i in range(N):
        r1[i+1] = r1[i]+xv[i][1]-(xv[i][0]-dis)
        r2[i+1] = r2[i]+xv[i][1]-2*(xv[i][0]-dis)
        dis = xv[i][0]
    dis = C
    for i in reversed(range(N)):
        l1[N-i]=l1[N-i-1]+xv[i][1]-(dis-xv[i][0])
        l2[N-i]=l2[N-i-1]+xv[i][1]-2*(dis-xv[i][0])
        dis = xv[i][0]
    for i in range(N):
        l1[i+1] = max(l1[i+1],l1[i])
        l2[i+1] = max(r1[i+1],r1[i])
    for i in range(N+1):
        subans = max(r2[i]+l1[N-i],l2[i]+r1[N-i])
        ans = max(ans,subans)
    print(ans)
if __name__ == "__main__":
    main() #endline===============================================

Submission status: Wrong Answer

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
#import sympy as syp(素因数分解とか)
Mod = 1000000007
import sys

sys.setrecursionlimit(100000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

def factorial(i):
    if i == 1:
        return 1
    else:
        return i*factorial(i-1)
 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)




def main(): #startline--------------------------------------------
    N,C = map(int,input().split())
    xv = [tuple(map(int,input().split())) for i in range(N)]
    r1,r2,l1,l2= [0]*(N+1),[0]*(N+1),[0]*(N+1),[0]*(N+1)
    ans = 0
    dis = 0
    for i in range(N):
        r1[i+1] = r1[i]+xv[i][1]-(xv[i][0]-dis)
        r2[i+1] = r2[i]+xv[i][1]-2*(xv[i][0]-dis)
        dis = xv[i][0]
    dis = C
    for i in reversed(range(N)):
        l1[N-i]=l1[N-i-1]+xv[i][1]-(dis-xv[i][0])
        l2[N-i]=l2[N-i-1]+xv[i][1]-2*(dis-xv[i][0])
        dis = xv[i][0]
    for i in range(N):
        l1[i+1] = max(l1[i+1],l1[i])
        l2[i+1] = max(r1[i+1],r1[i])
    for i in range(N):
        subans = max(r2[i]+l1[N-i],l2[i]+r1[N-i])
        ans = max(ans,subans)
    print(ans)
if __name__ == "__main__":
    main() #endline===============================================

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
from collections import deque
def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))
import math
import bisect


# class SegTree():
#     def __init__(self, n):
#         self.value = [0 for i in range(n*2)]

# MOD = 10 ** 9 + 7

# def getinvmod(n):
#     return [pow(i, MOD-2, MOD) for i in range(n+1)]

def main():
    n, m = getList()
    sushis = []
    for i in range(n):
        sushis.append(getList())

    tmp = 0
    acc = [(0, 0)]
    acc2 = [0]
    acc2idx = [0]

    for sushi in sushis:
        tmp += sushi[1]
        if tmp - sushi[0] > acc[-1][1]:
            acc.append((sushi[0], tmp - sushi[0]))
        if tmp - 2 * sushi[0] > acc2[-1]:
            acc2.append(tmp - sushi[0] * 2)
            acc2idx.append(sushi[0])

    tmp = 0
    racc = [(0, 0)]
    racc2 = [0]
    racc2idx = [0]

    for sushi in sushis[::-1]:
        tmp += sushi[1]
        dist = m - sushi[0]
        if tmp - dist > racc[-1][1]:
            racc.append((sushi[0], tmp - dist))
        if tmp - 2 * dist > racc2[-1]:
            racc2.append(tmp - 2 * dist)
            racc2idx.append(dist)


    ans = 0
    for asu in acc:
        tmp = asu[1]
        idx = bisect.bisect_left(racc2idx, n - asu[0])
        tmp += racc2[idx - 1]
        if tmp > ans:
            ans = tmp
            # print(asu, tmp)

    for asu in racc:
        tmp = asu[1]
        idx = bisect.bisect_left(acc2idx, asu[0])
        tmp += acc2[idx - 1]
        if tmp > ans:
            ans = tmp
            # print(asu, tmp)

    print(ans)
    # print(acc, acc2)
    # print(racc, racc2, racc2idx)

    return
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#設定
import sys
input = sys.stdin.buffer.readline

#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N, C = getlist()
	left1sum = [0]
	right1sum = [0]
	left2sum = [0]
	right2sum = [0]
	X = []
	V = []
	for i in range(N):
		x, v = getlist()
		X.append(x)
		V.append(v)
	for i in range(N):
		left1sum.append(left1sum[-1] + V[-1 - i])
		left2sum.append(left2sum[-1] + V[-1 - i])
		right1sum.append(right1sum[-1] + V[i])
		right2sum.append(right2sum[-1] + V[i])
	# print(right1sum)
	# print(right2sum)
	# print(left1sum)
	# print(left2sum)
	for i in range(N):
		right1sum[i + 1] -= X[i]
		right2sum[i + 1] -= 2 * X[i]
		left1sum[i + 1] -= C - X[-1 - i]
		left2sum[i + 1] -= 2 * (C - X[-1 - i])

	for i in range(N):
		right1sum[i + 1] = max(right1sum[i + 1], right1sum[i])
		right2sum[i + 1] = max(right2sum[i + 1], right2sum[i])
		left1sum[i + 1] = max(left1sum[i + 1], left1sum[i])
		left2sum[i + 1] = max(left2sum[i + 1], left2sum[i])
	# print(right1sum)
	# print(right2sum)
	# print(left1sum)
	# print(left2sum)
	ans = 0
	for i in range(N):
		ans = max(ans, right1sum[i] + left2sum[N - i])
		ans = max(ans, right2sum[i] + left1sum[N - i])
	print(ans)

if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
def main():
    from itertools import accumulate as ac
    n,c=map(int,input().split())
    xv=[list(map(int,input().split())) for _ in [0]*n]
    rmemo=[0]*n
    lmemo=[0]*n
    rmemo[0]=xv[0][1]-xv[0][0]
    lmemo[n-1]=xv[n-1][1]-(c-xv[n-1][0])
    for i in range(1,n):
        rmemo[i]=rmemo[i-1]+xv[i][1]-xv[i][0]+xv[i-1][0]
    for i in range(n-2,-1,-1):
        lmemo[i]=lmemo[i+1]+xv[i][1]+xv[i][0]-xv[i+1][0]

    temp=rmemo[0]
    rmemo2=[temp]*(n+1)
    rmemo2[-1]=0
    temp=lmemo[-1]
    lmemo2=[temp]*(n+1)
    lmemo2[-1]=0
    for i in range(1,n):
        rmemo2[i]=max(rmemo[i],rmemo2[i-1])
    for i in range(n-2,-1,-1):
        lmemo2[i]=max(lmemo[i],lmemo2[i+1])
    #print(rmemo2)
    #print(lmemo)
    ans=max(rmemo+lmemo+[0])
    for i in range(n):
        ans=max(ans,rmemo[i]+lmemo2[i+1]-xv[i][0])
        ans=max(ans,lmemo[i]+rmemo2[i+1]+xv[i][0]-c)
    print(ans)
main()

Submission status: Wrong Answer

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

N, C, *xv = map(int, read().split())
x, v = zip(*zip(*[iter(xv)] * 2))
v = np.array(v, np.int64)
x = np.array(x, np.int64)
ax = C - x
ax = ax[::-1]
cx = np.array(x, np.int64)
cx[1:] -= cx[:-1]
acx = C - np.array(x, np.int64)
acx[:-1] -= acx[1:]

clockwise = np.cumsum(v - cx)
anticlockwise = v - acx
anticlockwise = np.cumsum(anticlockwise[::-1])

con_max = np.maximum.accumulate(clockwise)
anti_max = np.maximum.accumulate(anticlockwise)

answer = [0]
answer.append(clockwise.max())
answer.append(anticlockwise.max())

for i in range(N - 1):
    answer.append(clockwise[i] - x[i] + anti_max[N - i - 2])
    answer.append(anticlockwise[i] - ax[i] + con_max[N - i - 2])

print(max(answer))