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:
memo = dict()
n,c = map(int, input().split())
data = []
for i in range(n):
    data.append(list(map(int, input().split())))
start_to_a = [0 for i in range(n)]
value_sum = 0
for i in range(n):
    value_sum += data[i][1]
    start_to_a[i] = value_sum - data[i][0]
    if start_to_a[i] in memo:
        memo[start_to_a[i]] = min(memo[start_to_a[i]], data[i][0])
    else:
        memo[start_to_a[i]] = data[i][0]
good_end_a_for_b = [0 for i in range(n+1)]
for i in range(1,n+1):
    good_end_a_for_b[i] = max(good_end_a_for_b[i-1], start_to_a[i-1])
#good_end_a_for_b[i] := 終了がiの時、最適に選んだAの時のvalue
end_at_b = [0 for i in range(n)]
value_sum = 0
for i in range(n-1,-1,-1):
    value_sum += data[i][1]
    end_at_b[i] = value_sum - (c - data[i][0])
ans = 0
#print(good_end_a_for_b)
#print(end_at_b)
memo[0] = 0
for end in range(n):
    #前者は、O -> A -> B
    #後者は  O -> B -> A
    k = memo[good_end_a_for_b[end]]
    #print("end at:{}, to a:{}".format(end,k))
    ans = max(ans,good_end_a_for_b[end] + end_at_b[end] - k,end_at_b[end] - (c - data[end][0]) + good_end_a_for_b[end])
    #print(ans)
    #print()
#O->Aの時
ans = max(ans, max(start_to_a))
ans = max(ans, max(end_at_b))
print(ans)

Submission status: Wrong Answer

Solution code:
memo = dict()
n,c = map(int, input().split())
data = []
for i in range(n):
    data.append(list(map(int, input().split())))
start_to_a = [0 for i in range(n)]
value_sum = 0
for i in range(n):
    value_sum += data[i][1]
    start_to_a[i] = value_sum - data[i][0]
    if start_to_a[i] in memo:
        memo[start_to_a[i]] = min(memo[start_to_a[i]], data[i][0])
    else:
        memo[start_to_a[i]] = data[i][0]
good_end_a_for_b = [0 for i in range(n+1)]
for i in range(1,n+1):
    good_end_a_for_b[i] = max(good_end_a_for_b[i-1], start_to_a[i-1])
#good_end_a_for_b[i] := 終了がiの時、最適に選んだAの時のvalue
end_at_b = [0 for i in range(n)]
value_sum = 0
for i in range(n-1,-1,-1):
    value_sum += data[i][1]
    end_at_b[i] = value_sum - (c - data[i][0])
ans = 0
#print(good_end_a_for_b)
#print(end_at_b)
memo[0] = 0
for end in range(n):
    #前者は、O -> A -> B
    #後者は  O -> B -> A
    k = memo[good_end_a_for_b[end]]
    #print("end at:{}, to a:{}".format(end,k))
    ans = max(ans,good_end_a_for_b[end] + end_at_b[end] - k,end_at_b[end] - (c - data[end][0]) + good_end_a_for_b[end])
    #print(ans)
    #print()
#O->Aの時
ans = max(ans, max(start_to_a))
print(ans)

Submission status: Wrong Answer

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

class SegmentTree(object):
    __slots__ = ["elem_size", "tree", "default", "op"]

    def __init__(self, a: list, default: int, op):
        from math import ceil, log
        real_size = len(a)
        self.elem_size = elem_size = 1 << ceil(log(real_size, 2))
        self.tree = tree = [default] * (elem_size * 2)
        tree[elem_size:elem_size + real_size] = a
        self.default = default
        self.op = op

        for i in range(elem_size - 1, 0, -1):
            tree[i] = op(tree[i << 1], tree[(i << 1) + 1])

    def get_value(self, x: int, y: int) -> int:  # 半開区間
        l, r = x + self.elem_size, y + self.elem_size
        tree, result, op = self.tree, self.default, self.op
        while l < r:
            if l & 1:
                result = op(tree[l], result)
                l += 1
            if r & 1:
                r -= 1
                result = op(tree[r], result)
            l, r = l >> 1, r >> 1

        return result

    def set_value(self, i: int, value: int) -> None:
        k = self.elem_size + i
        self.tree[k] = value
        self.update(k)

    def update(self, i: int) -> None:
        op, tree = self.op, self.tree
        while i > 1:
            i >>= 1
            tree[i] = op(tree[i << 1], tree[(i << 1) + 1])


