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 = []
V = []
for i in range(N):
  x, v = map(int, input().split())
  X.append(x)
  V.append(v)

frontS = []
prevX = 0
v = 0
for i in range(N):
  v = v + V[i] - (X[i] - prevX)
  frontS.append(v)
  prevX= X[i]

# 後ろi個の中での累計MAX
backMaxS = []
prevX = C
v = 0
tmpMax = float('-inf')
for i in range(N-1, -1, -1):
  v = v + V[i] - (prevX - X[i])
  tmpMax = max(tmpMax, v)
  backMaxS.append(tmpMax)
  prevX = X[i]
backMaxS.reverse()
backMaxS.append(0)

ans = backMaxS[0]
for i in range(N):
  # X個まで食べて、退出 or 引き返す
  ans = max(
    ans,
    frontS[i],
    frontS[i] - X[i] + backMaxS[i+1]
  )

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from sys import stdin, stdout
import math
from copy import deepcopy
rem=10**9+7
sys.setrecursionlimit(10 ** 6)
take = lambda: map(int, stdin.readline().split())



n,c=take()
ans=[]
ans2=[]
ans1=[]
ans3=[]
u=[0]
f=[0]
all=[[0,0]]
rev=[[0,0]]
for i in range(n):
    a,b=take()
    all.append([a,b])
    rev.append([c-a,b])
all.sort()
rev.sort()
#print all,rev
tot=0
en=0
for i in all:
    a,b=i
    tot+=b
    en=tot
    ans.append(en-a)
    ans1.append(en-2*a)

tot=0
en=0
for i in rev:
    a,b=i
    tot += b
    en = tot
    ans2.append(en - a)
    ans3.append(en-2*a)

fin=0
#print all,rev
#print ans,ans2
t=0
k=0
for i in range(len(ans)):
    t=max(t,ans[i])
    u.append(t)
    k=max(k,ans2[i])
    f.append(k)

#print ans,ans1,ans2,ans3,f
for i in range(n+1):
    fin=max(fin,ans1[i]+f[n-i])
    fin=max(fin,ans3[i]+u[n-i])
print fin

Submission status: Wrong Answer

Solution code:
import sys
from sys import stdin, stdout,maxint
import math
from copy import deepcopy
rem=10**9+7
sys.setrecursionlimit(10 ** 6)
take = lambda: map(int, stdin.readline().split())



n,c=take()
ans=[]
ans2=[]
ans1=[]
ans3=[]
u=[0]
f=[0]
all=[[0,0]]
rev=[[0,0]]
for i in range(n):
    a,b=take()
    all.append([a,b])
    rev.append([c-a,b])
all.sort()
rev.sort()
#print all,rev
tot=0
en=0
for i in all:
    a,b=i
    tot+=b
    en=tot
    ans.append(en-a)
    ans1.append(en-2*a)

tot=0
en=0
for i in rev:
    a,b=i
    tot += b
    en = tot
    ans2.append(en - a)
    ans3.append(en-2*a)

fin=0
#print all,rev
#print ans,ans2
t=0
k=0
for i in range(len(ans)):
    t=max(t,ans[i])
    u.append(t)
    k=max(k,ans2[i])
    f.append(k)

#print ans,ans1,ans2,ans3,f
for i in range(n):
    fin=max(fin,ans1[i]+f[n-i])
    fin=max(fin,ans3[i]+u[n-i])
print fin

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
#N=int(input())
#a = list(map(int,input().split()))

#x = []
#y = []
#for i in range(5):
#    x1,y1=[int(i) for i in input().split()]
#    x.append(x1)
#    y.append(y1)

N,C=[int(i) for i in input().split()]

x = [0]
v = [0]
for i in range(N):
    x1,v1=[int(i) for i in input().split()]
    x.append(x1)
    v.append(v1)

x.append(C)
v.append(0)

s = [0]*(N+1)
g = [0]*(N+2)
m1=0;m2=0

for i in range(1,N+1):
    s[i] = s[i-1]+v[i]-(x[i]-x[i-1])

for i in reversed(range(1,N+1)):
    g[i] = g[i+1]+v[i]+(x[i]-x[i+1])

