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:
import sys
import numpy as np

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def calc_cost(start, sushi_v, sushi_x):
    sum_list = []
    su = 0L
    for i in range(len(sushi_v)):
        su += sushi_v[i] - abs(start - sushi_x[i])
        sum_list.append(su)
        start = sushi_x[i]
    return sum_list


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, C] = [int(input_raw[0]), long(input_raw[1])]
    sushi_v = []
    sushi_x = []
    sushi_rev_v = []
    sushi_rev_x = []
    for i in range(N):
        input_raw = read_func().strip().split()
        sushi_x.append(long(input_raw[0]))
        sushi_v.append(long(input_raw[1]))
    for i in range(N):
        sushi_rev_x.append( (sushi_x[N - i - 1]) - C)
        sushi_rev_v.append((sushi_v[N - i - 1]))
    
    cost_sum_list = calc_cost(0, sushi_v, sushi_x)
    rev_cost_sum_list = calc_cost(0, sushi_rev_v, sushi_rev_x)
    max_cw = max(cost_sum_list)
    max_ccw = max(rev_cost_sum_list)
    max_value = max(max_cw, max_ccw)

    cost_sum_list_idx = np.argsort(cost_sum_list, kind='mergesort')[::-1]
    sorted_cost_sum_list = np.sort(cost_sum_list,kind='mergesort')[::-1]
    rev_cost_sum_list_idx = np.argsort(rev_cost_sum_list,kind='mergesort')[::-1]
    sorted_rev_cost_sum_list = np.sort(rev_cost_sum_list,kind='mergesort')[::-1]

    for i in range(len(cost_sum_list)):
        start_pos = sushi_x[i]
        start_cost = cost_sum_list[i]
        
        value = 0
        if len(rev_cost_sum_list[:N - i - 1]) > 0:
            #print max(new_rev_cost_sum_list), max(rev_cost_sum_list[:N - i - 1]) - sushi_x[i]
            #value = start_cost  + max(max(rev_cost_sum_list[:N - i - 1]) - sushi_x[i], 0)
            value = start_cost  + max(sorted_rev_cost_sum_list[rev_cost_sum_list_idx < N - i - 1][0]  - sushi_x[i], 0)
        #print value
        if value > max_value:
            max_value = value

    for i in range(len(rev_cost_sum_list)):
        rev_start_pos = sushi_rev_x[i]
        rev_start_cost = rev_cost_sum_list[i]
        #new_cost_sum_list = calc_cost(rev_start_pos, sushi_v[:N - i - 1], sushi_x[:N - i - 1])
        
        value = 0
        if len(cost_sum_list[:N - i - 1]) > 0:
            #value = rev_start_cost  + max(max(cost_sum_list[:N - i - 1]) + sushi_rev_x[i], 0)
            value = rev_start_cost  + max(sorted_cost_sum_list[cost_sum_list_idx < N - i - 1][0]   + sushi_rev_x[i], 0)
        #print value
        if value > max_value:
            max_value = value

    print max_value

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N, C = list(map(int, input().split()))
X = np.zeros(N + 2, dtype = int)
V = np.zeros(N + 1, dtype = int)
sumVClockwiwse = np.zeros(N + 1, dtype = int)
#sumVAntiClockwiwse = np.zeros(N + 1, dtype = int)
X[N+1] = C

for n in range(1, N + 1):
    X[n], V[n] = list(map(int, input().split()))
    sumVClockwiwse[n] = sumVClockwiwse[n-1] + V[n]

sumVAntiClockwiwse = sumVClockwiwse[N] - sumVClockwiwse[::-1]

max = 0
for nA in range(N + 1):
    for nB in range(N - nA + 1):
        value = sumVClockwiwse[nA] + sumVAntiClockwiwse[nB]
        if max >= value:
            continue

        cost = min(X[nA] * 2 + (C - X[N - nB + 1]), X[nA] + (C - X[N - nB + 1]) * 2)
        value -= cost 
        if max >= value:
            continue
        # print(nA, nB, ":", cost)
        
        max = value


