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: Runtime Error

Solution code:
import numpy as np
n, c = list(map(int, input().split()))
xv = list(list(map(int, input().split())) for i in range(n))
sushi1, max1, sushi2, max2 = [0], [0], [0], [0]
sum1, sum2 = 0, 0
for x, v in xv:
    sum1 += v
    sushi1.append(max(sum1 - x, sushi1[-1]))
    max1.append(max(sum1 - 2*x, max1[-2]))
for x, v in xv[::-1]:
    sum2 += v
    sushi2.append(max(sum2 - (c-x), sushi[-1]))
    max2.append(max(sum2 - 2*(c-x), max2[-1]))
ans1 = max(np.array(sushi1) + np.array(max2[::-1]))
ans2 = max(np.array(sushi2) + np.array(max1[::-1]))
print(max(0, ans1, ans2))

Submission status: Runtime Error

Solution code:
import numpy as np
n, c = list(map(int, input().split()))
for i in range(n):
    xv = list(list(map(int, input().split())) for i in range(n))
sushi1, max1, sushi2, max2 = [0], [0], [0], [0]
sum1, sum2 = 0, 0
for x, v in xv:
    sum1 += v
    sushi1.append(max(sum1 - x, sushi1[-1]))
    max1.append(max(sum1 - 2*x, max1[-2]))
for x, v in xv[::-1]:
    sum2 += v
    sushi2.append(max(sum2 - (c-x), sushi[-1]))
    max2.append(max(sum2 - 2*(c-x), max2[-1]))
ans1 = max(np.array(sushi1) + np.array(max2[::-1]))
ans2 = max(np.array(sushi2) + np.array(max1[::-1]))
print(max(0, ans1, ans2))

Submission status: Runtime Error

Solution code:
import numpy as np
n, c = list(map(int, input().split()))
for i in range(n):
    xv = list(list(map(int, input().split())) for i in range(n))
sushi1, max1, sushi2, max2 = [0], [0], [0], [0]
sum1, sum2 = 0, 0
for x, v in xv:
    sum1 += v
    sushi1.append(max(sum1 - x, sushi1[-1]))
    max1.append(max(sum1 - 2*x, max1[-2]))
for x, v in xv[::-1]:
    sum2 += v
    sushi2.append(max(sum2 - (c-x), sushi[-1]))
    max2.append(max(sum2 - 2*(c-x), max2[-1]))
ans1 = np.array(sushi1) + np.array(max2[::-1])
ans2 = np.array(sushi2) + np.array(max1[::-1])
print(max(0, ans1, ans2))

Submission status: Runtime Error

Solution code:
n, c = list(map(int, input().split()))
x, v = [], []
for i in range(n):
    x_, v_ = list(map(int, input().split()))
    x.append(x_)
    v.append(v_)
sushi1 = list((sum(v[:i+1]) - x[i]) for i in range(n))
sushi2 = list((sum(v[n-i:]) - (c - x[n-i])) for i in range(1, n+1))
max1, max2 = [0], [0]
for i in range(n):
    max1.append(max(sushi1[i] - x[i], max1[i]))
    max2.append(max(sushi2[i] - (c - x[n-i-1]), max2[i]))
def ans1(i):
    if i < 0:
        return 0
    elif i == 0:
        return max2[n]
    ans = max2[n-i] + sushi1[i-1]
    return max(ans, ans1(i-1))
def ans2(i):
    if i < 0:
        return 0
    elif i == 0:
        return max1[n]
    ans = max1[n-i] + sushi2[i-1]
    return max(ans, ans2(i-1))
print(max(ans1(n), ans2(n)))

Submission status: Runtime Error

Solution code:
n, c = list(map(int, input().split()))
x, v = [], []
for i in range(n):
    x_, v_ = list(map(int, input().split()))
    x.append(x_)
    v.append(v_)
sushi1 = list((sum(v[:i+1]) - x[i]) for i in range(n))
sushi2 = list((sum(v[n-i:]) - (c - x[n-i])) for i in range(1, n+1))
max1, max2 = [0], [0]
for i in range(n):
    max1.append(max(sushi1[i] - x[i], max1[i]))
    max2.append(max(sushi2[i] - (c - x[n-i-1]), max2[i]))
def ans1(i):
    if i < 0:
        return 0
    elif i == 0:
        return max2[n]
    ans = max2[n-i] + sushi1[i-1]
    return max(ans, ans1(i-1))
