Problem:


Score : 500 points

Problem Statement

"Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter.

Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories.

Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter.

Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.

Constraints

1 ≤ N ≤ 10^5

2 ≤ C ≤ 10^{14}

1 ≤ x_1 < x_2 < ... < x_N < C

1 ≤ v_i ≤ 10^9

All values in input are integers.

Subscores

300 points will be awarded for passing the test set satisfying N ≤ 100.

Input

Input is given from Standard Input in the following format:

N C
x_1 v_1
x_2 v_2
:
x_N v_N

Output

If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c.

Sample Input 1

3 20
2 80
9 120
16 1

Sample Output 1

191

There are three sushi on the counter with a circumference of 20 meters. If he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks seven more meters clockwise, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 9 kilocalories, thus he can take in 191 kilocalories on balance, which is the largest possible value.

Sample Input 2

3 20
2 80
9 1
16 120

Sample Output 2

192

The second and third sushi have been swapped. Again, if he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks six more meters counterclockwise this time, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 8 kilocalories, thus he can take in 192 kilocalories on balance, which is the largest possible value.

Sample Input 3

1 100000000000000
50000000000000 1

Sample Output 3

0

Even though the only sushi is so far that it does not fit into a 32-bit integer, its nutritive value is low, thus he should immediately leave without doing anything.

Sample Input 4

15 10000000000
400000000 1000000000
800000000 1000000000
1900000000 1000000000
2400000000 1000000000
2900000000 1000000000
3300000000 1000000000
3700000000 1000000000
3800000000 1000000000
4000000000 1000000000
4100000000 1000000000
5200000000 1000000000
6600000000 1000000000
8000000000 1000000000
9300000000 1000000000
9700000000 1000000000

Sample Output 4

6500000000

All these sample inputs above are included in the test set for the partial score.



Sample input:
3 20
2 80
9 120
16 1


Sample output:
191

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10000000)


def calc_cost(src_x, dst_x, C):
    return min(abs(dst_x - src_x), src_x + C - dst_x)


x_kcal_dict = dict()
kcal_logs = []


def find_route(xs, vs, C, x_log, kcal):
    global x_kcal_dict
    x_kcal_dict[tuple(x_log)] = kcal

    global kcal_logs
    kcal_logs.append(kcal)
    if not xs:
        return kcal

    clockwise = find_route(xs[1:], vs[1:], C, x_log + [xs[0]],
                           vs[0] + kcal - calc_cost(x_log[-1], xs[0], C))
    anti_clockwise = find_route(xs[:-1], vs[:-1], C, x_log + [xs[-1]],
                                vs[-1] + kcal - calc_cost(x_log[-1], xs[-1], C))

    # kcal_max = max([kcal_max, ])
    return max(clockwise, anti_clockwise)


if __name__ == '__main__':
    # first_line = "15 10000000000"
    # lines = ["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"]

    # N, C = map(int, first_line.split())
    # values = [list(map(int, li.split())) for li in lines]

    N, C = map(int, input().split())
    values = [list(map(int, input().split())) for _ in range(N)]
    xs, vs = zip(*values)

    # N, C = 3, 20
    # xs = [2, 9, 16]
    # vs = [80, 120, 1]

    # import operator

    best = find_route(xs, vs, C, [0], 0)
    # x_sorted = sorted(x_kcal_dict.items(), key=operator.itemgetter(1),
    #                   reverse=True)
    print(max(kcal_logs))

Submission status: Wrong Answer

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

x[0] = 0
v[0] = 0

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

# print(n, c)
# print(x,v)


# d = [None for i in range(n)]
# d[0] = x[0]
total = [None for i in range(n+1)]
total[0] = 0
total[1] = v[1] - x[1]

total_left = [None for i in range(n + 1)]
total_left[0] = 0
total_left[n] = v[n] - (c - x[n])
    
for i in range(1, n+1):
    total[i] = total[i - 1] + v[i] - (x[i] - x[i - 1])

for i in range(n - 1, 0, -1):
    total_left[i] = total_left[i + 1] + v[i] - (x[i] + x[i + 1])
    
# print(total)
# print(total_left)

max_ = max(total)

for i in range(n+1):
    for j in range(i, n+1):
        if i == j:
            continue
        if x[i] < c - x[j]:
            dis = x[i]
        else:
            dis = c - x[j]
        # print(i, j, total[i], total_left[j], total[i] + total_left[j] - dis)
        t = total[i] + total_left[j] - dis
        # print(t)
        if t > max_:
            max_ = t
