Language: C++

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using u64 = uint_fast64_t;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true;} return false; }
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true;} return false; }
inline int popcount(int x) {return __builtin_popcount(x);}
inline int popcount(long long x) {return __builtin_popcountll(x);}
void print() { cout << "\n"; }
template<class T, class... Args>
void print(const T &x, const Args &... args) {
    cout << x << " ";
    print(args...);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

ll dp[3000][3000][2];
const ll INF = 1e18;
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    ll L,N; cin >> L >> N;
    vector<ll> X(N);
    rep(i,N) cin >> X[i];
    ll L,N; cin >> L >> N;

    if (N <= 2000) {
        auto pos=[&](ll x) {
            if (x < 0) return x+N;
            if (x >= N) return x-N;
            return x;
        };
        auto calc=[&](int l, int r) {
            ll ret = X[r] - X[l];
            if (ret < 0) ret += L;
            return ret;
        };
        rep(i,N+1) rep(j,N+1) rep(k,2) dp[i][j][k] = -INF;
        dp[0][0][0] = X[0];
        dp[N-1][N-1][1] = L - X[N-1];
        rep(k,N) {
            rep(l,N) {
                int r = pos(l+k);
                if (dp[l][r][0] != -INF) {
                    chmax(dp[l][pos(r+1)][0],dp[l][r][0]+calc(r,pos(r+1)));
                    chmax(dp[pos(l-1)][r][1],dp[l][r][0]+calc(pos(l-1),r));
                }
                if (dp[l][r][1] != -INF) {
                    chmax(dp[l][pos(r+1)][0],dp[l][r][1]+calc(l,pos(r+1)));
                    chmax(dp[pos(l-1)][r][1],dp[l][r][1]+calc(pos(l-1),l));
                }
            }
        }
        ll ans = 0;
        rep(i,N) rep(j,2) chmax(ans,dp[i][pos(i+N-1)][j]);
        cout << ans << ln;
        return 0;
    }

    auto func=[&](int x) {
        ll val = X[x];
        int l = x, r = N-1;
        int d = r - l;
        rep(i,d) {
            val += L - (X[r] - X[l]);
            if (~i&1) l++;
            else r--;
        }
        return val;
    };

    ll ans = 0;
    int ok = 0;
    int ng = N;
    while (ng - ok > 1) {
        int mid = (ok+ng)/2;
        if (func(mid) - func(mid-1) >= 0) ok = mid;
        else ng = mid;
    }
    chmax(ans,func(ok));
    rep(i,N) X[i] = L - X[i];
    reverse(all(X));
    ok = 0;
    ng = N;
    while (ng - ok > 1) {
        int mid = (ok+ng)/2;
        if (func(mid) - func(mid-1) >= 0) ok = mid;
        else ng = mid;
    }
    chmax(ans,func(ok));

    cout << ans << ln;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:35:8: error: redeclaration of 'll L'
   35 |     ll L,N; cin >> L >> N;
      |        ^
a.cc:32:8: note: 'll L' previously declared here
   32 |     ll L,N; cin >> L >> N;
      |        ^
a.cc:35:10: error: redeclaration of 'll N'
   35 |     ll L,N; cin >> L >> N;
      |          ^
a.cc:32:10: note: 'll N' previously declared here
   32 |     ll L,N; cin >> L >> N;
      |          ^