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:
# -*- coding: utf-8 -*-

N, C = map(int, input().split())
sushi = [tuple(map(int, input().split())) for _ in range(N)]
sushi_pos = [x for x, v in sushi]
sushi_cal = [v for x, v in sushi]

ans = 0
for xa_index in range(N + 1): # for init

    # xa_index は初期位置か各寿司の位置
    if xa_index == N:
        xa = 0
        fa = 0
    else:
        xa = sushi_pos[xa_index]
        fa = sum(sushi_cal[:xa_index+1])

    # xb_index も同様に初期位置か各寿司の位置
    for xb_index in range(xa_index + 1, N + 1):
        if xb_index == N:
            xb = 0
            fb = 0
        else:
            xb = sushi_pos[xb_index]
            fb = sum(sushi_cal[xb_index:])

        # 消費カロリーは初期位置が含まれる場合一本道となる
        if xa == 0 or xb == 0:
            syohi = max(xa, xb)                                         # 一本道
        else:
            syohi = min(2 * xa + abs(C - xb), xa + 2 * abs(C - xb))     # 来た道を引き返すケース
        ans = max(ans, fa + fb - syohi)
        # print(xa_index, xb_index, xa, xb, fa, fb, syohi, fa+fb-syohi)
print(ans)

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+=[x]
  V+=[v]

ans = 0
ruiseki=[0]
for i in range(N):
  ruiseki+=[ruiseki[-1]+V[i]]
  ans = max(ans, ruiseki[-1]-X[i])

ruiseki=[0]
for i in range(N):
  ruiseki+=[ruiseki[-1]+V[N-1-i]]
  ans = max(ans, ruiseki[-1]-(C-X[N-1-i]))

print(ans)

Submission status: Wrong Answer

Solution code:
'''
  入力を読み込む
  n c
  x_1 v_1
  x_2 v_2

  x_n v_n
'''
n, c = map(int, input().split())

x = [0 for i in range(n + 1)]
v = [0 for i in range(n + 1)]
for i in range(1, n + 1):               # 添字を問題と合わせるために1から開始する
    x[i], v[i] = map(int, input().split())

# まず、反時計回りで移動した場合の各寿司のところまで行った時の
# カロリーの最大値を求める。
# ccmaxcal = [0 for i in range(n + 1)]
ccmaxcal = [[0 for i in range(n + 1)] for j in range(2)]
sumv = 0

for i in reversed(range(1, n + 1)):     # nから1まで降順でリストを作る
    sumv = sumv + v[i]                  # 摂取できるカロリーの累積値
    cal = sumv - (c - x[i])
    if i == n:
        ccmaxcal[0][i] = cal
        ccmaxcal[1][i] = c - x[i]
    else:
        if cal <= ccmaxcal[0][i + 1]:
            ccmaxcal[0][i] = ccmaxcal[0][i + 1]
            ccmaxcal[1][i] = ccmaxcal[1][i + 1]
        else:
            ccmaxcal[0][i] = cal
            ccmaxcal[1][i] = c - x[i]

#        ccmaxcal[i] = max([cal, ccmaxcal[i + 1]])

#     print("i       :", i)
#     print("cal     :", cal)
# print("ccmaxcal:", ccmaxcal)

# 時計回りに1つずつ寿司を食べていく。その際に、その場で停止する場合と、逆回転を始めて
# さらに寿司を食べた場合の摂取カロリーを比較して、カロリーが高い方を残す。
maxcal = 0
sumv = 0
for i in range(n + 1):
    sumv = sumv + v[i]
    cal_stop = sumv - x[i]
    if i == n:
        maxcal = max([maxcal, cal_stop])
    else:
        cal2 = cal_stop + ccmaxcal[0][i + 1] - x[i]
        cal3 = cal_stop + ccmaxcal[0][i + 1] - ccmaxcal[1][i + 1]
        maxcal = max([maxcal, cal_stop, cal2, cal3])
#    print("i       :", i)
#    print("sumv    :", sumv)
#    print("cal_stop:", cal_stop)
#    print("maxcal  :", maxcal)

print(maxcal)

Submission status: Wrong Answer

Solution code:
'''
  入力を読み込む
  n c
  x_1 v_1
  x_2 v_2

  x_n v_n
'''
n, c = map(int, input().split())

x = [0 for i in range(n + 1)]
v = [0 for i in range(n + 1)]
for i in range(1, n + 1):               # 添字を問題と合わせるために1から開始する
    x[i], v[i] = map(int, input().split())

# まず、反時計回りで移動した場合の各寿司のところまで行った時の
# カロリーの最大値を求める。
ccmaxcal = [0 for i in range(n + 1)]
sumv = 0

