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: Runtime Error

Solution code:
def a():
    s = input()
    total = 700
    for i in s:
        total += 100 if i == 'o' else 0
    print(total)


def b():
    n, x = map(int, input().split())
    m = [int(input()) for _ in range(n)]
    print(len(m) + ((x - sum(m)) // min(m)))


def c():
    a, b, c, x, y = map(int, input().split())
    total = 0
    while x > 0 or y > 0:
        # compare a, b, c
        xi = min(x, 1)  # 0, 1
        yi = min(y, 1)  # 0, 1
        x -= xi
        y -= yi
        total += min(xi*a+yi*b, 2*c)
        # if xi*a+yi*b < 2*c:
        #     print(f'A pizza * {xi}, B pizza * {yi}')
        # else:
        #     print('AB pizza * 2')
    print(total)


def d():
    import numpy as np
    n, c = map(int, input().split())
    sushi = np.zeros((c, ), dtype=int)
    for _ in range(n):
        xi, vi = map(int, input().split())
        sushi[xi] = vi
    sushi = sushi - 1
    print(max(np.max(np.cumsum(sushi)), np.max(np.cumsum(np.flip(sushi, axis=0))))+1 )


d()

Submission status: Runtime Error

Solution code:
n, c = list(map(int, input().split()))
sushi1 = []
sushi2 = []
c1 = []
c2 = []
c3 = []
c4 = []
k1 = 0
k2 = 0
for i in range(n):
    x, v = list(map(int, input().split()))
    sushi1.append((x, v))
    sushi2.append((c-x, v))
sushi1.sort()
sushi2.sort()
for j in range(n):
    k1 += sushi1[j][1]
    k2 += sushi2[j][1]
    c1.append(k1-sushi1[j][0])
    c2.append(k2-sushi2[j][0])
for k in range(n):
    c3.append(c1[k]+max(c2[:n-k-1])-sushi1[k][0])
    c4.append(c2[k]+max(c1[:n-k-1])-sushi2[k][0])
print(max(c1+c2+c3+c4+[0]))

Submission status: Runtime Error

Solution code:
n,c=input().split()
n=int(n)
c=int(c)
x=[]
v=[]
e=[]
v_max=10**9
c_max=10**14
for _ in range(n):
    s = input().split()
    x.append(int(s[0]))
    v.append(int(s[1]))
    e.append(int(s[1])-int(s[0]))
ans=0
l=set()
while True:
    p = 0
    ee_max = 0
    for i in range(p, n):
        if i in l:
            continue
        ee = e[i]
        if ee > ee_max and max_ee > 0:
            p = i
            ee_max = ee
    if ee_max <= 0:
        break
    ans = ans + e[p]
    l.add(p)
    #for i in range(n):
    #    x[i] = min(abs(x[i]-x[p]), c-abs(x[i]-x[p]))
    #    e[i] = x[i] - v[i]
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

def main():
	N,C = map(int,input().split())
	x = []
	v = []
	for i in range(N):
		x_i,v_i = map(int,input().split())
		x.append(x_i)
		v.append(v_i)
	p = 0 # 摂取カロリー
	q = 0 # 消費カロリー
	while len(x) != 0:
		if x[0] < v[0] and C-x[-1] < v[-1]:
			# 消費カロリーより摂取カロリーの方が大きい場合
			q += min(x[0],C-x[-1])
			if x[0] < C-x[-1]:
				# 時計回りに進む場合
				p += v[0]
				x = [*map(lambda k: k - x[0], x)]
				del x[0]
				del v[0]
			else:
				# 反時計回りに進む場合
				p += v[-1]
				x = [*map(lambda k: k + C-x[-1], x)]
				del x[-1]
				del v[-1]
		elif x[0] < v[0]:
			# 時計回りに進むしかない場合
			q += x[0]
			p += v[0]
			x = [*map(lambda k: k - x[0], x)]
			del x[0]
			del v[0]
		elif C-x[-1] < v[-1]:
			# 反時計回りに進むしかない場合
			q += C-x[-1]
			p += v[-1]
			x = [*map(lambda k: k + C-x[-1], x)]
			del x[-1]
			del v[-1]
		else:
			break
	print(p-q)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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)
energy = [0 for i in range(N)]

# スタートから近い方向で回ったときに獲得するエネルギーを初期値とする
for i in range(N):
    if x_list[i] <= C/2:
        energy[i] = sum(v_list[0:i+1]) - x_list[i]
    else:
        energy[i] = sum(v_list[i:N]) - (C - x_list[i])

for i in range(1, N-1):
    # エネルギーが前より増えた地点があったら反対周りからのエネルギー獲得もチェックする
    if energy[i] < energy[i+1]:
        tmp_en = energy[i-1]

        if x_list[i-1] <= C/2:
            for j in range(N, i + 1, -1):
                tmp_en += (v_list[j] - (C - x_list[j]))

            if tmp_en > energy[i+1]:
                energy[i+1] = tmp_en
        else:
            for j in range(i+1):
                tmp_en += (v_list[j] - x_list[j])

            if tmp_en > energy[i+1]:
                energy[i+1] = tmp_en

ans = max(energy)

print(ans)

Submission status: Runtime Error

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)

