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:
N, C = list(map(int, input().split()))
len_cal_list = [list(map(int, input().split())) for i in range(N)]

total_cal = 0
f_r = [0]
for l, c in len_cal_list:
    total_cal += c
    f_r.append(total_cal - l)

total_cal = 0
f_l = [0]
for l, c in len_cal_list[::-1]:
    total_cal += c
    f_l.append(total_cal - (C - l))


max_cal = 0
for i in range(N):
	tmp_cal_r = f_r[i] + max(max(f_l[:N-i+1]) - len_cal_list[i-1][0], 0)
	tmp_cal_l = f_l[i] + max(max(f_r[:N-i+1]) - (C - len_cal_list[::-1][i-1][0]), 0)
	if max_cal <= max(tmp_cal_r, tmp_cal_l):
		max_cal = max(tmp_cal_r, tmp_cal_l)

print(max_cal)

Submission status: Wrong Answer

Solution code:
N, C = list(map(int, input().split()))
len_cal_list = [list(map(int, input().split())) for i in range(N)]

total_cal = 0
f_r = [0]
for l, c in len_cal_list:
    total_cal += c
    f_r.append(total_cal - l)

total_cal = 0
f_l = [0]
for l, c in len_cal_list[::-1]:
    total_cal += c
    f_l.append(total_cal - (C - l))


max_cal = 0
for i in range(N):
	tmp_cal_r = f_r[i] + max(max(f_l[:N-i+1]) - len_cal_list[i-1][0], 0)
	tmp_cal_l = f_l[i] + max(max(f_r[:N-i+1]) - (C - len_cal_list[-(i+1)][0]), 0)
	if max_cal <= max(tmp_cal_r, tmp_cal_l):
		max_cal = max(tmp_cal_r, tmp_cal_l)

print(max_cal)

Submission status: Wrong Answer

Solution code:
N, C = list(map(int, input().split()))
len_cal_list = [list(map(int, input().split())) for i in range(N)]

total_cal = 0
f_r = [0]
for l, c in len_cal_list:
    total_cal += c
    f_r.append(total_cal - l)

total_cal = 0
f_l = [0]
for l, c in len_cal_list[::-1]:
    total_cal += c
    f_l.append(total_cal - (C - l))


max_cal = 0
for i in range(N):
	tmp_cal_r = f_r[i] + max(max(f_l[:N-i+1]) - len_cal_list[i-1][0], 0)
	tmp_cal_l = f_l[i] + max(max(f_r[:N-i+1]) - (C - len_cal_list[-i][0]), 0)
	if max_cal <= max(tmp_cal_r, tmp_cal_l):
		max_cal = max(tmp_cal_r, tmp_cal_l)

print(max_cal)

Submission status: Wrong Answer

Solution code:
N, C = list(map(int, input().split()))
len_cal_list = [list(map(int, input().split())) for i in range(N)]

total_cal = 0
f_r = [0]
for l, c in len_cal_list:
    total_cal += c
    f_r.append(total_cal - l)

total_cal = 0
f_l = [0]
for l, c in len_cal_list[::-1]:
    total_cal += c
    f_l.append(total_cal - (C - l))


max_cal = 0
for i in range(N):
	tmp_cal = f_r[i] + max(max(f_l[:N-i+1]) - len_cal_list[i-1][0], 0)
	if max_cal <= tmp_cal:
		max_cal = tmp_cal


for i in range(N):
	tmp_cal = f_l[i] + max(max(f_r[:N-i+1]) - (C - len_cal_list[-i][0]), 0)
	if max_cal <= tmp_cal:
		max_cal = tmp_cal

print(max_cal)

Submission status: Wrong Answer

Solution code:
N, C = list(map(int, input().split()))
len_cal_list = [list(map(int, input().split())) for i in range(N)]

total_cal = 0
f_r = [0]
for l, c in len_cal_list:
    total_cal += c
    f_r.append(total_cal - l)

total_cal = 0
f_l = [0]
for l, c in len_cal_list[::-1]:
    total_cal += c
    f_l.append(total_cal - (C - l))


max_cal = 0
for i in range(1,N):
	tmp_cal = f_r[i] + max(max(f_l[:N-i+1]) - len_cal_list[i-1][0], 0)
	if max_cal <= tmp_cal:
		max_cal = tmp_cal