r = [[0,0,0]]
for i in range(n):
    a,b = x[i]
    s = r[-1][0]+b-(a+r[-1][1])
    r.append([s,-a,i+1])

l = [[0,0] for i in range(n+1)]
l[-2] = [x[-1][1]-(c-x[-1][0]),x[-1][0]]
for i in range(2,n+1):
    a,b = x[-i]
    s = l[-i][0]+b-(l[-i][1]-a)
    l[-i-1] = [s,a]
l[-1][1] = c

rs = sorted(r,reverse=True)

now = 0
rm = [[0]*2 for i in range(n+1)]
for i in range(n,0,-1):
    if i >= rs[now][2]:
        rm[i] = [rs[now][0],rs[now][1]]
    else:
        while i < rs[now][2]:
            now += 1
        rm[i] = [rs[now][0],rs[now][1]]


ans = 0

for i in range(n+1):
    count = l[i][0]+rm[i][0] - min(c-l[i][1],-rm[i][1])
    ans = max(ans,count)
print(ans)

Submission status: Wrong Answer

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

class SegmentTree(object):
    __slots__ = ["elem_size", "tree", "default", "op"]

    def __init__(self, a: list, default: int, op):
        from math import ceil, log
        real_size = len(a)
        self.elem_size = elem_size = 1 << ceil(log(real_size, 2))
        self.tree = tree = [default] * (elem_size * 2)
        tree[elem_size:elem_size + real_size] = a
        self.default = default
        self.op = op

        for i in range(elem_size - 1, 0, -1):
            tree[i] = op(tree[i << 1], tree[(i << 1) + 1])

    def get_value(self, x: int, y: int) -> int:  # 半開区間
        l, r = x + self.elem_size, y + self.elem_size
        tree, result, op = self.tree, self.default, self.op
        while l < r:
            if l & 1:
                result = op(tree[l], result)
                l += 1
            if r & 1:
                r -= 1
                result = op(tree[r], result)
            l, r = l >> 1, r >> 1

        return result

    def set_value(self, i: int, value: int) -> None:
        k = self.elem_size + i
        self.tree[k] = value
        self.update(k)

    def update(self, i: int) -> None:
        op, tree = self.op, self.tree
        while i > 1:
            i >>= 1
            tree[i] = op(tree[i << 1], tree[(i << 1) + 1])


r = [[0,0,0]]
for i in range(n):
    a,b = x[i]
    s = r[-1][0]+b-(a-r[-1][1])
    r.append([s,a,i+1])

l = [[0,0] for i in range(n+1)]
l[-2] = [x[-1][1]-(c-x[-1][0]),x[-1][0]]
for i in range(2,n+1):
    a,b = x[-i]
    s = l[-i][0]+b-(l[-i][1]-a)
    l[-i-1] = [s,a]
l[-1][1] = c

rs = sorted(r,reverse=True)

now = 0
rm = [[0]*2 for i in range(n+1)]
for i in range(n,0,-1):
    if i >= rs[now][2]:
        rm[i] = [rs[now][0],rs[now][1]]
    else:
        while i < rs[now][2]:
            now += 1
        rm[i] = [rs[now][0],rs[now][1]]


ans = 0

for i in range(n+1):
    count = l[i][0]+rm[i][0] - min(c-l[i][1],rm[i][1])
    ans = max(ans,count)
print(ans)

Submission status: Wrong Answer

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

Y = [[C - i, v] for i, v in X[::-1]]

# Cumsum
x_cs = [[0, 0] for _ in range(N + 1)]
y_cs = [[0, 0] for _ in range(N + 1)]
for i in range(N):
    x_cs[i + 1][0] = X[i][0]
    x_cs[i + 1][1] = x_cs[i][1] + X[i][1]

    y_cs[i + 1][0] = Y[i][0]
    y_cs[i + 1][1] = y_cs[i][1] + Y[i][1]

fx = [v - x for x, v in x_cs]
fy = [v - x for x, v in y_cs]

gx = [0] * (N + 1)
gy = [0] * (N + 1)
for i in range(N):
    gx[i + 1] = max(gx[i], fx[i + 1])
    gy[i + 1] = max(gy[i], fy[i + 1])

