Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(a) a.begin(), a.end()
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
const long long MOD = 1000000007, INF = 1e17;

#define endl '\n'
#define IOS()                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

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;
}

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);
}

int A, B, C, D, E, F, G, H, I, J, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
    K;
string s, t;

signed main() {
  IOS();
  cin >> N >> K;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  auto dp = make_v<int>(N + 100, N + 100, K + 100);
  fill_v(dp, -INF);
  dp[0][0][0] = 0;
  rep(k, 0, K + 1) {
    rep(i, 0, N + 1) {
      rep(j, 0, N + 1) {
        if (dp[i][j][k] == -1)
          continue;
        if (i + j >= N)
          continue;
        if (i <= N - 1)
          chmax(dp[k + 1][i + 1][j], dp[k][i][j] + a[i]);
        if (N - j - 1 >= 0)
          chmax(dp[k + 1][i][j + 1], dp[k][i][j] + a[N - j - 1]);
        chmax(dp[k + 2][i + 1][j], dp[k][i][j]);
        chmax(dp[k + 2][i][j + 1], dp[k][i][j]);
      }
    }
  }
  int ans = -INF;
  rep(i, 0, N + 1) rep(j, 0, N + 1) rep(k, 0, K + 1) chmax(ans, dp[k][i][j]);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define rep(i, s, n) for (int i = s; i < n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(a) a.begin(), a.end()
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
const long long MOD = 1000000007, INF = 1e17;

#define endl '\n'
#define IOS()                                                                  \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

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;
}

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);
}

int A, B, C, D, E, F, G, H, I, J, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
    K;
string s, t;

signed main() {
  IOS();
  cin >> N >> K;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  auto dp = make_v<int>(K + 100, N + 100, N + 100);
  fill_v(dp, -INF);
  dp[0][0][0] = 0;
  rep(k, 0, K + 1) {
    rep(i, 0, N + 1) {
      rep(j, 0, N + 1) {
        if (dp[i][j][k] == -1)
          continue;
        if (i + j >= N)
          continue;
        if (i <= N - 1)
          chmax(dp[k + 1][i + 1][j], dp[k][i][j] + a[i]);
        if (N - j - 1 >= 0)
          chmax(dp[k + 1][i][j + 1], dp[k][i][j] + a[N - j - 1]);
        chmax(dp[k + 2][i + 1][j], dp[k][i][j]);
        chmax(dp[k + 2][i][j + 1], dp[k][i][j]);
      }
    }
  }
  int ans = -INF;
  rep(i, 0, N + 1) rep(j, 0, N + 1) rep(k, 0, K + 1) chmax(ans, dp[k][i][j]);
  cout << ans << endl;
}