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

#include <cstdio>
#include <iomanip>
#include <iostream>

#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

#include <algorithm>
#include <functional>
#include <numeric>

#include <cctype>

#include <complex>
#include <sstream>
#include <string>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define tr(it, container)                                                      \\
  for (typeof(container.begin()) it = container.begin();                       \\
       it != container.end(); ++it)
#define mp(a, b) make_pair((a), (b))

typedef long long ll;
typedef complex<double> P;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
const double EPS = 1e-9;
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int daysleap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main() {
  bool first = true;
  while (true) {
    int ans[4];
    int num[4];
    int n;

    cin >> n;
    if (n == 0)
      break;
    if (!first)
      cout << endl;
    first = false;
    cin >> num[3] >> num[2] >> num[1] >> num[0];
    int all = 0;
    int mic = 100000;
    rep(i, 4) all += num[i];
    rep(i, num[0] + 1) rep(j, num[1] + 1) rep(k, num[2] + 1)
        rep(l, num[3] + 1) {
      int kk = 500 * i + 100 * j + 50 * k + 10 * l;
      if (kk < n)
        continue;
      int retc = kk - n;
      int ret = 0;
      ret += retc / 500;
      retc %= 500;
      ret += retc / 100;
      retc %= 100;
      ret += retc / 50;
      retc %= 50;
      ret += retc / 10;
      int uu = i + j + k + l;
      int r = all - uu + ret;
      if (r < mic) {
        mic = r;
        ans[0] = i;
        ans[1] = j;
        ans[2] = k;
        ans[3] = l;
      }
    }
    for (int i = 3; i >= 0; i--) {
      if (ans[i] == 0)
        continue;
      cout << (i == 0   ? 500
               : i == 1 ? 100
               : i == 2 ? 50
                        : 10)
           << " " << ans[i] << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iomanip>
#include <iostream>

#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

#include <algorithm>
#include <functional>
#include <numeric>

#include <cctype>

#include <complex>
#include <sstream>
#include <string>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define tr(it, container)                                                      \\
  for (typeof(container.begin()) it = container.begin();                       \\
       it != container.end(); ++it)
#define mp(a, b) make_pair((a), (b))

typedef long long ll;
typedef complex<double> P;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
const double EPS = 1e-9;
const int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int daysleap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main() {
  bool first = true;
  while (true) {
    if (!first)
      cout << endl;
    first = false;
    int ans[4];
    int num[4];
    int n;
    cin >> n >> num[3] >> num[2] >> num[1] >> num[0];
    int all = 0;
    int mic = 100000;
    rep(i, 4) all += num[i];
    rep(i, num[0] + 1) rep(j, num[1] + 1) rep(k, num[2] + 1)
        rep(l, num[3] + 1) {
      int kk = 500 * i + 100 * j + 50 * k + 10 * l;
      if (kk < n)
        continue;
      int retc = kk - n;
      int ret = 0;
      ret += retc / 500;
      retc %= 500;
      ret += retc / 100;
      retc %= 100;
      ret += retc / 50;
      retc %= 50;
      ret += retc / 10;
      int uu = i + j + k + l;
      int r = all - uu + ret;
      if (r < mic) {
        mic = r;
        ans[0] = i;
        ans[1] = j;
        ans[2] = k;
        ans[3] = l;
      }
    }
    for (int i = 3; i >= 0; i--) {
      if (ans[i] == 0)
        continue;
      cout << (i == 0   ? 500
               : i == 1 ? 100
               : i == 2 ? 50
                        : 10)
           << " " << ans[i] << endl;
    }
  }
  return 0;
}