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: Accepted

Solution code:
# https://arc096.contest.atcoder.jp/tasks/arc096_b


def inp(): return map(int, input().split())


N, C = inp()
l = [tuple(inp()) for _ in range(N)]

r1 = [0] * N
r2 = [0] * N
l1 = [0] * N
l2 = [0] * N

cal = 0
ans1 = 0
ans2 = 0
for i, (x, v) in enumerate(l):  # clockwise
    cal += v
    ans1 = max(ans1, cal - x)
    ans2 = max(ans2, cal - 2 * x)
    r1[i] = ans1
    r2[i] = ans2

cal = 0
ans1 = 0
ans2 = 0
for i, (x, v) in enumerate(l[::-1]):  # counter clockwise
    x = C - x
    cal += v
    ans1 = max(ans1, cal - x)
    ans2 = max(ans2, cal - 2 * x)
    l1[-1 - i] = ans1
    l2[-1 - i] = ans2

ans = 0

for i in range(N - 1):
    ans = max(ans, r2[i] + l1[i + 1], r1[i] + l2[i + 1])

ans = max(ans, r1[-1], l1[0])
print(ans)

Submission status: Accepted

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)

e_clock = [0] * N
e_counter = [0] * N
e_clock0 = [0] * N # 時計回りしたあと、初期位置に戻った状態
e_counter0 = [0] * N # 反時計回りしたあと、初期位置に戻った状態

# 時計回り、反時計回りはそれぞれ多くて１回ずつである
# (何度も切り替えしても、距離だけ増えて効率的でない

# 時計回りに回ったときの各地点までの取得エネルギーで最大値を格納する
cal = 0
e = 0
e0 = 0
for i in range(N):
    cal += v_list[i]
    x = x_list[i]
    e = max(e, cal - x)
    e0 = max(e0, cal - 2*x)

    e_clock[i] = e
    e_clock0[i] = e0


# 反時計回りに回ったときの各地点までの取得エネルギーで最大値を格納する
cal = 0
e = 0
e0 = 0
for i in range(N-1, -1, -1):
    cal += v_list[i]
    x = C - x_list[i]
    e = max(e, cal - x)
    e0 = max(e0, cal - 2*x)
    e_counter[i] = e
    e_counter0[i] = e0

ans = 0

for i in range(N-1):
    # 時計回りのあと、反時計回りをしたとき
    e1 = e_clock0[i] + e_counter[i+1]
    # 反時計回りのあと、時計回りをしたとき
    e2 = e_counter0[i+1] + e_clock[i]

    ans = max(ans, e1, e2)

# 上はrange(N-1)のforなので
# 時計回りと反時計回りのそれぞれでまるまる１周した場合も加える
ans = max(ans, e_clock[N-1], e_counter[0])

print(ans)

Submission status: Accepted

Solution code:
def main():
    """
    1 <= N <= 10^5
    2 <= C <= 10^14
    1 <= x1 < x2 < ... < xN < C
    1 <= vi <= 10^9
    """
    N, C = map(int, input().split())
    X, V = zip(*(
        map(int, input().split())
        for _ in range(N)
    ))

    #part_point(N, C, X, V)
    editorial(N, C, X, V)


def editorial(N, C, X, V):
    rev_V = V[::-1]
    rev_X = tuple(map(lambda x: C-x, X[::-1]))

    a1 = solve_editorial(N, C, X, V)
    a2 = solve_editorial(N, C, rev_X, rev_V)
    ans = max(a1, a2)
    print(ans)


def solve_editorial(N, C, X, V):
    ans = 0
    
    # A側の累積和
    c1 = [0] * N
    c1[0] = V[0]
    for i in range(1, N):
        c1[i] = V[i] + c1[i-1]
    # 初期位置まで戻ったときの獲得カロリー
    d1 = [0] * N
    for i in range(N):
        # 食べた場所からすぐ帰る場合を計算しておく
        ans = max(c1[i] - X[i], ans)
        d1[i] = max(c1[i] - X[i] * 2, 0)
    # 寿司iまでの獲得カロリーの最大値
    e1 = [0] * N
    e1[0] = d1[0]
    for i in range(1, N):
        e1[i] = max(d1[i], e1[i-1])

    # B側の累積和
    rev_V = V[::-1]
    rev_X = tuple(map(lambda x: C-x, X[::-1]))
    c2 = [0] * N
    c2[0] = rev_V[0]
    for i in range(1, N):
        c2[i] = rev_V[i] + c2[i-1]

    # Bまでの獲得カロリーと、その1つ奥の寿司までのA側からの最大カロリー
    for i in range(N-1):
        y = max(c2[i] - rev_X[i], 0)
        ans = max(ans, e1[N-i-2] + y)

    return ans


