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:
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]))
tm=max(t[0:-1])
hm=max(h[0:-ti])
h2=[h[i]-c+x[-i] for i in range(1,len(h))]
t2=[t[i]-x[i] for i in range(1,len(t))]
print(h,t)
print(h2,t2)

#print(ti,hi)
print(max(max(t), max(h), hm+max(t2[0:-ti]),tm+max(h2[:-hi])))

Submission status: Runtime Error

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]))
tm=max(t[0:-1])
hm=max(h[0:-ti])
h2=[h[i]-c+x[-i] for i in range(1,len(h))]
t2=[t[i]-x[i] for i in range(1,len(h))]
print(h,t)
print(h2,t2)

#print(ti,hi)
print(max(max(t), max(h), hm+max(t2[0:-ti]),tm+max(h2[:-hi])))

Submission status: Runtime Error

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

import sys
sys.setswitchinterval(1000000)


N, C = list(map(int, input().split(' ')))
X = []
V = []
for n in range(N):
    X_, V_ = list(map(int, input().split(' ')))
    X.append(X_)
    V.append(V_)


def sushi(sum_v, ichi, xs, vs):
    e = sum_v
    if len(xs) == 0:
        return e
    if len(xs) == 1:
        m = (C + xs[0] - ichi) % C
        move_v = min(m, C - m)
        return max(e, sum_v - move_v + vs[0])

    if ichi < xs[0]:
        R_next_idx, L_next_idx = (0, len(xs) - 1)
    elif xs[-1] < ichi:
        R_next_idx, L_next_idx = (0, len(xs) - 1)
    else:
        for i in range(len(xs) - 1):
            if xs[i] < ichi < xs[i + 1]:
                R_next_idx, L_next_idx = (i + 1, i)
                break

    # 右周り
    R_x = xs[R_next_idx]
    R_v = sum_v + vs[R_next_idx]
    R_v -= (C + R_x - ichi) % C
    R_xs = xs[:R_next_idx] + xs[R_next_idx + 1:]
    R_vs = vs[:R_next_idx] + vs[R_next_idx + 1:]

    # 左周り
    L_x = xs[L_next_idx]
    L_v = sum_v + vs[L_next_idx]
    L_v -= (C + ichi - L_x) % C
    L_xs = xs[:L_next_idx] + xs[L_next_idx + 1:]
    L_vs = vs[:L_next_idx] + vs[L_next_idx + 1:]

    r = sushi(R_v, R_x, R_xs, R_vs)
    l = sushi(L_v, L_x, L_xs, L_vs)

    return max(e, r, l)


print(sushi(0, 0, X, V))

Submission status: Runtime Error

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]))
tm=max(t[0:-1])
hm=max(h[0:-ti])
h2=[h[i]-x[i] for i in range(1,len(h))]
t2=[t[i]-x[-i-1] for i in range(1,len(h))]
#print(h,t)
#print(h2,t2)

#print(ti,hi)
print(max(max(t), max(h), hm+max(t2[0:-ti]),tm+max(h2[:-hi])))
#print(hm, tm, hm+max(t2[0:-ti]),tm+max(h2[:-hi]))

Submission status: Runtime Error

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

import sys
sys.setswitchinterval(1000000)


N, C = list(map(int, input().split(' ')))
X = []
V = []
for n in range(N):
    X_, V_ = list(map(int, input().split(' ')))
    X.append(X_)
    V.append(V_)


