Problem p00614 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())

int P, N[6], cost[6] = {500, 100, 50, 10, 5, 1};

int check(int p) {
  int cnt = 0;
  REP(i, 6) {
    int n = min(N[i], p / cost[i]);
    cnt += n;
    p -= n * cost[i];
  }
  return p == 0 ? cnt : -1;
}

int f(int p) {
  int res = 0;
  REP(i, 6) {
    res += p / cost[i];
    p %= cost[i];
  }
  return res;
}

int main() {
  while (cin >> P && P) {
    int sum = 0, ans = 1e9;
    REP(i, 6) {
      cin >> N[5 - i];
      sum += N[5 - i] * cost[5 - i];
    }
    FOR(i, P, sum + 1) {
      int cnt = check(i);
      if (cnt != -1)
        ans = min(ans, cnt + f(i - P));
    }
    cout << ans << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())

int P, cost[6] = {1, 5, 10, 50, 100, 500};

vector<int> check(vector<int> &used) {
  int sum = 0;
  REP(i, 6) sum += used[i] * cost[i];
  vector<int> res(6, 0);
  if (sum - P <= 0)
    return res;
  sum -= P;
  for (int i = 5; i >= 0; --i) {
    res[i] += sum / cost[i];
    sum %= cost[i];
  }
  return res;
}

bool isEnd(vector<int> &N, vector<int> &used) {
  int sum = 0;
  REP(i, 6) sum += used[i] * cost[i];
  if (sum <= P)
    return false;
  vector<int> c = check(used);
  REP(i, 6) if (c[i] != 0 && used[i] != 0) return true;
  return false;
}

int main() {
  while (cin >> P && P) {
    vector<int> N(6);
    REP(i, 6) cin >> N[i];
    queue<vector<int>> open;
    open.push(vector<int>(6, 0));
    int ans = 1e9;
    set<vector<int>> closed;
    while (!open.empty()) {
      vector<int> used = open.front();
      open.pop();
      vector<int> c = check(used);
      int sum = 0, p = 0;
      ;
      REP(i, 6) {
        sum += used[i] + c[i];
        p += cost[i] * used[i];
      }
      if (p >= P)
        ans = min(ans, sum);
      REP(i, 6) {
        if (used[i] + 1 > N[i])
          continue;
        used[i] += 1;
        if (!isEnd(N, used) && closed.find(used) == closed.end()) {
          open.push(used);
          closed.insert(used);
        }
        used[i] -= 1;
      }
    }
    cout << ans << endl;
  }
  return 0;
}