def part_point(N, C, X, V):
    """
    円周すべてを通るパターンは最適ではない
        xN < C より
    時計回りにある寿司Aまで
    そこから初期位置まで
    最後に反時計回りにBまで
        OA * 2 + OB or
        OB * 2 + OA
    """
    # tupleの方が省memory (sys.getsizeof より)
    rev_V = V[::-1]
    rev_X = tuple(map(lambda x: C-x, X[::-1]))

    # 逆回りを計算し忘れてた
    a1 = solve_p(N, X, V, rev_X, rev_V)
    a2 = solve_p(N, rev_X, rev_V, X, V)

    ans = max(a1, a2)
    print(ans)


def solve_p(N, X, V, rev_X, rev_V):
    ans = 0
    for i in range(0, N):
        # Aまで
        a = sum(V[:i+1]) - X[i]
        ans = max(ans, a)
        # Oまで戻る
        a = max(a - X[i], 0)
        for j in range(N-i-1):
            # Bまで
            b = a + sum(rev_V[:j+1]) - rev_X[j]
            ans = max(ans, b)

    return ans


if __name__ == '__main__':
    main()

Submission status: Accepted

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


def solve():
    normal = [sushi[0][1] - sushi[0][0]]
    n_append = normal.append
    cur = normal[0]
    n_max = normal[0]
    for (s1_pos, s1_cal), (s2_pos, s2_cal) in zip(sushi, sushi[1:]):
        cur += s2_cal - (s2_pos - s1_pos)
        if n_max < cur:
            n_max = cur
        n_append(n_max)

    reverse = [sushi[-1][1] - (C-sushi[-1][0])]
    r_append = reverse.append
    cur = reverse[0]
    r_max = max(0, reverse[0])
    for (s1_pos, s1_cal), (s2_pos, s2_cal) in zip(sushi[::-1], sushi[-2::-1]):
        cur += s2_cal - (s1_pos-s2_pos)
        if r_max < cur:
            r_max = cur
        r_append(r_max)

    result= 0
    for i, (pos, cal) in enumerate(sushi[:-1]):
        result = max(result, normal[i], normal[i]-pos+reverse[N-2-i])
    for i, (pos, cal) in enumerate(sushi[:0:-1]):
        result = max(result, reverse[i], reverse[i]-(C-pos)+normal[N-2-i])
    result = max(result, normal[-1], reverse[-1])

    print(result)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

pre_mx = [(0,0)]
suf_mx = [(0,0)]
run_psum = 0
run_ssum = 0
data = []
for i in range(N):
    x,v = map(int,input().split())
    if run_psum + v - x > pre_mx[i][1]:
        pre_mx.append((x,run_psum + v - x))
    else:
        pre_mx.append((pre_mx[i][0],pre_mx[i][1]))
    run_psum += v
    data.append((x,v))

for i in range(N-1,-1,-1):
    if run_ssum + data[i][1] - C + data[i][0] > suf_mx[N-1-i][1]:
        suf_mx.append((C - data[i][0],run_ssum + data[i][1] - C + data[i][0]))
    else:
        suf_mx.append((suf_mx[N-1-i][0],suf_mx[N-1-i][1]))
    run_ssum += data[i][1]

ansp = anss = 0
for i in range(N+1):
    if ansp < pre_mx[i][1] + suf_mx[-i-1][1] - pre_mx[i][0]:
        ansp = pre_mx[i][1] + suf_mx[-i-1][1]
        if suf_mx[-i-1][0] != 0:
            ansp -= pre_mx[i][0]
            
    if anss < suf_mx[i][1] + pre_mx[-i-1][1] - suf_mx[i][0]:
        anss = suf_mx[i][1] + pre_mx[-i-1][1]
        if pre_mx[-i-1][0] != 0:
            anss -= suf_mx[i][0]

print(max(ansp,anss))

Submission status: Accepted

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

