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 C (GCC 5.4.1) (judge verdict: Runtime Error)


//#include "pch.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#define llong long long
#define fr(i,l,r) for(i=(l);i<(r);i++)
#define min(p,q) ((p)<(q)?(p):(q))
#define max(p,q) ((p)>(q)?(p):(q))
#define INF 1000000000000//10^12

static int x[200010];
static llong dp[2010][2010][2];
static int back[2010][2010][2];
int m,n;

llong run(int a,int b, int c){
	
	llong temp1,temp2;
	
	//停止条件
	if(dp[a][b][c]>0) return dp[a][b][c];
	if(a+1==b) return max(x[b]-x[a],m-x[b]+x[a]);
	
	//dp先
	if(c==0){
		temp1=x[a+1]-x[a]+run(a+1,b,0);
		temp2=m-x[b]+x[a]+run(a+1,b,1);
		if(temp1>temp2){
			back[a][b][c]=a+1;
		}else{
			back[a][b][c]=b;
		}
	}else{
		temp1=x[b]-x[b-1]+run(a,b-1,1);
		temp2=m-x[b]+x[a]+run(a,b-1,0);
		if(temp1>temp2){
			back[a][b][c]=b-1;
		}else{
			back[a][b][c]=a;
		}
	}
	
	dp[a][b][c]=max(temp1,temp2);
	return max(temp1,temp2);
}


int main(void)
{
	//変数の宣言
//	int m, n;
//	static int x[200010];
	static llong int suml[200010];
	static llong int sumr[200010];
//	static llong dp[1010][1010][2];

	//よく使う変数
	int i, j, k, l;
	int flag = 0;
	llong int ans = 0;
	int count = 0;
	llong int temp, temp1, temp2;
	int max, min;
	int len;
	int sum = 0;
	int under, upper, middle;
	int dummy;

	//データの読み込み
	scanf("%d %d", &m, &n);
//	scanf_s("%d %d", &m, &n);
	
	for (i = 0;i < n;i++) {
		scanf("%d", &x[i]);
//		scanf_s("%d", &x[i]);
	}
	
	for(i=0;i<2000;i++){
		for(j=0;j<2000;j++){
			for(k=0;k<2;k++){
				dp[i][j][k]=0;
			}
		}
	}
	
	//	printf("nは%dです\n", n);
	//	printf("データの読み込み終了\n");
	//実際の処理
	if(n>2000){
		printf("0");
		return 0;
	}
	temp1=run(0,n-1,0)+x[0];
	temp2=run(0,n-1,1)+m-x[n-1];
	
	ans=max(temp1,temp2);
	
	//	printf("計算部分終了\n");
	//出力
	
	printf("%lld", ans);
	
/*	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			for(k=0;k<2;k++){
				if(i<j){
					printf("dp[%d][%d][%d]=%d\n",i,j,k,dp[i][j][k]);
					printf("back[%d][%d][%d]=%d\n",i,j,k,back[i][j][k]);
				}
			}
		}
	}
*/	
	
	//	printf("結果の出力終了\n");
	
	return 0;
	
}