X = [[0, 0]] + X
Y = [[0, 0]] + Y
ans = max(gx + gy)
for i in range(N):
    ans = max(ans, gx[i] + gy[N - i - 1] - X[i][0],
              gx[N - i - 1] + gy[i] - Y[i][0])

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# https://atcoder.jp/contests/abc095/tasks/arc096_b
N, C = read_ints()
X, V = read_col(N)
# 遠くの寿司も見ないとダメそう(vがバカでかい可能性がある)
# とりあえず右スタートを前提に考える。歩く距離の最小は2パターン。
# 1. 右に進んで食べるだけ
# 2. 途中まで右に進んで途中で左に戻る


def ret_ans1(X, V):  # 右スタート想定
    sum_v = 0
    ans = -1
    for x, v in zip(X, V):
        sum_v += v
        ans = max(ans, sum_v - x)
    return ans


def ret_ans2(X, V):  # ただし右スタートを想定
    Z = []
    sum_v = 0
    for x, v in zip(X, V):
        sum_v += v
        Z.append(sum_v - 2 * x)

    Y = []
    sum_v = 0
    for x, v in reversed(list(zip(X, V))):
        sum_v += v
        Y.append(sum_v - (C - x))
    Y = list(reversed(Y))

    # optimize
    ans = max_sum(Z, Y)
    return ans


def max_sum(X, Y):
    # X_i + Y_j (i<j)を最大化する O(n)
    X_accum = [-10**15]
    for x in X:
        X_accum.append(max(x, X_accum[-1]))

    del X_accum[0]

    Y_accum = [-10**15]
    for y in reversed(Y):
        Y_accum.append(max(y, Y_accum[-1]))
    Y_accum = list(reversed(Y_accum))
    del Y_accum[-1]
    ret = 0
    for i in range(len(X) - 1):
        ret = max(ret, X_accum[i] + Y_accum[i + 1])
    return ret


ans = 0
ans = max(ans, ret_ans1(X, V))
ans = max(ans, ret_ans2(X, V))

# 左周りにスタートする
V = list(reversed(V))
X_r = []
for x in reversed(X):
    X_r.append(C - x)

ans = max(ans, ret_ans2(X_r, V))
ans = max(ans, ret_ans2(X_r, V))
print(ans)

Submission status: Wrong Answer

Solution code:
#ABC095-D Static Sushi
"""
問題：
start地点から外周Cmの円周上を歩き、利益を最大化できるように
寿司を食べる。
なお,1m歩くごとに利益が-1される。
解法：
どのように寿司を選んでも利益がマイナスとなる場合は0を出力。
時計回りに歩いた時の寿司の位置と、逆向きに歩いた時の位置を持って
置くと良いだろう。
最適な歩き方は以下のどれかとなるので、全部求めて利益の最大値を出力
1.円周上を右もしくは左回りのみに歩いて、利益が最大になるところで終了
2.初め右もしくは左に歩いて、途中で反転して利益が最大になるところで終了
"""
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n,c = map(int,readline().split())
"""
dis_r:時計回り時の距離
dis_l:反〃
val_r:時計回り時の価値の累積和
val_l:反〃
"""
res = []
dis_r = [0]
dis_l = [0]
val_r = [0]
val_l = [0]
for i in range(n):
    x,v = map(int,readline().split())
    res.append((x,v))
    dis_r.append(x)
    val_r.append(val_r[-1]+v)
res.reverse()
for i in range(n):
    dis_l.append(c-res[i][0])
    val_l.append(val_l[-1]+res[i][1])

ans = 0

mid = (c+1)//2

#右回りのみ
if c%2==0: #どちらも閉区間
    res_r = 0
    r_idx = 0
    for i in range(n):
        res = val_r[i]-dis_r[i]
        ans = max(ans,res)
        if dis_r[i] <= mid and res_r < res:
            r_idx = i
            res_r = res
    #左回りのみ
    res_l = 0
    l_idx = 0
    for i in range(n):
        res =val_l[i]-dis_l[i]
        ans = max(ans,res)
        if dis_l[i] <= mid and res_l < res:
            l_idx = i
            res_l = res

    #右行ってからある地点で左(但し、dis_r <= c//2迄)
    left_max_val = val_l[l_idx]-dis_l[l_idx]
    for i in range(n):
        if dis_r[i] > mid:
            break
        ans = max(ans,val_r[i]-dis_r[i]*2+left_max_val)
    #左行ってからある地点で右(但し、dis_l < c//2迄)
    right_max_val = val_r[r_idx]-dis_r[r_idx]
    for i in range(n):
        if dis_l[i] > mid:
            break
        ans = max(ans,val_l[i]-dis_l[i]*2+right_max_val)

