Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
L, N = map(int, input().split())
X = [0] + [int(input()) for _ in range(N)]
Y = [0] + [L - x for x in X[1:]][::-1]

dp = [[[0] * 2 for _ in range(N + 1)] for _ in range(N + 1)]
for i in range(N):
    for j in range(N):
        dp[i + 1][j][0] = max(
                dp[i][j][0] + Y[i + 1] - Y[i],
                dp[i][j][1] + X[j] + Y[i + 1])
        dp[i][j + 1][1] = max(
                dp[i][j][0] + X[j + 1] + Y[i],
                dp[i][j][1] + X[j + 1] - X[j])

ans = 0
for i in range(N + 1):
    ans = max(ans, dp[i][N - i][0], dp[i][N - i][1])

print(ans)


Fixed submission:
#!/usr/bin/env python3
from itertools import accumulate
L, N = map(int, input().split())
X = [int(input()) for _ in range(N)]
Y = [L - x for x in X][::-1]

sx = [0] + list(accumulate(X))
sy = [0] + list(accumulate(Y))

ans = 0
for i in range(N):
    j = N - i - 1
    mn = min(i, j)

    now = X[i]
    now += 2 * (sx[i] - sx[i - mn]) + 2 * (sy[j] - sy[j - mn - (i < j)])
    ans = max(ans, now)

    now = Y[j]
    now += 2 * (sx[i] - sx[i - mn - (i > j)]) + 2 * (sy[j] - sy[j - mn])
    ans = max(ans, now)

print(ans)

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
import numpy as np
L, N = map(int, input().split())
X = np.array([0] + [int(input()) for _ in range(N)])
Y = np.array([0] + [L - x for x in X[1:]][::-1])

if N > 2000:
    print(0)
dp = np.zeros((N + 1, N + 1, 2), dtype=np.int32)
for i in range(N):
    for j in range(N):
        dp[i + 1][j][0] = np.amax(np.array([
                dp[i][j][0] + Y[i + 1] - Y[i],
                dp[i][j][1] + X[j] + Y[i + 1]]))
        dp[i][j + 1][1] = np.amax(np.array([
                dp[i][j][0] + X[j + 1] + Y[i],
                dp[i][j][1] + X[j + 1] - X[j]]))

ans = 0
for i in range(N + 1):
    ans = max(ans, dp[i][N - i][0], dp[i][N - i][1])

print(ans)


Fixed submission:
#!/usr/bin/env python3
from itertools import accumulate
L, N = map(int, input().split())
X = [int(input()) for _ in range(N)]
Y = [L - x for x in X][::-1]

sx = [0] + list(accumulate(X))
sy = [0] + list(accumulate(Y))

ans = 0
for i in range(N):
    j = N - i - 1
    mn = min(i, j)

    now = X[i]
    now += 2 * (sx[i] - sx[i - mn]) + 2 * (sy[j] - sy[j - mn - (i < j)])
    ans = max(ans, now)

    now = Y[j]
    now += 2 * (sx[i] - sx[i - mn - (i > j)]) + 2 * (sy[j] - sy[j - mn])
    ans = max(ans, now)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
L, N = map(int, input().split())
X = [0] + [int(input()) for _ in range(N)]
Y = [0] + [L - x for x in X[1:]][::-1]

dp = [[[0] * 2 for _ in range(N + 1)] for _ in range(N + 1)]
for i in range(N):
    for j in range(N):
        dp[i + 1][j][0] = max(
                dp[i][j][0] + Y[i + 1] - Y[i],
                dp[i][j][1] + X[j] + Y[i + 1])
        dp[i][j + 1][1] = max(
                dp[i][j][0] + X[j + 1] + Y[i],
                dp[i][j][1] + X[j + 1] - X[j])

ans = 0
for i in range(N + 1):
    ans = max(ans, dp[i][N - i][0], dp[i][N - i][1])

print(ans)


Fixed submission:
#!/usr/bin/env python3
from itertools import accumulate
L, N = map(int, input().split())
X = [int(input()) for _ in range(N)]
Y = [L - x for x in X][::-1]

sx = [0] + list(accumulate(X))
sy = [0] + list(accumulate(Y))

ans = 0
for i in range(N):
    j = N - i - 1
    mn = min(i, j)

    now = X[i]
    now += 2 * (sx[i] - sx[i - mn]) + 2 * (sy[j] - sy[j - mn - (i < j)])
    ans = max(ans, now)

    now = Y[j]
    now += 2 * (sx[i] - sx[i - mn - (i > j)]) + 2 * (sy[j] - sy[j - mn])
    ans = max(ans, now)

print(ans)