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:
# def check(place, List):
#     flag = 0
#     for i in range(len(List)):
#         distance = abs(i - place)
#         if distance > len(List):
#             distance = len(List) - distance
#         if distance < List[i]:
#             flag = 1
#     return flag
# def getDistance(Now,Next,C):
#     dst = abs(Next - Now)
#     if(dst > C/2):
#         dst = C - dst
#     return dst
#
#
#
# N, C = map(int, input().split())
# # Counter = [0 for i in range(C)]
# X_V = []
# #V = []
# for i in range(N):
#      #x,v = map(int, input().split()))
#      X_V.append(tuple(map(int, input().split())))
# nowPlace = 0
# while(1):
#     getCal = list(map(lambda x:x[1] - getDistance(nowPlace, x[0], C), X_V))
#     nextPlace = X_V[getCal.index(max(getCal))][0]

N, C = map(int, input().split())
X = [0]
V = [0]
for i in range(N):
    x,v = map(int, input().split())
    X.append(x)
    V.append(v)
#print(-X[1])
inv_X = [C-X[i] for i in range(N+1)][::-1]
inv_X.pop()
inv_X.insert(0,0)
#累積和の計算
right = [0]
left = [0]
#print(V)
for i in range(1,N+1):
    right.append(right[i-1]+V[i])
    left.append(left[i-1]+V[N+1-i])
#print('right:', right, 'left:', left)
Rtemp_max = 0
#right -> left pattern
for i in range(N+1):
    for j in range(N+1-i):
        OA = X[i]
        OB = inv_X[j]
        RightMovingDistance = 2 * OA + OB
        getCal = right[i] + left[j] - RightMovingDistance
        #print(i, j,right[i], left[j], RightMovingDistance)
        #print('Right_i:',i,'Right_getCal:',getCal)
        Rtemp_max = max(Rtemp_max, getCal)
#left -> right pattern
Ltemp_max = 0
for i in range(N+1):
    for j in range(N+1-i):
        OA = X[i]
        OB = inv_X[j]
        LeftMovingDistance = OA + 2 * OB
        getCal = right[i] + left[j] - LeftMovingDistance
        #print(i, j,right[i], left[j], LeftMovingDistance)
        #print('Right_i:',i,'Right_getCal:',getCal)
        Ltemp_max = max(Ltemp_max, getCal)
ans = max(Rtemp_max, Ltemp_max)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N, C = [int(x) for x in input().split()]
left_sushi = np.array([ [int(x) for x in input().split()] for i in range(N)])
right_sushi = np.copy(left_sushi[::-1] )
right_sushi[:, 0] = C - right_sushi[:, 0]

left_gain = np.zeros(N)
right_gain = np.zeros(N)

for i in range(N):
    left_gain[i] = sum(left_sushi[:i+1, 1] ) - left_sushi[i, 0]
    right_gain[i] = sum(right_sushi[:i+1, 1] ) - right_sushi[i, 0]
left_round_gain = left_gain - left_sushi[:, 0]
right_round_gain = right_gain - right_sushi[:, 0]

sum_gain = 0
max_sum_gain = 0

left_gain = left_gain.tolist()
left_round_gain = left_round_gain.tolist()
right_gain = right_gain.tolist()
right_round_gain = right_round_gain.tolist()

# max_left_gain = max( left_gain)
# max_left_round_gain = 0
# max_right_gain = max( right_gain )
# max_right_round_gain = 0

for i in range( N+1):
    sum_gain = max( max(left_round_gain[:i]+[0]) + max(right_gain[:N - i]+[0]), sum_gain)
    sum_gain = max( max(right_round_gain[:i]+[0]) + max(left_gain[:N - i]+[0]), sum_gain)

print( int(sum_gain) )

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N, C = [int(x) for x in input().split()]
left_sushi = np.array([ [int(x) for x in input().split()] for i in range(N)])
right_sushi = np.copy(left_sushi[::-1] )
right_sushi[:, 0] = C - right_sushi[:, 0]
left_gain = np.array([ sum(left_sushi[:i+1, 1] ) - left_sushi[i, 0] for i in range(len(left_sushi)) ]  )
left_round_gain = left_gain - left_sushi[:, 0]
right_gain = np.array([ sum(right_sushi[:i+1, 1] ) - right_sushi[i, 0] for i in range(len(right_sushi)) ] )
right_round_gain = right_gain - right_sushi[:, 0]

