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: Time Limit Exceeded

Solution code:
# coding:utf-8

import sys
import heapq
import itertools
from collections import deque

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

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


n, c = LI()
sushi = [LI() for _ in range(n)]

clockwise = [0]
for x, v in sushi[::-1]:
    clockwise.append(clockwise[-1] + v)
counterclockwise = [0]
for x, v in sushi:
    counterclockwise.append(counterclockwise[-1] + v)

sushi = deque(sushi)
sushi.append([c, 0])
sushi.appendleft([0, 0])

ans = 0
for i in range(n + 1):
    for j in range(n + 1 - i):
        # cal1: 反時計回り->時計回り
        cal1 = counterclockwise[i] + clockwise[j]
        cal1 -= sushi[i][0] * 2 + (c - sushi[-1 - j][0])
        # cal2: 時計回り->反時計回り
        cal2 = counterclockwise[i] + clockwise[j]
        cal2 -= sushi[i][0] + (c - sushi[-1 - j][0]) * 2
        ans = max(ans, cal1, cal2)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations as comb

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

rev=[None]*n
norm=[None]*n
buf=0
ans=0

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

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

for i in range(n):
    for j in range(i,n):
        r=norm[j][1]-norm[j][0]
        r=max(r,rev[i][1]-rev[i][0])

        if i!=j:
            r=max(r,rev[j][1]+norm[i][1]-(rev[j][0]+norm[i][0])-norm[i][0])
            r=max(r,rev[j][1]+norm[i][1]-(rev[j][0]+norm[i][0])-rev[j][0])

        ans=max(ans,r)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations as comb

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

rev=[None]*n
norm=[None]*n
buf=0
ans=0

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

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

for i,j in comb(range(n),2):
    r1=norm[j][1]-norm[j][0]
    r2=rev[j][1]+norm[i][1]-(rev[j][0]+norm[i][0])-norm[i][0]
    r3=rev[i][1]-rev[i][0]
    r4=rev[j][1]+norm[i][1]-(rev[j][0]+norm[i][0])-rev[j][0]
    ans=max(ans,r1,r2,r3,r4)

if n==1:
    x,v=l[0]
    print(max(0,v-min(x,c-x)))
else:
    print(ans)

Submission status: Time Limit Exceeded

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

cal_r = list()
prev = 0
dist = 0
X = list()
V = list()
for i in range(N):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v)
    prev = prev + v - x + dist
    dist = x
    cal_r.append(prev)


cal_l = list()
prev = 0
dist = 0
reverse = list(map(lambda x: C - x, X))[::-1]
for x, v in zip(reverse, V[::-1]):
    prev = prev + v - x + dist
    dist = x
    cal_l.append(prev)

cal = list()
for r, rc in enumerate(cal_r):
    for l, lc in enumerate(cal_l[:-r-1]):
        cal.append(rc + lc - min(X[r], reverse[l]))

print(max(max(max(cal or [0]), max(cal_l), max(cal_r)), 0))

Submission status: Time Limit Exceeded

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

XV.sort()

def h(a, b):
    ans = 0
    score = 0
    pos = C
    for x, v in reversed(b):
        score += v - (pos - x)
        pos = x
        ans = max(ans, score)
    score -= (C - pos)
    pos = 0
    for x, v in a:
        score += v - (x - pos)
        pos = x
        ans = max(ans, score)
    return ans

def g(a, b):
    ans = 0
    score = 0
    pos = 0
    for x, v in a:
        score += v - (x - pos)
        pos = x
        ans = max(ans, score)
    score -= pos
    pos = C
    for x, v in reversed(b):
        score += v - (pos - x)
        pos = x
        ans = max(ans, score)
    return ans

def f():
    return max(
            max(g(XV[:i], XV[i:]) for i in range(N + 1)),
            max(h(XV[:i], XV[i:]) for i in range(N + 1))
        )