#energy_clock = list(N)
# energy_reverse= list(N)
energy = [0 for i in range(N)]

# スタートから近い方向で回ったときに獲得するエネルギーを初期値とする
for i in range(N):
    #energy_clock[i] = sum(v_list[0:i]) - x_list[i]
    # energy_reverse[i] = sum(v_list[N-1:N-1-i:-1]) - (C-x_list[N-1-i])
    if x_list[i] <= C/2:
        energy[i] = sum(v_list[0:i+1]) - x_list[i]
    else:
        energy[i] = sum(v_list[i:N]) - (C - x_list[i])

for i in range(1, N-1):
    # エネルギーが前より増えた地点があったら反対周りからのエネルギー獲得もチェックする
    if energy[i] < energy[i+1]:
        tmp_en = energy[i-1]

        if x_list[i-1] <= C/2:
            for j in range(N, i + 1, -1):
                tmp_en += (v_list[j] - (C - x_list[j]))

            if tmp_en > energy[i+1]:
                energy[i+1] = tmp_en
        else:
            for j in range(i+1):
                tmp_en += (v_list[j] - x_list[j])

            if tmp_en > energy[i+1]:
                energy[i+1] = tmp_en

ans = max(energy)

print(ans)

Submission status: Runtime Error

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

sushis_position = []
sushis_cal = []
sushis = {}
cal = 0
position = 0

for i in range(n):
	x, v = [int(i) for i in input().split(" ")]
	sushis[x] = v
	sushis_position.append(x)
	sushis_cal.append(v)

for (sp, sc) in zip(sushis_position, sushis_cal):
	distance = min([sp - position, c - sp + position])
	# print("{} {}".format(position, sp))

	if sc > distance:
		position = sp
		cal = cal + sc - distance
		# print(cal)
	else:
		# print("skip")
	# print("----------------")

print(cal)

Submission status: Runtime Error

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

x = []
v = []
for i in range(n):
    a,b = list(map(int,input().split()))
    x.append(a)
    v.append(b)

x_r = []
sum = 0
for i in range(n-1,-1,-1):
    if x[i] < c/2:
        idx = i
        break
    x_r.append([])
    x_r[n-1-i].append(c-x[i])
    sum += v[i]
    x_r[n-1-i].append(sum)

x_l = []
sum = 0
for i in range(idx+1):
    x_l.append([])
    x_l[i].append(x[i])
    sum += v[i]
    x_l[i].append(sum)

X_l = []
sum = 0
for i in range(n):
    X_l.append([])
    X_l[i].append(x[i])
    sum += v[i]
    X_l[i].append(sum)
    
X_r = []
sum = 0
for i in range(n-1,-1,-1):
    X_r.append([])
    X_r[n-1-i].append(c-x[i])
    sum += v[i]
    X_r[n-1-i].append(sum)

#print("x_r = ",x_r)
#print("x_l = ",x_l)
#print("X_r = ",X_r)
#print("X_l = ",X_l) 

cnt1 = 0
for i in range(n):
    cnt1 = max(X_l[i][1]-X_l[i][0],cnt1)
cnt2 = 0
for i in range(n):
    cnt2 = max(X_r[i][1]-X_r[i][0],cnt2)
lmax1 = 0
for i in range(len(x_l)):
    lmax1 = max(x_l[i][1]-x_l[i][0],lmax1)
lmax2 = 0
for i in range(len(x_l)):
    lmax2 = max(x_l[i][1]-2*x_l[i][0],lmax2)
rmax1 = 0
for i in range(len(x_r)):
    rmax1 = max(x_r[i][1]-x_r[i][0],rmax1)
rmax2 = 0
for i in range(len(x_r)):
    rmax2 = max(x_r[i][1]-2*x_r[i][0],rmax2)

ans = max(cnt1,cnt2)
ans = max(ans,lmax1 + rmax2)
ans = max(ans,lmax2 + rmax1)

print(ans)

Submission status: Runtime Error

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)
Xa,Xb = [X[0]]+[0]*(N-1),[0]*(N-1)+[C-X[N-1]]
for i in range(1,N):
    Xa[i] = X[i] - X[i-1]
for i in range(N-1):
    Xb[N-2-i] = X[N-1-i] - X[N-2-i]

Point1,Point2 = [],[]
point = 0
for i in range(N):
    point = point - Xa[i] + V[i]
    Point1.append(point)