def sushi(sum_v, ichi, xs, vs):
    e = sum_v
    if len(xs) == 0:
        return e

    if ichi < xs[0]:
        R_next_idx, L_next_idx = (0, len(xs) - 1)
    elif xs[-1] < ichi:
        R_next_idx, L_next_idx = (0, len(xs) - 1)
    else:
        for i in range(len(xs) - 1):
            if xs[i] < ichi < xs[i + 1]:
                R_next_idx, L_next_idx = (i + 1, i)
                break

    # 右周り
    R_x = xs[R_next_idx]
    R_v = sum_v + vs[R_next_idx]
    R_v -= (C + R_x - ichi) % C
    R_xs = xs[:R_next_idx] + xs[R_next_idx + 1:]
    R_vs = vs[:R_next_idx] + vs[R_next_idx + 1:]

    # 左周り
    L_x = xs[L_next_idx]
    L_v = sum_v + vs[L_next_idx]
    L_v -= (C + ichi - L_x) % C
    L_xs = xs[:L_next_idx] + xs[L_next_idx + 1:]
    L_vs = vs[:L_next_idx] + vs[L_next_idx + 1:]

    r = sushi(R_v, R_x, R_xs, R_vs)
    l = sushi(L_v, L_x, L_xs, L_vs)

    return max(e, r, l)


print(sushi(0, 0, X, V))

Submission status: Runtime Error

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]))
tm=max(t[0:-1])
hm=max(h[0:-ti])
h2=[h[i]-x[i] for i in range(len(h))]
t2=[t[i]-x[-i-1] for i in range(1,len(h))]
#print(h2,t2)

#print(ti,hi)
print(max( hm, tm, hm+max(t2[0:-ti]),tm+max(h2[:-ti])))

Submission status: Runtime Error

Solution code:
N, C = map(int, raw_input().split())
a = []
d = []
A = []
AA = []
for i in range(N):
	x, v = map(int, raw_input().split())
	a.append([x, C-x, v])
	d.append([])
d[0] = a[0][2] - a[0][0]
d[N-1] = a[N-1][2] - a[N-1][1]
A.append(0)
A.append(N-1)
AA.append(d[0])
AA.append(d[N-1])
while True:
	c = A[AA.index(max(AA))]
	if c == 0:
		nx = 1
		pv = N - 1
	elif c == N-1:
		nx = 0
		pv = N - 2
	else:
		nx = c + 1
		pv = c - 1
	if d[nx] == []:
		d[nx] = d[c] + a[nx][2] - a[nx][0]
		A.append(nx)
		AA.append(d[nx])
	elif d[nx] < d[c] + a[nx][2] - a[nx][0]:
		d[nx] = d[c] + a[nx][2] - a[nx][0]
	if d[pv] == []:
		d[pv] = d[c] + a[pv][2] - a[pv][1]
		A.append(pv)
		AA.append(d[pv])
	elif d[pv] < d[pv] + a[pv][2] - a[pv][1]:
		d[pv] = d[c] + a[pv][2] - a[pv][1]
	A.pop(c)
	AA.pop(c)
	if nx == pv:
		print max(d)

Submission status: Runtime Error

Solution code:
N, C = map(int, raw_input().split())
a = []
d = []
A = []
AA = []
for i in range(N):
	x, v = map(int, raw_input().split())
	a.append(x, C-x, v)
	d.append([])
d[0] = a[0][2] - a[0][0]
d[N-1] = a[N-1][2] - a[N-1][1]
A.append(0)
A.append(N-1)
AA.append(d[0])
AA.append(d[N-1])
while True:
	c = A[AA.index(max(AA))]
	if c == 0:
		nx = 1
		pv = N - 1
	elif c == N-1:
		nx = 0
		pv = N - 2
	else:
		nx = c + 1
		pv = c - 1
	if d[nx] == []:
		d[nx] = d[c] + a[nx][2] - a[nx][0]
		A.append(nx)
		AA.append(d[nx])
	elif d[nx] < d[c] + a[nx][2] - a[nx][0]:
		d[nx] = d[c] + a[nx][2] - a[nx][0]
	if d[pv] == []:
		d[pv] = d[c] + a[pv][2] - a[pv][1]
		A.append(pv)
		AA.append(d[pv])
	elif d[pv] < d[pv] + a[pv][2] - a[pv][1]:
		d[pv] = d[c] + a[pv][2] - a[pv][1]
	A.pop(c)
	AA.pop(c)
	if nx == pv:
		print max(d)

Submission status: Runtime Error

Solution code:
N, C = map(int, raw_input().split())
a = []
d = []
A = []
AA = []
next = 0
prev = N-1
for i in range(N):
	x, v = map(int, raw_input().split())
	a.append(x, C-x, v)
	d.append([])
