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())
x = [tuple(map(int, input().split())) for _ in range(n)]

S_cw = [0]
S_ccw = [0]
S_cw_return = [0]
S_ccw_return = [0]

for i in range(n):
    S_cw.append(S_cw[-1]+x[i][1])
    S_ccw.append(S_ccw[-1]+x[n-1-i][1])

for i in range(n):
    S_cw[i+1] -= x[i][0]
    S_ccw[i+1] -= c-x[n-1-i][0]
    S_cw_return.append(S_cw[i+1] - x[i][0])
    S_ccw_return.append(S_ccw[i+1] - (c-x[n-1-i][0]))

S_cw_return_max = [0]
S_ccw_return_max = [0]
for i in range(n):
    if S_cw_return[i+1] > S_cw_return_max[-1]:
        S_cw_return_max.append(S_cw_return[i+1])
    else:
        S_cw_return_max.append(S_cw_return_max[-1])

    if S_ccw_return[i+1] > S_ccw_return_max[-1]:
        S_ccw_return_max.append(S_ccw_return[i+1])
    else:
        S_ccw_return_max.append(S_ccw_return_max[-1])

ans = 0
for i in range(n):
    idx = n-i
    ans = max(ans, S_cw[i]+S_ccw_return_max[idx], S_ccw[i]+S_cw_return_max[idx])
print(ans)

Submission status: Wrong Answer

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

D = []

for i in range(N):
  D.append(list(map(int, input().split())))
  
R = [[0, 0] for i in range(N + 1)]
cnt = 0
ma = 0
man = 0
for i in range(N):
  cnt += D[i][1]
  if cnt - D[i][0] > ma:
    ma = cnt - D[i][0]
    man = i + 1
  R[i + 1] = [man, ma]

cnt = 0
ma = R[N][1]
Ans = [0, R[N][0]]
for i in range(1, N + 1):
  t = N - i
  cnt += D[t][1]
  if cnt + R[t][1] - (C - D[t][0]) > ma:
    ma = cnt + R[t][1] - (C - D[t][0])
    Ans = [t + 1, R[t][0]]

if Ans[0] == 0 or Ans[1] == 0:
  print(ma)
else:
  print(ma - min(C - D[Ans[0] - 1][0], D[Ans[1] - 1][0]))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
import sys
input = sys.stdin.readline

n,c = map(int,input().split())
x1,v1 = [0],[0]
x2,v2 = [],[]
for _ in range(n):
  a,b = map(int, input().split())
  x1.append(a)
  x2 = [c-a] + x2
  v1.append(b)
  v2 = [b] + v2
x2 = [0] + x2
v2 = [0] + v2

v1, v2 = list(accumulate(v1)), list(accumulate(v2))

f = [v2[i] - x2[i] for i in range(n+1)]
g,x = f[0],x2[0]
val = v1[n]-x1[n] + g - x - min(x1[n],x)
for i in range(1,n+1):
  if g<f[i]:
	  g,x = f[i], x2[i]
  val2 = v1[n-i]-x1[n-i] + g - min(x1[n-i],x)
  if val < val2:
      val = val2
print(val)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
import sys
input = sys.stdin.readline

n,c = map(int,input().split())
x1,v1 = [0],[0]
x2,v2 = [],[]
for _ in range(n):
  a,b = map(int, input().split())
  x1.append(a)
  x2 = [c-a] + x2
  v1.append(b)
  v2 = [b] + v2
x2 = [0] + x2
v2 = [0] + v2

v1, v2 = list(accumulate(v1)), list(accumulate(v2))

f = [v2[i] - x2[i] for i in range(n+1)]
g = [(f[0],x2[0])]
for i in range(n):
  if g[i-1][0]<f[i]:
	  g.append((f[i], x2[i]))
  else:
  	  g.append(g[i-1])
cost = [v1[i] - x1[i] + g[n-i][0] - min(x1[i],g[n-i][1]) for i in range(n+1)]
print(max(cost))

Submission status: Wrong Answer

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


# 順方向,逆方向の累積和
VF,VB = [0]*(N+1),[0]*(N+1)
for i in range(N):
    VF[i+1] += VF[i]+XV[i][1]