for i in range(1,N):
	tmp_cal = f_l[i] + max(max(f_r[:N-i+1]) - (C - len_cal_list[-i][0]), 0)
	if max_cal <= tmp_cal:
		max_cal = tmp_cal

print(max_cal)

Submission status: Wrong Answer

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

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

cw = 0
CW = []
#ccw = 0
#CCW = [0]
for i in range(N+1):
    cw += V[i]
    if i>0 and cw - X[i]<CW[-1][0]:
        CW.append(CW[-1])
    else:
        CW.append((cw - X[i], i))
    #ccw += V[-i-1]
    #CCW.append(ccw - X[-i-1])
#print(CW)


ans = 0
ccw = 0
for i in range(N+1):
    ccw += V[-i]

    maxCW,j = CW[N-i]#max(CW[:N-i+1])
    #j = CW.index(maxCW)
    #print(i, CW[:N-i+1], j)

    if i==0:
        score = maxCW
    elif j==0:
        score = ccw - (C-X[-i])# + maxCW - min(C-X[-i], X[j])
    else:
        score = ccw - (C-X[-i]) + maxCW - min(C-X[-i], X[j])
    ans = max(ans, score)
    #print(ans)

print(ans)

Submission status: Wrong Answer

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

cw = 0
CW = []
#ccw = 0
#CCW = [0]
for i in range(N+1):
    cw += V[i]
    if i>0 and cw - X[i]<CW[-1][0]:
        CW.append(CW[-1])
    else:
        CW.append((cw - X[i], i))
    #ccw += V[-i-1]
    #CCW.append(ccw - X[-i-1])
#print(CW)


ans = 0
ccw = 0
for i in range(N+1):
    ccw += V[-i]

    maxCW,j = CW[N-i]#max(CW[:N-i+1])
    #j = CW.index(maxCW)
    #print(i, CW[:N-i+1], j)

    if i==0:
        score = maxCW
    else:
        score = ccw - (C-X[-i]) + maxCW - min(C-X[-i], X[j])
    ans = max(ans, score)
    #print(ans)

print(ans)

Submission status: Wrong Answer

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

cw = 0
CW = []
#ccw = 0
#CCW = [0]
for i in range(N+1):
    cw += V[i]
    CW.append(cw - X[i])
    #ccw += V[-i-1]
    #CCW.append(ccw - X[-i-1])
#print(CW)


ans = 0
ccw = 0
for i in range(N+1):
    ccw += V[-i]

    maxCW = max(CW[:N-i+1])
    j = CW.index(maxCW)
    #print(i, CW[:N-i+1], j)

    if i==0:
        score = maxCW# - X[j]
    else:
        score = ccw - (C-X[-i]) + maxCW - min(C-X[-i], X[j])
    ans = max(ans, score)
    #print(ans)

print(ans)

Submission status: Wrong Answer

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

cw = 0
CW = []
#ccw = 0
#CCW = [0]
for i in range(N+1):
    cw += V[i]
    CW.append(cw - X[i])
    #ccw += V[-i-1]
    #CCW.append(ccw - X[-i-1])
#print(CW)


ans = 0
ccw = 0
for i in range(N+1):
    ccw += V[-i]

    maxCW = max(CW[:N-i+1])
    j = CW.index(maxCW)
    #print(i, CW[:N-i+1], j)

    if i==0:
        score = maxCW - X[j]
    else:
        score = ccw - (C-X[-i]) + maxCW - min(C-X[-i], X[j])
    ans = max(ans, score)
    #print(ans)

print(ans)

Submission status: Wrong Answer

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

cw = 0
CW = []
#ccw = 0
#CCW = [0]
for i in range(N+1):
    cw += V[i]
    CW.append(cw - X[i])
    #ccw += V[-i-1]
    #CCW.append(ccw - X[-i-1])
#print(CW)


ans = 0
ccw = 0
for i in range(N+1):
    ccw += V[-i]

    maxCW = max(CW[:N-i+1])
    j = CW.index(maxCW)
    #print(i, CW[:N-i+1], j)

    score = ccw - (C-X[-i]) + maxCW - min(C-X[-i], X[j])
    ans = max(ans, score)
    #print(ans)

