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:
import sys
input=sys.stdin.readline

def main():
    N,C = map(int, input().split())
    X = []
    for _ in range(N):
        X.append(list(map(int, input().split())))
    
    cw = [0] # 時計回りに数えて i 番目までを食べた時に摂取できるカロリー
    ccw = [0] # 反時計回り
    for i in range(N):
        cw.append(cw[i] + X[i][1])
        ccw.append(ccw[i] + X[N-1-i][1])
    for i in range(N):
        cw[i+1] -= X[i][0]
        ccw[i+1] -= C - X[N-1-i][0]

    ma = 0
    ga = []
    for i in range(0, N+1):
        ma = max(ma, cw[i])
        ga.append(ma)
    ma = 0
    gb = []
    for i in range(0, N+1):
        ma = max(ma, ccw[i])
        gb.append(ma)

    ans = 0
    for bi in range(N+1):
        tmp = ccw[bi]
        a = ga[N-bi]
        tmp += a
        ai = ga.index(a)
        if bi > 0 and ai > 0:
            tmp -= X[ai-1][0]
        ans = max(ans, tmp)

    for ai in range(N+1):
        tmp = cw[ai]
        b = gb[N-ai]
        tmp += b
        bi = gb.index(b)
        if bi > 0 and ai > 0:
            tmp -= C - X[N-bi][0]
        ans = max(ans, tmp)

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def main():
    N,C = map(int, input().split())
    X = []
    for _ in range(N):
        X.append(list(map(int, input().split())))
    
    cw = [0] # 時計回りに数えて i 番目までを食べた時に摂取できるカロリー
    ccw = [0] # 反時計回り
    for i in range(N):
        cw.append(cw[i] + X[i][1])
        ccw.append(ccw[i] + X[N-1-i][1])
    for i in range(N):
        cw[i+1] -= X[i][0]
        ccw[i+1] -= C - X[N-1-i][0]
    
    ans = 0
    # i 番目で向きを変えて、j 番目まで食べる場合を全探索
    # O(n^2) なのでアカンか…？
    for i in range(N):
        cwccw = cw[i]
        ccwcw = ccw[i]
        if i > 0:
            cwccw -= X[i-1][0] # 原点に戻るコスト
            ccwcw -= C - X[N-i][0]
        for j in range(N-i):
            ans = max(ans, cwccw + ccw[j])
            ans = max(ans, ccwcw + cw[j])

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

cp = [0]*n
cp_rev = [0]*n
d = [0]*n

for i in range(n):
    x,v = map(int,input().split())
    d[i] = x
    cp[i] = v
    cp_rev[n-1-i] = v

sm = [0]*n
sm_rev = [0]*n

sm[0] = cp[0] - d[0]
sm_rev[0] = cp_rev[0] - (c - d[n-1])

for i in range(1,n):
    sm[i] = sm[i-1] + cp[i] - d[i] + d[i-1]
    sm_rev[i] = sm_rev[i-1] + cp_rev[i] - (c - d[n-1-i]) + c - d[n-i]

ret_ind = sm.index(max(sm))
ret_ind_rev = sm_rev.index(max(sm_rev)) 

ans = 0

for i in range(ret_ind + 1):
    ans = max(ans, sm[i])
    if n-i-2 >= 0:
        ans = max(ans, sm[i] - d[i] + sm_rev[min(ret_ind_rev,n-i-2)])

for i in range(ret_ind_rev + 1):
    ans = max(ans, sm_rev[i])
    if n-i-2 >= 0:
        ans = max(ans,sm_rev[i] - (c - d[n-1-i]) + sm[min(ret_ind,n-i-2)])

print(ans)

Submission status: Wrong Answer

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

right_turn_sum = [0]
for i in range(N):
    right_turn_sum.append(sushi[i][1]+right_turn_sum[i])
for i in range(N):
    right_turn_sum[i+1] -= sushi[i][0]
right_turn_to_zero = [0]
for i in range(N):
    right_turn_to_zero.append(right_turn_sum[i+1]-sushi[i][0])
"""
print(right_turn_sum)
print(right_turn_to_zero)
"""
left_turn_sum = [0]
for i in range(N):
    left_turn_sum.append(sushi[-1-i][1]+left_turn_sum[i])
for i in range(N):
    left_turn_sum[i+1] -= (C - sushi[-1-i][0])