def ans2(i):
    if i < 0:
        return 0
    elif i == 0:
        return max1[n]
    ans = max1[n-i] + sushi2[i-1]
    return max(ans, ans1(i-1))
print(max(ans1(n), ans2(n)))

Submission status: Runtime Error

Solution code:
def f(n):
    return min(n,C-n)
N,C=map(int,input().split())
t=[0]*(C+1)
s=[]
u=[0]
for i in range(N):
    p,q=map(int,input().split())
    s.append(p)
    t[p]=q
from itertools import permutations 
for i in list(permutations(s)):
    a=0
    i=(0,)+i
    for j in range(len(i)-1):
        a+=t[i[j+1]]-f(abs(i[j+1]-i[j]))
        if a>0:
            u.append(a)
print(max(u))

Submission status: Runtime Error

Solution code:
N,C=map(int,input().split())
sushi = [list(map(int,input().split())) for _ in range(N)]
vs = [0]*(N)
vs[1] = sushi[0][1] - 2*sushi[0][0]
result = 0
ind = 0
for i in range(2,N):
  vs[i] = vs[i-1] + 2*sushi[i-2][0] + sushi[i-1][1] - 2*sushi[i-1][0]
  if result < vs[i]:
    result = vs[i]
    ind = sushi[i-1][0]
tmp = 0
result = result + ind
for x in range(1,N):
  tmp += sushi[-x][1] - (C - sushi[-x][0])
  if 1 < x:
    tmp += (C - sushi[-x+1][0])
  result = max(result,tmp+max(vs[:-x]))
print(result)

Submission status: Runtime Error

Solution code:
from itertools import accumulate, chain
 
n, c = map(int, input().split())
xs, vs = zip(*(map(int, input().split()) for _ in range(n)))
 
ds = [j - i for i, j in zip(chain([0], xs), chain(xs, [c]))]
 
clockwise1      = chain([0], (v - d*2 for v, d in zip(accumulate(vs), accumulate(ds))))
anticlockwise1  = chain([0], (v - d for v, d in zip(accumulate(vs[::-1]), accumulate(ds[::-1]))))
clock_anticlock = (x + y for x, y in zip(clockwise1, reversed(list(accumulate(anticlockwise1, func=max)))))
 
anticlockwise2  = chain([0], (v - d*2 for v, d in zip(accumulate(vs[::-1]), accumulate(ds[::-1]))))
clockwise2      = chain([0], (v - d for v, d in zip(accumulate(vs), accumulate(ds))))
anticlock_clock = (x + y for x, y in zip(anticlockwise2, reversed(list(accumulate(clockwise2, func=max)))))
 
print(max(chain(anticlock_clock, clock_anticlock)))
from itertools import accumulate, chain

n, c = map(int, input().split())
xs, vs = zip(*(map(int, input().split()) for _ in range(n)))

ds = [j - i for i, j in zip(chain([0], xs), chain(xs, [c]))]

clockwise1      = chain([0], (v - d*2 for v, d in zip(accumulate(vs), accumulate(ds))))
anticlockwise1  = chain([0], (v - d for v, d in zip(accumulate(vs[::-1]), accumulate(ds[::-1]))))
clock_anticlock = (x + y for x, y in zip(clockwise1, reversed(list(accumulate(anticlockwise1, func=max)))))

anticlockwise2  = chain([0], (v - d*2 for v, d in zip(accumulate(vs[::-1]), accumulate(ds[::-1]))))
clockwise2      = chain([0], (v - d for v, d in zip(accumulate(vs), accumulate(ds))))
anticlock_clock = (x + y for x, y in zip(anticlockwise2, reversed(list(accumulate(clockwise2, func=max)))))
 
print(max(chain(clock_anticlock, anticlock_clock)))

Submission status: Runtime Error

Solution code:
N, C = map(int, input().split())
x = []
v = []
for i in range(N):
    s, t = input().split()
    x.append(int(s))
    v.append(int(t))

moving_distance = []
for i in range(N):
    if x[i] <= C/2:
        moving_distance.append(x[i])
    else:
        moving_distance.append(C - x[i])
    
    print(moving_distance)

Submission status: Runtime Error

Solution code:
import numpy as np
N, C = map(int, input().split())
X = {}
V = {}
for i in range(1, N+1):
    X[i], V[i] = map(int, input().split())
    
x = np.zeros([N, 6])
for i in range(N):
    x[i, 0] = X[i+1]
    x[i, 1] = V[i+1]
    
    x[i, 2] = min(x[i, 0], abs(C-x[i, 0]))