j=N+1
M=0
val=s[i]
k=0
jj=j
for i in reversed(range(N+1)):
    val = s[i] + g[j] -min(k,x[i])
    while(jj-i>1):
#        print(jj)
        newval = s[i] + g[jj-1] -min(k+x[jj]-x[jj-1],x[i])
        if newval >= val:
            val = newval
            jj=jj-1
            k+= x[j]-x[jj]
            j=jj
        else:
            jj=jj-1
#    print(i,j,val)
    M=max(M,val)

#print(s,g)
print(M)

Submission status: Wrong Answer

Solution code:
tmp = list(map(int, input().split()))
n,c = tmp[0],tmp[1]
xs, vs = [0],[]
ans = [0]
for i in range(n):
    tmp = list(map(int, input().split()))
    xs.append(tmp[0])
    vs.append(tmp[1])
xs_rev, vs_rev = [0],[]
for i in range(n):
    xs_rev.append(c-xs[-(i+1)])
    vs_rev.append(vs[-(i+1)])
def findmax(v_data,x_data,m,mode = True):
    vs1 = [0]
    for i in range(m):
        if mode:
            vs1.append(vs1[-1] + v_data[i] - x_data[i+1] + x_data[i])
        else:
            vs1.append(vs1[-1] + v_data[i] - x_data[i+1] * 2 + x_data[i] * 2)
    return(max(vs1))
ans.append(findmax(vs,xs,n))
ans.append(findmax(vs_rev,xs_rev,n))
sumv = []
for i in range(1,n-1):
    sumv.append(findmax(vs[:i],xs[:i+1],i) + findmax(vs_rev[:n-i],xs_rev[:n-i+1],n-i,mode = False))
if sumv != []:
    ans.append(max(sumv))
sumv = []
for i in range(1,n-1):
    sumv.append(findmax(vs[:i],xs[:i+1],i,mode = False) + findmax(vs_rev[:n-i],xs_rev[:n-i+1],n-i))
if sumv != []:
    ans.append(max(sumv))
print(max(ans))

Submission status: Wrong Answer

Solution code:
tmp = list(map(int, input().split()))
n,c = tmp[0],tmp[1]
xs, vs = [0],[]
for i in range(n):
    tmp = list(map(int, input().split()))
    xs.append(tmp[0])
    vs.append(tmp[1])
xs_rev, vs_rev = [0],[]
for i in range(n):
    xs_rev.append(c-xs[-(i+1)])
    vs_rev.append(vs[-(i+1)])
def findmax(v_data,x_data,m,mode = True):
    vs1 = [0]
    for i in range(m):
        if mode:
            vs1.append(vs1[-1] + v_data[i] - x_data[i+1] + x_data[i])
        else:
            vs1.append(vs1[-1] + v_data[i] - x_data[i+1] * 2 + x_data[i] * 2)
    return(max(vs1))
ans = []
ans.append(findmax(vs,xs,n))
ans.append(findmax(vs_rev,xs_rev,n))
sumv = []
for i in range(1,n-1):
    sumv.append(findmax(vs[:i],xs[:i+1],i) + findmax(vs_rev[:n-i],xs_rev[:n-i+1],n-i,mode = False))
if sumv != []:
    ans.append(max(sumv))
sumv = []
for i in range(1,n-1):
    sumv.append(findmax(vs[:i],xs[:i+1],i,mode = False) + findmax(vs_rev[:n-i],xs_rev[:n-i+1],n-i))
if sumv != []:
    ans.append(max(sumv))
print(max(ans))

Submission status: Wrong Answer

Solution code:
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(N):
    x[i+1],v[i+1]=map(int,input().split())
r=N
l=1
#nh is not distance but index
nh=0
S=[0 for i in range(N+1)]
S[0]=0
def dis(a,b):
    return min([abs(b-a)%C,C-abs(b-a)%C])
for k in range(1,N+1):
    L=v[l]-dis(x[l],x[nh])
    R=v[r]-dis(x[r],x[nh])
    #equal no baai bug kanousei ari
    if R > L:
        nh=r
        S[k]=S[k-1]+R
        r-=1
    else:
        nh=l
        S[k]=S[k-1]+L
        l+=1
