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: Runtime Error

Solution code:
n, c = map(int, input().split())
ss = []
adj = 0
if c % 2 == 0:
    c += 1
    adj = 1
c_div_2 = c / 2
cnt_l = 0
for i in range(n):
    x, v = map(int, input().split())
    ss.append((x,v))
    if x < c_div_2:
        cnt_l += 1
cnt_r = 0
ss_rev = ss[::-1]
for i in ss_rev:
    x, v = i
    if c-adj-x < c_div_2:
        cnt_r += 1
    else:
        break

res = -c
for s in ss:
    res += s[1]
res = max(res,0)
rsw_l = [0]*(cnt_l)
if cnt_l >= 1:
    rsw_l[0] = ss[0][1]
if cnt_l >= 2:
    for i in range(1,cnt_l):
        rsw_l[i] = rsw_l[i-1] + ss[i][1]
rsw_l_opp = rsw_l.copy()
if cnt_l >= 1:
    rsw_l[0] -= ss[0][0]
    rsw_l_opp[0] -= ss[0][0]*2
if cnt_l >= 2:
    for i in range(1,cnt_l):
        rsw_l[i] -= ss[i][0]
        rsw_l_opp[i] -= ss[i][0]*2
rsw_r = [0]*(cnt_r)
if cnt_r >= 1:
    rsw_r[0] = ss_rev[0][1]
if cnt_r >= 2:
    for i in range(1,cnt_r):
        rsw_r[i] = rsw_r[i-1] + ss_rev[i][1]
rsw_r_opp = rsw_r.copy()
if cnt_r >= 1:
    rsw_r[0] -= c-adj-ss_rev[0][0]
    rsw_r_opp[0] -= (c-adj-ss_rev[0][0])*2
if cnt_r >= 2:
    for i in range(1,cnt_r):
        rsw_r[i] -= c-adj-ss_rev[i][0]
        rsw_r_opp[i] -= (c-adj-ss_rev[i][0])*2
res = max(res,max(rsw_l)+max([0]+rsw_r_opp))
res = max(res,max(rsw_r)+max([0]+rsw_l_opp))
print(res)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**9)
Input = open('input.txt')
def input(): return Input.readline()
def mapint(): return map(int, input().split())

N, C = mapint()
xv = [list(mapint()) for _ in range(N)]
righthand = [0]*N
lefthand = [0]*N
nowv = 0
maxv = -10**18
for i, (x, v) in enumerate(xv):
    nowv += v
    maxv = max(maxv, nowv-x)
    righthand[i] = maxv
nowv = 0
maxv = -10**18
for i, (x, v) in enumerate(xv[::-1]):
    nowv += v
    maxv = max(maxv, nowv-(C-x))
    lefthand[i] = maxv

xv = [(0, 0)] + xv +[(C, 0)]
righthand = [0]+righthand
lefthand = [0]+lefthand
rmax = 0
for i in range(N+1):
    rmax = max(rmax, righthand[i]-xv[i][0]+lefthand[N-i])
lmax = 0
for i in range(N):
    lmax = max(lmax, lefthand[i]-(C-xv[-i-1][0])+righthand[N-i])
print(rmax, lmax)

Submission status: Runtime Error

Solution code:
from itertools import accumulate
n,c=map(int,input().split())
sushi=[list(map(int,input().split())) for i in range(n)]

r=[sushi[0][1]-sushi[0][0]]
for i in range(1,n):
    r.append(r[-1]+sushi[i][1]-(sushi[i][0]-sushi[i-1][0]))
l=[sushi[n-1][1]-(c-sushi[n-1][0])]
for i in range(n-2,-1,-1):
    l.append(l[-1]+sushi[i][1]-((c-sushi[i][0])-(c-sushi[i+1][0])))
r2=[sushi[0][1]-2*sushi[0][0]]
for i in range(1,n):
    r2.append(r2[-1]+sushi[i][1]-2*(sushi[i][0]-sushi[i-1][0]))
l2=[sushi[n-1][1]-2*(c-sushi[n-1][0])]
for i in range(n-2,-1,-1):
    l2.append(l2[-1]+sushi[i][1]-2*((c-sushi[i][0])-(c-sushi[i+1][0])))
