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:
#!/usr/bin/env python3
import sys
from collections.abc import Iterable
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, C: int, x: "List[int]", v: "List[int]"):
    x = x
    v = v
    bi = []
    best = -float('inf') 
    besti = 0
    acc_v = list(accumulate(v, lambda acc, vv: acc + vv))
    for ii, (xx, vv) in enumerate(zip(x, acc_v)):
        if best < vv - xx:
            besti = ii
            best = vv - xx
        bi.append(besti)

    ret = 0
    for b in range(len(bi)):
        a = bi[b - 1]
        ca = max(0, acc_v[a] - x[a])
        cb = max(0, (acc_v[-1] - acc_v[b-1]) - (C - x[b]))
        c = ca + cb
        if 0 < ca and 0 < cb:
            c -= min(x[a], (C - x[b]))
        if ret < c:
            ret = c
    return ret

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    x = [int()] * (N)  # type: "List[int]" 
    v = [int()] * (N)  # type: "List[int]" 
    for i in range(N):
        x[i] = int(next(tokens))
        v[i] = int(next(tokens))
    result = solve(N, C, x, v)
    if isinstance(result, Iterable):
        result = '\n'.join([str(v) for v in result])
    print(result)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections.abc import Iterable
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, C: int, x: "List[int]", v: "List[int]"):
    x = x
    v = v
    bi = []
    best = 0
    besti = 0
    acc_v = list(accumulate(v, lambda acc, vv: acc + vv))
    for ii, (xx, vv) in enumerate(zip(x, acc_v)):
        if best < vv - xx:
            besti = ii
            best = vv - xx
        bi.append(besti)

    ret = 0
    for b in range(len(bi)):
        a = bi[b - 1]
        ca = max(0, acc_v[a] - x[a])
        cb = max(0, (acc_v[-1] - acc_v[b-1]) - (C - x[b]))
        c = ca + cb
        if 0 < ca and 0 < cb:
            c -= min(x[a], (C - x[b]))
        if ret < c:
            ret = c
    return ret

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    x = [int()] * (N)  # type: "List[int]" 
    v = [int()] * (N)  # type: "List[int]" 
    for i in range(N):
        x[i] = int(next(tokens))
        v[i] = int(next(tokens))
    result = solve(N, C, x, v)
    if isinstance(result, Iterable):
        result = '\n'.join([str(v) for v in result])
    print(result)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections.abc import Iterable
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, C: int, x: "List[int]", v: "List[int]"):
    x = [0] + x
    v = [0] + v
    bi = []
    best = 0
    besti = 0
    acc_v = list(accumulate(v, lambda acc, vv: acc + vv))
    for ii, (xx, vv) in enumerate(zip(x, acc_v)):
        if best < vv - xx:
            besti = ii
            best = vv - xx
        bi.append(besti)

    ret = 0
    for b in range(len(bi)):
        a = bi[b - 1]
        c = 0
        if 0 < a:
            c += acc_v[a] - x[a]
        if 0 < b:
            c += (acc_v[-1] - acc_v[b-1]) - ((C - x[b]) % C)
        if 0 < a and 0 < b:
            c -= min(x[a], ((C - x[b]) % C))
        if ret < c:
            ret = c
    return ret

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    x = [int()] * (N)  # type: "List[int]" 
    v = [int()] * (N)  # type: "List[int]" 
    for i in range(N):
        x[i] = int(next(tokens))
        v[i] = int(next(tokens))
    result = solve(N, C, x, v)
    if isinstance(result, Iterable):
        result = '\n'.join([str(v) for v in result])
    print(result)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from collections.abc import Iterable
from math import *
from itertools import *
from collections import *
from functools import *
from operator import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, C: int, x: "List[int]", v: "List[int]"):
    x = [0] + x
    v = [0] + v
    bi = []
    best = 0
    besti = 0
    acc_v = list(accumulate(v, lambda acc, vv: acc + vv))
    for ii, (xx, vv) in enumerate(zip(x, acc_v)):
        if best < vv - xx:
            besti = ii
            best = vv - xx
        bi.append(besti)

    ret = 0
    for b in range(len(bi)):
        a = bi[b - 1]
        c = 0
        if a != 0:
            c += acc_v[a] - x[a]
        if b != 0:
            c += (acc_v[-1] - acc_v[b-1]) - ((C - x[b]) % C)
        c -= min(x[a], ((C - x[b]) % C))
        if ret < c:
            ret = c
    return ret

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    x = [int()] * (N)  # type: "List[int]" 
    v = [int()] * (N)  # type: "List[int]" 
    for i in range(N):
        x[i] = int(next(tokens))
        v[i] = int(next(tokens))
    result = solve(N, C, x, v)
    if isinstance(result, Iterable):
        result = '\n'.join([str(v) for v in result])
    print(result)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,C = map(int,input().split())
