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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<short, short> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int W, N, w[1005], v[1005];
P dp[1005][10005];

P solve(int i, int j) {
  if (dp[i][j].first >= 0)
    return dp[i][j];
  if (i == N)
    return P(0, 0);
  P res;
  if (j < w[i])
    res = solve(i + 1, j);
  else {
    P a = solve(i + 1, j);
    P b = solve(i + 1, j - w[i]);
    b.first += v[i];
    b.second += w[i];
    if (a.first != b.first)
      res = max(a, b);
    else
      res = min(a, b);
  }

  return dp[i][j] = res;
}

int main() {
  int cse = 0;
  while (cin >> W >> N, W) {
    cse++;
    MS(dp, -1);
    char c;
    REP(i, N) cin >> v[i] >> c >> w[i];
    cout << "Case " << cse << ":" << endl
         << solve(0, W).first << endl
         << solve(0, W).second << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int W, N, w[1005], v[1005];
P dp[1005][10005];

P solve(int i, int j) {
  if (dp[i][j].first >= 0)
    return dp[i][j];
  if (i == N)
    return P(0, 0);
  P res;
  if (j < w[i])
    res = solve(i + 1, j);
  else {
    P a = solve(i + 1, j);
    P b = solve(i + 1, j - w[i]);
    b.first += v[i];
    b.second += w[i];
    if (a.first != b.first)
      res = max(a, b);
    else
      res = min(a, b);
  }

  return dp[i][j] = res;
}

int main() {
  int cse = 0;
  while (cin >> W >> N, W) {
    cse++;
    MS(dp, -1);
    char c;
    REP(i, N) cin >> v[i] >> c >> w[i];
    cout << "Case " << cse << ":" << endl
         << solve(0, W).first << endl
         << solve(0, W).second << endl;
  }
}