r=list(accumulate(r,max))
l=list(accumulate(l,max))

cand=[l[-1],r[-1]]
for i in range(n):
    if 0<=n-i-2<=n-1:
        cand.append(l2[i]+r[n-i-2])
        cand.append(r2[i]+l[n-i-2])

print(max(max(cand),0))

Submission status: Runtime Error

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, c = list(map(int, readline().split()))

    x = [0] * (n + 1)
    v = [0] * (n + 1)

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

    right_cum = [0] * (n + 1)
    left_cum = [0] * (n + 1)

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

    right_nutritive = [0] * (n + 1)
    left_nutritive = [0] * (n + 1)
    right_max_cum = [0] * (n + 1)
    left_max_cum = [0] * (n + 1)

    for i in range(1, n + 1):
        right_nutritive[i] = right_cum[i] - x[i]
        left_nutritive[i] = left_cum[i] - (c - x[n - i + 1])
        right_max_cum[i] = max(right_max_cum[i - 1], right_nutritive[i])
        left_max_cum[i] = max(left_max_cum[i - 1], left_nutritive[i])

    ans = max(*right_nutritive, *left_nutritive)

    for i in range(1, n + 1):
        rl = right_nutritive[i] - x[i] + left_max_cum[n - i]
        lr = left_nutritive[i] - (c - x[n - i + 1]) + right_max_cum[n - i]
        ans = max(ans, rl, lr)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,C=MI()
    XV=LLIN(N)
    DP1 = [0]*(N+1)
    DP2 = [0]*(N+1)
    before = 0
    for i,(x,v) in enumerate(XV):
        DP1[i+1]=DP1[i]+v-(abs(x-before))
        before = x
    before = C
    for i,(x,v) in enumerate(XV[::-1]):
        DP2[i+1]=DP2[i]+v-(abs(x-before))
        before = x
    ans = max(*DP1,*DP2)
    now = 0

    DP1_ = [d-XV[i][0] for i,d in enumerate(DP1[1:])]
    DP2_ = [d-(C-XV[~i][0]) for i,d in enumerate(DP2[1:])]
    for i in range(N-1):
        if(DP2_[i+1]<DP2_[i]):
            DP2_[i+1] = DP2_[i]
        if(DP1_[i+1]<DP1_[i]):
            DP1_[i+1] = DP1_[i]
    now1 = 0
    now2 = 0
    for i in range(N):
        idx = N-1-i
        # print(DP1[i],DP2_[idx])
        # print(DP2[i],DP1_[idx])
        ans = max(ans,DP1[i]+DP2_[idx])
        ans = max(ans,DP2[idx]+DP1_[i])
        # print(ans)
    # print(DP1)
    # print(DP2)
    # print(DP1_)
    # print(DP2_)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from itertools import accumulate
n,c = map(int,input().split())
wv = [[0,0]]+[list(map(int,input().split())) for i in range(n)]+[[c,0]]
n += 2
ansls = []
for i in range(n):
  if wv[i][0] > c//2:
    spr = i
    break
for sp in spr-1,spr:
  ls = [0]*n
  lsx = [0]*n
  for i in range(1,sp):
    w,v = wv[i]
    w -= wv[i-1][0]
    ls[i] = v-w*2
    lsx[i] = v-w
  for i in range(n-2,sp-1,-1):
    w,v = wv[i]
    w -= wv[i+1][0]
    ls[i] = v+w*2
    lsx[i] = v+w
  alsf = list(accumulate(ls[:sp]))
  alsl = list(accumulate(ls[sp:][::-1]))
  alsxf = list(accumulate(lsx[:sp]))
  alsxl = list(accumulate(lsx[sp:][::-1]))
  ansls.append(max(alsf)+max(alsxl))
  ansls.append(max(alsl)+max(alsxf))
print(max(ansls))

Submission status: Runtime Error

Solution code:
import sys
from bisect import bisect_left


N, C = map(int, input().split())
S = [tuple(map(int, s.split())) for s in sys.stdin.readlines()]
T = [x for x, v in S]

# 時計回り
score, pos, ans1 = 0, 0, 0
for x, v in S:
    score += v - (x - pos)
    ans1 = max(ans1, score)
    pos = x