sum_gain = 0
max_sum_gain = 0

left_gain = left_gain.tolist()
left_round_gain = left_round_gain.tolist()
right_gain = right_gain.tolist()
right_round_gain = right_round_gain.tolist()

for i in range( N+1):
    sum_gain = max( max(left_round_gain[:i]+[0]) + max(right_gain[:N - i]+[0]), sum_gain)
    sum_gain = max( max(right_round_gain[:i]+[0]) + max(left_gain[:N - i]+[0]), sum_gain)

print( sum_gain)

Submission status: Time Limit Exceeded

Solution code:
N,C = map(int,input().split())
X = [0]*N
V = [0]*N
XX = [0]*N
VV = [0]*N
for i in range(N):
    X[i],V[i] = map(int,input().split())
    XX[N-i-1] = C-X[i]
    VV[N-i-1] = V[i]
from numpy import array as arr
V = arr(V)
VV = arr(VV)
a = [0]*(2*N-1)
b = [0]*(2*N-1)
for i in range(N):
    x = X[i]
    v = V[:i+1].sum()
    xx = XX[i]
    vv = VV[:i+1].sum()
    a[i] = v-x
    b[i] = vv-xx
aa = arr([0]*N)
bb = arr([0]*N)
for i in range(N):
    aa[i] = max(a[:i+1])
    bb[i] = max(b[:i+1])
for i in range(N-1):
    x = 2*(C-X[-i-1])
    v = V[-i-1:].sum()
    xx = 2*(C-XX[-i-1])
    vv = VV[-i-1:].sum()
    a[N+i] = v-x+aa[N-i-2]
    b[N+i] = vv-xx+bb[N-i-2]
a.append(0)
print(max(max(a),max(b)))

Submission status: Time Limit Exceeded

Solution code:
N,C = map(int,input().split())
X = [0]*N
V = [0]*N
XX = [0]*N
VV = [0]*N
for i in range(N):
    X[i],V[i] = map(int,input().split())
    XX[N-i-1] = C-X[i]
    VV[N-i-1] = V[i]
from numpy import array as arr
X = arr(X)
V = arr(V)
XX = arr(XX)
VV = arr(VV)
a = []
b = []
for i in range(N):
    x = X[i]
    v = V[:i+1].sum()
    xx = XX[i]
    vv = VV[:i+1].sum()
    a.append(v-x)
    b.append(vv-xx)
aa = arr([0]*N)
bb = arr([0]*N)
for i in range(N):
    aa[i] = max(a[:i+1])
    bb[i] = max(b[:i+1])
for i in range(N-1):
    x = 2*(C-X[-i-1])
    v = V[-i-1:].sum()
    xx = 2*(C-XX[-i-1])
    vv = VV[-i-1:].sum()
    a.append(v-x+aa[N-i-2])
    b.append(vv-xx+bb[N-i-2])
a.append(0)
print(max(max(a),max(b)))

Submission status: Time Limit Exceeded

Solution code:
N,C = map(int,input().split())
X = [0]*N
V = [0]*N
for i in range(N):
    X[i],V[i] = map(int,input().split())
from numpy import array as arr
X = arr(X)
V = arr(V)
a = []
for i in range(N):
    for j in range(N):
        if i != j:
            k = min(i,j)
            l = max(i,j)
            x = min(2*X[k]+C-X[l],2*(C-X[l])+X[k])
            v = V[:k+1].sum() + V[l:].sum()
            a.append(v-x)
for i in range(N):
    x = X[i]
    v = V[:i+1].sum()
    a.append(max(V[:i+1].sum()-X[i],V[i:].sum()-(C-X[i])))
a.append(0)
a.sort()
print(a[-1])

Submission status: Time Limit Exceeded

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)]

