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 = map(int,input().split())
# zero-index
x = []
v = []
sv= [] # vの累積和
f = [] # sv - x
g = [] # f の最大
fa= []
ga= []
for i in range(n):
    temp = list(map(int,input().split()))
    x.append(temp[0])
    v.append(temp[1])
    if i == 0:
        sv.append(v[-1])
    else:
        sv.append(sv[-1] + v[-1])
    f.append(sv[-1] - x[-1])
    fa.append(sv[-1]- x[-1]*2)
    if i == 0:
        g.append(f[-1])
        ga.append(fa[-1])
    else:
        g.append(max(f[-1],g[-1]))
        ga.append(max(fa[-1],ga[-1]))
ans = 0
svr = 0
for i in range(n-1):
    svr += v[n-i-1]
    ans = max(ans, svr + x[n-i-1] - c + ga[n-i-2], svr +2*x[n-i-1] - 2*c * g[n-i-2])
svr += v[0]
ans = max(ans,svr+x[0]-c,g[n-1])
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

import sys

input=sys.stdin.readline

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

acu=list(accumulate(V))
if N>=2:
    data=[0 for i in range(0,N)]
    data[N-1]=acu[N-2]-2*X[N-1]
    for i in range(1,N):
        data[N-1-i]=min(data[N-i],acu[N-2-i]-2*X[N-1-i])

    data2=[0 for i in range(0,N)]
    data2[N-1]=acu[N-2]-X[N-1]
    for i in range(1,N):
        data2[N-1-i]=min(data2[N-i],acu[N-2-i]-X[N-1-i])
    ans=[0 for i in range(0,N-1)]
    for i in range(0,N-1):
        ans[i]=min(data[i+1]-acu[i]+X[i]+2*C,data2[i+1]-acu[i]+2*X[i]+C)
    test1=max(acu[i]-X[i] for i in range(0,N-1))
    k=sum(V)
    test2=max(k-acu[i]-(C-X[i+1]) for i in range(0,N-1))
    test2=max(test2,k-(C-X[0]))
    test3=k-min(ans)
    print(max(0,test1,test2,test3))
else:
    print(max(0,V[0]-min(C-X[0],X[0])))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

import sys

input=sys.stdin.readline

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

acu=list(accumulate(V))
if N>=2:
    data=[0 for i in range(0,N)]
    data[N-1]=acu[N-2]-2*X[N-1]
    for i in range(1,N):
        data[N-1-i]=min(data[N-i],acu[N-2-i]-2*X[N-1-i])

    data2=[0 for i in range(0,N)]
    data2[N-1]=acu[N-2]-X[N-1]
    for i in range(1,N):
        data2[N-1-i]=min(data2[N-i],acu[N-2-i]-X[N-1-i])
    ans=[0 for i in range(0,N-1)]
    for i in range(0,N-1):
        ans[i]=min(data[i+1]-acu[i]+X[i]+2*C,data2[i+1]-acu[i]+2*X[i]+C)
    test1=max(acu[i]-X[i] for i in range(0,N-1))
    k=sum(V)
    test2=max(k-acu[i]-(C-X[i+1]) for i in range(0,N-1))
    test2=max(test2,k-(C-X[0]))
    test3=k-min(ans)
    print(0,max(test1,test2,test3))
else:
    print(max(0,V[0]-min(C-X[0],X[0])))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

import sys

input=sys.stdin.readline

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

acu=list(accumulate(V))
if N>=2:
    data=[0 for i in range(0,N)]
    data[N-1]=acu[N-2]-2*X[N-1]
    for i in range(1,N):
        data[N-1-i]=min(data[N-i],acu[N-2-i]-2*X[N-1-i])

    data2=[0 for i in range(0,N)]
    data2[N-1]=acu[N-2]-X[N-1]
    for i in range(1,N):
        data2[N-1-i]=min(data2[N-i],acu[N-2-i]-X[N-1-i])
    ans=[0 for i in range(0,N-1)]
    for i in range(0,N-1):
        ans[i]=min(data[i+1]-acu[i]+X[i]+2*C,data2[i+1]-acu[i]+2*X[i]+C)
    test1=max(acu[i]-X[i] for i in range(0,N-1))
    k=sum(V)
    test2=max(k-acu[i]-(C-X[i+1]) for i in range(0,N-1))
    test2=max(test2,k-(C-X[0]))
    test3=k-min(ans)
    print(max(test1,test2,test3))
