Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    REP(WW, n) cerr << v[WW] << ' ';                                           \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, WW, HH)                                                      \
  {                                                                            \
    REP(W_, WW) {                                                              \
      REP(H_, HH) cerr << v[W_][H_] << ' ';                                    \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353

typedef long long ll;
typedef pair<ll, ll> P;

vector<ll> v;

ll dp[55][55][111];
ll n, k;

ll dfs(ll l, ll r, ll cou) {
  if (l > l)
    return 0;
  if (cou > k)
    return -LLINF;
  if (dp[l][r][cou] != -LLINF)
    return dp[l][r][cou];

  ll ret = 0;
  ret = max(ret, dfs(l + 1, r, cou + 1) + v[l]);
  ret = max(ret, dfs(l + 1, r, cou + 2));
  ret = max(ret, dfs(l, r - 1, cou + 1) + v[r]);
  ret = max(ret, dfs(l, r - 1, cou + 2));

  return dp[l][r][cou] = ret;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  REP(i, 55) REP(j, 55) REP(k, 111) dp[i][j][k] = -LLINF;

  cin >> n >> k;
  REP(i, n) {
    int tmp;
    cin >> tmp;
    v.PB(tmp);
  }

  cout << dfs(0, n - 1, 0) << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \
  {                                                                            \
    REP(WW, n) cerr << v[WW] << ' ';                                           \
    cerr << endl << endl;                                                      \
  }
#define SHOW2d(v, WW, HH)                                                      \
  {                                                                            \
    REP(W_, WW) {                                                              \
      REP(H_, HH) cerr << v[W_][H_] << ' ';                                    \
      cerr << endl;                                                            \
    }                                                                          \
    cerr << endl;                                                              \
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353

typedef long long ll;
typedef pair<ll, ll> P;

vector<ll> v;

ll dp[55][55][111];
ll n, k;

ll dfs(ll l, ll r, ll cou) {
  if (l > r || cou == k)
    return 0;
  if (cou > k)
    return -LLINF;
  if (dp[l][r][cou] != -LLINF)
    return dp[l][r][cou];

  ll ret = 0;
  ret = max(ret, dfs(l + 1, r, cou + 1) + v[l]);
  ret = max(ret, dfs(l + 1, r, cou + 2));
  ret = max(ret, dfs(l, r - 1, cou + 1) + v[r]);
  ret = max(ret, dfs(l, r - 1, cou + 2));

  return dp[l][r][cou] = ret;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  REP(i, 55) REP(j, 55) REP(k, 111) dp[i][j][k] = -LLINF;

  cin >> n >> k;
  REP(i, n) {
    int tmp;
    cin >> tmp;
    v.PB(tmp);
  }

  cout << dfs(0, n - 1, 0) << endl;

  return 0;
}