# 反時計回り
score, pos, ans2 = 0, 0, 0
for x, v in reversed(S):
    x = C - x
    score += v - (x - pos)
    ans2 = max(ans2, score)
    pos = x

mid = bisect_left(T, C / 2)

# 時計回り 半周
score, pos, r = 0, 0, 0
for i in range(mid):
    x, v = S[i]
    score += v - (x - pos) * 2
    r = max(r, score)
    pos = x
score, pos, l = 0, 0, 0
for i in range(N - mid):
    x, v = S[N - 1 - i]
    x = C - x
    score += v - (x - pos)
    l = max(l, score)
    pos = x
ans3 = r + l

# 反時計回り 半周
score, pos, l = 0, 0, 0
for i in range(N - mid):
    x, v = S[N - 1 - i]
    x = C - x
    score += v - (x - pos) * 2
    l = max(l, score)
    pos = x
score, pos, r = 0, 0, 0
for i in range(mid):
    x, v = S[i]
    score += v - (x - pos)
    r = max(r, score)
    pos = x
ans4 = r + l

# 時計回り 半周 + 1
mid += 1
score, pos, r = 0, 0, 0
for i in range(mid):
    x, v = S[i]
    score += v - (x - pos) * 2
    r = max(r, score)
    pos = x
score, pos, l = 0, 0, 0
for i in range(N - mid):
    x, v = S[N - 1 - i]
    x = C - x
    score += v - (x - pos)
    l = max(l, score)
    pos = x
ans5 = r + l

# 反時計回り 半周
score, pos, l = 0, 0, 0
for i in range(N - mid):
    x, v = S[N - 1 - i]
    x = C - x
    score += v - (x - pos) * 2
    l = max(l, score)
    pos = x
score, pos, r = 0, 0, 0
for i in range(mid):
    x, v = S[i]
    score += v - (x - pos)
    r = max(r, score)
    pos = x
ans6 = r + l


print(max([ans1, ans2, ans3, ans4, ans5, ans6]))

Submission status: Runtime Error

Solution code:
def main():
    n, c = map(int, input().split())
    sushi = [None]*n
    for i in range(n):
        sushi[i] = list(map(int, input().split()))
    cal_r = [None] * n
    cal_l = [None] * n
    max_r = [None] * n
    max_l = [None] * n
    cal_r[0] = sushi[0][1] - sushi[0][0]
    max_r[0] = cal_r[0]
    for i in range(1, n):
        cal_r[i] = cal_r[i-1] + sushi[i][1] - (sushi[i][0] - sushi[i-1][0])
        max_r[i] = max(max_r[i-1], cal_r[i])
    cal_l[n-1] = sushi[n-1][1] - (c - sushi[n-1][0])
    max_l[n-1] = cal_l[n-1]
    for i in reversed(range(n-1)):
        cal_l[i] = cal_l[i+1] + sushi[i][1] - (sushi[i+1][0] - sushi[i][0])
        max_l[i] = max(max_l[i+1], cal_l[i])
    ans = max(0, *max_r, *max_l)
    cal_r_2 = [None] * n
    cal_l_2 = [None] * n
    cal_r_2[n-1] = 0
    cal_l_2[0] = 0
    for i in range(n-1):
        cal_r_2[i] = cal_r[i] - sushi[i][0] + max_l[i+1]
    for i in reversed(range(1, n)):
        cal_l_2[i] = cal_l[i] - (c - sushi[i][0]) + max_r[i-1]
    ans = max(ans, *cal_r_2, *cal_l_2)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

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

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

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

        ans = max(ans, a1, a2)

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
#import sympy as syp(素因数分解とか)
Mod = 1000000007
import sys

sys.setrecursionlimit(100000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

def factorial(i):
    if i == 1:
        return 1
    else:
        return i*factorial(i-1)
 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)