else:
    print(max(0,V[0]-min(C-X[0],X[0])))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

import sys

input=sys.stdin.readline

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

acu=list(accumulate(V))
if N>=2:
    data=[0 for i in range(0,N)]
    data[N-1]=acu[N-2]-2*X[N-1]
    for i in range(1,N):
        data[N-1-i]=min(data[N-i],acu[N-2-i]-2*X[N-1-i])

    data2=[0 for i in range(0,N)]
    data2[N-1]=acu[N-2]-X[N-1]
    for i in range(1,N):
        data2[N-1-i]=min(data2[N-i],acu[N-2-i]-X[N-1-i])
    ans=[0 for i in range(0,N-1)]
    for i in range(0,N-1):
        ans[i]=min(data[i+1]-acu[i]+X[i]+2*C,data2[i+1]-acu[i]+2*X[i]+C)
    test1=max(acu[i]-X[i] for i in range(0,N-1))
    test2=max(sum(V)-acu[i]-(C-X[i+1]) for i in range(0,N-1))
    test2=max(test2,sum(V)-(C-X[0]))
    test3=sum(V)-min(ans)
    print(max(test1,test2,test3))
else:
    print(max(0,V[0]-min(C-X[0],X[0])))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

import sys

input=sys.stdin.readline

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

acu=list(accumulate(V))
if N>=2:
    data=[0 for i in range(0,N)]
    data[N-1]=acu[N-2]-2*X[N-1]
    for i in range(1,N):
        data[N-1-i]=min(data[N-i],acu[N-2-i]-2*X[N-1-i])

    data2=[0 for i in range(0,N)]
    data2[N-1]=acu[N-2]-X[N-1]
    for i in range(1,N):
        data2[N-1-i]=min(data2[N-i],acu[N-2-i]-X[N-1-i])
    ans=[0 for i in range(0,N-1)]
    for i in range(0,N-1):
        ans[i]=min(data[i+1]-acu[i]+X[i]+2*C,data2[i+1]-acu[i]+2*X[i]+C)
    test1=max(acu[i]-X[i] for i in range(0,N-1))
    test2=max(sum(V)-acu[i]-(C-X[i+1]) for i in range(0,N-1))
    test2=max(test2,sum(V)-(C-X[0]))
    test3=sum(V)-min(ans)
    print(max(test1,test2,test3))
else:
    print(max(0,V[0]-min(C-X[0],X[0])))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

acu=list(accumulate(V))
if N>=2:
    data=[0 for i in range(0,N)]
    data[N-1]=acu[N-2]-2*X[N-1]
    for i in range(1,N):
        data[N-1-i]=min(data[N-i],acu[N-2-i]-2*X[N-1-i])

    data2=[0 for i in range(0,N)]
    data2[N-1]=acu[N-2]-X[N-1]
    for i in range(1,N):
        data2[N-1-i]=min(data2[N-i],acu[N-2-i]-X[N-1-i])
    ans=[0 for i in range(0,N-1)]
    for i in range(0,N-1):
        ans[i]=min(data[i+1]-acu[i]+X[i]+2*C,data2[i+1]-acu[i]+2*X[i]+C)
    test1=max(acu[i]-X[i] for i in range(0,N-1))
    test2=max(sum(V)-acu[i]-(C-X[i+1]) for i in range(0,N-1))
    test2=max(test2,sum(V)-(C-X[0]))
    test3=sum(V)-min(ans)
    print(max(test1,test2,test3))
else:
    print(max(0,V[0]-min(C-X[0],X[0])))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N,C=map(int,input().split())
D=[[int(x) for x in input().split()] for _ in range(N)]

X,V,M=[0]*(N+1),[0]*(N+1),[0]*(N+1)
for i in range(1,N+1):
  X[i],V[i]=D[i-1][0],V[i-1]+D[i-1][1]
  M[i]=M[i-1] if M[i-1]>V[i]-D[i-1][0] else V[i]-D[i-1][0]

m=0
for i in range(1,N+1):
  e=M[i-1]-(C-X[i]) if M[i-1]>C-X[i] else 0
  e+=V[N]-V[i-1]-(C-X[i])
  m=m if m>e else e