for i in range(N):
    VB[N-i-1] += VB[N-i]+XV[N-1-i][1]
# 移動距離を減算
for i in range(N):
    VF[i+1] -= XV[i][0]
for i in range(N):
    VB[N-i-1] -= (C-XV[N-1-i][0])
# 食べに行くと損する場合を除くために単調増加に
for i in range(N):
    if VF[i+1] < VF[i]:
        VF[i+1] = VF[i]
for i in range(N):
    if VB[N-1-i] < VB[N-i]:
        VB[N-1-i] = VB[N-i]

# 順方向,逆方向で全部食べた場合で初期化
ans = 0
ans = max(ans, VF[N]-XV[N-1][0], VB[0]-(C-XV[0][0]))

# 反転 : 順方向→逆方向
for i in range(1,N):
    ans = max(ans, VF[i], VF[i]+VB[i]-XV[i-1][0])

# 反転 : 逆方向→順方向
for i in range(1,N):
    ans = max(ans, VB[i], VB[i]+VF[i]-(C-XV[i][0]))

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import combinations as comb

n,c=map(int,input().split())
l=[list(map(int,input().split())) for i in range(n)]

rev=[None]*n
norm=[None]*n
buf=0
ans=0

# normal rotation
for i in range(n):
    x,v=l[i]
    if i:
        norm[i]=(x,v+norm[i-1][1])
    else:
        norm[i]=(x,v)

# reverse rotation
for i in range(n)[::-1]:
    x,v=l[i]
    if i==n-1:
        rev[i]=(c-x,v)
    else:
        rev[i]=(c-x,v+rev[i+1][1])

for i,j in comb(range(n),2):
    r1=norm[j][1]-norm[j][0]
    r2=rev[j][1]+norm[i][1]-(rev[j][0]+norm[i][0])-norm[i][0]
    r3=rev[i][1]-rev[i][0]
    r4=rev[j][1]+norm[i][1]-(rev[j][0]+norm[i][0])-rev[j][0]
    ans=max(ans,r1,r2,r3,r4)

print(ans)

Submission status: Wrong Answer

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

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

if n == 1:
    print(max(sushi[0][1]-sushi[0][0], sushi[0][1]-(c-sushi[0][0])), 0)
    exit()

x = []
for i in range(n):
    x.append(sushi[i][0])

vcum = []
v = 0
for i in range(n):
    v += sushi[i][1]
    vcum.append(v)

ans = 0

# 1. 時計回りのみ（方向変換なし）
for i in range(n):
    if ans < vcum[i] - x[i]:
        ans = vcum[i] - x[i]


# 2. 反時計回りのみ（方向転換なし）
for i in range(n):
    if i == 0:
        if ans < vcum[n-1] - (c - x[0]):
            ans = vcum[n-1] - (c - x[0])
    else:
        if ans < (vcum[n - 1] - vcum[i - 1]) - (c - x[i]):
            ans = (vcum[n - 1] - vcum[i - 1]) - (c - x[i])


# 3. 時計回りのあと、方向変換して反時計回り
# right_dp[i]: 反時計回りに見て n-1~i番目 (0-index)までの寿司を取れる時(必ずしもi番目を取らなくて良い)の、カロリーの正味の最大値
right_dp = [0] * n
right_dp[n-1] = vcum[n-1] - vcum[n-2] - (c - x[n-1])
for i in reversed(range(1, n-1)):
    right_dp[i] = max(right_dp[i+1], vcum[n-1] - vcum[i-1] - (c - x[i]))

# 時計回りにi番目まで見てから折り返す(すると、反時計回りに回る時に得る正味のカロリーの最大値はright_dp[i+1]となる
for i in range(n-1):
    cal = vcum[i] - 2 * x[i] + right_dp[i+1]
    if ans < cal:
        ans = cal

# 4. 反時計回りのあと、方向変換して時計回り
# left_dp[i]: 時計回りにみてi番目(0-index)までの寿司を取れる時（必ずしもi番目を取らなくて良い）の、カロリーの正味の最大値
left_dp = [0] * n
left_dp[0] = vcum[0] - x[0]
for i in range(1, n):
    left_dp[i] = max(left_dp[i-1], vcum[i] - x[i])