x[:, 4] = x[:, 1] - x[:, 2]
x[x[:, 0] == x[:, 2], 3] = 1
tmp = 0


result = 0
while max(x[x[:, 5]==0, 4]) > 0:
    max_arg_i = np.argmax(x[:, 4])
    now_best = x[max_arg_i]
    get_flag = np.logical_and(np.logical_and(x[:, 2] <= now_best[2], x[:, 3] == now_best[3]), x[:, 5] == 0)
    temp_get_cal = sum(x[get_flag, 1]) - now_best[2]
    x[get_flag, 5] = 1
    result += temp_get_cal
    x[:, 0] = abs(x[:, 0] - now_best[0])
    x[get_flag, 1] = 0
    for i in range(N):
        x[i, 2] = min(x[i, 0], abs(C-x[i, 0]))
    x[:, 4] = x[:, 1] - x[:, 2]
    x[x[:, 0] == x[:, 2], 3] = 1
print(int(result))

Submission status: Runtime Error

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

x = [0]*N
rev_x = [0]*N
v = [0]*N

T = [0]*N
Trev = [0]*N

Tall = []

#
'''
for i in range(N):
  a,b = list(map(int,input().split()))
    x.append(a)
    rev_x.append(C - a)
    v.append(b)
'''
#


a = [list(map(int,input().split())) for i in range(N)]
x,v = [list(i) for i in zip(*a)]
rev_x = [C - i for i in x]

rev_x.reverse()


T = [sum(v[0:i+1]) - x[i] for i in range(N)]
Tmax = [max([T[j] for j in range(i+1)]) for i in range(N)]
Tmax_w = [max([T[j]-x[j] for j in range(i+1)]) for i in range(N)]

v.reverse()

Trev= [sum(v[0:i+1]) - rev_x[i] for i in range(N)]
Trevmax = [max([Trev[j] for j in range(i+1)]) for i in range(N)]
Trevmax_w = [max([Trev[j]-rev_x[j] for j in range(i+1)]) for i in range(N)]

Tall = max(T + Trev)

Tall_1 = max([Tmax[i] + Trevmax_w[N - 2 - i] for i in range(N - 1)])
Tall_2 = max([Tmax_w[i] + Trevmax[N - 2 - i] for i in range(N - 1)])

Tmax = max(Tall,Tall_1,Tall_2)
if Tmax < 0:
    Tmax = 0
print(Tmax)

Submission status: Runtime Error

Solution code:
max_c = 0

for i in range(N):
#    print("------")
#    print(i)
    c = 0
    x_tmp = x_list.copy()
    v_tmp = v_list.copy()
    for j in range(i):
        c += v_tmp[j]
#        print(v_tmp[j],"を食べた")
        c -= x_tmp[j+1] - x_tmp[j]
#        print(c)
        if max_c < c:
            max_c = c
    
    x_tmp = x_tmp[i:]
    v_tmp = v_tmp[i:]
    
    v_tmp = v_tmp[::-1]
    
    x_tmp = [x_tmp[0]+C] + x_tmp[1:][::-1]
    
#    print(x_tmp)
#    print(v_tmp)
    
    for j in range(N - i):
        c += v_tmp[j]
#        print(v_tmp[j],"を食べた")
        c -= x_tmp[j] - x_tmp[j+1]
#        print(c)
        if max_c < c:
            max_c = c

Submission status: Runtime Error

Solution code:
import numpy as np
import pandas as pd

n, c = list(map(int, input().split(' ')))
x, v = [], []

for i in range(n):
  x_, v_ = list(map(int, input().split(' ')))
  x.append(x_)
  v.append(v_)
x = np.array(x)
v = np.array(v)
sum_v = np.cumsum(v)
inv_x = c - x[::-1]
inv_v = v[::-1]
sum_inv_v = np.cumsum(inv_v)

cal = sum_v - x
inv_cal = sum_inv_v - inv_x

cal_cummax = pd.Series(cal).cummax().as_matrix()
inv_cal_cummax = pd.Series(inv_cal).cummax().as_matrix()

fr_double_cal = sum_v - 2 * x + \
    np.insert(inv_cal_cummax[:-1], 0, 0)[::-1]
fl_double_cal = sum_inv_v - 2 * inv_x + \
    np.insert(cal_cummax[:-1], 0, 0)[::-1]

print(np.max(
    [0, cal.max(), inv_cal.max(), fr_double_cal.max(), fl_double_cal.max()]))

Submission status: Runtime Error