v.reverse()

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

Tall = [max(T + Trev)]

#Tall_d = [(T[i] + Trev[j] - min(x[i], rev_x[j])) for j in range(N - i - 1) for i in range(N)]
Tall = Tall + [max([(T[i] + Trev[j] - min(x[i], rev_x[j])) for j in range(N - i -1)], default = 0) for i in range(N)]

Tmax = max(Tall)
if Tmax < 0:
    Tmax = 0
print(Tmax)

Submission status: Time Limit Exceeded

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)]

v.reverse()

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

Tall = T + Trev

#Tall_d = [(T[i] + Trev[j] - min(x[i], rev_x[j])) for j in range(N - i - 1) for i in range(N)]
Tall = Tall + [max([(T[i] + Trev[j] - min(x[i], rev_x[j])) for j in range(N - i -1)], default = 0) for i in range(N)]

Tmax = max(Tall)
if Tmax < 0:
    Tmax = 0
print(Tmax)

Submission status: Time Limit Exceeded

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)]

v.reverse()

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

Tall = T + Trev

for i in range(N):
    for j in range(N - i - 1):
        Tall.append(T[i] + Trev[j] - min(x[i], rev_x[j]))

Tmax = max(Tall)
if Tmax < 0:
    Tmax = 0
print(Tmax)

Submission status: Time Limit Exceeded

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

x = []
rev_x = []
v = []

T = []
Trev = []

Tall = []

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

rev_x.reverse()

for i in range(N):
    T.append(sum(v[0:i+1]) - x[i])

v.reverse()

for i in range(N):
    Trev.append(sum(v[0:i+1]) - rev_x[i])

Tall = T + Trev

for i in range(N):
    for j in range(N - i - 1):
        Tall.append(T[i] + Trev[j] - min(x[i], rev_x[j]))

Tmax = max(Tall)
if Tmax < 0:
    Tmax = 0
print(Tmax)

Submission status: Time Limit Exceeded

Solution code:
N, C = list(map(int,input().split()))
x_list = [0]
v_list = []

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

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
            
# 反時計回りスタート
# xとvの置き換えることで対応する
x_list = [x_list[0]] + [C-x for x in x_list[1:][::-1]]
v_list = v_list[::-1]
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

            
print(max_c)

Submission status: Time Limit Exceeded

Solution code:
N, C = list(map(int,input().split()))
x_list = [0]
v_list = []

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

max_c = 0

threshold_x = max(x_list)

# 時計回りスタート
for i in range(N):
#    print("------")
#    print(i)
    c = 0
    th = 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]
        th += x_tmp[j+1] - x_tmp[j]
        if th > threshold_x:
            break
#        print(c)
        if max_c < c:
            max_c = c
    if th > threshold_x:
        break
    
    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]
        th += x_tmp[j+1] - x_tmp[j]
        if th > threshold_x:
            break
#        print(c)
        if max_c < c:
            max_c = c
    if th > threshold_x:
        break
            
# 反時計回りスタート
# xとvの置き換えることで対応する
x_list = [x_list[0]] + [C-x for x in x_list[1:][::-1]]
v_list = v_list[::-1]
for i in range(N):
#    print("------")
#    print(i)
    c = 0
    th = 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]
        th += x_tmp[j+1] - x_tmp[j]
        if th > threshold_x:
            break
#        print(c)
        if max_c < c:
            max_c = c
    if th > threshold_x:
        break
    
    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]
        th += x_tmp[j+1] - x_tmp[j]
        if th > threshold_x:
            break
#        print(c)
        if max_c < c:
            max_c = c
    if th > threshold_x:
        break

            
print(max_c)

Submission status: Time Limit Exceeded

Solution code:
n,c = map(int, input().split())
x = [0]
xr = [0]
v = []
va = [0]
fa_clock = [0]
fa_reclock = [0]

prev = 0
for i in range(n):
    xi,vi = map(int, input().split())
    x.append(xi)
    xr.append(c - xi)
    v.append(vi)
    va.append(vi + prev)
    fa_clock.append(vi + prev - xi)
    fa_reclock.append(vi + prev - xi*2)
    prev += vi
