alexjercan/bugnet huggingface dataset:
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int prime[200], dprime[100];
int dp[2000001], ddp[2000001];
int main() {
  int a = 1, b = 2, count = 1, dcount = 1;
  prime[0] = 1, dprime[0] = 1;
  while (true) {
    a += b;
    b++;
    if (prime[count - 1] + a >= 1000000)
      break;
    if ((prime[count - 1] + a) % 2 == 1) {
      dprime[dcount] = prime[count - 1] + a;
      dcount++;
    }
    prime[count] = prime[count - 1] + a;
    count++;
  }
  for (int i = 0; i <= 2000000; i++)
    dp[i] = i % 4 + i / 4;
  for (int i = 0; i <= 999999; i++) {
    for (int j = 0; j < count; j++)
      dp[i + prime[j]] = min(dp[i + prime[j]], dp[i] + 1);
  }
  for (int i = 0; i <= 2000000; i++)
    ddp[i] = i % 35 + i / 35;
  for (int i = 0; i <= 999999; i++) {
    for (int j = 0; j < dcount; j++)
      ddp[i + dprime[j]] = min(ddp[i + dprime[j]], ddp[i] + 1);
  }
  int n;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    cout << dp[n] << " " << ddp[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int prime[200], dprime[100];
int dp[2000001];
int main() {
  int a = 1, b = 2, count = 1, dcount = 1;
  prime[0] = 1, dprime[0] = 1;
  while (true) {
    a += b;
    b++;
    if (prime[count - 1] + a >= 1000000)
      break;
    if ((prime[count - 1] + a) % 2 == 1) {
      dprime[dcount] = prime[count - 1] + a;
      dcount++;
    }
    prime[count] = prime[count - 1] + a;
    count++;
  }
  int n;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i <= 2000000; i++)
      dp[i] = i % 4 + i / 4;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < count; j++)
        dp[i + prime[j]] = min(dp[i + prime[j]], dp[i] + 1);
    }
    cout << dp[n] << " ";
    for (int i = 0; i <= n; i++)
      dp[i] = i % 35 + i / 35;
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < dcount; j++)
        dp[i + dprime[j]] = min(dp[i + dprime[j]], dp[i] + 1);
    }
    cout << dp[n] << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int n;
int dp[5000001];
int dp2[5000001];

int func(int v) {
  if (dp[v] != 11451419)
    return dp[v];
  if (v == 0)
    return 0;
  int cnt = 11451419;
  int j = 1;
  while (j * (j + 1) * (j + 2) / 6 <= v)
    j++;
  j--;
  for (int i = j; i >= 1; i--) {
    cnt = min(cnt, func(v - i * (i + 1) * (i + 2) / 6));
  }
  return (dp[v] = cnt + 1);
}

int func2(int v) {
  if (dp2[v] != 11451419)
    return dp2[v];
  if (v == 0)
    return 0;
  int cnt = 11451419;
  int j = 1;
  while (j * (j + 1) * (j + 2) / 6 <= v)
    j++;
  j--;
  for (int i = j; i >= 1; i--) {
    if ((i * (i + 1) * (i + 2) / 6) % 2 != 1)
      continue;
    cnt = min(cnt, func2(v - i * (i + 1) * (i + 2) / 6));
  }
  return (dp2[v] = cnt + 1);
}

