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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define M_N 1000
#define M_W 1000

typedef pair<int, int> ii; //?????????, ?????????

int W, N;
int v[M_N], w[M_N];
int dp[M_N + 1][M_W + 1][2];

//?????????j??\\?????¶??????i????????\\????????????????????????
ii dfs(int i, int j) {
  if (dp[i][j][0] != -1)
    return ii(dp[i][j][0], dp[i][j][1]);

  if (i == N) {
    //?????°??¶?????????????????¨???
    return ii(dp[i][j][0] = 0, dp[i][j][1] = 0);
  } else if (j < w[i]) {
    // i??????????????????????????¶?????§??\\??????????????¨???
    return ii(dp[i][j][0] = dfs(i + 1, j).first,
              dp[i][j][1] = dfs(i + 1, j).second);
  } else {
    //???????????\\???????????\\??????????????§?????????????????????????????§??¨?????????
    int a = dfs(i + 1, j).first, b = dfs(i + 1, j - w[i]).first + v[i];
    if (a >= b) //??\\?????????
      return ii(dp[i][j][0] = a, dp[i][j][1] = dfs(i + 1, j).second);
    else //??\\?????? ??\\????????????????????¶??????w[i]?????????
      return ii(dp[i][j][0] = b,
                dp[i][j][1] = dfs(i + 1, j - w[i]).second + w[i]);
  }
}

int main() {
  for (int h = 1; cin >> W, W; h++) {
    cin >> N;
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < N; i++) {
      char c;
      cin >> v[i] >> c >> w[i];
    }
    cout << "Case " << h << ":" << endl;
    cout << dfs(0, W).first << endl;
    cout << dfs(0, W).second << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define M_N 1000
#define M_W 10000

typedef pair<int, int> ii; //?????????, ?????????

int W, N;
int v[M_N], w[M_N];
int dp[M_N + 1][M_W + 1][2];

//?????????j??\\?????¶??????i????????\\????????????????????????
ii dfs(int i, int j) {
  if (dp[i][j][0] != -1)
    return ii(dp[i][j][0], dp[i][j][1]);

  if (i == N) {
    //?????°??¶?????????????????¨???
    return ii(dp[i][j][0] = 0, dp[i][j][1] = 0);
  } else if (j < w[i]) {
    // i??????????????????????????¶?????§??\\??????????????¨???
    return ii(dp[i][j][0] = dfs(i + 1, j).first,
              dp[i][j][1] = dfs(i + 1, j).second);
  } else {
    //???????????\\???????????\\??????????????§?????????????????????????????§??¨?????????
    int a = dfs(i + 1, j).first, b = dfs(i + 1, j - w[i]).first + v[i];
    if (a > b)
      return ii(dp[i][j][0] = a, dp[i][j][1] = dfs(i + 1, j).second);
    else
      return ii(dp[i][j][0] = b,
                dp[i][j][1] = dfs(i + 1, j - w[i]).second + w[i]);
  }
}

int main() {
  for (int h = 0; cin >> W, W; h++) {
    cin >> N;
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < N; i++) {
      char c;
      cin >> v[i] >> c >> w[i];
    }
    cout << "Case " << h << ":" << endl;
    cout << dfs(0, W).first << endl;
    cout << dfs(0, W).second << endl;
  }
  return 0;
}