v.append(0)

maxcal = 0
for ib in range(n+1):
    clock = max(fa_clock[:n+1-ib]) + sum(v[n-ib:]) - 2*xr[-ib]
    reclock = max(fa_reclock[:n+1-ib]) + sum(v[n-ib:]) - xr[-ib]
    ans = max(clock, reclock)
    if maxcal < ans: maxcal = ans
print(maxcal)

Submission status: Time Limit Exceeded

Solution code:
n,c = map(int, input().split())
x = [0]
xr = [0]
va = [0]
fa_clock = [0]
fa_reclock = [0]

prev = 0
vb = [0 for _ in range(n+1)]
for i in range(n):
    xi,vi = map(int, input().split())
    x.append(xi)
    xr.append(c - xi)
    va.append(vi + prev)
    fa_clock.append(vi + prev - xi)
    fa_reclock.append(vi + prev - xi*2)
    prev += vi
    for j in range(i+1):
        vb[j] += vi
vb.reverse()

maxcal = 0
for ib in range(n+1):
    clock = max(fa_clock[:n+1-ib]) + vb[ib] - 2*xr[-ib]
    reclock = max(fa_reclock[:n+1-ib]) + vb[ib] - xr[-ib]
    ans = max(clock, reclock)
    if maxcal < ans: maxcal = ans
print(maxcal)

Submission status: Time Limit Exceeded

Solution code:
n,c = map(int, input().split())
x = [0]
xr = [0]
v = [0]
for _ in range(n):
    xi,vi = map(int, input().split())
    x.append(xi)
    xr.append(c - xi)
    v.append(vi)
clockmax = -1
reclockmax = -1
score = 0
for i in range(n+1):
    for j in range(n+1-i):
        for k in range(i+1):
            score += v[k]
        for k in range(j+1):
            score += v[-k]
        score2 = score
        score -= 2*x[i] + xr[-j]
        score2 -= x[i] + 2*xr[-j]
        if clockmax < score: clockmax = score
        if reclockmax < score2: reclockmax = score2
        score = 0
        score2 = 0

print(max(clockmax,reclockmax))

Submission status: Time Limit Exceeded

Solution code:
n,c=map(int, input().split())
x=[]
v=[]

for _ in range(n):
    xi,vi=map(int, input().split())
    x.append(xi)
    v.append(vi)
xr=list((map(lambda _: c-_, x[::-1])))
x.insert(0,0)
xr.insert(0,0)

score=[0] 
score_r=[0]

tmp=0
for i, vi in enumerate(v):
    tmp+=vi
    score.append(tmp-x[i+1])

tmp=0
for i, vi in enumerate(v[::-1]):
    tmp+=vi
    score_r.append(tmp-xr[i+1])

maxscore=0
for i, si in enumerate(score):
    tmp=si-x[i]+max(score_r[:n-i+1])
    maxscore=max(tmp,maxscore)
for i, si in enumerate(score_r):
    tmp=si-xr[i]+max(score[:n-i+1])
    maxscore=max(tmp,maxscore)
print(maxscore)

Submission status: Time Limit Exceeded

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
for i in range(N):
    if i == 0:
        R[i] = v[i] - x[i]
        L[N-1-i] = v[N-1-i] - (C - x[N-1-i])
    else:
        R[i] = R[i - 1] + v[i] - (x[i] - x[i - 1])
        L[N-1-i] = L[N-i] + v[N-1-i] - (x[N-i] - x[N-1-i])
R2L = 0
L2R = 0
for i in range(N-1):
    R2L = max(R2L, R[i] - x[i] + max(L[i+1:]))
    L2R = max(L2R, L[i+1] - (C - x[i+1]) + max(R[:i+1]))

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

Submission status: Time Limit Exceeded

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])

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

print(max([0] + clock + antiClock + clock2antiClock + antiClock2clock))

Submission status: Time Limit Exceeded

Solution code:
n, c = [int(i) for i in input().split()]
xv = [[int(i) for i in input().split()] for i in range(n)]
xv2 = [x for x in xv]
xv.insert(0, [0, 0])
xv2.append([0,0])

