Problem p03171 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define DEBUG_VLL(vec)                                                         \
  REP(sz, vec.size())                                                          \
      std::cerr << vec[sz] << (sz == vec.size() - 1 ? '\n' : ' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

vll a;
vvll dp;

ll dfs(int left, int right) {
  if (dp[left][right] > -HIGHINF)
    return dp[left][right];
  if (left == right)
    return dp[left][right] = a[left];
  return max(a[left] - dfs(left + 1, right), a[right] - dfs(left, right - 1));
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  dp.resize(n, vll(n, -HIGHINF));
  cout << dfs(0, n - 1) << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
template <class T> using V = vector<T>;
template <class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i = k; i < (ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define DEBUG_VLL(vec)                                                         \
  REP(sz, vec.size())                                                          \
      std::cerr << vec[sz] << (sz == vec.size() - 1 ? '\n' : ' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

vll a;
vvll dp;

ll dfs(int left, int right) {
  if (dp[left][right] > -HIGHINF)
    return dp[left][right];
  if (left == right)
    return dp[left][right] = a[left];
  return dp[left][right] = max(a[left] - dfs(left + 1, right),
                               a[right] - dfs(left, right - 1));
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  a.resize(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  dp.resize(n, vll(n, -HIGHINF));
  cout << dfs(0, n - 1) << '\n';
  return 0;
}