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

def f(L, N, X, p, l, r, d_l):
    w = 0
    for _ in range(N - (l + r)):
        if d_l:  # L
            l += 1
            w += X[l] - p
            p = X[l]
            d_l = False
        else:  # R
            r += 1
            w += L - X[-r] + p
            p = X[-r] - L
            d_l = True
    return w

X_c = [L - i for i in X]

max_l = max(X[i] + f(L, N, X, X[i], i, 0, False) for i in range(1, N+1))
max_r = max(X_c[-i] + f(L, N, X, -X_c[-i], 0, i, True) for i in range(1, N+1))
print(max(max_l, max_r))


Fixed submission:
import math

L, N = (int(i) for i in input().split())
X = [int(input()) for _ in range(N)]
X.insert(0, 0)

X_r = [L - i for i in reversed(X)]
X_r.insert(0, 0)
X_r.pop()

def lsum(l):
    s = 0
    for i in l:
        s += i
        yield s

X_sum = list(lsum(X))
X_r_sum = list(lsum(X_r))

def f_l(p, n):
    N_tmp = N - n
    a = N_tmp // 2
    if N_tmp == 0:
        return 0
    elif N_tmp % 2 == 0:
        return 2 * X_sum[a] + X_r_sum[a + n] + X_r_sum[a + n - 1] - 2 * X_r_sum[n] + p
    else:
        return X_sum[a+1] + X_sum[a] + 2 * X_r_sum[a + n] - 2 * X_r_sum[n] + p


def f_r(p, n):
    N_tmp = N - n
    a = N_tmp // 2
    if N_tmp == 0:
        return 0
    elif N_tmp % 2 == 0:
        return 2 * X_r_sum[a] + X_sum[a + n] + X_sum[a + n - 1] - 2 * X_sum[n] + p
    else:
        return X_r_sum[a+1] + X_r_sum[a] + 2 * X_sum[a + n] - 2 * X_sum[n] + p

max_l = max(X[i] + f_r(X[i], i) for i in range(1, N+1))
max_r = max(X_r[i] + f_l(X_r[i], i) for i in range(1, N+1))

print(max(max_l, max_r))

Buggy python submission (verdict: Runtime Error):
L, N = (int(i) for i in input().split())
X = [int(input()) for _ in range(N)]
X.insert(0, 0)

X_c = [L - i for i in X]

def f(L, N, X, p, l, r, d_r):
    w = 0
    for _ in range(N - (l + r)):
        if d_r:  # L
            l += 1
            w += X[l] - p
            p = X[l]
            d_r = False
        else:  # R
            r += 1
            w += L - X[-r] + p
            p = X[-r] - L
            d_r = True
    return w

len_l = [X[i] + f(L, N, X, X[i], i, 0, True) for i in range(1, N)]
len_r = [X_c[-i] + f(L, N, X, X[-i] - L, 0, i, False) for i in range(1, N)]
max_l = max(len_l)
max_r = max(len_r)
print(max(max_l, max_r))


Fixed submission:
import math

L, N = (int(i) for i in input().split())
X = [int(input()) for _ in range(N)]
X.insert(0, 0)

X_r = [L - i for i in reversed(X)]
X_r.insert(0, 0)
X_r.pop()

def lsum(l):
    s = 0
    for i in l:
        s += i
        yield s

X_sum = list(lsum(X))
X_r_sum = list(lsum(X_r))

def f_l(p, n):
    N_tmp = N - n
    a = N_tmp // 2
    if N_tmp == 0:
        return 0
    elif N_tmp % 2 == 0:
        return 2 * X_sum[a] + X_r_sum[a + n] + X_r_sum[a + n - 1] - 2 * X_r_sum[n] + p
    else:
        return X_sum[a+1] + X_sum[a] + 2 * X_r_sum[a + n] - 2 * X_r_sum[n] + p


def f_r(p, n):
    N_tmp = N - n
    a = N_tmp // 2
    if N_tmp == 0:
        return 0
    elif N_tmp % 2 == 0:
        return 2 * X_r_sum[a] + X_sum[a + n] + X_sum[a + n - 1] - 2 * X_sum[n] + p
    else:
        return X_r_sum[a+1] + X_r_sum[a] + 2 * X_sum[a + n] - 2 * X_sum[n] + p

max_l = max(X[i] + f_r(X[i], i) for i in range(1, N+1))
max_r = max(X_r[i] + f_l(X_r[i], i) for i in range(1, N+1))

print(max(max_l, max_r))

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

d = {("S", 0, 0, 0): 0}
for _ in range(N):
    d_ = {}
    for (root, p, l, r), w in d.items():
        d_[(root+"L", X[l+1], l+1, r)] = w + X[l+1] - p
        d_[(root+"R", X[-(r+1)] - L, l, r+1)] = w + (L - X[-(r+1)]) + p
    max_d = max(d_.items(), key=lambda x: x[1])[1]
    d = {k: v for k, v in d_.items() if max_d >= v}
#     print(d)
# print(max(d.items(), key=lambda x: x[1]))
print(max(d.items(), key=lambda x: x[1])[1])


Fixed submission:
import math

L, N = (int(i) for i in input().split())
X = [int(input()) for _ in range(N)]
X.insert(0, 0)

X_r = [L - i for i in reversed(X)]
X_r.insert(0, 0)
X_r.pop()

def lsum(l):
    s = 0
    for i in l:
        s += i
        yield s

X_sum = list(lsum(X))
X_r_sum = list(lsum(X_r))

