Problem p03187 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#define NDEBUG
#define SHOW(...) static_cast<void>(0)
//!===========================================================!//
//!  dP     dP                          dP                    !//
//!  88     88                          88                    !//
//!  88aaaaa88a .d8888b. .d8888b. .d888b88 .d8888b. 88d888b.  !//
//!  88     88  88ooood8 88'  '88 88'  '88 88ooood8 88'  '88  !//
//!  88     88  88.  ... 88.  .88 88.  .88 88.  ... 88        !//
//!  dP     dP  '88888P' '88888P8 '88888P8 '88888P' dP        !//
//!===========================================================!//
template <typename T, typename A>
std::istream &operator>>(std::istream &is, std::vector<T, A> &v) {
  for (auto &e : v) {
    is >> e;
  }
  return is;
}
template <typename T> T read() {
  T v;
  return std::cin >> v, v;
}
template <typename T> std::vector<T> readVec(const std::size_t l) {
  std::vector<T> v(l);
  for (auto &e : v) {
    std::cin >> e;
  }
  return v;
}
using ld = long double;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr unsigned int MOD = 1000000007;
template <typename T> constexpr T INF = std::numeric_limits<T>::max() / 4;
template <typename F> constexpr F PI = static_cast<F>(3.1415926535897932385);
std::mt19937 mt{std::random_device{}()};
template <typename T> bool chmin(T &a, const T &b) {
  return (a > b ? a = b, true : false);
}
template <typename T> bool chmax(T &a, const T &b) {
  return (a < b ? a = b, true : false);
}
template <typename T> std::vector<T> Vec(const std::size_t n, T v) {
  return std::vector<T>(n, v);
}
template <class... Args> auto Vec(const std::size_t n, Args... args) {
  return std::vector<decltype(Vec(args...))>(n, Vec(args...));
}
template <typename T> constexpr T popCount(const T u) {
#ifdef __has_builtin
  return u == 0 ? T(0) : (T)__builtin_popcountll(u);
#else
  unsigned long long v = static_cast<unsigned long long>(u);
  return v = (v & 0x5555555555555555ULL) + (v >> 1 & 0x5555555555555555ULL),
         v = (v & 0x3333333333333333ULL) + (v >> 2 & 0x3333333333333333ULL),
         v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL,
         static_cast<T>(v * 0x0101010101010101ULL >> 56 & 0x7f);
#endif
}
template <typename T> constexpr T log2p1(const T u) {
#ifdef __has_builtin
  return u == 0 ? T(0) : T(64 - __builtin_clzll(u));
#else
  unsigned long long v = static_cast<unsigned long long>(u);
  return v = static_cast<unsigned long long>(v), v |= (v >> 1), v |= (v >> 2),
         v |= (v >> 4), v |= (v >> 8), v |= (v >> 16), v |= (v >> 32),
         popCount(v);
#endif
}
template <typename T> constexpr T clog(const T v) {
  return v == 0 ? T(0) : log2p1(v - 1);
}
template <typename T> constexpr T msbp1(const T v) { return log2p1(v); }
template <typename T> constexpr T lsbp1(const T v) {
#ifdef __has_builtin
  return __builtin_ffsll(v);
#else
  return v == 0 ? T(0) : popCount((v & (-v)) - T(1)) + T(1);
#endif
}
template <typename T> constexpr bool ispow2(const T v) {
  return popCount(v) == 1;
}
template <typename T> constexpr T ceil2(const T v) {
  return v == 0 ? T(1) : T(1) << log2p1(v - 1);
}
template <typename T> constexpr T floor2(const T v) {
  return v == 0 ? T(0) : T(1) << (log2p1(v) - 1);
}
//!============================================!//
//!    8888ba.88ba             oo              !//
//!    88  '8b  '8b                            !//
//!    88   88   88  .d8888b.  dP  88d888b.    !//
//!    88   88   88  88'  '88  88  88'  '88    !//
//!    88   88   88  88.  .88  88  88    88    !//
//!    dP   dP   dP  '88888P8  dP  dP    dP    !//
//!============================================!//
int main() {
  const ll L = read<ll>();
  const int N = read<int>();
  auto X = readVec<ll>(N);
  auto cwd = [&](const int s, const int t) { return (X[t] - X[s] + L) % L; };
  auto naive = [&](const int s) {
    bool clock = false;
    ll sum = X[s];
    std::set<int> res;
    for (int i = s + 1; i < N; i++) {
      res.insert(i);
    }
    for (int p = s; not res.empty(); clock = not clock) {
      if (clock) {
        const int nxt = *res.begin();
        sum += cwd(p, nxt), res.erase(nxt), p = nxt;
      } else {
        const int nxt = *res.rbegin();
        sum += cwd(nxt, p), res.erase(nxt), p = nxt;
      }
    }
    return sum;
  };
  auto tle = [&]() {
    ll ans = 0;
    for (int i = 0; i < N; i++) {
      chmax(ans, naive(i));
    }
    for (auto &e : X) {
      e = L - e;
    }
    std::reverse(X.begin(), X.end());
    for (int i = 0; i < N; i++) {
      chmax(ans, naive(i));
    }
    std::cout << ans << std::endl;
  };
  tle();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC diagnostic ignored "-Wsign-compare"
#pragma GCC diagnostic ignored "-Wsign-conversion"
#define NDEBUG
#define SHOW(...) static_cast<void>(0)
//!===========================================================!//
//!  dP     dP                          dP                    !//
//!  88     88                          88                    !//
//!  88aaaaa88a .d8888b. .d8888b. .d888b88 .d8888b. 88d888b.  !//
//!  88     88  88ooood8 88'  '88 88'  '88 88ooood8 88'  '88  !//
//!  88     88  88.  ... 88.  .88 88.  .88 88.  ... 88        !//
//!  dP     dP  '88888P' '88888P8 '88888P8 '88888P' dP        !//
//!===========================================================!//
template <typename T, typename A>
std::istream &operator>>(std::istream &is, std::vector<T, A> &v) {
  for (auto &e : v) {
    is >> e;
  }
  return is;
}
template <typename T> T read() {
  T v;
  return std::cin >> v, v;
}
template <typename T> std::vector<T> readVec(const std::size_t l) {
  std::vector<T> v(l);
  for (auto &e : v) {
    std::cin >> e;
  }
  return v;
}
using ld = long double;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr unsigned int MOD = 1000000007;
template <typename T> constexpr T INF = std::numeric_limits<T>::max() / 4;
template <typename F> constexpr F PI = static_cast<F>(3.1415926535897932385);
std::mt19937 mt{std::random_device{}()};
template <typename T> bool chmin(T &a, const T &b) {
  return (a > b ? a = b, true : false);
}
template <typename T> bool chmax(T &a, const T &b) {
  return (a < b ? a = b, true : false);
}
template <typename T> std::vector<T> Vec(const std::size_t n, T v) {
  return std::vector<T>(n, v);
}
template <class... Args> auto Vec(const std::size_t n, Args... args) {
  return std::vector<decltype(Vec(args...))>(n, Vec(args...));
}
template <typename T> constexpr T popCount(const T u) {
#ifdef __has_builtin
  return u == 0 ? T(0) : (T)__builtin_popcountll(u);
#else
  unsigned long long v = static_cast<unsigned long long>(u);
  return v = (v & 0x5555555555555555ULL) + (v >> 1 & 0x5555555555555555ULL),
         v = (v & 0x3333333333333333ULL) + (v >> 2 & 0x3333333333333333ULL),
         v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL,
         static_cast<T>(v * 0x0101010101010101ULL >> 56 & 0x7f);
#endif
}
template <typename T> constexpr T log2p1(const T u) {
#ifdef __has_builtin
  return u == 0 ? T(0) : T(64 - __builtin_clzll(u));
#else
  unsigned long long v = static_cast<unsigned long long>(u);
  return v = static_cast<unsigned long long>(v), v |= (v >> 1), v |= (v >> 2),
         v |= (v >> 4), v |= (v >> 8), v |= (v >> 16), v |= (v >> 32),
         popCount(v);
#endif
}
template <typename T> constexpr T clog(const T v) {
  return v == 0 ? T(0) : log2p1(v - 1);
}
template <typename T> constexpr T msbp1(const T v) { return log2p1(v); }
template <typename T> constexpr T lsbp1(const T v) {
#ifdef __has_builtin
  return __builtin_ffsll(v);
#else
  return v == 0 ? T(0) : popCount((v & (-v)) - T(1)) + T(1);
#endif
}
template <typename T> constexpr bool ispow2(const T v) {
  return popCount(v) == 1;
}
template <typename T> constexpr T ceil2(const T v) {
  return v == 0 ? T(1) : T(1) << log2p1(v - 1);
}
template <typename T> constexpr T floor2(const T v) {
  return v == 0 ? T(0) : T(1) << (log2p1(v) - 1);
}
//!============================================!//
//!    8888ba.88ba             oo              !//
//!    88  '8b  '8b                            !//
//!    88   88   88  .d8888b.  dP  88d888b.    !//
//!    88   88   88  88'  '88  88  88'  '88    !//
//!    88   88   88  88.  .88  88  88    88    !//
//!    dP   dP   dP  '88888P8  dP  dP    dP    !//
//!============================================!//
int main() {
  const ll L = read<ll>();
  const int N = read<int>();
  auto X = readVec<ll>(N);
  auto cwd = [&](const int s, const int t) { return (X[t] - X[s] + L) % L; };
  auto naive = [&](const int s) {
    bool clock = false;
    ll sum = X[s];
    std::set<int> res;
    for (int i = s + 1; i < N; i++) {
      res.insert(i);
    }
    for (int p = s; not res.empty(); clock = not clock) {
      if (clock) {
        const int nxt = *res.begin();
        sum += cwd(p, nxt), res.erase(nxt), p = nxt;
      } else {
        const int nxt = *res.rbegin();
        sum += cwd(nxt, p), res.erase(nxt), p = nxt;
      }
    }
    return sum;
  };
  auto tle = [&]() {
    ll ans = 0;
    for (int i = 0; i < N; i++) {
      chmax(ans, naive(i));
    }
    for (auto &e : X) {
      e = L - e;
    }
    std::reverse(X.begin(), X.end());
    for (int i = 0; i < N; i++) {
      chmax(ans, naive(i));
    }
    std::cout << ans << std::endl;
  };
  auto fast = [&]() {
    ll ans = X.back(), sum = ans;
    for (int s = N - 1, t = N - 1; s >= 1;) {
      bool hoge = (N - 1 - s) % 2 == 0;
      s--, t = hoge ? t : t - 1;
      sum += -cwd(s, t) + cwd(hoge ? t : (t + 1) % N, s);
      chmax(ans, sum);
      SHOW(s, t, sum);
    }
    return ans;
  };
  auto ac = [&]() {
    ll ans = fast();
    for (auto &e : X) {
      e = L - e;
    }
    std::reverse(X.begin(), X.end());
    chmax(ans, fast());
    std::cout << ans << std::endl;
  };
  ac();
  //    tle();
  return 0;
}