for i in reversed(range(1, n + 1)):     # nから1まで降順でリストを作る
    sumv = sumv + v[i]                  # 摂取できるカロリーの累積値
    cal = sumv - (c - x[i])
    if i == n:
        ccmaxcal[i] = cal
    else:
        ccmaxcal[i] = max([cal, ccmaxcal[i + 1]])

#     print("i       :", i)
#     print("cal     :", cal)
#     print("ccmaxcal:", ccmaxcal)

# 時計回りに1つずつ寿司を食べていく。その際に、その場で停止する場合と、逆回転を始めて
# さらに寿司を食べた場合の摂取カロリーを比較して、カロリーが高い方を残す。
maxcal = 0
sumv = 0
for i in range(n + 1):
    sumv = sumv + v[i]
    cal_stop = sumv - x[i]
    if i == n:
        maxcal = max([maxcal, cal_stop])
    else:
        cal_cc = cal_stop + ccmaxcal[i + 1] - x[i]
        maxcal = max([maxcal, cal_stop, cal_cc])
#    print("i       :", i)
#    print("sumv    :", sumv)
#    print("cal_stop:", cal_stop)
#    print("maxcal  :", maxcal)

print(maxcal)

Submission status: Wrong Answer

Solution code:
'''
  入力を読み込む
  n c
  x_1 v_1
  x_2 v_2

  x_n v_n
'''
n, c = map(int, input().split())

x = [0 for i in range(n + 1)]
v = [0 for i in range(n + 1)]
for i in range(1, n + 1):               # 添字を問題と合わせるために1から開始する
    x[i], v[i] = map(int, input().split())

# まず、反時計回りで移動した場合の各寿司のところまで行った時の
# カロリーの最大値を求める。
ccmaxcal = [0 for i in range(n + 1)]
sumv = 0

for i in reversed(range(1, n + 1)):     # nから1まで降順でリストを作る
    sumv = sumv + v[i]                  # 摂取できるカロリーの累積値
    cal = sumv - (c - x[i])
    if i == n:
        ccmaxcal[i] = cal
    else:
        ccmaxcal[i] = max([cal, ccmaxcal[i + 1]])

    print("i       :", i)
    print("cal     :", cal)
    print("ccmaxcal:", ccmaxcal)

# 時計回りに1つずつ寿司を食べていく。その際に、その場で停止する場合と、逆回転を始めて
# さらに寿司を食べた場合の摂取カロリーを比較して、カロリーが高い方を残す。
maxcal = 0
sumv = 0
for i in range(n + 1):
    sumv = sumv + v[i]
    cal_stop = sumv - x[i]
    if i == n:
        maxcal = max([maxcal, cal_stop])
    else:
        cal_cc = cal_stop + ccmaxcal[i + 1] - x[i]
        maxcal = max([maxcal, cal_stop, cal_cc])
    print("i       :", i)
    print("sumv    :", sumv)
    print("cal_stop:", cal_stop)
    print("maxcal  :", maxcal)

print(maxcal)

Submission status: Wrong Answer

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

    xv.append(tuple(map(int,input().split())))

a=[0]*n
a[0]=xv[0][1]-xv[0][0]
for i in range(1,n):
    a[i]=a[i-1]+xv[i][1]-xv[i][0]+xv[i-1][0]


b=[0]*n
b[n-1]=xv[n-1][1]-(c-xv[n-1][0])
for i in range(n-2,-1,-1):
    b[i]=b[i+1]+xv[i][1]-(c-xv[i][0])+(c-xv[i+1][0])

amax=[0]*n
bmax=[0]*n
amax[0],bmax[n-1]=max(0,a[0]),max(0,b[n-1])
for i in range(1,n):
    amax[i]=max(a[i],amax[i-1])
for i in range(n-2,0,-1):
    bmax[i]=max(b[i],bmax[i+1])

ans=0
for i in range(n):
    if i==n-1:
        ans=max(ans,amax[i])
    else:
        ans=max(ans,amax[i]+bmax[i+1]-xv[i][0])

for i in range(n-1,-1,-1):
    if i==0:
        ans=max(ans,bmax[i])
    else:
        ans=max(ans,amax[i-1]+bmax[i]-(c-xv[i][0]))
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, C = map(int, input().split())
X,V = [],[]
for _ in range(N):
    x,v = map(int, input().split())
    X.append(x)
    V.append(v)

r1,r2,l1,l2 = [0],[0],[0],[0]
sum = 0
#原点から時計回りに行ってくる、行って戻ってくるパターン。前の奴と比較して、それより小さかったら無視
for i in range(N):
    sum += V[i]
    #print(r1, r2)
    r1.append(max(r1[i], sum-X[i]))
    r2.append(max(r2[i], sum-2*X[i]))
sum = 0
for i in range(N,0,-1):
    #print('hogehoge',i,V[i-1],X[i-1])
    sum += V[i-1]
    #print(l1, l2)
    l1.append(max(l1[-1], sum-(C-X[i-1])))
    l2.append(max(l2[-1], sum-2*(C-X[i-1])))