left_turn_to_zero = [0]
for i in range(N):
    left_turn_to_zero.append(left_turn_sum[i+1]-sushi[-1-i][0])

"""
print(left_turn_sum)
print(left_turn_to_zero)
"""
ans = max(right_turn_sum)
ans = max(left_turn_sum+[ans])
"""
print(ans)
"""
maxi = 0
for i in range(N+1):
    if maxi < right_turn_sum[i]:
        maxi = right_turn_sum[i]
    right_turn_sum[i] = max(maxi, right_turn_sum[i])

maxi = 0
for i in range(N+1):
    if maxi < right_turn_to_zero[i]:
        maxi = right_turn_to_zero[i]
    right_turn_to_zero[i] = max(maxi, right_turn_to_zero[i])

maxi = 0
for i in range(N+1):
    if maxi < left_turn_sum[i]:
        maxi = left_turn_sum[i]
    left_turn_sum[i] = max(maxi, left_turn_sum[i])

maxi = 0
for i in range(N+1):
    if maxi < left_turn_to_zero[i]:
        maxi = left_turn_to_zero[i]
    left_turn_to_zero[i] = max(maxi, left_turn_to_zero[i])
"""
print(right_turn_sum)
print(right_turn_to_zero)
print(left_turn_sum)
print(left_turn_to_zero)
"""

for i in range(N+1):
    ans = max(ans, right_turn_to_zero[i]+left_turn_sum[N-i])

for i in range(N+1):
    ans = max(ans, left_turn_to_zero[i]+right_turn_sum[N-i])
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import*
from numpy import array
n,c=map(int,input().split())

l1=[]
l2=[]
for i in range(n):
    x,v=map(int,input().split())
    l1.append(x)
    l2.append(v)
#print(l1,l2)

ll=array([0]+l1)
lll=array([0]+list(accumulate(l2)))
lr=array([0]+[c-i for i in l1][::-1])
llr=array([0]+list(accumulate(l2[::-1])))
x=lll-ll
y=llr-lr
for i in range(2,n+1):
    x[i]=max(x[i-1],x[i])
    y[i]=max(y[i-1],y[i])
z=array([min(ll[i],lr[i])for i in range(n+1)])
print(max(x[::-1]+y-z))

Submission status: Wrong Answer

Solution code:
from itertools import*
from numpy import array
n,c=map(int,input().split())

l1=[]
l2=[]
for i in range(n):
    x,v=map(int,input().split())
    l1.append(x)
    l2.append(v)
#print(l1,l2)

ll=array([0]+l1)
lll=array([0]+list(accumulate(l2)))
lr=array([0]+[c-i for i in l1][::-1])
llr=array([0]+list(accumulate(l2[::-1])))
x=lll-ll
y=llr-lr
for i in range(1,n+1):
    x[i]=max(x[i-1],x[i])
    y[i]=max(y[i-1],y[i])
z=array([min(ll[i],lr[i])for i in range(n+1)])
print(max(x[::-1]+y-z))

Submission status: Wrong Answer

Solution code:
from itertools import*
from numpy import array
n,c=map(int,input().split())

l1=[]
l2=[]
for i in range(n):
    x,v=map(int,input().split())
    l1.append(x)
    l2.append(v)
#print(l1,l2)

ll=array([0]+l1)
lll=array([0]+list(accumulate(l2)))
lr=array([0]+[c-i for i in l1][::-1])
llr=array([0]+list(accumulate(l2[::-1])))
x=lll-ll
y=llr-lr
for i in range(1,n+1):
    x[i]=max(0,x[i-1],x[i])
    y[i]=max(0,y[i-1],y[i])
z=array([min(ll[i],lr[i])for i in range(n+1)])
print(max(x[::-1]+y-z))

Submission status: Wrong Answer

Solution code:
from itertools import*
n,c=map(int,input().split())

l1=[]
l2=[]
l3=[]
tmp=0
for i in range(n):
    x,v=map(int,input().split())
    l1+=[(x,v+tmp)]
    l2+=[c-x]
    l3.append(v)
    tmp+=v


l3=list(zip(l2[::-1],accumulate(l3[::-1])))[::-1]
ll=list(map(lambda x:x[1]-x[0],l3))
lr=list(map(lambda x:x[1]-x[0],l1))
#print(l3,l1)
#print(ll,lr)
ans=0
for i in range(n):
    c=max(ll[i:]+[0])
    d=max(lr[:i]+[0])
    if c+d>ans:
        ans=c+d
        tmp=i
