Problem p03128 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  const int cost[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  rep(i, m) cin >> a[i];
  sort(a.rbegin(), a.rend());
  vector<int> dp(n + 1);
  rep(i, m) rep(j, n + 1) if (j - cost[a[i]] >= 0)
      chmax(dp[j], dp[j - cost[a[i]]] + 1);

  string ans;
  int match = n;
  int remain = dp[n];
  while (match > 0) {
    rep(i, m) {
      if ((remain != 1 && match - cost[a[i]] >= 0 &&
           dp[match - cost[a[i]]] == remain - 1) ||
          (remain == 1 && match - cost[a[i]] == 0)) {
        match -= cost[a[i]];
        ans += '0' + a[i];
        remain--;
        break;
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  const int cost[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  rep(i, m) cin >> a[i];
  sort(a.rbegin(), a.rend());
  const int inf = 1001001001;
  vector<int> dp(n + 1, -inf);
  dp[0] = 0;
  rep(i, m) rep(j, n + 1) if (j - cost[a[i]] >= 0)
      chmax(dp[j], dp[j - cost[a[i]]] + 1);

  string ans;
  int match = n;
  int remain = dp[n];
  while (match > 0) {
    rep(i, m) {
      if ((remain != 1 && match - cost[a[i]] >= 0 &&
           dp[match - cost[a[i]]] == remain - 1) ||
          (remain == 1 && match - cost[a[i]] == 0)) {
        match -= cost[a[i]];
        ans += '0' + a[i];
        remain--;
        break;
      }
    }
  }
  cout << ans << endl;
}