print(f())

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10**5)

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


n,c = li()
x_cw = []
v_cw = []
x_ccw = []
for _ in range(n):
    x,v = li()
    x_cw.append(x)
    v_cw.append(v)
    x_ccw.append(c-x)
    
x_ccw = [0] + sorted(x_ccw)
v_ccw = [0] + v_cw[::-1]
x_cw = [0] + x_cw
v_cw = [0] + v_cw
    
for i in range(1,n+1):
    v_cw[i] += (v_cw[i-1] - (x_cw[i]-x_cw[i-1]))
    v_ccw[i] += (v_ccw[i-1] - (x_ccw[i]-x_ccw[i-1]))
    
    
cw = []
ccw = []

ans = 0
for cw in range(n+1):
    for ccw in range(n+1):
        if cw + ccw > n:
            continue
        
        temp = v_cw[cw] + v_ccw[ccw] - min(x_cw[cw],x_ccw[ccw])
        ans = max(temp, ans)
        
print(ans)

Submission status: Time Limit Exceeded

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

right_cal = [0 for i in range(N + 2)]
for i in range(1, N + 1):
    right_cal[i] = (v[i] + right_cal[i - 1] - x[i] + x[i - 1])
    
left_cal = [0 for i in range(N + 2)]
for i in range(N, 0, -1):
    left_cal[i] = (v[i] + left_cal[i + 1] - x[i + 1] + x[i])

ans = 0
for i in range(N + 1):
    ans = max(ans, right_cal[i] + max(left_cal[i + 1 : ]) - x[i])
    
ans = max(ans, left_cal[0] + max(right_cal))
for i in range(1, N + 1):
    ans = max(ans, left_cal[i] + max(right_cal[ : i]) - C + x[i])
    
# print(right_cal)
# print(left_cal)
# print(max_cal)
print(ans)

Submission status: Time Limit Exceeded

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

right_cal = [0 for i in range(N + 2)]
for i in range(1, N + 1):
    right_cal[i] = (v[i] + right_cal[i - 1] - x[i] + x[i - 1])
    
left_cal = [0 for i in range(N + 2)]
for i in range(N, 0, -1):
    left_cal[i] = (v[i] + left_cal[i + 1] - x[i + 1] + x[i])

ans = 0
max_cal = []
for i in range(N + 1):
    max_cal.append(max(left_cal[i + 1 : ]) - x[i])
for i in range(N + 1):
    ans = max(ans, right_cal[i] + max_cal[i])
    
max_cal = [max(right_cal)]
for i in range(1, N + 1):
    max_cal.append(max(right_cal[ : i]) - C + x[i])
for i in range(N + 1):
    ans = max(ans, left_cal[i] + max_cal[i])
    
# print(right_cal)
# print(left_cal)
# print(max_cal)
print(ans)

Submission status: Time Limit Exceeded

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

right_cal = [0 for i in range(N + 2)]
for i in range(1, N + 1):
    right_cal[i] = (v[i] + right_cal[i - 1] - x[i] + x[i - 1])
    
left_cal = [0 for i in range(N + 2)]
for i in range(N, 0, -1):
    left_cal[i] = (v[i] + left_cal[i + 1] - x[i + 1] + x[i])

ans = max(max(right_cal), max(left_cal))
for i in range(N + 2):
    for j in range(N + 2):
        if i >= j:
            continue
        ans = max(ans, right_cal[i] + left_cal[j] - x[i], right_cal[i] + left_cal[j] - C + x[j])
    
# print(right_cal)
# print(left_cal)
print(ans)

Submission status: Time Limit Exceeded

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

Sum_left = [0] * (N+1)
Sum_right = [0] * (N+1)
for i in range(N):
    Sum_left[i+1] = Sum_left[i] + XV[i+1][1]
    Sum_right[i+1] = Sum_right[i] + XV[N-i][1]