R1 = [0]*N
R2 = [0]*N
L1 = [0]*N
L2 = [0]*N

cal = 0
ans1 = 0
ans2 = 0
for i, (x, v) in enumerate(xv):
    cal += v
    ans1 = max(ans1, cal-x)
    ans2 = max(ans2, cal-2*x)
    R1[i] = ans1
    R2[i] = ans2


cal = 0
ans1 = 0
ans2 = 0
for i, (x, v) in enumerate(xv[::-1]):
    x = C-x
    cal += v
    ans1 = max(ans1, cal-x)
    ans2 = max(ans2, cal-2*x)
    L1[-1-i] = ans1
    L2[-1-i] = ans2

ans = 0

for i in range(N-1):
    ans = max(ans, R2[i]+L1[i+1], R1[i]+L2[i+1])

ans = max(ans, R1[-1], L1[0])
print(ans)

Submission status: Accepted

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

sum = 0
r = [0]
for x, v in m:
    sum += v
    r.append(sum-x)

sum = 0
l = [0]
for x, v in m[::-1]:
    sum += v
    l.append(sum-(c-x))

def right():
    max_ = r[0]
    for i in r:
        if max_ < i:
            max_ = i
        yield max_

def left():
    max_ = l[0]
    for i in l:
        if max_ < i:
            max_ = i
        yield max_

rm = [[0, 0]] + m
lm = m + [[c, c]]

rpath = list(right())
lmax = max([l[i]+rpath[-(i+1)]-(c-lm[-(i+1)][0]) for i in range(n)])

lpath = list(left())
rmax = max([r[i]+lpath[-(i+1)]-rm[i][0] for i in range(n)])


print(max(rmax, lmax))

Submission status: Accepted

Solution code:
def d_static_sushi(N, C, Sushi):
    sushi_pos_cw = [row[0] for row in Sushi]
    sushi_pos_ccw = [abs(row[0] - C) for row in Sushi][::-1]
    sushi_value_cw = [row[1] for row in Sushi]
    sushi_value_ccw = sushi_value_cw[::-1]

    # 時計回り
    f1,g1,f2,g2=[],[],[],[]
    s1,s2=0,0
    for i in range(N):
        s1+=sushi_value_cw[i]-sushi_pos_cw[i]
        f1.append(s1)
        g1.append(s1)
        s1+=sushi_pos_cw[i]
        s2+=sushi_value_ccw[i]-sushi_pos_ccw[i]
        f2.append(s2)
        g2.append(s2)
        s2+=sushi_pos_ccw[i]
    for i in range(1,N):
        if g1[i]<g1[i-1]:
            g1[i]=g1[i-1]
        if g2[i]<g2[i-1]:
            g2[i]=g2[i-1]
    # print(f1,g1,f2,g2)
    # f1 = [sum(sushi_value_cw[:i + 1]) - sushi_pos_cw[i] for i in range(N)]
    # g1 = [max(f1[:i + 1]) for i in range(N)]
    # #反時計回り
    # f2 = [sum(sushi_value_ccw[:i + 1]) - sushi_pos_ccw[i] for i in range(N)]
    # g2 = [max(f2[:i + 1]) for i in range(N)]
    # print(f1,g1,f2,g2)
    tmp1 = 0
    for b in range(N + 1):  # b:反時計回りに歩いて食べる寿司の個数
        if b == 0:
            cal = g1[N - 1]
        elif b == N:
            cal = f2[b - 1]
        else:
            cal = g1[N - b - 1] + f2[b - 1] - sushi_pos_ccw[b - 1]
        tmp1 = max(tmp1, cal)

    tmp2 = 0
    for a in range(N + 1):  # a:時計回りに歩いて食べる寿司の個数
        if a == 0:
            cal = g2[N - 1]
        elif a == N:
            cal = f1[a - 1]
        else:
            cal = g2[N - a - 1] + f1[a - 1] - sushi_pos_cw[a - 1]
        tmp2 = max(tmp2, cal)
    ans = max(tmp1, tmp2)
    return ans

N,C = [int(i) for i in input().split()]
Sushi = [[int(i) for i in input().split()] for j in range(N)]
print(d_static_sushi(N, C, Sushi))

Submission status: Accepted

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