Solution code:
 N, C = map(int, input().split())
 x = []
 v = []
 for i in range(N):
     a, b = map(int, input().split())
     x.append(a)
     v.append(b)

 R = [0] * N
 L = [0] * N
 R2L = 0
 L2R = 0
 for i in range(N):
     R[i] = sum(v[:i+1]) - x[i]
     L[i] = sum(v[-i-1:]) - (C - x[-i-1])
     if i < N - 1:
         R2L = max(R2L, R[i] - x[i] + max(L[:N-1-i]))
         L2R = max(L2R, L[i] - (C - x[-i-1]) + max(R[:N-1-i]))

 print(max(0, max(R), max(L), R2L, L2R))

Submission status: Runtime Error

Solution code:
N, C = map(int, input().split())
x = []
v = []
for i in range(N):
    a, b = map(int, input().split())
    x.append(a)
    v.append(b)

clock = [0] * N
antiClock = [0] * N
for i in range(N):
    clock[i] = sum(v[:i+1]) - x[i]
    antiClock[i] = sum(v[i:]) - (C - x[i])
clockMax = max(clock)
antiClockMax = max(antiClock)

clock2antiClock = max([(clock[i] - x[i] + max(antiClock[i+1:])) for i in range(N-1)])
antiClock2clock = max([(antiClock[i] - (C - x[i]) + max(clock[:i])) for i in range(1, N)])

print(max(0, clockMax, antiClockMax, clock2antiClock, antiClock2clock))

Submission status: Runtime Error

Solution code:
#import sys
#lines = sys.stdin.readlines()
#lines = lines.split('\n')
n, c = input().split()
#n, c = lines[0].split()
n = int(n)
c = int(c)
x = [0]
v = [0]
sv = [0]
ssv = 0
for i in range(n):
    xx, vv = input().split()
#    xx, vv = lines[i + 1].split()
    xx = int(xx)
    vv = int(vv)
    x.append(xx)
    v.append(vv)
    ssv += vv
    sv.append(ssv)
ans = -10**15
for i in range(0, n + 1, 1):
    for j in range(-1, -n +  i - 1, -1):
        e1 = sv[i] - x[i]
        e2 = sv[-1] - sv[j - 1] - (c - x[j])
        e3 = e1 + e2 - x[i]
        e3 = e1 + e2 - (c - x[j])
        ans = max([ans, e1, e2, e3, e4])
print(ans)

Submission status: Runtime Error

Solution code:
import sys
lines = sys.stdin.readlines()
lines = lines.split('\n')
#n, c = input().split()
n, c = lines[0].split()
n = int(n)
c = int(c)
x = [0]
v = [0]
sv = [0]
ssv = 0
for i in range(n):
#    xx, vv = input().split()
    xx, vv = lines[i + 1].split()
    xx = int(xx)
    vv = int(vv)
    x.append(xx)
    v.append(vv)
    ssv += vv
    sv.append(ssv)
ans = -10**15
for i in range(0, n + 1, 1):
    for j in range(-1, -n +  i - 1, -1):
        e1 = sv[i] - x[i]
        e2 = sv[-1] - sv[j - 1] - (c - x[j])
        e3 = e1 + e2 - x[i]
        e3 = e1 + e2 - (c - x[j])
        ans = max([ans, e1, e2, e3, e4])
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

def new_state(sushi, x, N, C):
    #if sushi.shape[0] == 0:
    #    return None
    s_c = np.copy(sushi)
    s_cc = np.copy(sushi)
    s_c[:, 0] -= x
    s_cc[:, 0] = C - (s_cc[:, 0] - x)
    
    # 実際に歩いて獲得できるカロリーを計算
    kcal_c = s_c[:, 1] - s_c[:, 0]
    kcal_cc = s_cc[:, 1] - s_cc[:, 0]
    # 目標の寿司を見つける
    dest_c = np.argmax(kcal_c)
    dest_cc = np.argmax(kcal_cc)
    if kcal_c[dest_c] < 0 and kcal_cc[dest_cc] < 0:
        return None
    # 時計回りと反時計回りでどちらがたくさんのカロリーを摂取できるか計算
    #print(s_c[:dest_c+1])
    #print(s_cc[:dest_cc-1:-1])
    result_c = s_c[:dest_c+1][:,1].sum() - s_c[dest_c][0]
    result_cc = s_c[:dest_cc-1:-1][:,1].sum() - s_cc[dest_cc][0]
    
    idx = np.arange(N)
    if result_c > result_cc:
        # 時計回りに回って寿司を食いまくる
        result = result_c
        new_x = s_c[dest_c][0]
        new_state = np.delete(sushi, idx[:dest_c+1], axis=0)
    else:
        result = result_cc
        new_x = s_cc[dest_cc][0]
        new_state = np.delete(sushi, idx[:dest_cc-1:-1], axis=0)
        
    return (result, new_x, new_state)