else: #左は開区間
    res_r = 0
    r_idx = 0
    for i in range(n):
        res = val_r[i]-dis_r[i]
        ans = max(ans,res)
        if dis_r[i] <= mid and res_r < res:
            r_idx = i
            res_r = res
    #左回りのみ
    res_l = 0
    l_idx = 0
    for i in range(n):
        res =val_l[i]-dis_l[i]
        ans = max(ans,res)
        if dis_l[i] < mid and res_l < res:
            l_idx = i
            res_l = res

    #右行ってからある地点で左(但し、dis_r <= c//2迄)
    left_max_val = val_l[l_idx]-dis_l[l_idx]
    for i in range(n):
        if dis_r[i] > mid:
            break
        ans = max(ans,val_r[i]-dis_r[i]*2+left_max_val)
    #左行ってからある地点で右(但し、dis_l < c//2迄)
    right_max_val = val_r[r_idx]-dis_r[r_idx]
    for i in range(n):
        if dis_l[i] >= mid:
            break
        ans = max(ans,val_l[i]-dis_l[i]*2+right_max_val)
print(ans)

Submission status: Wrong Answer

Solution code:
#ABC095-D Static Sushi
"""
問題：
start地点から外周Cmの円周上を歩き、利益を最大化できるように
寿司を食べる。
なお,1m歩くごとに利益が-1される。
解法：
どのように寿司を選んでも利益がマイナスとなる場合は0を出力。
時計回りに歩いた時の寿司の位置と、逆向きに歩いた時の位置を持って
置くと良いだろう。
最適な歩き方は以下のどれかとなるので、全部求めて利益の最大値を出力
1.円周上を右もしくは左回りのみに歩いて、利益が最大になるところで終了
2.初め右もしくは左に歩いて、途中で反転して利益が最大になるところで終了
"""
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n,c = map(int,readline().split())
"""
dis_r:時計回り時の距離
dis_l:反〃
val_r:時計回り時の価値の累積和
val_l:反〃
"""
res = []
dis_r = [0]
dis_l = [0]
val_r = [0]
val_l = [0]
for i in range(n):
    x,v = map(int,readline().split())
    res.append((x,v))
    dis_r.append(x)
    val_r.append(val_r[-1]+v)
res.reverse()
for i in range(n):
    dis_l.append(c-res[i][0])
    val_l.append(val_l[-1]+res[i][1])

ans = 0
#右回りのみ
res_r = 0
r_idx = 0
for i in range(n):
    res = val_r[i]-dis_r[i]
    ans = max(ans,res)
    if dis_r[i] <= c//2 and res_r < res:
        r_idx = i
        res_r = res
#左回りのみ
res_l = 0
l_idx = 0
for i in range(n):
    res =val_l[i]-dis_l[i]
    ans = max(ans,res)
    if dis_l[i] < c//2 and res_l < res:
        l_idx = i
        res_l = res

#右行ってからある地点で左(但し、dis_r <= c//2迄)
left_max_val = val_l[l_idx]-dis_l[l_idx]
for i in range(n):
    if dis_r[i] > c//2:
        break
    ans = max(ans,val_r[i]-dis_r[i]*2+left_max_val)
#左行ってからある地点で右(但し、dis_l < c//2迄)
right_max_val = val_r[r_idx]-dis_r[r_idx]
for i in range(n):
    if dis_l[i] >= c//2:
        break
    ans = max(ans,val_l[i]-dis_l[i]*2+right_max_val)

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n,C = map(int, input().split())
    xv = [list(map(int, input().split())) for _ in range(n)]
    xv.sort()
    a, b = [], []
    kcal = 0
    for x, v in xv:
        kcal += v
        a.append(kcal - x)
        b.append(kcal-2*x)
    xv.sort(reverse=True)
    c, d = [], []
    kcal = 0
    for x, v in xv:
        kcal += v
        x = C - x
        c.append(kcal - x)
        d.append(kcal-2*x)
    
    for v in [a,b,c,d]:
        for i in range(1, n):
            if v[i] < v[i-1]:
                v[i] = v[i-1]
    
    res = 0
    for i in range(n):
        res = max(res, a[i], b[i])
        if i < n-1:
            res = max(res, c[i]+b[-(i+2)], d[i]+a[-(i+2)])
    print(res)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n,c = map(int,input().split())
