Problem p03032 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
const int INF = 1e9 + 101;
const long long LINF = 1e18;
const double EPS = 1e-8;
#define pb push_back
#define mk make_pair
#define fr first
#define sc second
#define reps(i, j, k) for (ll i = (j); i < (k); ++i)
#define rep(i, j) reps(i, 0, j)
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007
typedef long long ll;
typedef pair<ll, ll> Pii;
typedef pair<Pii, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "{";
  rep(i, v.size()) { out << v[i] << ", "; }
  return out << "}" << endl;
}
int N, K;
vector<int> v, rv;
int memo[52][52][52];
int solve(int depth, int r_depth, int cnt) {

  if (cnt == K) {
    return 0;
  }
  if (cnt > K) {
    return -INF;
  }
  if (memo[depth][r_depth][cnt] != -INF) {
    return memo[depth][r_depth][cnt];
  }
  int ret = -INF;
  ret = solve(depth, r_depth, K);
  if (v[depth] < 0) {
    ret = max(ret, max(solve(depth + 1, r_depth, cnt + 2),
                       solve(depth + 1, r_depth, cnt + 1) + v[depth]));
    if (cnt + 2 <= K)
      ret = max(ret, solve(depth + 1, r_depth, K));
  } else {
    ret = max(ret, solve(depth + 1, r_depth, cnt + 1) + v[depth]);
    ret = max(ret, solve(depth + 1, r_depth, K) + v[depth]);
  }
  if (rv[r_depth] < 0) {
    ret = max(ret, max(solve(depth, r_depth + 1, cnt + 2),
                       solve(depth, r_depth + 1, cnt + 1) + rv[r_depth]));
    if (cnt + 2 <= K)
      ret = max(ret, solve(depth, r_depth + 1, K));
  } else {
    ret = max(ret, solve(depth, r_depth + 1, cnt + 1) + rv[r_depth]);
    ret = max(ret, solve(depth, r_depth + 1, K) + rv[r_depth]);
  }
  return memo[depth][r_depth][cnt] = ret;
}

int main() {
  for (int i = 0; i < 52; ++i) {
    for (int j = 0; j < 52; ++j) {
      for (int k = 0; k < 52; ++k) {
        memo[i][j][k] = -INF;
      }
    }
  }
  cin >> N >> K;
  rep(i, N) {
    int a;
    cin >> a;
    v.pb(a);
  }
  rep(i, N) { rv.pb(v[N - i - 1]); }
  int ans = solve(0, 0, 0);
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
const int INF = 1e9 + 101;
const long long LINF = 1e18;
const double EPS = 1e-8;
#define pb push_back
#define mk make_pair
#define fr first
#define sc second
#define reps(i, j, k) for (ll i = (j); i < (k); ++i)
#define rep(i, j) reps(i, 0, j)
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007
typedef long long ll;
typedef pair<ll, ll> Pii;
typedef pair<Pii, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "{";
  rep(i, v.size()) { out << v[i] << ", "; }
  return out << "}" << endl;
}
int N, K;
vector<int> v, rv;
int memo[52][52][52];
int solve(int depth, int r_depth, int cnt) {

  if (cnt == K || depth == N || r_depth == N || depth + r_depth == N) {
    return 0;
  }
  if (cnt > K) {
    return -INF;
  }
  if (memo[depth][r_depth][cnt] != -INF) {
    return memo[depth][r_depth][cnt];
  }
  int ret = -INF;
  ret = solve(depth, r_depth, K);
  if (v[depth] < 0) {
    ret = max(ret, max(solve(depth + 1, r_depth, cnt + 2),
                       solve(depth + 1, r_depth, cnt + 1) + v[depth]));
    if (cnt + 2 <= K)
      ret = max(ret, solve(depth + 1, r_depth, K));
  } else {
    ret = max(ret, solve(depth + 1, r_depth, cnt + 1) + v[depth]);
    ret = max(ret, solve(depth + 1, r_depth, K) + v[depth]);
  }
  if (rv[r_depth] < 0) {
    ret = max(ret, max(solve(depth, r_depth + 1, cnt + 2),
                       solve(depth, r_depth + 1, cnt + 1) + rv[r_depth]));
    if (cnt + 2 <= K)
      ret = max(ret, solve(depth, r_depth + 1, K));
  } else {
    ret = max(ret, solve(depth, r_depth + 1, cnt + 1) + rv[r_depth]);
    ret = max(ret, solve(depth, r_depth + 1, K) + rv[r_depth]);
  }
  return memo[depth][r_depth][cnt] = ret;
}

int main() {
  for (int i = 0; i < 52; ++i) {
    for (int j = 0; j < 52; ++j) {
      for (int k = 0; k < 52; ++k) {
        memo[i][j][k] = -INF;
      }
    }
  }
  cin >> N >> K;
  rep(i, N) {
    int a;
    cin >> a;
    v.pb(a);
  }
  rep(i, N) { rv.pb(v[N - i - 1]); }
  int ans = solve(0, 0, 0);
  cout << ans << endl;
  return 0;
}