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: Wrong Answer)

fn read_vec<T>() -> Vec<T>
where T: std::str::FromStr,
{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().split_whitespace().map(
        |e| e.parse().ok().unwrap()
    ).collect()

}

fn scramble_vec(trees: &mut Vec<u32>, pos :u32)
{
    let ps = pos as usize;
    let mut i = trees.len() - ps;
    let n_1 = trees.len() - 1;
    while i < n_1 {
        let k = i+1;
        trees.swap(i, k);
        i += 2;
    }
    //println!("{:?}", trees);
}

// anti-clockwise
//const K = 2;
struct Sub {
    l :u32,
    n :u32,
    st: Vec<u32>,
    //memo: Vec<u32>,
}
impl Sub {

fn burning_tree(&self, trees :&Vec<u32>, is_cclock :bool, sp :u32) -> u64
{
    let mut cur = if {is_cclock} {0} else {self.l};
    let mut ans :u64 = 0;
    let mut tr_ac: Vec<_> = trees.iter().cloned().collect();
    let mut tr_st: Vec<_> = self.st.iter().cloned().collect();
    //tr_st.sort_unstable();
    //println!("{:?}", trees);
    for ni in 0..self.n {
        let next = tr_ac[0];
        let head = tr_st[0];
        let tail = match tr_st.last() {Some(v) =>{*v}, None => {0}, };
        let mut m = 0;
        let mut d_anti = m;
        //let mut d_cloc = m;
        //println!("tr_ac: {:?}", tr_ac);
        //println!("(C, H, T) = ({}, {}, {})", cur, head, tail);
        if ni >= sp {
            //println!("trace1 : {} -> {}", cur, next);
            //let d_anti = if {next == head} {
            d_anti = if {next == head} {
                    if {next > cur} {next - cur} else {(self.l-cur) + next}
                } else {0};
            let d_cloc = if {next == tail} {
                    if {next < cur} {cur - next} else {(self.l-next) + cur}
                } else {0};
            let ds = [d_anti, d_cloc];
            //println!("ds :{:?}", ds);
            let tm = ds.iter().max().unwrap();
            m = *tm;
        } else {
            //println!("trace2 : {}", ni);
            if is_cclock {
                m = next - cur;
                d_anti = m;
            } else {
                m = cur - next;
                //d_cloc = m;
            }
        }
        ans += m as u64;
        //println!("m = {} -> ans = {}", m, ans);
        cur = tr_ac[0];
        tr_ac.remove(0);
        if {m == d_anti} {
            tr_st.remove(0);
        } else {
            tr_st.pop();
        }
    }
    ans
}

} // sub


// -------------------------------------------
fn main()
{
    // 入力
    let inputs_ln :Vec<u32> = read_vec();
    let ll :u32 = inputs_ln[0];
    let nn :u32 = inputs_ln[1];
    let mut xtree :Vec<u32> = Vec::new();
    let mut sub = Sub {l: ll, n: nn, st: Vec::new()};
    for _ in 0..nn {
        let mut s = String::new();
        std::io::stdin().read_line(&mut s).ok();
        let s: u32 = match s.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };
        xtree.push(s);
        sub.st.push(s);
    }
    // おおまかな作戦としては
    // 順路が最小になるけどあるところからジグザグに歩き始めると
    // 長い距離を歩くことになる、という考えで行く
    // それはどういうことかというと、木の並びを入れ替えて
    // 計算していく、という状態といえると思う
    let mut dmax1 :u64 = 0;
    for i in 1..(nn+1) {
        scramble_vec(&mut xtree, i);
        let a = sub.burning_tree(&xtree, true, (xtree.len() as u32) - i);
        //let a = 0;
        //println!(": {}", a);
        if a > dmax1 {
            dmax1 = a;
        } else {
            break;
        }
    }
    //println!("{}", dmax1);

    let mut dmax2 :u64 = 0;
    let mut xtree: Vec<_> = sub.st.iter().cloned().collect();
    xtree.reverse();
    //println!("{:?}", xtree);
    for i in 1..(nn+1) {
        scramble_vec(&mut xtree, i);
        let a = sub.burning_tree(&xtree, false, (xtree.len() as u32) - i);
        //println!(": {}", a);
        if a > dmax2 {
            dmax2 = a;
        } else {
            break;
        }
    }
    //println!("{}", dmax2);

    let dmx = if {dmax1 > dmax2} {dmax1} else {dmax2};
    println!("{}", dmx);
}