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=[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(1):
    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: Wrong Answer

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)
  else:
    count += (V[i] - (X[i] - X[i - 1]))
    right.append(count)
    right2.append(max(right2[-1],count))

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)
  else:
    count += (V[(i + 1) * (-1)] - ((X[i * (-1)] - X[((i + 1) * (-1))])))
    left.append(count)
    left2.append(max(left2[-1],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] + left2[n-i-1])
for i in range(n):
  if n - i - 1 >= 0:
    ans = max(ans, left[i] - (c - X[(i + 1) * (-1)]) + right2[n-i-1])
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()]

# 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_r] < c - x[i_l]:
        dis = x[i_r]
    else:
        dis = c - x[i_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 = 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)
  else:
    count += (V[i] - (X[i] - X[i - 1]))
    right.append(count)
    right2.append(max(right2[-1],count))

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)
  else:
    count += (V[(i + 1) * (-1)] - ((X[i * (-1)] - X[((i + 1) * (-1))])))
    left.append(count)
    left2.append(max(left2[-1],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] + left2[n-i-1])
for i in range(n):
  if n - i - 1 > 0:
    ans = max(ans, left[i] - (c - X[(i + 1) * (-1)]) + right2[n-i-1])
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
        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
        clock_max = self.clock_dp[np.argmax(self.clock_dp)]

        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

        counter_max = self.counter_clock_dp[np.argmax(self.counter_clock_dp)]

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


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

            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])]
            max_num = max(max_num, turn_left + clock_right)
        print(max(0, max_num))


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

solve()

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_r] < c - x[i_l]:
        dis = x[i_r]
    else:
        dis = c - x[i_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()))
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):
    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:
N, C = map(int, input().split())
sushi = []
for _ in range(N):
    x, y = map(int, input().split())
    rev_x = abs(C - x)
    cal = y - x
    reverse_cal = y - rev_x
    sushi.append(tuple([x, rev_x, y, cal, reverse_cal]))
sushi.sort(key=lambda x: x[0])
sushi_rev = sushi.copy()
sushi_rev.sort(key=lambda x: x[1])

current_pos = 0
sum_cal = 0
while True:
    min_m = float('inf')
    max_cal = 0
    can_idx = -1
    pos = 0
    for idx,s in enumerate(sushi):
        clock = True
        if s[0] - current_pos > s[1] + current_pos:
            clock = False
        m = min(s[0] - current_pos, s[1] + current_pos)
        c = s[2] - m
        if m < min_m and c >= 0:
            min_m = m
            can_idx = idx
            max_cal = c
            if clock:
                pos = s[0]
            else:
                pos = s[1]
    if can_idx == -1:
        break
    else:
        sum_cal += max_cal
        current_pos = pos
        sushi.pop(can_idx)
print(sum_cal)

Submission status: Wrong Answer

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

X = []
V = []
next_cand = []
for i in range(N):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v)
    next_cand.append({'left': (i-1)%N, 'right': (i+1)%N})

visited = [False]*N
calorie = 0
    
dist = [min(x, C - x) for x in X]
idx_cand = {'left': N-1, 'right': 0}
now_idx = -1

for _ in range(N):
    left = idx_cand['left']
    right = idx_cand['right']
    
    if V[left] - dist[left] > V[right] - dist[right]:
        direction = 'left'
    else:
        direction = 'right'
    
    next_idx = idx_cand[direction]
    
    if now_idx == -1:
        if direction == 'left':
             now_idx = N - 1
        else:
             now_idx = 0
    
    cal = V[next_idx] - dist[next_idx]
    
    if cal <= 0:
        break
    
    calorie += cal
    visited[next_idx] = True
    
    X = [x - dist[next_idx] for x in X]
    dist = [min(X[i], C - X[i]) if not visited[i] else None for i in range(N)]
    
    if direction == 'left':
        next_cand[next_idx]['right'] = next_cand[now_idx]['right']
    else:
        next_cand[next_idx]['left'] = next_cand[now_idx]['left']
    
    idx_cand = next_cand[next_idx]
    now_idx = next_idx
    