wv = [[0,0]]+[list(map(int,input().split())) for i in range(n)]+[[c,0]]
n += 2
ansls = []
for i in range(n):
  if wv[i][0] > c//2:
    spr = i
    break
for sp in spr-1,spr:
  ls = [0]*n
  lsx = [0]*n
  for i in range(1,sp):
    w,v = wv[i]
    w -= wv[i-1][0]
    ls[i] = v-w*2
    lsx[i] = v-w
  for i in range(n-2,sp-1,-1):
    w,v = wv[i]
    w -= wv[i+1][0]
    ls[i] = v+w*2
    lsx[i] = v+w
  alsf = list(accumulate(ls[:sp]))+[0]
  alsl = list(accumulate(ls[sp:][::-1]))+[0]
  alsxf = list(accumulate(lsx[:sp]))+[0]
  alsxl = list(accumulate(lsx[sp:][::-1]))+[0]
  ansls.append(max(alsf)+max(alsxl))
  ansls.append(max(alsl)+max(alsxf))
for i in range(1,n):
  w,v = wv[i]
  w -= wv[i-1][0]
  ls[i] = v-w
for i in range(n-2,0,-1):
  w,v = wv[i]
  w -= wv[i+1][0]
  lsx[i] = v+w
als = list(accumulate(ls))
alsx = list(accumulate(lsx))
ansls.extend((max(als),max(alsx)))
print(max(ansls))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n,c = map(int,input().split())
wv = [[0,0]]+[list(map(int,input().split())) for i in range(n)]+[[c,0]]
n += 2
ansls = []
for i in range(n):
  if wv[i][0] > c//2:
    spr = i
    break
for sp in spr-1,spr:
  ls = [0]*n
  lsx = [0]*n
  for i in range(1,sp):
    w,v = wv[i]
    w -= wv[i-1][0]
    ls[i] = v-w*2
    lsx[i] = v-w
  for i in range(n-2,sp-1,-1):
    w,v = wv[i]
    w -= wv[i+1][0]
    ls[i] = v+w*2
    lsx[i] = v+w
  alsf = list(accumulate(ls[:sp]))+[0]
  alsl = list(accumulate(ls[sp:][::-1]))+[0]
  alsxf = list(accumulate(lsx[:sp]))+[0]
  alsxl = list(accumulate(lsx[sp:][::-1]))+[0]
  ansls.append(max(alsf)+max(alsxl))
  ansls.append(max(alsl)+max(alsxf))
print(max(ansls))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n,c = map(int,input().split())
wv = [[0,0]]+[list(map(int,input().split())) for i in range(n)]+[[c,0]]
n += 2
for i in range(n):
  if wv[i][0] > c//2:
    sp = i
    break
ls = [0]*n
lsx = [0]*n
for i in range(1,sp):
  w,v = wv[i]
  w -= wv[i-1][0]
  ls[i] = v-w*2
  lsx[i] = v-w
for i in range(n-2,sp-1,-1):
  w,v = wv[i]
  w -= wv[i+1][0]
  ls[i] = v+w*2
  lsx[i] = v+w
alsf = list(accumulate(ls[:sp]))
alsl = list(accumulate(ls[sp:][::-1]))
alsxf = list(accumulate(lsx[:sp]))
alsxl = list(accumulate(lsx[sp:][::-1]))
ansls = []
ansls.append(max(alsf)+max(alsxl))
ansls.append(max(alsl)+max(alsxf))
print(max(ansls))

Submission status: Wrong Answer

Solution code:
N, C = map(int, input().split())
S = [tuple(map(int, input().split())) for _ in range(N)]

CW, CCW, CW_aft_ret, CCW_aft_ret = ([0] * N for _ in range(4))
'''
CW: 起点から時計回りにS[i]まで
CCW: 起点から反時計回りS[i]まで
CW_aft_ret: 起点から時計回りに行き、S[i]で折り返す
CCW_aft_ret: 起点から反時計回りに行き、S[i]で折り返す
'''

CW[0] = S[0][1] - S[0][0]
CW_aft_ret[0] = CW[0] - S[0][0]
for i in range(1, N):
    CW[i] = CW[i - 1] - (S[i][0] - S[i - 1][0]) + S[i][1]
    CW_aft_ret[i] = CW[i] - S[i][0]