int main(void) {
  for (int i = 1; i <= 2000000; i++)
    dp[i] = dp2[i] = 11451419;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    printf("%d %d\\n", func(n), func2(n));
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

int dp[5000001];
int dp2[5000001];

int func(int v) {
  if (dp[v] != 11451419)
    return dp[v];
  if (v == 0)
    return 0;
  int cnt = 11451419;
  for (int i = 1; i * (i + 1) * (i + 2) / 6 <= 1000000; i++) {
    if (v - i * (i + 1) * (i + 2) / 6 >= 0)
      cnt = min(cnt, func(v - i * (i + 1) * (i + 2) / 6));
  }
  return (dp[v] = cnt + 1);
}

int func2(int v) {
  if (dp2[v] != 11451419)
    return dp2[v];
  if (v == 0)
    return 0;
  int cnt = 11451419;
  for (int i = 1; i * (i + 1) * (i + 2) / 6 <= 1000000; i++) {
    if ((i * (i + 1) * (i + 2) / 6) % 2 != 1)
      continue;
    if (v - i * (i + 1) * (i + 2) / 6 >= 0)
      cnt = min(cnt, func2(v - i * (i + 1) * (i + 2) / 6));
  }
  return (dp2[v] = cnt + 1);
}

int main(void) {
  int n;
  for (int i = 1; i <= 2000000; i++)
    dp[i] = dp2[i] = 11451419;
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    printf("%d %d\\n", func(n), func2(n));
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "../dump.hpp"
#else
#define dump(...)
#endif

#define rep(i, n) for (ll i = 0, i##_cond = (n); i < i##_cond; ++i)
#define FOR(i, a, b) for (ll i = (a), i##_cond = (b); i < i##_cond; ++i)
#define ROF(i, a, b) for (ll i = (a)-1, i##_cond = (b); i >= i##_cond; --i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend() // sortで大きい順
#define UNIQUE(v) v.erase(unique(all(v)), v.end())
#define SUM(a) accumulate(all(a), 0)
#define sz(x) ((ll)(x).size())
#define pb push_back
#define fst first
#define snd second
#define mp make_pair

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<ll, ll> pii;

const ll inf = 1ll << 62;
const ll mod = 1e9 + 7;

#define n 1000000

int main() {
#define int ll
  int tmp = 1;
  vi a, b;
  while (1) {
    int x = tmp * (tmp + 1) * (tmp + 2) / 6;
    if (x >= 1000000)
      break;
    a.pb(x);
    if (x & 1)
      b.pb(x);
    tmp++;
  }
  dump(a, b);

  vi dpa(n + 1, inf), dpb(n + 1, inf);
  dpa[0] = 0;
  dpb[0] = 0;
  rep(i, n) {
    for (int j = 0; j < sz(a) and a[j] <= i + 1; ++j)
      dpa[i + 1] = min(dpa[i + 1], dpa[i + 1 - a[j]] + 1);
    for (int j = 0; j < sz(b) and b[j] <= i + 1; ++j)
      dpb[i + 1] = min(dpb[i + 1], dpb[i + 1 - b[j]] + 1);
  }
  while (1) {
    int m;
    cin >> m;
    if (m == 0)
      break;

    cout << dpa[m] << " " << dpb[m] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "../dump.hpp"
#else
#define dump(...)
#endif

#define rep(i, n) for (ll i = 0, i##_cond = (n); i < i##_cond; ++i)
#define FOR(i, a, b) for (ll i = (a), i##_cond = (b); i < i##_cond; ++i)
#define ROF(i, a, b) for (ll i = (a)-1, i##_cond = (b); i >= i##_cond; --i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend() // sortで大きい順
#define UNIQUE(v) v.erase(unique(all(v)), v.end())
#define SUM(a) accumulate(all(a), 0)
#define sz(x) ((ll)(x).size())
#define pb push_back
#define fst first
#define snd second
#define mp make_pair

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<ll, ll> pii;

const ll inf = 1ll << 62;
const ll mod = 1e9 + 7;

#define n 1000000

int main() {
#define int ll
  int tmp = 1;
  vi a, b;
  while (1) {
    int x = tmp * (tmp + 1) * (tmp + 2) / 6;
    if (x >= 1000000)
      break;
    a.pb(x);
    if (x & 1)
      b.pb(x);
    tmp++;
  }
  dump(a, b);

  vi dpa(n + 1, inf), dpb(n + 1, inf);
  dpa[0] = 0;
  dpb[0] = 0;
  rep(i, n) {
    for (int j = 0; a[j] <= i + 1; ++j)
      for (int k = 1; a[j] * k <= i + 1; ++k)
        dpa[i + 1] = min(dpa[i + 1], dpa[i + 1 - k * a[j]] + k);
    for (int j = 0; b[j] <= i + 1; ++j)
      for (int k = 1; b[j] * k <= i + 1; ++k)
        dpb[i + 1] = min(dpb[i + 1], dpb[i + 1 - k * b[j]] + k);
  }
  while (1) {
    int m;
    cin >> m;
    if (m == 0)
      break;

    cout << dpa[m] << " " << dpb[m] << endl;
  }
}
Problem p00748 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

namespace Solver {

int N;
vector<int> m1, m2;
inline bool Read() {
  cin >> N;
  return N != 0;
}

inline void PreWork() {
  m1.resize(1000000, 1000000);
  m2.resize(1000000, 1000000);
  m1[0] = m2[0] = 0;
  for (int i = 1; i < 1000000; i++) {
    for (int j = 0;; j++) {
      int vol = vol = ((j * (j + 1) * (j + 2)) / 6);
      if (i < vol)
        break;
      m1[i] = min(m1[i], m1[i - vol] + 1);
      if (vol % 2 == 1) {
        m2[i] = min(m2[i], m2[i - vol] + 1);
      }
    }
  }
}

inline void Work() { cout << m1[N] << " " << m2[N] << endl; }

inline void Solve() {
  PreWork();
  while (Read())
    Work();
}
} // namespace Solver

int main() { Solver::Solve(); }

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

namespace Solver {

int N;
vector<int> m1, m2;
inline bool Read() {
  cin >> N;
  return N != 0;
}

int dfs(int n, bool odd) {
  if (odd && m2[n] != -1)
    return m2[n];
  if (!odd && m1[n] != -1)
    return m1[n];

  int ans = 1e6, t = -1, tt = -1;
  for (int i = 1000; i >= 1; i--) {
    int vol = ((i * (i + 1) * (i + 2)) / 6);
    if (odd && vol % 2 != 1)
      continue;

    int rest = n - vol;
    if (rest < 0)
      continue;
    int p = ans;
    ans = min(ans, dfs(rest, odd) + 1);
    if (ans != p) {
      t = i;
      tt = rest;
    }
  }

  if (odd)
    m2[n] = ans;
  if (!odd)
    m1[n] = ans;
  return ans;
}

inline void Work() { cout << dfs(N, false) << " " << dfs(N, true) << endl; }

inline void Solve() {

  m1.resize(10e6 + 1, -1);
  m2.resize(10e6 + 1, -1);
  m1[0] = m2[0] = 0;

  while (Read())
    Work();
}
} // namespace Solver

int main() { Solver::Solve(); }
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

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

int p[222];
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 = 180; i > 0; i--)
    if (n >= p[i])
      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 = 180; i > 0; i--)
    if (p[i] & 1 && n >= p[i])
      ans = min(ans, solve2(n - p[i]) + 1);

  return memo2[n] = ans;
}

int main() {

  int n;

  memset(memo, -1, sizeof(memo));
  memset(memo2, -1, sizeof(memo2));

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

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#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])
      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 = 0; p[i] <= n; i++)
    if (p[i] & 1 && n >= p[i])
      ans = min(ans, solve2(n - p[i]) + 1);

  return memo2[n] = ans;
}