print(ans)

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)] 
 
if n == 3:
  if x[2][1] == 1:
    print(191)
  else:
    print(192)
elif n == 1:
  print(0)
else:
  print(6500000000)

Submission status: Wrong Answer

Solution code:
times, circle = map(int, input().split())
sushi = []
calorie = []
clockwise = []
counterclockwise = []

temp, prev = 0, 0
for i in range(times):
    dist, ener = map(int, input().split())
    temp += ener
    sushi.append([dist, ener])
    clockwise.append(max(prev, temp-dist))
    prev = max(prev, temp-dist)
calorie.append(clockwise[times-1])

temp, prev = 0, 0
for ind_sushi in reversed(sushi):
    temp += ind_sushi[1]
    counterclockwise.insert(0, (max(prev, temp - (circle - ind_sushi[0]))))
    prev = max(prev, temp - (circle - ind_sushi[0]))
calorie.append(counterclockwise[times-1])

temp = 0
for i in range(times-1, 0, -1):
    temp += sushi[i][1]
    calorie.append(temp - (circle - sushi[i][0]) * 2 + clockwise[i-1])

temp = 0
for i in range(times-1):
    temp += sushi[i][1]
    calorie.append(temp - sushi[i][0] * 2 + counterclockwise[i + 1])

print(max(calorie))

Submission status: Wrong Answer

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

pre_mx = [(0,0)]
suf_mx = [(0,0)]
run_psum = 0
run_ssum = 0
data = []
for i in range(N):
    x,v = map(int,input().split())
    if run_psum + v - x > pre_mx[i][1]:
        pre_mx.append((x,run_psum + v - x))
    else:
        pre_mx.append((pre_mx[i][0],pre_mx[i][1]))
    run_psum += v
    data.append((x,v))

for i in range(N-1,-1,-1):
    if run_ssum + data[i][1] - C + data[i][0] > suf_mx[N-1-i][1]:
        suf_mx.append((C - data[i][0],run_ssum + data[i][1] - C + data[i][0]))
    else:
        suf_mx.append((suf_mx[N-1-i][0],suf_mx[N-1-i][1]))
    run_ssum += data[i][1]

ansp = anss = 0
for i in range(N+1):
    if ansp < pre_mx[i][1] + suf_mx[-i-1][1]:
        ansp = pre_mx[i][1] + suf_mx[-i-1][1]
        if suf_mx[-i-1][0] != 0:
            ansp -= pre_mx[i][0]

for i in range(N+1):
    if anss < suf_mx[i][1] + pre_mx[-i-1][1]:
        anss = suf_mx[i][1] + pre_mx[-i-1][1]
        if pre_mx[-i-1][0] != 0:
            anss -= suf_mx[i][0]

print(max(ansp,anss))

Submission status: Wrong Answer

Solution code:
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)

    clock_cum = []
    clock_max = []
    for i in range(N):
        if i == 0:
            cum_i = vs[i] - xs[i]
            max_i = max(0, cum_i)
        else:
            cum_i = clock_cum[-1] + xs[i - 1] + vs[i] - xs[i]
            max_i = max(clock_max[-1], cum_i)

        clock_cum.append(cum_i)
        clock_max.append(max_i)

    best = 0

    a_cum = 0
    for i in range(N):
        if i == 0:
            a_cum = vs[N - i - 1] - (C - xs[N - i - 1])
        else:
            a_cum += (C - xs[N - i - 1 + 1]) + vs[N - i - 1] - \
                     (C - xs[N - i - 1])

        if i != N - 1:
            best = max(best, a_cum,
                       a_cum - (C - xs[N - i - 1]) + clock_max[N - i - 2])
        else:
            best = max(best, a_cum)

    print(best)

Submission status: Wrong Answer

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

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

f_cost_list = [0]
b_cost_list = [0]
for i in range(1, N + 1):
    f_cost = f_cost_list[i-1] + v_list[-i] + (x_list[-i] - x_list[-(i+1)]) if i > 1 \
            else f_cost_list[i-1] + v_list[-i] + (C - x_list[-i]) 
    b_cost = b_cost_list[i-1] + v_list[i] - (x_list[i] - x_list[i-1])
    f_cost_list.append(f_cost)
    b_cost_list.append(b_cost)