x=max(lr[:tmp]+[0])
y=max(ll[tmp:]+[0])
if x==0 or y==0:
    print(ans)
else:
    print(ans-min(l1[lr.index(x)][0],l3[ll.index(y)][0]))
#print(min(l1[lr.index(max(lr[:tmp]+[0]))][0],l3[ll.index(max(ll[tmp:]+[0]))][0]))
#print(tmp)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
N, C = map(int, input().split())

posDist = [0]*N
posVal = [0]*N
for i in range(N):
    d, v = list(map(int, input().split()))
    posDist[i] = d
    posVal[i] = d

negDist = [0]*N
negVal = [0]*N
for i, z in enumerate(zip(posDist, posVal)):
    negDist[N-i-1] = z[0]
    negVal[N-i-1] = z[1]


posDist = [0]+posDist
negDist = [0]+negDist

posVal = [0]+list(accumulate(posVal))
negVal = [0]+list(accumulate(negVal))
# print(posDist)
# print(negDist)
# print(posVal)
# print(negVal)
for i, d in enumerate(posDist):
    posVal[i] -= d
for i, d in enumerate(negDist):
    negVal[i] -= d
ans = 0

# print("after")
# print(posVal)
# print(negVal)
for l in range(N+1):
    if negVal[l] < 0:
        continue
    for r in range(N-l+1):
        tmp = posVal[r]+negVal[l]-min(posDist[r], negDist[l])
        #print("l,r->", l, r, tmp)
        ans = max(tmp, ans)
# print(ans)
print(0)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
N, C = map(int, input().split())

posDist = []
posVal = []
for _ in range(N):
    d, v = list(map(int, input().split()))
    posDist.append(d)
    posVal.append(v)

negDist = []
negVal = []
for d, v in zip(reversed(posDist), reversed(posVal)):
    negDist.append(C-d)
    negVal.append(v)


posDist = [0]+posDist
negDist = [0]+negDist

posVal = [0]+list(accumulate(posVal))
negVal = [0]+list(accumulate(negVal))
# print(posDist)
# print(negDist)
# print(posVal)
# print(negVal)
for i, d in enumerate(posDist):
    posVal[i] -= d
for i, d in enumerate(negDist):
    negVal[i] -= d
ans = 0

# print("after")
# print(posVal)
# print(negVal)
for l in range(N+1):
    if negVal[l] < 0:
        continue
    for r in range(N-l+1):
        tmp = posVal[r]+negVal[l]-min(posDist[r], negDist[l])
        #print("l,r->", l, r, tmp)
        ans = max(tmp, ans)
# print(ans)
print(0)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline
N, C = map(int, input().split())

posDist = []
posVal = []
for _ in range(N):
    d, v = list(map(int, input().split()))
    posDist.append(d)
    posVal.append(v)

negDist = []
negVal = []
for d, v in zip(reversed(posDist), reversed(posVal)):
    negDist.append(C-d)
    negVal.append(v)


posDist = [0]+posDist
negDist = [0]+negDist

posVal = [0]+list(accumulate(posVal))
negVal = [0]+list(accumulate(negVal))
# print(posDist)
# print(negDist)
# print(posVal)
# print(negVal)
for i, d in enumerate(posDist):
    posVal[i] -= d
for i, d in enumerate(negDist):
    negVal[i] -= d