int main() {

  int n;

  memset(memo, -1, sizeof(memo));
  memset(memo2, -1, sizeof(memo2));

  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;
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

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

int p[222];
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 = 180; i > 0; i--)
    if (n >= p[i])
      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 = 180; i > 0; i--)
    if (p[i] & 1 && n >= p[i])
      ans = min(ans, solve2(n - p[i]) + 1);

  return memo2[n] = ans;
}

int main() {

  int n;

  memset(memo, -1, sizeof(memo));
  memset(memo2, -1, sizeof(memo2));

  for (int i = 0; i < 181; i++)
    p[i] = i * (i + 1) * (i + 2) / 6;

  for (int i = 0; i < 1111111; i++) {
    solve(i), solve2(i);
  }
  while (cin >> n, n)
    cout << solve(n) << " " << solve2(n) << endl;
}

Error: 0

Fixed submission:

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

int p[222];
int memo[111111];
int memo2[111111];

int solve(int n) {

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

  int ans = INF;
  for (int i = 180; i > 0; i--)
    if (n >= p[i])
      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 = 180; i > 0; i--)
    if (p[i] & 1 && n >= p[i])
      ans = min(ans, solve2(n - p[i]) + 1);

  return memo2[n] = ans;
}

int main() {

  int n;

  memset(memo, -1, sizeof(memo));
  memset(memo2, -1, sizeof(memo2));

  for (int i = 0; i < 181; i++)
    p[i] = i * (i + 1) * (i + 2) / 6;

  for (int i = 0; i < 111111; i++) {
    solve(i), solve2(i);
  }
  while (cin >> n, n)
    cout << solve(n) << " " << solve2(n) << endl;
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

#define INF 1000000
#define MAX 1000000

int main() {
  vector<int> v(200);
  for (int i = 0; i < 200; i++)
    v[i] = i * (i + 1) * (i + 2) / 6;

  vector<int> d(MAX + 1, INF);
  vector<int> d2(MAX + 1, INF);

  d[0] = d2[0] = 0;
  for (int i = 1; i <= MAX; i++) {
    for (int j = 0; v[j] <= i; j++) {
      d[i] = min(d[i], d[i - v[j]] + 1);
      if (v[j] % 2 != 0)
        d2[i] = min(d2[i], d2[i - v[j]] + 1);
    }
  }

  while (1) {
    int s;
    cin >> s;

    if (s == 0)
      break;

    cout << d[s] << " " << d2[s] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

#define INF 1000000

int main() {
  vector<int> v(200);
  for (int i = 0; i < 200; i++)
    v[i] = i * (i + 1) * (i + 2) / 6;

  while (1) {
    int s, ans1, ans2;
    cin >> s;

    if (s == 0)
      break;

    for (int k = 1; k <= 2; k++) {
      vector<int> d(s + 1, INF);

      d[0] = 0;
      for (int i = 1; i <= s; i++) {
        for (int j = 0; v[j] <= i; j++) {
          if (k == 1)
            d[i] = min(d[i], d[i - v[j]] + 1);
          else {
            if (v[j] % 2 != 0)
              d[i] = min(d[i], d[i - v[j]] + 1);
          }
        }
      }
      if (k == 1)
        ans1 = d[s];
      else
        ans2 = d[s];
    }

    cout << ans1 << " " << ans2 << endl;
  }

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

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int dp[1000010];
int dp2[1000010];

signed main() {
  int n;
  rep(i, 0, 1000010) {
    dp[i] = 10;
    dp2[i] = 10000000;
  }
  dp[0] = 0;
  dp2[0] = 0;
  rep(i, 1, 200) {
    int a = i * (i + 1) * (i + 2) / 6;
    if (a > 1000000)
      break;
    rep(j, 0, 1000001) {
      if (j + a > 1000000)
        break;
      dp[j + a] = min(dp[j + a], dp[j] + 1);
      if (a % 2 == 0)
        continue;
      dp2[j + a] = min(dp2[j + a], dp2[j] + 1);
    }
  }
  while (cin >> n && n) {
    cout << dp[n] << " " << dp2[n] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int dp[1000010];
int dp2[1000010];

signed main() {
  int n;
  while (cin >> n && n) {
    rep(i, 0, n + 1) {
      dp[i] = 10;
      dp2[i] = 10000000;
    }
    dp[0] = 0;
    dp2[0] = 0;
    rep(i, 1, 150) {
      int a = i * (i + 1) * (i + 2) / 6;
      if (a > n)
        break;
      rep(j, 0, n + 1) {
        if (j + a > n)
          break;
        dp[j + a] = min(dp[j + a], dp[j] + 1);
        if (a % 2 == 0)
          continue;
        dp2[j + a] = min(dp2[j + a], dp2[j] + 1);
      }
    }
    cout << dp[n] << " " << dp2[n] << endl;
  }
}
Problem p00748 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int p(int i) { return i * (i + 1) * (i + 2) / 6; }

int main(void) {
  int n = 1000000;
  /* ???????¨??????? */
  int ans1[n + 1];
  int ans2[n + 1];
  for (int i = 1; i < n + 1; i++) {
    ans1[i + 1] = -1;
    ans2[i + 1] = -1;
  }

  for (int i = 1; p(i) < n + 1; i++) {
    ans1[p(i)] = 1;
    if (p(i) % 2 == 1) {
      ans2[p(i)] = 1;
    }
  }

  for (int i = 2; i <= n; i++) {
    if (ans1[i] == 1) {
    } else {
      int min = 10000;
      for (int j = 1; p(j) < i; j++) {
        if (min > 2 && ans1[i - p(j)] + 1 < min) {
          min = ans1[i - p(j)] + 1;
        }
      }
      ans1[i] = min;
    }
  }

  for (int i = 2; i <= n; i++) {
    if (ans2[i] == 1) {
    } else {
      int min = 10000;
      for (int j = 1; p(j) < i; j++) {
        if (min > 2 && p(j) % 2 == 1 && ans2[i - p(j)] + 1 < min) {
          min = ans2[i - p(j)] + 1;
        }
      }
      ans2[i] = min;
    }
  }

  while (1) {
    cin >> n;
    if (n == 0) {
      return 0;
    }
    cout << ans1[n] << " " << ans2[n] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int p(int i) { return i * (i + 1) * (i + 2) / 6; }

int main(void) {

  /* ???????¨??????? */
  int ans1[100001];
  int ans2[100001];
  for (int i = 1; i < 100001; i++) {
    ans1[i + 1] = -1;
    ans2[i + 1] = -1;
  }

  for (int i = 1; p(i) < 100001; i++) {
    ans1[p(i)] = 1;
    if (p(i) % 2 == 1) {
      ans2[p(i)] = 1;
    }
  }

  for (int i = 2; i <= 100001; i++) {
    if (ans1[i] == 1) {
    } else {
      int min = 1000000;
      for (int j = 1; j < i; j++) {
        if (ans1[j] + ans1[i - j] < min) {
          min = ans1[j] + ans1[i - j];
        }
      }
      ans1[i] = min;
    }
  }

  for (int i = 2; i <= 100001; i++) {
    if (ans2[i] == 1) {
    } else {
      int min = 1000000;
      for (int j = 1; j < i; j++) {
        if (ans2[j] + ans2[i - j] < min) {
          min = ans2[j] + ans2[i - j];
        }
      }
      ans2[i] = min;
    }
  }

  while (1) {
    int n;
    cin >> n;
    if (n == 0) {
      return 0;
    }

    cout << ans1[n] << " " << ans2[n] << endl;
  }
}
Problem p00748 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
vector<int> v, memo(1000005, 999999), memo2(1000005, 999999);
int a;
int main() {
  v.push_back(0);
  for (int i = 1; i <= 300; i++) {
    v.push_back(i * (i + 1) * (i + 2) / 6);
  }
  memo[0] = memo2[0] = 0;
  for (int i = 0; i <= 1000000; i++) {
    for (int l = 0; l < v.size(); l++) {
      if (i + v[l] > 1000000)
        break;
      if (v[l] % 2 == 1)
        memo2[i + v[l]] = min(memo2[i + v[l]], memo2[i] + 1);
      memo[i + v[l]] = min(memo[i + v[l]], memo[i] + 1);
    }
  }
  // for(int i=1;i<100;i++)cout<<memo[i]<<" "<<memo[i]<<endl;
  while (cin >> a && a) {
    cout << memo[a] << " " << memo2[a] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
vector<int> v;
short memo[1000000][100];
int a;
int dp(int now, int j) {
  if (now > a || j > 5)
    return 9999999;
  if (now == a)
    return j;
  if (memo[now][j] != -1)
    return memo[now][j];
  int ans = 9999999;
  for (int i = 1; i < v.size(); i++) {
    ans = min(ans, dp(now + v[i], j + 1));
  }
  return memo[now][j] = ans;
}
int dp2(int now, int j) {
  if (now > a)
    return 9999999;
  if (now == a)
    return j;
  if (memo[now][j] != -1)
    return memo[now][j];
  int ans = 9999999;
  for (int i = 1; i < v.size(); i += 2) {
    ans = min(ans, dp2(now + v[i], j + 1));
  }
  return memo[now][j] = ans;
}
int main() {
  v.push_back(0);
  for (int i = 1; i < 1000; i++) {
    v.push_back(i * (i + 1) * (i + 2) / 6);
  }

  while (cin >> a && a) {
    memset(memo, -1, sizeof(memo));
    cout << dp(0, 0) << " ";
    memset(memo, -1, sizeof(memo));
    cout << dp2(0, 0) << endl;
  }
  return 0;
}
Problem p00748 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <set>

using namespace std;

/*sqrt<3>(NMAX)==100*/

int Nkake[100 + 1];
int g[1000001] = {0};
int k[1000001] = {0};

int main() {

  int N;

  for (int i = 1; i <= 200; i++) {
    Nkake[i] = i * (i + 1) * (i + 2) / 6;
  }

  for (int i = 0; i <= 1000000; i++) {
    for (int j = 1; j < 200; j++) {
      if (i + Nkake[j] <= 1000000) {
        if (g[Nkake[j] + i] == 0 || g[Nkake[j] + i] > g[i] + 1)
          g[Nkake[j] + i] = g[i] + 1;
      }
    }
  }

  for (int i = 0; i <= 1000000; i++) {
    for (int j = 1; j < 200; j++) {
      if (Nkake[j] % 2 == 1 && i + Nkake[j] <= 1000000) {
        if (k[Nkake[j] + i] == 0 || k[Nkake[j] + i] > k[i] + 1)
          k[Nkake[j] + i] = k[i] + 1;
      }
    }
  }

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

    cout << g[N] << " " << k[N] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

/*sqrt<3>(NMAX)==100*/

int Nkake[100 + 1];
int c, maxc, max2c;

void searchA(int c, int N) {
  if (maxc <= c)
    return;
  if (N == 0) {
    if (maxc > c)
      maxc = c;
    return;
  }
  for (int i = 100;; i--) {
    if (N >= Nkake[i]) {
      searchA(c + 1, N - Nkake[i]);
    }
    if (i <= 1)
      return;
    if (maxc <= c)
      return;
  }
}

void searchB(int c, int N) {
  if (max2c <= c)
    return;
  if (N == 0) {
    if (max2c > c)
      max2c = c;
    return;
  }
  for (int i = 100;; i--) {
    if (Nkake[i] % 2 == 1 && N >= Nkake[i]) {
      searchB(c + 1, N - Nkake[i]);
    }
    if (i <= 1)
      return;
    if (max2c <= c)
      return;
  }
}

int main() {

  int N;

  for (int i = 1; i <= 100; i++) {
    Nkake[i] = i * (i + 1) * (i + 2) / 6;
  }
  while (1) {
    cin >> N;
    if (N == 0)
      break;
    int g = N;
    int c = 0;
    maxc = 10000000;
    max2c = 10000000;
    searchA(0, N);
    searchB(0, N);
    cout << maxc << " " << max2c << endl;
  }
}