oa = [0]*(n+1)
ob = [0]*(n+1)
for i in range(1,n+1):
    oa[i] += oa[i-1] + xv[i][1]
for i in range(n, 0, -1):
    ob[i-1] += ob[i] + xv[i][1]
ob = ob[::-1]
e = 0
for i in range(n+1):
    e = max(e, oa[i]-xv[i][0])
for i in range(n+1):
    e = max(e, ob[i]-(c - xv[n-i][0]))

for i in range(n+1):
    for j in range(n+1-i):
        e = max(e, max(oa[i]-xv[i][0]*2+ob[j]-(c - xv2[n-j][0]), oa[i]-xv[i][0]+ob[j]-(c - xv2[n-j][0])*2))
print(e)

Submission status: Time Limit Exceeded

Solution code:
n,c = map(int,input().split())
l = [[0,0] for _ in range(n)]
dp1 = [0 for _ in range(n)]
dp2 = [0 for _ in range(n)]
for i in range(n):
    l[i] = list(map(int, input().split()))

dp1[0] = l[0][1] - l[0][0]
dp2[0] = l[-1][1] - c + l[-1][0]
for i in range(1,n):
    dp1[i] = dp1[i-1] + l[i][1] - l[i][0] + l[i-1][0]
    dp2[i] = dp2[i-1] + l[-i-1][1] - l[-i][0] + l[-i-1][0]

ans = max(max(dp1),max(dp2))
tmp = 0
for i in range(n-1):
    for j in range(n-i-1):
        tmp = dp1[i]+dp2[j]-l[i][0]
        if ans < tmp:
            ans = tmp
tmp2 = 0
for i in range(n-1):
    for j in range(n-i-1):
        tmp2 = dp2[i] + dp1[j] - c + l[-i-1][0]
        if ans < tmp2:
            ans = tmp2

if ans > 0:
    print(ans)
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
# 外周Cメートル
# カウンター上には N 貫の寿司
# i 貫目は中橋くんがいる位置から x_i メートル時計回りに進んだ位置に置かれており、
# v_i キロカロリーの栄養価を持ちます
# 歩く際に 1 メートルあたり 1 キロカロリーを消費

N, C = map(int,input().split())
x=["" for i in range(N)]
v=["" for i in range(N)]


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

#時計回りの場合の最大
CalR=[0 for i in range(N)]
for i in range(N):
    CalR[i]=sum(v[:i+1])-x[i]

MaxR=[0 for i in range(N+1)]

for i in range(1,N+1):
    if CalR[i-1]>=MaxR[i-1]:
        MaxR[i]=CalR[i-1]
    else:
        MaxR[i]=MaxR[i-1]

#反時計回りの最大
CalL=[0 for i in range(N)]
for i in range(N):
    CalL[i]=sum(v[N-i-1:])-(C-x[N-i-1])

MaxL=[0 for i in range(N+1)]

for i in range(1,N+1):
    if CalL[i-1]>=MaxL[i-1]:
        MaxL[i]=CalL[i-1]
    else:
        MaxL[i]=MaxL[i-1]

#最初時計回りに進んだ場合の答え
AnsR=[0 for i in range(N)]

for i in range(N):
    AnsR[i]=CalR[i]-x[i]+MaxL[N-1-i]

#最初反時計回りに進んだ場合の答え
AnsL=[0 for i in range(N)]

for i in range(N):
    AnsL[i]=CalL[i]-(C-x[N-1-i])+MaxR[N-1-i]

print(max(max(AnsR),max(AnsL),max(CalR),max(CalL),0))

Submission status: Time Limit Exceeded

Solution code:
# 外周Cメートル
# カウンター上には N 貫の寿司
# i 貫目は中橋くんがいる位置から x_i メートル時計回りに進んだ位置に置かれており、
# v_i キロカロリーの栄養価を持ちます
# 歩く際に 1 メートルあたり 1 キロカロリーを消費

N, C = map(int,input().split())
x=["" for i in range(N)]
v=["" for i in range(N)]

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