for i in range(1, N):
    CW[i] = max(CW[i - 1], CW[i])

CCW[N - 1] = S[N - 1][1] - (C - S[N - 1][0])
CCW_aft_ret[0] = CCW[0] - (C - S[N - 1][0])
for i in range(N - 2, -1, -1):
    CCW[i] = CCW[i + 1] - (S[i + 1][0] - S[i][0]) + S[i][1]
    CCW_aft_ret[i] = CCW[i] - (C - S[i][0])
for i in range(N - 2, -1, -1):
    CCW[i] = max(CCW[i + 1], CCW[i])

for i in range(1, N):
    CW_aft_ret[i - 1] += CCW[i]
    CCW_aft_ret[i] += CW[i - 1]
print(max(0, max(CW), max(CW_aft_ret), max(CCW), max(CCW_aft_ret)))

Submission status: Wrong Answer

Solution code:
n, c = map(int, input().split())
xv = [input().split() for _ in range(n)]
xv = [(int(x[0]), int(x[1]))for x in xv]

# 時計回り
sum_v = 0
perfs = [[0, 0]]
for i in range(n):
    x, v = xv[i]
    sum_v += v
    perf = sum_v - x
    pre_perf, pre_x = perfs[-1]
    if perf < pre_perf:
        perf = pre_perf
        x = pre_x
    perfs.append([perf, x])

# 反時計回り
perfs = perfs[::-1]
xv.append([0, 0])
xv = xv[::-1]
xv = [[(c - int(x[0])) % c, int(x[1])] for x in xv]
ans = 0
sum_v = 0
for i in range(len(xv)):
    x, v = xv[i]
    sum_v += v
    other_v, other_x = perfs[i]
    perf = max(sum_v + other_v - 2 * x, sum_v + other_v - x - other_x)
    ans = max(ans, perf)
print(ans)

Submission status: Wrong Answer

Solution code:
n, c = map(int, input().split())
xv = [input().split() for _ in range(n)]
xv = [(int(x[0]), int(x[1]))for x in xv]

# 時計回り
sum_v = 0
perfs = [[0, 0]]
for i in range(n):
    x, v = xv[i]
    sum_v += v
    perf = sum_v - x
    pre_perf, pre_x = perfs[-1]
    if perf < pre_perf:
        perf = pre_perf
        x = pre_x
    perfs.append([perf, x])

# 反時計回り
xv = xv[::-1]
xv = [[c - int(x[0]), int(x[1])] for x in xv]
ans = perfs[-1][0]
sum_v = 0
for i in range(n):
    x, v = xv[i]
    sum_v += v
    other_v, other_x = perfs[n-1-i]
    perf = max(sum_v + other_v - 2 * x, sum_v + other_v - x - other_x)
    ans = max(ans, perf)
    pre_x = x
print(ans)

Submission status: Wrong Answer

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

x_list = []
v_list = []
one_list = []
round_list = []
r_one_list = []
r_round_list = []

total = 0

for i in range(n):
    x, v = map(int, input().split())
    x_list.append(x)
    v_list.append(v)
    total = total + v
    one_list.append(total - x)
    round_list.append(total - (2 * x))

prev = 0

for i in range(n):
    if prev > one_list[i]:
        one_list[i] = prev
    else:
        prev = one_list[i]

prev = 0

for i in range(n):
    if prev > round_list[i]:
        round_list[i] = prev
    else:
        prev = round_list[i]


total = 0
for i in range(1, n+1):
    total = total + v_list[-i]
    r_one_list.append(total - (c - x_list[-i]))
    r_round_list.append(total - 2 * (c - x_list[-i]))

prev = 0
for i in range(n):
    if prev > r_one_list[i]:
        r_one_list[i] = prev
    else:
        prev = r_one_list[i]

prev = 0
for i in range(n):
    if prev > r_round_list[i]:
        r_round_list[i] = prev
    else:
        prev = r_round_list[i]

ret = 0

for i in range(n-1):
    tmp = max(one_list[i] + r_round_list[-i-2],
              round_list[i] + r_one_list[-i-2])
    if tmp > ret:
        ret = tmp

print(ret)

Submission status: Wrong Answer

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

n, c = [int(x) for x in input().split()]
xv = [[0, 0]]
for _ in range(n):
    xv.append([int(x) for x in input().split()])