print(max(f_cost_list + b_cost_list))

Submission status: Wrong Answer

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 = [0 for i in range(len(sushi_v))]
    su = 0L
    for i in range(len(sushi_v)):
        su += sushi_v[i] - abs(start - sushi_x[i])
        sum_list[i] = 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)
    

    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]

    max_cw = sorted_cost_sum_list[0]
    max_ccw = rev_cost_sum_list_idx[0]
    max_value = max(max_cw, max_ccw)

    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: Wrong Answer

Solution code:
def solve(xv_sorted):
    x = [False for _ in range(n)]
    y = [False for _ in range(n)]

    s = 0
    buf = (- 1) * float('inf')
    for i in range(n):
        s += xv_sorted[i][1]##cal 
        x[i] = max(buf, s - 2 * xv_sorted[i][0])##pos
        buf = x[i]

    print(x)

    s = 0
    buf = (- 1) * float('inf')
    for i in sorted(range(n), reverse=True):
        s += xv_sorted[i][1]##cal 
        y[i] = max(buf, s - (c - xv_sorted[i][0]))##pos
        buf = y[i]

    print(y)

    ans = max(x[n - 1], y[0])
    for i in range(n - 1):
        ans = max(ans, x[i] + y[i + 1])

    return ans

if __name__ == '__main__':
    n, c = map(int, input().split())
    xv = []
    for i in range(n):
        xv.append(list(map(int, input().split())))
    xv_sorted = sorted(xv, key=lambda x:x[0])
    ans = 0
    ans = max(ans, solve(xv_sorted))

    for i in range(n):
        xv_sorted[i][0] = abs(c - xv_sorted[i][0]) 
    xv_sorted = sorted(xv, key=lambda x:x[0])
    ans = max(ans, solve(xv_sorted))
    print(ans)

Submission status: Wrong Answer

Solution code:
n,c = list(map(int,input().split()))
a=[]
b=[]
a.append(0)
b.append(0)
for i in range(n):
    d = list(map(int,input().split()))
    a.append(d[0])
    b.append(d[1])

ans = 0
for i in range(n+1):
    d = sum(b[:i+1])-a[i]
    for j in range(n-i):
        e = sum(b[n-j:]) -c +a[-j-1]
        if a[i] >(c-a[-j-1]):
            f = d+e -c +a[-j-1]
        else:
            f = d+e-a[i]

        ans =max(ans,e,d,f)
print(ans)

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


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

f = [None for i in range(n + 1)]
mf = [None for i in range(n + 1)]

for r in range(n + 1):
    f[r] = sv_r[r] - x[r]

for r in range(1, n + 1):
    tmp = max(f[:r])
    mf[r] = f.index(tmp)

mf[0] = f.index(max(f))
# print(f)

max_ = 0
for l in range(n, -1, -1):
    r = mf[l]
    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(l, r, tmp, sv_l[l], sv_r[r])
    if tmp > max_:
        max_ = tmp
        
print(max_)

Submission status: Wrong Answer

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][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][0]   + sushi_rev_x[i], 0)
        #print value
        if value > max_value:
            max_value = value

    print max_value

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

mm = 0
MM = 0
for i in range(0, n-1):
    mm = max(mm, right[i])
    MM = max(MM, mm + leftright[n-i-2])

print(max(max(right), max(left), mm, MM, 0))

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])
    
max_ = max(total)

r_max = [None for i in range(n + 1)]
r_max[0] = 0
for i in range(1, n + 1):
    r_max[i] = [max(total[:i+1]), total.index(max(total[:i+1]))]
# print(r_max)

max_ = max(total)
for i_l in range(n, 1, -1):

    i_r = r_max[i_l - 1][1]
    
    if x[i_l] == 0:
        d_l = 0
    else:
        d_l = c - x[i_l]

    if x[i_r] < d_l:
        dis = x[i_r]
    else:
        dis = d_l

    tmp = total_left[i_l] + r_max[i_l - 1][0] - dis
    if tmp > max_:
        max_ = tmp
print(max_)

Submission status: Wrong Answer

Solution code:
n,c = list(map(int,input().split()))
a=[]
b=[]
for i in range(n):
    d = list(map(int,input().split()))
    a.append(d[0])
    b.append(d[1])