print(calorie)

Submission status: Wrong Answer

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

dis = []
cal = []
for n in range(N):
    x, v = map(int, input().split())
    dis.append(x)
    cal.append(v)

cal_sum = sum(cal)
cal_max = 0

for i in range(N):
    for j in range(i, N):
        eat_cal = sum(cal[:i]) + sum(cal[j+1:])
        left_dis = dis[i-1] if i > 0 else 0
        right_dis = C - dis[j+1] if j < N-1 else 0
        if i != 0 or j != N-1:
            walk_dis = min(left_dis, right_dis) * 2 + max(left_dis, right_dis)
            total = eat_cal - walk_dis
            cal_max = max(cal_max, total)

all_cal = sum(cal) - min(dis[N-1], C-dis[0])
cal_max = max(cal_max, all_cal)

print(cal_max)

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().strip()

n,c = na()
ke = [0]*n
for i in range(n):
    ke[i] = na()

ans = 0
spot = 0
gyaku = 0
kyori = 0
nodone = []
for i in range(len(ke)):
    gyaku = spot + (c-ke[i][0])
    if ke[i][0] > gyaku:
        if gyaku < ke[i][1]:
            for j in range(i+1,n):
                ans += ke[j][1]
            ans += ke[i][1]
            kyori += gyaku
            spot = ke[i][0]
            for k in reversed(nodone):
                if (spot-ke[k][0]) < ke[k][1]:
                    ans += ke[k][1]
                    kyori += spot-ke[k][0]
                    spot = ke[k][0]
            break
        else:
            continue
    else:
        if ke[i][0] < ke[i][1]:
            if len(nodone) > 0:
                for l in nodone:
                    ans += ke[l][1]
                ans += ke[i][1]
                kyori += ke[i][0] - spot
                nodone = []
            else:
                ans += ke[i][1]
                kyori += ke[i][0] - spot
            spot = ke[i][0]
        else:
            nodone.append(i)
print(ans-kyori)

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
if n > 100:
    print(0)
else:
    x = []
    v = []
    for i in range(n):
        a, b = map(int,input().split())
        x.append(a)
        v.append(b)

    sub = []
    sum = 0
    for i in range(n):
        sum += x[i]
        sub.append(sum)



    #折り返し0パターン
    ans = 0
    for i in range(n):
        fir = sub[i] - v[i]
        sec = (sum-sub[i]+x[i])-(c-v[i])
        ans = max(ans,max(fir,sec))

    #左に折り返し1パターン
    for i in range(n):
        arr = sub[i]-2*v[i]
        for k in range(i+1,n):
            ans = max(ans,arr-(c-v[k])+(sum-sub[k-1]))
            
            
    #右に折り返し1パターン

    for i in range(n-1,0,-1):
        brr = (sum-sub[i]+x[i])-2*(c-v[i])
        for k in range(0,i-1):
            ans = max(ans,brr+sub[k]-v[k])

    print(ans)

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
if n > 100:
    print(0)
else:
    x = []
    v = []
    for i in range(n):
        a, b = map(int,input().split())
        x.append(a)
        v.append(b)

    sub = []
    sum = 0
    for i in range(n):
        sum += x[i]
        sub.append(sum)



    #折り返し0パターン
    ans = -100000
    for i in range(n):
        fir = sub[i] - v[i]
        sec = (sum-sub[i]+x[i])-(c-v[i])
        ans = max(ans,max(fir,sec))

    #左に折り返し1パターン
    for i in range(n):
        arr = sub[i]-2*v[i]
        for k in range(i+1,n):
            ans = max(ans,arr-(c-v[k])+(sum-sub[i-1]))
            
            
    #右に折り返し1パターン

    for i in range(n-1,0,-1):
        brr = (sum-sub[i]+x[i])-2*(c-v[i])
        for k in range(0,i-1):
            ans = max(ans,brr+sub[k]-v[k])

    print(ans)