pre_mx = [(0,0)]
suf_mx = [(0,0)]
run_psum = 0
run_ssum = 0
data = []
for i in range(N):
    x,v = map(int,input().split())
    if run_psum + v - x > pre_mx[i][1]:
        pre_mx.append((x,run_psum + v - x))
    else:
        pre_mx.append((pre_mx[i][0],pre_mx[i][1]))
    run_psum += v
    data.append((x,v))

for i in range(N-1,-1,-1):
    if run_ssum + data[i][1] - C + data[i][0] > suf_mx[N-1-i][1]:
        suf_mx.append((C - data[i][0],run_ssum + data[i][1] - C + data[i][0]))
    else:
        suf_mx.append((suf_mx[N-1-i][0],suf_mx[N-1-i][1]))
    run_ssum += data[i][1]

ansp = anss = 0
for i in range(N+1):
    if ansp < pre_mx[i][1] + suf_mx[-i-1][1] - pre_mx[i][0]:
        ansp = pre_mx[i][1] + suf_mx[-i-1][1]
        if suf_mx[-i-1][0] != 0:
            ansp -= pre_mx[i][0]

for i in range(N+1):
    if anss < suf_mx[i][1] + pre_mx[-i-1][1] - suf_mx[i][0]:
        anss = suf_mx[i][1] + pre_mx[-i-1][1]
        if pre_mx[-i-1][0] != 0:
            anss -= suf_mx[i][0]

print(max(ansp,anss))

Submission status: Accepted

Solution code:
N,C = map(int, input().split())
xv = []
for _ in range(N):
    xv.append(tuple(map(int, input().split())))
ccw_first = [0]
ccw_second = [0]
cw_first = [0]
cw_second = [0]
sm = 0
for x,v in xv:
    sm += v
    ccw_first.append(max(ccw_first[-1], sm - 2 * x))
    ccw_second.append(max(ccw_second[-1], sm - x))
sm = 0
for x,v in reversed(xv):
    sm += v
    cw_first.append(max(cw_first[-1], sm - 2 * (C - x)))
    cw_second.append(max(cw_first[-1], sm - (C - x)))
ans = 0
for i in range(N+1):
    ans = max(ans, cw_first[i] + ccw_second[N-i], ccw_first[i] + cw_second[N-i])
print(ans)

Submission status: Accepted

Solution code:
n,c=[int(i) for i in input().split()]
x=[[0 for i in range(2)] for j in range(n)]
for i in range(n):
    x[i]= [int(i) for i in input().split()]
x.sort(key=lambda x:x[0])

state=0

left_wa=[0 for i in range(n)]
right_wa=[0 for i in range(n)]

for i in range(n):
    if i==0:
        left_wa[i]=x[i][1]-abs(x[i][0]-state)
    else:
        left_wa[i]=left_wa[i-1]+x[i][1]-abs(x[i][0]-state)
    state=x[i][0]

state=c

for i in range(n):
    if i==0:
        right_wa[i]=x[n-1-i][1]-abs(x[n-1-i][0]-state)
    else:
        right_wa[i]= right_wa[i-1]+x[n-1-i][1]-abs(abs(c-x[n-1-i][0])-abs(state))
    state=c-x[n-1-i][0]

kcal=0

m_right=[0 for i in range(n+1)]
m_left=[0 for i in range(n+1)]

for i in range(n+1):
    if i!=0:
        m_right[i]=max(m_right[i-1], right_wa[i-1])
        m_left[i]=max(m_left[i-1], left_wa[i-1])

#print(m_left)

for i in range(n):
    if i!=n-1:
#        right=max(right_wa[:n-1-i])-x[i][0]
        right= m_right[n-1-i]-x[i][0]
    else: right=0
    kcal=max(kcal, left_wa[i]+max(0, right))

for i in range(n):
    if i!=n-1:
#        left=max(left_wa[:n-1-i])-abs(c-x[n-i-1][0])
       left=m_left[n-1-i]-abs(c-x[n-i-1][0])

    else: left=0
    kcal=max(kcal, right_wa[i]+max(0, left))