print(max)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def calc_cost(start, sushi_v, sushi_x):
    sum_list = []
    su = 0L
    for i in range(len(sushi_v)):
        su += sushi_v[i] - abs(start - sushi_x[i])
        sum_list.append(su)
        start = sushi_x[i]
    return sum_list


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, C] = [int(input_raw[0]), long(input_raw[1])]
    sushi_v = []
    sushi_x = []
    sushi_rev_v = []
    sushi_rev_x = []
    for i in range(N):
        input_raw = read_func().strip().split()
        sushi_x.append(long(input_raw[0]))
        sushi_v.append(long(input_raw[1]))
    for i in range(N):
        sushi_rev_x.append( (sushi_x[N - i - 1]) - C)
        sushi_rev_v.append((sushi_v[N - i - 1]))
    
    cost_sum_list = calc_cost(0, sushi_v, sushi_x)
    rev_cost_sum_list = calc_cost(0, sushi_rev_v, sushi_rev_x)
    max_cw = max(cost_sum_list)
    max_ccw = max(rev_cost_sum_list)
    max_value = max(max_cw, max_ccw)

    cost_sum_list_idx = np.argsort(cost_sum_list)[::-1]
    sorted_cost_sum_list = np.sort(cost_sum_list)[::-1]
    rev_cost_sum_list_idx = np.argsort(rev_cost_sum_list)[::-1]
    sorted_rev_cost_sum_list = np.sort(rev_cost_sum_list)[::-1]

    for i in range(len(cost_sum_list)):
        start_pos = sushi_x[i]
        start_cost = cost_sum_list[i]
        
        value = 0
        if len(rev_cost_sum_list[:N - i - 1]) > 0:
            #print max(new_rev_cost_sum_list), max(rev_cost_sum_list[:N - i - 1]) - sushi_x[i]
            #value = start_cost  + max(max(rev_cost_sum_list[:N - i - 1]) - sushi_x[i], 0)
            value = start_cost  + max(sorted_rev_cost_sum_list[rev_cost_sum_list_idx < N - i - 1][0]  - sushi_x[i], 0)
        #print value
        if value > max_value:
            max_value = value

    for i in range(len(rev_cost_sum_list)):
        rev_start_pos = sushi_rev_x[i]
        rev_start_cost = rev_cost_sum_list[i]
        #new_cost_sum_list = calc_cost(rev_start_pos, sushi_v[:N - i - 1], sushi_x[:N - i - 1])
        
        value = 0
        if len(cost_sum_list[:N - i - 1]) > 0:
            #value = rev_start_cost  + max(max(cost_sum_list[:N - i - 1]) + sushi_rev_x[i], 0)
            value = rev_start_cost  + max(sorted_cost_sum_list[cost_sum_list_idx < N - i - 1][0]   + sushi_rev_x[i], 0)
        #print value
        if value > max_value:
            max_value = value

    print max_value

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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


def d(a, b):
    r = x[a]
    l = c - x[b]
    if r < l:
        return r
    else:
        return l

sv_r = [None for i in range(n+1)]
sv_r[0] = 0
sv_r[1] = v[1]

sv_l = [None for i in range(n + 1)]
sv_l[0] = 0
sv_l[n] = v[n]
    
for i in range(1, n+1):
    sv_r[i] = sv_r[i - 1] + v[i]

for i in range(n - 1, 0, -1):
    sv_l[i] = sv_l[i + 1] + v[i]


# print(sv_r)
# print(sv_l)

max_ = 0

for r in range(n + 1):
    test = list(range(n, r, -1))
    test.append(0)
    for l in test:
        # print(l, r)
        if x[l] == 0:
            d_l = 0
        else:
            d_l = c - x[l]
        if d_l < x[r]:
            d = d_l * 2 + x[r]
        else:
            d = d_l + x[r] * 2
        # print(d)
        tmp = sv_l[l] + sv_r[r] - d
        # print(tmp, sv_l[l], sv_r[r])
        if tmp > max_:
            max_ = tmp
        
print(max_)

Submission status: Time Limit Exceeded