L = []
for i in range(N):
  L.append(list(map(int,input().split())))
A = [0]
k = 0
for i in range(N):
  k += L[i][1]
  p = max(A[i],k-L[i][0])
  A.append(p)
ans = A[N]
t = 0
for i in range(N):
  b = L[N-i-1][0]
  t += L[N-i-1][1]
  if ans <= t-2*(C-b)+A[N-i-1]:
    ans = t-2*(C-b)+A[N-i-1]
B = [0]
k = 0
for i in range(N):
  k += L[N-i-1][1]
  p = max(B[i],k-(C-L[N-i-1][0]))
  B.append(p)
t = 0
for i in range(N):
  a = L[i][0]
  t += L[i][1]
  if ans <= t-2*a+B[N-i-1]:
    ans = t-2*a+B[N-i-1]
print(ans)

Submission status: Wrong Answer

Solution code:
N,C = map(int,input().split())
L = []
for i in range(N):
  L.append(list(map(int,input().split())))
A = [[0,0]]
k = 0
for i in range(N):
  k += L[i][1]
  p = max(A[i][0],k-L[i][0])
  if A[i][0] >= k-L[i][0]:
    A.append([p,A[i][1]])
  else:
    A.append([p,L[i][0]])
ans = A[N][0]
t = 0
for i in range(N):
  b = L[N-i-1][0]
  t += L[N-i-1][1]
  if ans <= t-(C-b)+A[N-i-1][0]-min(A[N-i-1][1],C-b):
    ans = t-(C-b)+A[N-i-1][0]-min(A[N-i-1][1],C-b)
print(ans)

Submission status: Wrong Answer

Solution code:
N,C=map(int, input().split())
inarr=[[int(i) for i in input().split()] for _ in range(N)]
maxes=[(0, 0)]*N
cums=[0]*(N+1)
cum = 0
cummax = (0, 0)
for i, [a, v] in enumerate(inarr):
    cum += v
    if (cum - a) > cummax[0]:
        cummax = (cum - a, a)
    maxes[i]=cummax
    cums[i+1]=cum

resmax = 0
for i, [b, v] in enumerate(inarr):
    oasum, x = (0, 0) if i == 0 else maxes[i-1]
    obsum = cums[N] - cums[i] - (C-b)
    # print("len oa: {} len ob: {} oa sum: {} ob sum: {} score: {}".format(x, C-b, oasum, obsum, obsum + oasum - min(C-b, x)))
    resmax = max(resmax, obsum + oasum - min(C-b, x))
    # print(resmax)

# print(maxes[N-1])
print(max(resmax, maxes[N-1][0], cum - (C-inarr[0][0]), 0))

Submission status: Wrong Answer

Solution code:
N,C=map(int, input().split())
inarr=[[int(i) for i in input().split()] for _ in range(N)]
maxes=[(0, 0)]*N
cums=[0]*(N+1)
cum = 0
cummax = (0, 0)
for i, [a, v] in enumerate(inarr):
    cum += v
    if (cum - a) > cummax[0]:
        cummax = (cum - a, a)
    maxes[i]=cummax
    cums[i+1]=cum

# print("cummax: {}, cums: {}".format(cummax, cums))
# if cummax[0] == 0:
#     print(0)
#     exit()

resmax = 0
for i, [b, v] in enumerate(inarr):
    oasum, x = (0, 0) if i == 0 else maxes[i-1]
    obsum = cums[N] - cums[i] - (C-b)
    # print("len oa: {} len ob: {} oa sum: {} ob sum: {} score: {}".format(x, C-b, oasum, obsum, obsum + oasum - min(C-b, x)))
    resmax = max(resmax, obsum + oasum - min(C-b, x))
    # print(resmax)