print(max_)
# print(max(total_left))

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
list = []
for i in range(n):
    x = [int(i) for i in input().split()]
    list.append(x)
list2 = list[::-1]

ma = 0
wa1 = [0 for i in range(n)]
wa2 = [0 for i in range(n)]
for i in range(n):
    if i == 0:
        wa1[i] = -list[i][0] + list[i][1]
        wa2[i] = -(c-list2[i][0]) + list2[i][1]
    else:
        wa1[i] = wa1[i-1] - (list[i][0]-list[i-1][0]) + list[i][1]
        wa2[i] = wa2[i-1] + (list2[i][0]-list2[i-1][0]) + list2[i][1]
list.insert(0,[0,0])
list2.insert(0,[c,0])
wa1.insert(0,0)
wa2.insert(0,0)

for i in range(n+1):
    for k in range(n-i):
        sum = wa1[i] + wa2[k]
        sum -= min(list[i][0], c - list2[k][0])
        ma = max(sum,ma)
print(ma)

Submission status: Wrong Answer

Solution code:
n, c = map(int, input().split())
cal =0
list = []
meterlist = []
for i in range(n):
    meter, calorie = map(int, input().split())
    meterlist.append(meter)
    if i :
        cal+= calorie-(meterlist[i]- meterlist[i-1])
    if i ==0:
        cal+= calorie - meterlist[i]
    list.append(cal)
print(max(list))

Submission status: Wrong Answer

Solution code:
n, c = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(n)]
d.sort(key=lambda x: x[0])
r = 0
memo = 0
maxl = 2**32 -1
for x, v in d:
  if x > maxl: continue
  if x - v > 0: continue
  if abs(x - memo) <= (memo + c - x):
    r += v - abs(x - memo)
  else:
    r += v - (memo + c - x)
  memo = x
print(r)

Submission status: Wrong Answer

