Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/python3

from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def solve(N, C, x, v):
    pass


def main():
    N, C = map(int, input().split())

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

    cs_x, cs_v = [0] * (N + 1), [0] * (N + 1)
    for i in range(N):
        cs_x[i + 1] = sushi[i][0]
        cs_v[i + 1] = cs_v[i] + sushi[i][1]

    cs_x_rev, cs_v_rev = [0] * (N + 1), [0] * (N + 1)
    for i in range(N):
        cs_x_rev[i + 1] = C - sushi[-i - 1][0]
        cs_v_rev[i + 1] = cs_v_rev[i] + sushi[-i - 1][1]


    # print(cs_x)
    # print(cs_v)
    # print(cs_x_rev)
    # print(cs_v_rev)

    # right
    ans = 0
    for i in range(1, N + 1):
        total_x = cs_x[i]
        total_v = cs_v[i]
        ans = max(ans, total_v - total_x)

        base_x = total_x * 2
        base_v = total_v
        for j in range(1, N - i + 1):
            total_x = cs_x_rev[j] + base_x
            total_v = cs_v_rev[j] + base_v
            ans = max(ans, total_v - total_x)

    # left
    for i in range(1, N + 1):
        total_x = cs_x_rev[i]
        total_v = cs_v_rev[i]
        ans = max(ans, total_v - total_x)

        base_x = total_x * 2
        base_v = total_v
        for j in range(1, N - i + 1):
            total_x = cs_x[j] + base_x
            total_v = cs_v[j] + base_v
            ans = max(ans, total_v - total_x)

    print(ans)

if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/python3

from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def main():
    N, C = map(int, input().split())

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

    cs_x, cs_v, cs_p = [0] * (N + 1), [0] * (N + 1), [0] * (N + 1)
    for i in range(N):
        cs_x[i + 1] = sushi[i][0]
        cs_v[i + 1] = cs_v[i] + sushi[i][1]
        cs_p[i + 1] = max(cs_p[i], cs_v[i + 1] - cs_x[i + 1])

    cs_x_rev, cs_v_rev, cs_p_rev = [0] * (N + 1), [0] * (N + 1), [0] * (N + 1)
    for i in range(N):
        cs_x_rev[i + 1] = C - sushi[-i - 1][0]
        cs_v_rev[i + 1] = cs_v_rev[i] + sushi[-i - 1][1]
        cs_p_rev[i + 1] = max(cs_p_rev[i], cs_v_rev[i + 1] - cs_x_rev[i + 1])


    # print(cs_x)
    # print(cs_v)
    # print(cs_x_rev)
    # print(cs_v_rev)

    # right
    ans = 0
    for i in range(1, N + 1):
        total_x = cs_x[i]
        total_v = cs_v[i]
        profit = total_v - total_x
        ans = max(ans, profit)

        profit -= total_x
        profit += cs_p_rev[N - i]
        ans = max(ans, profit)

    # left
    for i in range(1, N + 1):
        total_x = cs_x_rev[i]
        total_v = cs_v_rev[i]
        profit = total_v - total_x
        ans = max(ans, profit)

        profit -= total_x
        profit += cs_p[N - i]
        ans = max(ans, profit)

    print(ans)

if __name__ == '__main__':
    main()