print(l1, l2)
a = max(np.array(r1) + np.array(l2)[::-1])
b = max(np.array(r2) + np.array(l1)[::-1])
print(max(a,b))

Submission status: Wrong Answer

Solution code:
import numpy as np
N, C = [int(x) for x in input().split()]
left_sushi = np.array([ [int(x) for x in input().split()] for i in range(N)])
right_sushi = np.copy(left_sushi[::-1] )
right_sushi[:, 0] = C - right_sushi[:, 0]

left_gain = np.zeros(N)
right_gain = np.zeros(N)

for i in range(N):
    left_gain[i] = sum(left_sushi[:i+1, 1] ) - left_sushi[i, 0]
    right_gain[i] = sum(right_sushi[:i+1, 1] ) - right_sushi[i, 0]
left_round_gain = left_gain - left_sushi[:, 0]
right_round_gain = right_gain - right_sushi[:, 0]

sum_gain = 0
max_sum_gain = 0

left_gain = [0] + left_gain.tolist()
left_round_gain = left_round_gain.tolist()
right_gain = [0] + right_gain.tolist()
right_round_gain = right_round_gain.tolist()

# max_left_gain = max( left_gain)
# max_left_round_gain = 0
# max_right_gain = max( right_gain )
# max_right_round_gain = 0

for i in range( N+1):
    sum_gain = max( max(left_round_gain[:i]+[0]) + max(right_gain[:N - i]+[0]), sum_gain)
    sum_gain = max( max(right_round_gain[:i]+[0]) + max(left_gain[:N - i]+[0]), sum_gain)

print( int(sum_gain) )

Submission status: Wrong Answer

Solution code:
N,C = map(int,input().split())
X = [0]*N
V = [0]*N
XX = [0]*N
VV = [0]*N
for i in range(N):
    X[i],V[i] = map(int,input().split())
    XX[N-i-1] = C-X[i]
    VV[N-i-1] = VV[i]
from numpy import array as arr
X = arr(X)
V = arr(V)
XX = arr(XX)
VV = arr(VV)
a = []
b = []
for i in range(N):
    x = X[i]
    v = V[:i+1].sum()
    xx = XX[i]
    vv = VV[:i+1].sum()
    a.append(v-x)
    b.append(vv-xx)
aa = arr([0]*N)
bb = arr([0]*N)
for i in range(N):
    aa[i] = max(a[:i+1])
    bb[i] = max(b[:i+1])
for i in range(N-1):
    x = 2*(C-X[-i-1])
    v = V[-i-1:].sum()
    xx = 2*(C-XX[-i-1])
    vv = V[-i-1:].sum()
    a.append(v-x+aa[N-i-2])
    b.append(vv-xx+bb[N-i-2])
print(max(max(a),max(b)))

Submission status: Wrong Answer

Solution code:
n, c = map(int, input().split())
x = [0 for i in range(n)]
v = [0 for i in range(n)]
hc = c // 2
rights = 0
lefts = 0
maxrx = 0
maxrv = 0
maxlx = 0
maxlv = 0
for i in range(n):
    x[i], v[i] = map(int, input().split())
for i in range(n):
    if x[i] < hc:
        rights += v[i]
        if maxrv < rights - 2*x[i]:
            maxrx = x[i]
            maxrv = rights - 2*x[i]

    else:
        break

for i in range(n-1, -1, -1):
    if x[i] >= hc:
        lefts += v[i]
        if maxlv < lefts - (c - x[i]):
            maxlx = x[i]
            maxlv = lefts - (c - x[i])
    else:
        break

if maxlv == 0:
    print(maxrv+maxrx)
elif maxrx > (c-maxlx):
    print(maxrv+maxlv+maxrx-(c-maxlx))
else:
    print(maxrv+maxlv)

Submission status: Wrong Answer

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

x = [0]*N
rev_x = [0]*N
v = [0]*N

T = [0]*N
Trev = [0]*N

Tall = []

#
'''
for i in range(N):
  a,b = list(map(int,input().split()))
    x.append(a)
    rev_x.append(C - a)
    v.append(b)
'''
#


a = [list(map(int,input().split())) for i in range(N)]
x,v = [list(i) for i in zip(*a)]
rev_x = [C - i for i in x]

rev_x.reverse()


T = [sum(v[0:i+1]) - x[i] for i in range(N)]
Tmax = [max([T[j] for j in range(i+1)]) for i in range(N)]
Tmax_w = [max([T[j]-x[j] for j in range(i+1)]) for i in range(N)]

v.reverse()

