Language: C++

/**************************************
 * Au: Hany01
 * Prob: agc030b
 * Date: Feb 8th, 2019
 * Email: hany01@foxmail.com
**************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define Rep(i, j) for (register int i = 0, i##_end_ = j; i < i##_end_; ++ i)
#define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.begin(), a.end()
#define PB(a) push_back(a)
#define MP(a, b) make_pair(a, b)
#define X first
#define Y second
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define y1 WoXiHuanNiA 
#ifdef hany01
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline T read() {
    register char c_; register T _, __;
    for (_ = 0, __ = 1, c_ = getchar(); !isdigit(c_); c_ = getchar()) if (c_ == '-')  __ = -1;
    for ( ; isdigit(c_); c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}
//EOT


const int MAXN = 2e5 + 5;

int L, n, p[MAXN];
LL ans = 0;

int getdis(int x, int y) {
	return max(abs(p[x] - p[y]), L - abs(p[x] - p[y]));
}
void calc() {
	static LL sum1[MAXN], sum2[MAXN];
	For(i, 1, n)
		sum1[i] = sum1[i - 1] + p[i];
	Fordown(i, n, 1)
		sum2[i] = sum2[i + 1] + (L - p[i]);

	For(i, 1, n - 1) {
		if ((n - i) & 1) {
			int x = i + (n - i) / 2 + 1;
			chkmax(ans, (sum1[x - 1] - sum1[i - 1]) * 2 + sum2[x + 1] * 2 + getdis(x, x - 1) - p[x - 1]);
		} else {
			int x = i + (n - i) / 2;
			chkmax(ans, (sum1[x - 1] - sum1[i - 1]) * 2 + sum2[x + 1] * 2 + getdis(x, x + 1) - (L - p[x + 1]));
		}
	}
}

int main() {
#ifdef hany01
    File("agc030b");
#endif

	L = read<int>(), n = read<int>();
	For(i, 1, n) p[i] = read<int>();
	calc(), chkmax(ans, p[n]);
	For(i, 1, n) p[i] = L - p[i];
	reverse(p + 1, p + 1 + n);
	calc(), chkmax(ans, p[n]);
	cout << ans << endl;

    return 0;
}


Compiler output:
a.cc: In function 'T read()':
a.cc:40:19: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   40 |     register char c_; register T _, __;
      |                   ^~
a.cc:40:34: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   40 |     register char c_; register T _, __;
      |                                  ^
a.cc:40:37: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   40 |     register char c_; register T _, __;
      |                                     ^~
a.cc: In function 'void calc()':
a.cc:58:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   58 |         For(i, 1, n)
      |             ^
a.cc:18:40: note: in definition of macro 'For'
   18 | #define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
      |                                        ^
a.cc:58:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   58 |         For(i, 1, n)
      |             ^
a.cc:18:49: note: in definition of macro 'For'
   18 | #define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
      |                                                 ^
a.cc:60:17: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   60 |         Fordown(i, n, 1)
      |                 ^
a.cc:19:44: note: in definition of macro 'Fordown'
   19 | #define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
      |                                            ^
a.cc:60:17: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   60 |         Fordown(i, n, 1)
      |                 ^
a.cc:19:53: note: in definition of macro 'Fordown'
   19 | #define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
      |                                                     ^
a.cc:63:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   63 |         For(i, 1, n - 1) {
      |             ^
a.cc:18:40: note: in definition of macro 'For'
   18 | #define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
      |                                        ^
a.cc:63:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   63 |         For(i, 1, n - 1) {
      |             ^
a.cc:18:49: note: in definition of macro 'For'
   18 | #define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
      |                                                 ^
a.cc: In function 'int main()':
a.cc:80:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   80 |         For(i, 1, n) p[i] = read<int>();
      |             ^
a.cc:18:40: note: in definition of macro 'For'
   18 | #define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
      |                                        ^
a.cc:80:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   80 |         For(i, 1, n) p[i] = read<int>();
      |             ^
a.cc:18:49: note: in definition of macro 'For'
   18 | #define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
      |                                                 ^
a.cc:81:23: error: no matching function for call to 'chkmax(LL&, int&)'
   81 |         calc(), chkmax(ans, p[n]);
      |                 ~~~~~~^~~~~~~~~~~
a.cc:37:34: note: candidate: 'template<class T> bool chkmax(T&, T)'
   37 | template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
      |                                  ^~~~~~
a.cc:37:34: note:   template argument deduction/substitution failed:
a.cc:81:23: note:   deduced conflicting types for parameter 'T' ('long long int' and 'int')
   81 |         calc(), chkmax(ans, p[n]);
      |                 ~~~~~~^~~~~~~~~~~
a.cc:82:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   82 |         For(i, 1, n) p[i] = L - p[i];
      |             ^
a.cc:18:40: note: in definition of macro 'For'
   18 | #define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
      |                                        ^
a.cc:82:13: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   82 |         For(i, 1, n) p[i] = L - p[i];
      |             ^
a.cc:18:49: note: in definition of macro 'For'
   18 | #define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
      |                                                 ^
a.cc:84:23: error: no matching function for call to 'chkmax(LL&, int&)'
   84 |         calc(), chkmax(ans, p[n]);
      |                 ~~~~~~^~~~~~~~~~~
a.cc:37:34: note: candidate: 'template<class T> bool chkmax(T&, T)'
   37 | template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
      |                                  ^~~~~~
a.cc:37:34: note:   template argument deduction/substitution failed:
a.cc:84:23: note:   deduced conflicting types for parameter 'T' ('long long int' and 'int')
   84 |         calc(), chkmax(ans, p[n]);
      |                 ~~~~~~^~~~~~~~~~~
a.cc: In instantiation of 'T read() [with T = int]':
a.cc:79:15:   required from here
   79 |         L = read<int>(), n = read<int>();
      |             ~~~~~~~~~^~
a.cc:40:19: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   40 |     register char c_; register T _, __;
      |                   ^~
a.cc:40:34: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   40 |     register char c_; register T _, __;
      |                                  ^
a.cc:40:37: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
   40 |     register char c_; register T _, __;
      |                                     ^~