Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < eps)
vector<int> pol;
vector<int> pol_odd;
int dp1[1000100], dp2[1000100];
int main() {
  for (int i = 0; i <= 1000000; i++)
    dp1[i] = dp2[i] = INF;
  dp1[0] = dp2[0] = 0;
  for (int i = 1;; i++) {
    int k = i * (i + 1) * (i + 2) / 6;
    if (k > 1000000)
      break;
    pol.pb(k);
    if (k % 2)
      pol_odd.pb(k);
  }
  for (int i = 0; i <= 1000000; i++) {
    for (int j = 0; j < pol.size(); j++) {
      if (i < pol[j])
        break;
      dp1[i] = min(dp1[i], dp1[i - pol[j]] + 1);
    }
  }
  for (int i = 0; i <= 1000000; i++) {
    for (int j = 0; j < pol_odd.size(); j++) {
      if (i < pol_odd[j])
        break;
      dp2[i] = min(dp2[i], dp2[i - pol_odd[j]] + 1);
    }
  }
  while (1) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;
    printf("%d %d\\n", dp1[n], dp2[n]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < eps)
vector<int> pol;
vector<int> pol_odd;
set<int> s;
int dp1[1000100], dp2[1000100];
int dfs1(int k) {
  if (dp1[k] != -1)
    return dp1[k];
  int res = INF;
  for (int i = 0; i < pol.size(); i++) {
    if (pol[i] > k)
      break;
    res = min(res, dfs1(k - pol[i]) + 1);
  }
  return dp1[k] = res;
}
int dfs2(int k) {
  if (dp2[k] != -1)
    return dp2[k];
  int res = INF;
  for (int i = 0; i < pol_odd.size(); i++) {
    if (pol_odd[i] > k)
      break;
    res = min(res, dfs2(k - pol_odd[i]) + 1);
  }
  return dp2[k] = res;
}
int main() {
  memset(dp1, -1, sizeof(dp1));
  memset(dp2, -1, sizeof(dp2));
  dp1[0] = dp2[0] = 0;
  s.insert(0);
  for (int i = 1;; i++) {
    int k = i * (i + 1) * (i + 2) / 6;
    if (k > 1000000)
      break;
    pol.pb(k);
    if (k % 2)
      pol_odd.pb(k);
  }
  while (1) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;
    printf("%d %d\\n", dfs1(n), dfs2(n));
  }
  return 0;
}