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:
N,C = map(int,input().split())
table=[]
for i in range(N):
    x,v=map(int,input().split())
    table.append([x,v])
V=[0]*N
V[0]=table[0][1]
for i in range(1,N):
    V[i]=V[i-1]+table[i][1]
ans=0
for i in range(N):
    ans=max(ans,V[i]-table[i][0])
for i in range(N-1,0,-1):
    ans = max(ans,V[i]-V[i-1]-C+table[i][0])
ans=max(ans,table[0][1]-C+table[0][0])
for i in range(N):
    for k in range(i+1,N):
        ans = max(ans, V[i]+V[N-1]-V[k-1]-C-2*table[i][0]+table[k][0])
        ans = max(ans,V[i]+V[N-1]-V[k-1]-table[i][0]-2*C+2*table[k][0])
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
import sys

# 標準入力
data = tuple(map(int, sys.stdin.readline().strip().split(' ')))
n = data[0]
c = data[1]


plates = []

plates.append([0, 0])
for i in range(n):
    plates.append(list(map(int, sys.stdin.readline().strip().split(' '))))
plates.append([c, 0])

# カロリーの累積和を計算。一緒にそれぞれの最大カロリーになる箇所を取得しておく。
leftdp = [] 
rightdp = []
leftcalorie = 0
rightcalorie = 0
leftMaxIndex = 0
rightMaxIndex = 0
length = len(plates)
for i in range(length):
    leftcalorie = leftcalorie + plates[i][1]
    rightcalorie = rightcalorie + plates[length - 1 - i][1]

    leftdp.append(leftcalorie - plates[i][0])
    rightdp.append(rightcalorie - (c - plates[length - 1 - i][0]))

    if (leftdp[leftMaxIndex] < leftdp[-1]):
        leftMaxIndex = i
    if (rightdp[rightMaxIndex] < rightdp[-1]):
        rightMaxIndex = i


# 右回り、左回りどちらが最大になるか
if (leftdp[leftMaxIndex] > rightdp[rightMaxIndex]):
    rightMaxIndex = 0
    for i in range(1, length - 1 - leftMaxIndex):
        if (rightdp[rightMaxIndex] < rightdp[i]):
            rightMaxIndex = i
else:
    leftMaxIndex = 0
    for i in range(1, length - 1 - rightMaxIndex):
        if (leftdp[leftMaxIndex] < leftdp[i]):
            leftMaxIndex = i


# 右回り、左回りのそれぞれの最大値までの距離から、どちら方向に最初移動するかを決定してカロリーを算出
calorie = leftdp[leftMaxIndex] + rightdp[rightMaxIndex]
if (plates[leftMaxIndex][0] < c - plates[length - 1 - rightMaxIndex][0]):
    calorie = calorie - plates[leftMaxIndex][0]
else:
    calorie = calorie - (c - plates[length - 1 - rightMaxIndex][0])


# 標準出力
print(calorie)

Submission status: Wrong Answer

Solution code:

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

dp_r = []
for i in range(N):
    dp_r.append(sum(vs[:i+1]) - xs[i])
dp_l = []
rev_vs = list(reversed(vs))
rev_xs = list(reversed(xs))
for i in range(N):
    dp_l.append(sum(rev_vs[:i+1]) - (C-rev_xs[i]))

max_right = max(dp_r)
max_left = max(dp_l)

max_bothes = [0]
for i in range(1, N-1):
    mr = max(dp_r[:i])
    max_bothes.append(mr - xs[dp_r.index(mr)] + max(dp_l[:N-dp_r.index(mr)-1]))
max_both = max(max_bothes)

print(max([0, max_right, max_left, max_both]))

