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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;

#define INF 1000000000

const int coins[] = {10, 50, 100, 500};

// number of conins of the yen
int n_coin(int yen) {
  int res = 0;
  rep(i, 4) {
    res += yen / coins[3 - i];
    yen = yen % coins[3 - i];
  }
  return res;
}

int main() {
  int yen;
  bool isfirst = true;
  while (cin >> yen, yen) {
    if (!isfirst) {
      cout << endl;
    } else
      isfirst = false;

    int bill[4];
    int total = 0;
    rep(i, 4) {
      scanf("%d", &bill[i]);
      total += bill[i];
    }

    int res[4];

    int min = INF;
    rep(i, bill[0] + 1) {
      rep(j, bill[1] + 1) {
        rep(k, bill[2] + 1) {
          rep(l, bill[3] + 1) {
            int pay = i * coins[0] + j * coins[1] + k * coins[2] + l * coins[3];
            int charge = pay - yen;
            if (charge < 0) {
              continue;
            }
            int left = total - i - j - k - l + n_coin(charge);
            if (left < min) {
              res[0] = i;
              res[1] = j;
              res[2] = k;
              res[3] = l;
              min = left;
            }
            break;
          }
        }
      }
    }

    rep(i, 4) {
      if (res[i] != 0)
        printf("%d %d\\n", coins[i], res[i]);
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;

#define INF 1000000000

const int coins[] = {10, 50, 100, 500};

// number of conins of the yen
int n_coin(int yen) {
  int res = 0;
  rep(i, 4) {
    res += yen / coins[3 - i];
    yen = yen % coins[3 - i];
  }
  return res;
}

int main() {
  int yen;
  while (cin >> yen, yen) {
    int bill[4];
    int total = 0;
    rep(i, 4) {
      scanf("%d", &bill[i]);
      total += bill[i];
    }

    int res[4];

    int min = INF;
    rep(i, bill[0] + 1) {
      rep(j, bill[1] + 1) {
        rep(k, bill[2] + 1) {
          rep(l, bill[3] + 1) {
            int pay = i * coins[0] + j * coins[1] + k * coins[2] + l * coins[3];
            int charge = pay - yen;
            dbg(pay);
            if (charge < 0) {
              continue;
            }
            int left = total - i - j - k - l + n_coin(charge);
            if (left < min) {
              res[0] = i;
              res[1] = j;
              res[2] = k;
              res[3] = l;
              min = left;
            }
          }
        }
      }
    }

    rep(i, 4) {
      if (res[i] != 0)
        printf("%d %d\\n", coins[i], res[i]);
    }

    cout << endl;
  }
}