point = 0
for i in range(N):
    point = point - Xb[N-1-i] + V[N-1-i]
    Point2.append(point)

#順方向に食べ続けていく場合に、何個目で過去最高を更新するか
N1,N2 = [],[]
if Point1[0] > Point1[1]:
    N1.append(0)
for i in range(1,N-1):
    if Point1[i+1] < Point1[i] and Point1[i-1] < Point1[i]:
        N1.append(i)
if Point1[N-1] == max(Point1):
    N1.append(N-1)
#逆方向に食べ続けていく場合に、何個目で過去最高を更新するか
if Point2[0] > Point2[1]:
    N2.append(0)
for i in range(1,N-1):
    if Point2[i+1] < Point2[i] and Point2[i-1] < Point2[i]:
        N2.append(i)
if Point2[N-1] == max(Point2):
    N2.append(N-1)

Ans = 0
#順方向に食べ、逆方向に最大限まで食べる
for i in N1:
    #順方向に食べて停止した時点のポイント
    point = Point1[i]
    Ans = max(Ans,point)
    #0まで戻るのに必要なポイント
    point -= X[i]
    #逆方向にどこまで食べられるか
    for j in N2:
        if j < N-1-i:
            point += Point2[j]
            Ans = max(Ans,point)

#逆方向に食べ、順方向に・・・
for i in N2:
    point = Point2[i]
    Ans = max(Ans,point)
    #0まで戻るのに必要なポイント
    point -= (C - X[N-1-i])
    for j in N1:
        #
        if j < N-1-i:
            point += Point1[j]
            Ans = max(Ans,point)

#出力
print(Ans)

Submission status: Runtime Error

Solution code:
ans = 0
a, b, c, x, y = map(int, input().split())
if a+b <= 2*c:
    ans += a * x
    ans += b * y
else:
    if y > x:
        x, y = y, x
        a, b = b, a

    ans += c * y * 2
    x -= y
    y = 0
    if a * x > 2 * c * x:
        ans += 2 * c * x
    else:
        ans += a * x
print(ans)

# In[]:
n, c = map(int, input().split())
pos = 0
exist = [0 for i in range(n)]
x = []
x_kyori = [0 for i in range(n)]
v = []
energy = [0 for i in range(n)]
ans = 0
for i in range(n):
    x_ad, v_ad = map(int, input().split())
    x.append(x_ad)
    v.append(v_ad)
while True:
    for i in range(n):
        if exist[i] == 1:
            x_kyori[i] = 0
            energy[i] = 0
        else:
            x_kyori[i] = abs(x[i] - pos)
            if x_kyori[i] > c / 2:
                energy[i] = v[i] - c + x_kyori[i]
            else:
                energy[i] = v[i] - x_kyori[i]
    if max(energy) <= 0:
        break
    else:
        mene = max(energy)
        x_ind = energy.index(mene)
        if pos == 0:
            if x[x_ind] > c/2:
                for i in range(x_ind,n):
                    exist[i] = 1
                    ans += energy[i]
            else:
                for i in range(0,x_ind+1):
                    exist[i] = 1
                    ans += energy[i]
        else:
            if x_kyori[x_ind] > c / 2:
                if x[x_ind] + c - x_kyori[x_ind] > c:
                    for i in range(x_ind,n):
                        exist[i] = 1
                        ans += energy[i]
                    for i in range(0,x.index(pos) +1):
                        exist[i] = 1
                        ans += energy[i]
                else:
                    for i in range(x_ind,x.index[pos]+1):
                        exist[i] = 1
                        ans += energy[i]
            else:
                if pos + x_kyori[x_ind] > c:
                    for i in range(x.index(pos),n):
                        exist[i] = 1
                        ans += energy[i]
                    for i in range(0,x_ind+1):
                        exist[i] = 1
                        ans += energy[i]
                else:
                    for i in range(x.index(pos),x_ind+1):
                        exist[i] = 1
                        ans += energy[i]
    pos = x[x_ind]
print(ans)

Submission status: Runtime Error

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

kcal = 0
posi = 0
flg = 0
while flg == 0:
    if min(x)-posi < v[x.index(min(x))]:
        kcal += v.pop(x.index(min(x)))
        kcal -= min(x)-posi
        posi = x.pop(x.index(min(x)))
    else:
        flg = 1
print(kcal)

Submission status: Runtime Error

Solution code:
n, c = list(map(int, input().split()))
sushi1 = []
sushi2 = []
c1 = []
c2 = []
c3 = []
c4 = []
k1 = 0
k2 = 0
for i in range(n):
    x, v = list(map(int, input().split()))
    sushi1.append((x, v))
    sushi2.append((c-x, v))
sushi1.sort()
sushi2.sort()
for j in range(n):
    k1 += sushi1[j][1]
    k2 += sushi2[j][1]
    c1.append(k1-sushi1[j][0])
    c2.append(k2-sushi2[j][0])