Submission status: Wrong Answer

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)
            print(i,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: Wrong Answer

Solution code:
import numpy as np
n, c = map(int, input().split())

x_cw = np.empty(n, dtype=np.int64)
v = np.empty(n, dtype=np.int64)
for i in range(n):
    x_cw[i], v[i] = map(np.int64, input().split())

v_cw = v.cumsum(dtype=np.int64) - x_cw
v_cw_r = v_cw - x_cw
x_ccw = c - x_cw[::-1]
v_ccw = v[::-1].cumsum(dtype=np.int64) - x_ccw
v_ccw_r = v_ccw - x_ccw

cw_argmax = v_cw.argmax()
cw_r_argmax = v_cw_r.argmax()
ccw_argmax = v_ccw.argmax()
ccw_r_argmax = v_ccw_r.argmax()

ans = 0
if cw_argmax + ccw_r_argmax < n-1:
    ans = max(ans, v_cw[cw_argmax] + max(v_ccw_r[ccw_r_argmax], 0))
else:
    for i in range(n-ccw_r_argmax-1, cw_argmax+1):
        if n-i-2 < 0:
            ans = max(ans, v_cw[i])
        elif n-i-2 ==0:
            ans = max(ans, v_cw[i] + max(v_ccw_r[n-i-2], 0))
        else:
            ans = max(ans, v_cw[i] + max(v_ccw_r[:n-i-2].max(), 0))

if ccw_argmax + cw_r_argmax < n-1:
    ans = max(ans, v_ccw[ccw_argmax] + max(v_cw_r[cw_r_argmax], 0))
else:
    for i in range(n-cw_r_argmax-1, ccw_argmax+1):
        if n-i-2 < 0:
            ans = max(ans, v_ccw[i])
        elif n-i-2 == 0:
            ans = max(ans, v_ccw[i] + max(v_cw_r[n-i-2], 0))
        else:
            ans = max(ans, v_ccw[i] + max(v_cw_r[:n-i-2].max(), 0))
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

# 右行って左に引き返す

a_list = [0]

for i in range(0, len(v_list)):
    if i > 0:
        l = a_list[i] + v_list[i] - (x_list[i] - x_list[i - 1])
        a_list.append(l)
    elif i == 0:
        l = v_list[i] - (x_list[i])
        a_list.append(l)

a_accum_max = [max(a_list[:x+1]) for x in range(0, len(a_list))]


b_list = [0]

for i in reversed(range(0, len(v_list))):
    if i == N-1:
        l = v_list[i] - 2 * (C - x_list[i])
        b_list.append(l)
    elif i < N-1:
        l = b_list[N - i - 1] + v_list[i] - 2 * (x_list[i+1] -x_list[i])
        b_list.append(l)

first_list = []
for i in range(0, len(b_list)):
    first_list.append(a_accum_max[N-i] + b_list[i])

max_fir = max(first_list)

# 左行って右に引き返す
# 
# c_list = [0]
# 
# for i in range(0, len(v_list)):
#     if i > 0:
#         l = c_list[i] + v_list[i] - 2 * (x_list[i] - x_list[i - 1])
#         c_list.append(l)
#     elif i == 0:
#         l = v_list[i] - 2 * (x_list[i])
#         c_list.append(l)
# 
# c_accum_max = [max(c_list[:x+1]) for x in range(0, len(c_list))]
# 
# d_list = [0]
# 
# for i in reversed(range(0, len(v_list))):
#     if i == N-1:
#         l = v_list[i] - (C - x_list[i])
#         d_list.append(l)
#     elif i < N-1:
#         l = d_list[N - i - 1] + v_list[i] - (x_list[i+1] -x_list[i])
#         d_list.append(l)
# 
# second_list = []
# for i in range(0, len(d_list)):
#     second_list.append(c_accum_max[N-i] + d_list[i])

# max_sec = max(second_list)
max_num = max([max_fir, 0])
print(max_num)

Submission status: Wrong Answer

Solution code:
n,c=map(int,input().split())
x=[0]
v=[]
for i in range(n):
    a,b=map(int,input().split())
    x.append(a)
    v.append(b)
x1=[x[i+1]-x[i] for i in range(len(x)-1)]
x2=[c-x[-1]]+x1[:0:-1]
#print(x1,x2)
h=[0]
t=[0]
for i,j in zip(x2,reversed(v)):
    h.append(h[-1]+j+-i)
hm=[max(h[:-i]) for i in range(1,len(h))]+[0]
#print(h,hm)
m=0
for k,(i,j) in enumerate(zip(x1,v)):
    t.append(t[-1]+j-i)
    #print(m,t[-1],i)
    m=max(m,t[-1]+hm[k])
print(m)

Submission status: Wrong Answer

Solution code:
n,c=map(int,input().split())
x=[0]
v=[]
for i in range(n):
    a,b=map(int,input().split())
    x.append(a)
    v.append(b)
x1=[x[i+1]-x[i] for i in range(len(x)-1)]
x2=[c-x[-1]]+x1[:0:-1]
#print(x1,x2)
h=[0]
t=[0]
for i,j in zip(x2,reversed(v)):
    h.append(h[-1]+j+-i)
#print(h)
m=0
for k,(i,j) in enumerate(zip(x1,v)):
    t.append(t[-1]+j-i)
    #print(m)
    m=max(m,t[-1]+max(h[:-(k+1)]))
print(m)

Submission status: Wrong Answer

Solution code:
n,c= [int(i) for i in input().split()]
t_tk_r=0
t_md_r=0
g_tk_r=0
g_md_r=0
t_tk_m=0
t_md_m=0
g_tk_m=0
g_md_m=0
t_tk=[]
t_md=[]
g_tk=[]
g_md=[]
a=[]
d1=0
for i in range(n):
  a.append([int(i) for i in input().split()])
for d0,v0 in a:
  t_tk_r+=v0-(d0-d1)
  t_md_r+=v0-(d0-d1)*2
  t_tk_m=max(t_tk_m,t_tk_r)
  t_md_m=max(t_md_m,t_md_r)
  t_tk.append(t_tk_m)
  t_md.append(t_md_m)
  d1=d0
d1=0  
for d0,v0 in a[::-1]:
  g_tk_r+=v0-(c-d0)-d1
  g_md_r+=v0-((c-d0)-d1)*2
  g_tk_m=max(g_tk_m,g_tk_r)
  g_md_m=max(g_md_m,g_md_r)
  g_tk.append(g_tk_m)
  g_md.append(g_md_m)
  d1=(c-d0) 
mx=max(max(t_tk),max(g_tk),0)
for i in range(n-1):
  mx=max(mx,t_tk[i]+g_md[n-2-i],t_md[i]+g_tk[n-i-2])
print(mx)

Submission status: Wrong Answer

Solution code:
n,c= [int(i) for i in input().split()]
t_tk_r=0
t_md_r=0
g_tk_r=0
g_md_r=0
t_tk_m=0
t_md_m=0
g_tk_m=0
g_md_m=0
t_tk=[]
t_md=[]
g_tk=[]
g_md=[]
a=[]
d1=0
for i in range(n):
  a.append([int(i) for i in input().split()])
for d0,v0 in a:
  t_tk_r+=v0-(d0-d1)
  t_md_r+=v0-(d0-d1)*2
  t_tk_m=max(t_tk_m,t_tk_r)
  t_md_m=max(t_md_m,t_md_r)
  t_tk.append(t_tk_m)
  t_md.append(t_md_m)
  d1=d0
d1=0  
for d0,v0 in a[::-1]:
  g_tk_r+=v0-(c-d0)-d1
  g_md_r+=v0-((c-d0)-d1)*2
  g_tk_m=max(g_tk_m,g_tk_r)
  g_md_m=max(g_md_m,g_md_r)
  g_tk.append(g_tk_m)
  g_md.append(g_md_m)
  d1=(c-d0) 
mx=max(max(t_tk),max(g_tk),0)
for i in range(1,n):
  mx=max(mx,t_tk[i]+g_md[n-i],t_md[i]+g_tk[n-i])
print(mx)

Submission status: Wrong Answer

Solution code:
n,c= [int(i) for i in input().split()]
t_tk_r=0
t_md_r=0
g_tk_r=0
g_md_r=0
t_tk_m=0
t_md_m=0
g_tk_m=0
g_md_m=0
t_tk=[]
t_md=[]
g_tk=[]
g_md=[]
a=[]
d1=0
for i in range(n):
  a.append([int(i) for i in input().split()])
for d0,v0 in a:
  t_tk_r+=v0-(d0-d1)
  t_md_r+=v0-(d0-d1)*2
  t_tk_m=max(t_tk_m,t_tk_r)
  t_md_m=max(t_md_m,t_md_r)
  t_tk.append(t_tk_m)
  t_md.append(t_md_m)
  d1=d0
d1=0  
for d0,v0 in a[::-1]:
  g_tk_r+=v0-(c-d0)-d1
  g_md_r+=v0-((c-d0)-d1)*2
  g_tk_m=max(g_tk_m,g_tk_r)
  g_md_m=max(g_md_m,g_md_r)
  g_tk.append(g_tk_m)
  g_md.append(g_md_m)
  d1=(c-d0) 
mx=max(max(t_tk),max(g_tk),0)
for i in range(1,n):
  mx=max(mx,t_tk[i]+g_md[n-i-1],t_md[i]+g_tk[n-i-1])
print(mx)

Submission status: Wrong Answer

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

plus_arr=[]
plus_max=0
plus_max_num=-1
plus_max_minus_distance=0
plus_max_minus_distance_num=-1
plus_max_half=0
minus_arr=[]
minus_max=0
minus_max_num=N
minus_max_half=0
minus_max_minus_distance=0
minus_max_minus_distance_num=N
for i,(x,v) in enumerate(nums):
    if i == 0:
        vi = v- x
        plus_arr.append(v-x)
        #plus_max=v-x
        #plus_max_num=x
        #plus_max_minus_distance=v-x
        #plus_max_minus_distance_num=x
    else:
        vi = plus_arr[i-1] + nums[i-1][0] -x + v
        plus_arr.append( vi  )
    if plus_max <= vi:
        plus_max = vi
        plus_max_num  = x
    if 2 * x < C:
        plus_max_half = plus_max
        if plus_max_minus_distance <= vi - x:
            plus_max_minus_distance = vi - x
            plus_max_minus_distance_num = x

for i,(x,v) in enumerate(nums[::-1]):
    if i == 0:
        vi = v- C + x
        minus_arr.append(v- C + x)
        #minus_max=v- C + x
        #minus_max_num=x
        #minus_max_minus_distance=v- C + x
        #minus_max_minus_distance_num=x
    else:
        vi = minus_arr[i-1] - nums[N-i][0]  + x + v
        minus_arr.append( vi  )
    if minus_max <= vi:
        minus_max = vi
        minus_max_num  = x
    if 2 * (C - x) < C:
        minus_max_half=minus_max
        if minus_max_minus_distance <= vi -  C + x:
            minus_max_minus_distance = vi -  C + x
            minus_max_minus_distance_num = x
if plus_max_num *2 >= C or  (N - minus_max_num) * 2 >= C:
    print(max([ plus_max,minus_max,minus_max]))
else:
    print(max([plus_max,minus_max,plus_max_minus_distance + minus_max_half, plus_max_half+ minus_max_minus_distance]))

Submission status: Wrong Answer

Solution code:
def eval_cal(x,v,n):
    tmp=0
    maxtotal=0
    for i in range(n):
        if i ==0:
            dis=abs(x[0])
        else :
            dis=x[i]-x[i-1]
    
        tmp+=v[i]-dis
        #print(tmp)
        if tmp > maxtotal:
            maxtotal=tmp
            
    return maxtotal

def setup_cal(x,v,n):
    tmp=0
    maxtotal=0
    maxx=0
    vm=[]
    xm=[]
    #print("x:",x)
    #print("v:",v)
    for i in range(n):
        if i ==0:
            dis=abs(x[0])
        else :
            dis=x[i]-x[i-1]
    
        tmp+=v[i]-dis
        #print(tmp)
        if tmp > maxtotal:
            maxtotal=tmp
            maxx=x[i]
        xm.append(maxx)
        vm.append(maxtotal)
        #print(x[i],maxtotal)
    return xm,vm
    

n, c = map(int,input().split(' '))
x=[]
v=[]
for i in range(n):
    xt,vt = map(int,input().split(' '))
    x.append(xt)
    v.append(vt)

xm1, vm1=setup_cal(x,v,n)
#print(xm1,vm1)
#反時計回りの場合
#設定
x.reverse()
v.reverse()
for i in range(n):
    x[i]=c-x[i]

xm2, vm2=setup_cal(x,v,n)
#print(xm2,vm2)

maxtotal=0

for i in range(n):
    if i == 0:
        tmp = vm1[0]
    elif i == n-1:
        tmp = vm2[n-1]
    else:
        tmp = vm1[i]+vm2[i-1]-xm1[i-1]
    #print(tmp)
    if maxtotal < tmp:
        maxtotal=tmp

print(maxtotal)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

line = input().split(" ")
n = int(line[0])
c = int(line[1])

sushi = []
for _ in range(n):
    line = input().split(" ")
    sushi.append([int(line[0]), int(line[1])]) #[x,v]

r = []
state = 0
rmax = -9999999999
for i in range(n):
    s = sushi[i]
    state += s[1]

    if state-s[0]>rmax:
        rmax = state-s[0]
    r.append(rmax)

state = 0
ret1 = r[-1]
for i in range(n-1,0,-1):
    s = sushi[i]
    state += s[1]

    s = state-2*(c-s[0]) + r[i-1]
    if s>ret1:
        ret1 = s




l = [0 for _ in range(n)]
state = 0
lmax = -9999999999999
for i in range(n-1,-1,-1):
    s = sushi[i]
    state += s[1]

    if state-(c-s[0])>lmax:
        lmax = state-(c-s[0])
    l[i] = lmax

state = 0
ret2 = l[-1]
for i in range(n-1):
    s = sushi[i]
    state += s[1]

    s = state-2*s[0] + l[i+1]
    if s>ret2:
        ret2 = s

print(max(ret1,ret2,0))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

line = input().split(" ")
n = int(line[0])
c = int(line[1])

sushi = []
for _ in range(n):
    line = input().split(" ")
    sushi.append([int(line[0]), int(line[1])]) #[x,v]

r = []
state = 0
rmax = 0
for i in range(n):
    s = sushi[i]
    state += s[1]

    if state-s[0]>rmax:
        rmax = state-s[0]
    r.append(rmax)

state = 0
ret1 = r[-1]
for i in range(n-1,0,-1):
    s = sushi[i]
    state += s[1]

    s = state-2*(c-s[0]) + r[i-1]
    if s>ret1:
        ret1 = s




l = [0 for _ in range(n)]
state = 0
lmax = 0
for i in range(n-1,-1,-1):
    s = sushi[i]
    state += s[1]

    if state-(c-s[0])>lmax:
        lmax = state-(c-s[0])
    l[i] = lmax

state = 0
ret2 = l[-1]
for i in range(n-1):
    s = sushi[i]
    state += s[1]

    s = state-2*s[0] + l[i+1]
    if s>ret2:
        ret2 = s

print(max(ret1,ret2,0))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

line = input().split(" ")
n = int(line[0])
c = int(line[1])

sushi = []
for _ in range(n):
    line = input().split(" ")
    sushi.append([int(line[0]), int(line[1])]) #[x,v]

r = []
state = 0
rmax = 0
for i in range(n):
    s = sushi[i]
    state += s[1]

    if state-s[0]>rmax:
        rmax = state-s[0]
    r.append(rmax)

state = 0
ret1 = r[-1]
for i in range(n-1,0,-1):
    s = sushi[i]
    state += s[1]

    s = state-2*(c-s[0]) + r[i-1]
    if s>ret1:
        ret1 = s




l = [0 for _ in range(n)]
state = 0
lmax = 0
for i in range(n-1,-1,-1):
    s = sushi[i]
    state += s[1]

    if state-(c-s[0])>lmax:
        lmax = state-(c-s[0])
    l[i] = lmax

state = 0
ret2 = l[-1]
for i in range(n-1):
    s = sushi[i]
    state += s[1]

    s = state-2*s[0] + l[i+1]
    if s>ret2:
        ret2 = s

print(max(ret1,ret2))

Submission status: Wrong Answer

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

plus_arr=[]
plus_max=0
plus_max_num=-1
plus_max_minus_distance=0
plus_max_minus_distance_num=-1
plus_max_half=0
minus_arr=[]
minus_max=0
minus_max_num=N
minus_max_half=0
minus_max_minus_distance=0
minus_max_minus_distance_num=N
for i,(x,v) in enumerate(nums):
    if i == 0:
        vi = v- x
        plus_arr.append(v-x)
        #plus_max=v-x
        #plus_max_num=x
        #plus_max_minus_distance=v-x
        #plus_max_minus_distance_num=x
    else:
        vi = plus_arr[i-1] + nums[i-1][0] -x + v
        plus_arr.append( vi  )
    if plus_max <= vi:
        plus_max = vi
        plus_max_num  = x
    if 2 * x < C:
        plus_max_half = plus_max
        if plus_max_minus_distance <= vi - x:
            plus_max_minus_distance = vi - x
            plus_max_minus_distance_num = x

for i,(x,v) in enumerate(nums[::-1]):
    if i == 0:
        vi = v- C + x
        minus_arr.append(v- C + x)
        #minus_max=v- C + x
        #minus_max_num=x
        #minus_max_minus_distance=v- C + x
        #minus_max_minus_distance_num=x
    else:
        vi = minus_arr[i-1] - nums[N-i][0]  + x + v
        minus_arr.append( vi  )
    if minus_max <= vi:
        minus_max = vi
        minus_max_num  = x
    if 2 * (C - x) < C:
        minus_max_half=minus_max
        if minus_max_minus_distance <= vi -  C + x:
            minus_max_minus_distance = vi -  C + x
            minus_max_minus_distance_num = x
print(max([ plus_max,minus_max,minus_max,plus_max_minus_distance + minus_max_half, plus_max_half+ minus_max_minus_distance]))

Submission status: Wrong Answer

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

plus_arr=[]
plus_max=0
plus_max_num=-1
plus_max_minus_distance=0
plus_max_minus_distance_num=-1
plus_max_half=0
minus_arr=[]
minus_max=0
minus_max_num=N
minus_max_half=0
minus_max_minus_distance=0
minus_max_minus_distance_num=N
for i,(x,v) in enumerate(nums):
    if i == 0:
        vi = v- x
        plus_arr.append(v-x)
        #plus_max=v-x
        #plus_max_num=x
        #plus_max_minus_distance=v-x
        #plus_max_minus_distance_num=x
    else:
        vi = plus_arr[i-1] + nums[i-1][0] -x + v
        plus_arr.append( vi  )
    if plus_max <= vi:
        plus_max = vi
        plus_max_num  = x
    if 2 * x < C:
        plus_max_half = plus_max
        if plus_max_minus_distance <= vi - x:
            plus_max_minus_distance = vi - x
            plus_max_minus_distance_num = x

for i,(x,v) in enumerate(nums[-1:]):
    if i == 0:
        vi = v- C + x
        minus_arr.append(v- C + x)
        #minus_max=v- C + x
        #minus_max_num=x
        #minus_max_minus_distance=v- C + x
        #minus_max_minus_distance_num=x
    else:
        vi = minus_arr[i-1] + nums[i-1][0] - C + x + v
        minus_arr.append( vi  )
    if minus_max <= vi:
        minus_max = vi
        minus_max_num  = x
    if 2 * (C - x) < C:
        minus_max_half=minus_max
        if minus_max_minus_distance <= vi -  C + x:
            minus_max_minus_distance = vi -  C + x
            minus_max_minus_distance_num = x
if plus_max_num * 2  >= C or  (C - minus_max_num) * 2 >= C:
    print(max([ plus_max,minus_max,plus_max_minus_distance + minus_max_half, plus_max_half+ minus_max_minus_distance]))

Submission status: Wrong Answer

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

ans = 0
goleftsushi = 0
goleftwalk = 0

rightmax = [0 for i in range(n)]
rightmaxtmp = 0

gorightsushi = 0
gorightwalk = 0

rightmaxpos=0

for j in range(n-1,0,-1):
    gorightsushi = gorightsushi + sushi_li[j][1]
    gorightwalk = c - sushi_li[j][0]
    if rightmaxtmp <= gorightsushi-gorightwalk:
        rightmaxtmp = gorightsushi - gorightwalk
        rightmaxpos=j
    rightmax[j] = rightmaxtmp

ans = max(rightmaxtmp,0)

for i in range(n):
    goleftsushi = goleftsushi + sushi_li[i][1]
    goleftwalk = sushi_li[i][0]
    if i < n-1:
        tmpans = goleftsushi-goleftwalk + max(0,rightmax[i+1]-sushi_li[i][0])
    else:
        tmpans = goleftsushi - goleftwalk
    if ans <= tmpans:
        ans = tmpans


print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
#N=int(input())
#a = list(map(int,input().split()))

#x = []
#y = []
#for i in range(5):
#    x1,y1=[int(i) for i in input().split()]
#    x.append(x1)
#    y.append(y1)

N,C=[int(i) for i in input().split()]

x = [0]
v = [0]
for i in range(N):
    x1,v1=[int(i) for i in input().split()]
    x.append(x1)
    v.append(v1)

x.append(C)
v.append(0)

s = [0]*(N+1)
g = [0]*(N+2)
m1=0;m2=0

for i in range(1,N+1):
    s[i] = s[i-1]+v[i]-(x[i]-x[i-1])

for i in reversed(range(1,N+1)):
    g[i] = g[i+1]+v[i]+(x[i]-x[i+1])

j=N+1
M=0
k=0
jj=j
for i in reversed(range(N+1)):
    val = s[i] + g[j] -min(k,x[i])
    while(jj-i>1):
#        print(jj)
        newval = s[i] + g[jj-1] -min(k+x[j]-x[jj-1],x[i])
        if newval >= val:
            val = newval
            jj=jj-1
            k+= x[j]-x[jj]
            j=jj
        else:
            jj=jj-1
#    print(i,j,val)
    M=max(M,val)

#print(s,g)
print(M)

Submission status: Wrong Answer

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)

