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::io::*;
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 {
    let inf = isize::min_value();

    let count = x.len();

    let mut x = x.to_vec();
    x.insert(0, 0);
    x.insert(0, 0);
    x.push(l);
    x.push(l);

    // r[n] 順方向にn本、逆方向にi-n本焼いた場合の総移動距離
    // r[n].0 : 最後に順方向に焼いた場合
    // r[n].1 : 最後に逆方向に焼いた場合
    let mut r = vec![(inf, inf); x.len() + 1];
    r[1] = (0, 0);

    // i : 燃した木の本数（スコープの後の状態）
    for i in 1..count + 1 {
        // 逆方向に燃やした木の本数（スコープの後の状態）
        for j in (0..i + 1).rev() {
            let b = j + 1;
            let b0 = i - j + 1; // 最後に順方向に燃やした木のインデックス（スコープ後の状態）
            let b1 = count - j + 2; // 最後に逆方向に燃やした木のインデックス（スコープ後の状態）

            // 順方向に燃やす
            r[b].0 = max(
                r[b].0 + x[b0] - x[b0 - 1],   // 順→順
                r[b].1 + x[b0] + (l - x[b1]), // 逆→順
            );

            // 逆方向に燃やす
            r[b].1 = max(
                r[b - 1].0 + (l - x[b1]) + x[b0],           // 順→逆
                r[b - 1].1 + (l - x[b1]) - (l - x[b1 + 1]), // 逆→逆
            );
        }
    }
    let mut m = isize::min_value();
    for i in r {
        m = max(m, i.0);
        m = max(m, i.1);
    }
    m
}

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) }
    }
    fn read_next<'a, T: FromStr, I: Iterator<Item = &'a str>>(i: &mut I) -> T {
        i.next().unwrap().parse().ok().unwrap()
    }

}