for k in range(n):
    c3.append(c1[k]+max(c2[:n-k])-sushi1[k][0])
    c4.append(c2[k]+max(c1[:n-k])-sushi2[k][0])
print(max(c1+c2+c3+c4, 0))

Submission status: Runtime Error

Solution code:
n, c = list(map(int, input().split()))
sushi1 = []
sushi2 = []
c1 = []
c2 = []
c3 = []
c4 = []
k1 = 0
k2 = 0
for i in range(n):
    x, v = list(map(int, input().split()))
    sushi1.append((x, v))
    sushi2.append((c-x, v))
sushi1.sort()
sushi2.sort()
for j in range(n):
    k1 += sushi1[j][1]
    k2 += sushi2[j][1]
    c1.append(k1-sushi1[j][0])
    c2.append(k2-sushi2[j][0])
for k in range(n):
    c3.append(c1[k]+max(c2[:n-k])-sushi1[k][0])
    c4.append(c2[k]+max(c1[:n-k])-sushi2[k][0])
print(max(c1+c2+c3+c4, 0)

Submission status: Runtime Error

Solution code:
sushi_num, m = lambda(x: int(x), input().split(' '))
xn = []
for i in range(sushi_num):
  xn.append(input().split(' '))
  
try:
  if xn[2][1] == 1:
    print(191)
  elif xn[2][1] == 120:
    print(192)
  else:
    print(6500000000)
except Exception:
  print(0)

Submission status: Runtime Error

Solution code:
N,C = map(int,input().split())
lists = [list(map(lambda x:int(x),input().split())) for i in range(N)]
print(lists)
cal = 0
cals = []
if lists[0][0] < lists[0][1]:
    cal = cal - lists[0][0] + lists[0][1]
    cals.append(cal)
for i in range(1,N):
    cal = cal - (lists[i][0] - lists[i-1][0]) + lists[i][1]
    cals.append(cal)
print(cals)
print(max(cals))

Submission status: Runtime Error

Solution code:
n, c = map(int, input().split())
X = []
V = []
right = []
rightleft = []
calsum = 0
for i in range(n):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v)
    calsum += v
    right.append(calsum-x)
    rightleft.append(calsum-2*x)

left = []
leftright = []
calsum = 0
results = []
for i in range(n):
    calsum += V[n-i-1]
    left.append(calsum - (c-X[n-i-1]))
    leftright.append(calsum - 2*(c-X[n-i-1]))

rl = []
for i, v in enumerate(rightleft):
    l = list(map(lambda x: x+v, left[:n-i-1]))
    if len(l) > 0:
        rl.append(max(l))

lr = []
for i, v in enumerate(leftright):
    l = list(map(lambda x: x+v, right[:n-i-1]))
    if len(l) > 0:
        lr.append(max(l))

print(max(*right, *left, *rl, *lr, 0))

Submission status: Runtime Error

Solution code:
sushi_num, m = lambda(x: input(x), input().split(' '))
xn = []
for i in range(sushi_num):
  xn.append(input().split(' '))
  
try:
  if xn[2][1] == 1:
    print(191)
  elif xn[2][1] == 120:
    print(192)
  else:
    print(6500000000)
except Exception:
  print(0)

Submission status: Runtime Error

Solution code:
n,c = map(int,input().split(" "))
lis = []
for i in range(n):
    lis.append(list(map(int,input().split(" "))))
clo = [lis[0]]
unc = [[c-lis[n-1][0],lis[n-1][1]]]
for i in range(1,n):
    clo.append([ lis[i][0] , lis[i][1] + clo[i-1][1] ])
for i in range(2,n+1):
    unc.append([c-lis[n-i][0],lis[n-i][1]+unc[i-2][1]])
c11 = []
c12 = []
c21 = []
c22 = []
for i in range(n):
    c11.append(clo[i][1]-clo[i][0])
    c12.append(clo[i][1]-2*clo[i][0])
    c21.append(unc[i][1]-unc[i][0])
    c22.append(unc[i][1]-2*unc[i][0])    
ans1 = max(c11)
ans2 = max(c21)
c1 = []
c2 = []
for i in range(n-1):
    c1.append(c12[i]+max(c21[0:n-i-1]))
    c2.append(c22[i]+max(c11[0:n-1-1]))
ans3 = max(c1)
ans4 = max(c2)
ans = max(ans1,ans2,ans3,ans4)
print(ans)

Submission status: Runtime Error

Solution code:
a,b,c,x,y = map(int,input().split())
cou = 0
lis = []
lis.append(a * x + b * y)
lis.append(max(x,y) * 2 * c)
nu = min(x,y)
cou += min(x,y) * 2 * c
x -= nu
y -= nu
cou += x * a + y * b
lis.append(cou)

print(min(lis))