frontS = []
prevX = 0
v = 0
for i in range(N):
  v = v + V[i] - (X[i] - prevX)
  frontS.append(v)
  prevX= X[i]

# 前i個の中での累計MAX
backMaxS = []
prevX = C
v = 0
tmpMax = float('-inf')
for i in range(N-1, -1, -1):
  v = v + V[i] - (prevX - X[i])
  tmpMax = max(tmpMax, v)
  backMaxS.append(tmpMax)
  prevX = X[i]
backMaxS.reverse()
backMaxS.append(0)

print([0]+X)
print([0]+frontS)
print(backMaxS)

ans = backMaxS[0]
for i in range(N):
  # X個まで食べて、退出 or 引き返す
  ans = max(
    ans,
    frontS[i],
    frontS[i] - X[i] + backMaxS[i+1]
  )

print(ans)

Submission status: Wrong Answer

Solution code:
n,c= [int(i) for i in input().split()]
t_tk_r=0
t_md_r=0
g_tk_r=0
g_md_r=0
t_tk=[]
t_md=[]
g_tk=[]
g_md=[]
a=[]
d1=0
for i in range(n):
  a.append([int(i) for i in input().split()])
for d0,v0 in a:
  t_tk_r+=v0-(d0-d1)
  t_md_r+=v0-(d0-d1)*2
  t_tk.append(t_tk_r)
  t_md.append(t_md_r)
  d1=d0