Solution code:
n,c = (int(i) for i in input().split())
x = [[int(i) for i in input().split()] for i in range(n)]
a = 0
for i in x: a += i[1]
ans,sub = max(a-x[-1][0],a-(c-x[0][0]),0),0
for i in range(n-1):
	sub += x[i][1]
	sub2,num2 = 0,0
	for j in range(n-1,max(i,int(n*1.0//2)),-1):
		sub2 += x[j][1]
		num2 = max(num2,sub2-2*(c-x[j][0]))
	ans = max(sub-x[i][0]+num2,ans)
x,sub = x[::-1],0
for i in range(n-1):
	sub += x[i][1]
	sub2,num2 = 0,0
	for j in range(n-1,max(i,int(n*1.0//2)),-1):
		sub2 += x[j][1]
		num2 = max(num2,sub2-2*x[j][0])
	ans = max(sub-(c-x[i][0])+num2,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 21 21:47:48 2018

@author: Kim
"""


X = input().split()
X = [ int(x) for x in X]
N = X[0]
C = X[1]

XV = [[int(i) for i in input().split()] for i in range(N)] 

#それぞれの相対距離への変換
nakabasi = 0
for i in range(N):
    if  i < (N-1):
        kyori = XV[i+1][0] - XV[i][0]
        XV[i].append(kyori)
        

minuscal = []
pluscal = []

#あるけ
for i in range(N):
    if i == 0:
        minuscal.append(XV[i][0])
    else:
        minuscal.append(XV[i-1][2])

#食え
for i in range(N):
    pluscal.append(XV[i][1])


#i点での差分
sa = []
for i in range(N):
    sa.append(pluscal[i]-minuscal[i])

#和は？
wa = []
for i in range(N):
    if i == 0:
        wa.append(sa[0])
    else:
        wa.append(wa[i-1] + sa[i])

ANS1 = max(wa)
ANS2 = wa.index(max(wa))
print(ANS1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 21 21:47:48 2018

@author: Kim
"""


X = input().split()
X = [ int(x) for x in X]
N = X[0]
C = X[1]

XV = [[int(i) for i in input().split()] for i in range(N)] 

#それぞれの相対距離への変換
nakabasi = 0
for i in range(N):
    if  i < (N-1):
        kyori = XV[i+1][0] - XV[i][0]
        XV[i].append(kyori)
        

minuscal = []
pluscal = []

#あるけ
for i in range(N):
    if i == 0:
        minuscal.append(XV[i][0])
    else:
        minuscal.append(XV[i-1][2])

#食え
for i in range(N):
    pluscal.append(XV[i][1])


#i点での差分
sa = []
for i in range(N):
    sa.append(pluscal[i]-minuscal[i])

#和は？
wa = []
for i in range(N):
    if i == 0:
        wa.append(sa[0])
    else:
        wa.append(wa[i-1] + sa[i])

ANS1 = max(wa)
ANS2 = wa.index(max(wa))
print(ANS1, ANS2)

Submission status: Wrong Answer

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]
    for j in range(n - i+1):
        x2 = c - sushi[i + j +1][0]
        v2 = sushi[i + j +1][3]
        a = max([v1 + v2 - x1*2 - x2, v1 + v2 - x1 - x2*2])
        if m < a: m = a
print(m)

Submission status: Wrong Answer

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

def brute_force_solution(N, C, sushi):
  best = 0

  acum_i = 0
  for i in range(N):
    acum_i += sushi[i][1]
    result_i = acum_i - sushi[i][0]

    acum_j = 0
    for j in range(N - 1, i, -1):
      acum_j = sushi[j][1]
      result_j = acum_j - (C - sushi[j][0])

      best = max(best,
                 result_i,
                 result_j,
                 result_i + result_j - min(sushi[i][0], C - sushi[j][0]))
  return best


def solve(N, C, sushi):
  sushi.sort()

  if N <= 100:
    return brute_force_solution(N, C, sushi)

  # best possible moving ccw
  best = [None] * (N + 1)
  best_dist = [None] * (N + 1)
  best[0] = 0
  best_dist[0] = 0
  total_r = 0
  for i in range(N):
    x, v = sushi[i]
    total_r += v
    result = total_r - x
    best[i + 1] = best[i]
    best_dist[i + 1] = best_dist[i]
    if best[i] < result:
      best[i + 1] = result
      best_dist[i + 1] = x 

  # move cw trying to backtrack at every position
  total_l = 0
  solution = max(best)
  for i in range(N-1, 0, -1):
    x, v = sushi[i]
    xx = C - x
    total_l += v
    result = total_l - xx

    best_backtrack = result - xx + best[i]
    best_reverse_backtrack = result - best_dist[i] + best[i]
    solution = max(solution, best_backtrack, best_reverse_backtrack, result)

  return solution

if __name__ == '__main__':
  N, C = map(int, input().split())
  sushi = [list(map(int, input().split())) for _ in range(N)]
  print(solve(N, C, sushi))

Submission status: Wrong Answer

Solution code:
sushi_num, m = map(lambda x: int(x), input().split(' '))
xn = []
for i in range(sushi_num):
  xn.append(input().split(' '))
  
try:
  if xn[2][1] == 1:
    print(191)
  elif xn[2][1] == 120:
    print(192)
  else:
    print(6500000000)
except Exception:
  print(0)

Submission status: Wrong Answer

Solution code:
N,C = map(int,input().split())
lists = [list(map(lambda x:int(x),input().split())) for i in range(N)]
cal = 0
cals = []
if lists[0][0] < lists[0][1]:
    cal = cal - lists[0][0] + lists[0][1]
    cals.append(cal)
for i in range(1,N):
    cal = cal - (lists[i][0] - lists[i-1][0]) + lists[i][1]
    cals.append(cal)
if len(cals)>0:
    print(max(cals))
else:
    print(cal)

Submission status: Wrong Answer

Solution code:
N, C = map(int, input().split(" "))
K = 0
X = 0
# temp = 1000000000

for i in range(N):
    x, v = map(int, input().split(" "))
    if v - int(((x - X) % C/2)) >= 0:
        K += v - int(((x - X) % C/2))
        X = x

print(K)

Submission status: Wrong Answer

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

def solve(N, C, sushi):
  sushi.sort()

  # best possible moving ccw
  best = [None] * (N + 1)
  best_dist = [None] * (N + 1)
  best[0] = 0
  best_dist[0] = 0
  total_r = 0
  for i in range(N):
    x, v = sushi[i]
    total_r += v
    result = total_r - x
    best[i + 1] = best[i]
    best_dist[i + 1] = best_dist[i]
    if best[i] < result:
      best[i + 1] = result
      best_dist[i + 1] = x 

  # move cw trying to backtrack at every position
  total_l = 0
  solution = max(best)
  for i in range(N-1, 0, -1):
    x, v = sushi[i]
    xx = C - x
    total_l += v
    result = total_l - xx

    best_backtrack = result - xx + best[i]
    best_reverse_backtrack = result - best_dist[i] + best[i]
    solution = max(solution, best_backtrack, best_reverse_backtrack, result)

  return solution

if __name__ == '__main__':
  N, C = map(int, input().split())
  sushi = [list(map(int, input().split())) for _ in range(N)]
  print(solve(N, C, sushi))

Submission status: Wrong Answer

Solution code:
N, C = map(int, input().split(" "))
K = 0
X = 0
temp = 1000000000

for i in range(N):
    x, v = map(int, input().split(" "))
    if int(((x - X) % C/2)) > int(((temp - X) % C/2)):
        K += v - int(((temp - X) % C/2))
        X = x
    elif v - int(((x - X) % C/2)) >= 0:
        K += v - int(((x - X) % C/2))
        X = x
    elif v - int(((x - X) % C/2)) == 0:
        temp = x

print(K)

Submission status: Wrong Answer

Solution code:
n, c = [int(x) for x in input().split()]
xl = []
vl = []
for i in range(n):
  tmp = [int(x) for x in input().split()]
  xl.append(tmp[0])
  vl.append(tmp[1])
  
pos = 0
cal = 0
while len(xl) > 0:
  f = vl[0]/(xl[0] - pos)
  b = vl[-1]/(c - xl[-1] + pos)

  if f < 1 and b < 1:
    print(cal)
    exit()
  elif f >= b:
    cal = cal + vl[0] - (xl[0] - pos)
    pos = xl[0]
    del vl[0]
    del xl[0]
  elif f < b:
    cal = cal + vl[-1] - (c - xl[-1] + pos)
    pos = xl[-1]
    del vl[-1]
    del xl[-1]
print(cal)

Submission status: Wrong Answer

Solution code:
n, c = list(map(int, input().split()))
sushi1 = []
sushi2 = []
c1 = [0]
c2 = [0]
k1 = 0
k2 = 0
for i in range(n):
    x, v = list(map(int, input().split()))
    sushi1.append((x, v))
    sushi2.append((c-x, v))
sushi1.sort()
sushi2.sort()
for j in range(n):
    k1 += sushi1[j][1]
    k2 += sushi2[j][1]
    c1.append(k1-sushi1[j][0])
    c2.append(k2-sushi2[j][0])
print(max(c1+c2))

Submission status: Wrong Answer

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

x = []
v = []

for i in range(N):
    xi,vi = map(int,input().split())
    x.append(xi)
    v.append(vi)
    
kal = 0
x_sum = 0

for i in range(N):
    get_kal = v[i]-x[i]+x_sum
    if get_kal>0:
        kal += get_kal
        x_sum += x[i]
    else:
        break
        
print(kal)

Submission status: Wrong Answer

Solution code:
def inpl(): return list(map(int, input().split()))
N, C = inpl()
xv = []
for i in range(N):
    xv.append(inpl())
max_cal = 0
cal = 0
pos = 0
for i in xv:
    cal += i[1]-(i[0]-pos)
    pos = i[0]
    max_cal = max(max_cal, cal)
print(max_cal)

Submission status: Time Limit Exceeded

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

# x = 0 に v = 0 の寿司があると考える
X = [0]
V = [0]

for _ in range(N):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v)
# print(X)
# print(V)

# 0, v_1, v_1+v_2, v_1+v_2+v_3, ...
cumsum1 = [0]
for i in range(1, N + 1):
    cumsum1.append(cumsum1[-1] + V[i])

# 0, v_N, v_N+v_N-1, v_N+v_N-1+v_N-2, ...
cumsum2 = [0]
for i in range(N, 0, -1):
    cumsum2.append(cumsum2[-1] + V[i])

# print(cumsum1)
# print(cumsum2)

# 歩く範囲を弧ABと考える(A<=B)
# x_A = 0, x_1, x_2, ..., x_N
# x_B = x_A, ..., x_N
ans = 0

for i in range(N + 1):
    for j in range(i + 1, N + 1):
        # print(i, j)
        a = X[i]
        b = X[j]
        if i == 0:
            # 反時計回り(反転しない)
            ans = max(ans, cumsum1[j] - b)
            # 時計回り(反転しない)
            ans = max(ans, cumsum2[N + 1 - j] - (C - b))
        else:
            # O -> A -> B(反転)
            ans = max(ans, cumsum1[i] + cumsum2[N + 1 - j] - 2 * a - (C - b))
            # O -> B -> A(反転)
            ans = max(ans, cumsum1[i] + cumsum2[N + 1 - j] - 2 * (C - b) - a)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, C = map(int, input().split())
R = [[0, 0]]
L = [[0, 0]]
for i in range(N):
    x, v = map(int, input().split())
    R.append([x, v])
for i in R[::-1]:
    L.append([C - i[0], i[1]])
L.pop(-1)

R_sum = [0]
L_sum = [0]
R_sum2 = [0]
L_sum2 = [0]
total_1 = 0
total_2 = 0
total_3 = 0
total_4 = 0
for i in range(1, N + 1):
    total_1 += R[i][1] - (R[i][0] - R[i-1][0])
    total_2 += L[i][1] - (L[i][0] - L[i-1][0])
    total_3 += R[i][1] - 2*(R[i][0] - R[i-1][0])
    total_4 += L[i][1] - 2*(L[i][0] - L[i-1][0])
    R_sum.append(total_1)
    L_sum.append(total_2)
    R_sum2.append(total_3)
    L_sum2.append(total_4)
ans = 0
for i in range(N + 1):
    for j in range(N - i + 1):
        ans = max([ans, R_sum[i] + L_sum2[j], L_sum[i] + R_sum2[j]])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate

n, c = (int(x) for x in input().split())
X = []
V = []
D = []
prev_x = 0
for _ in range(n):
    x, v = (int(x) for x in input().split())
    X.append(x)
    D.append(x - prev_x)
    V.append(v)
    prev_x = x
D.append(c - X[-1])
rev_D = list(reversed(D))
rev_V = list(reversed(V))

ans = 0
# 時計回り
A = [0] * n
for i in range(n):
    A[i] = V[i] - D[i]
acc_A = list(accumulate(A))
ans = max(ans, max(acc_A))

# 反時計回り
B = [0] * n
for i in range(n):
    B[i] = rev_V[i] - rev_D[i]
acc_B = list(accumulate(B))
ans = max(ans, max(acc_B))

# 時計回りからの反時計回り i個まで食べて向きを変える
tmp = 0
for i in range(n - 1):
    tmp_cal = acc_A[i] - X[i]  # 食べて原点まで戻った時点のカロリー
    tmp = max(tmp, tmp_cal + max(acc_B[:-i - 1]))
ans = max(ans, tmp)

# 反時計回りからの時計回り
tmp = 0
for i in range(n - 1):
    tmp_cal = acc_B[i] - (c - X[-i - 1])  # 食べて原点まで戻った時点のカロリー
    tmp = max(tmp, tmp_cal + max(acc_A[:-i - 1]))
ans = max(ans, tmp)

print(ans)

Submission status: Time Limit Exceeded

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

p =[ab[0][1]-ab[0][0]]
for i in range(1,n):
    p.append(ab[i][1]-ab[i][0]+ab[i-1][0])
    
import itertools
psum = list(itertools.accumulate(p))
    
px = max(psum)

q =[ab[n-1][1]-(c-ab[n-1][0])]
for i in range(1,n):
    q.append(ab[n-1-i][1]-(ab[n-i][0]-ab[n-1-i][0]))

qsum = list(itertools.accumulate(q))
    
qx = max(qsum)

#p方向に行った戻ってq方向
rx=0
for i in range(n-1):
    r = psum[i]-ab[i][0]
    rr = max(qsum[:n-i-1])
    rx =max(rx,r+rr)
    
sx=0
for j in range(n-1):
    s = qsum[j]-(c-ab[n-j-1][0])
    ss = max(psum[:n-j-1])
    sx =max(sx,s+ss)
    
print(max(px,qx,rx,sx))

Submission status: Time Limit Exceeded

Solution code:
import itertools
N, C = map(int, input().split())
X = []
V = []
for n in range(N):
    x, v = map(int,input().split())
    X.append(x)
    V.append(v)
X_r = X[::-1]
V_r = V[::-1]
X = [0] + X
V = [0] + V
X_r = [0] + [C-x for x in X_r]
V_r = [0] + V_r

#cx = list(itertools.accumulate(X))
cv = list(itertools.accumulate(V))
#cxr = list(itertools.accumulate(X_r))
cvr = list(itertools.accumulate(V_r))

# print (X)
# print (X_r)
# print (cv)
# print (cvr)

# L = []
# for i in range(N+1):
#     L.append(cv[i]-X[i])
# R = []
# for i in range(N+1):
#     R.append(cvr[i]-X_r[i])

# print (L,R)
mx = 0
for l in range(N+1):
    m1 = -2*X[l]+cv[l]
    m2 = -2*X_r[l]+cvr[l]
    for r in range(N+1-l):
        #print (l,r)
        a = m1+cvr[r]-X_r[r]
        mx = max(mx,a)
        a = m2+cv[r]-X[r]
        mx = max(mx,a)
print (mx)

Submission status: Time Limit Exceeded

Solution code:
import itertools
N, C = map(int, input().split())
X = []
V = []
for n in range(N):
    x, v = map(int,input().split())
    X.append(x)
    V.append(v)
X_r = X[::-1]
V_r = V[::-1]
X = [0] + X
V = [0] + V
X_r = [0] + [C-x for x in X_r]
V_r = [0] + V_r

#cx = list(itertools.accumulate(X))
cv = list(itertools.accumulate(V))
#cxr = list(itertools.accumulate(X_r))
cvr = list(itertools.accumulate(V_r))

# print (X)
# print (X_r)
# print (cv)
# print (cvr)

# L = []
# for i in range(N+1):
#     L.append(cv[i]-X[i])
# R = []
# for i in range(N+1):
#     R.append(cvr[i]-X_r[i])

# print (L,R)
mx = 0
for l in range(N+1):
    for r in range(N+1-l):
        #print (l,r)
        a = -2*X[l]+cv[l]+cvr[r]-X_r[r]
        mx = max(mx,a)
        a = -2*X_r[l]+cv[r]+cvr[l]-X[r]
        mx = max(mx,a)
print (mx)

Submission status: Time Limit Exceeded

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

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

left = [0] * N
left2 = [0] * N
right = [0] * N
right2 = [0] * N

d = 0
d2 = 0
cal = 0
for i in range(N):
    x, v = sushi[i]
    d = x
    d2 = x * 2
    cal += v
    right[i] = cal - d
    right2[i] = cal - d2

d = 0
d2 = 0
cal = 0
for i in reversed(range(N)):
    x, v = sushi[i]
    d = C - x
    d2 = (C - x) * 2
    cal += v
    left[i] = cal - d
    left2[i] = cal - d2

ans = 0
# 左側を全部試す
for i in range(N):
    if i == 0:
        ans = max(ans, left[i])
    else:
        ans = max(ans, left[i] + max(max(right2[:i]), 0))
# 右側を全部試す
for i in range(N):
    if i == N - 1:
        ans = max(ans, right[i])
    else:
        ans = max(ans, right[i] + max(max(left2[i+1:]), 0))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, C = map(int, input().split())
X_V = [list(map(int, input().split())) for i in range(N)]
max_en = 0
point = [0] * (N+1)
point_back = [0] * (N+1)
point_rev = [0] * (N+1)
point_rev_back = [0] * (N+1)
for i, x_v in enumerate(X_V):
    # print(X_V[N-i-1])
    if i == 0:
        point[i+1] = x_v[1] - x_v[0]
        point_back[i+1] = x_v[1] - x_v[0]*2
        point_rev[i+1] = X_V[N-i-1][1] - (C-X_V[N-i-1][0])
        point_rev_back[i+1] = X_V[N-i-1][1] - (C-X_V[N-i-1][0])*2
    else:
        point[i+1] = point[i]+x_v[1] - (x_v[0]-X_V[i - 1][0])
        point_back[i+1] = point[i+1] - x_v[0]
        point_rev[i+1] = point_rev[i]+X_V[N-i-1][1] - (X_V[N-i][0]-X_V[N-i-1][0])
        point_rev_back[i+1] = point_rev[i+1] - (C-X_V[N-i-1][0])

# print(point)
# print(point_back)
# print(point_rev)
# print(point_rev_back)
for i in range(N+1):
    for j in range(N+1):
        if i + j > N:
            break
        else:
            if j == 0:
                max_en = max(max_en, point[i])
                # print(i, j, point[i])
            elif i == 0:
                max_en = max(max_en, point_rev[j])
                # print(i, j, point_rev[j])
            else:
                max_en = max(max_en, point_back[i] + point_rev[j], point[i] + point_rev_back[j])
                # print(i, j, point_back[i] + point_rev_back[j])

print(max_en)

Submission status: Time Limit Exceeded

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

xvlist=[]
xvlist_rev=[]
for i in range(N):
  x,v=map(int,input().split())
  xvlist.append((x,v))
  xvlist.append((x+C,v))
  xvlist_rev.append((C-x,v))
  xvlist_rev.append((2*C-x,v))
xvlist.sort()
xvlist_rev.sort()
#print(xvlist)

answer=0
#ccw->cw
for i in range(N+1):
  score=0
  pos=C
  for j in range(i):
    score+=xvlist[N-1-j][1]
    score-=pos-xvlist[N-1-j][0]
    pos=xvlist[N-1-j][0]
  #print(i,score)
  
  max_score=score
  for j in range(N,2*N-i):
    score+=xvlist[j][1]
    score-=xvlist[j][0]-pos
    max_score=max(max_score,score)
    pos=xvlist[j][0]    
  #print(i,max_score)
  answer=max(answer,max_score)

#print(xvlist_rev)  
#cw->ccw
for i in range(N+1):
  score=0
  pos=C
  for j in range(i):
    score+=xvlist_rev[N-1-j][1]
    score-=pos-xvlist_rev[N-1-j][0]
    pos=xvlist_rev[N-1-j][0]
  #print(i,score)
  
  max_score=score
  for j in range(N,2*N-i):
    score+=xvlist_rev[j][1]
    score-=xvlist_rev[j][0]-pos
    max_score=max(max_score,score)
    pos=xvlist_rev[j][0]    
  #print(i,max_score)
  answer=max(answer,max_score)
  
print(answer)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N,C = map(int,input().split())
XV = [list(map(int,input().split())) for i in range(N)]
x,v = map(list,zip(*XV))
tmp_f,tmp_l = 0,0
form,latt = [0],[0]
x = [0]+x+[C]
for i in range(N):
    tmp_f += v[i]
    tmp_l += v[-i-1]
    form.append(tmp_f)
    latt.append(tmp_l)
ans = 0
for i in range(N+1):
    for j in range(N+1-i):
        tmp = form[i]+latt[j]-(x[i]+C-x[-j-1]+min(x[i],C-x[-j-1]))
        ans = max(ans,tmp)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import*
n,c,*L=map(int,open(0).read().split())
acc=[a-x for x,a in zip(L[::2],accumulate(L[1::2]))]
R=L[::-1]
rev=[a-(c-x) for x,a in zip(R[1::2],accumulate(R[::2]))]
p=max(max([0]+rev[:n-i-1])+a-b for i,(a,b) in enumerate(zip(acc,L[::2])))
q=max(max([0]+acc[:n-i-1])+a-(c-b) for i,(a,b) in enumerate(zip(rev,R[1::2])))
r=max(0,max(acc),max(rev))
print(max(p,q,r))

Submission status: Time Limit Exceeded

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

X = [0]
V = [0]
sV = [0]
for _ in range(N):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v)
    sV.append(sV[-1]+v)
X.append(C)

ans = 0
for a in range(N+1):
    for b in range(a+1, N+2):
        # O -> A -> B
        a1 = sV[a]+sV[-1]-sV[b-1]-2*X[a]-(C-X[b])

        # O -> B -> A
        a2 = sV[a]+sV[-1]-sV[b-1]-X[a]-2*(C-X[b])

        ans = max(ans, a1, a2)

print(ans)

Submission status: Time Limit Exceeded

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

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

ans = 0
for a in range(N+1):
    for b in range(a+1, N+2):
        # O -> A -> B
        a1 = sum(V[:a+1])+sum(V[b:])-2*X[a]-(C-X[b])

        # O -> B -> A
        a2 = sum(V[:a+1])+sum(V[b:])-X[a]-2*(C-X[b])

        ans = max(ans, a1, a2)

print(ans)

Submission status: Time Limit Exceeded

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

#左右の折り返し毎に計算すると　2**(N-1) で地球爆発。
#折り返しは無駄なので1回のみにする
#∴折り返し点、N通りの計算のみでOK


#入力を読み込みつつ、時計回りの移動のカロリーを計算
xv_list = [(0,0)]
total_cal_list_cw = [0] #時計回りに進んだ時のカロリーリスト

for i in range(1,N+1):
    x,v = map(int, input().split())
    xv_list.append((x,v))
    total_cal_list_cw.append(total_cal_list_cw[i-1] + v - x + xv_list[i-1][0])


#半時計回り移動のカロリーを計算
total_cal_list_acw = [0]
rev_xv_list = [(0,0)] + [(C - xv[0],xv[1]) for xv in xv_list[:-(N+1):-1]] #xvを逆順にして、xは原点から反時計回り時の距離にする

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

#print("xv_list")
#print(xv_list)
#print("rev_xv_list")
#print(rev_xv_list)
#print("cw_cal_list")
#print(total_cal_list_cw)
#print("acw_cal_list")
#print(total_cal_list_acw)

ans = 0

for i in range(1,N+1):
    #print("i:",i)
    #print("###CLOCKWISE")
    #先に時計回りをする場合
    #print("cw:",total_cal_list_cw[i])
    #折り返さない場合
    if ans < total_cal_list_cw[i]:
        ans = total_cal_list_cw[i]
    #折り返す場合
    total_cal = total_cal_list_cw[i] - xv_list[i][0] #原点まで戻る
    #print("back to origin:",xv_list[i][0])
    #print("acw:",max(total_cal_list_acw[:N+1-i]))
    total_cal += max(total_cal_list_acw[:N+1-i]) #反時計回りで最大取れるところまで進む。ただし探索範囲は折り返し地点の前まで
    #print("total_cal",total_cal)
    
    if ans < total_cal:
        ans = total_cal
    
    #print("###ANTI CLOCKWISE")
    #先に反時計回りをする場合
    #print("acw:",total_cal_list_acw[i])
    #折り返さない場合
    if ans < total_cal_list_acw[i]:
        ans = total_cal_list_acw[i]
    #折り返す場合
    total_cal = total_cal_list_acw[i] - rev_xv_list[i][0] #原点まで戻る
    #print("back to origin:",rev_xv_list[i][0])
    #print("cw:",max(total_cal_list_cw[:N+1-i]))
    total_cal += max(total_cal_list_cw[:N+1-i]) #反時計回りで最大取れるところまで進む。ただし探索範囲は折り返し地点の前まで
    #print("total_cal",total_cal)
    
    if ans < total_cal:
        ans = total_cal
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline


def to_cumsum(arr):
    """
    1次元配列を累積和の1次元配列に変換する
    """
    cumsum = 0
    new = [0] * len(arr)
    for i, e in enumerate(arr):
        cumsum += e
        new[i] = cumsum
    return new


def main():
    N, C = map(int, input().split())
    X = [0] * (N + 1)
    X_inv = [0] * (N + 1)
    V = [0] * (N + 1)
    V_inv = [0] * (N + 1)
    for i in range(1, N + 1):
        x, v = map(int, input().split())
        X[i] = x
        X_inv[N + 1 - i] = C - x
        V[i] = v
        V_inv[N + 1 - i] = v
    V_sum = to_cumsum(V)
    V_inv_sum = to_cumsum(V_inv)

    ans = 0
    for i in range(N + 1):
        for j in range(N - i + 1):
            v = V_sum[i] + V_inv_sum[j]
            x1 = X[i] + 2 * X_inv[j]
            x2 = 2 * X[i] + X_inv[j]
            ans = max(ans, v - min(x1, x2))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N, C = map(int, input().split())
X = [0 for _ in range(N + 1)]
V = [0 for _ in range(N + 1)]
for i in range(1, N + 1):
    X[i], V[i] = map(int, input().split())
X = np.array(X)
V = np.array(V)

X_inv = np.hstack((np.zeros(1, dtype=int), (C - X[1:])[::-1]))
V_sum = V.cumsum()
V_inv_sum = np.hstack((np.zeros(1, dtype=int), V[1:][::-1])).cumsum()


def solve(i, j):
    v = V_sum[i] + V_inv_sum[j]
    x1 = X[i] + 2 * X_inv[j]
    x2 = 2 * X[i] + X_inv[j]
    return v - min(x1, x2)


ans = [solve(i, j) for i in range(N + 1) for j in range(N - i + 1)]
print(max(ans))