T=[0 for i in range(N+1)]
T[0]=0
l=1
r=N
nh=0
for k in range(1,N+1):
    L=-dis(x[l],x[nh])
    R=-dis(x[r],x[nh])
    #equal no baai bug kanousei ari
    if R > L:
        nh=r
        T[k]=T[k-1]+R+v[r]
        r-=1
    else:
        nh=l
        T[k]=T[k-1]+L+v[l]
        l+=1
print(max([max(S),max(T)]))

Submission status: Wrong Answer

Solution code:
n,c=map(int,input().split())
x=[0]
v=[]
for i in range(n):
    a,b=map(int,input().split())
    x.append(a)
    v.append(b)
x1=[x[i+1]-x[i] for i in range(len(x)-1)]
x2=[c-x[-1]]+x1[:0:-1]
h=[0]
for i,j in zip(x2,reversed(v)):
    h.append(h[-1]+j+-i)
t=[0]
for i,j in zip(x1,v):
    t.append(t[-1]+j-i)
hi=h.index(max(h[0:-1]))
ti=t.index(max(t[0:-1]))

hm=max(h[0:-1])
tm=max(t[0:-1])

if x[-hi]==x[ti]: print(max(tm,hm))
else: print(max( hm, tm, hm+tm-x[-hi],hm+tm-x[ti]))
#print(h,t)
#print(x[-hi],x[ti])

Submission status: Wrong Answer

Solution code:
n,c=map(int,input().split())
x=[0]
v=[]
for i in range(n):
    a,b=map(int,input().split())
    x.append(a)
    v.append(b)
    
x1=[x[i+1]-x[i] for i in range(len(x)-1)]
x2=[c-x[-1]]+x1[:0:-1]
h=[0]
for i,j in zip(x2,reversed(v)):
    h.append(h[-1]+j+-i)
t=[0]
for i,j in zip(x1,v):
    t.append(t[-1]+j-i)
hi=h.index(max(h[0:-1]))
ti=t.index(max(t[0:-1]))

hm=max(h[0:-1])
tm=max(t[0:-1])

print(max( hm, tm, hm+tm-x[-hi],hm+tm-x[ti]))
#print(h,t)
#print(x[-hi],x[ti])

Submission status: Wrong Answer

Solution code:
from functools import reduce

import numpy as np


def get_max():
    a = 0

    def _max(x):
        nonlocal a
        a = max(a, x)
        return a

    return _max


def solve(fwd, bck):
    dp = []
    cur = 0
    for x, v in fwd:
        cur -= x
        cur += v
        dp.append(cur)

    dp = list(map(get_max(), dp))

    cur = 0
    ans = 0
    for i, (x, v) in enumerate(bck[:-1]):
        cur -= 2 * x
        cur += v
        # print(i, x, v, ans, cur, dp[n - i - 2])
        ans = max(ans, dp[n - i - 2] + cur)
        # print(i, x, v, ans, cur, dp[n - i - 2])

    return ans


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

fwd = np.array(sushi).T
fwd[0] = np.diff(np.concatenate(([0], fwd[0])))
fwd = fwd.T.tolist()

bck = np.array(sushi).T
bck[0] = np.diff(np.concatenate(([0], (c - bck[0])[::-1])))
bck[1] = bck[1][::-1]
bck = bck.T.tolist()

print(max(solve(fwd, bck), solve(bck, fwd)))

Submission status: Wrong Answer

Solution code:
N, C = map(int, raw_input().split())
a = []
b = []
c = []
for i in range(N):
	a.append(map(int, raw_input().split()))
b.append(a[0][1] - a[0][0])
for i in range(1, N-1):
	b.append(b[i-1] + a[i][1] - a[i][0])
c.append(a[N-1][1] + a[N-1][0] - C)
for i in range(1, N-1):
	c.append(c[i-1] + a[N-i-1][1] + a[N-i-1][0] - C)
if max(b) < max(c):
	if max(c) > 0:
		print max(c)
	else:
		print 0
else:
	if max(b) > 0:
		print max(b)
	else:
		print 0

Submission status: Wrong Answer

Solution code:
N, C = map(int, raw_input().split())
a = []
b = []
c = []
for i in range(N):
	a.append(map(int, raw_input().split()))
b.append(a[0][1] - a[0][0])
for i in range(1, N-1):
	b.append(b[i-1] + a[i][1] - a[i][0])