def get_max(sushi, N, C):
    total = 0
    state = new_state(sushi, 0, N, C)
    while state is not None:
        result, new_x, new_s = state
        total += result
        N = new_s.shape[0]
        state = new_state(new_s, new_x, N, C)
    return total

header = np.array(list(map(int, input().split())))
sushi = []
for i in range(header[0]):
    sushi.append(list(map(int, input().split())))
sushi = np.array(sushi)

N = header[0]
C = header[1]
total = get_max(sushi, N, C)
print(total)

Submission status: Runtime Error

Solution code:
n, c = map(int, input().split())
X = [0 for _ in range(n)]
V = [0 for _ in range(n)]
right = [0 for _ in range(n)]
left = [0 for _ in range(n)]
rl = [0 for _ in range(n)]
lr = [0 for _ in range(n)]
rlm = [0 for _ in range(n)]
lrm = [0 for _ in range(n)]

calsum = 0
for i in range(n):
    x, v = map(int, input().split())
    X[i] = x
    V[i] = v
    calsum += v
    right[i] = calsum-x
    rl[i] = calsum-2*x

calsum = 0
for i in range(n):
    calsum += V[n-i-1]
    left[i] = calsum - (c-X[n-i-1])
    lr[i] = calsum - 2*(c-X[n-i-1])


m = 0
for i in range(0, n-1):
    m = max(m, left[i])
    rlm[i] = m + rl[n-i-2]

m = 0
for i in range(0, n-1):
    m = max(m, right[i])
    lrm[i] = m + lr[n-i-2]

print(max(0, max(right), max(left), max(rlm), max(lrm))

Submission status: Runtime Error

Solution code:
import pdb
import copy
import time

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)

#t1 = time.time()

x.insert(0, 0)
v.insert(0, 0)

reverse = [0, 0]

def find_sushi(x, v, c, pos, cal, max_cal, route):
  if len(x) == 1:
    return max_cal
  x.pop(0)
  v.pop(0)
  max_cal1 = 0
  max_cal2 = 0
  if route != 'right_left':
    if route == 'first':
      route1 = 'right'
    elif route == 'left':
      route1 = 'left_right'
    else:
      route1 = route
    x1 = copy.deepcopy(x)
    v1 = copy.deepcopy(v)
    cal1 = cal
    if x1[0] > pos:
      cal1 += v1[0] - (x1[0] - pos)
    else:
      cal1 += v1[0] - (c - pos + x1[0])
    if cal1 > max_cal:
      max_cal = cal1
    max_cal1 = find_sushi(x1, v1, c, x1[0], cal1, max_cal, route1)
  if route != 'left_right':
    if route == 'first':
      route2 = 'left'
    elif route == 'right':
      route2 = 'right_left'
    else:
      route2 = route
    x2 = copy.deepcopy(x)
    v2 = copy.deepcopy(v)
    cal2 = cal
    x2.insert(0, x2[-1])
    x2.pop()
    v2.insert(0, v2[-1])
    v2.pop()
    if x2[0] > pos:
      cal2 += v2[0] - (c + pos - x2[0])
    else:
      cal2 += v2[0] - (pos - x2[0])
    if cal2 > max_cal:
      max_cal = cal2
    max_cal2 = find_sushi(x2, v2, c, x2[0], cal2, max_cal, route2)
  
  if max_cal1 > max_cal2:
    return(max_cal1)
  else:
    return(max_cal2)

print(find_sushi(x, v, c, 0, 0, 0, 'first'))

#t2 = time.time()

#total_time = t2 - t1
#print(total_time)

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python3

n, c = map(int, input().split())

sushis = []
for i in range(n):
    sushis.append(tuple(map(int, input().split())))
sushis.sort(key=lambda tup: -tup[1])

move_range = [0, 0]
ng_sushis = []

# 確実に食べるsushiを探索対象から除外
v_sum = 0
for sushi in sushis:
    x = sushi[0]
    v = sushi[1]
    distance = max(0, x - move_range[1])
    if distance == 0:
        v_sum += v
        continue
    distance_cc = max(0, (c - x) - move_range[0] * (-1))
    if distance_cc == 0:
        v_sum += v
        continue
    if distance <= distance_cc and distance * 3 < v:
        v_sum += v
        move_range[1] += distance
        continue
    if distance > distance_cc and distance_cc * 3 < v:
        v_sum += v
        move_range[0] -= distance_cc
        continue

    ng_sushis.append(sushi)

