Problem:
"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.

Incorrect submission (verdict: Wrong Answer):
n, c = map(int, input().split())
x = []
v = []
vcum = []
cum = 0
for i in range(n):
    X, V = map(int, input().split())
    x.append(X)
    cum += V
    v.append(V)
    vcum.append(cum)

maxnut = 0

for i in range(n):
    maxnut = max(maxnut, vcum[i] - x[i])

for i in range(n):
    if i == 0:
        maxnut = max(maxnut, vcum[n-1] - (c - x[i]))
    else:
        maxnut = max(maxnut, vcum[n-1] - vcum[i-1] - (c - x[i]))

for i in range(n):
    for j in range(i+1, n):
        maxnut = max(maxnut, vcum[i] - 2 * x[i] + vcum[n-1] - vcum[j-1],  - (c - x[j]))

for i in range(n):
    for j in range(i+1, n):
        maxnut = max(maxnut, vcum[i] - x[i] + vcum[n-1] - vcum[j-1],  - (c - x[j]) * 2)

print(maxnut)

Fixed, accepted submission:
import sys
input = sys.stdin.readline

n, c = map(int, input().split())
sushi = [list(map(int, input().split())) for _ in range(n)]

if n == 1:
    print(max(sushi[0][1]-sushi[0][0], sushi[0][1]-(c-sushi[0][0]), 0))
    exit()

x = []
for i in range(n):
    x.append(sushi[i][0])

vcum = []
v = 0
for i in range(n):
    v += sushi[i][1]
    vcum.append(v)

ans = 0


for i in range(n):
    if ans < vcum[i] - x[i]:
        ans = vcum[i] - x[i]



for i in range(n):
    if i == 0:
        if ans < vcum[n-1] - (c - x[0]):
            ans = vcum[n-1] - (c - x[0])
    else:
        if ans < (vcum[n - 1] - vcum[i - 1]) - (c - x[i]):
            ans = (vcum[n - 1] - vcum[i - 1]) - (c - x[i])




right_dp = [0] * n
right_dp[n-1] = vcum[n-1] - vcum[n-2] - (c - x[n-1])
for i in reversed(range(1, n-1)):
    right_dp[i] = max(right_dp[i+1], vcum[n-1] - vcum[i-1] - (c - x[i]))


for i in range(n-1):
    cal = vcum[i] - 2 * x[i] + right_dp[i+1]
    if ans < cal:
        ans = cal



left_dp = [0] * n
left_dp[0] = vcum[0] - x[0]
for i in range(1, n):
    left_dp[i] = max(left_dp[i-1], vcum[i] - x[i])


for i in range(1, n):
    cal = vcum[n-1] - vcum[i-1] - 2 * (c - x[i]) + left_dp[i-1]
    if ans < cal:
        ans = cal


print(ans)