M=[0]*(N+2)
for i in range(N,0,-1):
  p=V[i]-V[i-1]-(C-X[i])
  M[i]=M[i+1] if M[i+1]>p else p

for i in range(1,N+1):
  e=M[i+1]-X[i] if M[i+1]>X[i] else 0
  e+=V[i]-X[i]
  m=m if m>e else e

print(m)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
N,C = map(int,input().split())
xv = [list(map(int,input().split())) for i in range(N)]
if N == 1:
    print(max(0,xv[0][1]-xv[0][0]))
else:
    x = [i[0] for i in xv]
    v = [i[1] for i in xv]
    fv = list(accumulate(v))
    rv = list(accumulate(v[::-1]))
    fcal = [j-i for i,j in zip(x,fv)]
    rcal = [j - (C-i) for i,j in zip(x[::-1],rv)]
    ans = max(0,max(fcal),max(rcal))
    for i in range(N-1):
        ans = max(ans,fcal[i]-x[i]+max([0]+rcal[:N-i]),rcal[i]-(C-x[N-i-1])+max([0]+fcal[:N-i]))
    print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
N,C = map(int,input().split())
xv = [list(map(int,input().split())) for i in range(N)]
x = [i[0] for i in xv]
v = [i[1] for i in xv]
fv = list(accumulate(v))
rv = list(accumulate(v[::-1]))
fcal = [0]+[j-i for i,j in zip(x,fv)]
rcal = [0]+[j - (C-i) for i,j in zip(x[::-1],rv)]

ans = 0
for i in range(N+1):
    ans = max(ans,fcal[i]+rcal[N-i],fcal[i],rcal[i])
print(ans)

Submission status: Wrong Answer

Solution code:
N, C = map(int, input().split())
S = [list(map(int, input().split())) for _ in range(N)]
SV = [0]
for x, v in S:
    SV.append(v+SV[-1])
left = [0]
leftturn = [0]
for i in range(N):
    left.append(SV[i+1]-S[i][0])
    leftturn.append(SV[i+1]-S[i][0]*2)
S.reverse()
SV = [0]
for x, v in S:
    SV.append(v+SV[-1])
right = [0]
rightturn = [0]
for i in range(N):
    right.append(SV[i+1]-(C-S[i][0]))
    rightturn.append(SV[i+1]-(C-S[i][0])*2)
ml = [0]
mr = [0]
mlt = [0]
mrt = [0]
for i in range(N):
    ml.append(max(left[i+1], ml[-1]))
    mr.append(max(right[i+1], mr[-1]))
    mlt.append(max(leftturn[i+1], mlt[-1]))
    mrt.append(max(rightturn[i+1], mrt[-1]))
res = 0
for i in range(N):
    res = max(res, ml[i]+mrt[N-i], mr[i]+mlt[N-i])
print(res)

Submission status: Wrong Answer

Solution code:
N,C=map(int,input().split())
X=[0]*N
V=[0]*N
Fa=[0]*N
Fb=[0]*N
_=0
for i in range(N):
  x,v=map(int,input().split())
  X[i]=x
  V[i]=v
  _+=v
  Fa[i]=_-x
_=0
for i in range(N):
  i=N-1-i
  _+=V[i]
  Fb[i]=_-(C-X[i])
Sa=[0]*N
Sb=[0]*N
for i in range(1,N):
  Sa[i]=max(Sa[i-1],Fa[i-1])
  Sb[N-1-i]=max(Sb[N-i],Fb[N-i])
ans=0
for i in range(N):
  ans=max(Fb[i]-(C-X[i])+Sa[i], ans, Fa[i]-X[i]+Sb[i])
ans=max(Sa[-1], ans, Sb[0])
print(ans)

Submission status: Wrong Answer

Solution code:
N,C=map(int,input().split())
X=[0]*N
V=[0]*N
Fa=[0]*N
Fb=[0]*N
_=0
for i in range(N):
  x,v=map(int,input().split())
  X[i]=x
  V[i]=v
  _+=v
  Fa[i]=_-x
_=0
for i in range(N):
  i=N-1-i
  _+=V[i]
  Fb[i]=_-(C-X[i])