d1=0  
for d0,v0 in a[::-1]:
  g_tk_r+=v0-((c-d0)-d1)
  g_md_r+=v0-((c-d0)-d1)*2
  g_tk.append(g_tk_r)
  g_md.append(g_md_r)
  d1=(c-d0)
  
mx=max(max(t_tk),max(g_tk),0)
for i in range(1,n):
  print(t_tk[:i],g_md[:n-i])
  mx=max(mx,max(t_tk[:i])+max(g_md[:n-i]),max(t_md[:i])+max(g_tk[:n-i]))
print(mx)

Submission status: Wrong Answer

Solution code:
n,c= [int(i) for i in input().split()]
t_tk_r=0
t_md_r=0
g_tk_r=0
g_md_r=0
t_tk=[]
t_md=[]
g_tk=[]
g_md=[]
a=[]
d1=0
for i in range(n):
  a.append([int(i) for i in input().split()])
for d0,v0 in a:
  t_tk_r+=v0-(d0-d1)
  t_md_r+=v0-(d0-d1)*2
  t_tk.append(t_tk_r)
  t_md.append(t_md_r)
  d1=d0
d1=0  
for d0,v0 in a[::-1]:
  g_tk_r+=v0-(c-d0)-d1
  g_md_r+=v0-((c-d0)-d1)*2
  g_tk.append(g_tk_r)
  g_md.append(g_md_r)
  d1=(c-d0)