c.append(a[N-1][1] + a[N-1][0] - C)
for i in range(1, N):
	c.append(c[i-1] + a[N-i-1][1] + a[N-i-1][0] - C)
if max(b) < max(c):
	print max(c)
else:
	print max(b)

Submission status: Time Limit Exceeded

Solution code:
n,c1 = map(int, input().split())
g = []
c = [0]
d = [0]
a = 0
ans=[]
for i in range(n):
  x,v = map(int, input().split())
  g.append([x,v])
  a+=v
  c.append(a-x)
  d.append(a-2*x)
b = 0
e = [0]
f = [0]
for i,j in reversed(g):
  b+=j
  e.append(b-(c1-i))
  f.append(b-2*(c1-i))
for i in range(1,n+2):
  ans.append(max(c[:i])+f[-i])
  ans.append(max(d[:i])+e[-i])
print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
N,C=map(int, input().split())
#Aは時計回り
A,B=[],[]
allv=0
for i in range(N):
  a,b=map(int, input().split())
  A.append([a,b])
  B.append([C-a,b])
  allv+=b
A,B=sorted(A),sorted(B)
OA,OB,AS,BS=[],[],[],[]
ax,av,bx,bv=0,0,0,0
for i in range(N):
  ax,bx=A[i][0],B[i][0]
  av+=A[i][1]
  bv+=B[i][1]
  OA.append((ax,av))
  OB.append((bx,bv))
  AS.append(av-ax)
  BS.append(bv-bx)
#OA→AO→OBといくとき
ansa=0
for i in range(N-1):
  #iまで行く
  bma=N-i
  oa=OA[i][1]-OA[i][0]
  ao=-OA[i][0]
  ob=max(BS[:N-i-1])
  d=max(oa,oa+ao+ob)
  ansa=max(ansa,d)
ansb=0
for i in range(N-1):
  #iまで行く
  ama=N-i
  ob=OB[i][1]-OB[i][0]
  bo=-OB[i][0]
  oa=max(AS[:N-i-1])
  d=max(ob,ob+bo+oa)
  ansb=max(ansb,d)
print(max(ansa,ansb,allv-(C-max(OA[0][0],OB[0][0]))))

Submission status: Time Limit Exceeded

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

x = []
v = []
array1 = []
array2 = []

for i in range(n):
  a,b = map(int, input().split())
  x.append(a)
  v.append(b)
  
rx = []
rv = []
for i in reversed(range(len(x))):
  rx.append(c-x[i])
  rv.append(v[i])
  
count = x[0]
array1.append(v[0] - x[0])
for i in range(1,n):
  array1.append(v[i]-x[i]+array1[i-1]+count)
  count = x[i]
  
count = rx[0]
array2.append(rv[0] - rx[0])
for i in range(1,n):
  array2.append(rv[i]-rx[i]+array2[i-1]+count)
  count = rx[i]
  
ans = 0
for i in range(n):
  if i != n-1:
    tmp = max(array1[i], array1[i]-x[i]+max(array2[:n-i-1]))
    ans = max(ans, tmp)
  else:
    ans = max(ans, array1[i])
  
for i in range(n):
  if i != n-1:
    tmp = max(array2[i], array2[i]-rx[i]+max(array1[:n-i-1]))
    ans = max(ans, tmp)
  else:
    ans = max(ans, array2[i])
    

  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, C= map(int,input().split())
XV = sorted([list(map(int,input().split())) for _ in range(N)])
acmXV = []
dbl_acmXV = []
for i, xv in enumerate(XV):
  x,v = xv
  if i == 0:
    acmXV.append(v-x)
    dbl_acmXV.append(v-x*2)
  else:
    acmXV.append(v-(x-XV[i-1][0])+acmXV[i-1])
    dbl_acmXV.append(v-(x-XV[i-1][0])*2+dbl_acmXV[i-1])

acmXV_rev = [0 for _ in range(N)]
dbl_acmXV_rev = [0 for _ in range(N)]
for i in range(N-1, -1, -1):
  x,v = XV[i]
  if i == N-1:
    acmXV_rev[i] = v-(C-x)
    dbl_acmXV_rev[i] = v-(C-x)*2
  else:
    acmXV_rev[i] = (v-(XV[i+1][0]-XV[i][0])+acmXV_rev[i+1])
    dbl_acmXV_rev[i] = (v-(XV[i+1][0]-XV[i][0])*2+dbl_acmXV_rev[i+1])
    
