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

#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
typedef pair<int, int> pii;

int dp[2][101][1001];

int main() {
  while (true) {
    int LIMIT, N, M, X;
    cin >> LIMIT >> N >> M >> X;
    if (LIMIT == 0 && N == 0 && M == 0 && X == 0) {
      break;
    }
    memset(dp, -1, sizeof(dp));
    vector<pii> secret(N);
    for (int i = 0; i < N; ++i) {
      string name;
      cin >> name >> secret[i].first >> secret[i].second;
      if (secret[i].first <= LIMIT) {
        dp[0][0][secret[i].first] =
            max(dp[0][0][secret[i].first], secret[i].second);
      }
    }
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < i; ++j) {
        pii s1 = secret[i], s2 = secret[j];
        int e = s1.first + s2.first;
        if (e <= LIMIT) {
          dp[0][0][e] = max(dp[0][0][e], s1.second + s2.second);
        }
      }
    }
    vector<pii> normal(M);
    for (int i = 0; i < M; ++i) {
      string name;
      cin >> name >> normal[i].first >> normal[i].second;
    }
    for (int i = 0; i < M; ++i) {
      int cur = i & 1, next = 1 - cur;
      pii g = normal[i];
      for (int j = 0; j <= i; ++j) {
        for (int k = 0; k <= LIMIT; ++k) {
          if (dp[cur][j][k] < 0) {
            continue;
          }
          dp[next][j][k] = max(dp[next][j][k], dp[cur][j][k]);
          int e = k + g.first;
          if (e <= LIMIT) {
            dp[next][j + 1][e] =
                max(dp[next][j + 1][e], dp[cur][j][k] + g.second);
          }
        }
      }
      for (int j = 0; j <= i; ++j) {
        for (int k = 0; k <= LIMIT; ++k) {
          dp[cur][j][k] = -1;
        }
      }
    }
    int answer = 0;
    for (int i = X; i <= M; ++i) {
      for (int j = 0; j <= LIMIT; ++j) {
        answer = max(answer, dp[M & 1][i][j]);
      }
    }
    cout << answer << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
typedef pair<int, int> pii;

int dp[101][101][1001];

int main() {
  while (true) {
    int LIMIT, N, M, X;
    cin >> LIMIT >> N >> M >> X;
    if (LIMIT == 0 && N == 0 && M == 0 && X == 0) {
      break;
    }
    memset(dp, 0, sizeof(dp));
    vector<pii> secret(N);
    for (int i = 0; i < N; ++i) {
      string name;
      cin >> name >> secret[i].first >> secret[i].second;
      if (secret[i].first <= LIMIT) {
        dp[0][0][secret[i].first] =
            max(dp[0][0][secret[i].first], secret[i].second);
      }
    }
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < i; ++j) {
        pii s1 = secret[i], s2 = secret[j];
        int e = s1.first + s2.first;
        if (e <= LIMIT) {
          dp[0][0][e] = max(dp[0][0][e], s1.second + s2.second);
        }
      }
    }
    vector<pii> normal(M);
    for (int i = 0; i < M; ++i) {
      string name;
      cin >> name >> normal[i].first >> normal[i].second;
    }
    for (int i = 0; i < M; ++i) {
      pii g = normal[i];
      for (int j = 0; j <= i; ++j) {
        for (int k = 0; k <= LIMIT; ++k) {
          dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
          int e = k + g.first;
          if (e <= LIMIT) {
            dp[i + 1][j + 1][e] =
                max(dp[i + 1][j + 1][e], dp[i][j][k] + g.second);
          }
        }
      }
    }
    int answer = 0;
    for (int i = X; i <= M; ++i) {
      for (int j = 0; j <= LIMIT; ++j) {
        answer = max(answer, dp[M][i][j]);
      }
    }
    cout << answer << endl;
  }
  return 0;
}