Problem p01402 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for ((i) = 0; (i) < (int)(n); ++(i))
#define foreach(c, itr)                                                        \\
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
const int inf = 1 << 28;

int limit, n, m, x;
int e0[102], s0[102], e1[102], s1[102];
int dp[102][1002];

void solve() {
  int i, j, k, l, ii, jj;
  string in;
  rep(i, n) cin >> in >> e0[i] >> s0[i];
  rep(i, m) cin >> in >> e1[i] >> s1[i];

  // memset(dp, -1, sizeof dp);
  rep(i, 102) fill(dp[i], dp[i] + 1002, -inf);
  // dp[A][B] :=
  // Aツ人ツづ個人ツづーBツ円ツづ古卍づつスツ湘ェツ債づ個催妥・ツづ個鳴楪堕ォツ度
  // ツづヲツづづp[0][0] = 0ツづづ按づゥ
  dp[0][0] = 0;
  for (i = 0; i < m; ++i) {
    for (j = i; j >= 0; --j) {
      for (k = e1[i]; k <= limit; ++k) {
        dp[j + 1][k] = max(dp[j + 1][k], dp[j][k - e1[i]] + s1[i]);
      }
    }
  }

  // ツシツーツクツδ個ッツトツアツーツテツィツスツトツづ個δ環スツトツづーツ催ャツづづつィツつュ
  /*
  int secret[21];
  memset(secret, 0, sizeof secret);
  rep (i,n) {
    secret[e0[i]] = max(secret[e0[i]], s0[i]);
    rep (j,n) if (i != j) {
      secret[e0[i] + e0[j]] = max(secret[e0[i] + e0[j]], s0[i] + s0[j]);
    }
  }*/

  int ans = -inf;
  for (i = x; i <= m; ++i) {
    for (j = 0; j <= limit; ++j)
      if (dp[i][j] >= 0) {
        /*
        for (k = 0; k < 21 && k + j <= limit; ++k) {
          ans = max(ans, dp[i][j] + secret[k]);
        }*/

        for (k = 0; k < n; ++k) {
          if (j + e0[k] <= limit)
            ans = max(ans, dp[i][j] + s0[k]);
          for (l = 0; l < k; ++l) {
            if (j + e0[k] + e0[l] <= limit) {
              ans = max(ans, dp[i][j] + s0[k] + s0[l]);
            }
          }
        }
      }
  }
  cout << ans << endl;
}

int main() {
  while (cin >> limit >> n >> m >> x, limit) {
    solve();
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for ((i) = 0; (i) < (int)(n); ++(i))
#define foreach(c, itr)                                                        \\
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)

int limit, n, m, x;
int e[202], s[202];
int dp[1002][202][4][102];

int rec(int money, int id, int attend_sec, int attend_st) {
  int ret = 0;
  if (id == m + n) {
    bool check = (attend_sec == 0 || attend_sec == 1);
    check = attend_st >= x;
    return (check) ? 0 : -10000;
  }
  if (dp[money][id][attend_sec][attend_st] >= 0) {
    return dp[money][id][attend_sec][attend_st];
  }

  if (id < n) {
    // secret artist
    if ((attend_sec == 0 || attend_sec == 1) && money - e[id] >= 0) {
      ret = max(rec(money - e[id], id + 1, attend_sec + 1, attend_st) + s[id],
                rec(money, id + 1, attend_sec, attend_st));
    } else {
      ret = rec(money, id + 1, attend_sec, attend_st);
    }
  } else {
    // standard artist
    if (money - e[id] >= 0) {
      ret = max(rec(money - e[id], id + 1, attend_sec, attend_st + 1) + s[id],
                rec(money, id + 1, attend_sec, attend_st));
    } else {
      ret = rec(money, id + 1, attend_sec, attend_st);
    }
  }
  return dp[money][id][attend_sec][attend_st] = ret;
}

void solve() {
  int i, j, k, l;
  string in;
  rep(i, n + m) cin >> in >> e[i] >> s[i];

  memset(dp, -1, sizeof dp);
  cout << rec(limit, 0, 0, 0) << endl;
  return;
  // sÏÈ§ñ
  // 1. rec(?,m+n,?,?) ÌÄÑoµÍK¸0
  // 2. A[eBXgªÙ¦éê (V[Nbg)
  //    rec(money,id,asc,ast) := max(rec(money-e[id],id+1,asc+1,ast) + e[id],
  //                                 rec(money,id+1,asc,ast))
  //    æèAid+1ÌÊ©çAidÌÊªª©é
  // ãLæèAQ»®ð±­
  // int dp[1002][202][102][102]

  // 1. æèú»
  for (i = 0; i <= limit; ++i)
    for (j = 0; j <= n; ++j)
      for (k = 0; k <= m; ++k) {
        dp[i][n + m][j][k] = 0;
      }
  // dp
  for (i = limit; i >= 0; --i) {
    for (j = m + n - 1; j >= 0; --j) {
      if (j < n) {
        // secret
      } else {
        // standard
      }
    }
  }
}

int main() {
  while (cin >> limit >> n >> m >> x, limit) {
    solve();
  }
  return 0;
}