mx=max(max(t_tk),max(g_tk),0)
for i in range(1,n):
  mx=max(mx,max(t_tk[:i])+max(g_md[:n-i]),max(t_md[:i])+max(g_tk[:n-i]))
print(mx)

Submission status: Wrong Answer

Solution code:
#!python
# -*- coding: utf-8 -*-

import sys
sys.setswitchinterval(1000000)


N, C = list(map(int, input().split(' ')))
X = []
V = []
for n in range(N):
    X_, V_ = list(map(int, input().split(' ')))
    X.append(X_)
    V.append(V_)


def sushi(sum_v, ichi, xs, vs):
    e = sum_v
    if len(xs) == 0:
        return e
    if len(xs) == 1:
        m = (C + xs[0] - ichi) % C
        move_v = min(m, C - m)
        return max(e, sum_v - move_v + vs[0])

    if ichi < xs[0]:
        R_next_idx, L_next_idx = (0, len(xs) - 1)
    elif xs[-1] < ichi:
        R_next_idx, L_next_idx = (0, len(xs) - 1)
    else:
        for i in range(len(xs) - 1):
            if xs[i] < ichi < xs[i + 1]:
                R_next_idx, L_next_idx = (i + 1, i)
                break

    # 右周り
    R_x = xs[R_next_idx]
    R_v = sum_v + vs[R_next_idx]
    R_v -= (C + R_x - ichi) % C
    R_xs = xs[:R_next_idx] + xs[R_next_idx + 1:]
    R_vs = vs[:R_next_idx] + vs[R_next_idx + 1:]

    # 左周り
    L_x = xs[L_next_idx]
    L_v = sum_v + vs[L_next_idx]
    L_v -= (C + ichi - L_x) % C
    L_xs = xs[:L_next_idx] + xs[L_next_idx + 1:]
    L_vs = vs[:L_next_idx] + vs[L_next_idx + 1:]

    r = sushi(R_v, R_x, R_xs, R_vs)
    l = sushi(L_v, L_x, L_xs, L_vs)

    return max(e, r, l)

