Buggy python submission (verdict: Runtime Error):
L, N = map(int, input().split())
if N > 2000:
    print(1/0)
X = [0, L]
for i in range(N):
    X.append(int(input()))

X = sorted(X)
dpl = [[0]*(N+1) for i in range(N+1)]
dpr = [[0]*(N+1) for i in range(N+1)]

for k in range(1, N+1):
    for l in range(k + 1):
        r = k - l
        if l > 0:
            dpl[l][r] = max(dpl[l-1][r] + X[l]-X[l-1], dpr[l-1][r] + X[l] + L - X[-r-1])
        if r > 0:
            dpr[l][r] = max(dpr[l][r-1] + X[-r-1]-X[-r], dpl[l][r-1] + X[l] + L - X[-r-1])
        
ma = 0
for l in range(N+1):
    ma = max(ma, dpl[l][N-l], dpr[l][N-l])

print(ma)


Fixed submission:
L, N = map(int, input().split())
X = []
for i in range(N):
    X.append(int(input()))

X = sorted(X)

ma = 0
for k in range(2):
    if k == 1:
        X = [L-X[-i] for i in range(1, N+1)]
    s = X[0]
    for i in range(1, N):
        if i % 2 == 1:
            s += L - X[-(i//2)-1] + X[i//2]
        else:
            s += X[i//2] + (L - X[-(i//2)])

    ma = max(ma, s)
    lt = (N-1) // 2 if N % 2 == 1 else -N//2
    for i in range(1, N):
        if lt > 0:
            if N % 2 == 1:
                s += X[lt] - X[i-1]*2 - (L - X[lt + 1])
            else:
                s += X[lt] - X[i-1] * 2 - (L - X[lt + 1])
            lt = 1 + lt - N
            ma = max(ma, s)
        else:
            if N % 2 == 1:
                s += X[lt] - X[i-1] * 2 - (L - X[lt])
            else:
                s += X[lt]*2 - L - X[i-1] * 2
            lt = N + lt
            ma = max(ma, s)

print(ma)

Buggy python submission (verdict: Runtime Error):
L, N = map(int, input().split())
if N > 2000:
    print(1/0)
X = [0, L]
for i in range(N):
    X.append(int(input()))

X = sorted(X)
dpl = [[0]*(N+1) for i in range(N+1)]
dpr = [[0]*(N+1) for i in range(N+1)]

for k in range(1, N+1):
    for l in range(k + 1):
        r = k - l
        if l > 0:
            dpl[l][r] = max(dpl[l-1][r] + X[l]-X[l-1], dpr[l-1][r] + X[l] + L - X[-r-1])
        if r > 0:
            dpr[l][r] = max(dpr[l][r-1] + X[-r-1]-X[-r], dpl[l][r-1] + X[l] + L - X[-r-1])
        
ma = 0
for l in range(N+1):
    ma = max(ma, dpl[l][N-l], dpr[l][N-l])

print(ma)


Fixed submission:
L, N = map(int, input().split())
X = []
for i in range(N):
    X.append(int(input()))

X = sorted(X)

ma = 0
for k in range(2):
    if k == 1:
        X = [L-X[-i] for i in range(1, N+1)]
    s = X[0]
    for i in range(1, N):
        if i % 2 == 1:
            s += L - X[-(i//2)-1] + X[i//2]
        else:
            s += X[i//2] + (L - X[-(i//2)])

    ma = max(ma, s)
    lt = (N-1) // 2 if N % 2 == 1 else -N//2
    for i in range(1, N):
        if lt > 0:
            if N % 2 == 1:
                s += X[lt] - X[i-1]*2 - (L - X[lt + 1])
            else:
                s += X[lt] - X[i-1] * 2 - (L - X[lt + 1])
            lt = 1 + lt - N
            ma = max(ma, s)
        else:
            if N % 2 == 1:
                s += X[lt] - X[i-1] * 2 - (L - X[lt])
            else:
                s += X[lt]*2 - L - X[i-1] * 2
            lt = N + lt
            ma = max(ma, s)

print(ma)

Buggy python submission (verdict: Time Limit Exceeded):
L, N = map(int, input().split())
X = [0, L]
for i in range(N):
    X.append(int(input()))

X = sorted(X)
dpl = [[0]*(N+1) for i in range(N+1)]
dpr = [[0]*(N+1) for i in range(N+1)]

for k in range(1, N+1):
    for l in range(k + 1):
        r = k - l
        if l > 0:
            dpl[l][r] = max(dpl[l-1][r] + X[l]-X[l-1], dpr[l-1][r] + X[l] + L - X[-r-1])
        if r > 0:
            dpr[l][r] = max(dpr[l][r-1] + X[-r-1]-X[-r], dpl[l][r-1] + X[l] + L - X[-r-1])
        
ma = 0
for l in range(N+1):
    ma = max(ma, dpl[l][N-l], dpr[l][N-l])

print(ma)

Fixed submission:
L, N = map(int, input().split())
X = []
for i in range(N):
    X.append(int(input()))

X = sorted(X)

ma = 0
for k in range(2):
    if k == 1:
        X = [L-X[-i] for i in range(1, N+1)]
    s = X[0]
    for i in range(1, N):
        if i % 2 == 1:
            s += L - X[-(i//2)-1] + X[i//2]
        else:
            s += X[i//2] + (L - X[-(i//2)])

    ma = max(ma, s)
    lt = (N-1) // 2 if N % 2 == 1 else -N//2
    for i in range(1, N):
        if lt > 0:
            if N % 2 == 1:
                s += X[lt] - X[i-1]*2 - (L - X[lt + 1])
            else:
                s += X[lt] - X[i-1] * 2 - (L - X[lt + 1])
            lt = 1 + lt - N
            ma = max(ma, s)
        else:
            if N % 2 == 1:
                s += X[lt] - X[i-1] * 2 - (L - X[lt])
            else:
                s += X[lt]*2 - L - X[i-1] * 2
            lt = N + lt
            ma = max(ma, s)

print(ma)