Solution code:
import sys

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def calc_cost(start, sushi_v, sushi_x):
    sum_list = []
    su = 0L
    for i in range(len(sushi_v)):
        su += sushi_v[i] - abs(start - sushi_x[i])
        sum_list.append(su)
        start = sushi_x[i]
    return sum_list


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, C] = [int(input_raw[0]), long(input_raw[1])]
    sushi_v = []
    sushi_x = []
    sushi_rev_v = []
    sushi_rev_x = []
    for i in range(N):
        input_raw = read_func().strip().split()
        sushi_x.append(long(input_raw[0]))
        sushi_v.append(long(input_raw[1]))
    for i in range(N):
        sushi_rev_x.append( (sushi_x[N - i - 1]) - C)
        sushi_rev_v.append((sushi_v[N - i - 1]))
    
    cost_sum_list = calc_cost(0, sushi_v, sushi_x)
    rev_cost_sum_list = calc_cost(0, sushi_rev_v, sushi_rev_x)
    max_value = max(max(cost_sum_list), max(rev_cost_sum_list))

    for i in range(len(cost_sum_list)):
        start_pos = sushi_x[i]
        start_cost = cost_sum_list[i]
        
        value = 0
        if len(rev_cost_sum_list[:N - i - 1]) > 0:
            #print max(new_rev_cost_sum_list), max(rev_cost_sum_list[:N - i - 1]) - sushi_x[i]
            value = start_cost  + max(max(rev_cost_sum_list[:N - i - 1]) - sushi_x[i], 0)
        #print value
        if value > max_value:
            max_value = value

    for i in range(len(rev_cost_sum_list)):
        rev_start_pos = sushi_rev_x[i]
        rev_start_cost = rev_cost_sum_list[i]
        #new_cost_sum_list = calc_cost(rev_start_pos, sushi_v[:N - i - 1], sushi_x[:N - i - 1])
        
        value = 0
        if len(cost_sum_list[:N - i - 1]) > 0:
            value = rev_start_cost  + max(max(cost_sum_list[:N - i - 1]) + sushi_rev_x[i], 0)
        #print value
        if value > max_value:
            max_value = value

    print max_value

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

# N,Cを取得
n, c = map(int, input().split())

# x1, v1..xN, vNを取得
xs = []
vs = []
for _i in range(n):
    x, v = map(int, input().split())
    xs.append(x)
    vs.append(v)

r1, r2, l1, l2 = [[], [], [], []]
r_sum, l_sum = [0, 0]
for i in range(n):
    r_sum += vs[i]
    r1.append(r_sum - xs[i])
    r2.append(r_sum - 2 * xs[i])

    i_rev = n - i - 1
    l_sum += vs[i_rev]
    l1.append(l_sum - (c - xs[i_rev]))
    l2.append(l_sum - 2 * (c - xs[i_rev]))

r1_l2_max = 0
l1_r2_max = 0
for i in range(n):
    r1_max = max(r1[:i + 1])
    l2_max = max(l2[:n - i - 1] or [0])
    r1_l2_max = max(r1_l2_max, r1_max + l2_max)

    l1_max = max(l1[:i + 1])
    r2_max = max(r2[:n - i - 1] or [0])
    l1_r2_max = max(l1_r2_max, l1_max + r2_max)

total_max = max(r1_l2_max, l1_r2_max)

# 結果を出力
print("{}".format(total_max))

Submission status: Time Limit Exceeded

Solution code:
n,c = map(int,input().split())
X = []
V = []
for i in range(n):
  x,v = map(int,input().split())
  X.append(x)
  V.append(v)
right = []
right2 = []
left = []
count = 0
left2 = []
for i in range(n):
  if i == 0:
    count += (V[i] - X[i])
    right.append(count)
    right2.append(count - X[i])
  else:
    count += (V[i] - (X[i] - X[i - 1]))
    right.append(count)
    right2.append(max(right2[-1],count - X[i]))

count = 0
for i in range(n):
  if i == 0:
    count += (V[(i + 1) * (-1)] - (c - X[(i + 1) * (-1)]))
    left.append(count)
    left2.append(count - (c - X[(i + 1) * (-1)]))
  else:
    count += (V[(i + 1) * (-1)] - ((X[i * (-1)] - X[((i + 1) * (-1))])))
    left.append(count)
    left2.append(max(left2[-1],count - (c - X[(i + 1) * (-1)])))
ans = max(0,max(right),max(left))
for i in range(n):
  if n - i - 1 > 0:
    ans = max(ans, right2[i] + max(left[0:n-i-1]))
for i in range(n):
  if n - i - 1 > 0:
    ans = max(ans, left2[i] + max(right[0:n-i-1]))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,c=[int(i) for i in input().split()]