def main(): #startline--------------------------------------------
    N,C = map(int,input().split())
    xv = [tuple(map(int,input().split())) for i in range(N)]
    r1,r2,l1,l2= [0]*(N+1),[0]*(N+1),[0]*(N+1),[0]*(N+1)
    ans = 0
    dis = 0
    for i in range(N):
        r1[i+1] = r1[i]+xv[i][1]-(xv[i][0]-dis)
        r2[i+1] = r2[i]+xv[i][1]-2*(xv[i][0]-dis)
        dis = xv[i][0]
    dis = C
    for i in reversed(range(N)):
        l1[N-i+1]=l1[N-i]+xv[i][1]-(xv[i][0]-dis)
        l2[N-i+1]=l2[N-i]+xv[i][1]-2*(xv[i][0]-dis)
        dis = C-xv[i][0]
    for i in range(N):
        l1[i+1] = max(l1[i+1],l1[i])
        l2[i+1] = max(r1[i+1],r1[i])
    for i in range(N):
        subans = max(r2[i]+l1[N-i],l2[i]+r1[N-i])
        ans = max(ans,subans)
    print(ans)
if __name__ == "__main__":
    main() #endline===============================================

Submission status: Runtime Error

Solution code:
n,c=map(int,input().split())
xv = list(list(map(int, input().split())) for i in range(n))
vx=xy[::-1]
sushi1, max1, sushi2, max2 = [0], [0], [0], [0]
import numpy as np

sum1,sum2=0,0
for x,v in xv:
        sum1+=v
        sushi1.append(max(sum1 - x, sushi1[-1]))
        max1.append(max(sum1 - 2*x, max1[-1]))
for x, v in vx:
    sum2 += v
    sushi2.append(max(sum2 - (c-x), sushi2[-1]))
    max2.append(max(sum2 - 2*(c-x), max2[-1]))
ans1 = max(np.array(sushi1) + np.array(max2[::-1]))
ans2 = max(np.array(sushi2) + np.array(max1[::-1]))
print(max(0, ans1, ans2))

Submission status: Runtime Error

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

n, c = map(int, input().split())
sushi = [tuple(map(int, input().split())) for _ in range(n)]

ans = 0

cand1 = [0] * n
v_sum = 0
for i in range(n):
    v_sum += sushi[i][1]
    cand1[i] = v_sum - sushi[i][0]
    if i > 0:
        cand1[i] = max(cand1[i], cand1[i-1])

cand2 = [0] * n
v_sum = 0
for i in reversed(range(n)):
    v_sum += sushi[i][1]
    cand2[i] = v_sum - (c - sushi[i][0])
    if i != n - 1:
        cand2[i] = max(cand2[i], cand2[i+1])

ans = max(ans, *cand1, *cand2)

v_sum = 0
for i in reversed(range(1, n)):
    v_sum += sushi[i][1]
    ans = max(ans, v_sum - (c - sushi[i][0]) * 2 + cand1[i-1])

v_sum = 0
for i in range(n-1):
    v_sum += sushi[i][1]
    ans = max(ans, v_sum - sushi[i][0] * 2 + cand2[i+1])

print(ans)

Submission status: Runtime Error

Solution code:
import bisect
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
ans=[]
for i in range(29):
  T=2**i
  B_=list(B)
  A_=list(A)
  t=0
  for j in range(N):
    B_[j]%=2*T
    A_[j]%=2*T
  B_.sort()
  for j in range(N):
    a=A_[j]
    p=bisect.bisect_left(B_,T-a)
    q=bisect.bisect_left(B_,2*T-a)
    r=bisect.bisect_left(B_,3*T-a)
    s=bisect.bisect_left(B_,4*T-a)
    t+=(q-p)+(s-r)
  ans.append(t%2)
a=0
for i in range(29):
  a+=2**i*ans[i]
print(a)

Submission status: Runtime Error

Solution code:
#ABC095-D
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return list(input())
n,c=IL()
Sushi=[IL() for i in range(n)]
inf=float("INF")
E=[0]+[-inf for i in range(n)]
e=0
for i in range(n):
    x,v=Sushi[i]
    e+=v
    E[i+1]=max(E[i],E[i+1],e-x)
ans=E[n]
e=0
for i in range(n):
    x,v=Sushi[-i-1]
    e+=v
    ans=max(ans,e-2*(c-x)+E[-i-2])

for i in range(n):
    Sushi[i][0]=c-Sushi[i][0]
Sushi.sort()

E=[0]+[-inf for i in range(n)]
e=0
for i in range(n):
    x,v=Sushi[i]
    e+=v
    E[i+1]=max(E[i],E[i+1],e-x)