def f_l(p, n):
    N_tmp = N - n
    a = N_tmp // 2
    if N_tmp == 0:
        return 0
    elif N_tmp % 2 == 0:
        return 2 * X_sum[a] + X_r_sum[a + n] + X_r_sum[a + n - 1] - 2 * X_r_sum[n] + p
    else:
        return X_sum[a+1] + X_sum[a] + 2 * X_r_sum[a + n] - 2 * X_r_sum[n] + p


def f_r(p, n):
    N_tmp = N - n
    a = N_tmp // 2
    if N_tmp == 0:
        return 0
    elif N_tmp % 2 == 0:
        return 2 * X_r_sum[a] + X_sum[a + n] + X_sum[a + n - 1] - 2 * X_sum[n] + p
    else:
        return X_r_sum[a+1] + X_r_sum[a] + 2 * X_sum[a + n] - 2 * X_sum[n] + p

max_l = max(X[i] + f_r(X[i], i) for i in range(1, N+1))
max_r = max(X_r[i] + f_l(X_r[i], i) for i in range(1, N+1))

print(max(max_l, max_r))

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

d = {("S", 0, 0, 0, 0): 0}
for _ in range(N):
    d_ = {}
    for (root, p, l, r, f), w in d.items():
        if root[-1] == "R":
            if f == 0:
                d_[(root+"R", X[-(r+1)] - L, l, r+1, 1)] = w + (L - X[-(r+1)]) + p
            d_[(root+"L", X[l+1], l+1, r, 0)] = w + X[l+1] - p
        elif root[-1] == "L":
            if f == 0:
                d_[(root+"L", X[l+1], l+1, r, 1)] = w + X[l+1] - p
            d_[(root+"R", X[-(r+1)] - L, l, r+1, 0)] = w + (L - X[-(r+1)]) + p
        else:
            d_[(root+"L", X[l+1], l+1, r, 0)] = w + X[l+1] - p
            d_[(root+"R", X[-(r+1)] - L, l, r+1, 0)] = w + (L - X[-(r+1)]) + p

    d = d_
#     print(d)
# print(max(d.items(), key=lambda x: x[1]))
print(max(d.items(), key=lambda x: x[1])[1])


Fixed submission:
import math

L, N = (int(i) for i in input().split())
X = [int(input()) for _ in range(N)]
X.insert(0, 0)

X_r = [L - i for i in reversed(X)]
X_r.insert(0, 0)
X_r.pop()

def lsum(l):
    s = 0
    for i in l:
        s += i
        yield s

X_sum = list(lsum(X))
X_r_sum = list(lsum(X_r))

def f_l(p, n):
    N_tmp = N - n
    a = N_tmp // 2
    if N_tmp == 0:
        return 0
    elif N_tmp % 2 == 0:
        return 2 * X_sum[a] + X_r_sum[a + n] + X_r_sum[a + n - 1] - 2 * X_r_sum[n] + p
    else:
        return X_sum[a+1] + X_sum[a] + 2 * X_r_sum[a + n] - 2 * X_r_sum[n] + p


def f_r(p, n):
    N_tmp = N - n
    a = N_tmp // 2
    if N_tmp == 0:
        return 0
    elif N_tmp % 2 == 0:
        return 2 * X_r_sum[a] + X_sum[a + n] + X_sum[a + n - 1] - 2 * X_sum[n] + p
    else:
        return X_r_sum[a+1] + X_r_sum[a] + 2 * X_sum[a + n] - 2 * X_sum[n] + p

max_l = max(X[i] + f_r(X[i], i) for i in range(1, N+1))
max_r = max(X_r[i] + f_l(X_r[i], i) for i in range(1, N+1))

print(max(max_l, max_r))

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

d = {("", 0, 0, 0): 0}
for _ in range(N):
    d_ = {}
    for (root, p, l, r), w in d.items():
        d_[(root+"R", X[l+1], l+1, r)] = w + X[l+1] - p
        d_[(root+"L", X[-(r+1)] - L, l, r+1)] = w + (L - X[-(r+1)]) + p
    d = d_
    # print(d)
print(max(d.items(), key=lambda x: x[1])[1])


Fixed submission:
import math

L, N = (int(i) for i in input().split())
X = [int(input()) for _ in range(N)]
X.insert(0, 0)

X_r = [L - i for i in reversed(X)]
X_r.insert(0, 0)
X_r.pop()

def lsum(l):
    s = 0
    for i in l:
        s += i
        yield s

X_sum = list(lsum(X))
X_r_sum = list(lsum(X_r))

def f_l(p, n):
    N_tmp = N - n
    a = N_tmp // 2
    if N_tmp == 0:
        return 0
    elif N_tmp % 2 == 0:
        return 2 * X_sum[a] + X_r_sum[a + n] + X_r_sum[a + n - 1] - 2 * X_r_sum[n] + p
    else:
        return X_sum[a+1] + X_sum[a] + 2 * X_r_sum[a + n] - 2 * X_r_sum[n] + p


def f_r(p, n):
    N_tmp = N - n
    a = N_tmp // 2
    if N_tmp == 0:
        return 0
    elif N_tmp % 2 == 0:
        return 2 * X_r_sum[a] + X_sum[a + n] + X_sum[a + n - 1] - 2 * X_sum[n] + p
    else:
        return X_r_sum[a+1] + X_r_sum[a] + 2 * X_sum[a + n] - 2 * X_sum[n] + p

max_l = max(X[i] + f_r(X[i], i) for i in range(1, N+1))
max_r = max(X_r[i] + f_l(X_r[i], i) for i in range(1, N+1))

print(max(max_l, max_r))