x=[[0 for i in range(2)] for j in range(n)]
for i in range(n):
    x[i]= [int(i) for i in input().split()]
x.sort(key=lambda x:x[0])

state=0

left_wa=[0 for i in range(n)]
right_wa=[0 for i in range(n)]


for i in range(n):
    if i==0:
        left_wa[i]=x[i][1]-abs(x[i][0]-state)
    else:
        left_wa[i]=left_wa[i-1]+x[i][1]-abs(x[i][0]-state)
    state=x[i][0]

state=c

for i in range(n):
    if i==0:
        right_wa[i]=x[n-1-i][1]-abs(x[n-1-i][0]-state)
    else:
        right_wa[i]= right_wa[i-1]+x[n-1-i][1]-abs(abs(c-x[n-1-i][0])-abs(state))
    state=c-x[n-1-i][0]

kcal=0

for i in range(n):
    if i!=n-1:
        right=max(right_wa[:n-1-i])-x[i][0]
    else: right=0
    kcal=max(kcal, left_wa[i]+max(0, right))

for i in range(n):
    if i!=n-1:
        left=max(left_wa[:n-1-i])-abs(c-x[n-i-1][0])
    else: left=0
    kcal=max(kcal, right_wa[i]+max(0, left))



#print(left_wa)
#print(right_wa)

print(kcal)

Submission status: Time Limit Exceeded

Solution code:
import sys

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def calc_cost(start, sushi_v, sushi_x):
    sum_list = []
    su = 0L
    for i in range(len(sushi_v)):
        su += sushi_v[i] - abs(start - sushi_x[i])
        sum_list.append(su)
        start = sushi_x[i]
    return sum_list


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, C] = [int(input_raw[0]), long(input_raw[1])]
    sushi_v = []
    sushi_x = []
    sushi_rev_v = []
    sushi_rev_x = []
    for i in range(N):
        input_raw = read_func().strip().split()
        sushi_x.append(long(input_raw[0]))
        sushi_v.append(long(input_raw[1]))
    for i in range(N):
        sushi_rev_x.append( (sushi_x[N - i - 1]) - C)
        sushi_rev_v.append((sushi_v[N - i - 1]))
    
    cost_sum_list = calc_cost(0, sushi_v, sushi_x)
    rev_cost_sum_list = calc_cost(0, sushi_rev_v, sushi_rev_x)
    max_value = max(max(cost_sum_list), max(rev_cost_sum_list))

    for i in range(len(cost_sum_list)):
        start_pos = sushi_x[i]
        start_cost = cost_sum_list[i]
        new_rev_cost_sum_list = calc_cost(start_pos, sushi_rev_v[:N - i - 1], sushi_rev_x[:N - i - 1])
        value = 0
        if len(new_rev_cost_sum_list) > 0:
            value = start_cost  + max(max(new_rev_cost_sum_list), 0)
        #print value
        if value > max_value:
            max_value = value

    for i in range(len(rev_cost_sum_list)):
        rev_start_pos = sushi_rev_x[i]
        rev_start_cost = rev_cost_sum_list[i]
        new_cost_sum_list = calc_cost(rev_start_pos, sushi_v[:N - i - 1], sushi_x[:N - i - 1])
        value = 0
        if len(new_cost_sum_list) > 0:
            value = rev_start_cost  + max(max(new_cost_sum_list), 0)
        #print value
        if value > max_value:
            max_value = value

    print max_value

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n,c = map(int,input().split())
X = []
V = []
for i in range(n):
  x,v = map(int,input().split())
  X.append(x)
  V.append(v)
right = []
left = []
count = 0
for i in range(n):
  if i == 0:
    count += (V[i] - X[i])
  else:
    count += (V[i] - (X[i] - X[i - 1]))
  right.append(count)
count = 0
for i in range(n):
  if i == 0:
    count += (V[(i + 1) * (-1)] - (c - X[(i + 1) * (-1)]))
  else:
    count += (V[(i + 1) * (-1)] - ((X[i * (-1)] - X[((i + 1) * (-1))])))
  left.append(count)
ans = max(0,max(right),max(left))
for i in range(n):
  if n - i - 1 > 0:
    ans = max(ans, right[i] - X[i] + max(left[0:n-i-1]))