# 反時計回りにi番目まで見てから折り返す (すると、時計回りに回る時に得る正味のカロリーの最大値はleft_dp[i-1]となる)
for i in range(1, n):
    cal = vcum[n-1] - vcum[i-1] - 2 * (c - x[i]) + left_dp[i-1]
    if ans < cal:
        ans = cal


print(ans)

Submission status: Wrong Answer

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

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

if n == 1:
    print(max(sushi[0][1]-sushi[0][0], sushi[0][1]-(c-sushi[0][0])), 0)
    exit()

x = []
for i in range(n):
    x.append(sushi[i][0])

vcum = []
v = 0
for i in range(n):
    v += sushi[i][1]
    vcum.append(v)

ans = 0

# 1. 時計回りのみ（方向変換なし）
for i in range(n):
    if ans < vcum[i] - x[i]:
        ans = vcum[i] - x[i]


# 2. 反時計回りのみ（方向転換なし）
for i in range(n):
    if i == 0:
        if ans < vcum[n-1] - (c - x[0]):
            ans = vcum[n-1] - (c - x[0])
    else:
        if ans < (vcum[n - 1] - vcum[i - 1]) - (c - x[i]):
            ans = (vcum[n - 1] - vcum[i - 1]) - (c - x[i])


# 3. 時計回りのあと、方向変換して反時計回り
# right_dp[i]: 反時計回りに見て n-1~i番目 (0-index)までの寿司を取れる時(必ずしもi番目を取らなくて良い)の、カロリーの正味の最大値
right_dp = [0] * n
right_dp[n-1] = vcum[n-1] - vcum[n-2] - (c - x[n-1])
for i in reversed(range(1, n-1)):
    right_dp[i] = max(right_dp[i+1], vcum[n-1] - vcum[i-1] - (c - x[i]))

# 時計回りにi番目まで見てから折り返す(すると、反時計回りに回る時に得る正味のカロリーの最大値はright_dp[i+1]となる
for i in range(n-1):
    cal = vcum[i] - 2 * x[i] + right_dp[i+1]

# 4. 反時計回りのあと、方向変換して時計回り
# left_dp[i]: 時計回りにみてi番目(0-index)までの寿司を取れる時（必ずしもi番目を取らなくて良い）の、カロリーの正味の最大値
left_dp = [0] * n
left_dp[0] = vcum[0] - x[0]
for i in range(1, n):
    left_dp[i] = max(left_dp[i-1], vcum[i] - x[i])

# 反時計回りにi番目まで見てから折り返す (すると、時計回りに回る時に得る正味のカロリーの最大値はleft_dp[i-1]となる)
for i in range(1, n):
    cal = vcum[n-1] - vcum[i-1] - 2 * (c - x[i]) + left_dp[i-1]
    if ans < cal:
        ans = cal


print(ans)

Submission status: Wrong Answer

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

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

if n == 1:
    print(max(sushi[0][1]-sushi[0][0], sushi[0][1]-(c-sushi[0][0])))
    exit()

x = []
for i in range(n):
    x.append(sushi[i][0])

vcum = []
v = 0
for i in range(n):
    v += sushi[i][1]
    vcum.append(v)

ans = 0

# 1. 時計回りのみ（方向変換なし）
for i in range(n):
    if ans < vcum[i] - x[i]:
        ans = vcum[i] - x[i]


# 2. 反時計回りのみ（方向転換なし）
for i in range(n):
    if i == 0:
        if ans < vcum[n-1] - (c - x[0]):
            ans = vcum[n-1] - (c - x[0])
    else:
        if ans < (vcum[n - 1] - vcum[i - 1]) - (c - x[i]):
            ans = (vcum[n - 1] - vcum[i - 1]) - (c - x[i])


# 3. 時計回りのあと、方向変換して反時計回り
# right_dp[i]: 反時計回りに見て n-1~i番目 (0-index)までの寿司を取れる時(必ずしもi番目を取らなくて良い)の、カロリーの正味の最大値
right_dp = [0] * n
right_dp[n-1] = vcum[n-1] - vcum[n-2] - (c - x[n-1])
for i in reversed(range(1, n-1)):
    right_dp[i] = max(right_dp[i+1], vcum[n-1] - vcum[i-1] - (c - x[i]))

