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 Java8 (OpenJDK 1.8.0) (judge verdict: Runtime Error)


import java.io.PrintStream;
import java.util.Scanner;

public class Main {
    private static final PrintStream so = System.out;
    private static final Scanner     sc = new Scanner(System.in);

    public static void main(String[] args) {
        int L = sc.nextInt();
        int N = sc.nextInt();
        int[] trees = new int[N];
        for (int i = 0; i < N; i++) {
            trees[i] = sc.nextInt();
        }
        if (N == 1)  {
            so.println(Math.max(d(0, trees[0], true, L), d(0, trees[0], false, L)));
            return;
        }
        long max = 0;
        for (int i = 1; i < N; i++) {
            
            int temp = d(0, trees[i], CLOCKWISE, L);
            long sum = calcMax(trees, i, i-1, 0, L);
            max = Math.max(max, sum+temp);
        }
        
        for (int i = 1; i < N; i++) {
            int temp = d(0, trees[i], COUNTER_CLOCKWISE, L);
            long sum = calcMax(trees, i, N-1, i+1, L);
            max = Math.max(max, sum+temp);
        }
        so.println(max);
    }
    
    static boolean CLOCKWISE = true;
    static boolean COUNTER_CLOCKWISE = false;
    public static long calcMax(int[] trees, int startI, int maxI, int minI, int L) {
        long sum_clockwise_start = 0;
        int current = startI;
        int next_max = maxI;
        int next_min = minI;
        while (true) {
            // 時計回りから
            sum_clockwise_start += d(trees[current], trees[next_max], CLOCKWISE, L);
//            so.println("sum = " + sum_clockwise_start);
            current = next_max;
            next_max--;
            if (next_max < next_min) break;
            
            sum_clockwise_start += d(trees[current], trees[next_min], COUNTER_CLOCKWISE, L);
//            so.println("sum = " + sum_clockwise_start);
            current = next_min;
            next_min++;
            if (next_max < next_min) break;
        }
        
        long sum_counterclockwise_start = 0;
        current = startI;
        next_max = maxI;
        next_min = minI;
        while (true) {
//            so.println("current = " + current + ", n_max = " + next_max + ", n_min = " + next_min);
            // 反時計周りから
            sum_counterclockwise_start += d(trees[current], trees[next_min], COUNTER_CLOCKWISE, L);
//            so.println("sum2 = " + sum_counterclockwise_start);
            current = next_min;
            next_min++;
            if (next_max < next_min) break;
            
//            so.println("current = " + current + ", n_max = " + next_max + ", n_min = " + next_min);
            sum_counterclockwise_start += d(trees[current], trees[next_max], CLOCKWISE, L);
//            so.println("sum2 = " + sum_counterclockwise_start);
            current = next_max;
            next_max--;
            if (next_max < next_min) break;
        }
        
        return Math.max(sum_clockwise_start, sum_counterclockwise_start);
    }
    
    public static int d(int from, int to, boolean clockwise, int L) {
        if (clockwise) {
            int temp = from - to;
            if (temp < 0) temp += L;
            return temp;
        } else {
            return d(to, from, CLOCKWISE, L);
        }
    }
}