ans = max(0,max(acmXV),max(acmXV_rev))
for i in range(N):
  for j in range(N-1,i,-1):
    #print(i,j,dbl_acmXV[i],acmXV_rev[N-1-j])
    ans = max(ans, dbl_acmXV[i]+acmXV_rev[j], acmXV[i]+dbl_acmXV_rev[j]) 
print(ans)

Submission status: Time Limit Exceeded

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+1):
        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+1-i):
            ans = max(ans, cwccw + ccw[j])
            ans = max(ans, ccwcw + cw[j])

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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] + max(sm_rev[:n-i-1]))

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]) + max(sm[:n-i-1]))

print(ans)

Submission status: Time Limit Exceeded

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

def main():
	N,C = map(int,input().split())
	X = [list(map(int,input().split())) for  _ in range(N)]
	stand_c = 0
	stand_cc = C
	clock = [0 for _ in range(N+1)]
	c_clock = [0 for _ in range(N+1)]

	for i in range(N):
		clock[i+1] = clock[i]+X[i][1]-(X[i][0]-stand_c)
		stand_c = X[i][0]
		c_clock[i+1] = c_clock[i]+X[-i-1][1]-(stand_cc-X[-i-1][0])
		stand_cc = X[-i-1][0]

	ans = 0
	for i in range(N+1):
		if i == 0:
			ans = max(ans,max(c_clock))
		elif i == N:
			ans = max(ans,max(clock))
		else:
			cal = clock[i]-X[i-1][0]+max(c_clock[:(N-i+1)])
			c_cal = c_clock[i]-(C-X[-i][0])+max(clock[:(N-i+1)])
			ans = max(ans,cal,c_cal)

	print(ans)

if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

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)

Submission status: Time Limit Exceeded

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)

Submission status: Time Limit Exceeded

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))
ans = 0
# print(posDist)
# print(negDist)
# print(posVal)
# print(negVal)
for l in range(N+1):
    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: Time Limit Exceeded

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+1):
        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: Time Limit Exceeded

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)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
import copy

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, C] = [long(input_raw[0]), long(input_raw[1])]
    x_list = []
    v_list = []
    for i in range(N):
        input_raw = read_func().strip().split()
        [x, v] = [long(input_raw[0]), long(input_raw[1])]
        x_list.append(x)
        v_list.append(v)

    left_v_sum = [v_list[0]]
    for i in range(1, N):
        left_v_sum.append(left_v_sum[i - 1] + v_list[i])

    right_v_sum = [v_list[N - 1]]
    for i in range(N - 2, -1, -1):
        right_v_sum.append(right_v_sum[N - i - 2] + v_list[i])

    left_max = 0
    for l in range(N):
        left_max = max(left_max, left_v_sum[l] - x_list[l])
        for r in range(N - l - 1):
            left_max = max(left_max, left_v_sum[l] + right_v_sum[r] - (2 * x_list[l] + (C - x_list[N - r - 1])))

    right_max = 0
    for r in range(N):
        right_max = max(right_max, right_v_sum[r] - (C - x_list[N - r - 1]))
        for l in range(N - r - 1):
            right_max = max(right_max, left_v_sum[l] + right_v_sum[r] - (2 *(C - x_list[N - r - 1]) + x_list[l]))

    print max(left_max, right_max)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N,C=map(int,input().split())
x=[0 for i in range(N)]
v=[0 for i in range(N)]
for i in range(N):
    x[i],v[i]=map(int,input().split())
a=[0 for i in range(N)]
b=[0 for i in range(N)]
a[0]=v[0]
b[N-1]=v[N-1]
for i in range(1,N):
    a[i]=v[i]+a[i-1]
for i in range(N-1)[::-1]:
    b[i]=b[i+1]+v[i]
