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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (int i = 0; i < sz(v); i++) {
    os << v[i];
    if (i < sz(v) - 1)
      os << " ";
  }
  return os;
}

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  auto dp0 = make_vec<ll>(200200, 4);
  auto dp1 = make_vec<ll>(200200, 4);
  rep(i, 200200) rep(j, 4) dp0[i][j] = dp1[i][j] = -LINF;
  dp0[0][1] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (i % 2 == 1) {
        // 使う
        chmax(dp1[i + 1][j], dp0[i][j] + a[i]);
        // 使わない
        if (j - 1 >= 0)
          chmax(dp0[i + 1][j - 1], dp0[i][j]);
        if (j - 1 >= 0)
          chmax(dp0[i + 1][j - 1], dp1[i][j]);
      }
      if (i % 2 == 0) {
        // 使う
        chmax(dp1[i + 1][j + 1], dp0[i][j] + a[i]);
        // 使わない
        chmax(dp0[i + 1][j], dp0[i][j]);
        chmax(dp0[i + 1][j], dp1[i][j]);
      }
    }
  }
  cout << max(dp0[n][1], dp1[n][1]) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (int i = 0; i < sz(v); i++) {
    os << v[i];
    if (i < sz(v) - 1)
      os << " ";
  }
  return os;
}
vector<ll> a;
vector<map<int, ll>> memo(200200);
ll dp(int i, int j) {
  if (i < 0)
    return -LINF;
  if (i == 1 && j == 1)
    return a[0];
  if (j == 0)
    return memo[i][j] = 0;
  if (i == 0)
    return memo[i][j] = -LINF;
  if (memo[i].count(j))
    return memo[i][j];
  ll ret = -LINF;
  for (int k = 2; k <= 20; k++)
    chmax(ret, dp(i - k, j - 1) + a[i - 1]);
  return memo[i][j] = ret;
}
int main() {
  int n;
  cin >> n;
  a.resize(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;
  rep(i, n + 1) chmax(ans, dp(i, n / 2));
  cout << ans << endl;
}