print(kcal)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N, C = map(int, input().split())
    values = [list(map(int, input().split())) for _ in range(N)]
    xs, vs = zip(*values)

    # clock = clockwise, aclock = anti-clockwise
    clock_cum = []
    clock_max = []
    aclock_cum = []
    aclock_max = []
    for i in range(N):
        if i == 0:
            cum_i = vs[i] - xs[i]
            max_i = max(0, cum_i)

            acum_i = vs[N - i - 1] - (C - xs[N - i - 1])
            amax_i = max(0, acum_i)
        else:
            cum_i = clock_cum[-1] + xs[i - 1] + vs[i] - xs[i]
            max_i = max(clock_max[-1], cum_i)

            acum_i = aclock_cum[-1] + (C - xs[N - i - 1 + 1]) + vs[N - i - 1] - (C - xs[N - i - 1])
            amax_i = max(aclock_max[-1], acum_i)

        clock_cum.append(cum_i)
        clock_max.append(max_i)
        aclock_cum.append(acum_i)
        aclock_max.append(amax_i)

    best = 0
    for i in range(N):
        if i != N - 1:
            # clock or clock -> return -> aclock
            best = max(best, clock_cum[i],
                       clock_cum[i] - xs[i] + aclock_max[N - i - 2])
            # aclock or aclock -> return -> clock
            best = max(best, aclock_cum[i],
                       aclock_cum[i] - (C - xs[N - i - 1]) + clock_max[N - i - 2])
        else:
            best = max(best, clock_cum[i])
            best = max(best, aclock_cum[i])

    print(best)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    # first_line = "15 10000000000"
    # lines = ["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"]
    # N, C = map(int, first_line.split())
    # values = [list(map(int, li.split())) for li in lines]

    N, C = map(int, input().split())
    values = [list(map(int, input().split())) for _ in range(N)]
    xs, vs = zip(*values)

    clock_cum = []
    clock_max = []
    aclock_cum = []
    aclock_max = []
    for i in range(N):
        if i == 0:
            cum_i = vs[i] - xs[i]
            max_i = max(0, cum_i)

            acum_i = vs[N - i - 1] - (C - xs[N - i - 1])
            amax_i = max(0, acum_i)
        else:
            cum_i = clock_cum[-1] + xs[i - 1] + vs[i] - xs[i]
            max_i = max(clock_max[-1], cum_i)

            acum_i = aclock_cum[-1] + (C - xs[N - i - 1 + 1]) + vs[N - i - 1] - (C - xs[N - i - 1])
            amax_i = max(aclock_max[-1], acum_i)

        clock_cum.append(cum_i)
        clock_max.append(max_i)
        aclock_cum.append(acum_i)
        aclock_max.append(amax_i)

    best = 0
    for i in range(N):
        if i != N - 1:
            # clock or clock -> return -> aclock
            best = max(best, clock_cum[i],
                       clock_cum[i] - xs[i] + aclock_max[N - i - 2])
            # aclock or aclock -> return -> clock
            best = max(best, aclock_cum[i],
                       aclock_cum[i] - (C - xs[N - i - 1]) + clock_max[N - i - 2])
        else:
            best = max(best, clock_cum[i])
            best = max(best, aclock_cum[i])

    print(best)

Submission status: Accepted

Solution code:
n, c = map(int, input().split())
X = [0] * n
V = [0] * n
right = [0] * n
left = [0] * n
rl = [0] * n
lr = [0] * n
rlm = [0] * n
lrm = [0] * n

##(lambda arr, idx:sum(arr[:idx]))([1,2,3,4,5],3)


calsum = 0
for i in range(n):
    X[i], V[i] = map(int, input().split())
    calsum += V[i]
    right[i] = calsum - X[i]
    rl[i] = calsum - 2 * X[i]

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

m = 0
for i in range(0, n - 1):
    m = max(m, left[i])
    rlm[i] = m + rl[n - i - 2]

m = 0
for i in range(0, n - 1):
    m = max(m, right[i])
    lrm[i] = m + lr[n - i - 2]

print(max(0, max(right), max(left), max(rlm), max(lrm)))

Submission status: Accepted

Solution code:
n, c = map(int, input().split())
X = [0 for _ in range(n)]
V = [0 for _ in range(n)]
right = [0 for _ in range(n)]
left = [0 for _ in range(n)]
rl = [0 for _ in range(n)]
lr = [0 for _ in range(n)]
rlm = [0 for _ in range(n)]
lrm = [0 for _ in range(n)]

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

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

m = 0
for i in range(0, n-1):
    m = max(m, left[i])
    rlm[i] = m + rl[n-i-2]

m = 0
for i in range(0, n-1):
    m = max(m, right[i])
    lrm[i] = m + lr[n-i-2]

