Problem p03128 (C++)

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

typedef long long int lli;
typedef unsigned long long int ulli;
typedef pair<int, int> pii;
typedef priority_queue<int, vector<int>, greater<int>> minHeap;

#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d", &n)
#define scnll(n) scanf("%lld", &n)
#define scn2(n, m) scanf("%d%d", &n, &m)
#define scn3(n, m, w) scanf("%d%d%d", &n, &m, &w)
#define scn2ll(n, m) scanf("%lld%lld", &n, &m)
#define atoz(v) v.begin(), v.end()
#define ratoz(v) v.rbegin(), v.rend()
#define Fill(a, v) memset(a, v, sizeof(a))
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define mod 1000000007
#define FastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define bug(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << "\n"

int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }

inline int addmod(lli x, lli y) { return (x % mod + y % mod) % mod; }
inline int submod(lli x, lli y) { return (x % mod - y % mod + mod) % mod; }
inline int mulmod(lli x, lli y) { return (x % mod * y % mod) % mod; }

inline int nextSubMask(int i, int mask) {
  return (i - 1) & mask; /// returns next  submask
}

template <typename T> void we_r_done(T mssg) {
  cout << mssg;
  exit(0);
}

int main() {
  /// freopen("output.txt","w",stdout);
  // freopen("input.txt","r",stdin);
  FastIO;

  int n, m;
  while (cin >> n >> m) {
    int d[m];
    for (int i = 0; i < m; i++) {
      cin >> d[i];
    }
    sort(d, d + m);
    int dp[n + 1], need[23] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

    dp[0] = 0;
    for (int i = 1; i <= n; i++)
      dp[i] = -1;

    for (int make = 1; make <= n; make++) {
      for (int i = 0; i < m; i++) {
        if (make < need[d[i]])
          continue;

        if (dp[make - need[d[i]]] == -1)
          continue;

        if (dp[make - need[d[i]]] + 1 >= dp[make]) {
          dp[make] = dp[make - need[d[i]]] + 1;
        }
      }
    }
    assert(dp[n] != -1);
    vector<int> res;
    for (int made = n; made;) {

      for (int i = m - 1; i >= 0; i--) {

        if (dp[made - need[d[i]]] + 1 == dp[made]) {
          res.pb(d[i]);
          made -= need[d[i]];
          break;
        }
      }
    }
    sort(res.rbegin(), res.rend());
    for (int t : res)
      cout << t;
    cout << endl;
  }

  return 0;
}

/// sin and cos expect input in radians not degrees. so use , sin(degrees * pi /
/// 180) using bs = bitset<MX>; // how many distinct number can be form?
/// sort(atoz(v), [](const data x, const data y){return (x.a==y.a?x.b>y.b :
/// x.a<y.a);});


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

typedef long long int lli;
typedef unsigned long long int ulli;
typedef pair<int, int> pii;
typedef priority_queue<int, vector<int>, greater<int>> minHeap;

#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d", &n)
#define scnll(n) scanf("%lld", &n)
#define scn2(n, m) scanf("%d%d", &n, &m)
#define scn3(n, m, w) scanf("%d%d%d", &n, &m, &w)
#define scn2ll(n, m) scanf("%lld%lld", &n, &m)
#define atoz(v) v.begin(), v.end()
#define ratoz(v) v.rbegin(), v.rend()
#define Fill(a, v) memset(a, v, sizeof(a))
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define mod 1000000007
#define FastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define bug(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << "\n"

int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }

inline int addmod(lli x, lli y) { return (x % mod + y % mod) % mod; }
inline int submod(lli x, lli y) { return (x % mod - y % mod + mod) % mod; }
inline int mulmod(lli x, lli y) { return (x % mod * y % mod) % mod; }

inline int nextSubMask(int i, int mask) {
  return (i - 1) & mask; /// returns next  submask
}

template <typename T> void we_r_done(T mssg) {
  cout << mssg;
  exit(0);
}

int main() {
  /// freopen("output.txt","w",stdout);
  // freopen("input.txt","r",stdin);
  FastIO;

  int n, m;
  while (cin >> n >> m) {
    int d[m];
    for (int i = 0; i < m; i++) {
      cin >> d[i];
    }
    sort(d, d + m);
    int dp[n + 1], need[23] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

    dp[0] = 0;
    for (int i = 1; i <= n; i++)
      dp[i] = -1;

    for (int make = 1; make <= n; make++) {
      for (int i = 0; i < m; i++) {
        if (make < need[d[i]])
          continue;

        if (dp[make - need[d[i]]] == -1)
          continue;

        if (dp[make - need[d[i]]] + 1 >= dp[make]) {
          dp[make] = dp[make - need[d[i]]] + 1;
        }
      }
    }
    assert(dp[n] != -1);
    vector<int> res;
    for (int made = n; made;) {

      for (int i = m - 1; i >= 0; i--) {
        if (made < need[d[i]])
          continue;
        if (dp[made - need[d[i]]] + 1 == dp[made]) {
          res.pb(d[i]);
          made -= need[d[i]];
          break;
        }
      }
    }
    sort(res.rbegin(), res.rend());
    for (int t : res)
      cout << t;
    cout << endl;
  }

  return 0;
}

/// sin and cos expect input in radians not degrees. so use , sin(degrees * pi /
/// 180) using bs = bitset<MX>; // how many distinct number can be form?
/// sort(atoz(v), [](const data x, const data y){return (x.a==y.a?x.b>y.b :
/// x.a<y.a);});