ans = 0
for i in range(N+1):
    for j in range(N+1-i):
        ans = max(ans, Sum_left[i] + Sum_right[j] - XV[i][0] - 2 * (C - XV[N+1-j][0]), Sum_left[i] + Sum_right[j] - 2 * XV[i][0] - (C - XV[N+1-j][0]))

print(ans)

Submission status: Time Limit Exceeded

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

Sum_left = [[0]*(N+1) for _ in range(N+1)]

for j in range(N):
    if j == 0:
        Sum_left[0][j+1] = Sum_left[0][j] + XV[j][1] - XV[j][0]
    else:
        Sum_left[0][j+1] = Sum_left[0][j] + XV[j][1] - (XV[j][0] - XV[j-1][0])
    ans = max(ans,Sum_left[0][j+1])

for i in range(N):
    if i == 0:
        Sum_left[i+1][0] = Sum_left[i][0] + XV[-i-1][1] - 2 * (C - XV[-i-1][0])
    else:
        Sum_left[i+1][0] = Sum_left[i][0] + XV[-i-1][1] - 2 * (XV[-i][0] - XV[-i-1][0])

for i in range(1,N):
    if Sum_left[i][0] < 0:
        continue
    for j in range(1,N+1-i):
        if Sum_left[0][j] < 0:
            continue
        Sum_left[i][j] = Sum_left[i][0] + Sum_left[0][j]
        ans = max(ans,Sum_left[i][j])



Sum_right = [[0]*(N+1) for _ in range(N+1)]

for j in range(N):
    if j == 0:
        Sum_right[0][j+1] = Sum_right[0][j] + XV[-1-j][1] - (C-XV[-1-j][0])
    else:
        Sum_right[0][j+1] = Sum_right[0][j] + XV[-1-j][1] - (XV[-j][0] - XV[-1-j][0])
    ans = max(ans,Sum_right[0][j+1])

for i in range(N):
    if i == 0:
        Sum_right[i+1][0] = Sum_right[i][0] + XV[i][1] - 2 * XV[i][0]
    else:
        Sum_right[i+1][0] = Sum_right[i][0] + XV[i][1] - 2 * (XV[i][0] - XV[i-1][0])

for i in range(1,N):
    if Sum_right[i][0] < 0:
        continue
    for j in range(1,N+1-i):
        if Sum_right[0][j] < 0:
            continue
        Sum_right[i][j] = Sum_right[i][0] + Sum_right[0][j]
        ans = max(ans,Sum_right[i][j])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

s = sys.stdin.readlines()

N,C = map(int,s[0].split())
XV = [[int(j) for j in s[i+1].split()] for i in range(N)]
ans = 0


Sum_left = [[0]*(N+1) for _ in range(N+1)]

for j in range(N):
    if j == 0:
        Sum_left[0][j+1] = Sum_left[0][j] + XV[j][1] - XV[j][0]
    else:
        Sum_left[0][j+1] = Sum_left[0][j] + XV[j][1] - (XV[j][0] - XV[j-1][0])
    ans = max(ans,Sum_left[0][j+1])

for i in range(N):
    if i == 0:
        Sum_left[i+1][0] = Sum_left[i][0] + XV[-i-1][1] - 2 * (C - XV[-i-1][0])
    else:
        Sum_left[i+1][0] = Sum_left[i][0] + XV[-i-1][1] - 2 * (XV[-i][0] - XV[-i-1][0])

for i in range(1,N):
    for j in range(1,N+1-i):
        Sum_left[i][j] = Sum_left[i][0] + Sum_left[0][j]
        ans = max(ans,Sum_left[i][j])



Sum_right = [[0]*(N+1) for _ in range(N+1)]

for j in range(N):
    if j == 0:
        Sum_right[0][j+1] = Sum_right[0][j] + XV[-1-j][1] - (C-XV[-1-j][0])
    else:
        Sum_right[0][j+1] = Sum_right[0][j] + XV[-1-j][1] - (XV[-j][0] - XV[-1-j][0])
    ans = max(ans,Sum_right[0][j+1])