ans = 0
# print("after")
# print(posVal)
# print(negVal)
for l in range(N+1):
    if negVal[l] < 0:
        continue
    for r in range(N-l+1):
        tmp = posVal[r]+negVal[l]-posDist[r] - \
            negDist[l]-min(posDist[r], negDist[l])
        #print("l,r->", l, r, tmp)
        ans = max(tmp, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
x = [0]
v = [0]
f = [0] * (n+1)
g = [(0,0)] * (n+1)
dis = 0
for i in range(0,n):
    X,V = map(int,input().split())
    x.append(X)
    v.append(V)
for i in range(0,n):
    f[i+1] = f[i] + v[i+1]
    rv,rd = g[i]
    if f[i+1] - x[i+1] > rv:
        dis = x[i+1]
    g[i+1] = (max(rv,f[i+1] - x[i+1]),dis)

rv,rd = g[n]
ans = max(0,rv)
lv = 0
for b in range(n,0,-1):
    rv,rd = g[b-1]
    lv += v[b]
    dis = min(rd,(c-x[b]))
    ans = max(ans,rv + lv - (c-x[b]) - dis)
print(ans)

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
x = []
v = []
f = []
g = []
dis = 0
for i in range(0,n):
    X,V = map(int,input().split())
    x.append(X)
    v.append(V)
    if i == 0:
        f.append(v[i]-x[i])
        dis = X
        g.append((max(f),dis))
        continue
    f.append(f[i-1]+v[i]-x[i]+x[i-1])
    if f[i] > max(f[0:i]):
        dis = X
    g.append((max(f),dis))
ans = 0
vb = 0
for b in range(n-1,0,-1):
    va , da = g[b-1]
    dis = min((c-x[b]) , da)
    ans = max(ans,vb + v[b]-(c-x[b]) + va - dis)
    vb += v[b]
ans = max(ans,vb + v[0] - (c-x[0]))
va , da = g[n-1]
ans = max(ans,va)
print(ans)

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
x = []
v = []
f = []
g = []
dis = 0
for i in range(0,n):
    X,V = map(int,input().split())
    x.append(X)
    v.append(V)
    if i == 0:
        f.append(v[i]-x[i])
        dis = X
        g.append((max(f),dis))
        continue
    f.append(f[i-1]+v[i]-x[i]+x[i-1])
    if f[i] >= max(f[0:i]):
        dis = X
    g.append((max(f),dis))
ans = 0
vb = 0
for b in range(n-1,0,-1):
    va , da = g[b-1]
    dis = min((c-x[b]) , da)
    ans = max(ans,vb + v[b]-(c-x[b]) + va - dis)
    vb += v[b]
ans = max(ans,vb + v[0] - (c-x[0]))
va , da = g[n-1]
ans = max(ans,va)
print(ans)

Submission status: Wrong Answer

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

rev=[None]*(n+1)
norm=[None]*(n+1)
revmax=[None]*(n+1)

rev[0]=(0,0)
revmax[0]=(0,0)
norm[0]=(0,0)
ans=0

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

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

for i in range(1,n+1):
    revmax[-i]=(max(revmax[-i+1][0],rev[-i+1][1]-2*rev[-i+1][0]),max(revmax[-i+1][1],rev[-i+1][1]-rev[-i+1][0]))

for i in range(n+1):
    v1=norm[i][1]-norm[i][0]+revmax[i-n][0]
    v2=norm[i][1]-2*norm[i][0]+revmax[i-n][1]
    ans=max(ans,v1,v2)

print(ans)

Submission status: Wrong Answer

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

rev=[None]*(n+1)
norm=[None]*(n+1)
rev[0]=(0,0)
norm[0]=(0,0)
ans=0

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

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

for i in range(n+1):
    for j in range(n-i):
        cost=min(norm[i][0]+2*rev[-j][0],2*norm[i][0]+rev[-j][0])
        ans=max(ans,rev[-j][1]+norm[i][1]-cost)

print(ans)

Submission status: Wrong Answer

Solution code:
N, C = map(int, input().split())
Sushi = [[int(info) for info in input().split()] for n in range(N)]
Clocksum = [0 for i in range(N+1)]
AntiClocksum = [0 for i in range(N+1)]
maxClocksum, maxAntisum = [0 for i in range(N+1)], [0 for i in range(N+1)]
for i in range(N): #一方向にのみ進む
    Clocksum[i+1] = Clocksum[i] + Sushi[i][1] - Sushi[i][0] 
    AntiClocksum[i+1] = AntiClocksum[i] + Sushi[N-i-1][1] - (C - Sushi[N-i-1][0])
    if i > 0:
        Clocksum[i+1] += Sushi[i-1][0]
        AntiClocksum[i+1] += C - Sushi[N-i][0]
    maxClocksum[i+1] = max(maxClocksum[i], Clocksum[i+1])
    maxAntisum[i+1] = max(maxAntisum[i], AntiClocksum[i+1])
maxCalorie = max(max(Clocksum), max(AntiClocksum))

#途中で折り返す
ClockToAnticlock = 0
AntiClockToClock = 0
for i in range(N-1):
    print(i, N-1-i)
    ClockToAnticlock = max(Clocksum[i+1] + maxAntisum[N-1-i] - Sushi[i][0], ClockToAnticlock)
    AntiClockToClock = max(AntiClocksum[i+1] + maxClocksum[N-1-i] - (C - Sushi[N-i-1][0]), AntiClockToClock)

print(max(maxCalorie, max(ClockToAnticlock, AntiClockToClock)))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
from operator import sub

N, C = map(int, input().split())
xs = []
vs = []
for i in range(N):
    x, v = map(int, input().split())
    xs += [x]
    vs += [v]
xs += [C]
vs += [0]

fAs = accumulate(vs)
fAs = [(fA, -xA) for fA, xA in zip(map(sub, fAs, xs), xs)]
gAs = list(accumulate([(0, 0)] + fAs[:-1], max))

ans = 0
eneIn = 0
for B in reversed(range(N + 1)):
    eneIn += vs[B]
    eneOut = C - xs[B]
    ans1 = eneIn - eneOut + max(0, -eneOut + gAs[B][0])
    ans2 = gAs[B][0] + max(0, gAs[B][1] + eneIn - eneOut)
    ans = max(ans, ans1, ans2)

print(ans)

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(n)]
ans = 0
num = 0
nu = 0
cou = 0
co = 0
go = 0
left = []
right = []
for i in range(n):
    num += lis[i][1]
    go = lis[i][0]
    ans = max(ans,num-go)
    if cou < num-go*2:
        cou = num-go*2
        left.append([go,num])
    else:
        try:left.append(left[-1])
        except:left.append([0,0])