for i in range(n):
  if n - i - 1 > 0:
    ans = max(ans, left[i] - (c - X[(i+1) * (-1)]) + max(right[0:n-i-1]))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10000000)


def calc_cost(src_x, dst_x, C):
    """
    >>> calc_cost(9300000000, 400000000, 10000000000)
    1
    """

    src_x, dst_x = min(src_x, dst_x), max(src_x, dst_x)
    return min(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]

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

Submission status: Time Limit Exceeded

Solution code:
n, c = list(map(int, input().split()))
sushi1 = []
sushi2 = []
c1 = []
c2 = []
c3 = []
c4 = []
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])
for k in range(n-1):
    c3.append(c1[k]+max(c2[:n-k-1])-sushi1[k][0])
    c4.append(c2[k]+max(c1[:n-k-1])-sushi2[k][0])
print(max(c1+c2+c3+c4+[0]))

Submission status: Time Limit Exceeded

Solution code:
def solve(c, sushis):
    clockwise = solve_one_direction(c, sushis, True)
    counter = solve_one_direction(c, sushis[::-1], False)
    return max(clockwise, counter)

def solve_one_direction(c, sushis, clockwise=True, already_turn=False):
    total_v = 0
    max_v = 0
    for i, sushi in enumerate(sushis):
        total_v += sushi[1]
        x = sushi[0] if clockwise else c - sushi[0]
        v = total_v - x
        if max_v < v:
            max_v = v
        if not already_turn and x <= c / 2:
            rev = v - x + solve_one_direction(c, sushis[i + 1::][::-1], not clockwise, True)
            if max_v < rev:
                max_v = rev

    return max_v


n, c = map(int, input().split())
sushis = []
for _ in range(n):
    sushis.append([int(x) for x in input().split()])
print(solve(c, sushis))

Submission status: Time Limit Exceeded

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+1):
        sum = wa1[i] + wa2[k]
        sum -= min(list[i][0], c - list2[k][0])
        ma = max(sum,ma)
print(ma)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
lis = []
li = []
ans = []
for i in range(n):
    a,b = map(int,input().split())
    lis.append(b)
    li.append(a)
coli = sorted(li)
colis = []
for i in range(n):colis.append(lis[li.index(coli[i])])
for k in range(n+1):
    for h in range(n-k+2):
        if k + h < n + 1:
            cou = 0
            coucou = 0
            if k > 0:
                cou += sum(colis[:k])
                cou -= li[k-1]

            if h > 0:
                if k > 0:
                    cou -= li[k-1]
                cou += sum(colis[-h:])
                cou -= (m-li[-h])
            ans.append(cou)

            if k > 0:
                coucou += sum(colis[-k:])
                coucou -= (m-li[-k])
            if h > 0:
                if k > 0:
                    coucou -= (m-li[-k])
                coucou += sum(colis[:h])
                coucou -= li[h-1]
            ans.append(coucou)
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int,input().split())
lis = []
li = []
ans = []
for i in range(n):
    a,b = map(int,input().split())
    lis.append(b)
    li.append(a)
coli = sorted(li)
colis = []
for i in range(n):colis.append(lis[li.index(coli[i])])
for k in range(n+1):
    for h in range(n+1):
        if k + h < n + 1:
            cou = 0
            coucou = 0
            if k > 0:
                cou += sum(colis[:k])
                cou -= li[k-1]

            if h > 0:
                if k > 0:
                    cou -= li[k-1]
                cou += sum(colis[-h:])
                cou -= (m-li[-h])
            ans.append(cou)

            if k > 0:
                coucou += sum(colis[-k:])
                coucou -= (m-li[-k])
            if h > 0:
                if k > 0:
                    coucou -= (m-li[-k])
                coucou += sum(colis[:h])
                coucou -= li[h-1]
            ans.append(coucou)
print(max(ans))

Submission status: Time Limit Exceeded

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

for i in range(n):
    y, u = map(int, input().split())
    x.append(y)
    v.append(u)

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

a2 = [0] * n
b2 = [0] * n

for i in range(n):
    if i != n-1 and max(b[:-i-1]) - x[i] > 0:
        a2[i] = a[i] + max(b[:-i-1]) - x[i]
    else:
        a2[i] = a[i]
    if i != n-1 and max(a[:-i-1]) - (c - x[n-1-i]) > 0:
        b2[i] = b[i] + max(a[:-i-1]) - (c - x[n-1-i])
    else:
        b2[i] = b[i]