#時計回りの場合の最大
CalR=[0 for i in range(N)]
for i in range(N):
    CalR[i]=sum(v[:i+1])-x[i]

MaxR=[0 for i in range(N)]

for i in range(N):
    MaxR[i]=max(CalR[:i+1])
    
MaxR=[0]+MaxR

#反時計回りの最大
CalL=[0 for i in range(N)]
for i in range(N):
    CalL[i]=sum(v[N-i-1:])-(C-x[N-i-1])

MaxL=[0 for i in range(N)]

for i in range(N):
    MaxL[i]=max(CalL[:i+1])

MaxL=[0]+MaxL

#最初時計回りに進んだ場合の答え
AnsR=[0 for i in range(N)]

for i in range(N):
    AnsR[i]=CalR[i]-x[i]+MaxL[N-1-i]

#最初反時計回りに進んだ場合の答え
AnsL=[0 for i in range(N)]

for i in range(N):
    AnsL[i]=CalL[i]-(C-x[N-1-i])+MaxR[N-1-i]

print(max(max(AnsR),max(AnsL),max(CalR),max(CalL),0))

Submission status: Time Limit Exceeded

Solution code:
n, c = input().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 = 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):
    e1 = sv[i] - x[i]
    for j in range(-1, -n +  i  - 1, -1):
        e2 = sv[-1] - sv[j - 1] - (c - x[j])
        e3 = e1 + e2 - x[i]
        e4 = e1 + e2 - (c - x[j])
        ans = max([ans, e2, e3, e4])
    ans = max([ans, e1])
for j in range(0, -n - 1, -1):
    e2 = sv[-1] - sv[j - 1] - (c - x[j])
    for i in range(1, n - i + 1, 1):
        e1 = sv[i] - x[i]        
        e3 = e1 + e2 - x[i]
        e4 = e1 + e2 - (c - x[j])
        ans = max([ans, e1, e3, e4])
    ans = max([ans, e1])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def solve():
    N, C = map(int, raw_input().split())
    xs, vs = [], []
    for i in range(N):
        x, v = map(int, raw_input().split())
        xs.append(x)
        vs.append(v)
    xs, vs = map(list, zip(*sorted(zip(xs, vs), key=lambda _: _[0])))
    a = f(xs, vs, N, C)
    b = f(map(lambda x: C-x, xs[::-1]), vs[::-1], N, C)
    ans = max(a, b)
    return ans

def f(xs, vs, N, C):
    best = 0
    cur = 0
    px = 0
    best_index = -1
    targets = [(best_index, best)]
    for i in range(N):
        x, v = xs[i], vs[i]
        cur += v - abs(x-px)
        if best < cur:
            best_index = i
            best = cur
            targets.append((best_index, best))
        px = x
    for target_index, target in targets:
        cur = target
        if target_index != -1:
            cur -= xs[target_index]
        px = C
        for i in range(N-1, target_index, -1):
            x, v = xs[i], vs[i]
            cur += v - abs(x-px)
            if best < cur:
                best = cur
            px = x
    return best

print solve()

Submission status: Time Limit Exceeded

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

    rev_V = V[::-1]
    rev_X = tuple(map(lambda x: C-x, X[::-1]))
    a1 = solve(N, X, V, rev_X, rev_V)
    a2 = solve(N, rev_X, rev_V, X, V)
    ans = max(a1,a2)
    return ans

def solve(N, X, V, rev_X, rev_V):
    ans = 0
    for i in range(N):
        a = sum(V[:i+1]) - X[i]
        ans = max(ans, a)
        a = max(a - X[i], 0)
        for k in range(N-i-1):
            b = a + sum(rev_V[:k+1]) - rev_X[k]
            ans = max(ans, b)
    return ans

# m()
# print(ans())
print(someone_ans())

Submission status: Time Limit Exceeded

Solution code:
N, C = map(long, raw_input().split())
Sushi  = [map(long, raw_input().split()) for i in range(N)]
Sushi_rev = []
for s in Sushi[::-1]:
  Sushi_rev.append([C-s[0], s[1]])