for i in reversed(range(n)):
    nu += lis[i][1]
    go = c-lis[i][0]
    ans = max(ans,nu-go)
    if co < nu-go*2:
        co = nu-go*2
        right.append([go,nu])
    else:
        try:right.append(right[-1])
        except:right.append([0,0])
gone = 0
gon = 0
for i in range(n-1):
    gone += lis[i][1]
    ans = max(ans,gone + right[n-i-2][1] - lis[i][0] * 2 - right[n-i-2][0])
    gon += lis[-(i+1)][1]
    ans = max(ans,gon + left[n-i-2][1] - (c-lis[-(i+1)][0]) * 2 - left[n-i-2][0])
print(ans)

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(n)]
ans = 0
left = [[0,0]]
right = [[0,0]]
for i in range(n):
    left.append([lis[i][0],left[-1][1]+lis[i][1]])
    right.append([c-lis[-(i+1)][0],right[-1][1]+lis[-(i+1)][1]])
"""
for i in range(n+1):
    for j in range(n-i+1):
          # print(left[i],right[j],left[i][1]+right[j][1]-left[i][0]*2-right[j][0],left[i][1]+right[j][1]-left[i][0]-right[j][0]*2)
        ans = max(ans,left[i][1]+right[j][1]-left[i][0]*2-right[j][0])
        ans = max(ans,left[i][1]+right[j][1]-left[i][0]-right[j][0]*2)
"""
cou = 0
co = 0
num = 0
nu = 0
for i in range(1,n+1):
    if cou < left[i][1]-left[i][0]:
        cou = left[i][1]-left[i][0]
        num = i
    if co < right[i][1]-right[i][0]:
        co = right[i][1]-right[i][0]
        nu = i
for j in range(n-num+1):
      # print(left[num],right[j],left[num][1]+right[j][1]-min(left[num][0]*2+right[j][0],left[num][0]+right[j][0]*2))
    ans = max(ans,left[num][1]+right[j][1]-min(left[num][0]*2+right[j][0],left[num][0]+right[j][0]*2))
for j in range(n-nu+1):
      # print(right[nu],left[j],left[j][1]+right[nu][1]-min(left[j][0]+right[nu][0]*2,left[j][0]*2+right[nu][0]))
    ans = max(ans,left[j][1]+right[nu][1]-min(left[j][0]+right[nu][0]*2,left[j][0]*2+right[nu][0]))
print(ans)

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(n)]
ans = 0
left = [[0,0]]
right = [[0,0]]
for i in range(n):
    left.append([lis[i][0],left[-1][1]+lis[i][1]])
    right.append([c-lis[-(i+1)][0],right[-1][1]+lis[-(i+1)][1]])