for i in range(N):
    if i == 0:
        Sum_right[i+1][0] = Sum_right[i][0] + XV[i][1] - 2 * XV[i][0]
    else:
        Sum_right[i+1][0] = Sum_right[i][0] + XV[i][1] - 2 * (XV[i][0] - XV[i-1][0])

for i in range(1,N):
    for j in range(1,N+1-i):
        Sum_right[i][j] = Sum_right[i][0] + Sum_right[0][j]
        ans = max(ans,Sum_right[i][j])

print(ans)

Submission status: Time Limit Exceeded

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


Sum_left = [[0]*(N+1) for _ in range(N+1)]

for j in range(N):
    if j == 0:
        Sum_left[0][j+1] = Sum_left[0][j] + XV[j][1] - XV[j][0]
    else:
        Sum_left[0][j+1] = Sum_left[0][j] + XV[j][1] - (XV[j][0] - XV[j-1][0])
    ans = max(ans,Sum_left[0][j+1])

for i in range(N):
    if i == 0:
        Sum_left[i+1][0] = Sum_left[i][0] + XV[-i-1][1] - 2 * (C - XV[-i-1][0])
    else:
        Sum_left[i+1][0] = Sum_left[i][0] + XV[-i-1][1] - 2 * (XV[-i][0] - XV[-i-1][0])

for i in range(1,N):
    for j in range(1,N+1-i):
        Sum_left[i][j] = Sum_left[i][0] + Sum_left[0][j]
        ans = max(ans,Sum_left[i][j])



Sum_right = [[0]*(N+1) for _ in range(N+1)]

for j in range(N):
    if j == 0:
        Sum_right[0][j+1] = Sum_right[0][j] + XV[-1-j][1] - (C-XV[-1-j][0])
    else:
        Sum_right[0][j+1] = Sum_right[0][j] + XV[-1-j][1] - (XV[-j][0] - XV[-1-j][0])
    ans = max(ans,Sum_right[0][j+1])

for i in range(N):
    if i == 0:
        Sum_right[i+1][0] = Sum_right[i][0] + XV[i][1] - 2 * XV[i][0]
    else:
        Sum_right[i+1][0] = Sum_right[i][0] + XV[i][1] - 2 * (XV[i][0] - XV[i-1][0])

for i in range(1,N):
    for j in range(1,N+1-i):
        Sum_right[i][j] = Sum_right[i][0] + Sum_right[0][j]
        ans = max(ans,Sum_right[i][j])

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N, C = list(map(int,input().split()))

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

sum_c = 0
sum_d = 0
ans = 0
for f in range(N+1):
    sum_c += sushi[f,1]
    sum_d = sushi[f,0]
    tmp_c = sum_c
    tmp_d = sum_d
    for b in range(1,N+2-f):
        tmp_c += sushi[-b,1]
        tmp_d = min(sum_d, C-sushi[-b,0])*2 + max(sum_d, C-sushi[-b,0])
        if ans < tmp_c-tmp_d:
            ans = tmp_c-tmp_d

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

n,l = list(map(int,input().split()))
D = []
C = [0]

for i in range(n):
    d,c = list(map(int,input().split()))
    D.append(d)
    C.append(C[i]+c)

#時計周りに歩く場合を考える
L = []
for i in range(n):
    L.append(C[i+1]-D[i])

#反時計周りに歩く場合を考える
R = []
for i in range(n):
    R.append(C[n]-C[n-i-1]-(l-D[-i-1]))

#時計周りに歩いた後、反時計周りに歩く場合を考える
LR = []
for i in range(n-1):
    LR.append(C[i+1]-D[i]*2+max(R[:n-i-1]))