print(max(0, max(right), max(left), max(rlm), max(lrm)))

Submission status: Accepted

Solution code:
n, c = map(int, input().split())
X = [0 for _ in range(n)]
V = [0 for _ in range(n)]
right = [0 for _ in range(n)]
left = [0 for _ in range(n)]
rl = [0 for _ in range(n)]
lr = [0 for _ in range(n)]

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

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

M = max(0, max(right), max(left))
m = 0
for i in range(0, n-1):
    m = max(m, left[i])
    M = max(M, m + rl[n-i-2])

m = 0
for i in range(0, n-1):
    m = max(m, right[i])
    M = max(M, m + lr[n-i-2])

print(M)

Submission status: Accepted

Solution code:
def solve(xv_sorted):
    x = [False for _ in range(n)]
    y = [False for _ in range(n)]

    s = 0
    buf = (- 1) * float('inf')
    for i in range(n):
        s += xv_sorted[i][1]##cal 
        x[i] = max(buf, s - 2 * xv_sorted[i][0])##pos
        buf = x[i]

    ##print(x)

    s = 0
    buf = (- 1) * float('inf')
    for i in sorted(range(n), reverse=True):
        s += xv_sorted[i][1]##cal 
        y[i] = max(buf, s - (c - xv_sorted[i][0]))##pos
        buf = y[i]

    ##print(y)

    ans = max(x[n - 1], y[0])
    for i in range(n - 1):
        ans = max(ans, x[i] + y[i + 1])

    return ans

if __name__ == '__main__':
    n, c = map(int, input().split())
    xv = []
    for i in range(n):
        xv.append(list(map(int, input().split())))
    xv_sorted = sorted(xv, key=lambda x:x[0])
    ans = 0
    ans = max(ans, solve(xv_sorted))

    for i in range(n):
        xv_sorted[i][0] = abs(c - xv_sorted[i][0]) 
    xv_sorted = sorted(xv_sorted, key=lambda x:x[0])
    ans = max(ans, solve(xv_sorted))
    print(ans)

Submission status: Accepted

Solution code:
def solve(xv_sorted):
    x = [False for _ in range(n)]
    y = [False for _ in range(n)]

    s = 0
    buf = (- 1) * float('inf')
    for i in range(n):
        s += xv_sorted[i][1]##cal 
        x[i] = max(buf, s - 2 * xv_sorted[i][0])##pos
        buf = x[i]

    ##print(x)

    s = 0
    buf = (- 1) * float('inf')
    for i in sorted(range(n), reverse=True):
        s += xv_sorted[i][1]##cal 
        y[i] = max(buf, s - (c - xv_sorted[i][0]))##pos
        buf = y[i]

    ##print(y)

    ans = max(x[n - 1], y[0])
    for i in range(n - 1):
        ans = max(ans, x[i] + y[i + 1])

    return ans

if __name__ == '__main__':
    n, c = map(int, input().split())
    xv = []
    for i in range(n):
        xv.append(list(map(int, input().split())))
    xv_sorted = sorted(xv, key=lambda x:x[0])
    ans = 0
    ans = max(ans, solve(xv_sorted))

    for i in range(n):
        xv_sorted[i][0] = abs(c - xv_sorted[i][0]) 
    xv_sorted = sorted(xv, key=lambda x:x[0])
    ans = max(ans, solve(xv_sorted))
    print(ans)

Submission status: Accepted

Solution code:
n, c = map(int, input().split())
X, V, right, left, rl, lr = [], [], [], [], [], []

calsum = 0
for _ in range(n):
    x, v = map(int, input().split())
    X.append(x)
    V.append(v)
    calsum += v
    right.append(calsum-x)
    rl.append(calsum-2*x)

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

M = max(0, max(right), max(left))
m = 0
for i in range(0, n-1):
    m = max(m, left[i])
    M = max(M, m + rl[n-i-2])

m = 0
for i in range(0, n-1):
    m = max(m, right[i])
    M = max(M, m + lr[n-i-2])

print(M)

Submission status: Accepted

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
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]))

m = 0
M = 0
for i in range(0, n-1):
    m = max(m, left[i])
    M = max(M, m + rightleft[n-i-2])

mm = 0
MM = 0
for i in range(0, n-1):
    mm = max(mm, right[i])
    MM = max(MM, mm + leftright[n-i-2])

