Problem p03128 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for (int64 i = 0; i < (n); i++)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

::std::int64_t gcd(::std::int64_t a, ::std::int64_t b) {
  while (b != 0) {
    ::std::swap(a, b);
    b = b % a;
  }
  return a;
}

int64 need[10] = {INF, 2, 5, 5, 4, 5, 6, 3, 7, 6};
bool canuse[10] = {};

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int64 N, M;
  cin >> N >> M;
  REP(i, M) {
    int32 a;
    cin >> a;
    canuse[a] = 1;
  }
  vector<int64> dp(N, -INF);
  dp[0] = 0;
  REP(i, N) {
    for (int32 j = 9; j >= 1; j--) {
      if (!canuse[j])
        continue;
      if (need[j] + i > N)
        continue;
      chmax(dp[need[j] + i], dp[i] + 1);
    }
  }
  int32 now = N;
  string res = "";
  while (now > 0) {
    for (int32 i = 9; i >= 1; i--) {
      if (!canuse[i])
        continue;
      if (now - need[i] < 0)
        continue;
      if (dp[now - need[i]] == dp[now] - 1) {
        now -= need[i];
        res += (char)('0' + i);
        break;
      }
    }
  }
  cout << res << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for (int64 i = 0; i < (n); i++)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

::std::int64_t gcd(::std::int64_t a, ::std::int64_t b) {
  while (b != 0) {
    ::std::swap(a, b);
    b = b % a;
  }
  return a;
}

int64 need[10] = {INF, 2, 5, 5, 4, 5, 6, 3, 7, 6};
bool canuse[10] = {};

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int64 N, M;
  cin >> N >> M;
  REP(i, M) {
    int32 a;
    cin >> a;
    canuse[a] = 1;
  }
  vector<int64> dp(N + 1, -INF);
  dp[0] = 0;
  REP(i, N) {
    for (int32 j = 9; j >= 1; j--) {
      if (!canuse[j])
        continue;
      if (need[j] + i > N)
        continue;
      chmax(dp[need[j] + i], dp[i] + 1);
    }
  }
  int32 now = N;
  string res = "";
  while (now > 0) {
    for (int32 i = 9; i >= 1; i--) {
      if (!canuse[i])
        continue;
      if (now - need[i] < 0)
        continue;
      if (dp[now - need[i]] == dp[now] - 1) {
        now -= need[i];
        res += (char)('0' + i);
        break;
      }
    }
  }
  cout << res << endl;
}