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


ans = 0
if N == 1:
    ans = max(X[0], L-X[0])
else:
    X += X
    #print('X: ', X)
    dmax = 0
    for i in range(1, N):
        seg = X[i:i+N]
        #print('seg: ', seg)
        for s in (0, N-1):
            os = (N-1)-s
            cl = N-1    # counterclockwise most
            cr = 0      # clockwise most
            d = 0
            lflg = False
            if s == cl:
                lflg = True
            while True:
                #print('tmpd: ', d)
                # l to l
                if lflg and seg[s] >= seg[os]:
                    d += seg[s]
                    break
                # r to r
                elif not(lflg) and seg[s] <= seg[os]:
                    d += L-seg[s]
                    break
                
                # l to r
                if lflg:
                    d += seg[s]+(L-seg[os])
                    # update segment
                    cl = s-1 if s-1>=0 else N-1 # move to r by 1
                    cr = os
                    # next
                    s = cr
                    os = cl
                # r to l
                else:
                    d += seg[os]+(L-seg[s])
                    # update segment
                    cl = os
                    cr = s+1 if s+1 < N else 0  # move to l by 1
                    # next
                    s = cl
                    os = cr
                lflg = not(lflg)
            #print('d: ', d)
            if d > dmax:
                dmax = d
    ans = dmax

print(ans)


Fixed submission:
(L, N) = map(int, input().split())
X = [0 for i in range(N)]
sumld = [0 for i in range(N)]
sumrd = [0 for i in range(N)]
for i in range(N):
    X[i] = int(input())
    sumld[i] = X[i]*2 if i == 0 else sumld[i-1] + X[i]*2
for i in range(N):
    sumrd[N-1-i] = (L-X[N-1-i])*2 if i == 0 else sumrd[N-1-i+1] + (L-X[N-1-i])*2
#print('X: ', X)
#print('sumld: ', sumld)
#print('sumrd: ', sumrd)

ans = 0
if N == 1:
    ans = max(X[0], L-X[0])
else:
    dmax = 0
    for i in range(1, N):
        l = (N-1+i)%N
        r = i
        lnum = l+1
        rnum = N-lnum
        #print('l: ', l)
        #print('r: ', r)
        #print('lnum: ', lnum)
        #print('rnum: ', rnum)

        # start from l
        d = 0
        # end at l
        if lnum > rnum:
            d = sumld[l] + sumrd[r] - X[l]
            if lnum-rnum > 1:
                d -= sumld[l-rnum-1]
        # end at r
        else:
            d = sumld[l] + sumrd[r] - X[l]
            if rnum-lnum > 0:
                d -= sumrd[r+lnum]
        #print('d from l: ', d)
        if d > dmax:
            dmax = d

        # start from r
        d = 0
        # end at l
        if lnum >= rnum:
            d = sumld[l] + sumrd[r] - (L-X[r])
            if lnum-rnum > 0:
                d -= sumld[l-rnum]
        # end at r
        else:
            d = sumld[l] + sumrd[r] - (L-X[r])
            if rnum-lnum > 1:
                d -= sumrd[r+lnum+1]
        #print('d from r: ', d)
        if d > dmax:
            dmax = d
    ans = dmax

print(ans)