n_ngsushi = len(ng_sushis)


def dfs(i, left, right, yn):

    res = 0
    if yn == True:

        x = ng_sushis[i][0]
        v = ng_sushis[i][1]
        res += v
        distance = max(0, x - right)
        distance_cc = max(0, (c - x) - left * (-1))

        if distance != 0 and distance_cc != 0:
            if distance <= distance_cc:
                right += distance
            else:
                left -= distance_cc

    if i == n_ngsushi - 1:
        return res - min(right * 2 + left * (-1), right + left * (-2))

    res1 = dfs(i+1, left, right, True)
    res2 = dfs(i+1, left, right, False)

    return(res + max(res1, res2))

if n_ngsushi > 0:
    res1 = dfs(0, move_range[0], move_range[1], True)
    res2 = dfs(0, move_range[0], move_range[1], False)
    res = v_sum + max(res1, res2)

else:
    res = v_sum - min(move_range[1] * 2 + move_range[0] * (-1), move_range[1] + move_range[0] * (-2))

print(res)

Submission status: Runtime Error

Solution code:
import pdb
import copy

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)

x.insert(0, 0)
v.insert(0, 0)

def find_sushi(x, v, c, pos, cal, max_cal):
  if len(x) == 1:
    return max_cal
#  pdb.set_trace()
  x1 = copy.deepcopy(x)
  v1 = copy.deepcopy(v)
#  pdb.set_trace()
  cal1 = cal
  x1.pop(0)
  v1.pop(0)
  if x1[0] > pos:
    cal1 += v1[0] - (x1[0] - pos)
  else:
    cal1 += v1[0] - (c - pos + x1[0])
  if cal1 > max_cal:
    max_cal = cal1
  max_cal1 = find_sushi(x1, v1, c, x1[0], cal1, max_cal)
#  pdb.set_trace()
  x2 = copy.deepcopy(x)
  v2 = copy.deepcopy(v)
#  pdb.set_trace()
  cal2 = cal
  x2.pop(0)
  v2.pop(0)
  x2.insert(0, x2[-1])
  x2.pop()
  v2.insert(0, v2[-1])
  v2.pop()
  if x2[0] > pos:
    cal2 += v2[0] - (c + pos - x2[0])
  else:
    cal2 += v2[0] - (pos - x2[0])
  if cal2 > max_cal:
    max_cal = cal2
  max_cal2 = find_sushi(x2, v2, c, x2[0], cal2, max_cal)

  if max_cal1 > max_cal2:
    return(max_cal1)
  else:
    return(max_cal2)

print(find_sushi(x, v, c, 0, 0, 0))

Submission status: Runtime Error

Solution code:

class Cell(object):
    def __init__(self, pos, value, left, right):
        self.pos = pos
        self.value = value
        self.left = left
        self.right = right
    
    def left_distance(self, c):
        d = self.pos - self.left.pos
        if d < 0:
            d += c
        return d
    
    def right_distance(self, c):
        d = self.right.pos - self.pos
        if d < 0:
            d += c
        return d
    
    def __repr__(self):
        return repr((self.pos, self.value))

def solve(current, c):
    if current.left == current.right:
        # rest 1 sushi
        l_val = current.left.value - current.left_distance(c)
        r_val = current.right.value - current.right_distance(c)
        return max(l_val, r_val, 0)
    else:
        l_cell = Cell(current.left.pos, 0, current.left.left, current.right)
        r_cell = Cell(current.right.pos, 0, current.left, current.right.right)

        l_val = solve(l_cell, c) + current.left.value - current.left_distance(c)
        r_val = solve(r_cell, c) + current.right.value - current.right_distance(c)
        return max(l_val, r_val, 0)

def main():
    n, c = map(int, input().split())
    p = []

    for _ in range(n):
        pos, v = map(int, input().split())
        p.append(Cell(pos, v, None, None))
    
    p.sort(key=lambda cell: cell.pos)

    for i in range(n):
        p[i].left = p[i - 1]
        p[i].right = p[(i + 1) % n]

    current = Cell(0, 0, p[-1], p[0])
    print(solve(current, c))

main()

Submission status: Runtime Error

