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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) begin((a)), end((a))
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

vector<int> tri;
vector<int> b;
vector<int> a;
int ansa[1000005];
int ansb[1000005];

int main() {
  tri.push_back(0);
  a.push_back(0);
  FOR(i, 1, 10000) tri.push_back(tri[i - 1] + i);
  FOR(i, 1, 10000) {
    a.push_back(a[i - 1] + tri[i]);
    if (a.back() & 1)
      b.push_back(a.back());
    if (a.back() > 1000001)
      break;
  }
  fill(ALL(ansa), INF);
  fill(ALL(ansb), INF);
  ansa[0] = ansb[0] = 0;
  REP(j, 1000001) {
    REP(i, a.size()) {
      int t = j + a[i];
      if (t > 1000001)
        break;
      ansa[t] = min(ansa[t], ansa[j] + 1);
    }
  }
  REP(j, 1000001) {
    REP(i, b.size()) {
      int t = j + b[i];
      if (t > 1000001)
        break;
      ansb[t] = min(ansb[t], ansb[j] + 1);
    }
  }

  int n;
  while (cin >> n, n) {
    cout << ansa[n] << " " << ansb[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) begin((a)), end((a))
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

vector<int> tri;
vector<int> b;
vector<int> a;
int ansa[1000005];
int ansb[1000005];

int main() {
  tri.push_back(0);
  a.push_back(0);
  FOR(i, 1, 10000) tri.push_back(tri[i - 1] + i);
  FOR(i, 1, 10000) {
    a.push_back(a[i - 1] + tri[i]);
    if (a.back() & 1)
      b.push_back(a.back());
    if (a.back() > 1000001)
      break;
  }
  fill(ALL(ansa), INF);
  fill(ALL(ansb), INF);

  REP(i, a.size()) {
    ansa[a[i]] = 1;
    REP(j, 1000001) {
      int t = j + a[i];
      if (t > 1000001)
        break;
      ansa[t] = min(ansa[t], ansa[j] + 1);
    }
  }
  REP(i, b.size()) {
    ansb[b[i]] = 1;
    REP(j, 1000001) {
      int t = j + b[i];
      if (t > 1000001)
        break;
      ansb[t] = min(ansb[t], ansb[j] + 1);
    }
  }

  int n;
  while (cin >> n, n) {
    cout << ansa[n] << " " << ansb[n] << endl;
  }
  return 0;
}