print(max(max(right), max(left), M, MM, 0))

Submission status: Accepted

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

# N,Cを取得
n, c = map(int, input().split())

# x1, v1..xN, vNを取得
xs = []
vs = []
for _i in range(n):
    x, v = map(int, input().split())
    xs.append(x)
    vs.append(v)

r1, r2, l1, l2 = [[], [], [], []]
r_sum, l_sum = [0, 0]
for i in range(n):
    r_sum += vs[i]
    r1.append(r_sum - xs[i])
    r2.append(r_sum - 2 * xs[i])

    i_rev = n - i - 1
    l_sum += vs[i_rev]
    l1.append(l_sum - (c - xs[i_rev]))
    l2.append(l_sum - 2 * (c - xs[i_rev]))

r1_max, r2_max = [0, 0]
l1_max, l2_max = [0, 0]
r1_maxs, r2_maxs = [[0], [0]]
l1_maxs, l2_maxs = [[0], [0]]
for i in range(n):
    r1_max = max(r1_max, r1[i])
    r1_maxs.append(r1_max)
    r2_max = max(r2_max, r2[i])
    r2_maxs.append(r2_max)
    l1_max = max(l1_max, l1[i])
    l1_maxs.append(l1_max)
    l2_max = max(l2_max, l2[i])
    l2_maxs.append(l2_max)

r1_l2_max = 0
l1_r2_max = 0
for i in range(0, n + 1):
    r1_l2_max = max(r1_l2_max, r1_maxs[i] + l2_maxs[n - i])
    l1_r2_max = max(l1_r2_max, l1_maxs[i] + r2_maxs[n - i])

total_max = max(r1_l2_max, l1_r2_max)

# 結果を出力
print("{}".format(total_max))

Submission status: Accepted

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)
right = []
right2 = []
left = []
count = 0
left2 = []
for i in range(n):
  if i == 0:
    count += (V[i] - X[i])
    right.append(count)
    right2.append(count)
  else:
    count += (V[i] - (X[i] - X[i - 1]))
    right.append(count)
    right2.append(max(right2[-1],count))

count = 0
for i in range(n):
  if i == 0:
    count += (V[(i + 1) * (-1)] - (c - X[(i + 1) * (-1)]))
    left.append(count)
    left2.append(count)
  else:
    count += (V[(i + 1) * (-1)] - ((X[i * (-1)] - X[((i + 1) * (-1))])))
    left.append(count)
    left2.append(max(left2[-1],count))
ans = max(0,max(right),max(left))
for i in range(n - 1):
  ans = max(ans, right[i] - X[i] + left2[n-i-2])
for i in range(n - 1):
  ans = max(ans, left[i] - (c - X[(i + 1) * (-1)]) + right2[n-i-2])
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

def main():
    N,C = map(int, input().split())
    x = [0 for _ in range(N)]
    v = [0 for _ in range(N)]
    for i in range(N):
        x[i],v[i] = map(int, input().split())

    a = [0 for _ in range(N)]
    amax = 0
    b = [0 for _ in range(N)]
    bmax = 0
    cal1 = []
    cal2 = []
    for i in range(N):
        if i == 0:
            cal1.append(v[i]-x[i])
            cal2.append(v[-i-1]-(C-x[-i-1]))
            if amax < cal1[-1]:
                amax = cal1[-1]
            if bmax < cal2[-1]:
                bmax = cal2[-1]
        else:
            cal1.append(cal1[-1]+v[i]-abs(x[i]-x[i-1]))
            cal2.append(cal2[-1]+v[-i-1]-abs(x[-i-1]-x[-i]))
            if amax < cal1[-1]:
                amax = cal1[-1]
            if bmax < cal2[-1]:
                bmax = cal2[-1]
        a[i] = max([amax, cal1[-1]])
        b[i] = max([bmax, cal2[-1]])
    c = [0 for _ in range(N-1)]
    d = [0 for _ in range(N-1)]
    if c==[]:
        c.append(0)
        d.append(0)
    for i in range(N-1):
        c[i] = a[i] + b[-i-2] - x[i]
        d[i] = b[i] + a[-i-2] - (C-x[-i-1])
    return max([amax,bmax,max(c),max(d)])