Submission status: Wrong Answer

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

# AtCoder Beginner Contest
# Problem D

if __name__ == '__main__':
    n, c = list(map(int, input().split()))
    x = []
    v = []

    for i in range(n):
        line = list(map(int, input().split()))
        x.append(line[0])
        v.append(line[1])

    consume_cal = 0
    clockwise_cal = 0

    for walking_cal, eat_cal in zip(x, v):
        if (eat_cal > walking_cal):
            if(eat_cal - walking_cal) > clockwise_cal:
                clockwise_cal += eat_cal
                consume_cal = walking_cal

    xr = x.copy()
    vr = v.copy()
    x_init = xr.pop(0)
    v_init = vr.pop(0)
    x_cur = x_init
    v_cur = v_init
    total_dist = x_init
    counterclockwise_cal = v_init

    for walking_cal, eat_cal in zip(reversed(xr), reversed(vr)):
        dist = min(walking_cal - x_cur, x_cur + (c - walking_cal))

        if (eat_cal > walking_cal):
            if(eat_cal - walking_cal) > counterclockwise_cal:
                counterclockwise_cal += eat_cal
                total_dist += dist
                x_cur = walking_cal

    print(max(clockwise_cal - consume_cal, counterclockwise_cal - total_dist))

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
if n > 100:
    print(0)
x = []
v = []
for i in range(n):
    a, b = map(int,input().split())
    x.append(a)
    v.append(b)

sub = []
sum = 0
for i in range(n):
    sum += x[i]
    sub.append(sum)



#折り返し0パターン
ans = -100000
for i in range(n):
    fir = sub[i] - v[i]
    sec = (sum-sub[i]+x[i])-(c-v[i])
    ans = max(ans,max(fir,sec))

#左に折り返し1パターン
for i in range(n):
    arr = sub[i]-2*v[i]
    for k in range(i+1,n):
        ans = max(ans,arr-(c-v[k])+(sum-sub[i-1]))
        
        
#右に折り返し1パターン

for i in range(n-1,0,-1):
    brr = (sum-sub[i]+x[i])-2*(c-v[i])
    for k in range(0,i-1):
        ans = max(ans,brr+sub[k]-v[k])

print(ans)

Submission status: Wrong Answer

Solution code:
n_num,c_meter = (int(i) for i in input().split())
x_met_and_cal = [[int(i) for i in input().split()] for i in range(n_num)] 

#pattern1:clockwise_end
max_clock = 0
cal_clock_en_route = 0
max_clock_num = 0
pre_x = 0
for i in range(n_num):
    cal_clock_en_route += x_met_and_cal[i][1] - (x_met_and_cal[i][0]-pre_x)
    pre_x = x_met_and_cal[i][0]
    if max_clock < cal_clock_en_route:
        max_clock = cal_clock_en_route
        max_clock_num = i

#pet2:counterclock
max_counterclock = 0
cal_counterclock_en_route = 0
max_counterclock_num = 0
pre_x = 0

for i in range(n_num):
    cal_counterclock_en_route += x_met_and_cal[n_num-i-1][1] - ((c_meter - x_met_and_cal[n_num-i-1][0])-pre_x)
    pre_x = (c_meter - x_met_and_cal[n_num-i-1][0])
    if max_counterclock < cal_counterclock_en_route:
        max_counterclock = cal_counterclock_en_route
        max_counterclock_num = i
                
#pet3:clock_counterclock
max_3 = 0
cal_3 = 0
pre_x = 0

for i in range(n_num):
    cal_3 += x_met_and_cal[i][1] - (x_met_and_cal[i][0]-pre_x)


#pet3:counterclock_clock

print(int(max(max_clock,max_clock)))