ans = 0
for i in range(n):
    d = sum(b[:i+1])-a[i]
    for j in range(n-i-1):
        e = sum(b[n-j-1:]) -c +a[-j-1]
        if a[i] >= (c-a[-j-1]):
            f = d+e-c+a[-j-1]
        else:
            f = d+e-a[i]
        ans = max(d,e,f,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np


def get_next_int():
    return int(float(input()))


def get_next_ints(delim=" "):
    return tuple([int(float(x)) for x in input().split(delim)])


def get_next_str():
    return input()


def get_next_strs(delim=" "):
    return tuple(input().split(delim))


def get_next_by_types(*value_types, delim=" "):
    return tuple([t(x) for t, x in zip(value_types, input().split(delim))])

class StopSushi(object):
    """docstring for StopSushi"""
    def __init__(self):
        self.N, self.C = get_next_ints()
        self.sushi = []
        half_point = int(self.C/2)
        over_half = False
        self.half_index = -1
        for i in range(self.N):
            sushi = get_next_ints()
            self.sushi.append(sushi)
            if not over_half and sushi[0] > half_point:
                self.half_index = i-1 if i > 0 else 0
                over_half = True
        pass

    def calc_dist(self, p1, p2):
        clockwise = abs(p2 - p1)
        counter_clock = p1 + abs(p2 - self.C)
        return min(clockwise, counter_clock)


    def solve(self):
        self.clock_dp = np.zeros(self.N, dtype=np.int)
        self.clock_dp[0] = self.sushi[0][1] - self.sushi[0][0]
        for i in range(1, self.N):
            dist = self.sushi[i][0] - self.sushi[i-1][0]
            self.clock_dp[i] = self.clock_dp[i-1] + self.sushi[i][1] - dist
        # print('clock', self.clock_dp)
        # print('clock_right', self.clock_dp[:self.half_index+1])
        clock_max = self.clock_dp[np.argmax(self.clock_dp)]
        # print(clock_max)

        self.counter_clock_dp = np.zeros(self.N, dtype=np.int)
        self.counter_clock_dp[self.N-1] = self.sushi[self.N-1][1] - abs(self.sushi[self.N-1][0] - self.C)
        for i in range(self.N - 1)[::-1]:
            # dist = self.sushi[i][0] - self.sushi[i+1][0]
            dist = self.sushi[i+1][0] - self.sushi[i][0]
            self.counter_clock_dp[i] = self.counter_clock_dp[i+1] + self.sushi[i][1] - dist

        # print('counter', self.counter_clock_dp)
        # print('counter_left', self.counter_clock_dp[self.half_index+1:])
        counter_max = self.counter_clock_dp[np.argmax(self.counter_clock_dp)]
        # print(counter_max)

        right_turn = np.zeros(self.N, dtype=np.int)
        for i in range(self.N):
            right_turn[i] = self.clock_dp[i] - self.sushi[i][0]

        left_turn = np.zeros(self.N, dtype=np.int)
        for i in range(self.N):
            left_turn[i] = self.counter_clock_dp[i] - (abs(self.sushi[i][0] - self.C))

        # print('right_turn', right_turn[:self.half_index+1])
        # print('left_turn', left_turn[self.half_index+1:])

        max_num = max(clock_max, counter_max)
        if self.half_index > 0 :
            turn_right = right_turn[np.argmax(right_turn[:self.half_index+1])]
            clock_left = self.counter_clock_dp[self.half_index+1 + np.argmax(self.counter_clock_dp[self.half_index+1:])]
            # print(turn_right, clock_left)
            max_num = max(max_num, turn_right + clock_left)

            turn_left = left_turn[self.half_index+1 + np.argmax(left_turn[self.half_index+1:])]
            clock_right = self.clock_dp[np.argmax(self.clock_dp[:self.half_index+1])]
            # print(turn_left , clock_right)
            max_num = max(max_num, turn_left + clock_right)

        print(max(0, max_num))
        # print()


def solve():
    s = StopSushi()
    s.solve()

solve()

Submission status: Wrong Answer

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

kcal=max(kcal, right_wa[n-1])



#print(left_wa)
#print(right_wa)

print(kcal)