Trev= [sum(v[0:i+1]) - rev_x[i] for i in range(N)]
Trevmax = [max([Trev[j] for j in range(i+1)]) for i in range(N)]
Trevmax_w = [max([T[j]-rev_x[j] for j in range(i+1)]) for i in range(N)]

Tall = max(T + Trev)

Tall_1 = max([Tmax[i] + Trevmax_w[N - 1 - i] for i in range(N)])
Tall_2 = max([Tmax_w[i] + Trevmax[N - 1 - i] for i in range(N)])

Tmax = max(Tall,Tall_1,Tall_2)
if Tmax < 0:
    Tmax = 0
print(Tmax)

Submission status: Wrong Answer

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

x = [0]*N
rev_x = [0]*N
v = [0]*N

T = [0]*N
Trev = [0]*N

Tall = []

#
'''
for i in range(N):
  a,b = list(map(int,input().split()))
    x.append(a)
    rev_x.append(C - a)
    v.append(b)
'''
#

a = [list(map(int,input().split())) for i in range(N)]
x,v = [list(i) for i in zip(*a)]
rev_x = [C - i for i in x]

rev_x.reverse()

for i in range(N):
    T.append(sum(v[0:i+1]) - x[i])

v.reverse()

for i in range(N):
    Trev.append(sum(v[0:i+1]) - rev_x[i])

Tall = T + Trev

for i in range(N):
    for j in range(N - i - 1):
        Tall.append(T[i] + Trev[j] - min(x[i], rev_x[j]))

Tmax = max(Tall)
if Tmax < 0:
    Tmax = 0
print(Tmax)

Submission status: Wrong Answer

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

max_c = 0

for i in range(N):
#    print("------")
#    print(i)
    c = 0
    x_tmp = x_list.copy()
    v_tmp = v_list.copy()
    for j in range(i):
        c += v_tmp[j]
#        print(v_tmp[j],"を食べた")
        c -= x_tmp[j+1] - x_tmp[j]
#        print(c)
        if max_c < c:
            max_c = c
    
    x_tmp = x_tmp[i:]
    v_tmp = v_tmp[i:]
    
    v_tmp = v_tmp[::-1]
    
    x_tmp = [x_tmp[0]+C] + x_tmp[1:][::-1]
    
#    print(x_tmp)
#    print(v_tmp)
    
    for j in range(N - i):
        c += v_tmp[j]
#        print(v_tmp[j],"を食べた")
        c -= x_tmp[j] - x_tmp[j+1]
#        print(c)
        if max_c < c:
            max_c = c
            
print(max_c)

Submission status: Wrong Answer

Solution code:
N, C = [int(r) for r in raw_input().split()]
x = []
v = []
for i in range(N):
  xi, vi = [int(r) for r in raw_input().split()]
  x.append(xi)
  v.append(vi)
b = [0]
sc = 0
xc = 0
su = 0
xu = C
for i in range(N):
  sc += v[i] - (x[i] - xc)
  xc = x[i]
  b.append(sc)
  su += v[N-1-i] - (xu - x[N-1-i])
  xu = x[N-1-i]
  b.append(su)
print(max(b))

Submission status: Wrong Answer

Solution code:
n, c = map(int, input().split())
ls = [map(int, input().split()) for i in range(n)]
acc = 0
temp = [0]
for x, v in ls:
    acc += v
    temp.append(acc-x)
print(max(temp))

Submission status: Wrong Answer

Solution code:
n,c=map(int, input().split())
xv=[]
for _ in range(n):
    xv.append(list(map(int, input().split())))
xv.sort(key=lambda x:x[0])
score=[0] 
score_back=[0] 
score_r=[0]
score_rback=[0] 

tmp=0
tmpr=0
for i in range(n):
    tmp+=xv[i][1]
    tmpr=xv[-(i+1)][1]
    
    score.append(tmp-xv[i][0])
    score_r.append(tmpr-(c-xv[-(i+1)][0]))
    score_back.append(tmp-2*xv[i][0])
    score_rback.append(tmpr-2*(c-xv[-(i+1)][0]))
#print(score_back)
#print(score_r)
#print(score_rback)
#print(score)
maxscore=0
for i in range(n+1):
    tmp=score_back[i]
    for j in range(n-i+1):
        scorei=tmp+score_r[j]
#        print(scorei)
        if scorei>maxscore:
            maxscore=scorei
for i in range(n+1):
    tmp=score_rback[i]
    for j in range(n-i+1):
        scorei=tmp+score[j]
#        print(scorei)
        if scorei>maxscore:
            maxscore=scorei
print(maxscore)

Submission status: Wrong Answer

Solution code:
n, c = map(int,input().split())
x = []
v = []
for i in range(n):
    s, t = input().split()
    x.append(int(s))
    v.append(int(t))
