Buggy python submission (verdict: Time Limit Exceeded):
from itertools import accumulate

N, C = map(int, input().split())
sushi = [[int(x) for x in input().split()] for _ in range(N)]


def solve():
    """
    全探索O(N^2)
    v を事前に計算
    """
    V = [0] * (N + 1)
    for i in range(N):
        V[i + 1] = V[i] + sushi[i][1]

    ans = 0
    for i in range(N + 1):
        # OA
        if i == 0:
            oa_x = 0
        else:
            oa_x = sushi[i - 1][0]
        oa_v = V[i]

        for j in range(N + 1):
            # OB
            if j == 0:
                ob_x = 0
            else:
                ob_x = C - sushi[j - 1][0]
            ob_v = V[N] - V[j - 1]

            v = oa_v + ob_v

            # 2OA + OB
            x = 2 * oa_x + ob_x
            if x < C:
                ans = max(ans, v - x)

            # OA + 2OB
            x = oa_x + 2 * ob_x
            if x < C:
                ans = max(ans, v - x)
    print(ans)


solve()


Fixed submission:
from itertools import accumulate

N, C = map(int, input().split())
sushi = [[int(x) for x in input().split()] for _ in range(N)]


def solve():
    """
    O(N)
    Bを固定したときの最適なAを事前に計算
    """
    V = [0] * (N + 1)
    for i in range(N):
        V[i + 1] = V[i] + sushi[i][1]

    f1 = [0] + [V[a + 1] - sushi[a][0] for a in range(N)]
    f2 = [0] + [V[a + 1] - 2 * sushi[a][0] for a in range(N)]

    g1 = [x for x in accumulate(f1, max)]
    g2 = [x for x in accumulate(f2, max)]

    ans = 0
    for j in range(N + 1):
        # OA
        if j == 0:
            i = N
        else:
            i = j - 1

        # OB
        if j == 0:
            ob_x = 0
        else:
            ob_x = C - sushi[j - 1][0]
        ob_v = V[N] - V[j - 1]

        # 2OA + OB
        ans = max(ans, g2[i] + ob_v - ob_x)

        # OA + 2OB
        ans = max(ans, g1[i] + ob_v - 2 * ob_x)
    print(ans)


solve()

Buggy python submission (verdict: Time Limit Exceeded):
N, C = map(int, input().split())
sushi = [[int(x) for x in input().split()] for _ in range(N)]

ans = 0
for i in range(N + 1):
    # OA
    if i == 0:
        oa_x = 0
        oa_v = 0
    else:
        oa_x = sushi[i - 1][0]
        oa_v = sum([sushi[ii][1] for ii in range(i)])

    for j in range(N + 1):
        # OB
        if j == 0:
            ob_x = 0
            ob_v = 0
        else:
            ob_x = C - sushi[j - 1][0]
            ob_v = sum([sushi[jj][1] for jj in range(N - 1, j - 2, -1)])

        v = oa_v + ob_v

        # 2OA + OB
        x = 2 * oa_x + ob_x
        if x < C:
            ans = max(ans, v - x)

         # OA + 2OB
        x = oa_x + 2 * ob_x
        if x < C:
            ans = max(ans, v - x)
print(ans)


Fixed submission:
from itertools import accumulate

N, C = map(int, input().split())
sushi = [[int(x) for x in input().split()] for _ in range(N)]


def solve():
    """
    O(N)
    Bを固定したときの最適なAを事前に計算
    """
    V = [0] * (N + 1)
    for i in range(N):
        V[i + 1] = V[i] + sushi[i][1]

    f1 = [0] + [V[a + 1] - sushi[a][0] for a in range(N)]
    f2 = [0] + [V[a + 1] - 2 * sushi[a][0] for a in range(N)]

    g1 = [x for x in accumulate(f1, max)]
    g2 = [x for x in accumulate(f2, max)]

    ans = 0
    for j in range(N + 1):
        # OA
        if j == 0:
            i = N
        else:
            i = j - 1

        # OB
        if j == 0:
            ob_x = 0
        else:
            ob_x = C - sushi[j - 1][0]
        ob_v = V[N] - V[j - 1]

        # 2OA + OB
        ans = max(ans, g2[i] + ob_v - ob_x)

        # OA + 2OB
        ans = max(ans, g1[i] + ob_v - 2 * ob_x)
    print(ans)