d[0] = a[0][2] - a[0][0]
d[N-1] = a[N-1][2] - a[N-1][1]
A.append(0)
A.append(N-1)
AA.append(d[0])
AA.append(d[N-1])
while(true):
	c = A[AA.index(max(AA))]
	if c == 0:
		next = 1
		prev = N - 1
	elif c == N-1:
		next = 0
		prev = N - 2
	else:
		next = c + 1
		prev = c - 1
	if d[next] == []:
		d[next] = d[c] + a[next][2] - a[next][0]
		A.append(next)
		AA.append(d[next])
	elif d[next] < d[c] + a[next][2] - a[next][0]:
		d[next] = d[c] + a[next][2] - a[next][0]
	if d[prev] == []:
		d[prev] = d[c] + a[prev][2] - a[prev][1]
		A.append(prev)
		AA.append(d[prev])
	elif d[prev] < d[prev] + a[prev][2] - a[prev][1]:
		d[prev] = d[c] + a[prev][2] - a[prev][1]
	A.pop(c)
	AA.pop(c)
	if next == prev:
		print max(d)

Submission status: Runtime Error

Solution code:
N, C = map(int, raw_input().split())
a = []
d = []
A = []
AA = []
next = 0
prev = N-1
for i in range(N):
	x, v = map(int, raw_input().split())
	a.append(x, C-x, v)
	d.append([])
	p.append([])
d[0] = a[0][2] - a[0][0]
d[N-1] = a[N-1][2] - a[N-1][1]
A.append(0)
A.append(N-1)
AA.append(d[0])
AA.append(d[N-1])
while(true):
	c = A[AA.index(max(AA))]
	if c == 0:
		next = 1
		prev = N - 1
	elif c == N-1:
		next = 0
		prev = N - 2
	else:
		next = c + 1
		prev = c - 1
	if d[next] == []:
		d[next] = d[c] + a[next][2] - a[next][0]
		A.append(next)
		AA.append(d[next])
	elif d[next] < d[c] + a[next][2] - a[next][0]:
		d[next] = d[c] + a[next][2] - a[next][0]
	if d[prev] == []:
		d[prev] = d[c] + a[prev][2] - a[prev][1]
		A.append(prev)
		AA.append(d[prev])
	elif d[prev] < d[prev] + a[prev][2] - a[prev][1]:
		d[prev] = d[c] + a[prev][2] - a[prev][1]
	if next == prev:
		print max(d)

Submission status: Runtime Error

Solution code:
N,C = map(int, input().split())
dis = []
cal = []
for i in range(N):
    x,v = map(int, input().split())
    dis.append(x)
    cal.append(v)
r = [(cal[0] - dis[0])]
for i in range(N-1):
    r.append(r[i] + dis[i] + cal[i+1] - dis[i+1])
l = [(cal[-1] - (C - dis[-1]))]
for i in range(N-1):
    l.append(l[i] - dis[-i-1] + cal[-i-2] + dis[-i-2])
r2 = [0]
l2 = [0]
for i in range(N):
    r2.append(r[i] - dis[i])
    l2.append(l[i] - (C - dis[-i-1]))
r3 = []
l3 = []
for i in range(N-1):
    r3.append(r[i] + max(l2[:-i-1]))
    l3.append(l[i] + max(r2[:-i-1]))
print(max(max(r3),max(l3)))

Submission status: Runtime Error

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

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):
	b.append(b[i-1] + a[i][1] - a[i][0])
c.append(a[N][1] + a[N][0] - C)
for i in range(1, N):
	c.append(c[i-1] + a[N-i][1] + a[N-i][0] - C)
if max(b) < max(c):
	print max(c)
else:
	print max(b)

Submission status: Runtime Error

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):
	b.append(b[i-1] + a[i][1] - a[i][0])
c.append(a[N][1] + a[N][0] - C)
for i in range(1, N):
	c.append(c[i-1] + a[N-i][1] + a[N-i][0] - C)
if max(b) < max(c):
	print max(c)
else:
	print max(b)