# 時計回りにi番目まで見てから折り返す(すると、反時計回りに回る時に得る正味のカロリーの最大値はright_dp[i+1]となる
for i in range(n-1):
    cal = vcum[i] - 2 * x[i] + right_dp[i+1]

# 4. 反時計回りのあと、方向変換して時計回り
# left_dp[i]: 時計回りにみてi番目(0-index)までの寿司を取れる時（必ずしもi番目を取らなくて良い）の、カロリーの正味の最大値
left_dp = [0] * n
left_dp[0] = vcum[0] - x[0]
for i in range(1, n):
    left_dp[i] = max(left_dp[i-1], vcum[i] - x[i])

# 反時計回りにi番目まで見てから折り返す (すると、時計回りに回る時に得る正味のカロリーの最大値はleft_dp[i-1]となる)
for i in range(1, n):
    cal = vcum[n-1] - vcum[i-1] - 2 * (c - x[i]) + left_dp[i-1]
    if ans < cal:
        ans = cal


print(ans)

Submission status: Wrong Answer

Solution code:
N,C = map(int,input().split())
xv = []
for i in range(N):
    xv.append(list(map(int,input().split())))
MAX = 0
v = []
for l in range(N):
    v.append(xv[l][1])
for j in range(N):
    k = 0
    for k in range(N): 
        right = xv[j][0]
        left = xv[k][0]
        left = C - left
        cost = min(2*right+left,right+2*left)
        energy = sum(v)-sum(v[j+1:k])
        MAX = max(energy-cost,MAX)

print(MAX)

Submission status: Wrong Answer

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

cal = list()

prev = 0
dist = 0
X = list()
V = list()
for i in range(N):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v)
    prev = prev + v - x + dist
    dist = x
    cal.append(prev)


prev = 0
dist = 0
reverse = list(map(lambda x: C - x, X))
for x, v in zip(reverse[::-1], V[::-1]):
    prev = prev + v - x + dist
    dist = x
    cal.append(prev)

print(max(cal))

Submission status: Wrong Answer

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

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

print(Way1)
print(p1)
print(Way2)
print(p2)

Submission status: Wrong Answer

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

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

sushi = []

for i in range(N):
    sushi.append(map(int, raw_input().split()))

max_calorie = 0
previous_calorie = 0
previous_d = 0

for i in range(N):
    s = sushi[i]
    spend_calorie = s[0] - previous_d
    get_calorie = s[1]

    previous_calorie += get_calorie - spend_calorie
    previous_d = s[0]
    if previous_calorie > max_calorie:
        max_calorie = previous_calorie
print(max_calorie)

Submission status: Wrong Answer

Solution code:
n,c=map(int,input().split())


kyori=[0]*(n+2)
kyori[n+1]=c
calo=[0]*(n+2)
ans=[0]
tmp=0

for i in range(1,n+1):
    x,v=map(int,input().split())
    kyori[i]=x
    calo[i]=v
    tmp+=v-(x-kyori[i-1])
    ans.append(tmp)


tmp=0
for i in range(n):
    tmp+=calo[-2-i] - (kyori[-1-i]-kyori[-2-i])
    ans.append(tmp)



i=1
rmax=0
tmpmax=0
tmp=0
while(2*kyori[i]< c and i<=n):
    tmp+=calo[i]-2*(kyori[i] -kyori[i-1])
    if(tmpmax<tmp):
        tmpmax=tmp
        rmax=i
    i+=1

tmp=0
tmpmax_list=[0]
i=0
while(rmax < -i+n):
    tmp+=calo[-2-i] - (kyori[-1-i] - kyori[-2-i])
    tmpmax_list.append(tmp)
    i+=1

ans.append(tmpmax+ max(tmpmax_list))




i=0
lmax=0
tmpmax=0
tmp=0
while(2*kyori[-2-i] > c and i<n):
    tmp+=calo[-2-i]-2*(kyori[-1-i] -kyori[-2-i])
    if(tmpmax<tmp):
        tmpmax=tmp
        lmax=-i+n
    i+=1