if N <= 100:
    print(sushi(0, 0, X, V))

Submission status: Wrong Answer

Solution code:
n,c= [int(i) for i in input().split()]
t_tk_r=0
t_md_r=0
g_tk_r=0
g_md_r=0
t_tk=[]
t_md=[]
g_tk=[]
g_md=[]
a=[]
d1=0
for i in range(n):
  a.append([int(i) for i in input().split()])
for d0,v0 in a:
  t_tk_r+=v0-(d0-d1)
  t_md_r+=v0-(d0-d1)*2
  t_tk.append(t_tk_r)
  t_md.append(t_md_r)
  d1=d0
d1=0  
for d0,v0 in a[::-1]:
  g_tk_r+=v0-(c-d0)-d1
  g_md_r+=v0-((c-d0)-d1)*2
  g_tk.append(g_tk_r)
  g_md.append(g_md_r)
  d1=(c-d0)
mx=max(max(t_tk),max(g_tk),0)
for i in range(1,n):
  print(t_tk[:i],g_md[:n-i])
  mx=max(mx,max(t_tk[:i])+max(g_md[:n-i]),max(t_md[:i])+max(g_tk[:n-i]))

Submission status: Wrong Answer

Solution code:
N, C = map(int, input().split())
xv = [list(map(int, input().split())) for _ in range(N)]
xv_rev = list(reversed(xv))
xv = [[0,0]]+xv
xv_rev = [[0,0]] + xv_rev
for i in range(1,N+1):
    xv[i] = [xv[i][0],xv[i-1][1]+xv[i][1]+xv[i-1][0]-xv[i][0]]
    xv_rev[i] = [C-xv_rev[i][0],xv_rev[i-1][1]+xv_rev[i][1]+xv_rev[i-1][0]-(C-xv_rev[i][0])]

