Problem p02716 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fst first
#define snd second

template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pii> vii;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  int n;
  cin >> n;
  const ll inf = 1e18;
  vvl dp(n + 5, vl(2, -inf));
  vl a(n + 5, -inf);
  rep(i, 0, n) cin >> a[i];
  dp[0][0] = a[0];
  dp[1][0] = a[1];
  dp[1][1] = a[1];
  dp[2][1] = a[2];
  rep(i, 0, n) {
    rep(j, 0, 2) {
      if (j and (n % 2 == 0))
        break;
      if (dp[i][j] == -inf)
        continue;
      ckmax(dp[i + 2][j], dp[i][j] + a[i + 2]);
      if (((i % 2) ^ (j)) == 0)
        ckmax(dp[i + 3][j], dp[i][j] + a[i + 3]);
      if (j == 0 and n % 2 == 1)
        ckmax(dp[i + 1][1], dp[i][j]);
    }
  }

  cout << max(*max_element(all(dp[n - 2])), *max_element(all(dp[n - 1])))
       << '\\n';
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fst first
#define snd second

template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<pair<ll, ll>> vll;

const ll inf = 1e18;

ll solve(vl &a) {
  int n = sz(a);
  if (n == 0)
    return -inf;
  if (n == 2)
    max(a[0], a[1]);
  rep(i, 0, n) cerr << a[i] << ' ';
  cerr << '\\n';

  assert(n % 2 == 0);
  vll couples;
  rep(i, 0, n) {
    couples.emplace_back(a[i], a[i + 1]);
    ++i;
  }
  vl left(n / 2), right(n / 2);
  left[0] = couples[0].fst;
  right[n / 2 - 1] = couples[n / 2 - 1].snd;
  rep(i, 1, n / 2) left[i] = left[i - 1] + couples[i].fst;
  per(i, 0, n / 2 - 1) right[i] = right[i + 1] + couples[i].snd;
  ll ans = max(left[n / 2 - 1], right[0]);
  rep(i, 0, n / 2 - 1) ckmax(ans, left[i] + right[i + 1]);
  return ans;
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  int n;
  cin >> n;

  vl a(n);
  rep(i, 0, n) cin >> a[i];

  if (n == 2)
    return cout << max(a[0], a[1]) << '\\n', 0;
  if (n == 3)
    return cout << max(a[0], max(a[1], a[2])) << '\\n', 0;

  ll ans = -inf;
  if (n & 1) {
    vl lfts;
    rep(i, 0, n) {
      lfts.pb(a[i]);
      ++i;
    }
    sort(all(lfts));
    reverse(all(lfts));
    rep(i, 0, n / 2) ans += lfts[i];

    if (n > 5) {
      vl sub1, sub2;
      rep(i, 2, n - 4) sub1.pb(a[i]);
      sub1.pb(max(a[n - 4], a[n - 3]));
      sub2.pb(max(a[2], a[3]));
      rep(i, 4, n - 2) sub2.pb(a[i]);
      ckmax(ans, solve(sub1) + a[0] + a.back());
      ckmax(ans, solve(sub2) + a[0] + a.back());
    }
  }

  vl b = a, c = a;
  if (n & 1) {
    reverse(all(b));
    b.pop_back();
    reverse(all(b));
    c.pop_back();
  }

  ckmax(ans, solve(b));
  ckmax(ans, solve(c));

  cout << ans << '\\n';
}