"""
for i in range(n+1):
    for j in range(n-i+1):
          # print(left[i],right[j],left[i][1]+right[j][1]-left[i][0]*2-right[j][0],left[i][1]+right[j][1]-left[i][0]-right[j][0]*2)
        ans = max(ans,left[i][1]+right[j][1]-left[i][0]*2-right[j][0])
        ans = max(ans,left[i][1]+right[j][1]-left[i][0]-right[j][0]*2)
"""
cou = 0
co = 0
num = 0
nu = 0
for i in range(1,n+1):
    if cou < left[i][1]-2*left[i][0]:
        cou = left[i][1]-2*left[i][0]
        num = i
    if co < right[i][1]-2*right[i][0]:
        co = right[i][1]-2*right[i][0]
        nu = i
for j in range(n-num+1):
    ans = max(ans,left[num][1]+right[j][1]-left[num][0]-right[j][0]*2)
for j in range(n-nu+1):
    ans = max(ans,left[j][1]+right[nu][1]-left[j][0]*2-right[nu][0])
print(ans)

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(n)]
ans = 0
left = [[0,0]]
right = [[0,0]]
for i in range(n):
    left.append([lis[i][0],left[-1][1]+lis[i][1]])
    right.append([c-lis[-(i+1)][0],right[-1][1]+lis[-(i+1)][1]])
"""
for i in range(n+1):
    for j in range(n-i+1):
          # print(left[i],right[j],left[i][1]+right[j][1]-left[i][0]*2-right[j][0],left[i][1]+right[j][1]-left[i][0]-right[j][0]*2)
        ans = max(ans,left[i][1]+right[j][1]-left[i][0]*2-right[j][0])
        ans = max(ans,left[i][1]+right[j][1]-left[i][0]-right[j][0]*2)
"""
cou = 0
co = 0
num = 0
nu = 0
for i in range(1,n+1):
    if cou < left[i][1]-2*left[i][0]:
        cou = left[i][1]-2*left[i][0]
        num = i
    if co < right[i][1]/right[i][0]:
        co = right[i][1]/right[i][0]
        nu = i
for j in range(n-num+1):
    ans = max(ans,left[num][1]+right[j][1]-left[num][0]-right[j][0]*2)
for j in range(n-nu+1):
    ans = max(ans,left[j][1]+right[nu][1]-left[j][0]*2-right[nu][0])
print(ans)

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(n)]
ans = 0
left = [[0,0]]
right = [[0,0]]
for i in range(n):
    left.append([lis[i][0],left[-1][1]+lis[i][1]])
    right.append([c-lis[-(i+1)][0],right[-1][1]+lis[-(i+1)][1]])
"""
for i in range(n+1):
    for j in range(n-i+1):
          # print(left[i],right[j],left[i][1]+right[j][1]-left[i][0]*2-right[j][0],left[i][1]+right[j][1]-left[i][0]-right[j][0]*2)
        ans = max(ans,left[i][1]+right[j][1]-left[i][0]*2-right[j][0])
        ans = max(ans,left[i][1]+right[j][1]-left[i][0]-right[j][0]*2)
"""
cou = 0
co = 0
num = 0
nu = 0
for i in range(1,n+1):
    if cou < left[i][1]-2*left[i][0]:
        cou = left[i][1]-2*left[i][0]
        num = i
    if co < right[i][1]/right[i][0]:
        co = right[i][1]/right[i][0]
        nu = i
for j in range(n-num):
    ans = max(ans,left[num][1]+right[j][1]-left[num][0]-right[j][0]*2)
for j in range(n-nu):
    ans = max(ans,left[j][1]+right[nu][1]-left[j][0]*2-right[nu][0])
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10**5)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

from itertools import accumulate

n,c = li()
x = []
v = []
for _ in range(n):
    xi,vi = li()
    x.append(xi)
    v.append(vi)

# 時計回りのmax
cw_dist = [0] + x
value_cum = [0] + list(accumulate(v))
cw_value = [value - dist for dist,value in zip(cw_dist, value_cum)]
cw_max = [0]
for i in range(1,n+1):
    cw_max.append(max(cw_max[-1], cw_value[i]))

# 反時計回りのmax
ccw_dist = [0] + [c-xi for xi in x[::-1]]
value_cum2 = [0] + list(accumulate(v[::-1]))
ccw_value = [value - dist for dist,value in zip(ccw_dist, value_cum2)]
ccw_max = [0]
for i in range(1,n+1):
    ccw_max.append(max(ccw_max[-1], ccw_value[i]))
    
