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

#include <algorithm>
#include <cassert>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF INT_MAX / 3
#define LINF LLONG_MAX / 3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cout << #x << ":" << x << endl
#define debug2(x, y) cout << #x "," << #y ":" << x << "," << y << endl

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

vi num;
vi num_odd;
int N;
int N_odd;

void init() {
  int t = 1;
  int i = 1;
  while (t < 1000000) {
    num.EB(t);
    i++;
    t = i * (i + 1) * (i + 2) / 6;
  }
  N = num.size();

  for (i = 0; i < N; i++) {
    if (num[i] % 2 == 1)
      num_odd.emplace_back(num[i]);
  }
  N_odd = num_odd.size();
}

void solve(int n) {
  vi dp(n + 10, INF);
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = num[i]; j <= n; j++) {
      dp[j] = min(dp[j], dp[j - num[i]] + 1);
    }
  }

  cout << dp[n] << " ";

  dp = vector<int>(n + 10, INF);
  dp[0] = 0;
  for (int i = 0; i < N_odd; i++) {
    for (int j = num_odd[i]; j <= n; j++) {
      dp[j] = min(dp[j], dp[j - num_odd[i]] + 1);
    }
  }
  cout << dp[n] << endl;
}

int main() {
  init();
  int n;

  while (cin >> n) {
    if (n == 0)
      break;
    solve(n);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF INT_MAX / 3
#define LINF LLONG_MAX / 3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cout << #x << ":" << x << endl
#define debug2(x, y) cout << #x "," << #y ":" << x << "," << y << endl

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

vi num;
vi num_odd;
int N;
int N_odd;

void init() {
  int t = 1;
  int i = 1;
  while (t < 1000000) {
    num.EB(t);
    i++;
    t = i * (i + 1) * (i + 2) / 6;
  }
  N = num.size();

  for (i = 0; i < N; i++) {
    if (num[i] % 2 == 1)
      num_odd.emplace_back(num[i]);
  }
  N_odd = num_odd.size();
}

void solve(int n) {
  vvi dp(N + 1, vi(n + 10, INF));
  for (int i = 0; i < N; i++)
    dp[i][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= n; j++) {
      if (j - num[i] >= 0) {
        dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - num[i]] + 1);
      } else {
        dp[i + 1][j] = dp[i][j];
      }
    }
  }

  cout << dp[N][n] << " ";

  dp.shrink_to_fit();
  dp.resize(N_odd + 1, vi(n + 10, INF));

  for (int i = 0; i < N_odd; i++)
    dp[i][0] = 0;

  for (int i = 0; i < N_odd; i++) {
    for (int j = 0; j <= n; j++) {
      if (j - num_odd[i] >= 0)
        dp[i + 1][j] = min(dp[i][j], dp[i + 1][j - num_odd[i]] + 1);
      else
        dp[i + 1][j] = dp[i][j];
    }
  }
  // for(int i=0;i<5;i++){
  //     for(int j=0;j<=n;j++){
  //         printf("%02d ",dp[i][j]);
  //     }
  //     cout << endl;
  // }
  cout << dp[N_odd][n] << endl;
}

int main() {
  init();
  int n;

  while (cin >> n) {
    if (n == 0)
      break;
    solve(n);
  }
}