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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;

struct Edge {
  int to, from, cost;
};

#ifdef DEBUG
#define debug cout
#else
stringstream __ss__;
#define debug __ss__
#endif

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    debug << *i << " ";
  debug << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; i--)
#define repn(i, m, n) for (int i = (m); i <= (n); ++i)
#define repnd(i, m, n) for (int i = (n)-1; i >= (m); i--)
#define rep0(i, n) for (i = 0; i < (n); ++i)
#define all(n) n.begin(), n.end()
#define sz(n) ((int)(n).size())
#define IL for (;;)
#define MP make_pair
#define PB push_back
#define SS stringstream
#define X second
#define Y first
#define PUTLINE debug << "LINE:" << __LINE__ << endl;

const int INF = 2147483647 / 3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[] = {1, -1, 0, 0, 1, -1, 1, -1, 0};
const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1, 0};

int se[110], sc[110], e[110], c[110];
int dp[110][1010][2];
int mx[1010];

int main() {
  int limit, n, m, x;
  IL {
    cin >> limit >> n >> m >> x;
    if (limit == 0 && n == 0 && m == 0 && x == 0)
      break;
    string name;
    rep(i, n) { cin >> name >> se[i] >> sc[i]; }
    rep(i, m) { cin >> name >> e[i] >> c[i]; }
    memset(dp, 0, sizeof(dp));
    rep(i, m) {
      repn(k, 0, i + 1) {
        repn(j, 0, limit) {
          if (j < e[i] || k == 0 || k > 1 && dp[k - 1][j - e[i]][0] == 0)
            dp[k][j][1] = dp[k][j][0];
          else
            dp[k][j][1] = max(dp[k][j][0], dp[k - 1][j - e[i]][0] + c[i]);
        }
      }
      repn(k, 0, i + 1) repn(j, 0, limit) {
        dp[k][j][0] = dp[k][j][1];
        dp[k][j][1] = 0;
      }
    }
    memset(mx, 0, sizeof(mx));
    repn(i, 0, limit) {
      repn(j, x, m) { chmax(mx[i], dp[j][i][0]); }
      debug << i << " " << mx[i] << endl;
    }
    int res = 0;
    rep(i, n) repn(j, i + 1, n - 1) {
      if (limit < se[i] + se[j])
        continue;
      chmax(res, mx[limit - se[i] - se[j]] + sc[i] + sc[j]);
    }
    rep(i, n) {
      if (limit < se[i])
        continue;
      chmax(res, mx[limit - se[i]] + sc[i]);
    }
    cout << res << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<string> vstring;
typedef vector<pint> vpint;

struct Edge {
  int to, from, cost;
};

#ifdef DEBUG
#define debug cout
#else
stringstream __ss__;
#define debug __ss__
#endif

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    debug << *i << " ";
  debug << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repd(i, n) for (int i = (n)-1; i >= 0; i--)
#define repn(i, m, n) for (int i = (m); i <= (n); ++i)
#define repnd(i, m, n) for (int i = (n)-1; i >= (m); i--)
#define rep0(i, n) for (i = 0; i < (n); ++i)
#define all(n) n.begin(), n.end()
#define sz(n) ((int)(n).size())
#define IL for (;;)
#define MP make_pair
#define PB push_back
#define SS stringstream
#define X second
#define Y first
#define PUTLINE debug << "LINE:" << __LINE__ << endl;

const int INF = 2147483647 / 3;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[] = {1, -1, 0, 0, 1, -1, 1, -1, 0};
const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1, 0};

int se[110], sc[110], e[110], c[110];
int dp[110][1010][110];
int mx[1010];

int main() {
  int limit, n, m, x;
  IL {
    cin >> limit >> n >> m >> x;
    if (limit == 0 && n == 0 && m == 0 && x == 0)
      break;
    string name;
    rep(i, n) { cin >> name >> se[i] >> sc[i]; }
    rep(i, m) { cin >> name >> e[i] >> c[i]; }
    memset(dp, 0, sizeof(dp));
    rep(i, m) {
      repn(k, 0, i + 1) {
        repn(j, 0, limit) {
          if (j < e[i] || k == 0)
            dp[k][j][i + 1] = dp[k][j][i];
          else
            dp[k][j][i + 1] = max(dp[k][j][i], dp[k - 1][j - e[i]][i] + c[i]);
        }
      }
    }
    memset(mx, 0, sizeof(mx));
    repn(i, 0, limit) {
      repn(j, x, m) { chmax(mx[i], dp[j][i][m]); }
      debug << i << " " << mx[i] << endl;
    }
    int res = 0;
    rep(i, n) repn(j, i + 1, n - 1) {
      chmax(res, mx[limit - se[i] - se[j]] + sc[i] + sc[j]);
    }
    rep(i, n) { chmax(res, mx[limit - se[i]] + sc[i]); }
    cout << res << endl;
  }
  return 0;
}