# print(maxes[N-1])
resmax = max(resmax, maxes[N-1][0])
print(resmax if resmax > 0 else 0)

Submission status: Wrong Answer

Solution code:
N,C=map(int, input().split())
inarr=[[int(i) for i in input().split()] for _ in range(N)]
maxes=[(0, 0)]*N
cums=[0]*(N+1)
cum = 0
cummax = (0, 0)
for i, [a, v] in enumerate(inarr):
    cum += v
    if (cum - a) > cummax[0]:
        cummax = (cum - a, a)
    maxes[i]=cummax
    cums[i+1]=cum

# print("cummax: {}, cums: {}".format(cummax, cums))
if cummax[0] == 0:
    print(0)
    exit(0)

resmax = 0
for i, [b, v] in enumerate(inarr):
    oasum, x = (0, 0) if i == 0 else maxes[i-1]
    obsum = cums[N] - cums[i] - (C-b)
    # print("len oa: {} len ob: {} oa sum: {} ob sum: {} score: {}".format(x, C-b, oasum, obsum, obsum + oasum - min(C-b, x)))
    resmax = max(resmax, obsum + oasum - min(C-b, x))
    # print(resmax)

# print(maxes[N-1])
resmax = max(resmax, maxes[N-1][0])
print(resmax)

Submission status: Wrong Answer

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

max_e = 0

# clock
e = 0
for x, v in ss:
    e += v
    max_e = max(max_e, e - x)

# aclock
e = 0
for x, v in ss:
    e += v
    max_e = max(max_e, e - x)


# aclock -> clock
clocks = []
e = 0
for x, v in ss:
    e += v
    clocks.append(e - x)

max_clocks = clocks[:]
for i in range(1, len(clocks)):
    max_clocks[i] = max(clocks[i], max_clocks[i-1])

e = 0
for i in range(len(ss) - 1, 0, -1):
    x, v = ss[i]
    e += v
    max_e = max(max_e, e - 2 * (c - x) + max_clocks[i-1])

# clock -> aclock
aclocks = []
e = 0
for x, v in ss[::-1]:
    e += v
    aclocks.append(e - (c - x))

max_aclocks = aclocks[:]
for i in range(1, len(aclocks)):
    max_aclocks[i] = max(aclocks[i], max_aclocks[i-1])

aclocks.reverse()
max_aclocks.reverse()

e = 0
for i in range(len(ss) - 1):
    x, v = ss[i]
    e += v
    max_e = max(max_e, e - 2 * x + max_aclocks[i+1])

print(max_e)

Submission status: Wrong Answer

Solution code:
import numpy as np

N,C = list(map(int, input().split()))
XV = [list(map(int, input().split())) for _ in range(N)]
X = np.array([x for x,v in XV])
V = np.array([v for x,v in XV])

V_cw_cumsum = V.cumsum()

right_one_ways = V_cw_cumsum - X

V_ccw_cumsum = V[::-1].cumsum()[::-1]

left_one_ways = V_ccw_cumsum - (C-X)

def reduce_each(func, l):
  temp = l.copy()
  for i in range(1,len(l)):
    temp[i] = func(temp[i-1], temp[i])
  return temp


additional_left = np.roll(reduce_each(max, left_one_ways[::-1])[::-1], -1)
additional_left[-1] = 0
right_to_left = V_cw_cumsum - 2*X + additional_left

additional_right = np.roll(reduce_each(max, right_one_ways), 1)
additional_right[0] = 0
left_to_right = V_ccw_cumsum - 2*(C-X) + additional_right

print(max(max(right_one_ways),
          max(left_one_ways),
          max(right_to_left),
          max(left_to_right)))

Submission status: Wrong Answer

Solution code:
N, C = map(int,input().split())
xv = [[C,0]]+[list(map(int,input().split())) for _ in range(N)] + [[C,0]]

ans = 0
Sv = [xv[0][1]]
for x,v in xv[1:-1]:
    Sv.append(Sv[-1]+v)
for i in range(N+1):
    for j in range(N+1-i):
        ans = max(ans,Sv[i]+(Sv[N]-Sv[N-j])-xv[i][0]-(C-xv[N+1-j][0]) - min(xv[i][0],(C-xv[N+1-j][0])))
print(ans)

Submission status: Wrong Answer