solve()

Buggy python submission (verdict: Time Limit Exceeded):
n, c = map(int, input().split())
x = [0] * (n + 1)
v = [0] * (n + 1)
for i in range(1, n + 1):
    x[i], v[i] = map(int, input().split())

ans = 0

for i in range(n + 1):
    d = x[i]
    s = v[:i + 1]
    k = sum(s) - d
    if k > ans:
        ans = k

for j in range(n + 1):
    d = c - x[j]
    s = v[j:]
    k = sum(s) - d
    if k > ans:
        ans = k

for i in range(1, n + 1):
    d1r = 2 * x[i]
    d1l = x[i]
    s1 = sum(v[:i + 1])
    for j in range(i + 1, n + 1):
        d2r = c - x[j]
        s = s1 + sum(v[j:])
        k = s - (d1r + d2r)
        if k > ans:
            ans = k
        d2l = 2 * d2r
        k = s - (d1l + d2l)
        if k > ans:
            ans = k

print(ans)

Fixed submission:
from itertools import accumulate

N, C = map(int, input().split())
sushi = [[int(x) for x in input().split()] for _ in range(N)]


def solve():
    """
    O(N)
    Bを固定したときの最適なAを事前に計算
    """
    V = [0] * (N + 1)
    for i in range(N):
        V[i + 1] = V[i] + sushi[i][1]

    f1 = [0] + [V[a + 1] - sushi[a][0] for a in range(N)]
    f2 = [0] + [V[a + 1] - 2 * sushi[a][0] for a in range(N)]

    g1 = [x for x in accumulate(f1, max)]
    g2 = [x for x in accumulate(f2, max)]

    ans = 0
    for j in range(N + 1):
        # OA
        if j == 0:
            i = N
        else:
            i = j - 1

        # OB
        if j == 0:
            ob_x = 0
        else:
            ob_x = C - sushi[j - 1][0]
        ob_v = V[N] - V[j - 1]

        # 2OA + OB
        ans = max(ans, g2[i] + ob_v - ob_x)

        # OA + 2OB
        ans = max(ans, g1[i] + ob_v - 2 * ob_x)
    print(ans)


solve()

Buggy python submission (verdict: Time Limit Exceeded):
import itertools

n, c = map(int, input().split())
x = [0] * (n + 1)
v = [0] * (n + 1)
for i in range(1, n + 1):
    x[i], v[i] = map(int, input().split())

ans = 0

for i in range(n + 1):
    d = x[i]
    s = v[:i + 1]
    k = sum(s) - d
    if k > ans:
        ans = k

for j in range(n + 1):
    d = c - x[j]
    s = v[j:]
    k = sum(s) - d
    if k > ans:
        ans = k

for i, j in itertools.combinations(range(1, n + 1), 2):
    d = 2 * x[i] + (c - x[j])
    s = v[:i + 1] + v[j:]
    k = sum(s) - d
    if k > ans:
        ans = k

for i, j in itertools.combinations(range(1, n + 1), 2):
    d = x[i] + 2 * (c - x[j])
    s = v[:i + 1] + v[j:]
    k = sum(s) - d
    if k > ans:
        ans = k

print(ans)

Fixed submission:
from itertools import accumulate

N, C = map(int, input().split())
sushi = [[int(x) for x in input().split()] for _ in range(N)]


def solve():
    """
    O(N)
    Bを固定したときの最適なAを事前に計算
    """
    V = [0] * (N + 1)
    for i in range(N):
        V[i + 1] = V[i] + sushi[i][1]

    f1 = [0] + [V[a + 1] - sushi[a][0] for a in range(N)]
    f2 = [0] + [V[a + 1] - 2 * sushi[a][0] for a in range(N)]

    g1 = [x for x in accumulate(f1, max)]
    g2 = [x for x in accumulate(f2, max)]

    ans = 0
    for j in range(N + 1):
        # OA
        if j == 0:
            i = N
        else:
            i = j - 1

        # OB
        if j == 0:
            ob_x = 0
        else:
            ob_x = C - sushi[j - 1][0]
        ob_v = V[N] - V[j - 1]

        # 2OA + OB
        ans = max(ans, g2[i] + ob_v - ob_x)

        # OA + 2OB
        ans = max(ans, g1[i] + ob_v - 2 * ob_x)
    print(ans)


solve()