vl1 = [0]
vl2 = [0]
suml = 0
vr1 = [0]
vr2 = [0]
sumr = 0
for i in range(n):
    suml += v[i]
    #3項演算子ver
    vl1.append(vl1[i] if vl1[i] > suml - x[i] else suml - x[i])
    vl2.append(vl2[i] if vl2[i] > suml - x[i]*2 else suml - x[i]*2)
    #vl1.append(max(vl1[i],suml - x[i]))
    #vl2.append(max(vl2[i],suml - x[i]*2))
    sumr += v[n-1-i]
    vr1.append(vr1[i] if vr1[i] > sumr - c + x[n-1-i] else sumr - c + x[n-1-i])
    vr2.append(vr2[i] if vr2[i] > sumr - (c - x[n-1-i])*2 else sumr - (c + x[n-1-i])*2)
    #vr1.append(max(vr1[i],sumr - c + x[n-1-i]))
    #vr2.append(max(vr2[i],sumr - (c - x[n-1-i])*2))
print(max( [max(vl1[i]+vr2[n-i],vr1[i]+vl2[n-i]) for i in range(n+1)] ))

Submission status: Wrong Answer

Solution code:
n, c = map(int,input().split())
x = []
v = []
for i in range(n):
    s, t = input().split()
    x.append(int(s))
    v.append(int(t))
vl1 = [0]
vl2 = [0]
suml = 0
vr1 = [0]
vr2 = [0]
sumr = 0
for i in range(n):
    suml += v[i]
    #vl1.append(vl1[i] if vl1[i] > suml - x[i] else suml - x[i])
    #vl2.append(vl2[i] if vl2[i] > suml - x[i]*2 else suml - x[i]*2)
    vl1.append(max(vl1[i],suml - x[i]))
    vl2.append(max(vl2[i],suml - x[i]*2))
    sumr += v[n-1-i]
    #vr1.append(vr1[i] if vr1[i] > sumr - c + x[n-1-i] else sumr - c + x[n-1-i])
    #vr2.append(vr2[i] if vr2[i] > sumr - (c - x[n-1-i])*2 else sumr - (c + x[n-1-i])*2)
    vr1.append(max(vr1[i],sumr - c + x[n-1-i]))
    vr2.append(max(vr2[i],sumr - (c - x[n-1-i])*2))
print(max( [max(vl1[i]+vr2[n-i],vr1[i]+vl2[n-i]) for i in range(n)] ))

Submission status: Wrong Answer

Solution code:
n, c = input().split()
n = int(n)
c = int(c)
x = [0]
v = [0]
ss = [0]
s = 0
fs = [0]
f = 0
gs = [0]
g = 0
ps = [0]
p = 0
for i in range(n):
    xx, vv = input().split()
    xx = int(xx)
    vv = int(vv)
    x.append(xx)
    v.append(vv)
    s += vv
    ss.append(s)
    f = s - xx
    fs.append(f)
    if f > g:
        g = f
        p = xx
    gs.append(g)
    ps.append(p)
ans = -10**15
for i in range(-1, -n - 1, -1):
    h1 = ss[-1] - ss[i - 1] - (c - x[i]) 
    h2 = gs[i - 1] + h1
    ans = max(ans, h1)
    ans = max(ans, h2 - ps[i - 1])
    ans = max(ans, h2 - (c - x[i]))
ans = max(ans, gs[n])
print(ans)

Submission status: Wrong Answer

Solution code:
n, c = input().split()
n = int(n)
c = int(c)
x = [0]
v = [0]
ss = [0]
s = 0
fs = [0]
f = 0
gs = [0]
g = 0
ps = [0]
p = 0
for i in range(n):
    xx, vv = input().split()
    xx = int(xx)
    vv = int(vv)
    x.append(xx)
    v.append(vv)
    s += vv
    ss.append(s)
    f = s - xx
    fs.append(f)
    if f > g:
        g = f
        p = xx
    gs.append(g)
    ps.append(p)
ans = -10**15
for i in range(-1, -n - 1, -1):
    h = gs[i - 1] + ss[-1] - ss[i - 1] - (c - x[i]) 
    ans = max(ans, h - ps[i - 1])
    ans = max(ans, h - (c - x[i]))
ans = max(ans, gs[n])
print(ans)

Submission status: Wrong Answer

Solution code:
n, c = input().split()
n = int(n)
c = int(c)
x = [c]
v = [0]
ss = [0]
s = 0
fs = [0]
f = 0
gs = [0]
g = 0
for i in range(n):
    xx, vv = input().split()
    xx = int(xx)
    vv = int(vv)
    x.append(xx)
    v.append(vv)
    s += vv
    ss.append(s)
    f = s - xx
    fs.append(f)
    g = max(g, f)
    gs.append(g)
ans = -10**15
for i in range(0, -n - 1, -1):
    h = gs[i - 1] + ss[-1] - ss[i - 1] - (c - x[i])
    ans = max(ans, h)