#時計周りに歩いた後、反時計周りに歩く場合を考える
RL = []
for i in range(n-1):
    RL.append(C[n]-C[n-i-1]-(l-D[-i-1])*2+max(L[:n-i-1]))
    
#即退店する場合を考える
O = [0]

print(max(L+R+LR+RL+O))

Submission status: Time Limit Exceeded

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

x_list = [0, ]
v_list = [0, ]
x_oa = [0, ]
x_2oa = [0, ]
max_a = {0: 0}
max_2a = {0: 0}

tmp_v = 0

for i in range(N):
    x, v = map(int, input().split())

    tmp_v += v

    x_list.append(x)
    v_list.append(v)
    x_oa.append(tmp_v - x)
    x_2oa.append(tmp_v - 2 * x)
    max_a[i + 1] = max(x_oa)
    max_2a[i + 1] = max(x_2oa)

x_list.append(C)
v_list.append(0)

tmp_v = 0
ans = 0
for i in reversed(range(1, N + 2)):

    x = x_list[i]
    v = v_list[i]
    tmp_v += v
    # print(x, v, i)

    tmp_oa = tmp_v - 2 * (C - x) + max_a[i - 1]
    tmp_2oa = tmp_v - C + x + max_2a[i - 1]
    ans = max(ans, tmp_oa, tmp_2oa)

print(ans)

Submission status: Time Limit Exceeded

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

x_list = [0, ]
v_list = [0, ]
x_oa = [0, ]
x_2oa = [0, ]

tmp_v = 0

for i in range(N):
    x, v = map(int, input().split())

    tmp_v += v

    x_list.append(x)
    v_list.append(v)
    x_oa.append(tmp_v - x)
    x_2oa.append(tmp_v - 2 * x)

x_list.append(C)
v_list.append(0)
x_oa.append(None)
x_2oa.append(None)

tmp_v = 0
ans = 0
for i in reversed(range(1, N + 2)):

    x = x_list[i]
    v = v_list[i]
    tmp_v += v
    # print(x, v, i)

    tmp_oa = tmp_v - 2 * (C - x) + max(x_oa[:i])
    tmp_2oa = tmp_v - C + x + max(x_2oa[:i])
    ans = max(ans, tmp_oa, tmp_2oa)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate

N, C = map(int, input().split())
sushi = [[int(x) for x in input().split()] for _ in range(N)]


def solve():
    """
    全探索O(N^2)
    v を事前に計算
    """
    V = [0] * (N + 1)
    for i in range(N):
        V[i + 1] = V[i] + sushi[i][1]

    ans = 0
    for i in range(N + 1):
        # OA
        if i == 0:
            oa_x = 0
        else:
            oa_x = sushi[i - 1][0]
        oa_v = V[i]

        for j in range(N + 1):
            # OB
            if j == 0:
                ob_x = 0
            else:
                ob_x = C - sushi[j - 1][0]
            ob_v = V[N] - V[j - 1]

            v = oa_v + ob_v

            # 2OA + OB
            x = 2 * oa_x + ob_x
            if x < C:
                ans = max(ans, v - x)

            # OA + 2OB
            x = oa_x + 2 * ob_x
            if x < C:
                ans = max(ans, v - x)
    print(ans)


solve()

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(N + 1):
    # OA
    if i == 0:
        oa_x = 0
        oa_v = 0
    else:
        oa_x = sushi[i - 1][0]
        oa_v = sum([sushi[ii][1] for ii in range(i)])

    for j in range(N + 1):
        # OB
        if j == 0:
            ob_x = 0
            ob_v = 0
        else:
            ob_x = C - sushi[j - 1][0]
            ob_v = sum([sushi[jj][1] for jj in range(N - 1, j - 2, -1)])

        v = oa_v + ob_v

        # 2OA + OB
        x = 2 * oa_x + ob_x
        if x < C:
            ans = max(ans, v - x)

         # OA + 2OB
        x = oa_x + 2 * ob_x
        if x < C:
            ans = max(ans, v - x)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3