Solution code:
N, C = map(int,input().split())
xv = [[C,0]]+[list(map(int,input().split())) for _ in range(N)] + [[C,0]]

ans = 0
Sv = [xv[0][1]]
for x,v in xv[1:-1]:
    Sv.append(Sv[-1]+v)
for i in range(N+1):
    for j in range(N-i):
        ans = max(ans,Sv[i]+(Sv[N]-Sv[N-j])-xv[i][0]-(C-xv[N+1-j][0]) - min(xv[i][0],(C-xv[N+1-j][0])))
print(ans)

Submission status: Wrong Answer

Solution code:
N, C = map(int,input().split())
xv = [list(map(int,input().split())) for _ in range(N)] + [[C,0]]

ans = 0
Sv = [xv[0][1]]
for x,v in xv[1:-1]:
    Sv.append(Sv[-1]+v)
for i in range(N):
    for j in range(N-i):
        ans = max(ans,Sv[i]+(Sv[N-1]-Sv[N-1-j])-xv[i][0]-(C-xv[N-j][0]) - min(xv[i][0],(C-xv[N-j][0])))
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
N, C = map(int, input().split())
XV = [list(map(int, input().split())) for i in range(N)]
# V_minus_X = [a[1]-a[0] for a in XV]

V_minus_X = [XV[i][1]-(XV[i][0]-XV[i-1][0])
             for i in range(1, N)]  # i==0なら見ない それ以外ならXV[i-1][0]足して上げる
V_minus_X.insert(0, XV[0][1]-XV[0][0])
cum_ = np.cumsum(V_minus_X)


V_minus_X_reverse = [XV[i][1]-(XV[i+1][0]-XV[i][0])
                     for i in range(0, N-1)]
V_minus_X_reverse.append(XV[-1][1]-(C-XV[-1][0]))
V_minus_X_reverse.reverse()
cum_reverse = np.cumsum(V_minus_X_reverse)
# print(cum_reverse)
m_reverse = []
maxim_reverse = 0
maxim_reverse_where = 0
# print("print")
for i in range(N):
    # print(cum_reverse[i])
    if maxim_reverse < cum_reverse[i]:
        maxim_reverse = cum_reverse[i]
        maxim_reverse_where = C - XV[-i-1][0]
    m_reverse.append((maxim_reverse, maxim_reverse_where))

ans = 0
# print("print")
for i in range(N - 1):
    next = cum_[i] + m_reverse[-i - 2][0] - \
        min(XV[i][0], m_reverse[-i - 2][1])
    # print(next)
    # print(cum_[i], m_reverse[-i - 1][0], min(XV[i][0], m_reverse[-i - 1][1]))
    if ans < next:
        ans = next

# print(m_reverse)
print(max(ans, max(cum_), max(cum_reverse)))

Submission status: Wrong Answer

Solution code:
import numpy as np
N, C = map(int, input().split())
XV = [list(map(int, input().split())) for i in range(N)]
# V_minus_X = [a[1]-a[0] for a in XV]

V_minus_X = [XV[i][1]-(XV[i][0]-XV[i-1][0])
             for i in range(1, N)]  # i==0なら見ない それ以外ならXV[i-1][0]足して上げる
V_minus_X.insert(0, XV[0][1]-XV[0][0])
cum_ = np.cumsum(V_minus_X)


V_minus_X_reverse = [XV[i][1]-(XV[i+1][0]-XV[i][0])
                     for i in range(0, N-1)]
V_minus_X_reverse.append(XV[-1][1]-(C-XV[-1][0]))
V_minus_X_reverse.reverse()
cum_reverse = np.cumsum(V_minus_X_reverse)
# print(cum_reverse)
m_reverse = []
maxim_reverse = 0
maxim_reverse_where = 0
# print("print")
for i in range(N):
    # print(cum_reverse[i])
    if maxim_reverse < cum_reverse[i]:
        maxim_reverse = cum_reverse[i]
    maxim_reverse_where = C - XV[-i-1][0]
    m_reverse.append((maxim_reverse, maxim_reverse_where))
# print(m_reverse)
ans = 0
# print("print")
for i in range(N - 1):
    next = cum_[i] + m_reverse[-i - 2][0] - \
        min(XV[i][0], m_reverse[-i - 2][1])
    # print(next)
    # print(cum_[i], m_reverse[-i - 1][0], min(XV[i][0], m_reverse[-i - 1][1]))
    if ans < next:
        ans = next