#
cands = [0]
for i in range(n):
    cand = max(cw_max[i],
               ccw_max[n-i],
               cw_max[i]+ccw_max[n-i] - min(cw_dist[i], ccw_dist[n-i]))
               
    cands.append(max(cands[-1],cand))

print(max(cands))

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(n)]
ans = 0
left = [[0,0]]
right = [[0,0]]
for i in range(n):
    left.append([lis[i][0],left[-1][1]+lis[i][1]])
    right.append([c-lis[-(i+1)][0],right[-1][1]+lis[-(i+1)][1]])
for i in range(n):
    for j in range(n-i+1):
          # print(left[i],right[j],left[i][1]+right[j][1]-left[i][0]*2-right[j][0],left[i][1]+right[j][1]-left[i][0]-right[j][0]*2)
        ans = max(ans,left[i][1]+right[j][1]-left[i][0]*2-right[j][0])
        ans = max(ans,left[i][1]+right[j][1]-left[i][0]-right[j][0]*2)
print(ans)

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
lis = [list(map(int,input().split())) for i in range(n)]
ans = 0
left = [[0,0]]
right = [[0,0]]
for i in range(n):
    left.append([lis[i][0],left[-1][1]+lis[i][1]])
    right.append([c-lis[-(i+1)][0],right[-1][1]+lis[-(i+1)][1]])
for i in range(n):
    for j in range(n-i):
        ans = max(ans,left[i][1]+right[j][1]-left[i][0]*2-right[j][0])
        ans = max(ans,left[i][1]+right[j][1]-left[i][0]-right[j][0]*2)
print(ans)

Submission status: Wrong Answer

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

xv = []
go = []
goback = [0]
back = []
backgo = [0]

mid = 0

for i in range(n):
    xv.append(list(map(int,input().split())))
    if 2*xv[i][0] < c: mid = i

temp = 0
for i in range(n):
    temp += xv[i][1]
    go.append(temp-xv[i][0])
    if i <= mid:
        goback.append(temp-2*xv[i][0])
temp = 0
for i in range(n)[::-1]:
    temp += xv[i][1]
    back.append(temp-(c-xv[i][0]))
    if i > mid:
        backgo.append(temp-2*(c-xv[i][0]))



print(max(max(go[:mid+1]+[0])+max(backgo),max(back[:n-mid-1]+[0])+max(goback),max(go),max(back)))

Submission status: Wrong Answer

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

xv = []
go = [0]
goback = [0]
back = [0]
backgo = [0]

mid = 0

for i in range(n):
    xv.append(list(map(int,input().split())))
    if 2*xv[i][0]<= c: mid = i

temp = 0
for i in range(n):
    temp += xv[i][1]
    go.append(temp-xv[i][0])
    if i <= mid:
        goback.append(temp-2*xv[i][0])
temp = 0
for i in range(n)[::-1]:
    temp += xv[i][1]
    back.append(temp-(c-xv[i][0]))
    if i > mid:
        backgo.append(temp-2*(c-xv[i][0]))



print(max(max(go[:mid+2]+[0])+max(backgo),max(back[:n-mid]+[0])+max(goback),max(go),max(back)))

Submission status: Wrong Answer

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

xv = []
go = [0]
goback = [0]
back = [0]
backgo = [0]

mid = 0

for i in range(n):
    xv.append(list(map(int,input().split())))
    if 2*xv[i][0]<= c: mid = i

temp = 0
for i in range(mid+1):
    temp += xv[i][1]
    go.append(temp-xv[i][0])
    goback.append(temp-2*xv[i][0])
temp = 0
for i in range(mid+1,n)[::-1]:
    temp += xv[i][1]
    back.append(temp-(c-xv[i][0]))
    backgo.append(temp-2*(c-xv[i][0]))

print(max(max(go)+max(backgo),max(back)+max(goback)))

Submission status: Wrong Answer

Solution code:
n, c = map(int, input().split())
sushi = [list(map(int, input().split())) for _ in range(n)]
clock = [[0, 0] for _ in range(n)]
anticlock = [[0, 0] for _ in range(n)]

max_clock = max(sushi[0][1] - sushi[0][0], 0)
clock[0] = [sushi[0][1] - sushi[0][0], max_clock]
for i in range(1, n):
	clock[i][0] = clock[i-1][0] + sushi[i][1] - (sushi[i][0] - sushi[i-1][0])
	max_clock = max(max_clock, clock[i][0])
	clock[i][1] = max_clock