#print(list_meter_cal[0][1])

Submission status: Wrong Answer

Solution code:
from collections import deque

def main():
    N, C = map(int, input().split())
    T = tuple(tuple(map(int, input().split())) for _ in [0] * N)

    sushi = deque(T)
    cal = 0
    dist = 0
    for _ in range(N - 1):
        cal -= sushi[0][0] - dist
        cal += sushi[0][1]
        dist = sushi[0][0]
        sushi.rotate(-1)
    if cal - sushi[0][0] - dist + sushi[0][1] > cal:
        cal = cal - sushi[0][0] - dist + sushi[0][1]
    print(cal)

main()

Submission status: Wrong Answer

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 = 0
    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(new_rev_cost_sum_list)
        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, left[i])
    MM = max(MM, mm + rightleft[n-i-2])

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

Submission status: Wrong Answer

Solution code:
def solve():
    ans = 0
    for s in range(N):
        for e in range(s, s+N-1):
            if N<=e:
                tmp = sum(V[s:]) + sum(V[:e+1-N]) - (C-X[s]) - X[e-N] - min(C-X[s], X[e-N])
            else:
                tmp = sum(V[s:e+1]) - min(X[e], C-X[s])
            if ans<tmp:
                #print(s,e,tmp)
                ans = tmp
    return ans



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

print(solve())

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
x = []
v = []
for i in range(n):
    a, b = map(int,input().split())
    x.append(a)
    v.append(b)

sub = []
sum = 0
for i in range(n):
    sum += v[i]
    sub.append(sum)
    



#折り返し0パターン
ans = 0
for i in range(n):
    fir = sub[i] - x[i]
    sec = (sum-sub[i]+v[i])-(c-x[i])
    ans = max(ans,max(fir,sec))

#左に折り返し1パターン
for i in range(n):
    arr = sub[i]-2*x[i]
    for k in range(i+1,n):
        ans = max(ans,arr-(c-x[k])+(sum-sub[k-1]))

        
        
#右に折り返し1パターン

for i in range(n-1,0,-1):
    brr = (sum-sub[i]+v[i])-2*(c-x[i])
    for k in range(0,i-1):
        ans = max(ans,brr+sub[k]-x[k])


print(ans)

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

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

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

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

Submission status: Wrong Answer

Solution code:
from sys import stdin


def main():
    N, C = [int(x) for x in stdin.readline().rstrip().split()]
    xvi = [tuple(int(x) for x in stdin.readline().rstrip().split())
           for _ in range(N)]
    cal = 0
    now = 0
    while len(xvi):
        spam = [(i, min(abs(now - xv[0]), C - abs(now - xv[0])), xv[1]) for i, xv in enumerate(xvi)]
        spam = [(i, x, v) for i, x, v in spam if v - x > 0]
        if len(spam) == 0:
            break
        maxv = min(spam, key=lambda x: x[1])
        idx, x, v = maxv
        cal += v - x
        now += x
        if now >= C:
            now -= C
        del xvi[idx]
    print(cal)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
col0 = input('').split()
n = int(col0[0])
c = int(col0[1])

xi = []
vi = []
for i in range(0, n):
    coli = input('').split()
    xi.append(int(coli[0]))
    vi.append(int(coli[1]))


if n == 1:
    print(max(0, vi[0]-min(xi[0],c-xi[0])))