right = max(xv,key=lambda x:x[1])
left = max(xv_rev,key=lambda x:x[1])
both = 0

for i in range(N):
    for j in range(N-i):
        both = max(both,xv[i][1]+xv_rev[j][1]-min(xv[i][0],xv_rev[j][0]))

print(max(right[1],left[1],both))

Submission status: Wrong Answer

Solution code:
N, C = map(int, input().split())
xv = [list(map(int, input().split())) for _ in range(N)]
xv_rev = list(reversed(xv))
xv = [[0,0]]+xv
xv_rev = [[0,0]] + xv_rev
for i in range(1,N+1):
    xv[i] = [xv[i][0],xv[i-1][1]+xv[i][1]+xv[i-1][0]-xv[i][0]]
    xv_rev[i] = [C-xv_rev[i][0],xv_rev[i-1][1]+xv_rev[i][1]+xv_rev[i-1][0]-(C-xv_rev[i][0])]

right = max(xv,key=lambda x:x[1])
left = max(xv_rev,key=lambda x:x[1])
both = 0

for i in range(N):
    for j in range(N-i):
        both = max(both,xv[i][1]+xv_rev[j][1]-min(xv[i][0],xv_rev[j][0]))
print(xv)
print(xv_rev)
print(max(right[1],left[1],both))