# print(m_reverse)
print(max(ans, max(cum_), max(cum_reverse)))

Submission status: Wrong Answer

Solution code:
import numpy as np
N, C = map(int, input().split())
XV = [list(map(int, input().split())) for i in range(N)]
# V_minus_X = [a[1]-a[0] for a in XV]

V_minus_X = [XV[i][1]-(XV[i][0]-XV[i-1][0])
             for i in range(1, N)]  # i==0なら見ない それ以外ならXV[i-1][0]足して上げる
V_minus_X.insert(0, XV[0][1]-XV[0][0])
cum_ = np.cumsum(V_minus_X)


V_minus_X_reverse = [XV[i][1]-(XV[i+1][0]-XV[i][0])
                     for i in range(0, N-1)]
V_minus_X_reverse.append(XV[-1][1]-(C-XV[-1][0]))
V_minus_X_reverse.reverse()
cum_reverse = np.cumsum(V_minus_X_reverse)
#print(cum_reverse)
m_reverse = []
maxim_reverse = 0
maxim_reverse_where = 0
for i in range(N-1):
    if maxim_reverse < cum_reverse[i]:
        maxim_reverse = cum_reverse[i]
        maxim_reverse_where = C - XV[-i-1][0]
    m_reverse.append((maxim_reverse, maxim_reverse_where))
ans = 0

for i in range(N - 1):
    next = cum_[i] + m_reverse[-i - 1][0] - min(XV[i][0], m_reverse[-i - 1][1])
    #print(cum_[i], m_reverse[-i - 1][0], min(XV[i][0], m_reverse[-i - 1][1]))
    if ans < next:
        ans = next

#print(m_reverse)
print(max(ans, max(cum_), max(cum_reverse)))

Submission status: Wrong Answer

Solution code:
import numpy as np
N, C = map(int, input().split())
XV = [list(map(int, input().split())) for i in range(N)]

V_minus_X = [XV[i][1]-(XV[i][0]-XV[i-1][0])
             for i in range(1, N)]  # i==0なら見ない それ以外ならXV[i-1][0]足して上げる
V_minus_X.insert(0, XV[0][1]-XV[0][0])
cum_ = np.cumsum(V_minus_X)


V_minus_X_reverse = [XV[i][1]-(XV[i+1][0]-XV[i][0])
                     for i in range(0, N-1)]
V_minus_X_reverse.append(XV[-1][1]-(C-XV[-1][0]))
V_minus_X_reverse.reverse()
cum_reverse = np.cumsum(V_minus_X_reverse)
m_reverse = []
maxim_reverse = 0
maxim_reverse_where = 0
for i in range(N):
    if maxim_reverse < cum_reverse[i]:
        maxim_reverse = cum_reverse[i]
        maxim_reverse_where = C - XV[-i-1][0]
    m_reverse.append((maxim_reverse, maxim_reverse_where))
ans = 0

for i in range(N - 1):
    next = cum_[i] + m_reverse[-i - 2][0] - min(XV[i][0], m_reverse[-i - 2][1])
    if ans < next:
        ans = next

print(max(ans, max(cum_), max(cum_reverse)))

Submission status: Wrong Answer

Solution code:
import numpy as np
N, C = map(int, input().split())
XV = [list(map(int, input().split())) for i in range(N)]
# V_minus_X = [a[1]-a[0] for a in XV]

V_minus_X = [XV[i][1]-(XV[i][0]-XV[i-1][0])
             for i in range(1, N)]  # i==0なら見ない それ以外ならXV[i-1][0]足して上げる
V_minus_X.insert(0, XV[0][1]-XV[0][0])
cum_ = np.cumsum(V_minus_X)


V_minus_X_reverse = [XV[i][1]-(XV[i+1][0]-XV[i][0])
                     for i in range(0, N-1)]
V_minus_X_reverse.append(XV[-1][1]-(C-XV[-1][0]))
V_minus_X_reverse.reverse()
cum_reverse = np.cumsum(V_minus_X_reverse)
# print(cum_reverse)
m_reverse = []
maxim_reverse = 0
maxim_reverse_where = 0
for i in range(N):
    if maxim_reverse < cum_reverse[i]:
        maxim_reverse = cum_reverse[i]
        maxim_reverse_where = C - XV[-i-1][0]
    m_reverse.append((maxim_reverse, maxim_reverse_where))