Sa=[0]*N
Sb=[0]*N
for i in range(1,N):
  Sa[i]=max([0]+Fa[:i])
  Sb[N-1-i]=max(Fb[N-i:])
ans=0
for i in range(N):
  ans=max(Fb[i]-((C-X[i])%C)+Sa[i], ans, Fa[i]-X[i]+Sb[i])
ans=max(Sa[-1], ans, Sb[0])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline

INF = -(2 ** 31)

N, C = [int(_) for _ in input().split()]
X = [[int(x) for x in input().split()] for _ in range(N)]
L = [0] * N

pre = 0
pre_max = -(2 ** 31)
for i in range(N):
    x, v = X[-i - 1]
    L[i] = max(pre_max, pre + v - (C - x))
    pre += v
    pre_max = L[i]

# print(L)
ans = max(pre_max, 0)
pre = 0
for i in range(N):
    x, v = X[i]
    tmp = pre + v - x

    if i < N - 1:
        tmp2 = max(L[N - i - 2] - x, 0)
    else:
        tmp2=0
    # print(i, tmp, tmp2)
    ans = max(ans, tmp + tmp2)
    pre += v
print(ans)

Submission status: Wrong Answer

Solution code:
#17:38
n,c = map(int,input().split())
x = []
for _ in range(n):
  x.append(list(map(int,input().split())))
  
right = []
right1 = []
right2 = []
eaten = 0
for i in range(n):
  s,v = x[i]
  eaten += v
  right.append(eaten-s)
  if s < c/2:
    right1.append(eaten-s)
    right2.append(eaten-2*s)
x.reverse()

left = []
left1 = []
left2 = []
eaten = 0
for i in range(n):
  s,v = x[i]
  eaten += v
  left.append(eaten-(c-s))
  if s > c/2:
    left1.append(eaten-(c-s))
    left2.append(eaten-2*(c-s))

can = [0]
can.append(max(right))
can.append(max(left))
try:
  can.append(max(right2)+max(left1))
  can.append(max(left2)+max(right1))
except:
  pass
print(max(can))

Submission status: Wrong Answer

Solution code:
#17:38
n,c = map(int,input().split())
x = []
for _ in range(n):
  x.append(list(map(int,input().split())))
  
right = []
right1 = []
right2 = []
eaten = 0
for i in range(n):
  s,v = x[i]
  eaten += v
  right.append(eaten-s)
  if s <= c//2:
    right1.append(eaten-s)
    right2.append(eaten-2*s)
x.reverse()

left = []
left1 = []
left2 = []
eaten = 0
for i in range(n):
  s,v = x[i]
  eaten += v
  left.append(eaten-(c-s))
  if s > c//2:
    left1.append(eaten-(c-s))
    left2.append(eaten-2*(c-s))

can = [0]
can.append(max(right))
can.append(max(left))
try:
  can.append(max(right2)+max(left1))
  can.append(max(left2)+max(right1))
except:
  pass
print(max(can))

Submission status: Wrong Answer

Solution code:
import itertools
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 = [list(map(int,input().split())) for i in range(n)]
    x = [xv[i][0] for i in range(n)]
    v = [xv[i][1] 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]+v[i]-(x[i]-dis)
        r2[i+1] = r1[i]+v[i]-2*(x[i]-dis)
        dis = x[i]
    dis = c
    for i in reversed(range(n)):
        l1[n-i] = l1[n-i-1]+v[i]-(dis-x[i])
        l2[n-i] = l2[n-i-1]+v[i]-2*(dis-x[i])
        dis = x[i]
    for i in range(n):
        l1[i+1] = max(l1[i+1],l1[i])
        r1[i+1] = max(r1[i+1],r1[i])
    for i in range(n+1):
        ans = max(ans,max(r2[i]+l1[n-i],l2[i]+r1[n-i]))
    print(ans)

if __name__ == "__main__":
    main() #endline===============================================

Submission status: Wrong Answer

Solution code:
import itertools
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 = [list(map(int,input().split())) for i in range(n)]
    x = [xv[i][0] for i in range(n)]
    v = [xv[i][1] 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]+v[i]-(x[i]-dis)
        r2[i+1] = r1[i]+v[i]-2*(x[i]-dis)
        dis = x[i]
    dis = c
    for i in reversed(range(n)):
        l1[n-i] = l1[n-i-1]+v[i]-(dis-x[i])
        l2[n-i] = l2[n-i-1]+v[i]-2*(dis-x[i])
        dis = x[i]
    for i in range(n):
        l1[i+1] = max(l1[i+1],l1[i])
        r1[i+1] = max(r1[i+1],r1[i])
    for i in range(n):
        ans = max(ans,max(r2[i]+l1[n-i],l2[i]+r1[n-i]))
    print(ans)