from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def solve(N, C, x, v):
    pass


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

    sushi = []
    for _ in range(N):
        x, v = map(int, input().split())
        sushi.append((x, v))

    cs_x, cs_v = [0] * (N + 1), [0] * (N + 1)
    for i in range(N):
        cs_x[i + 1] = sushi[i][0]
        cs_v[i + 1] = cs_v[i] + sushi[i][1]

    cs_x_rev, cs_v_rev = [0] * (N + 1), [0] * (N + 1)
    for i in range(N):
        cs_x_rev[i + 1] = C - sushi[-i - 1][0]
        cs_v_rev[i + 1] = cs_v_rev[i] + sushi[-i - 1][1]


    # print(cs_x)
    # print(cs_v)
    # print(cs_x_rev)
    # print(cs_v_rev)

    # right
    ans = 0
    for i in range(1, N + 1):
        total_x = cs_x[i]
        total_v = cs_v[i]
        ans = max(ans, total_v - total_x)

        base_x = total_x * 2
        base_v = total_v
        for j in range(1, N - i + 1):
            total_x = cs_x_rev[j] + base_x
            total_v = cs_v_rev[j] + base_v
            ans = max(ans, total_v - total_x)

    # left
    for i in range(1, N + 1):
        total_x = cs_x_rev[i]
        total_v = cs_v_rev[i]
        ans = max(ans, total_v - total_x)

        base_x = total_x * 2
        base_v = total_v
        for j in range(1, N - i + 1):
            total_x = cs_x[j] + base_x
            total_v = cs_v[j] + base_v
            ans = max(ans, total_v - total_x)

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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]*(n+1), [0]*(n+1)
for i in range(n):
    max1[i+1] = max(sushi1[i] - x[i], max1[i])
    max2[i+1] = max(sushi2[i] - (c - x[n-i-1]), max2[i])
sushi1.reverse()
sushi2.reverse()
sushi1.append(0)
sushi2.append(0)
ans = 0 
for i in range(n+1):
    a1 = max1[i] + sushi2[i]
    a2 = max2[i] + sushi1[i]
    ans = max(ans, a1, a2)
print(ans)

Submission status: Time Limit Exceeded

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]))
sushi1.reverse()
sushi2.reverse()
sushi1.append(0)
sushi2.append(0)
ans = []
for i in range(n+1):
    ans.append(max1[i]+sushi2[i])
    ans.append(max2[i]+sushi1[i])
ans.append(0)
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
# your code goes here
def hanten(C, X, V):
	return (C*np.ones(X.shape[0])-X)[::-1], V[::-1]

def solv(N, C, X, V):
	return max(solv1(N, C, X, V, 0), solv2(N, C, X, V))

def solv1(N, C, X, V, pos):
	r = 0
	rbuf = 0
	#print(X, V)
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
	return r