max_anti = max(sushi[-1][1] - (c - sushi[-1][0]), 0)
anticlock[0] = [sushi[-1][1] - (c - sushi[-1][0]), max_anti]
for i in range(1, n):
	anticlock[i][0] = anticlock[i-1][0] + sushi[-(i+1)][1] - (sushi[-i][0] - sushi[-(i+1)][0])
	max_anti = max(max_anti, anticlock[i][0])
	anticlock[i][1] = max_anti

ans_clock = 0
ans_anti = 0

for i in range(n - 1):
	if clock[i][0] - sushi[i][0] + anticlock[n - 2 - i][1] > ans_clock:
		ans_clock = clock[i][0] - sushi[0][0] + anticlock[n - 2 - i][1]
	if anticlock[i][0] - (c - sushi[-(i+1)][0]) + clock[n - 2 - i][1] > ans_anti:
		ans_anti = anticlock[i][0] - (c - sushi[-(i+1)][0]) + clock[n - 2 - i][1]

print(max(ans_clock, ans_anti, clock[-1][1], anticlock[-1][1]))

Submission status: Wrong Answer

Solution code:
N,C = map(int,input().split())
table=[]
for i in range(N):
    x,v=map(int,input().split())
    table.append([x,v])
V=[0]*N
V[0]=table[0][1]
for i in range(1,N):
    V[i]=V[i-1]+table[i][1]
ans=0
clo_1=0
clo_2=0
rclo_1=0
rclo_2=0
for i in range(N):
    #print(V[i]-table[i][0])
    ans=max(ans,V[i]-table[i][0])
    #print(V[i]-table[i][0])
    #print(V[i]-2*table[i][0])
    clo_1 = max(clo_1,V[i]-table[i][0])
    clo_2 = max(clo_2,V[i]-2*table[i][0])
for i in range(N-1,0,-1):
    #print(V[N-1]-V[i-1]-C+table[i][0])
    ans = max(ans,V[N-1]-V[i-1]-C+table[i][0])
    #print(V[N-1]-V[i-1]-C+table[i][0])
    #print(V[N-1]-V[i-1]-2*C+2*table[i][0])
    rclo_1 = max(rclo_1,V[N-1]-V[i-1]-C+table[i][0])
    rclo_2 = max(rclo_2,V[N-1]-V[i-1]-2*C+2*table[i][0])
ans=max(ans,V[N-1]-C+table[0][0])
ans =max(ans,clo_1+rclo_2)
ans =max(ans,clo_2+rclo_1)
#print(clo_1+rclo_2)
#print(clo_2+rclo_1)
print(ans)

Submission status: Wrong Answer

Solution code:
N,C = map(int,input().split())
table=[]
for i in range(N):
    x,v=map(int,input().split())
    table.append([x,v])
V=[0]*N
V[0]=table[0][1]
for i in range(1,N):
    V[i]=V[i-1]+table[i][1]
ans=0
clo_1=0
clo_2=0
rclo_1=0
rclo_2=0
for i in range(N):
    #print(V[i]-table[i][0])
    ans=max(ans,V[i]-table[i][0])
    if table[i][0]<=C//2:
        #print(V[i]-table[i][0])
        #print(V[i]-2*table[i][0])
        clo_1 = max(clo_1,V[i]-table[i][0])
        clo_2 = max(clo_2,V[i]-2*table[i][0])
for i in range(N-1,0,-1):
    #print(V[N-1]-V[i-1]-C+table[i][0])
    ans = max(ans,V[N-1]-V[i-1]-C+table[i][0])
    if table[i][0]>C//2 and i!=0:
        #print(V[N-1]-V[i-1]-C+table[i][0])
        #print(V[N-1]-V[i-1]-2*C+2*table[i][0])
        rclo_1 = max(rclo_1,V[N-1]-V[i-1]-C+table[i][0])
        rclo_2 = max(rclo_2,V[N-1]-V[i-1]-2*C+2*table[i][0])
ans=max(ans,V[N-1]-C+table[0][0])
ans =max(ans,clo_1+rclo_2)
ans =max(ans,clo_2+rclo_1)
#print(clo_1+rclo_2)
#print(clo_2+rclo_1)
print(ans)