ans = 0

for i in range(N - 1):
    next = cum_[i] + m_reverse[-i - 1][0] - min(XV[i][0], m_reverse[-i - 1][1])
    #print(cum_[i], m_reverse[-i - 1][0], min(XV[i][0], m_reverse[-i - 1][1]))
    if ans < next:
        ans = next

# print(m_reverse)
print(max(ans, max(cum_), max(cum_reverse)))

Submission status: Wrong Answer

Solution code:
import numpy as np
N, C = map(int, input().split())
XV = [list(map(int, input().split())) for i in range(N)]
# V_minus_X = [a[1]-a[0] for a in XV]

V_minus_X = [XV[i][1]-(XV[i][0]-XV[i-1][0])
             for i in range(1, N)]  # i==0なら見ない それ以外ならXV[i-1][0]足して上げる
V_minus_X.insert(0, XV[0][1]-XV[0][0])
cum_ = np.cumsum(V_minus_X)

# XV_reverse = [[C-a[0], a[1] for a in XV]
# V_minus_X_reverse = [a[1]-a[0] for a in XV_reverse]
# V_minus_X_reverse = [a[1] - (C - a[0]) for a in XV]
V_minus_X_reverse = [XV[i][1]-(XV[i+1][0]-XV[i][0])
                     for i in range(0, N-1)]
V_minus_X_reverse.append(XV[-1][1]-(C-XV[-1][0]))
V_minus_X_reverse.reverse()
# print(V_minus_X_reverse)
# print(V_minus_X_reverse)
cum_reverse = np.cumsum(V_minus_X_reverse)
#print("c_rev", cum_reverse)

m = []
m_reverse = []
maxim = 0
maxim_where = 0
maxim_reverse = 0
maxim_reverse_where = 0
for i in range(N-1):
    """if maxim < cum_[i]:
        maxim = cum_[i]
        maxim_where = XV[i][0]"""
    if maxim_reverse < cum_reverse[i]:
        maxim_reverse = cum_reverse[i]
        maxim_reverse_where = C - XV[-i-1][0]
    m_reverse.append((maxim_reverse, maxim_reverse_where))
ans = 0
# print(V_minus_X)
#print("cum", cum_)
#print("m_rev", m_reverse)
for i in range(N - 1):
    next = cum_[i] + m_reverse[-i-1][0] - min(XV[i][0], m_reverse[-i-1][1])
    #print(cum_[i], next)
    if ans < next:
        ans = next
        # print(V_minus_X)
"""print("cum_reverse", cum_reverse)
print(V_minus_X_reverse)
print(cum_)
print(cum_reverse) """
#print(ans, max(cum_), max(cum_reverse))
print(max(ans, max(cum_), max(cum_reverse)))

Submission status: Wrong Answer

Solution code:
N, C = map(int, input().split())
X, V = [], []

cw1 = [0]
cw2 = [0]
sum_cw = 0
for _ in range(N):
  x, v = map(int, input().split())
  X.append(x)
  V.append(v)
  sum_cw += v
  cw1.append(max(cw1[-1], sum_cw-x))
  cw2.append(max(cw2[-1], sum_cw-2*x))


ccw1 = [0]
ccw2 = [0]
sum_ccw = 0

for i in reversed(range(N)):
  cx,cv = X[i], V[i]
  sum_ccw += cv
  ccw1.append(max(ccw1[-1], sum_ccw-(C-cx)))
  ccw2.append(max(ccw2[-1], sum_ccw-(C-cx)*2))

ans = 0
for i in range(N+1):
  ans = max(ans, cw1[i]+ccw2[N-i], cw2[i]+ccw2[N-i])

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
import bisect

n, c = [int(item) for item in input().split()]

xs, vs, xrs = [], [], []
for i in range(n):
  x, v = [int(item) for item in input().split()]
  xs.append(x)
  xrs.append(c-x)
  vs.append(v)
xrs = xrs[::-1]

lft_v = list(accumulate(vs))
rgt_v = list(accumulate(vs[::-1]))