a2.extend(b2)
res = max(a2)

print(max([0, res]))

Submission status: Time Limit Exceeded

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

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

Solution code:
input_conditions = input().split()
times = int(input_conditions[0])
circle = int(input_conditions[1])
sushi_list = []
c_list = []
c_list_reverse = []
all_list = []

for i in range(times):
    sushi_condition = input().split()
    temp = [int(sushi_condition[0]), int(sushi_condition[1])]
    sushi_list.append(temp)

temp_total = 0
for i in range(times):
    temp_total += sushi_list[i][1]
    c_list.append(temp_total - sushi_list[i][0])

temp_total = 0
for i in range(times):
    temp_total += sushi_list[times-i-1][1]
    c_list_reverse.append(temp_total + sushi_list[times-i-1][0] - circle)

for i in range(times-1):
    higher = c_list[i] + max(c_list_reverse[:times-i-1]) - sushi_list[i][0]
    all_list.append(higher)

for i in range(times-1):
    higher = c_list_reverse[i] + max(c_list[:times-i-1]) - abs(circle - sushi_list[times-i-1][0])
    all_list.append(higher)

c_list.append(0)
c_list_reverse.append(0)
all_list.append(0)

print(max(0, max(c_list), max(c_list_reverse), max(all_list)))

Submission status: Time Limit Exceeded

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

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

rl = []
for i, v in enumerate(rightleft):
    if n-i > 1:
        rl.append(max(left[:n-i-1]) + v)

lr = []
for i, v in enumerate(leftright):
    if n-i > 1:
        lr.append(max(right[:n-i-1]) + v)

result = max(max(right), max(left), 0)
if len(rl) > 0:
    result = max(result, max(rl))
if len(lr) > 0:
    result = max(result, max(lr))
print(result)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, C = mapint()

Xs = []
Vs = []
for _ in range(N):
    x, v = mapint()
    Xs.append(x)
    Vs.append(v)
r_Xs = [C-x for x in Xs[::-1]]
r_Vs = Vs[::-1]

max_neg = []
tmp = 0
maxi = -10**18
for i in range(N):
    x, v = r_Xs[i], r_Vs[i]
    tmp += v
    max_neg.append(max(tmp-x, maxi))
    maxi = max(tmp-x, maxi)

max_pos = []
tmp = 0
maxi = -10**18
for i in range(N):
    x, v = Xs[i], Vs[i]
    tmp += v
    max_pos.append(max(tmp-x, maxi))
    maxi = max(tmp-x, maxi)
ans = 0
for i in range(N):
    x, v = Xs[i], Vs[i]
    if i!=N-1:
        ans = max(ans, max_pos[i], max_pos[i]-x+max_neg[N-i-2])
    else:
        ans = max(ans, max_pos[i])
for i in range(N):
    x, v = r_Xs[i], r_Vs[i]
    if i!=N-1:
        ans = max(ans, max_neg[i], max_neg[i]-x+max_pos[N-i-2])
    else:
        ans = max(ans, max_neg[i])
print(ans)

Submission status: Accepted

Solution code:
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
ips = lambda:input().split()
import collections
import math
import itertools
import heapq as hq
import sys
n,c = ma()
INF=10**18
xv = []
for i in range(n):
    xv.append(lma())
xvr=xv[::-1]

ans=0
v1=0
v2=0
cost1=[0]*(n+1) #cost[i] ::iまで来た時の最大gain(戻る分を含む)
cost2=[0]*(n+1) #反時計回り
for i in range(n):
    v1 += xv[i][1]
    cost1[i]=max(v1-2*xv[i][0],cost1[i-1])
    v2+=xvr[i][1]
    cost2[i]=max(v2-2*(c-xvr[i][0]),cost2[i-1])
tmp=0
# print(cost1,cost2)
for i in range(n):
    tmp+=xv[i][1]
    #print(tmp,cost2[n-i-1])
    ans = max(ans, cost2[n-i-2] +tmp-xv[i][0] )
tmp=0
for i in range(n):
    tmp+=xvr[i][1]
    #print(tmp,cost1[n-i-1])
    ans = max(ans, cost1[n-i-2] +tmp-(c-xvr[i][0]) )

print(ans)