Buggy python submission (verdict: Time Limit Exceeded):
N, C = map(int, input().split())
xs = []
vs = []
for i in range(N):
    x, v = map(int, input().split())
    xs += [x]
    vs += [v]

sumVtos = [vs[0]]
for i in range(1, N):
    sumVtos += [sumVtos[-1] + vs[i]]

sumV = sumVtos[-1]
sumVfrs = [sumV]
for i in range(1, N):
    sumVfrs += [sumVfrs[-1] - vs[i - 1]]

# 初手：時計回り
ans = 0
for i in range(N):
    ans1 = sumVtos[i] - xs[i]

    # 2手目：反時計回り
    ans2 = 0
    for j in range(i + 1, N):
        ans2 = max(ans2, sumVfrs[j] - (xs[i] + C - xs[j]))

    ans = max(ans, ans1 + ans2)

# 初手：反時計回り
for i in range(N):
    ans1 = sumVfrs[i] - (C - xs[i])

    # 2手目：時計回り
    ans2 = 0
    for j in range(i):
        ans2 = max(ans2, sumVtos[j] - (C - xs[i] + xs[j]))

    ans = max(ans, ans1 + ans2)

print(ans)


Fixed submission:
N, C = map(int, input().split())
xs = []
vs = []
for i in range(N):
    x, v = map(int, input().split())
    xs += [x]
    vs += [v]


fAs = [0] * N
fAs[0] = vs[0]
for A in range(1, N):
    fAs[A] = fAs[A - 1] + vs[A]
for A in range(N):
    fAs[A] -= xs[A]

gAs = [0] * N
gAs[0] = 0
for A in range(1, N):
    gAs[A] = max(gAs[A - 1], fAs[A - 1])


fBs = [0] * N
fBs[N - 1] = vs[N - 1]
for B in range(0, N - 1)[::-1]:
    fBs[B] = fBs[B + 1] + vs[B]

for B in range(N):
    fBs[B] -= C - xs[B]

gBs = [0] * N
gBs[N - 1] = 0
for B in range(0, N - 1)[::-1]:
    gBs[B] = max(gBs[B + 1], fBs[B + 1])


ans = 0

# 初手：時計回り
for A in range(N):
    ans1 = fAs[A]

    # 2手目：反時計回り
    ans2 = max(0, gBs[A] - xs[A])

    ans = max(ans, ans1 + ans2)


# 初手：反時計回り
for B in range(N):
    ans1 = fBs[B]

    # 2手目：時計回り
    ans2 = max(0, gAs[B] - (C - xs[B]))

    ans = max(ans, ans1 + ans2)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, C = map(int, input().split())
xs = []
vs = []
for i in range(N):
    x, v = map(int, input().split())
    xs += [x]
    vs += [v]

# 初手：時計回り
ans = 0
for i in range(N):
    ans1 = sum(vs[:i + 1]) - xs[i]

    # 2手目：反時計回り
    ans2 = 0
    for j in range(i + 1, N):
        ans2 = max(ans2, sum(vs[j:]) - (xs[i] + C - xs[j]))

    ans = max(ans, ans1 + ans2)

# 初手：反時計回り
for i in range(N):
    ans1 = sum(vs[i:]) - (C - xs[i])

    # 2手目：時計回り
    ans2 = 0
    for j in range(i):
        ans2 = max(ans2, sum(vs[:j + 1]) - (C - xs[i] + xs[j]))

    ans = max(ans, ans1 + ans2)

print(ans)


Fixed submission:
N, C = map(int, input().split())
xs = []
vs = []
for i in range(N):
    x, v = map(int, input().split())
    xs += [x]
    vs += [v]


fAs = [0] * N
fAs[0] = vs[0]
for A in range(1, N):
    fAs[A] = fAs[A - 1] + vs[A]
for A in range(N):
    fAs[A] -= xs[A]

gAs = [0] * N
gAs[0] = 0
for A in range(1, N):
    gAs[A] = max(gAs[A - 1], fAs[A - 1])


fBs = [0] * N
fBs[N - 1] = vs[N - 1]
for B in range(0, N - 1)[::-1]:
    fBs[B] = fBs[B + 1] + vs[B]

for B in range(N):
    fBs[B] -= C - xs[B]

gBs = [0] * N
gBs[N - 1] = 0
for B in range(0, N - 1)[::-1]:
    gBs[B] = max(gBs[B + 1], fBs[B + 1])


ans = 0

# 初手：時計回り
for A in range(N):
    ans1 = fAs[A]

    # 2手目：反時計回り
    ans2 = max(0, gBs[A] - xs[A])

    ans = max(ans, ans1 + ans2)


# 初手：反時計回り
for B in range(N):
    ans1 = fBs[B]

    # 2手目：時計回り
    ans2 = max(0, gAs[B] - (C - xs[B]))

    ans = max(ans, ans1 + ans2)

print(ans)