if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
import sys
raw_input = sys.stdin.readline
range = xrange
N, C = map(int, raw_input().split())
x = [None]*N
v = [None]*N
for i in range(N):
    x[i], v[i] = map(int, raw_input().split())
cw = [0]*(N+1)
ccw = [0]*(N+1)
for i in range(N):
    cw[i+1] = cw[i] + v[i]
    ccw[i+1] = ccw[i] + v[N-1-i]
for i in range(1, N+1):
    cw[i] -= x[i-1]
    ccw[i] -= (C - x[N-i])
maxccw = [0]*(N+1)
maxcw = [0]*(N+1)
for i in range(1, N+1):
    maxccw[i] = max(maxccw[i-1], ccw[i])
    maxcw[i] = max(maxcw[i-1], cw[i])
ans = max(0, maxcw[-1], maxccw[-1])
for i in range(1, N+1):
    ans = max(ans, cw[i] - x[i-1] + maxccw[N-i])
    ans = max(ans, ccw[i] - C + x[N-i] + maxcw[N-i])
print ans

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3


n, c = list(map(int, input().split()))
xv = [[0, 0]]+[list(map(int, input().split())) for i in range(n)] + [[c, 0]]
xv_rev = reversed(xv)

print(xv)
ans = 0
cal = 0
dis = 0

for i in range(1, len(xv)):
    cal += xv[i][1]
    cal -= (xv[i][0]-xv[i-1][0])
    ans = max(cal, ans)

for i in range(1, xv_rev):
    cal += xv[i][1]
    cal -= (xv[i][0]-xv[i-1][0])
    ans = max(cal, ans)

print(ans)

Submission status: Runtime Error

Solution code:
n, c = map(int, input().split())
ss = []
adj = 0
if c % 2 == 0:
    c += 1
    adj = 1
c_div_2 = c / 2
cnt_l = 0
for i in range(n):
    x, v = map(int, input().split())
    ss.append((x,v))
    if x < c_div_2:
        cnt_l += 1
cnt_r = 0
ss_rev = ss[::-1]
for i in ss_rev:
    x, v = i
    if c-adj-x < c_div_2:
        cnt_r += 1
    else:
        break

res = -c
for s in ss:
    res += s[1]
res = max(res,0)

rsw_all_l = [0]*n
rsw_all_l[0] = ss[0][1]
for i in range(1,n):
    rsw_all_l[i] = rsw_all_l[i-1] + ss[i][1]
for i in range(n):
    rsw_all_l[i] -= ss[i][0]
rsw_all_r = [0]*n
rsw_all_r[0] = ss_rev[0][1]
for i in range(1,n):
    rsw_all_r[i] = rsw_all_r[i-1] + ss_rev[i][1]
for i in range(n):
    rsw_all_r[i] -= c-ss_rev[i][0]
res = max(res, max(rsw_all_l), max(rsw_all_r))
rsw_l = [0]*(cnt_l)
if cnt_l >= 1:
    rsw_l[0] = ss[0][1]
if cnt_l >= 2:
    for i in range(1,cnt_l):
        rsw_l[i] = rsw_l[i-1] + ss[i][1]
rsw_l_opp = rsw_l.copy()
if cnt_l >= 1:
    rsw_l[0] -= ss[0][0]
    rsw_l_opp[0] -= ss[0][0]*2
if cnt_l >= 2:
    for i in range(1,cnt_l):
        rsw_l[i] -= ss[i][0]
        rsw_l_opp[i] -= ss[i][0]*2
rsw_r = [0]*(cnt_r)
if cnt_r >= 1:
    rsw_r[0] = ss_rev[0][1]
if cnt_r >= 2:
    for i in range(1,cnt_r):
        rsw_r[i] = rsw_r[i-1] + ss_rev[i][1]
rsw_r_opp = rsw_r.copy()
if cnt_r >= 1:
    rsw_r[0] -= c-adj-ss_rev[0][0]
    rsw_r_opp[0] -= (c-adj-ss_rev[0][0])*2
if cnt_r >= 2:
    for i in range(1,cnt_r):
        rsw_r[i] -= c-adj-ss_rev[i][0]
        rsw_r_opp[i] -= (c-adj-ss_rev[i][0])*2
res = max(res,max(rsw_l)+max([0]+rsw_r_opp))
res = max(res,max(rsw_r)+max([0]+rsw_l_opp))
print(res)