alexjercan/bugnet huggingface dataset:
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define INF 1e9
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long

using namespace std;
int main() {
  int dp[1000010] = {};
  int dp2[1000010] = {};
  for (int i = 1; i <= 1000000; i++) {
    dp[i] = INF;
    dp2[i] = INF;
  }
  vector<ll> gu;
  vector<ll> ki;
  ll cnt = 1;
  while (1) {
    if ((cnt * (cnt + 1) * (cnt + 2) / 6) > 1000000)
      break;
    if (((cnt * (cnt + 1) * (cnt + 2)) / 6) % 2) {
      ki.pb(cnt * (cnt + 1) * (cnt + 2) / 6);
      dp[ki[ki.size() - 1]] = 1;
      dp2[ki[ki.size() - 1]] = 1;
    } else {
      gu.pb(cnt * (cnt + 1) * (cnt + 2) / 6);
      dp[gu[gu.size() - 1]] = 1;
    }
    cnt++;
  }
  dp[1] = 1;
  dp2[1] = 1;
  for (int i = 1; i < 1000000; i++) {
    for (int j = 0; j < gu.size(); j++) {
      if (i + gu[j] > 1000000)
        break;
      dp[i + gu[j]] = min(dp[i + gu[j]], (dp[i] + 1));
    }
    for (int j = 0; j < ki.size(); j++) {
      if (i + ki[j] > 1000000)
        break;
      dp[i + ki[j]] = min(dp[i + ki[j]], (dp[i] + 1));
      dp2[i + ki[j]] = min(dp2[i + ki[j]], (dp2[i] + 1));
    }
  }
  int n;
  while (cin >> n, n) {
    cout << dp[n] << " " << dp2[n] << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define INF 1e9
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long

using namespace std;
int main() {
  int dp[1000010] = {};
  int dp2[1000010] = {};
  for (int i = 1; i <= 1000000; i++) {
    dp[i] = INF;
    dp2[i] = INF;
  }
  vector<ll> gu;
  vector<ll> ki;
  ll cnt = 1;
  while (1) {
    if ((cnt * (cnt + 1) * (cnt + 2) / 6) > 1000000)
      break;
    if (((cnt * (cnt + 1) * (cnt + 2)) / 6) % 2) {
      ki.pb(cnt * (cnt + 1) * (cnt + 2) / 6);
      dp[ki[ki.size() - 1]] = 1;
      dp2[gu[gu.size() - 1]] = 1;
    } else {
      gu.pb(cnt * (cnt + 1) * (cnt + 2) / 6);
      dp[gu[gu.size() - 1]] = 1;
    }
    cnt++;
  }
  dp[1] = 1;
  dp2[1] = 1;
  for (int i = 1; i < 1000000; i++) {
    for (int j = 0; j < gu.size(); j++) {
      if (i + gu[j] > 1000000)
        break;
      dp[i + gu[j]] = min(dp[i + gu[j]], (dp[i] + 1));
    }
    for (int j = 0; j < ki.size(); j++) {
      if (i + ki[j] > 1000000)
        break;
      dp[i + ki[j]] = min(dp[i + ki[j]], (dp[i] + 1));
      dp2[i + ki[j]] = min(dp2[i + ki[j]], (dp2[i] + 1));
    }
  }
  int n;
  while (cin >> n, n) {
    cout << dp[n] << " " << dp2[n] << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define INF (int)(1e9)
#define MAXN 1000001
using namespace std;

int sankaku[500];
int simen[500];
int simen2[500];
int maxi, maxj;
int dp[MAXN];
int dp2[MAXN];

void solve() {
  int i, j, t;
  int n = 1111111;
  for (i = 0; i < MAXN; i++) {
    dp[i] = dp2[i] = INF;
  }
  dp[0] = dp2[0] = 0;
  for (i = 0; i <= maxj; i++) {
    t = simen[i];
    for (j = t; j < MAXN; j++) {
      dp[j] = min(dp[j], dp[j - t] + 1);
    }
  }

  for (i = 0; i <= maxi; i++) {
    t = simen2[i];
    // if(t%2==0) continue;
    for (j = t; j < MAXN; j++) {
      dp2[j] = min(dp2[j], dp2[j - t] + 1);
    }
  }
}

int main(void) {
  int n, i, j, t;
  t = i = j = 1;
  sankaku[0] = 1;
  simen[0] = 1;
  simen2[0] = 1;
  while (simen[j - 1] < MAXN) {
    sankaku[j] = sankaku[j - 1] + j + 1;
    simen[j] = simen[j - 1] + sankaku[j];
    t = simen[j++];
    if (t % 2 == 1)
      simen2[i++] = t;
  }
  maxi = i;
  maxj = j;

  solve();
  /*
  for(i=0;i<10;i++){
    printf("%d : %d   %d\\n",i,simen[i],simen2[i]);
  }
  */
  while (scanf("%d", &n), n) {
    cout << dp[n] << " " << dp2[n] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
#define INF (int)(1e9)
using namespace std;

int sankaku[111];
int simen[111];
int simen2[111];
int maxi, maxj;
int dp[111111];
int dp2[111111];
void solve() {
  int i, j, t;
  int n = 111111;
  for (i = 0; i < n; i++) {
    dp[i] = dp2[i] = INF;
  }
  dp[0] = dp2[0] = 0;
  for (i = 0; i <= maxj; i++) {
    t = simen[i];
    for (j = t; j <= n; j++) {
      dp[j] = min(dp[j], dp[j - t] + 1);
    }
  }

  for (i = 0; i <= maxi; i++) {
    t = simen2[i];
    for (j = t; j <= n; j++) {
      dp2[j] = min(dp2[j], dp2[j - t] + 1);
    }
  }
}

int main(void) {
  int n, i, j, t;
  t = i = j = 1;
  sankaku[0] = 1;
  simen[0] = 1;
  simen2[0] = 1;
  while (simen[j - 1] < 111111) {
    sankaku[j] = sankaku[j - 1] + j + 1;
    simen[j] = simen[j - 1] + sankaku[j];
    t = simen[j++];
    if (t % 2 == 1)
      simen2[i++] = t;
  }
  maxi = i;
  maxj = j;
  solve();
  while (scanf("%d", &n), n) {
    cout << dp[n] << " " << dp2[n] << endl;
  }

  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for ((i) = 0; (i) < (int)(n); (i)++)
#define foreach(itr, c)                                                        \\
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)

#define INF (1 << 29)

int N;
int a[1010], dp[2][1000010];

void make() {
  int n;
  for (n = 1; n <= 1000; n++)
    a[n] = n * (n + 1) * (n + 2) / 6;
}

int main() {
  int i, j, k;

  make();

  rep(i, 2) rep(j, 1000010) dp[i][j] = INF;

  dp[0][0] = dp[1][0] = 0;
  for (i = 1; a[i] <= 1000000 && i < 1010; i++) {
    for (j = 0; j <= 1000000; j++) {
      if (j - a[i] >= 0)
        dp[0][j] = min(dp[0][j], dp[0][j - a[i]] + 1);
      if (a[i] % 2 == 1 && j - a[i] >= 0)
        dp[1][j] = min(dp[1][j], dp[1][j - a[i]] + 1);
    }
  }

  for (;;) {
    scanf("%d", &N);
    if (N == 0)
      break;
    cout << dp[0][N] << ' ' << dp[1][N] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for ((i) = 0; (i) < (int)(n); (i)++)
#define foreach(itr, c)                                                        \\
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)

#define INF (1 << 29)

int N;
int a[1010], dp[2][1000010];

void make() {
  int n;
  for (n = 1; n <= 1000; n++)
    a[n] = n * (n + 1) * (n + 2) / 6;
}

int main() {
  int i, j, k;

  make();

  for (;;) {
    scanf("%d", &N);
    if (N == 0)
      break;

    rep(i, 2) rep(j, 1000010) dp[i][j] = INF;

    dp[0][0] = dp[1][0] = 0;
    for (i = 1; a[i] <= N && i < 1010; i++) {
      for (j = 0; j <= N; j++) {
        if (j - a[i] >= 0)
          dp[0][j] = min(dp[0][j], dp[0][j - a[i]] + 1);
        if (a[i] % 2 == 1 && j - a[i] >= 0)
          dp[1][j] = min(dp[1][j], dp[1][j - a[i]] + 1);
      }
    }

    cout << dp[0][N] << ' ' << dp[1][N] << endl;
  }

  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <iostream>
#include <list>
#include <set>
#include <string>
#define foreach(t, p) for (t::iterator it = p.begin(); it != p.end(); ++it)
#define rforeach(t, p)                                                         \\
  for (t::reverse_iterator it = p.rbegin(); it != p.rend(); ++it)
using namespace std;
#define REP(n) for (int i = 0; i < n; i++)
#define N 1000001
#define POL 180
int pol[POL];
int pol2[POL];
int dp1[N];
int dp2[N];
int num;

void solve1() {
  REP(N) dp1[i] = N;
  dp1[0] = 0;
  for (int k = 0; k < N; k++) {
    for (int j = 0; j < POL; j++)
      if (k + pol[j] <= N)
        dp1[k + pol[j]] = min(dp1[k + pol[j]], dp1[k] + 1);
  }
}

void solve2() {
  REP(N) dp2[i] = N;
  dp2[0] = 0;
  for (int i = 0; i < N; i++) {
    //                cout << i << " "  << " " << dp2[i] << endl;
    for (int j = 0; j < num; j++) {
      if (i + pol2[j] <= N) {
        dp2[i + pol2[j]] = min(dp2[i + pol2[j]], dp2[i] + 1);
      }
    }
  }
}

int main() {
  num = 0;
  REP(POL) {
    pol[i] = (i + 1) * (i + 2) * (i + 3) / 6;
    if (pol[i] % 2 == 1)
      pol2[num++] = pol[i];
  }
  solve1();
  solve2();
  //    REP(30) cout << dp1[i];
  int q;
  while (cin >> q && q) {
    cout << dp1[q] << " " << dp2[q] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <iostream>
#include <list>
#include <set>
#include <string>
#define foreach(t, p) for (t::iterator it = p.begin(); it != p.end(); ++it)
#define rforeach(t, p)                                                         \\
  for (t::reverse_iterator it = p.rbegin(); it != p.rend(); ++it)
using namespace std;
#define REP(n) for (int i = 0; i < n; i++)
#define N 1000001
#define POL 180
int pol[POL];
int pol2[POL];
int dp1[N];
int dp2[N];
int num;

void solve1() {
  REP(N) dp1[i] = N;
  dp1[0] = 0;
  REP(5) {
    for (int k = 0; k < N; k++) {
      if (dp1[k] == i) {
        for (int j = 0; j < POL; j++)
          if (k + pol[j] <= N)
            dp1[k + pol[j]] = min(dp1[k + pol[j]], dp1[k] + 1);
      }
    }
  }
}

int solve2(int n) {
  REP(N) dp2[i] = N;
  dp2[0] = 0;
  for (int cnt = 0;; cnt++) {
    for (int i = 0; i < N; i++) {
      if (dp2[i] == cnt) {
        //                cout << i << " "  << " " << dp2[i] << endl;
        for (int j = 0; j < num; j++) {
          if (i + pol2[j] <= N) {
            dp2[i + pol2[j]] = min(dp2[i + pol2[j]], dp2[i] + 1);
            if (i + pol2[j] == n)
              return dp2[i + pol2[j]];
          }
        }
      }
    }
  }
}

int main() {
  num = 0;
  REP(POL) {
    pol[i] = (i + 1) * (i + 2) * (i + 3) / 6;
    if (pol[i] % 2 == 1)
      pol2[num++] = pol[i];
  }
  solve1();
  //    REP(30) cout << dp1[i];
  int q;
  while (cin >> q && q) {
    cout << dp1[q] << " " << solve2(q) << endl;
  }

  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define INF 1 << 28
using namespace std;

int p[1111111];
int memo[1111111];
int memo2[1111111];

int solve(int n) {

  if (!n)
    return 0;
  if (memo[n])
    return memo[n];

  int ans = INF;
  for (int i = 199; i > 0; i--)
    if (n - p[i] >= 0)
      ans = min(ans, solve(n - p[i]) + 1);

  return memo[n] = ans;
}

int solve2(int n) {

  if (!n)
    return 0;
  if (memo2[n])
    return memo2[n];

  int ans = INF;
  for (int i = 199; i > 0; i--)
    if (n - p[i] >= 0 && p[i] & 1)
      ans = min(ans, solve2(n - p[i]) + 1);

  return memo2[n] = ans;
}

int main() {

  int n;

  for (int i = 0; i < 200; i++)
    p[i] = i * (i + 1) * (i + 2) / 6;
  while (cin >> n, n)
    cout << solve(n) << " " << solve2(n) << endl;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#define INF 1 << 28
using namespace std;

int p[1000001];
int memo[1000001];
int memo2[1000001];

int solve(int n, int count) {

  if (!n)
    return 0;
  if (n <= 0 || !count)
    return INF;
  if (memo[n])
    return memo[n];

  int ans = INF;
  for (int i = 0; p[i] <= n; i++)
    ans = min(ans, solve(n - p[i], count - 1) + 1);

  return memo[n] = ans;
}

int solve2(int n) {

  if (!n)
    return 0;
  if (n < 0)
    return INF;
  if (memo2[n])
    return memo2[n];

  int ans = INF;
  for (int i = 0; p[i] <= n; i++) {
    if (!(p[i] % 2))
      continue;
    ans = min(ans, solve2(n - p[i]) + 1);
  }

  return memo2[n] = ans;
}

int main() {

  int n;

  for (int i = 0; i <= 1000000; i++)
    p[i] = i * (i + 1) * (i + 2) / 6;
  // for(int i=1;i<=1000000;i++) solve(i, 5), solve2(i);

  while (cin >> n, n)
    cout << solve(n, 5) << " " << solve2(n) << endl;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, j, k) for (int i = (int)j; i < (int)k; i++)
#define itrep(i, x) for (auto i = (x).begin(); i != (x).end(); i++)
#define Sort(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define vec vector
#define INF (int)1e9
#define INFL 1e18
#define MOD 1000000007
#define pb push_back
#define MP make_pair
#define PI 3.1415926535
typedef long long int ll;
typedef std::pair<int, int> P;
int D = 1;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};

using namespace std;

int main() {
  vector<int> v, oddv;
  for (ll i = 1;; i++) {
    if (i * (i + 1) * (i + 2) / 6 <= 2000000) {
      v.pb(i * (i + 1) * (i + 2) / 6);
    } else
      break;
  }
  int ans1[1000010], ans2[1000010];
  rep(i, 0, 1000010) ans1[i] = ans2[i] = INF;
  ans1[0] = 0;
  ans2[0] = 0;
  rep(i, 0, 1000000) {
    ans1[i] = i;
    ans2[i] = i;
    for (int j = 0; i >= v[j]; j++) {
      ans1[i] = min(ans1[i - v[j]] + 1, ans1[i]);
      if (v[j] % 2 == 1)
        ans2[i] = min(ans2[i], ans2[i - v[j]] + 1);
    }
  }

  int n;
  while (cin >> n && n) {
    cout << ans1[n] << " " << ans2[n] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, j, k) for (int i = (int)j; i < (int)k; i++)
#define itrep(i, x) for (auto i = (x).begin(); i != (x).end(); i++)
#define Sort(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define vec vector
#define INF (int)1e9
#define INFL 1e18
#define MOD 1000000007
#define pb push_back
#define MP make_pair
#define PI 3.1415926535
typedef long long int ll;
typedef std::pair<int, int> P;
int D = 1;
int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};

using namespace std;

int main() {
  vector<int> v, oddv;
  for (ll i = 1;; i++) {
    if (i * (i + 1) * (i + 2) / 6 <= 1000000) {
      v.pb(i * (i + 1) * (i + 2) / 6);
    } else
      break;
  }
  int ans1[1000010], ans2[1000010];
  rep(i, 0, 1000010) ans1[i] = ans2[i] = INF;
  ans1[0] = 0;
  ans2[0] = 0;
  rep(i, 0, 1000000) {
    ans1[i] = i;
    ans2[i] = i;
    for (int j = 0; i >= v[j]; j++) {
      ans1[i] = min(ans1[i - v[j]] + 1, ans1[i]);
      if (v[j] % 2 == 1)
        ans2[i] = min(ans2[i], ans2[i - v[j]] + 1);
    }
  }

  int n;
  while (cin >> n && n) {
    cout << ans1[n] << " " << ans2[n] << endl;
  }

  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
static const int MAX = 1000001;
static const int INF = 1500000;
int Min[MAX]; // memo for speeding things up
int odd[MAX]; // same

// this is the improved version to 1167.cpp
void memo() {
  /*
    Same as problem 17.1 in the book Algo and Data Struct
    http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_A

   */

  /*
    Made the program substantially faster by making a memo of two different
    array with one loop



   */

  Min[0] = 0;
  Min[1] = 1;
  odd[0] = 0;
  odd[1] = 1;
  int i, j;
  for (i = 0; i < MAX; i++) {
    for (j = 1; j < MAX; j++) {
      int b = (j * (j + 1) * (j + 2)) / 6;
      if (i + b > MAX)
        break;
      Min[i + b] = min(Min[i + b], Min[i] + 1);
      if (b % 2) {
        odd[i + b] = min(odd[i + b], odd[i] + 1);
      }
    }
  }
}

int main() {
  int n;

  int i;
  memset(Min, INF, sizeof(Min));
  memset(odd, INF, sizeof(odd));
  memo();

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

    cout << Min[n] << " " << odd[n] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
static const int MAX = 1000000;
int Pollock[MAX];
int T[MAX];
static const int INF(1 << 29);
int min(int n) {
  /*
    Same as problem 17.1 in the book Algo and Data Struct
    http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DPL_1_A

   */

  int i = 1;
  Pollock[i] = 1;
  while (Pollock[i] <= n) {
    i++;
    Pollock[i] = (i * (i + 1) * (i + 2) / 6);
  }
  // Pollock to i-1 th element is used
  int j, k;
  for (j = 0; j < MAX; j++)
    T[j] = INF;
  T[0] = 0;
  for (j = 1; j < i; j++) {
    for (k = Pollock[j]; k <= n; k++) {
      T[k] = min(T[k], T[k - Pollock[j]] + 1);
    }
  }
  // DP?????£???????°????

  return T[n];
}

int is_odd(int n) {
  if (n % 2 == 1)
    return 1;
  return 0;
}

int cal_pollock(int i) { return (i * (i + 1) * (i + 2) / 6); }
int odd(int n) {
  int i = 1;
  Pollock[i] = 1;
  int j = 1;
  while (cal_pollock(i) <= n) {
    if (is_odd(cal_pollock(i))) {
      Pollock[j] = cal_pollock(i);
      j++;
    }
    i++;
  }
  // Pollock to i-1 th element is used
  int k, l;
  for (l = 0; l < MAX; l++)
    T[l] = INF;
  T[0] = 0;
  for (l = 1; l < j; l++) {
    for (k = Pollock[l]; k <= n; k++) {
      T[k] = min(T[k], T[k - Pollock[l]] + 1);
    }
  }
  // DP?????£???????°????

  return T[n];
}

int main() {
  int n;
  while (1) {
    cin >> n;
    if (n == 0)
      break;

    cout << min(n) << " " << odd(n) << endl;
  }

  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
typedef pair<int, int> P;

int d[1000001], d2[1000001];

int main() {
  int tet[1001];
  for (int i = 1; i <= 1000; i++) {
    tet[i] = i * (i + 1) * (i + 2) / 6;
  }

  fill(d, d + 1000001, INF);
  d[0] = 0;
  for (int i = 1; tet[i] <= 1000000; i++) {
    for (int j = 0; j + tet[i] <= 1000000; j++) {
      d[j + tet[i]] = min(d[j + tet[i]], d[j] + 1);
    }
  }

  fill(d2, d2 + 1000001, INF);
  d2[0] = 0;
  for (int i = 1; tet[i] <= 1000000; i++) {
    if (tet[i] % 2 == 0)
      continue;
    for (int j = 0; j + tet[i] <= 1000000; j++) {
      d2[j + tet[i]] = min(d2[j + tet[i]], d2[j] + 1);
    }
  }

  int n;
  while (scanf("%d", &n), n) {
    cout << d[n] << " " << d2[n] << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
typedef pair<int, int> P;

int d[1000001], d2[1000001];

int main() {
  int tet[1001];
  for (int i = 1; i <= 1000; i++) {
    tet[i] = i * (i + 1) * (i + 2) / 6;
  }

  fill(d, d + 1000001, INF);
  queue<P> que;
  for (que.push(P(0, 0)); que.size(); que.pop()) {
    P p = que.front();
    if (d[p.first] <= p.second)
      continue;
    d[p.first] = p.second;
    for (int i = 1; p.first + tet[i] <= 1000000; i++) {
      if (d[p.first + tet[i]] <= p.second + 1)
        continue;
      que.push(P(p.first + tet[i], p.second + 1));
    }
  }

  fill(d2, d2 + 1000001, INF);
  while (!que.empty())
    que.pop();
  for (que.push(P(0, 0)); que.size(); que.pop()) {
    P p = que.front();
    if (d2[p.first] <= p.second)
      continue;
    d2[p.first] = p.second;

    for (int i = 1; p.first + tet[i] <= 1000000; i++) {
      if (tet[i] % 2 == 0)
        continue;
      if (d2[p.first + tet[i]] <= p.second + 1)
        continue;
      que.push(P(p.first + tet[i], p.second + 1));
    }
  }

  int n;
  while (scanf("%d", &n), n) {
    cout << d[n] << " " << d2[n] << endl;
  }

  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define N 1000000

int main() {
  int dp[N] = {0}, dp_odd[N] = {0};

  for (int i = 1; i < N; i++)
    dp[i] = dp_odd[i] = N;

  for (int i = 1; i < N; i++) {
    int t = i * (i + 1) * (i + 2) / 6;
    if (t > N)
      break;
    for (int j = t; j < N; j++)
      dp[j] = min(dp[j], dp[j - t] + 1);
    if (t % 2 == 1)
      for (int j = t; j < N; j++)
        dp_odd[j] = min(dp_odd[j], dp_odd[j - t] + 1);
  }

  int n;
  while (cin >> n && n) {
    cout << dp[n] << " " << dp_odd[n] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define N 1000000

int main() {
  vector<int> tn;
  for (int i = 1; i < 100; i++) {
    tn.push_back(i * (i + 1) * (i + 2) / 6);
  }

  int dp[N] = {0};
  int dp_odd[N] = {0};

  for (int i = 0; i < N; i++) {
    dp[i] = -1;
    dp_odd[i] = -1;
  }
  for (int t : tn) {
    dp[t] = 1;
    if (t % 2 == 1)
      dp_odd[t] = 1;
  }

  reverse(tn.begin(), tn.end());

  for (int t : tn) {
    for (int i = N - 1; i >= 1; i--) {
      if (dp[i] == -1)
        continue;
      for (int j = 1; i + j * t < N && dp[i + j * t] == -1; j++) {
        if (dp[i + j * t] == -1)
          dp[i + j * t] = dp[i] + j;
        else
          dp[i + j * t] = min(dp[i + j * t], dp[i] + j);
      }

      if (t % 2 == 1) {
        if (dp_odd[i] == -1)
          continue;
        for (int j = 1; i + j * t < N && dp_odd[i + j * t] == -1; j++) {
          if (dp_odd[i + j * t] == -1)
            dp_odd[i + j * t] = dp_odd[i] + j;
          else
            dp_odd[i + j * t] = min(dp_odd[i + j * t], dp_odd[i] + j);
        }
      }
    }
  }

  int n;
  while (cin >> n && n) {
    cout << dp[n] << " " << dp_odd[n] << endl;
  }

  return 0;
}