Language: C++

//By MagicSpark
/*
Problem:
Main Idea:
Status:
*/
#include<bits/stdc++.h>
#define int long long
#pragma optimize("Ofast")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
//#include<ext/pb_ds/assoc_container.hpp>
#pragma comment(linker, "/STACK:16000000")
//#define map gp_hash_table
#define PI 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
using namespace std;
//using namespace __gnu_pbds;
const int inf=0x3f3f3f3f;
const long long inf2=0x3f3f3f3f3f3f3f3f;
const double eps=1e-6;
const int mod=1000000007;
typedef long long ll;
namespace fastio{
	char in[100000];
	int itr=0,llen=0;
	char get(){
		if(itr==llen)llen=fread(in,1,100000,stdin),itr=0;
		if(llen==0)return EOF;
		return in[itr++];
	}
	char out[100000];
	int itr2=0;
	void put(char c){
		out[itr2++]=c;
		if(itr2==100000){
			fwrite(out,1,100000,stdout);
			itr2=0;
		}
	}
	int clear(){
		fwrite(out,1,itr2,stdout);
		itr2=0;
		return 0;
	}
	int getint(){
    	int r=0; bool ng=0; char c; c=get();
    	while (c!='-'&&(c<'0'||c>'9')) c=get();
   		if (c=='-') ng=1, c=get();
    	while (c>='0'&&c<='9') r=r*10+c-'0', c=get();
    	return ng?-r:r;
	}
	ll getll(){
    	ll r=0; bool ng=0; char c; c=get();
    	while (c!='-'&&(c<'0'||c>'9')) c=get();
   		if (c=='-') ng=1, c=get();
    	while (c>='0'&&c<='9') r=r*10+c-'0', c=get();
    	return ng?-r:r;
	}
	string getstr(){
		string ret="";
		char ch=get();
		while(ch==' '||ch=='\n')ch=get();
		while(ch!=' '&&ch!='\n')ret.push_back(ch),ch=get();
		return ret;
	}
	void putstr(string s){
		for(int i=0;i<s.size();i++)put(s[i]);
	}
	void putint(int x){
		if(x==0){
			put('0');
			return;
		}
		char c[10];int pos=0;
		while(x){
			c[pos++]='0'+x%10;
			x/=10;
		}
		for(int i=pos-1;i>=0;i--)put(c[i]);
	}
	void putll(int x){
		if(x==0){
			put('0');
			return;
		}
		char c[20];int pos=0;
		while(x){
			c[pos++]='0'+x%10;
			x/=10;
		}
		for(int i=pos-1;i>=0;i--)put(c[i]);
	}
}
using namespace fastio;
#define fastio
const int N=2005;
ll f[N][N][2];
int n,L;
int a[N];
int main(){
	L=getint();n=getint();
	if(n>2000)return 0;
	for(int i=1;i<=n;i++)a[i]=getint();
	f[1][n][0]=a[1];
	f[1][n][1]=L-a[n];
	for(int i=1;i<=n;i++)
		for(int j=n;j>i;j--){
			f[i+1][j][0]=max(f[i+1][j][0],f[i][j][0]+a[i+1]-a[i]);
			f[i+1][j][1]=max(f[i+1][j][1],f[i][j][0]+a[i]+L-a[j]);
			f[i][j-1][0]=max(f[i][j-1][0],f[i][j][1]+a[i]+L-a[j]);
			f[i][j-1][1]=max(f[i][j-1][1],f[i][j][1]+a[j]-a[j-1]);
		}
	ll ans=0;
	for(int i=1;i<=n;i++){
		ans=max(ans,(ll)max(f[i][i][0],f[i][i][1]));
	}
	putll(ans);put('\n');
	#ifdef fastio
		clear();
	#endif
	return 0;
}

Compiler output:
a.cc:10:51: warning: bad option '-funsafe-loop-optimizations' to pragma 'optimize' [-Wpragmas]
   10 | #pragma GCC optimize("-funsafe-loop-optimizations")
      |                                                   ^
a.cc:12:39: warning: bad option '-fwhole-program' to pragma 'optimize' [-Wpragmas]
   12 | #pragma GCC optimize("-fwhole-program")
      |                                       ^
a.cc:21:41: warning: bad option '-fcse-skip-blocks' to pragma 'optimize' [-Wpragmas]
   21 | #pragma GCC optimize("-fcse-skip-blocks")
      |                                         ^
a.cc:42:41: warning: bad option '-fstrict-overflow' to pragma 'optimize' [-Wpragmas]
   42 | #pragma GCC optimize("-fstrict-overflow")
      |                                         ^
a.cc:61:18: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   61 |         char get(){
      |                  ^
a.cc:61:18: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:61:18: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:61:18: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:68:24: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   68 |         void put(char c){
      |                        ^
a.cc:68:24: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:68:24: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:68:24: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:75:19: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   75 |         int clear(){
      |                   ^
a.cc:75:19: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:75:19: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:75:19: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:80:20: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   80 |         int getint(){
      |                    ^
a.cc:80:20: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:80:20: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:80:20: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:87:18: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   87 |         ll getll(){
      |                  ^
a.cc:87:18: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:87:18: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:87:18: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:94:23: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
   94 |         string getstr(){
      |                       ^
a.cc:94:23: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:94:23: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:94:23: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:101:29: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
  101 |         void putstr(string s){
      |                             ^
a.cc:101:29: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:101:29: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:101:29: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:104:26: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
  104 |         void putint(int x){
      |                          ^
a.cc:104:26: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:104:26: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:104:26: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:116:25: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
  116 |         void putll(int x){
      |                         ^
a.cc:116:25: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:116:25: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:116:25: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
a.cc:135:10: warning: bad option '-funsafe-loop-optimizations' to attribute 'optimize' [-Wattributes]
  135 | int main(){
      |          ^
a.cc:135:10: warning: bad option '-fwhole-program' to attribute 'optimize' [-Wattributes]
a.cc:135:10: warning: bad option '-fcse-skip-blocks' to attribute 'optimize' [-Wattributes]
a.cc:135:10: warning: bad option '-fstrict-overflow' to attribute 'optimize' [-Wattributes]
cc1plus: error: '::main' must return 'int'