tmp=0
tmpmax_list=[0]
i=1
while(i < lmax):
    tmp+=calo[i] - (kyori[i] - kyori[i-1])
    tmpmax_list.append(tmp)
    i+=1

ans.append(tmpmax+ max(tmpmax_list))

print(max(ans))

Submission status: Wrong Answer

Solution code:
n, c = map(int, input().split())
x = []
v = []
vcum = []
cum = 0
for i in range(n):
    X, V = map(int, input().split())
    x.append(X)
    cum += V
    v.append(V)
    vcum.append(cum)

maxnut = 0

for i in range(n):
    maxnut = max(maxnut, vcum[i] - x[i])

for i in range(n):
    if i == 0:
        maxnut = max(maxnut, vcum[n-1] - vcum[i] -(c - x[i]))
    else:
        maxnut = max(maxnut, vcum[n-1] - vcum[i-1] - (c - x[i]))

for i in range(n):
    for j in range(i+1, n):
        maxnut = max(maxnut, vcum[i] - 2 * x[i] + vcum[n-1] - vcum[j-1],  - (c - x[j]))

for i in range(n):
    for j in range(i+1, n):
        maxnut = max(maxnut, vcum[i] - x[i] + vcum[n-1] - vcum[j-1],  - (c - x[j]) * 2)

print(maxnut)

Submission status: Wrong Answer

Solution code:
n, c = map(int, input().split())
x = []
v = []
vcum = []
cum = 0
for i in range(n):
    X, V = map(int, input().split())
    x.append(X)
    cum += V
    v.append(V)
    vcum.append(cum)

maxnut = 0

for i in range(n):
    maxnut = max(maxnut, vcum[i] - x[i])

for i in range(n):
    if i == 0:
        maxnut = max(maxnut, vcum[n-1] - (c - x[i]))
    else:
        maxnut = max(maxnut, vcum[n-1] - vcum[i-1] - (c - x[i]))

for i in range(n):
    for j in range(i+1, n):
        maxnut = max(maxnut, vcum[i] - 2 * x[i] + vcum[n-1] - vcum[j-1],  - (c - x[j]))

for i in range(n):
    for j in range(i+1, n):
        maxnut = max(maxnut, vcum[i] - x[i] + vcum[n-1] - vcum[j-1],  - (c - x[j]) * 2)

print(maxnut)

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)

st1 = [0] * N
st2 = [0] * N
st1[0] = max(0, V[0] - X[0])
st2[0] = max(0, V[0] - 2 * X[0])
cur1, cur2 = st1[0], st2[0]
pre = X[0]
for i in range(1, N):
    cur1 += V[i] - (X[i] - pre)
    cur2 += V[i] - (X[i] - pre) * 2
    st1[i] = max(st1[i - 1], cur1)
    st2[i] = max(st2[i - 1], cur2)
    pre = X[i]

ba1 = [0] * N
ba2 = [0] * N
ba1[-1] = max(0, V[-1] - (C - X[-1]))
ba2[-1] = max(0, V[-1] - 2 * (C - X[-1]))
cur1, cur2 = ba1[-1], ba2[-1]
pre = X[-1]
for i in range(N - 2, -1, -1):
    cur1 += V[i] - (pre - X[i])
    cur2 += V[i] - (pre - X[i]) * 2
    ba1[i] = max(ba1[i + 1], cur1)
    ba2[i] = max(ba2[i + 1], cur2)
    pre = X[i]

num = 0
num = max(st1[N - 1], ba1[0])
for i in range(N - 1):
    num = max(num, st1[i] + ba2[i + 1])

for i in range(1, N - 1):
    num = max(num, ba1[i] + st2[i - 1])

print(num)

Submission status: Wrong Answer

Solution code:
import heapq
n, c = list(map(int, input().split()))
xv = [[0, 0]]+[list(map(int, input().split())) for _ in range(n)]

for_cals = [0]
for_cals_max = [0]
x_cur = 0
for i in range(1, n+1):
    x_i, v_i = xv[i]
    cal = for_cals[i-1] + v_i - (x_i-x_cur)
    cal_max = for_cals[i-1]
    for_cals_max.append(max(cal_max, cal))

    x_cur = x_i
    for_cals.append(cal)
        