Sushi.insert(0L, [0L, 0L])
Sushi_rev.insert(0L, [0L, 0L])
N += 1
ans = 0
cal_clock = []
tmp = 0
for i in Sushi:
  tmp += i[1]
  cal_clock.append(tmp)
cal_anticlock = []
tmp = 0
for i in Sushi_rev:
  tmp += i[1]
  cal_anticlock.append(tmp)
ans = 0
for a in range(N):
  for b in range(N-a):
    oa = Sushi[a][0]
    ob = Sushi_rev[b][0]
    eaten = cal_clock[a]+cal_anticlock[b]
    get_cal_oab = eaten - (2 * oa +ob)
    get_cal_oba = eaten - (2 * ob +oa)
    get_cal = eaten - (oa+ob) - (oa if oa < ob else ob)
    if get_cal > ans:
      ans = get_cal
print ans

Submission status: Time Limit Exceeded

Solution code:
def main():
    """
    1 <= N <= 10^5
    2 <= C <= 10^14
    1 <= x1 < x2 < ... < xN < C
    1 <= vi <= 10^9
    """
    N, C = map(int, input().split())
    X, V = zip(*(
        map(int, input().split())
        for _ in range(N)
    ))

    part_point(N, C, X, V)


def part_point(N, C, X, V):
    """
    円周すべてを通るパターンは最適ではない
        xN < C より
    時計回りにある寿司Aまで
    そこから初期位置まで
    最後に反時計回りにBまで
        OA * 2 + OB or
        OB * 2 + OA
    """
    # tupleの方が省memory (sys.getsizeof より)
    rev_V = V[::-1]
    rev_X = tuple(map(lambda x: C-x, X[::-1]))

    # 逆回りを計算し忘れてた
    a1 = solove_p(N, X, V, rev_X, rev_V)
    a2 = solove_p(N, rev_X, rev_V, X, V)

    ans = max(a1, a2)
    print(ans)

def solove_p(N, X, V, rev_X, rev_V):
    ans = 0
    for i in range(0, N):
        # Aまで
        a = sum(V[:i+1]) - X[i]
        ans = max(ans , a) 
        # Oまで戻る
        a = max(a - X[i], 0)
        for j in range(N-i-1):
            # Bまで
            b = a + sum(rev_V[:j+1]) - rev_X[j]
            ans = max(ans , b)

    return ans


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def d_static_sushi(N, C, Sushi):
    sushi_pos_cw = [row[0] for row in Sushi]
    sushi_pos_ccw = [abs(row[0] - C) for row in Sushi][::-1]
    sushi_value_cw = [row[1] for row in Sushi]
    sushi_value_ccw = sushi_value_cw[::-1]

    # 時計回り
    f1 = [sum(sushi_value_cw[:i + 1]) - sushi_pos_cw[i] for i in range(N)]
    g1 = [max(f1[:i + 1]) for i in range(N)]
    f2 = [sum(sushi_value_ccw[:i + 1]) - sushi_pos_ccw[i] for i in range(N)]
    g2 = [max(f2[:i + 1]) for i in range(N)]

    tmp1 = 0
    for b in range(N + 1):  # b:反時計回りに歩いて食べる寿司の個数
        if b == 0:
            cal = g1[N - 1]
        elif b == N:
            cal = f2[b - 1]
        else:
            cal = g1[N - b - 1] + f2[b - 1] - sushi_pos_ccw[b - 1]
        tmp1 = max(tmp1, cal)

    tmp2 = 0
    for a in range(N + 1):  # a:時計回りに歩いて食べる寿司の個数
        if a == 0:
            cal = g2[N - 1]
        elif a == N:
            cal = f1[a - 1]
        else:
            cal = g2[N - a - 1] + f1[a - 1] - sushi_pos_cw[a - 1]
        tmp2 = max(tmp2, cal)
    ans = max(tmp1, tmp2)
    return ans

N,C = [int(i) for i in input().split()]
Sushi = [[int(i) for i in input().split()] for j in range(N)]
print(d_static_sushi(N, C, Sushi))