if __name__ == "__main__":
    main() #endline===============================================

Submission status: Wrong Answer

Solution code:
import itertools
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 = [list(map(int,input().split())) for i in range(n)]
    x = [xv[i][0] for i in range(n)]
    v = [xv[i][1] 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]+v[i]-(x[i]-dis)
        r2[i+1] = r2[i]+v[i]-2*(x[i]-dis)
        dis = x[i]
    dis = c
    for i in reversed(range(n)):
        l1[n-i] = l1[n-i-1]+v[i]-(dis-x[i])
        l2[n-i] = l1[n-i-1]+v[i]-2*(dis-x[i])
        dis = x[i]
    for i in range(n):
        l1[i+1] = max(l1[i+1],l1[i])
        r1[i+1] = max(r1[i+1],r1[i])
    for i in range(n+1):
        ans = max(ans,max(r2[i]+l1[n-i],l2[i]+r1[n-i]))
    print(ans)
if __name__ == "__main__":
    main() #endline===============================================

Submission status: Wrong Answer

Solution code:
import itertools
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 = [list(map(int,input().split())) for i in range(n)]
    x = [xv[i][0] for i in range(n)]
    v = [xv[i][1] 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]+v[i]-(x[i]-dis)
        r2[i+1] = r2[i]+v[i]-2*(x[i]-dis)
        dis = c
    for i in reversed(range(n)):
        l1[n-i] = l1[n-i-1]+v[i]-(dis-x[i])
        l2[n-i] = l1[n-i-1]+v[i]-2*(dis-x[i])
        dis = x[i]
    for i in range(n):
        l1[i+1] = max(l1[i+1],l1[i])
        r1[i+1] = max(r1[i+1],r1[i])
    for i in range(n+1):
        ans = max(ans,max(r2[i]+l1[n-i],l2[i]+r1[n-i]))
    print(ans)
if __name__ == "__main__":
    main() #endline===============================================

Submission status: Wrong Answer

Solution code:
N,C = map(int, input().split())
L = [[int(l) for l in input().split()] for _ in range(N)]

right = [0]*N
right[0] = L[0][1] - L[0][0]
for i in range(N-1):
    right[i+1] = right[i] + L[i+1][1] - (L[i+1][0] - L[i][0])

left = [0]*N
left[N-1] = L[N-1][1] - (C - L[N-1][0])
for i in range(N-1, 0, -1):
    left[i-1] = left[i] + L[i-1][1] - (L[i][0] - L[i-1][0])

r_temp = 0
r_idx = -1
for i in range(N):
    if L[i][0] >= C / 2:
        break
    if r_temp < right[i] - L[i][0]:
        r_temp = right[i] - L[i][0]
        r_idx = i

l_temp = 0
l_idx = N
for i in range(N-1, -1, -1):
    if C - L[i][0] >= C / 2:
        break
    if l_temp < left[i] - (C - L[i][0]):
        l_temp = left[i] - (C - L[i][0])
        l_idx = i

