Problem p01137 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
 * 宇宙ヤシガニ（枝狩り問題）
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2012
 */
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <sstream>
#include <vector>
#define REP(i, p, n) for (int i = p; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define rep_split(tok, a_str, re)                                              \\
  for (char *tok = strtok((char *)a_str.c_str(), re); tok != NULL;             \\
       tok = strtok(NULL, re))
#define ALL(c) (c).begin(), (c).end()
#define dump(a) cerr << #a << "=" << (a) << endl
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
} // t=min
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
} // t=max
using namespace std;

int main() {
  // x=1,000,000
  // y=1,000
  // z=100

  int E;
  while (cin >> E, E) {
    int min_m = E;
    for (int z = 0; z * z * z <= E; z++) {
      for (int y = 0; y * y <= E - z * z * z; y++) {
        int x = E - y * y - z * z * z;
        if (x + y + z < min_m) {
          min_m = x + y + z;
        }
      }
    }
    cout << min_m << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

/*
 * 宇宙ヤシガニ（枝狩り問題）
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2012
 */
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <sstream>
#include <vector>
#define REP(i, p, n) for (int i = p; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define rep_split(tok, a_str, re)                                              \\
  for (char *tok = strtok((char *)a_str.c_str(), re); tok != NULL;             \\
       tok = strtok(NULL, re))
#define ALL(c) (c).begin(), (c).end()
#define dump(a) cerr << #a << "=" << (a) << endl
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
} // t=min
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
} // t=max
using namespace std;

int main() {
  // x=1,000,000
  // y=1,000
  // z=100

  int min_m = INT_MAX, m = 0, power = 0;
  int E;
  int ww, xx, yy, zz;
  while (cin >> E && E) {
    min_m = INT_MAX, m = 0, power = 0;
    zz = E;
    rep(z, 100) {
      yy = zz - z * z * z;
      if (yy >= 0) {
        rep(y, 1000) {
          xx = yy - y * y;
          if (xx >= 0) {
            rep(x, 1000000) {
              ww = xx - x;
              if (ww >= 0) {
                power = x + y * y + z * z * z;
                if (power == E) {
                  m = x + y + z;
                  chmin(min_m, m);
                }
              } else {
                break;
              }
            }
          } else {
            break;
          }
        }

      } else {
        break;
      }
    }
    cout << min_m << endl;
  }

  return 0;
}