else:
    # only right
    v_1 = []
    for i in range(0, n):
        if len(v_1) == 0:
            v_1.append(vi[0]-xi[0])
        else:
            v_1.append(v_1[i-1]+vi[i]-(xi[i]-xi[i-1]))

    # only left
    v_2 = []
    for i in range(0,n):
        if len(v_2) == 0:
            v_2.append(vi[n-1]-(c-xi[n-1]))
        else:
            v_2.append(v_2[i-1]+vi[n-i-1]-((c-xi[n-i-1])-(c-xi[n-i])))

    # right -> left
    v_3 = []
    for i in range(0,n-1):
        for j in range(0,n-i-1):
            if j == 0:
                v_3.append(v_1[i]-xi[i]+vi[n-1]-(c-xi[n-1]))
            else:
                v_3.append(v_3[j-1]+vi[n-j-1]-((c-xi[n-j-1])-(c-xi[n-j])))

    # left -> right
    v_4 = []
    for i in range(0,n-1):
        for j in range(0,n-i-1):
            if j == 0:
                v_4.append(v_2[i]-(c-xi[n-i-1])+vi[0]-xi[0])
            else:
                v_4.append(v_4[j-1]+vi[j]-(xi[j]-xi[j-1]))

    print(max(max(v_1), max(v_2), max(v_3), max(v_4), 0))

Submission status: Wrong Answer

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

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


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

x=[0]+x
v=[0]+v
#時計回りにR個食べて、引き返してL個食べたときのカロリー消費を計算する


Cal=[0 for i in range((N+1)**2//2)]
ind=0

for R in range(N+1):
    for L in range(N-R):
        if R==0:
            CalR=0
        else:
            CalR=sum(v[:R+1])-x[R]
        
        if L==0:
            CalL=0
        else:
            CalL=sum(v[N-L+1:])-x[R]-(C-x[N-L+1])
        
        Cal[ind]=CalR+CalL
        ind=ind+1

print(max(Cal))

Submission status: Wrong Answer

Solution code:
N, C = map(int, input().split(" "))
K = 0
X = 0
temp = C * 2
pair = []

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

print(K)

Submission status: Wrong Answer

Solution code:
n,c=map(int,input().split())
c2=c/2
sushi=[]
for i in range(n):
  x,v=map(int,input().split())
  sushi.append([x,v])
res=0
ich=0
while True:
  max=-1145141919810
  xtmp=-1
  rev=1
  tmp1=0
  tmp2=0
  for i in range(len(sushi)):
    s=sushi[i]
    if s[0]>c2:break
    tmp2+=s[1]-(s[0]-tmp1)
    tmp1=s[0]
    if tmp2>max:
      max=tmp2
      xtmp=s[0]
      rev=1
  tmp1=c
  tmp2=0
  ntmp=len(sushi)
  for i in range(len(sushi)):
    ntmp-=1
    s=sushi[ntmp]
    if s[0]<=c2:break
    tmp2+=s[1]-(tmp1-s[0])
    tmp1=s[0]
    if tmp2>max:
      max=tmp2
      xtmp=s[0]
      rev=-1
  if max<0:
    break
  res+=max
  ich=xtmp
  if rev==1:
    for i in range(len(sushi)):
      s=sushi[i]
      if s[0]>ich:
        del sushi[0:i-1]
        break
  elif rev==-1:
    for i in range(len(sushi)):
      s=sushi[i]
      if s[0]>=ich:
        del sushi[i:]
        break
  sushi2=[]
  for s in sushi:
    if s[0]>ich:sushi2.append([s[0]-ich,s[1]])
  for s in sushi:
    if s[0]>ich:break
    sushi2.append([c+s[0]-ich,s[1]])
  if len(sushi2)<=0:break
  sushi=sushi2
  ich=0
print(res)

Submission status: Wrong Answer

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

dis = []
cal = []
for n in range(N):
    x, v = map(int, input().split())
    dis.append(x)
    cal.append(v)

cal_sum = sum(cal)
cal_max = 0

for i in range(N):
    for j in range(i, N):
        eat_cal = sum(cal[:i]) + sum(cal[j+1:])
        left_dis = dis[i-1] if i > 0 else 0
        right_dis = C - dis[j+1] if j < N-1 else 0
        walk_dis = min(left_dis, right_dis) * 2 + max(left_dis, right_dis)

        total = eat_cal - walk_dis
        cal_max = max(cal_max, total)

print(cal_max)