print(ans)

Submission status: Wrong Answer

Solution code:
n, c = input().split()
n = int(n)
c = int(c)
x = [0]
v = [0]
sv = [0]
ssv = 0
for i in range(n):
    xx, vv = input().split()
    xx = int(xx)
    vv = int(vv)
    x.append(xx)
    v.append(vv)
    ssv += vv
    sv.append(ssv)
ans = -10**15
for i in range(0, n + 1, 1):
    for j in range(-1, -n +  i - 1, -1):
        e1 = sv[i] - x[i]
        e2 = sv[-1] - sv[j - 1] - (c - x[j])
        e3 = e1 + e2 - x[i]
        e4 = e1 + e2 - (c - x[j])
        ans = max([ans, e1, e2, e3, e4])
print(ans)

Submission status: Wrong Answer

Solution code:
n, c = [int(i) for i in input().split()]
xv = [[int(i) for i in input().split()] for i in range(n)]

xv.insert(0,[0,0])

ans = -100000000
for i in range(n+1):
    for j in range(i+1, n+1):
        e = 0
        e2 = 0
        for k in range(i+1):
            e += xv[k][1]
        e2 = e
        for l in range(n, j-1, -1):
            e += xv[l][1]
        e2 -= xv[k][0]
        e -=max((xv[i-1][0]*2 + (c-xv[j][0])), (xv[i-1][0] + (c-xv[j][0])*2))
        e = max(e2,e)
        if e > ans:
            ans = e

print(max(ans,0))

Submission status: Wrong Answer

Solution code:
N, C = map(int, input().split())
xy = [[int(i) for i in input().split()] for _ in range(N)]
out = 0
def m(xx):
    sum = 0
    c = [0]
    rc = [0]
    for x, cal in xx:
        sum += cal
        c.append(max(c[-1], sum - x))
        rc.append(max(rc[-1], sum - 2 * x))
    return c, rc
r, rr = m(xy)
l, lr = m(xy[::-1])
for w, wr in zip(r, lr[::-1]):
    out = max(out, w+wr)
for w, wr in zip(l, rr[::-1]):
    out = max(out, w+wr)
print(out)

Submission status: Wrong Answer

Solution code:
from collections import deque
N, C = map(int, input().split())

sushi = [tuple(map(int, input().split())) for _ in [0]*N]
normal = [sushi[0][1] - sushi[0][0]]

cur = normal[0]
n_max = max(0, normal[0])

for (s1_pos, s1_cal), (s2_pos, s2_cal) in zip(sushi, sushi[1:]):
    cur = cur + s2_cal - (s2_pos - s1_pos)
    if n_max < cur:
        n_max = cur
    normal.append(n_max)


reverse = [sushi[-1][1] - (C - sushi[-1][0])]

cur = reverse[0]
r_max = max(0, reverse[0])

for (s1_pos, s1_cal), (s2_pos, s2_cal) in zip(sushi[::-1], sushi[-2::-1]):
    cur = cur + s2_cal - (s1_pos - s2_pos)
    if r_max < cur:
        r_max = cur
    reverse.append(r_max)

res = 0
for i, (pos, cal) in enumerate(sushi[:-1]):
    result = max(res, normal[i], normal[i] - pos + reverse[N-2-i])
for i, (pos, cal) in enumerate(sushi[:0:-1]):
    result = max(res, reverse[i], reverse[i] - (C - pos) + normal[N-2-i])
res = max(res, normal[-1], reverse[-1])
 
print(res)

Submission status: Wrong Answer

Solution code:
import numpy as np

def new_state(sushi, x, N, C):
    if sushi.shape[0] == 0:
        return None
    s_c = np.copy(sushi)
    s_cc = np.copy(sushi)
    s_c[:, 0] -= x
    s_cc[:, 0] = C - (s_cc[:, 0] - x)
    
    # 実際に歩いて獲得できるカロリーを計算
    kcal_c = s_c[:, 1] - s_c[:, 0]
    kcal_cc = s_cc[:, 1] - s_cc[:, 0]
    # 目標の寿司を見つける
    dest_c = np.argmax(kcal_c)
    dest_cc = np.argmax(kcal_cc)
    if kcal_c[dest_c] < 0 and kcal_cc[dest_cc] < 0:
        return None
    # 時計回りと反時計回りでどちらがたくさんのカロリーを摂取できるか計算
    #print(s_c[:dest_c+1])
    #print(s_cc[:dest_cc-1:-1])
    result_c = s_c[:dest_c+1][:,1].sum() - s_c[dest_c][0]
    result_cc = s_c[:dest_cc-1:-1][:,1].sum() - s_cc[dest_cc][0]
    
    idx = np.arange(N)
    if result_c > result_cc:
        # 時計回りに回って寿司を食いまくる
        result = result_c
        new_x = s_c[dest_c][0]
        new_state = np.delete(sushi, idx[:dest_c+1], axis=0)
    else:
        result = result_cc
        new_x = s_cc[dest_cc][0]
        new_state = np.delete(sushi, idx[:dest_cc-1:-1], axis=0)
        
    return (result, new_x, new_state)

