Problem:
Score : 800 points

Problem Statement

Takahashi Lake has a perimeter of L. On the circumference of the lake, there is a residence of the lake's owner, Takahashi.
Each point on the circumference of the lake has a coordinate between 0 and L (including 0 but not L), which is the distance from the Takahashi's residence, measured counter-clockwise.

There are N trees around the lake; the coordinate of the i-th tree is X_i. There is no tree at coordinate 0, the location of Takahashi's residence.

Starting at his residence, Takahashi will repeat the following action:

If all trees are burnt, terminate the process.

Specify a direction: clockwise or counter-clockwise.

Walk around the lake in the specified direction, until the coordinate of a tree that is not yet burnt is reached for the first time.

When the coordinate with the tree is reached, burn that tree, stay at the position and go back to the first step.

Find the longest possible total distance Takahashi walks during the process.

Partial Score

A partial score can be obtained in this problem:

300 points will be awarded for passing the input satisfying N \leq 2000.

Constraints

2 \leq L \leq 10^9

1 \leq N \leq 2\times 10^5

1 \leq X_1 < ... < X_N \leq L-1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

L N
X_1
:
X_N

Output

Print the longest possible total distance Takahashi walks during the process.

Sample Input 1

10 3
2
7
9

Sample Output 1

15

Takahashi walks the distance of 15 if the process goes as follows:

Walk a distance of 2 counter-clockwise, burn the tree at the coordinate 2 and stay there.

Walk a distance of 5 counter-clockwise, burn the tree at the coordinate 7 and stay there.

Walk a distance of 8 clockwise, burn the tree at the coordinate 9 and stay there.

Sample Input 2

10 6
1
2
3
6
7
9

Sample Output 2

27

Sample Input 3

314159265 7
21662711
77271666
89022761
156626166
160332356
166902656
298992265

Sample Output 3

1204124749

Solution in Rust (1.15.1) (judge verdict: Time Limit Exceeded)

use std::cmp::*;
use std::collections::*;
use std::io::*;
use std::ops::*;
use std::str::FromStr;
use utils::*;

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(i.lock(), &mut o);
    stdout().write_all(&o).unwrap();
}

fn run<R: BufRead, W: Write>(i: R, o: &mut W) {
    let mut i = CpReader::new(i);
    let (l, n) = i.read2::<isize, usize>();
    let mut x = Vec::new();
    for _ in 0..n {
        x.push(i.read::<isize>());
    }

    writeln!(o, "{}", solve(l, &x)).unwrap();
}
fn solve(l: isize, x: &[isize]) -> isize {
    fn get(l: isize, c: isize, x: &[isize]) -> isize {
        if x.len() == 0 {
            return 0;
        }
        let x0 = x[0];
        let x1 = x[x.len() - 1] - l;

        let d0 = x0 - c;
        let d1 = -(x1 - c);
        max(get(l, x0, &x[1..]) + d0, get(l, x1, &x[..x.len() - 1]) + d1)
    }
    get(l, 0, x)
}

/*
fn solve(l: isize, x: &[isize]) -> isize {
    let inf = isize::min_value();

    // r0[n].0 : 順方向にn本、逆方向にi-n本焼き、さらに順方向に焼いた場合の距離
    // r0[n].1 : 順方向にn本、逆方向にi-n本焼き、さらに逆方向に焼いた場合の距離
    let mut r0 = vec![(inf, inf); x.len()];
    let mut r1 = vec![(inf, inf); x.len()];
    r0[0].0 = x[0];
    r0[0].1 = l - x[x.len() - 1];

    // i : 燃やした木の本数
    for i in 1..x.len() {
        // 順方向に燃やした木の本数
        for j in 0..i {
            let b = j + 1;
            let b0 = j;
            let b1 = x.len() - (i - j);
            // 順方向に燃やす
            r1[j + 1].0 = max(
                r0[j].0 + x[b] - x[b0],           // 順→順
                r0[j + 1].1 + x[b] + (l - x[b1]), // 逆→順
            );

            let b = x.len() - (i - j);
            let b0 = j;
            let b1 = x.len() - (i - j) - 1;

            // 逆方向に燃やす
            r1[j].0 = max(
                r0[j + 1].0 + (l - x[b]) + x[b0], // 順→逆
                r0[j].1 + x[b] - x[b1],           // 逆→逆
            );
        }
        println!("r0 = {:?}", &r0[0..i as usize + 1]);
        std::mem::swap(&mut r0, &mut r1);
    }
    let mut result = isize::min_value();
    for i in r0 {
        result = max(result, i.0);
        result = max(result, i.1);
    }
    result
}*/

mod utils {
    use super::*;

    pub struct CpReader<R: BufRead> {
        r: R,
        s: String,
    }

    macro_rules! fn_read {
        { $f:ident($($t:ident),*) } => {
            #[allow(dead_code)]
            pub fn $f<$($t: FromStr),*>(&mut self) -> ($($t),*) {
                let i = &mut self.read_line().split(' ');
                ($(read_next::<$t,_>(i)),*)
            }
        };
    }

    impl<R: BufRead> CpReader<R> {
        pub fn new(r: R) -> Self {
            CpReader {
                r: r,
                s: String::new(),
            }
        }
        pub fn read_line(&mut self) -> &str {
            self.s.clear();
            self.r.read_line(&mut self.s).unwrap();
            self.s.trim()
        }

        pub fn read<T: FromStr>(&mut self) -> T {
            self.read_line().parse().ok().unwrap()
        }
        fn_read! { read2(T1, T2) }
        fn_read! { read3(T1, T2, T3) }
        fn_read! { read4(T1, T2, T3, T4) }
        fn_read! { read5(T1, T2, T3, T4, T5) }

        pub fn read_vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
            self.read_line()
                .split(' ')
                .take(n)
                .map(|x| x.parse().ok().unwrap())
                .collect()
        }
        pub fn read_array<A: AsMut<[T]> + Default, T: FromStr>(&mut self) -> A {
            let mut a: A = Default::default();
            {
                let a: &mut [T] = a.as_mut();
                let i = &mut self.read_line().split(' ');
                for n in 0..a.len() {
                    a[n] = read_next(i);
                }
            }
            a
        }
    }
    fn read_next<'a, T: FromStr, I: Iterator<Item = &'a str>>(i: &mut I) -> T {
        i.next().unwrap().parse().ok().unwrap()
    }

}