ans=0
for i in range(N+1):
    for j in range(N-i+1):
        if i==0:
            OA=0
            EA=0
        else:
            OA=x[i-1]
            EA=a[i-1]
        if j==0:
            OB=0
            EB=0
        else:
            OB=C-x[N-j]
            EB=b[N-j]
        tmpA=EA+EB-OA-OA-OB
        tmpB=EA+EB-OA-OB-OB
        if ans<tmpA:
            ans=tmpA
        if ans<tmpB:
            ans=tmpB
print(ans)

Submission status: Time Limit Exceeded

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
for i in range(N):
    #print(V[i]-table[i][0])
    ans=max(ans,V[i]-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])
ans=max(ans,V[N-1]-C+table[0][0])
#print(V[N-1]-C+table[0][0])
#print(1)
for i in range(N):
    for k in range(i+1,N):
        #print(V[i]+V[N-1]-V[k-1]-C-2*table[i][0]+table[k][0])
        #print(V[i]+V[N-1]-V[k-1]-table[i][0]-2*C+2*table[k][0])
        ans = max(ans, V[i]+V[N-1]-V[k-1]-C-2*table[i][0]+table[k][0])
        ans = max(ans,V[i]+V[N-1]-V[k-1]-table[i][0]-2*C+2*table[k][0])
print(ans)

Submission status: Time Limit Exceeded

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

V.insert(0, 0)
V.append(0)
X.insert(0, 0)
X.append(C)

clockwise = [sum(V[:i+1]) - X[i] for i in range(N+1)]
counterclockwise = [sum(V[i:]) - C + X[i] for i in range(1,N+2)]

clockwisemax = [max(clockwise[:i+1]) for i in range(N+1)]
counterclockwisemax = [max(counterclockwise[i:]) for i in range(N+1)]



value_list = []
for i in range(N+1):
    value_list.append(clockwisemax[i])
    value_list.append(clockwisemax[i] - X[i] + counterclockwisemax[i])
    value_list.append(counterclockwisemax[i])
    value_list.append(counterclockwisemax[i] - C + X[i+1] + clockwisemax[i])


print(max(value_list))

Submission status: Time Limit Exceeded

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

V.insert(0, 0)
V.append(0)
X.insert(0, 0)
X.append(C)

clockwise = [sum(V[:i+1]) for i in range(N+1)]
counterclockwise = [sum(V[i:]) for i in range(1,N+2)]


value_list = []
for i in range(N+1):
    for j in range(i, N+1):
        value = clockwise[i] + clockwise[N] - clockwise[j] - 2 * min(X[i], C - X[j+1]) - max(X[i], C - X[j+1])
        value_list.append(value)


print(max(value_list))

Submission status: Time Limit Exceeded

Solution code:
def clockwisefirst(N, C, X, V):
    clockwise = [sum(V[:i+1]) - X[i] for i in range(N)]
    counterclockwise = [sum(V[i:]) - C + X[i] for i in range(N)]
    counterclockwise.append(0)

    clockwiselist = []
    clockwiselist.append(0)

    for i in range(N):
        backwardmax = max(counterclockwise[i+1:])
        clockwiselist.append(clockwise[i] + max(int(0), backwardmax - X[i]))

    return max(clockwiselist)



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

Y = [C - X[i] for i in range(N)]

maxcal = max(clockwisefirst(N, C, X, V), clockwisefirst(N, C, Y[::-1], V[::-1]))

print(maxcal)

Submission status: Time Limit Exceeded

Solution code:
n, c = map(int, input().split())
sushi = []
for i in range(n):
    x, v = map(int, input().split())
    sushi.append((x, v))
sushi.sort()
sushi_reversed = list(reversed([(c - x, v) for x, v in sushi]))


# def solve(s, sr):
#     d = 0
#     v = 0
#     v_d_max = (0, 0)
#     v_2d_max = (0, 0)
#     conds = []

#     for x_i, v_i in s:
#         v = v + v_i - (x_i - d)
#         d = x_i
#         v_d_max = max(v_d_max, (v, d))
#         if v - d > v_2d_max[0]:
#             v_2d_max = (v - d, d)
#             conds.append((v - d, d))
#     return v_d_max, v_2d_max, conds