def get_max(sushi, N, C):
    total = 0
    state = new_state(sushi, 0, N, C)
    while state is not None:
        result, new_x, new_s = state
        total += result
        N = new_s.shape[0]
        state = new_state(new_s, new_x, N, C)
    return total

header = np.array(list(map(int, input().split())))
sushi = []
for i in range(header[0]):
    sushi.append(list(map(int, input().split())))
sushi = np.array(sushi)

N = header[0]
C = header[1]
total = get_max(sushi, N, C)
print(total)

Submission status: Wrong Answer

Solution code:

# coding: utf-8

# In[11]:


line = input().split(' ')
n = int(line[0])
c = int(line[1])


# In[19]:


x = [0] * n
v = [0] * n
for i in range(n):
    line = input().split(' ')
    x[i] = int(line[0])
    v[i] = int(line[1])


# In[20]:


d_clock = [0] * n
d_counter = [0] * n
d_clock[0] = x[0]
d_counter[0] = c - x[n-1]
for i in range(1, n):
    d_clock[i] = x[i] - x[i-1]
    d_counter[i] = x[n-i] - x[n-1-i]


# In[21]:


costClock = 0
costCounter = 0
scoreClock = [0] * n
scoreCounter = [0] * n
for i in range(n):
    costClock += v[i] - d_clock[i]
    scoreClock[i] = costClock
    costCounter += v[n-1-i] - d_counter[i]
    scoreCounter[i] = costCounter


# In[24]:


walkMax = max(max(scoreClock), max(scoreCounter))
print (max(0, walkMax))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
n,c=map(int,input().split())
lane_r=[]
lane_l=[]
for i in range(n):
    a,b=map(int,input().split())
    lane_r.append((a,b))
    lane_l.append((c-a,b))
lane_l=lane_l[::-1]
acm_lane_r=[0]
acm_lane_l=[0]
for i in range(n):
    acm_lane_r.append(acm_lane_r[-1]+lane_r[i][1])
    acm_lane_l.append(acm_lane_l[-1]+lane_l[i][1])
for i in range(1,n+1):
    acm_lane_r[i]-=lane_r[i-1][0]
    acm_lane_l[i]-=lane_l[i-1][0]
mxl=0
mxr=0
p=0
for i in range(n+1):
    if mxl<acm_lane_l[i]:
        mxl=acm_lane_l[i]
        p=lane_l[i-1][0]
    acm_lane_l[i]=(mxl,p)
    if mxr<acm_lane_r[i]:
        mxr=acm_lane_r[i]
        p=lane_r[i-1][0]
    acm_lane_r[i]=(mxr,p)
mx=0
lane_r.insert(0,(0,0))
lane_l.insert(0,(0,0))
for i in range(n+1):
    mx=max(acm_lane_r[n-i][0]+acm_lane_l[i][0]-min(acm_lane_r[n-i][1],acm_lane_l[i][1]),mx)
for i in range(n+1):
    mx=max(acm_lane_l[n-i][0]+acm_lane_r[i][0]-min(acm_lane_l[n-i][1],acm_lane_r[i][1]),mx)
print(mx)

Submission status: Wrong Answer

Solution code:
N, C = map(long, raw_input().split())
Sushi  = [map(long, raw_input().split()) for i in range(N)]
Sushi_rev = []
for s in Sushi[::-1]:
  Sushi_rev.append([C-s[0], s[1]])
Sushi.insert(0L, [0L, 0L])
Sushi_rev.insert(0L, [0L, 0L])
N += 1
ans = 0
cal_clock = []
tmp = 0
for i in Sushi:
  tmp += i[1]
  cal_clock.append(tmp)
cal_anticlock = []
tmp = 0
for i in Sushi_rev:
  tmp += i[1]
  cal_anticlock.append(tmp)
ans = 0
for a in range(N):
  for b in range(N-a):
    oa = Sushi[a][0]
    ob = Sushi_rev[b][0]
    eaten = cal_clock[a]+cal_anticlock[b]
    get_cal_oab = eaten - (2 * oa +ob)
    get_cal_oba = eaten - (2 * ob +oa)
    get_cal = eaten - (oa+ob) - (oa if oa < ob else ob)
    if get_cal > ans:
      ans = get_cal

Submission status: Wrong Answer

Solution code:
# coding: utf-8

N, C = map(int, input().split())
x_list = []
v_list = []
for i in range(N):
    xi, vi = map(int, input().split())
    x_list.append(xi)
    v_list.append(vi)