xv.sort()

cal = [0]

for i in range(1, n + 1):
    cal.append(cal[-1] + xv[i][1] - (xv[i][0] - xv[i - 1][0]))

ans = max(cal)

N = n + 1

# N: 処理する区間の長さ
INF = 2**31-1

LV = (N-1).bit_length()
N0 = 2**LV
data = [0]*(2*N0)
lazy = [0]*(2*N0)

def gindex(l, r):
    L = (l + N0) >> 1; R = (r + N0) >> 1
    lc = 0 if l & 1 else (L & -L).bit_length()
    rc = 0 if r & 1 else (R & -R).bit_length()
    for i in range(LV):
        if rc <= i:
            yield R
        if L < R and lc <= i:
            yield L
        L >>= 1; R >>= 1

# 遅延伝搬処理
def propagates(*ids):
    for i in reversed(ids):
        v = lazy[i-1]
        if not v:
            continue
        lazy[2*i-1] += v; lazy[2*i] += v
        data[2*i-1] += v; data[2*i] += v
        lazy[i-1] = 0

# 区間[l, r)にxを加算
def update(l, r, x):
    *ids, = gindex(l, r)
    propagates(*ids)

    L = N0 + l; R = N0 + r
    while L < R:
        if R & 1:
            R -= 1
            lazy[R-1] += x; data[R-1] += x
        if L & 1:
            lazy[L-1] += x; data[L-1] += x
            L += 1
        L >>= 1; R >>= 1
    for i in ids:
        data[i-1] = max(data[2*i-1], data[2*i])

# 区間[l, r)内の最大値を求める
def query(l, r):
    propagates(*gindex(l, r))
    L = N0 + l; R = N0 + r

    s = -INF
    while L < R:
        if R & 1:
            R -= 1
            s = max(s, data[R-1])
        if L & 1:
            s = max(s, data[L-1])
            L += 1
        L >>= 1; R >>= 1
    return s

for i in range(n + 1):
    update(i, i + 1, cal[i])

place = 0

for i in range(n, 0, -1):
    update(0, n + 1, xv[i][1] - 2 * (c - xv[i][0] - place))
    update(i, i + 1, -abs(query(i, i + 1)))
    ans = max(ans, query(0, n + 1))

xv = xv[::-1]

for i in range(n):
    xv[i][0] = c - xv[i][0]
xv.sort()
cal = [0]

for i in range(1, n + 1):
    cal.append(cal[-1] + xv[i][1] - (xv[i][0] - xv[i - 1][0]))

ans = max(ans, max(cal))

N = n + 1

# N: 処理する区間の長さ
INF = 2**31-1

LV = (N-1).bit_length()
N0 = 2**LV
data = [0]*(2*N0)
lazy = [0]*(2*N0)

def gindex(l, r):
    L = (l + N0) >> 1; R = (r + N0) >> 1
    lc = 0 if l & 1 else (L & -L).bit_length()
    rc = 0 if r & 1 else (R & -R).bit_length()
    for i in range(LV):
        if rc <= i:
            yield R
        if L < R and lc <= i:
            yield L
        L >>= 1; R >>= 1

# 遅延伝搬処理
def propagates(*ids):
    for i in reversed(ids):
        v = lazy[i-1]
        if not v:
            continue
        lazy[2*i-1] += v; lazy[2*i] += v
        data[2*i-1] += v; data[2*i] += v
        lazy[i-1] = 0

# 区間[l, r)にxを加算
def update(l, r, x):
    *ids, = gindex(l, r)
    propagates(*ids)

    L = N0 + l; R = N0 + r
    while L < R:
        if R & 1:
            R -= 1
            lazy[R-1] += x; data[R-1] += x
        if L & 1:
            lazy[L-1] += x; data[L-1] += x
            L += 1
        L >>= 1; R >>= 1
    for i in ids:
        data[i-1] = max(data[2*i-1], data[2*i])

# 区間[l, r)内の最大値を求める
def query(l, r):
    propagates(*gindex(l, r))
    L = N0 + l; R = N0 + r

    s = -INF
    while L < R:
        if R & 1:
            R -= 1
            s = max(s, data[R-1])
        if L & 1:
            s = max(s, data[L-1])
            L += 1
        L >>= 1; R >>= 1
    return s

for i in range(n + 1):
    update(i, i + 1, cal[i])

place = 0