def solve(s, sr):
    d = 0
    v = 0
    v_2d_max = 0
    v_max = 0
    n = len(s)
    for i in range(n):
        x_i, v_i = s[i]
        v = v + v_i - (x_i - d)
        d = x_i
        v_max = max(v_max, v)
        if v - d > v_2d_max:
            v_2d_max = v - d
            v_sub = sub_solve(sr[:n - 1 - i])
            v_max = max(v_max, v_2d_max + v_sub)
    return v_max


def sub_solve(s):
    d = 0
    v = 0
    v_max = 0

    for x_i, v_i in s:
        v = v + v_i - (x_i - d)
        d = x_i
        v_max = max(v_max, v)
    return v_max

# l_v_d_max, l_v_2d_max, l_conds = solve(sushi, sushi_reversed)
# r_v_d_max, r_v_2d_max, r_conds = solve(sushi_reversed, sushi)
# print(l_v_d_max, l_v_2d_max, l_conds, r_v_d_max, r_v_2d_max, r_conds)

# max_v = max(l_v_d_max[0], r_v_d_max[0])
# if l_v_d_max[1] + r_v_2d_max[1] < c:
#     max_v = max(max_v, l_v_d_max[0] + r_v_2d_max[0])
# else:
#     for v, d in r_conds:
#         if l_v_d_max[1] + d < c:
#             max_v = max(max_v, l_v_d_max[0] + v)

# if l_v_2d_max[1] + r_v_d_max[1] < c:
#     max_v = max(max_v, l_v_2d_max[0] + r_v_d_max[0])
# else:
#     for v, d in l_conds:
#         if d + r_v_d_max[1] < c:
#             max_v = max(max_v, v + r_v_d_max[0])

# print(max_v)

# l_v_d_max, l_v_2d_max, l_conds = solve(sushi, sushi_reversed)
# r_v_d_max, r_v_2d_max, r_conds = solve(sushi_reversed, sushi)
# print(max_v)


l = solve(sushi, sushi_reversed)
r = solve(sushi_reversed, sushi)
print(max(solve(sushi, sushi_reversed), solve(sushi_reversed, sushi)))

Submission status: Time Limit Exceeded

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

lx, lv = [], []

for _ in range(n):
    tmpx, tmpv = map(int, input().split())
    lx.append(tmpx)
    lv.append(tmpv)

lux = [c-x for x in reversed(lx)]

#両方向の累積和
tmpv1, tmpv2 = 0,0
vc, vuc = [],[]
for i in range(n):
    tmpv1 += lv[i]
    tmpv2 += lv[n-1-i]
    _ = [x.append(y) for x,y in zip([vc,vuc],[tmpv1,tmpv2])]

sc, suc = [],[]
tmp1,tmp2 = 0,0
for i in range(n):
    sc.append(vc[i] - lx[i])
    suc.append(vuc[i] - (c - lx[n-1-i]))

ans = 0

s3,s4 = 0,0

for i in range(n):
    s1 = sc[i]
    s2 = suc[i]
    tmpv,tmpx,s3 = 0,0,0
    for j in range(n-1-i):
        s3 = max(s1 + suc[j] - min(lx[i],lux[j]),s3)
    ans = max(ans,s1,s2,s3)

print(ans)

Submission status: Time Limit Exceeded

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

lx, lv = [], []

for _ in range(n):
    tmpx, tmpv = map(int, input().split())
    lx.append(tmpx)
    lv.append(tmpv)

lux = [c-x for x in reversed(lx)]

#両方向の累積和
tmpv1, tmpv2 = 0,0
vc, vuc = [],[]
for i in range(n):
    tmpv1 += lv[i]
    tmpv2 += lv[n-1-i]
    _ = [x.append(y) for x,y in zip([vc,vuc],[tmpv1,tmpv2])]

sc, suc = [],[]
tmp1,tmp2 = 0,0
for i in range(n):
    sc.append(vc[i] - lx[i])
    suc.append(vuc[i] - (c - lx[n-1-i]))

ans = 0

for i in range(n):
    s1 = sc[i]
    s2 = suc[i]
    tmpv,tmpx,s3,s4 = 0,0,0,0
    for j in range(n-1-i):
        s3 = max(s1 + suc[j] - min(lx[i],lux[j]),s3)
        s4 = max(s2 + sc[j] - min(lx[j],lux[i]),s4)
    ans = max(ans,s1,s2,s3,s4)

print(ans)