e_clock = [0] * N
e_counter = [0] * N
e_clock0 = [0] * N # 時計回りしたあと、初期位置に戻った状態
e_counter0 = [0] * N # 反時計回りしたあと、初期位置に戻った状態

# 時計回り、反時計回りはそれぞれ多くて１回ずつである
# (何度も切り替えしても、距離だけ増えて効率的でない

# 時計回りに回ったときの各地点までの取得エネルギーで最大値を格納する
cal = 0
e = 0
e0 = 0
for i in range(N):
    cal += v_list[i]
    x = x_list[i]
    e = max(e, cal - x)
    e0 = max(e0, cal - 2*x)

    e_clock[i] = e
    e_clock0[i] = e0


# 反時計回りに回ったときの各地点までの取得エネルギーで最大値を格納する
cal = 0
e = 0
e0 = 0
for i in range(N-1, -1 ,-1):
    cal += v_list[i]
    x = C - x_list[i]
    e = max(e, cal - x)
    e0 = max(e0, cal - 2*x)
    e_counter[i] = e
    e_counter0[i] = e0

ans = 0

for i in range(N-1):
    # 時計回りのあと、反時計回りをしたとき
    e1 = e_clock0[i] + e_counter[i+1]
    # 反時計回りのあと、時計回りをしたとき
    e2 = e_counter0[i+1] + e_clock[i]

    ans = max(ans, e1, e2)

# 上はrange(N-1)のforなので
# 時計回りと反時計回りのそれぞれでまるまる１周した場合も加える
ans = max(ans, e_clock[N-1], e_counter[N-1])

print(ans)

Submission status: Wrong Answer

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


def solve():
    normal = [sushi[0][1] - sushi[0][0]]
    n_append = normal.append
    cur = normal[0]
    n_max = normal[0]
    for (s1_pos, s1_cal), (s2_pos, s2_cal) in zip(sushi, sushi[1:]):
        cur += s2_cal - (s2_pos - s1_pos)
        if n_max < cur:
            n_max = cur
        n_append(n_max)

    reverse = [sushi[-1][1] - (C-sushi[-1][0])]
    r_append = reverse.append
    cur = reverse[0]
    r_max = max(0, reverse[0])
    for (s1_pos, s1_cal), (s2_pos, s2_cal) in zip(sushi[::-1], sushi[-2::-1]):
        cur += s2_cal - (s1_pos-s2_pos)
        if r_max < cur:
            rmax = cur
        r_append(r_max)

    result= 0
    for i, (pos, cal) in enumerate(sushi[:-1]):
        result = max(result, normal[i], normal[i]-pos+reverse[N-2-i])
    for i, (pos, cal) in enumerate(sushi[:0:-1]):
        result = max(result, reverse[i], reverse[i]-(C-pos)+normal[N-2-i])
    result = max(result, normal[-1], reverse[-1])

    print(result)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
# coding: utf-8

N, C = map(int, input().split())
x_list = []
v_list = []
for i in range(N):
    xi, vi = map(int, input().split())
    x_list.append(xi)
    v_list.append(vi)

e_clock = [0] * N
e_counter = [0] * N

# 時計回り、反時計回りはそれぞれ多くて１回ずつである
# (何度も切り替えしても、距離だけ増えて効率的でない

# 時計回りに回ったときの各地点での取得エネルギー
e_clock[0] = v_list[0] - x_list[0]
for i in range(2, N):
    e_clock[i-1] = e_clock[i-2] + v_list[i-1] - (x_list[i-1] - x_list[i-2])

# 反時計回りに回ったときの各地点での取得エネルギー
e_counter[N-1] = v_list[N-1] - (C-x_list[N-1])
for i in range(N-2, -1, -1):
    e_counter[i] = e_counter[i+1] + v_list[i] - (x_list[i+1]-x_list[i])

res = 0

# 時計回りの後に、切り替えして反時計回りに回ったときの各地点での取得エネルギーを算出
for i in range(N):
    # 反時計回りの切り返しがない場合に獲得エネルギーが高い場合も考慮
    if e_clock[i] > res:
        res = e_clock[i]
    for j in range(i+1, N):
        e = e_clock[i] + e_counter[j] - x_list[i]
        if e > res:
            res = e


# 反時計回りの後に、切り替えして時計回りに回ったときの各地点での取得エネルギー
for i in range(N-1,0,-1):
    # 時計回りの切り返しがない場合に獲得エネルギーが高い場合も考慮
    if e_counter[i] > res:
        res = e_counter[i]
    for j in range(0, i, 1):
        e = e_counter[i] + e_clock[j] - (C-x_list[i])
        if e > res:
            res = e


print(res)

Submission status: Wrong Answer

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

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

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


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

print(max_cal)