e=0
for i in range(n):
    x,v=Sushi[-i-1]
    e+=v
    ans=max(ans,e-2*(c-x)+E[-i-2])
print(max(0,ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
from collections.abc import Iterable
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, C: int, x: "List[int]", v: "List[int]"):
    x = [0, *x]
    v = [0, *v]
    bi = []
    best = 0
    besti = 0
    acc_v = list(accumulate(v, lambda acc, vv: acc + vv))
    for ii, (xx, vv) in enumerate(zip(x, acc_v)):
        if best < vv - xx:
            besti = ii
            best = vv - xx
        bi.append(besti)

    ret = 0
    for b in range(len(bi)):
        a = bi[b - 1]
        c = 0
        if a != 0:
            c += acc_v[a] - x[a]
        if b != 0:
            c += (acc_v[-1] - acc_v[b-1]) - ((C - x[b]) % C)
        c -= min(x[a], ((C - x[b]) % C))
        if ret < c:
            ret = c
    return ret

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    x = [int()] * (N)  # type: "List[int]" 
    v = [int()] * (N)  # type: "List[int]" 
    for i in range(N):
        x[i] = int(next(tokens))
        v[i] = int(next(tokens))
    result = solve(N, C, x, v)
    if isinstance(result, Iterable):
        result = '\n'.join([str(v) for v in result])
    print(result)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N,C=map(int, input().split())
inarr=[[int(i) for i in input().split()] for _ in range(N)]
maxes=[(0, 0)]*N
cums=[0]*(N+1)
cum = 0
cummax = (0, 0)
for i, [a, v] in enumerate(inarr):
    cum += v
    if (cum - a) > cummax[0]:
        cummax = (cum - a, a)
    maxes[i]=cummax
    cums[i+1]=cum

# print("cummax: {}, cums: {}".format(cummax, cums))
if cummax[0] == 0:
    print(0)
    exit(1)

resmax = 0
for i, [b, v] in enumerate(inarr):
    oasum, x = (0, 0) if i == 0 else maxes[i-1]
    obsum = cums[N] - cums[i] - (C-b)
    # print("len oa: {} len ob: {} oa sum: {} ob sum: {} score: {}".format(x, C-b, oasum, obsum, obsum + oasum - min(C-b, x)))
    resmax = max(resmax, obsum + oasum - min(C-b, x))
    # print(resmax)

# print(maxes[N-1])
resmax = max(resmax, maxes[N-1][0])
print(resmax)

Submission status: Runtime Error

Solution code:
N,C=map(int, input().split())
inarr=[[int(i) for i in input().split()] for _ in range(N)]
maxes=[(0, 0)]*N
cums=[0]*(N+1)
cum = 0
cummax = (0, 0)
for i, [a, v] in enumerate(inarr):
    cum += v
    if (cum - a) > cummax[0]:
        cummax = (cum - a, a)
    maxes[i]=cummax
    cums[i+1]=cum

print("cummax: {}, cums: {}".format(cummax, cums))
if cummax[0] == 0:
    print(0)
    exit(1)

resmax = 0
for i, [b, v] in enumerate(inarr):
    oasum, x = (0, 0) if i == 0 else maxes[i-1]
    obsum = cums[N] - cums[i] - (C-b)
    print("len oa: {} len ob: {} oa sum: {} ob sum: {} score: {}".format(x, C-b, oasum, obsum, obsum + oasum - min(C-b, x)))
    resmax = max(resmax, obsum + oasum - min(C-b, x))
    # print(resmax)

print(maxes[N-1])
resmax = max(resmax, maxes[N-1][0])
print(resmax)

Submission status: Runtime Error

Solution code:
N,C=map(int, input().split())
inarr=[[int(i) for i in input().split()] for _ in range(N)]
maxes=[(0, 0)]*N
cums=[0]*(N+1)
cum = 0
maxi = 0
cummax = (0, 0)
for i, [a, v] in enumerate(inarr):
    cum += v
    if cum - a > cummax[0]:
        maxi = i
        cummax = (cum - a, a)
    maxes[i]=cummax
    cums[i+1]=cum

if cummax[0] == 0:
    print(0)
    exit(1)

# print("cummax: {}, cum: {}".format(cummax, cum))
resmax = 0
for i, [b, v] in enumerate(inarr):
    sc, x = (0, 0) if i == 0 else maxes[i-1]
    obsum = cums[N] - cums[i] - (C-b)
    # print("len oa: {} len ob: {} oa sum: {} ob sum: {}".format(x, C-b, sc, obsum))
    resmax = max(resmax, obsum + sc - min(C-b, x))
    # print(resmax)

resmax = max(resmax, maxes[N-1][0])
print(resmax)

Submission status: Runtime Error

Solution code:
N,C=map(int, input().split())
inarr=[[int(i) for i in input().split()] for _ in range(N)]
maxes=[0]*N
cums=[0]*(N+1)
cum = 0
maxi = 0
cummax = (0, 0)
for i, [a, v] in enumerate(inarr):
    cum += v
    if cum - a > cummax[0]:
        maxi = i
        cummax = (cum - a, a)
    maxes[i]=cummax
    cums[i+1]=cum

if cummax[0] == 0:
    print(0)
    exit(1)

# print("cummax: {}, cum: {}".format(cummax, cum))
resmax = 0
for i, [b, v] in enumerate(inarr):
    sc, x = (0, 0) if i == 0 else maxes[i-1]
    obsum = cums[N] - cums[i] - (C-b)
    # print("len oa: {} len ob: {} oa sum: {} ob sum: {}".format(x, C-b, sc, obsum))
    resmax = max(resmax, obsum + sc - min(C-b, x))
    # print(resmax)

resmax = max(resmax, maxes[N-1][0])
print(resmax)

Submission status: Runtime Error

Solution code:
n, c, *t = map(int, open(0).read().split())
right = [0]
left = [0]
sushi = tuple([(0, 0)]) + tuple(zip(t[::2], t[1::2])) + tuple([(c, 0)])
for i in range(1, n + 1):
    x, v = sushi[i]
    y = sushi[i - 1][0]
    cost = right[-1] + v - (x - y)
    right.append(cost)
    x, v = sushi[n + 1 - i]
    y = sushi[n + 2 - i][0]
    cost = left[-1] + v - (y - x)
    left.append(cost)
ans = max(*right, *left)
right_max = right[:]
left_max = left[:]
for i in range(1, n + 1):
    right_max[i] = max(right_max[i], right_max[i - 1])
    left_max[i] = max(left_max[i], left_max[i - 1])
for i in range(1, n + 1):
    ans = max(ans, \
            right[i] - sushi[i][0] + left_max[n - i], \
            left[i] - c + sushi[n + 1 - i][0] + right_max[n - i])
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from bisect import bisect_right,bisect_left
N,C=map(int,input().split())
S=[[int(i) for i in input().split()] for i in range(N)]
Way1=[S[0][1]-S[0][0]]
p1=[]
if Way1[0]>0:
  p1.append(0)
for i in range(1,N):
  Way1.append(Way1[i-1]+S[i][1]-(S[i][0]-S[i-1][0]))
  if p1==[] and Way1[i]>0:
    p1.append(i)
  elif p1!=[]:
    if Way1[p1[-1]]<Way1[i]:
      p1.append(i)
    
Way2=[S[-1][1]-(C-S[-1][0])]
p2=[]
if Way2[0]>0:
  p2.append(N-1)
for i in range(1,N):
  Way2.append(Way2[i-1]+S[N-i-1][1]+(S[N-i-1][0]-S[N-i][0]))
  if p2==[] and Way2[i]>0:
    p2.append(N-i-1)
  elif p2!=[]:
    if Way2[N-1-p2[-1]]<Way2[i]:
      p2.append(N-i-1)
Way2.reverse()
p2.reverse()

ans=0
for i in p1:
  if i!=N-1:
    if bisect_right(p2,i+0.1)<len(p2):
      j=p2[bisect_right(p2,i+0.1)]
      ans=max(ans,Way1[i],Way1[i]-S[i][0]+Way2[j])
    else:
      ans=max(ans,Way1[i])
  else:
    ans=max(ans,Way1[i])
for j in p2:
  if j!=0:
    i=p1[bisect_left(p1,j-0.1)-1]
    ans=max(ans,Way2[j],Way1[i]-(C-S[j][0])+Way2[j])
  else:
    ans=max(ans,Way2[j])
    
print(ans)

Submission status: Runtime Error

Solution code:
from bisect import bisect_right
N,C=map(int,input().split())
S=[[int(i) for i in input().split()] for i in range(N)]
Way1=[S[0][1]-S[0][0]]
p1=[]
if Way1[0]>0:
  p1.append(0)
for i in range(1,N):
  Way1.append(Way1[i-1]+S[i][1]-(S[i][0]-S[i-1][0]))
  if p1==[] and Way1[i]>0:
    p1.append(i)
  elif Way1[p1[-1]]<Way1[i]:
    p1.append(i)
    
Way2=[S[-1][1]-(C-S[-1][0])]
p2=[]
if Way2[0]>0:
  p2.append(N-1)
for i in range(1,N):
  Way2.append(Way2[i-1]+S[N-i-1][1]+(S[N-i-1][0]-S[N-i][0]))
  if p2==[] and Way2[i]>0:
    p2.append(N-i-1)
  elif Way2[N-1-p2[-1]]<Way2[i]:
    p2.append(N-i-1)
Way2.reverse()
p2.reverse()

ans=0
for i in p1:
  if i!=N-1:
    if bisect_right(p2,i+0.1)<len(p2):
      j=p2[bisect_right(p2,i+0.1)]
      ans=max(ans,Way1[i],Way1[i]-S[i][0]+Way2[j],Way1[i]-(C-S[j][0])+Way2[j])
    else:
      ans=max(ans,Way1[i])
  else:
    ans=max(ans,Way1[i])
for j in p2:
  ans=max(ans,Way2[j])
print(ans)

Submission status: Runtime Error

Solution code:
from bisect import bisect_right
N,C=map(int,input().split())
S=[[int(i) for i in input().split()] for i in range(N)]
Way1=[S[0][1]-S[0][0]]
p1=[]
if Way1[0]>0:
  p1.append(0)
for i in range(1,N):
  Way1.append(Way1[i-1]+S[i][1]-(S[i][0]-S[i-1][0]))
  if p1==[] and Way1[i]>0:
    p_Way1.append([i])
  elif Way1[p1[-1]]<Way1[i]:
    p1.append(i)
    
Way2=[S[-1][1]-(C-S[-1][0])]
p2=[]
if Way2[0]>0:
  p2.append(N-1)
for i in range(1,N):
  Way2.append(Way2[i-1]+S[N-i-1][1]+(S[N-i-1][0]-S[N-i][0]))
  if p2==[] and Way2[i]>0:
    p2.append(N-i-1)
  elif Way2[N-1-p2[-1]]<Way2[i]:
    p2.append(N-i-1)
Way2.reverse()
p2.reverse()

ans=0
for i in p1:
  if i!=N-1:
    if bisect_right(p2,i+0.1)<len(p2):
      j=p2[bisect_right(p2,i+0.1)]
      ans=max(ans,Way1[i],Way1[i]-S[i][0]+Way2[j],Way1[i]-(C-S[j][0])+Way2[j])
    else:
      ans=max(ans,Way1[i])
  else:
    ans=max(ans,Way1[i])
for j in p2:
  ans=max(ans,Way2[j])
print(ans)

Submission status: Runtime Error

Solution code:
from bisect import bisect
N,C=map(int,input().split())
S=[[int(i) for i in input().split()] for i in range(N)]
Way1=[S[0][1]-S[0][0]]
p1=[]
if Way1[0]>0:
  p1.append(0)
for i in range(1,N):
  Way1.append(Way1[i-1]+S[i][1]-(S[i][0]-S[i-1][0]))
  if p1==[] and Way1[i]>0:
    p_Way1.append([i])
  elif Way1[p1[-1]]<Way1[i]:
    p1.append(i)
    
Way2=[S[-1][1]-(C-S[-1][0])]
p2=[]
if Way2[0]>0:
  p2.append(N-1)
for i in range(1,N):
  Way2.append(Way2[i-1]+S[N-i-1][1]+(S[N-i-1][0]-S[N-i][0]))
  if p2==[] and Way2[i]>0:
    p2.append(N-i-1)
  elif Way2[N-1-p2[-1]]<Way2[i]:
    p2.append(N-i-1)
Way2.reverse()
p2.reverse()

ans=0
for i in p1:
  if i!=N-1:
    j=p2[bisect(p2,i+0.1)]
    ans=max(ans,Way1[i],Way1[i]-S[i][0]+Way2[j],Way1[i]-(C-S[j][0])+Way2[j])
  else:
    ans=max(ans,Way1[i])
for j in p2:
  ans=max(ans,Way2[j])
print(ans)

Submission status: Runtime Error

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

ans=0

B1=[] #時計回り、引き返さない
su=0
for i in range(n):
    su+=A[i][1]
    B1.append(su-A[i][0])

#print(B1)

ans=max(ans,max(B1))

B2=[] #反時計回り、引き返さない
su=0
for i in range(n-1,-1,-1):
    su+=A[i][1]
    B2.append(su-(c-A[i][0]))

#print(B2)


ans=max(ans,max(B2))


B3=[] #時計回り、引き返す(半周以上周ってから引き返してはいけない)
B4=[] #反時計回り、引き返す（同上）

for i in range(n):
    if A[i][0]<=(c-0.1)//2 :
        B3.append(B1[i]-A[i][0])

#print(B3)

for i in range(n-1,-1,-1):
    j=0
    if (c-A[i][0])<=(c-0.1)//2 :
        B4.append(B2[j]-(c-A[i][0]))
    j+=1

#print(B4)

if len(B3)>0 :
    aa=B3.index(max(B3))
    #print(max(B3)+max(B2[:n-aa-1]))
    ans=max(ans,max(B3)+max(B2[:n-aa-1]))
if len(B4)>0 :
    bb=B4.index(max(B4))
    #print(max(B4)+max(B1[:n-bb-1]))
    ans=max(ans,max(B4)+max(B1[:n-bb-1]))

print(ans)

Submission status: Runtime Error

Solution code:
N,C = map(int,input().split()) 
XV = [[0,0]] + [[int(i) for i in input().split()] for _ in range(N)] + [[C,0]]

Sum_left = [0] * (N+1)
Sum_right = [0] * (N+1)
for i in range(N):
    Sum_left[i+1] = Sum_left[i] + XV[i+1][1]
    Sum_right[i+1] = Sum_right[i] + XV[N-i][1]

ans = 0
for i in range(N+1):
    for j in range(N+1-i):
        ans = max(ans, Sum_left[i] + Sum_right[j] - XV[i][0] - 2 * (C - XV[N+1-j][0]), Sum
_left[i] + Sum_right[j] - 2 * XV[i][0] - (C - XV[N+1-j][0]))

print(ans)

Submission status: Runtime Error

Solution code:
N,C = map(int,input().split()) 
XV = [[0,0]] + [[int(i) for i in input().split()] for _ in range(N)] + [[C,0]]

print(XV)

Sum_left = [0] * (N+1)
Sum_right = [0] * (N+1)
for i in range(N):
    Sum_left[i+1] = Sum_left[i] + XV[i+1][1]
    Sum_right[i+1] = Sum_right[i] + XV[N-i][1]

ans = 0
for i in range(N+1):
    for j in range(N+1-i):
        ans = max(ans, Sum_left[i] + Sum_right[j] - XV[i][0] - 2 * (C - XV[N+1-j][0]), Sum
_left[i] + Sum_right[j] - 2 * XV[i][0] - (C - XV[N+1-j][0]))

print(ans)

Submission status: Runtime Error

Solution code:
N,C = map(int,input().split())
XV = [0,0] + [[int(i) for i in input().split()] for _ in range(N)] + [C,0]

Sum_left = [0] * (N+1)
Sum_right = [0] * (N+1)
for i in range(N):
    Sum_left[i+1] = Sum_left[i] + XV[i+1][1]
    Sum_right[i+1] = Sum_right[i] + XV[N-i][1]

ans = 0
for i in range(N+1):
    for j in range(N+1-i):
        ans = max(ans, Sum_left[i] + Sum_right[j] - XV[i][0] - 2 * (C - XV[N+1-j][0]), Sum
_left[i] + Sum_right[j] - 2 * XV[i][0] - (C - XV[N+1-j][0]))

print(ans)