Solution code:
import numpy as np
n, c = map(int, input().split())
x_list = []
v_list = []
cp_list = []
m = 0
count = 0
for i in range(n):
    x, v = map(int, input().split())
    x_list.append(x)
    v_list.append(v)

while m >= 0:
    count += m
    nx_list = []
    for i in range(len(v_list)):
        nx_list.append(min(x_list[i], c-x_list[i]))
        cp_list = [x - y for (x, y) in zip(v_list, nx_list)]
    m = max(cp_list)
    i = cp_list.index(m)
    left = sum(v_list[0:i])-nx_list[i]
    right = sum(v_list[i+1:n])-nx_list[i]
    x_list = np.array(x_list)
    if left > right:
        x_list = x_list[i+1:n] - x_list[i]
        v_list = v_list[i+1:n]
        count += sum(v_list[0:i])
    else:
        x_list = x_list[0:i] - c + x_list[i]
        v_list = v_list[0:i]
        count += sum(v_list[i+1:n])
    x_list = x_list.tolist()
print(count)

Submission status: Runtime Error

Solution code:
def a():
    s = input()
    total = 700
    for i in s:
        total += 100 if i == 'o' else 0
    print(total)


def b():
    n, x = map(int, input().split())
    m = [int(input()) for _ in range(n)]
    print(len(m) + ((x - sum(m)) // min(m)))


def c():
    a, b, c, x, y = map(int, input().split())
    total = 0
    while x > 0 or y > 0:
        # compare a, b, c
        xi = min(x, 1)  # 0, 1
        yi = min(y, 1)  # 0, 1
        x -= xi
        y -= yi
        total += min(xi*a+yi*b, 2*c)
        # if xi*a+yi*b < 2*c:
        #     print(f'A pizza * {xi}, B pizza * {yi}')
        # else:
        #     print('AB pizza * 2')
    print(total)


def d():
    import numpy as np
    n, c = map(int, input().split())
    sushi = np.zeros((c, ), dtype=int)
    for _ in range(n):
        xi, vi = map(int, input().split())
        sushi[xi] = vi
    sushi = sushi - 1
    print(max(max(np.max(np.cumsum(sushi)), np.max(np.cumsum(np.flip(sushi, axis=0))))+1, 0) )


d()

Submission status: Runtime Error

Solution code:
import numpy as np
n, c = map(int, input().split())
x_list = []
v_list = []
cp_list = []
m = 0
count = 0
for i in range(n):
    x, v = map(int, input().split())
    x_list.append(x)
    v_list.append(v)

while m >= 0:
    count += m
    nx_list = []
    for i in range(len(v_list)):
        nx_list.append(min(x_list[i], c-x_list[i]))
        cp_list = [x - y for (x, y) in zip(v_list, nx_list)]
    m = max(cp_list)
    i = cp_list.index(m)
    left = sum(v_list[0:i])-nx_list[i]
    right = sum(v_list[i+1:n])-nx_list[i]
    x_list = np.array(x_list)
    if left > right:
        x_list = x_list[i+1:n] - x_list[i]
        v_list = v_list[i+1:n]
        count += v_list[0:i]
    else:
        x_list = x_list[0:i] - c + x_list[i]
        v_list = v_list[0:i]
        count += v_list[i+1:n]
    x_list = x_list.tolist()
print(count)

Submission status: Runtime Error

Solution code:
import numpy as np
n, c = map(int, input().split())
x_list = []
v_list = []
cp_list = []
m = 0
count = 0
for i in range(n):
    x, v = map(int, input().split())
    x_list.append(x)
    v_list.append(v)

while m >= 0:
    count += m
    nx_list = []
    for i in range(len(v_list)):
        nx_list.append(min(x_list[i], c-x_list[i]))
        cp_list = [x - y for (x, y) in zip(v_list, nx_list)]
    m = max(cp_list)
    i = cp_list.index(m)
    left = sum(v_list[0:i])-nx_list[i]
    right = sum(v_list[i+1:n])-nx_list[i]
    x_list = np.array(x_list)
    if left > right:
        x_list = x_list[i+1:n] - x_list[i]
        v_list = v_list[i+1:n]
        count += v_list[0:i]
    else:
        x_list = x_list[0:i] - c + x_list[i]
        v_list = v_list[0:i]
        count += v_list[i+1;n]
    x_list = x_list.tolist()
print(count)

Submission status: Runtime Error

Solution code:
from sys import stdin


def main():
    N, C = [int(x) for x in stdin.readline().rstrip().split()]
    xvi = [tuple(int(x) for x in stdin.readline().rstrip().split())
           for _ in range(N)]
    cal = 0
    now = 0
    result = []
    while len(xvi):
        spam = [(i, min(abs(now - xv[0]), C - abs(now - xv[0])), xv[1]) for i, xv in enumerate(xvi)]
        spam = [(i, x, v) for i, x, v in spam if v - x > 0]
        # if len(spam) == 0:
        #     break
        maxv = min(spam, key=lambda x: x[1])
        idx, x, v = maxv
        cal += v - x
        result.append(cal)
        now += x
        if now >= C:
            now -= C
        del xvi[idx]
    print(max(result))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import copy
N, C = map(int, input().split())
X =[]
V =[]
for _ in range(N):
	x_tmp, v_tmp = map(int, input().split())
	X.append(x_tmp)
	V.append(v_tmp)

def solve_min(x, v, C):
	if x-v > 0 and C-x-v > 0:
		return 0:
	elif v-x > v-C+x: 
		return v-x
	else:
		return v-C+x

def solve(X, V, C, o):
	if len(X) == 1:
		return solve_min(X.pop(),V.pop(), C)
	Xr = copy.deepcopy(X)
	X0 = Xr.pop(0)
	Vr = copy.deepcopy(V)
	V0 = Vr.pop(0)
	r = solve(Xr, Vr, C, o-X0) - V0
	Xl = copy.deepcopy(X)
	XN = Xl.pop()
	Vl = copy.deepcopy(V)
	VN = Vl.pop()
	l = solve(Xl, Vl, C, o+C-XN) -(C-VN)
	return max(r,l)

print(solve(X, V, C, 0))

Submission status: Runtime Error

Solution code:
n, c = list(map(int, input().split()))
sushi = [[0, 0, 0, 0]]
sv1 = 0
for i in range(n):
    x, v = list(map(int, input().split()))
    sv1 += v
    sushi.append([x, v, sv1, 0])
sushi.append([c, 0, 0, 0])
sumv = sv1
sv2 = 0
for i in range(n):
    sv2 += sushi[n-i][1]
    sushi[n-i][3] = sv2
    
m = 0
for i in range(n):
    x1 = sushi[i][0]
    v1 = sushi[i][2]
    if v1 - x1 < 0: continue
    for j in range(n - i+2):
        x2 = c - sushi[i + j +1][0]
        v2 = sushi[i + j +1][3]
        if v2 - x2 < 0: continue
        a = max([v1 + v2 - x1*2 - x2, v1 + v2 - x1 - x2*2])
        if m < a: m = a
print(m)

Submission status: Runtime Error

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

x = []
v = []
for i in range(n):
    a,b = list(map(int,input().split()))
    x.append(a)
    v.append(b)

x_r = []
sum = 0
for i in range(n-1,-1,-1):
    if x[i] < c/2:
        idx = i
        break
    x_r.append([])
    x_r[n-1-i].append(c-x[i])
    sum += v[i]
    x_r[n-1-i].append(sum)

x_l = []
sum = 0
for i in range(idx+1):
    x_l.append([])
    x_l[i].append(x[i])
    sum += v[i]
    x_l[i].append(sum)

X_l = []
sum = 0
for i in range(n):
    X_l.append([])
    X_l[i].append(x[i])
    sum += v[i]
    X_l[i].append(sum)
    
X_r = []
sum = 0
for i in range(n-1,-1,-1):
    X_r.append([])
    X_r[n-1-i].append(c-x[i])
    sum += v[i]
    X_r[n-1-i].append(sum)

cnt1 = 0
for i in range(n):
    cnt1 = max(X_l[i][1]-X_l[i][0],cnt1)
cnt2 = 0
for i in range(n):
    cnt2 = max(X_r[i][1]-X_r[i][0],cnt2)
lmax1 = 0
for i in range(len(x_l)):
    lmax1 = max(x_l[i][1]-x_l[i][0],lmax1)
lmax2 = 0
for i in range(len(x_l)):
    lmax2 = max(x_l[i][1]-2*x_l[i][0],lmax2)
rmax1 = 0
for i in range(len(x_r)):
    rmax1 = max(x_r[i][1]-x_r[i][0],rmax1)
rmax2 = 0
for i in range(len(x_r)):
    rmax2 = max(x_r[i][1]-2*x_r[i][0],rmax2)

ans = max(cnt1,cnt2)
ans = max(ans,lmax1 + rmax2)
ans = max(ans,lmax2 + rmax1)

print(ans)