Submission status: Wrong Answer

Solution code:
import numpy as np
n,c= [int(i) for i in input().split()]
t_tk_r=0
t_md_r=0
g_tk_r=0
g_md_r=0
t_tk=[]
t_md=[]
g_tk=[]
g_md=[]
a=[]
d1=0
for i in range(n):
  a.append([int(i) for i in input().split()])
for d0,v0 in a:
  t_tk_r+=v0-(d0-d1)
  t_md_r+=v0-(d0-d1)*2
  t_tk.append(t_tk_r)
  t_md.append(t_md_r)
  d1=d0
d1=0  
for d0,v0 in a[::-1]:
  g_tk_r+=v0-(c-d0)-d1
  g_md_r+=v0-((c-d0)-d1)*2
  g_tk.append(g_tk_r)
  g_md.append(g_md_r)
  d1=(c-d0)
mx=max(max(t_tk),max(g_tk),0)
for i in range(1,n):
  mx=max(mx,max(t_tk[:i])+max(g_md[:n-i]),max(t_md[:i])+max(g_tk[:n-i]))
print(mx)

Submission status: Wrong Answer

Solution code:
N,C = map(int, input().split())
dis = []
cal = []
for i in range(N):
    x,v = map(int, input().split())
    dis.append(x)
    cal.append(v)
r = [cal[0] - dis[0]]
for i in range(N-1):
    r.append(r[i] + dis[i] + cal[i+1] - dis[i+1])
l = [cal[-1] - (C - dis[-1])]
for i in range(N-1):
    l.append(l[i] - dis[-i-1] + cal[-i-2] + dis[-i-2])
r2 = [0]
l2 = [0]
for i in range(N):
    r2.append(r[i] - dis[i])
    l2.append(l[i] - (C - dis[-i-1]))
for i in range(N-1):
    r[i] = r[i] + max(l2[:-i-1])
    l[i] = l[i] + max(r2[:-i-1])
print(max(max(r),max(l)))

Submission status: Wrong Answer

Solution code:
import numpy as np
n,c= [int(i) for i in input().split()]
t_tk_r=0
t_md_r=0
g_tk_r=0
g_md_r=0
t_tk=[]
t_md=[]
g_tk=[]
g_md=[]
a=[]
d1=0
for i in range(n):
  a.append([int(i) for i in input().split()])
for d0,v0 in a:
  t_tk_r+=v0-(d0-d1)
  t_md_r+=v0-(d0-d1)*2
  t_tk.append(t_tk_r)
  t_md.append(t_md_r)
  d1=d0
d1=0  
for d0,v0 in a[::-1]:
  g_tk_r+=v0-(c-d0)-d1
  g_md_r+=v0-((c-d0)-d1)*2
  g_tk.append(g_tk_r)
  g_md.append(g_md_r)
  d1=(c-d0)
mx=max(max(t_tk),max(g_tk))
for i in range(1,n):
  mx=max(mx,max(t_tk[:i])+max(g_md[:n-i]),max(t_md[:i])+max(g_tk[:n-i]))
print(mx)