back_cals = [0]
back_cals_max = [0]
x_cur = 0
for i in range(1, n+1):
    x_i, v_i = xv[-i]
    x_i = c - x_i
    cal = back_cals[i-1] + v_i - (x_i-x_cur)
    cal_max = back_cals[i-1]
    back_cals_max.append(max(cal_max, cal))

    x_cur = x_i
    back_cals.append(cal)
    

fb_cals = [back_cals_max[-1]]
bf_cals = [for_cals_max[-1]]
for i in range(1, n+1):
    x_f, _ = xv[i]
    cal_f = for_cals[i]
    fb_cals.append(cal_f - x_f + back_cals_max[n-i])

    x_b, _ = xv[-i]
    x_b = c - x_b
    cal_b = back_cals[i]
    bf_cals.append(cal_b - x_b + for_cals_max[n-i])
    
ans = max(max(for_cals_max), max(back_cals_max), max(fb_cals), max(bf_cals))
print(ans)

Submission status: Wrong Answer

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]))
    if n-aa-1>0 :
        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]))
    if n-bb-1>0 :
        ans=max(ans,max(B4)+max(B1[:n-bb-1]))

print(ans)

Submission status: Wrong Answer

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

sushi_front = 0
sushi_back = 0
sushi_front2 = 0
sushi_back2 = 0
sushi = []

sum_c= 0
sum_d = 0
for _ in range(N):
    sushi.append(list(map(int,input().split())))
    
    sum_c += sushi[-1][1]
    sum_d += sushi[-1][0]
    
    sushi_front = max(sushi_front, sum_c-sum_d)
    sushi_front2 = max(sushi_front2, sum_c-2*sum_d)

sushi.reverse()
sum_c = 0
sum_d = 0
for x in sushi:
    
    sum_c += x[1]
    sum_d += C -x[0]
    
    sushi_back = max(sushi_back, sum_c-sum_d)
    sushi_back2 = max(sushi_back2, sum_c-2*sum_d)

print(max(sushi_front+sushi_back2, sushi_back+sushi_front2))

Submission status: Wrong Answer

Solution code:
import numpy as np
N, C = list(map(int,input().split()))

sushi = [[0,0]]
for _ in range(N):
    sushi.append(list(map(int,input().split())))
sushi.append([C,0])
sushi = np.array(sushi)
print(sushi)
sum_c = 0
sum_d = 0
ans = 0
for f in range(N+1):
    sum_c += sushi[f,1]
    sum_d = sushi[f,0]
    tmp_c = sum_c
    tmp_d = sum_d
    for b in range(1,N+2-f):
        tmp_c += sushi[-b,1]
        tmp_d = min(sum_d, C-sushi[-b,0])*2 + max(sum_d, C-sushi[-b,0])
        if ans < tmp_c-tmp_d:
            ans = tmp_c-tmp_d

print(ans)

Submission status: Wrong Answer

Solution code:
n, c = map(int, input().split())
max_ene = 0
now = [0, 0]
for i in range(n):
    a, b = map(int, input().split())
    now = [a, now[0] + now[1] -a + b]
    if now[1] > max_ene:
        max_ene = now[1]

print(max_ene)

Submission status: Wrong Answer

Solution code:
import numpy as np
import copy
N, C = map(int, input().split())

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

cum_right = np.cumsum(v)
cum_left = np.cumsum(v[::-1])

max_right = np.zeros(N)
max_left = np.zeros(N)
max_right[0] = cum_right[0] - x[0]
max_left[0] = cum_left[0] - (C - x[N - 1])
for i in range(1, N):
    max_right[i] = max(max_right[i - 1], cum_right[i] - x[i])
    max_left[i] = max(max_left[i - 1], cum_left[i] - (C - x[N - 1 - i]))

ans = max(0, max_left[N - 1], max_right[N - 1])

for i in range(N - 1):
    ans = max(ans, cum_left[i] + max_right[N - 2 - i] - (C - x[N - 1 - i]) * 2)
    ans = max(ans, cum_right[i] + max_left[N - 2 - i] - x[i] * 2)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
def hanten(C, X, V):
	return (C*np.ones(X.shape[0])-X)[::-1], V[::-1]