def solv2(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	VV = V.copy()
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		VV[i] = 0
		Xbuf, Vbuf = hanten(C, X, VV)
		rbufbuf = rbuf + solv1(N, C, Xbuf, Vbuf, -pos)
		#print(Xbuf, Vbuf, pos, rbufbuf)
		if(rbufbuf > r):
			r = rbufbuf
	return r
	
def main():
	buf = input().split()
	N = int(buf[0])
	C = int(buf[1])
	X = np.zeros(N)
	V = np.zeros(N)
	for i in range(N):
		buf = input().split()
		X[i] = int(buf[0])
		V[i] = int(buf[1])
	
	r1 = solv2(N, C, X, V)
	X, V = hanten(C, X, V)
	r2 = solv2(N, C, X, V)
	print(int(max(r1, r2)))

main()

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
n, cir = map(int,input().split())
List = [[0,0]] + [list(map(int,input().split())) for i in range(n)]

maxv = 0
clck = [0] * (n+1)
anti = [0] * (n+1)
for i in range(1,n+1):
    clck[i] = clck[i-1] + List[i][1] - List[i][0] + List[i-1][0]
    
List[0] = [cir,0]
for i in range(1,n+1):
    anti[i] = anti[i-1] + List[-i][1] + List[-i][0] - List[-i+1][0]
    

maxv = max(clck + anti)
for i in range(1,n+1):
    kcal = clck[i] - List[i][0]
    for j in range(n+1-i):
        maxv = max(kcal + anti[j], maxv)

for i in range(1,n+1):
    kcal = anti[i] - (cir - List[-i][0])
    for j in range(n+1-i):
        maxv = max(kcal + clck[j], maxv)
print(maxv)

Submission status: Time Limit Exceeded

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

x = [0 for _ in range(n + 2)]
v = [0 for _ in range(n + 1)]
for i in range(n):
    x[i + 1], v[i + 1] = map(int, input().split())

x[n + 1] = c

left = [0 for _ in range(n + 1)]
right = [0 for _ in range(n + 1)]
left_rev = [0 for _ in range(n + 1)]
right_rev = [0 for _ in range(n + 1)]

for i in range(1, n + 1):
    left[i] = left[i - 1] + v[i] - (x[i] - x[i - 1])
    left_rev[i] = left[i] - x[i]
    right[i] = right[i - 1] + v[n - i + 1] - (x[n - i + 2] - x[n - i + 1])
    right_rev[i] = right[i] - (c - x[n - i + 1])

result = 0
for i in range(1, n + 1):
    temp = max( (left[i] + max(right_rev[:n - i + 1])) , (right[i] + max(left_rev[:n - i + 1])) )
    if temp > result:
        result = temp

print(result)

Submission status: Time Limit Exceeded

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

v_1 = [sum(v[:i]) for i in range(1, N+1)]
v_n = [sum(v[i:]) for i in range(N-1, -1, -1)]

c = 0
for i in range(N):
    c_t = v_1[i] - x[i]
    if c_t > c:
        c = c_t
    for j in range(i+1, N):
        c_t = v_1[i] + v_n[N-j-1] - (2 * x[i] + (C - x[j]))
        if c_t > c:
            c = c_t
for i in range(N-1, -1, -1):
    c_t = v_n[N-i-1] - (C - x[i])
    if c_t > c:
        c = c_t
    for j in range(i-1, -1, -1):
        c_t = v_1[j] + v_n[N-i-1] - (x[j] + 2*(C - x[i]))
        if c_t > c:
            c = c_t

print(c)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3

n, c = map(int, input().split())
x = [0]
v = [0]
ca = [0]
csum = 0
for i in range(n):
    xi, vi = map(int, input().split())
    x.append(xi)
    v.append(vi)
    csum += vi
    ca.append(csum)
x.append(c)

cb = [0]
for i in range(n):
    csum -= v[i]
    cb.append(csum)
cb.append(0)

gainMax = 0
for a in range(n + 1):
    for b in range(a + 1, n + 2):
        gain = ca[a] + cb[b]
        loss1 = x[a] * 2 + (c - x[b])
        loss2 = x[a] + (c - x[b]) * 2
        loss = min(loss1, loss2)
        gainMax = max(gainMax, gain - loss)

print(gainMax)

Submission status: Time Limit Exceeded

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

ans = 0

for i in range(n + 1):
    d = x[i]
    s = v[:i + 1]
    k = sum(s) - d
    if k > ans:
        ans = k

for j in range(n + 1):
    d = c - x[j]
    s = v[j:]
    k = sum(s) - d
    if k > ans:
        ans = k

for i in range(1, n + 1):
    d1r = 2 * x[i]
    d1l = x[i]
    s1 = sum(v[:i + 1])
    for j in range(i + 1, n + 1):
        d2r = c - x[j]
        s = s1 + sum(v[j:])
        k = s - (d1r + d2r)
        if k > ans:
            ans = k
        d2l = 2 * d2r
        k = s - (d1l + d2l)
        if k > ans:
            ans = k

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

n, c = map(int, input().split())
x = [0] * (n + 1)
v = [0] * (n + 1)
for i in range(1, n + 1):
    x[i], v[i] = map(int, input().split())

ans = 0

for i in range(n + 1):
    d = x[i]
    s = v[:i + 1]
    k = sum(s) - d
    if k > ans:
        ans = k

for j in range(n + 1):
    d = c - x[j]
    s = v[j:]
    k = sum(s) - d
    if k > ans:
        ans = k

for i, j in itertools.combinations(range(1, n + 1), 2):
    d = 2 * x[i] + (c - x[j])
    s = v[:i + 1] + v[j:]
    k = sum(s) - d
    if k > ans:
        ans = k

for i, j in itertools.combinations(range(1, n + 1), 2):
    d = x[i] + 2 * (c - x[j])
    s = v[:i + 1] + v[j:]
    k = sum(s) - d
    if k > ans:
        ans = k

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,C=map(int, raw_input().split())
X=[ tuple( map(int, raw_input().split()) ) for i in range(N)]
T=[]
R=[]

a=[]

v_sum=0
for x,v in X:
	v_sum+=v
	T.append(v_sum-x)

a+=T

v_sum=0
for x,v in X[::-1]:
	v_sum+=v
	R.append(v_sum-(C-x))

a+=R

for i in range(N):
	extra_x=X[i][0]
	if len(R[:N-i-1])>0: val=T[i]+max(R[:N-i-1])-extra_x
	else: val=T[i]
	a.append(val)


for i in range(N):
	extra_x=C-X[N-i-1][0]
	if len(T[:N-i-1])>0: val=R[i]+max(T[:N-i-1])-extra_x
	else: val=R[i]
	a.append(val)


print max(0,max(a))

Submission status: Time Limit Exceeded

Solution code:
N,C=map(int, raw_input().split())
X=[ tuple( map(int, raw_input().split()) ) for i in range(N)]
T=[]
R=[]

a=[]
for i in range(N):
	temp=[]
	for j in range(i+1):
		temp.append(X[j])

	back=X[i][0]

	for j in range(N-1,i,-1):
		x=C-X[j][0]
		v=X[j][1]
		temp.append( (x+back*2,v) )
	val=0
	for x,y in temp:
		val+=y
		a.append(val-x)


for i in range(N):
	
	temp=[]
	for j in range(N-1,N-i-2,-1):
		x=C-X[j][0]
		v=X[j][1]
		temp.append( (x,v) )
	back=C-X[N-1-i][0]

	for j in range(N-i-1):
		x=X[j][0]
		v=X[j][1]
		temp.append( (x+back*2,v) )
	val=0
	for x,y in temp:
		val+=y
		a.append(val-x)


print max(0,max(a))

Submission status: Time Limit Exceeded

Solution code:
N,C=map(int, raw_input().split())
X=[ tuple( map(int, raw_input().split()) ) for i in range(N)]
T=[]
R=[]

a=[]
for i in range(N):
	temp=[]
	for j in range(i+1):
		temp.append(X[j])

	back=X[i][0]

	for j in range(N-1,i,-1):
		x=C-X[j][0]
		v=X[j][1]
		temp.append( (x+back*2,v) )
	val=0
	for x,y in temp:
		val+=y
		a.append(val-x)


for i in range(N):
	
	temp=[]
	for j in range(N-1,N-i-2,-1):
		x=C-X[j][0]
		v=X[j][1]
		#print j
		temp.append( (x,v) )
	back=C-X[N-1-i][0]

	for j in range(N-i-1):
		x=X[j][0]
		v=X[j][1]
		temp.append( (x+back*2,v) )
	val=0
	for x,y in temp:
		val+=y
		a.append(val-x)


print max(0,max(a))