Problem p03032 (C++)

Buggy submission (status: Time Limit Exceeded):
// ↓template↓

#include "bits/stdc++.h"
using namespace std;
#define Would
#define you
#define all(n) n.begin(), n.end()
#define rall(n) n.rbegin(), n.rend()
typedef long long ll;
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
const double pi = acos(-1);
const long long SIZE = 1 << 17;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}, alp[30];
long long fac[200005], finv[200005], inv[200005];
vector<long long> dij;
struct edge {
  long long to, cost;
};
vector<vector<edge>> G;

long long mod_pow(long long a, long long b) {
  long long res = 1, mul = a;
  for (int i = 0; i < 31; ++i) {
    if (b >> i & 1) {
      res *= mul;
      res %= MOD;
    }
    mul = (mul * mul) % MOD;
  }

  return res;
}

void addedge(int from, int to, int cost) {
  G[from].push_back({to, cost});
  G[to].push_back({from, cost});
}

template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

// ↑template↑

int a, b;
vector<ll> v(a);
ll dp(ll a, ll b, ll c);
vector<vector<vector<ll>>> m;

int main() {
  cin >> a >> b;
  v.resize(a);
  for (int i = 0; i < a; ++i) {
    cin >> v[i];
  }
  m.resize(a, vector<std::vector<ll>>(a, std::vector<ll>(b + 1, -1)));
  cout << dp(0, a - 1, b) << endl;
}

ll dp(ll a, ll b, ll c) {
  if (b < a || c < 0) {
    return -INF;
  }
  if (c == 0) {
    return 0;
  }
  if (a == b) {
    return max(0ll, v[a]);
  }

  ll aa = dp(a + 1, b, c - 1) + v[a], bb = dp(a + 1, b, c - 2),
     cc = dp(a, b - 1, c - 1) + v[b], dd = dp(a, b - 1, c - 2);

  return m[a][b][c] = max({aa, bb, cc, dd, 0ll});
}

Fixed submission:
// ↓template↓

#include "bits/stdc++.h"
using namespace std;
#define Would
#define you
#define all(n) n.begin(), n.end()
#define rall(n) n.rbegin(), n.rend()
typedef long long ll;
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
const double pi = acos(-1);
const long long SIZE = 1 << 17;
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}, alp[30];
long long fac[200005], finv[200005], inv[200005];
vector<long long> dij;
struct edge {
  long long to, cost;
};
vector<vector<edge>> G;

long long mod_pow(long long a, long long b) {
  long long res = 1, mul = a;
  for (int i = 0; i < 31; ++i) {
    if (b >> i & 1) {
      res *= mul;
      res %= MOD;
    }
    mul = (mul * mul) % MOD;
  }

  return res;
}

void addedge(int from, int to, int cost) {
  G[from].push_back({to, cost});
  G[to].push_back({from, cost});
}

template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

// ↑template↑

int a, b;
vector<ll> v(a);
ll dp(ll a, ll b, ll c);
vector<vector<vector<ll>>> m;

int main() {
  cin >> a >> b;
  v.resize(a);
  for (int i = 0; i < a; ++i) {
    cin >> v[i];
  }
  m.resize(a, vector<std::vector<ll>>(a, std::vector<ll>(b + 1, -1)));
  cout << dp(0, a - 1, b) << endl;
}

ll dp(ll a, ll b, ll c) {
  if (b < a || c < 0) {
    return -INF;
  }
  if (c == 0) {
    return 0;
  }
  if (a == b) {
    return max(0ll, v[a]);
  }
  if (m[a][b][c] != -1) {
    return m[a][b][c];
  }

  ll aa = dp(a + 1, b, c - 1) + v[a], bb = dp(a + 1, b, c - 2),
     cc = dp(a, b - 1, c - 1) + v[b], dd = dp(a, b - 1, c - 2);

  return m[a][b][c] = max({aa, bb, cc, dd, 0ll});
}