for i in range(n, 0, -1):
    update(0, n + 1, xv[i][1] - 2 * (c - xv[i][0] - place))
    update(i, i + 1, -abs(query(i, i + 1)))
    ans = max(ans, query(0, n + 1))

print(ans)

Submission status: Wrong Answer

Solution code:
#095_D
n, c = map(int, input().split())
x, v = [0], [0]
for _ in range(n):
    a, b = map(int, input().split())
    x.append(a)
    v.append(b)
x.append(c)
v.append(0)

fr1, fr2 = [0 for _ in range(n+1)], [0 for _ in range(n+1)]
fl1, fl2 = [0 for _ in range(n+2)], [0 for _ in range(n+2)]

tmp1, tmp2 = 0, 0
for i in range(0, n):
    tmp1 += v[i+1] - (x[i+1] - x[i])
    tmp2 += v[i+1] - 2 * (x[i+1] - x[i])
    fr1[i+1] = max(tmp1, fr1[i])
    fr2[i+1] = max(tmp2, fr2[i])
    
tmp1, tmp2 = 0, 0
for i in range(n, 0, -1):
    tmp1 += v[i] - abs(x[i+1] - x[i])
    tmp2 += v[i] - 2 * abs(x[i+1] - x[i])
    fl1[i] = max(tmp1, fl1[i+1])
    fl2[i] = max(tmp2, fl2[i+1])

ans = 0
for i in range(n):
    ans = max(ans, fr1[i] + fl2[i+1], fr2[i] + fl1[i+1])
print(ans)

Submission status: Wrong Answer

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

A1 = [0, 0]
X1 = [0]
A2 = [0, 0]
X2 = [0]
for x, v in xv:
  A1[-1] += v - x
  A1.append(A1[-1] + x)
  X1.append(x)
A1.pop()

xv = xv[::-1]
for x, v in xv:
  A2[-1] += v - (C-x)
  A2.append(A2[-1] + (C-x))
  X2.append(C-x)
A2.pop()

max_1 = []
now = -1
for i, a in enumerate(A1):
  if a > now:
    max_1.append(i)
    now = a
  else:
    max_1.append(max_1[-1])

ans = 0
for i, a in enumerate(A2):
  idx = max_1[-i-1]
  calory = max(
    a - X2[i] + A1[idx], A1[idx] - X1[idx] + a, a
  )
  ans = max(ans, calory)

print(ans)

Submission status: Wrong Answer

Solution code:

Submission status: Wrong Answer

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

A1 = [0, 0]
X1 = [0]
A2 = [0, 0]
X2 = [0]
for x, v in xv:
  A1[-1] += v - x
  A1.append(A1[-1] + x)
  X1.append(x)
A1.pop()

xv = xv[::-1]
for x, v in xv:
  A2[-1] += v - (C-x)
  A2.append(A2[-1] + (C-x))
  X2.append(C-x)
A2.pop()

max_1 = []
now = -1
for i, a in enumerate(A1):
  if a > now:
    max_1.append(i)
    now = a
  else:
    max_1.append(max_1[-1])

ans = 0
for i, a in enumerate(A2):
  idx = max_1[-i-1]
  calory = max(
    a - X2[i] + A1[idx], A1[idx] - X1[idx] + a
  )
  ans = max(ans, calory)

print(ans)

Submission status: Wrong Answer

Solution code:
n,c = map(int,input().split())
# zero-index
x = []
v = []
sv= [] # vの累積和
f = [] # sv - x
g = [] # f の最大
fa= []
ga= []
for i in range(n):
    temp = list(map(int,input().split()))
    x.append(temp[0])
    v.append(temp[1])
    if i == 0:
        sv.append(v[-1])
    else:
        sv.append(sv[-1] + v[-1])
    f.append(sv[-1] - x[-1])
    fa.append(sv[-1]- x[-1]*2)
    if i == 0:
        g.append(f[-1])
        ga.append(fa[-1])
    else:
        g.append(max(f[-1],g[-1]))
        ga.append(max(fa[-1],ga[-1]))
ans = 0
svr = 0
for i in range(n-1):
    svr += v[n-i-1]
    ans = max(ans, svr + x[n-i-1] - c + ga[n-i-2], svr +2*x[n-i-1] - 2*c + g[n-i-2])
svr += v[0]
ans = max(ans,svr+x[0]-c,g[n-1])
print(ans)