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

sys.setrecursionlimit(10000000)


def calc_cost(src_x, dst_x, C):
    """
    >>> calc_cost(9300000000, 400000000, 10000000000)
    1
    """

    src_x, dst_x = min(src_x, dst_x), max(src_x, dst_x)
    return min(dst_x - src_x, src_x + (C - dst_x))


x_kcal_dict = dict()
kcal_logs = []


def find_route(xs, vs, C, x_log, kcal):
    global x_kcal_dict
    x_kcal_dict[tuple(x_log)] = kcal

    global kcal_logs
    kcal_logs.append(kcal)
    if not xs:
        return kcal

    clockwise = find_route(xs[1:], vs[1:], C, x_log + [xs[0]],
                           vs[0] + kcal - calc_cost(x_log[-1], xs[0], C))
    anti_clockwise = find_route(xs[:-1], vs[:-1], C, x_log + [xs[-1]],
                                vs[-1] + kcal - calc_cost(x_log[-1], xs[-1], C))

    # kcal_max = max([kcal_max, ])
    return max(clockwise, anti_clockwise)


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)

    # N, C = 3, 20
    # xs = [2, 9, 16]
    # vs = [80, 120, 1]

    best = find_route(xs, vs, C, [0], 0)
    # import operator
    # x_sorted = sorted(x_kcal_dict.items(), key=operator.itemgetter(1),
    #                   reverse=True)
    print(max(kcal_logs))


Fixed submission:
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)