ans = max(0, max(right), max(left))
#for i in range(N-1, r_idx, -1):
#    ans = max(ans, r_temp + left[i])
for i in range(l_idx):
    ans = max(ans, l_temp + right[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N,C = map(int, input().split())
L = [[int(l) for l in input().split()] for _ in range(N)]

right = [0]*N
right[0] = L[0][1] - L[0][0]
for i in range(N-1):
    right[i+1] = right[i] + L[i+1][1] - (L[i+1][0] - L[i][0])

left = [0]*N
left[N-1] = L[N-1][1] - (C - L[N-1][0])
for i in range(N-1, 0, -1):
    left[i-1] = left[i] + L[i-1][1] - (L[i][0] - L[i-1][0])

r_temp = 0
r_idx = -1
for i in range(N):
    if L[i][0] >= C / 2:
        break
    if r_temp < right[i] - L[i][0]:
        r_temp = right[i] - L[i][0]
        r_idx = i

l_temp = 0
l_idx = N
for i in range(N-1, -1, -1):
    if C - L[i][0] >= C / 2:
        break
    if l_temp < left[i] - (C - L[i][0]):
        l_temp = left[i] - (C - L[i][0])
        l_idx = i

ans = max(0, max(right), max(left))
for i in range(N-1, r_idx, -1):
    ans = max(ans, r_temp + left[i])
#for i in range(l_idx):
#    ans = max(ans, l_temp + right[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N,C = map(int, input().split())
L = [[int(l) for l in input().split()] for _ in range(N)]

right = [0]*N
right[0] = L[0][1] - L[0][0]
for i in range(N-1):
    right[i+1] = right[i] + L[i+1][1] - (L[i+1][0] - L[i][0])

left = [0]*N
left[N-1] = L[N-1][1] - (C - L[N-1][0])
for i in range(N-1, 0, -1):
    left[i-1] = left[i] + L[i-1][1] - (L[i][0] - L[i-1][0])

r_temp = 0
r_idx = -1
for i in range(N):
    if L[i][0] >= C / 2:
        break
    if r_temp < right[i] - L[i][0]:
        r_temp = right[i] - L[i][0]
        r_idx = i

l_temp = 0
l_idx = N
for i in range(N-1, -1, -1):
    if C - L[i][0] >= C / 2:
        break
    if l_temp < left[i] - (C - L[i][0]):
        l_temp = left[i] - (C - L[i][0])
        l_idx = i

ans = max(0, max(right), max(left))
for i in range(N-1, r_idx, -1):
    ans = max(ans, r_temp + left[i])
for i in range(l_idx):
    ans = max(ans, l_temp + right[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N,C = map(int, input().split())
L = [[int(l) for l in input().split()] for _ in range(N)]

right = [0]*N
right[0] = L[0][1] - L[0][0]
for i in range(N-1):
    right[i+1] = right[i] + L[i+1][1] - (L[i+1][0] - L[i][0])

left = [0]*N
left[N-1] = L[N-1][1] - (C - L[N-1][0])
for i in range(N-1, 0, -1):
    left[i-1] = left[i] + L[i-1][1] - (L[i][0] - L[i-1][0])

r_temp = 0
r_idx = -1
for i in range(N):
    if L[i][0] >= C / 2:
        break
    if r_temp < right[i] - L[i][0]:
        r_temp = right[i] - L[i][0]
        r_idx = i

l_temp = 0
l_idx = N
for i in range(N-1, -1, -1):
    if C - L[i][0] >= C / 2:
        break
    if l_temp < left[i] - (C - L[i][0]):
        l_temp = left[i] - (C - L[i][0])
        l_idx = i

ans = 0
for i in range(N-1, r_idx, -1):
    ans = max(ans, r_temp + left[i])
for i in range(l_idx):
    ans = max(ans, l_temp + right[i])

print(ans)

Submission status: Wrong Answer

Solution code:
N,C = map(int, input().split())
L = [[int(l) for l in input().split()] for _ in range(N)]

right = [0]*N
right[0] = L[0][1] - L[0][0]
for i in range(N-1):
    right[i+1] = right[i] + L[i+1][1] - (L[i+1][0] - L[i][0])

left = [0]*N
left[N-1] = L[N-1][1] - (C - L[N-1][0])
for i in range(N-1, 0, -1):
    left[i-1] = left[i] + L[i-1][1] - (L[i][0] - L[i-1][0])

r_temp = 0
r_idx = -1
for i in range(N):
    if L[i][0] >= C / 2:
        break
    if r_temp < right[i] - L[i][0]:
        r_temp = right[i] - L[i][0]
        r_idx = i

l_temp = 0
l_idx = -1
for i in range(N-1, -1, -1):
    if C - L[i][0] >= C / 2:
        break
    if l_temp < left[i] - (C - L[i][0]):
        l_temp = left[i] - (C - L[i][0])
        l_idx = i

ans = max(0, max(left), max(right))
if r_idx >= 0:
    for i in range(N-1, r_idx, -1):
        ans = max(ans, r_temp + left[i])
if l_idx >= 0:
    for i in range(l_idx):
        ans = max(ans, l_temp + right[i])

print(ans)