Language: C++

#include<iostream>
#include<vector>
#include<algorithm>

#define int long long
using namespace std;

int L, N;
int ans;
vector<int> Y;
/*
int len(int i) {
	int an = 0;
	int z = 0;
	bool flag = true;
	z = N - i + 1;
	for (int j = 0; j <= N; j++) {
		if (j < i) {
			an += Y[j] * z;
		}
		else if (flag) {
			z -= 2;
			if (z >0) {
				an += Y[j] * z;
			}
			else {
				flag = false;
				if (z == -1) {
					z = 0;
				}
				else {
					z = -1;
				}
			}
		}
		else {
			z += 2;
			an += Y[j] * z;
		}
	}
	return an;
}
*/

signed main()
{
	vector<int> X;

	cin >> L >> N;
	X.push_back(0);
	int x;
	for (int i = 1; i <= N; i++) {
		cin >> x;
		X.push_back(x);
	}

	vector<int> y;
	for (int i = 0; i < N; i++) {
		Y.push_back(X[i + 1] - X[i]);
		y.insert(y.begin(), X[i + 1] - X[i]);
	}
	Y.push_back(L - X[N]);
	y.insert(y.begin(), L - X[N]);

	int a[10];
	int b[10];
	int c[10];
	a[1] = 1;
	a[2] = N / 3;
	a[3] = N/ 3 * 2;
	a[4] = N;
	int an = 0;

	while (true) {
		for (int i = 1; i <= 4; i++) {
			int an = 0;
			int z = 0;
			bool flag = true;
			z = N - i + 1;
			for (int j = 0; j <= N; j++) {
				if (j < i) {
					an += Y[j] * z;
				}
				else if (flag) {
					z -= 2;
					if (z >0) {
						an += Y[j] * z;
					}
					else {
						flag = false;
						if (z == -1) {
							z = 0;
						}
						else {
							z = -1;
						}
					}
				}
				else {
					z += 2;
					an += Y[j] * z;
				}
			}
			b[i] = an;
		}
		if (b[2] > b[3]) {
			a[4] = a[3];
			a[2] = a[1] + (a[4] - a[1]) / 3;
			a[3] = a[1] + (a[4] - a[1]) / 3 * 2;
		}
		else {
			a[1] = a[2];
			a[2] = a[1] + (a[4] - a[1]) / 3;
			a[3] = a[1] + (a[4] - a[1]) / 3 * 2;
		}
		if (a[4] - a[1] < 10) {
			for (int i = a[1]; i <= a[4]; i++) {
				int an = 0;
				int z = 0;
				bool flag = true;
				z = N - i + 1;
				for (int j = 0; j <= N; j++) {
					if (j < i) {
						an += Y[j] * z;
					}
					else if (flag) {
						z -= 2;
						if (z >0) {
							an += Y[j] * z;
						}
						else {
							flag = false;
							if (z == -1) {
								z = 0;
							}
							else {
								z = -1;
							}
						}
					}
					else {
						z += 2;
						an += Y[j] * z;
					}
				}
				if (an > ans) {
					ans = an;
				}
			}
			break;
		}
	}

	/*
	for (int i = 1; i <= N; i++) {
		int an = 0;
		int z = 0;
		bool flag = true;
		z = N - i + 1;
		for (int j = 0; j <= N; j++) {
			if (j < i) {
				an += Y[j] * z;
			}
			else if (flag) {
				z -= 2;
				if (z >0) {
					an += Y[j] * z;
				}
				else {
					flag = false;
					if (z == -1) {
						z = 0;
					}
					else {
						z = -1;
					}
				}
			}
			else {
				z += 2;
				an += Y[j] * z;
			}
		}
		if (an > ans) {
			ans = an;
		}
	}*/
	Y = y;


	int a[10];
	int b[10];
	int c[10];
	a[1] = 1;
	a[2] = N / 3;
	a[3] = N / 3 * 2;
	a[4] = N;
	int an = 0;
	while (true) {
		for (int i = 1; i <= 4; i++) {
			int an = 0;
			int z = 0;
			bool flag = true;
			z = N - i + 1;
			for (int j = 0; j <= N; j++) {
				if (j < i) {
					an += Y[j] * z;
				}
				else if (flag) {
					z -= 2;
					if (z >0) {
						an += Y[j] * z;
					}
					else {
						flag = false;
						if (z == -1) {
							z = 0;
						}
						else {
							z = -1;
						}
					}
				}
				else {
					z += 2;
					an += Y[j] * z;
				}
			}
			b[i] = an;
		}
		if (b[2] > b[3]) {
			a[4] = a[3];
			a[2] = a[1] + (a[4] - a[1]) / 3;
			a[3] = a[1] + (a[4] - a[1]) / 3 * 2;
		}
		else {
			a[1] = a[2];
			a[2] = a[1] + (a[4] - a[1]) / 3;
			a[3] = a[1] + (a[4] - a[1]) / 3 * 2;
		}
		if (a[4] - a[1] < 10) {
			for (int i = a[1]; i <= a[4]; i++) {
				int an = 0;
				int z = 0;
				bool flag = true;
				z = N - i + 1;
				for (int j = 0; j <= N; j++) {
					if (j < i) {
						an += Y[j] * z;
					}
					else if (flag) {
						z -= 2;
						if (z >0) {
							an += Y[j] * z;
						}
						else {
							flag = false;
							if (z == -1) {
								z = 0;
							}
							else {
								z = -1;
							}
						}
					}
					else {
						z += 2;
						an += Y[j] * z;
					}
				}
				if (an > ans) {
					ans = an;
				}
			}
			break;
		}
	}
	cout << ans << endl;
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:191:13: error: redeclaration of 'long long int a [10]'
  191 |         int a[10];
      |             ^
a.cc:65:13: note: 'long long int a [10]' previously declared here
   65 |         int a[10];
      |             ^
a.cc:192:13: error: redeclaration of 'long long int b [10]'
  192 |         int b[10];
      |             ^
a.cc:66:13: note: 'long long int b [10]' previously declared here
   66 |         int b[10];
      |             ^
a.cc:193:13: error: redeclaration of 'long long int c [10]'
  193 |         int c[10];
      |             ^
a.cc:67:13: note: 'long long int c [10]' previously declared here
   67 |         int c[10];
      |             ^
a.cc:198:13: error: redeclaration of 'long long int an'
  198 |         int an = 0;
      |             ^~
a.cc:72:13: note: 'long long int an' previously declared here
   72 |         int an = 0;
      |             ^~