def solv(N, C, X, V):
	#print(solv1(N, C, X, V), solv2(N, C, X, V))
	return max(solv1(N, C, X, V), solv2(N, C, X, V))

def solv1(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
	return r

def solv2(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	ibuf = 0
	for i in range(N):
		rbuf += V[i] - 2*(X[i]-pos)
		pos = X[i]
		if(pos > (C-max(X[0], (C-X[-1])))/3):
			break
		if(rbuf > r):
			r = rbuf
			ibuf = i
			#print(r)
	Vbuf = V.copy()
	for i in range(ibuf+1):
		Vbuf[i] = 0
	Xbuf, Vbuf = hanten(C, X, Vbuf)
	#print(r)
	r += solv1(N, C, Xbuf, Vbuf)
	#print(r, Xbuf, Vbuf)
	return r
	
def main():
	buf = input().split()
	N = int(buf[0])
	C = int(buf[1])
	X = np.zeros(N)
	V = np.zeros(N)
	for i in range(N):
		buf = input().split()
		X[i] = int(buf[0])
		V[i] = int(buf[1])
	
	r1 = solv(N, C, X, V)
	X, V = hanten(C, X, V)
	r2 = solv(N, C, X, V)
	print(int(max(r1, r2)))

main()

Submission status: Wrong Answer

Solution code:
import numpy as np
def hanten(C, X, V):
	return (C*np.ones(X.shape[0])-X)[::-1], V[::-1]

def solv(N, C, X, V):
	#print(solv1(N, C, X, V), solv2(N, C, X, V))
	return max(solv1(N, C, X, V), solv2(N, C, X, V))

def solv1(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
	return r

def solv2(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	ibuf = 0
	for i in range(N):
		rbuf += V[i] - 2*(X[i]-pos)
		pos = X[i]
		if(pos > C/3):
			break
		if(rbuf > r):
			r = rbuf
			ibuf = i
			#print(r)
	Vbuf = V.copy()
	for i in range(ibuf+1):
		Vbuf[i] = 0
	Xbuf, Vbuf = hanten(C, X, Vbuf)
	#print(r)
	r += solv1(N, C, Xbuf, Vbuf)
	#print(r, Xbuf, Vbuf)
	return r
	
def main():
	buf = input().split()
	N = int(buf[0])
	C = int(buf[1])
	X = np.zeros(N)
	V = np.zeros(N)
	for i in range(N):
		buf = input().split()
		X[i] = int(buf[0])
		V[i] = int(buf[1])
	
	r1 = solv(N, C, X, V)
	X, V = hanten(C, X, V)
	r2 = solv(N, C, X, V)
	print(int(max(r1, r2)))

main()

Submission status: Wrong Answer

Solution code:
import numpy as np
def hanten(C, X, V):
	return (C*np.ones(X.shape[0])-X)[::-1], V[::-1]

def solv(N, C, X, V):
	#print(solv1(N, C, X, V), solv2(N, C, X, V))
	return max(solv1(N, C, X, V), solv2(N, C, X, V))

def solv1(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
	return r

def solv2(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	ibuf = 0
	for i in range(N):
		rbuf += V[i] - 2*(X[i]-pos)
		pos = X[i]
		if(pos > C/2):
			break
		if(rbuf > r):
			r = rbuf
			ibuf = i
			#print(r)
	Vbuf = V.copy()
	for i in range(ibuf+1):
		Vbuf[i] = 0
	Xbuf, Vbuf = hanten(C, X, Vbuf)
	#print(r)
	r += solv1(N, C, Xbuf, Vbuf)
	#print(r, Xbuf, Vbuf)
	return r
	
def main():
	buf = input().split()
	N = int(buf[0])
	C = int(buf[1])
	X = np.zeros(N)
	V = np.zeros(N)
	for i in range(N):
		buf = input().split()
		X[i] = int(buf[0])
		V[i] = int(buf[1])
	
	r1 = solv(N, C, X, V)
	X, V = hanten(C, X, V)
	r2 = solv(N, C, X, V)
	print(int(max(r1, r2)))

main()

Submission status: Wrong Answer

Solution code:
import numpy as np
def hanten(C, X, V):
	return (C*np.ones(X.shape[0])-X)[::-1], V[::-1]

def solv(N, C, X, V):
	print(solv1(N, C, X, V), solv2(N, C, X, V))
	return max(solv1(N, C, X, V), solv2(N, C, X, V))

def solv1(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
	return r

def solv2(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	ibuf = 0
	for i in range(N):
		rbuf += V[i] - 2*(X[i]-pos)
		pos = X[i]
		if(pos > C/2):
			break
		if(rbuf > r):
			r = rbuf
			ibuf = i
			#print(r)
	Vbuf = V.copy()
	for i in range(ibuf+1):
		Vbuf[i] = 0
	Xbuf, Vbuf = hanten(C, X, Vbuf)
	#print(r)
	r += solv1(N, C, Xbuf, Vbuf)
	#print(r, Xbuf, Vbuf)
	return r
	
def main():
	buf = input().split()
	N = int(buf[0])
	C = int(buf[1])
	X = np.zeros(N)
	V = np.zeros(N)
	for i in range(N):
		buf = input().split()
		X[i] = int(buf[0])
		V[i] = int(buf[1])
	
	r1 = solv(N, C, X, V)
	X, V = hanten(C, X, V)
	r2 = solv(N, C, X, V)
	print(int(max(r1, r2)))

main()

Submission status: Wrong Answer

Solution code:
import numpy as np
def hanten(C, X, V):
	return (C*np.ones(X.shape[0])-X)[::-1], V[::-1]

def solv(N, C, X, V):
	return max(solv1(N, C, X, V), solv2(N, C, X, V))

def solv1(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
	return r

def solv2(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	ibuf = 0
	for i in range(N):
		rbuf += V[i] - 2*(X[i]-pos)
		pos = X[i]
		if(rbuf > r and (-(C-X[i]) < - 2*(X[i]-pos))):
			r = rbuf
			ibuf = i
			#print(r)
		if(pos > C/3):
			break
	Vbuf = V.copy()
	for i in range(ibuf+1):
		Vbuf[i] = 0
	Xbuf, Vbuf = hanten(C, X, Vbuf)
	#print(r)
	r += solv1(N, C, Xbuf, Vbuf)
	#print(r, Xbuf, Vbuf)
	return r
	
def main():
	buf = input().split()
	N = int(buf[0])
	C = int(buf[1])
	X = np.zeros(N)
	V = np.zeros(N)
	for i in range(N):
		buf = input().split()
		X[i] = int(buf[0])
		V[i] = int(buf[1])
	
	r1 = solv(N, C, X, V)
	X, V = hanten(C, X, V)
	r2 = solv(N, C, X, V)
	print(int(max(r1, r2)))

main()

Submission status: Wrong Answer

Solution code:
import numpy as np
def hanten(C, X, V):
	return (C*np.ones(X.shape[0])-X)[::-1], V[::-1]

def solv(N, C, X, V):
	#print(solv1(N, C, X, V), solv2(N, C, X, V))
	return max(solv1(N, C, X, V), solv2(N, C, X, V))

def solv1(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	for i in range(N):
		rbuf += V[i] - (X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
	return r

def solv2(N, C, X, V):
	r = 0
	rbuf = 0
	pos = 0
	ibuf = 0
	for i in range(N):
		rbuf += V[i] - 2*(X[i]-pos)
		pos = X[i]
		if(rbuf > r):
			r = rbuf
			ibuf = i
			#print(r)
		if(pos > C/2):
			break
	Vbuf = V.copy()
	for i in range(ibuf+1):
		Vbuf[i] = 0
	Xbuf, Vbuf = hanten(C, X, Vbuf)
	#print(r)
	r += solv1(N, C, Xbuf, Vbuf)
	#print(r, Xbuf, Vbuf)
	return r
	
def main():
	buf = input().split()
	N = int(buf[0])
	C = int(buf[1])
	X = np.zeros(N)
	V = np.zeros(N)
	for i in range(N):
		buf = input().split()
		X[i] = int(buf[0])
		V[i] = int(buf[1])
	
	r1 = solv(N, C, X, V)
	X, V = hanten(C, X, V)
	r2 = solv(N, C, X, V)
	print(int(max(r1, r2)))

main()