Buggy python submission (verdict: Time Limit Exceeded):
N, C = [int(i) for i in input().split(" ")]
x = [0]
v = [0]
for i in range(N):
    t = [int(i) for i in input().split(" ")]
    x.append(t[0])
    v.append(t[1])
x.append(C)
v.append(0)
    

right_cal = [0 for i in range(N + 2)]
for i in range(1, N + 1):
    right_cal[i] = (v[i] + right_cal[i - 1] - x[i] + x[i - 1])
    
left_cal = [0 for i in range(N + 2)]
for i in range(N, 0, -1):
    left_cal[i] = (v[i] + left_cal[i + 1] - x[i + 1] + x[i])

ans = 0
for i in range(N + 1):
    ans = max(ans, right_cal[i] + max(left_cal[i + 1 : ]) - x[i])
    
ans = max(ans, left_cal[0] + max(right_cal))
for i in range(1, N + 1):
    ans = max(ans, left_cal[i] + max(right_cal[ : i]) - C + x[i])
    
# print(right_cal)
# print(left_cal)
# print(max_cal)
print(ans)

Fixed submission:
x = [0 for i in range(100001)]
v = [0 for i in range(100001)]
v_sum_right = [0 for i in range(100001)]
v_sum_left = [0 for i in range(100001)]
g = [[0, 0] for i in range(100001)]
g2= [0 for i in range(100001)]
N = 0
C = 0
i = 0
ans = 0
t = 0

N, C = [int(i) for i in input().split(" ")]
for i in range(N):
    x[i], v[i] = [int(i) for i in input().split(" ")]


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

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


# // g計算
g[0][0] = 0
g[0][1] = 0
g2[0] = 0
for i in range(1, N + 1):
    g[i][0] = max(g[i - 1][0], v_sum_right[i - 1] - x[i - 1])
    g2[i] = max(g2[i - 1], v_sum_right[i - 1] - 2 * x[i - 1])
    if g[i - 1][0] == g[i][0]:
        g[i][1] = g[i - 1][1]
    else:
        g[i][1] = x[i - 1]
    
# // 探査
ans = v_sum_left[0] - C + x[0]
for i in range(1, N):
    ans = max(ans, v_sum_left[i] + g[i][0] - 2 * (C +- x[i]), v_sum_left[i] - C + x[i] + g2[i])
ans = max(ans, g[N][0])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, C = [int(i) for i in input().split(" ")]
x = [0]
v = [0]
for i in range(N):
    t = [int(i) for i in input().split(" ")]
    x.append(t[0])
    v.append(t[1])
x.append(C)
v.append(0)
    

right_cal = [0 for i in range(N + 2)]
for i in range(1, N + 1):
    right_cal[i] = (v[i] + right_cal[i - 1] - x[i] + x[i - 1])
    
left_cal = [0 for i in range(N + 2)]
for i in range(N, 0, -1):
    left_cal[i] = (v[i] + left_cal[i + 1] - x[i + 1] + x[i])

ans = 0
max_cal = []
for i in range(N + 1):
    max_cal.append(max(left_cal[i + 1 : ]) - x[i])
for i in range(N + 1):
    ans = max(ans, right_cal[i] + max_cal[i])
    
max_cal = [max(right_cal)]
for i in range(1, N + 1):
    max_cal.append(max(right_cal[ : i]) - C + x[i])
for i in range(N + 1):
    ans = max(ans, left_cal[i] + max_cal[i])
    
# print(right_cal)
# print(left_cal)
# print(max_cal)
print(ans)

Fixed submission:
x = [0 for i in range(100001)]
v = [0 for i in range(100001)]
v_sum_right = [0 for i in range(100001)]
v_sum_left = [0 for i in range(100001)]
g = [[0, 0] for i in range(100001)]
g2= [0 for i in range(100001)]
N = 0
C = 0
i = 0
ans = 0
t = 0

N, C = [int(i) for i in input().split(" ")]
for i in range(N):
    x[i], v[i] = [int(i) for i in input().split(" ")]


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

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


# // g計算
g[0][0] = 0
g[0][1] = 0
g2[0] = 0
for i in range(1, N + 1):
    g[i][0] = max(g[i - 1][0], v_sum_right[i - 1] - x[i - 1])
    g2[i] = max(g2[i - 1], v_sum_right[i - 1] - 2 * x[i - 1])
    if g[i - 1][0] == g[i][0]:
        g[i][1] = g[i - 1][1]
    else:
        g[i][1] = x[i - 1]
    
# // 探査
ans = v_sum_left[0] - C + x[0]
for i in range(1, N):
    ans = max(ans, v_sum_left[i] + g[i][0] - 2 * (C +- x[i]), v_sum_left[i] - C + x[i] + g2[i])
ans = max(ans, g[N][0])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, C = [int(i) for i in input().split(" ")]
x = [0]
v = [0]
for i in range(N):
    t = [int(i) for i in input().split(" ")]
    x.append(t[0])
    v.append(t[1])
x.append(C)
v.append(0)
    

right_cal = [0 for i in range(N + 2)]
for i in range(1, N + 1):
    right_cal[i] = (v[i] + right_cal[i - 1] - x[i] + x[i - 1])
    
left_cal = [0 for i in range(N + 2)]
for i in range(N, 0, -1):
    left_cal[i] = (v[i] + left_cal[i + 1] - x[i + 1] + x[i])

ans = max(max(right_cal), max(left_cal))
for i in range(N + 2):
    for j in range(N + 2):
        if i >= j:
            continue
        ans = max(ans, right_cal[i] + left_cal[j] - x[i], right_cal[i] + left_cal[j] - C + x[j])
    
# print(right_cal)
# print(left_cal)
print(ans)

Fixed submission:
x = [0 for i in range(100001)]
v = [0 for i in range(100001)]
v_sum_right = [0 for i in range(100001)]
v_sum_left = [0 for i in range(100001)]
g = [[0, 0] for i in range(100001)]
g2= [0 for i in range(100001)]
N = 0
C = 0
i = 0
ans = 0
t = 0

N, C = [int(i) for i in input().split(" ")]
for i in range(N):
    x[i], v[i] = [int(i) for i in input().split(" ")]


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

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


# // g計算
g[0][0] = 0
g[0][1] = 0
g2[0] = 0
for i in range(1, N + 1):
    g[i][0] = max(g[i - 1][0], v_sum_right[i - 1] - x[i - 1])
    g2[i] = max(g2[i - 1], v_sum_right[i - 1] - 2 * x[i - 1])
    if g[i - 1][0] == g[i][0]:
        g[i][1] = g[i - 1][1]
    else:
        g[i][1] = x[i - 1]
    
# // 探査
ans = v_sum_left[0] - C + x[0]
for i in range(1, N):
    ans = max(ans, v_sum_left[i] + g[i][0] - 2 * (C +- x[i]), v_sum_left[i] - C + x[i] + g2[i])
ans = max(ans, g[N][0])

print(ans)