ans = 0
# No return
for i in range(n):
  ans = max(ans, lft_v[i]-xs[i], rgt_v[i]-(c-xs[::-1][i]))
  
# Includes return
lft_lim = bisect.bisect(xs, c//2-1)
rgt_lim = bisect.bisect(xrs, c//2)
for i in range(lft_lim):
  for j in range(rgt_lim):
    ans = max(ans, lft_v[i] - xs[i]*2 + rgt_v[j] - xrs[j])
    ans = max(ans, rgt_v[j] - xrs[j]*2 + lft_v[i] - xs[i])
    
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
import bisect

n, c = [int(item) for item in input().split()]

xs, vs, xrs = [], [], []
for i in range(n):
  x, v = [int(item) for item in input().split()]
  xs.append(x)
  xrs.append(c-x)
  vs.append(v)
xrs = xrs[::-1]

lft_v = list(accumulate(vs))
rgt_v = list(accumulate(vs[::-1]))

ans = 0
# No return
for i in range(n):
  ans = max(ans, lft_v[i]-xs[i], rgt_v[i]-(c-xs[::-1][i]))
  
# Includes return
lft_lim = bisect.bisect(xs, c//2)
rgt_lim = bisect.bisect(xrs, c//2)
for i in range(lft_lim):
  for j in range(rgt_lim):
    ans = max(ans, lft_v[i] - xs[i]*2 + rgt_v[j] - xrs[j])
    ans = max(ans, rgt_v[j]- xrs[j]*2 + lft_v[i] - xs[i])
    
print(ans)

Submission status: Wrong Answer

Solution code:
def solve():
    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())

    sc = 0
    max_sc1 = 0
    max_i1 = 0
    for i in range(N):
        sc += v[i]
        if max_sc1 < sc - x[i]:
            max_sc1 = sc - x[i]
            max_i1 = i

    sc = 0
    max_sc2 = 0
    max_i2 = 0
    for i in range(N-1,-1,-1):
        sc += v[i]
        if max_sc2 < sc - (C - x[i]):
            max_sc2 = sc - (C - x[i])
            max_i2 = i

    return max(max_sc1,max_sc2)

print(solve())

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
info = [[0,0]]+[list(map(int,input().split())) for i in range(n)]+[[c,0]]
left_info = info[::-1]

right_ruiseki = [0]*(n+1)
left_ruiseki = [0]*(n+1)
ou_right_ruiseki = [0]*(n+1)
ou_left_ruiseki = [0]*(n+1)

for i in range(n):
  right_ruiseki[i+1] = right_ruiseki[i] + info[i+1][1] - info[i+1][0] + info[i][0]
  left_ruiseki[i+1] = left_ruiseki[i] + left_info[i+1][1] + left_info[i+1][0] - left_info[i][0]
  ou_right_ruiseki[i+1] = ou_right_ruiseki[i] + info[i+1][1] - 2*info[i+1][0] + 2*info[i][0]
  ou_left_ruiseki[i+1] = ou_left_ruiseki[i] + left_info[i+1][1] + 2*left_info[i+1][0] - 2*left_info[i][0]

#print(right_ruiseki,left_ruiseki,ou_right_ruiseki,ou_left_ruiseki)

for i in range(n):
  right_ruiseki[i+1]=max(right_ruiseki[i],right_ruiseki[i+1])
  left_ruiseki[i+1]=max(left_ruiseki[i],left_ruiseki[i+1])

ans = 0
for i in range(n):
  tmp1 = ou_right_ruiseki[i+1]+left_ruiseki[n-(i+1)]
  tmp2 = ou_left_ruiseki[i+1]+right_ruiseki[n-(i+1)]
  ans = max(tmp1,tmp2,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    N, C = map(int, input().split())
    sushis = [(0, 0) for _ in range(N)]
    for i in range(N):
        x, v = map(int, input().split())
        sushis[i] = (x, v)
    ls, rs = 0, 0
    left = [(0, 0) for _ in range(N+1)]
    right = [(0, 0) for _ in range(N+1)]
    for i in range(N):
        ls += sushis[i][1]
        lx = sushis[i][0]
        rs += sushis[-i-1][1]
        rx = C - sushis[-i-1][0]
        left[i+1] = (max(ls-lx, left[i][0]),
                     max(ls-lx*2, left[i][1]))
        right[i+1] = (max(rs-rx, right[i][0]),
                      max(rs-rx*2, right[i][1]))
    ans = 0
    for i in range(1, N+1):
        ans = max([left[i][0] + right[N-i][1],
                   left[i][1] + right[N-i][0],
                   ans])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate

def main():
  input = sys.stdin.readline
  N, C = map(int, input().split())
  X = []
  V = []
  for _ in range(N):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v)
  Vr = V[::-1]
  Xr = X[::-1]

  F = list(accumulate(V))
  R = list(accumulate(Vr))
  F[0], R[0] = F[0] - X[0], R[0] - (C - Xr[0])
  for i in range(1, N):
    F[i] = max(F[i-1], F[i] - X[i])
    R[i] = max(R[i-1], R[i] - (C - Xr[i]))

  val = 0
  for i in range(N-1):
    v1 = F[i] + R[N-(i+1)] - X[i]
    v2 = R[i] + F[N-(i+1)] - (C - Xr[i])
    val = max(val, v1, v2)
  
  ans = max(0, val, max(F), max(R))
  print(ans)


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate

def main():
  input = sys.stdin.readline
  N, C = map(int, input().split())
  X = []
  V = []
  for _ in range(N):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v)
  Vr = V[::-1]
  Xr = X[::-1]

  F = list(accumulate(V))
  R = list(accumulate(Vr))
  F[0], R[0] = F[0] - X[0], R[0] - (C - Xr[0])
  for i in range(1, N):
    F[i] = max(F[i-1], F[i] - X[i])
    R[i] = max(R[i-1], R[i] - (C - Xr[i]))

  val = 0
  for i in range(N-1):
    v1 = F[i] + R[-(i+1)] - X[i]
    v2 = R[i] + F[-(i+1)] - (C - Xr[i])
    val = max(val, v1, v2)
  
  ans = max(0, val, max(F), max(R))
  print(ans)


if __name__ == '__main__':
  main()

Submission status: Wrong Answer

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

D = []

for i in range(N):
  D.append(list(map(int, input().split())))
  
R = [[0, 0] for i in range(N + 1)]
cnt = 0
ma = 0
man = -1
R[0][0] = -1
for i in range(N):
  cnt += D[i][1]
  if cnt - D[i][0] > ma:
    ma = cnt - D[i][0]
    man = i
  R[i + 1] = [man, ma]

cnt = 0
Ans = R[N][1]
for i in range(1, N + 1):
  t = N - i
  cnt += D[t][1]
  tmp = cnt - (C - D[t][0]) + R[t][1]
  if R[t][0] != -1:
    tmp -= min(C - D[t][0], D[R[t][0]][0])

  if tmp > Ans:
    Ans = tmp
print(Ans)

Submission status: Wrong Answer

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

D = []

for i in range(N):
  D.append(list(map(int, input().split())))
  
R = [[0, 0] for i in range(N + 1)]
cnt = 0
ma = 0
man = 0
for i in range(N):
  cnt += D[i][1]
  if cnt - D[i][0] > ma:
    ma = cnt - D[i][0]
    man = i
  R[i + 1] = [man, ma]

cnt = 0
Ans = R[N][1]
for i in range(1, N + 1):
  t = N - i
  cnt += D[t][1]
  tmp = cnt - (C - D[t][0]) + R[t][1]
  if R[t][0] != 0:
    tmp -= min(C - D[t][0], D[R[t][0]][0])

  if tmp > Ans:
    Ans = tmp
print(Ans)

Submission status: Wrong Answer

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

D = []

for i in range(N):
  D.append(list(map(int, input().split())))
  
R = [[0, 0] for i in range(N + 1)]
cnt = 0
ma = 0
man = 0
for i in range(N):
  cnt += D[i][1]
  if cnt - D[i][0] > ma:
    ma = cnt - D[i][0]
    man = i
  R[i + 1] = [man, ma]

cnt = 0
Ans = R[N][1]
for i in range(1, N + 1):
  t = N - i
  cnt += D[t][1]
  tmp = cnt - (C - D[t][0]) + R[t][1]
  if D[t][0] != 0:
    tmp -= min(C - D[t][0], D[R[t][0]][0])
  if tmp > Ans:
    Ans = tmp
print(Ans)