Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rt "\n"
#define sp " "
#define test(n) cout << "test" << n << endl;
#define fixsp(n) fixed << setprecision(n)
#define defsp(n) defaultfloat << setprecision(n)
#define kyopro                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL)
#define MOD 1000000007
#define iikae auto &
#define vector3D(K, N, M, DP)                                                  \
  vector<vector<vector<ll>>> DP(K, vector<vector<ll>>(N, vector<ll>(M)));
#define vector4D(K, N, M, O, DP)                                               \
  vector<vector<vector<vector<ll>>>> DP(                                       \
      K, vector<vector<vector<ll>>>(N, vector<vector<ll>>(M, vector<ll>(O))));
#define zenbu(a) a.begin(), a.end()
#define rzenbu(a) a.rbegin(), a.rend()
#define rep(i, s, N) for (ll i{s}; i < N; i++)
#define rem(i, N, s) for (ll i{N}; i > s; i--)
using namespace std;
using ll = long long int;
using ld = long double;
using P = pair<ll, ll>; /* 最大公約数 */
template <typename T> T gcd(T a, T b) {
  return b != 0 ? gcd(b, a % b) : a;
} /* UnionFind */
template <typename T> struct UnionFind {
  vector<T> par;
  UnionFind(T n) : par(n, -1) {}
  void init(T n) { par.assign(n, -1); }
  T root(T x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }
  bool issame(T x, T y) { return root(x) == root(y); }
  bool merge(T x, T y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }
  int size(int x) { return -par[root(x)]; }
}; /* コンビネーション */
template <typename T> ll combpm(T N_, T C_) {
  const int NUM_ = 400001;
  static ll fact[NUM_ + 1], factr[NUM_ + 1], inv[NUM_ + 1];
  if (fact[0] == 0) {
    inv[1] = fact[0] = factr[0] = 1;
    for (int i = 2; i <= NUM_; ++i)
      inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
    for (int i = 1; i <= NUM_; ++i)
      fact[i] = fact[i - 1] * i % MOD, factr[i] = factr[i - 1] * inv[i] % MOD;
  }
  if (C_ < 0 || C_ > N_)
    return 0;
  return factr[C_] * fact[N_] % MOD * factr[N_ - C_] % MOD;
}
/* ここからコード開始 */

int main() {
  kyopro;
  ll N, M;
  cin >> N >> M;
  vector<P> digit;
  vector<ll> num = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  rep(i, 0, M) {
    ll tmp;
    cin >> tmp;
    rep(i, 0, 9) {
      if (tmp == i + 1)
        digit.push_back(P(i + 1, num[i]));
    }
  }
  sort(zenbu(digit));
  vector<string> ans(N + 1, "X");
  for (auto &&j : digit) {
    ans[j.second] = to_string(j.first);
  }
  // test(1);
  // rep(i, 1, N+1){
  //   cout << i << sp << ans[i] << rt;
  // }
  // test(2);
  rep(i, 1, N + 1) {
    for (auto &&j : digit) {
      auto &figure = j.first;
      auto &amount = j.second;
      string append = ans[i] + to_string(figure);
      if (i + amount < N + 1 ? append.size() > ans[i + amount].size() ||
                                   (append.size() == ans[i + amount].size() &&
                                    append >= ans[i + amount])
                             : 0) {
        if (ans[i] != "X")
          ans[i + amount] = append;
      }
    }
    // cout << i << ": " << ans[i] << endl;
  }
  // test(3);
  cout << ans[N] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rt "\n"
#define sp " "
#define test(n) cout << "test" << n << endl;
#define fixsp(n) fixed << setprecision(n)
#define defsp(n) defaultfloat << setprecision(n)
#define kyopro                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(NULL)
#define MOD 1000000007
#define iikae auto &
#define vector3D(K, N, M, DP)                                                  \
  vector<vector<vector<ll>>> DP(K, vector<vector<ll>>(N, vector<ll>(M)));
#define vector4D(K, N, M, O, DP)                                               \
  vector<vector<vector<vector<ll>>>> DP(                                       \
      K, vector<vector<vector<ll>>>(N, vector<vector<ll>>(M, vector<ll>(O))));
#define zenbu(a) a.begin(), a.end()
#define rzenbu(a) a.rbegin(), a.rend()
#define rep(i, s, N) for (ll i{s}; i < N; i++)
#define rem(i, N, s) for (ll i{N}; i > s; i--)
using namespace std;
using ll = long long int;
using ld = long double;
using P = pair<ll, ll>; /* 最大公約数 */
template <typename T> T gcd(T a, T b) {
  return b != 0 ? gcd(b, a % b) : a;
} /* UnionFind */
template <typename T> struct UnionFind {
  vector<T> par;
  UnionFind(T n) : par(n, -1) {}
  void init(T n) { par.assign(n, -1); }
  T root(T x) {
    if (par[x] < 0)
      return x;
    else
      return par[x] = root(par[x]);
  }
  bool issame(T x, T y) { return root(x) == root(y); }
  bool merge(T x, T y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }
  int size(int x) { return -par[root(x)]; }
}; /* コンビネーション */
template <typename T> ll combpm(T N_, T C_) {
  const int NUM_ = 400001;
  static ll fact[NUM_ + 1], factr[NUM_ + 1], inv[NUM_ + 1];
  if (fact[0] == 0) {
    inv[1] = fact[0] = factr[0] = 1;
    for (int i = 2; i <= NUM_; ++i)
      inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
    for (int i = 1; i <= NUM_; ++i)
      fact[i] = fact[i - 1] * i % MOD, factr[i] = factr[i - 1] * inv[i] % MOD;
  }
  if (C_ < 0 || C_ > N_)
    return 0;
  return factr[C_] * fact[N_] % MOD * factr[N_ - C_] % MOD;
}
/* ここからコード開始 */

int main() {
  kyopro;
  ll N, M;
  cin >> N >> M;
  vector<P> digit;
  vector<ll> num = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  rep(i, 0, M) {
    ll tmp;
    cin >> tmp;
    rep(i, 0, 9) {
      if (tmp == i + 1)
        digit.push_back(P(i + 1, num[i]));
    }
  }
  sort(zenbu(digit));
  vector<string> ans(N + 1, "X");
  for (auto &&j : digit) {
    if (j.second < N + 1)
      ans[j.second] = to_string(j.first);
  }
  // test(1);
  // rep(i, 1, N+1){
  //   cout << i << sp << ans[i] << rt;
  // }
  // test(2);
  rep(i, 1, N + 1) {
    for (auto &&j : digit) {
      auto &figure = j.first;
      auto &amount = j.second;
      string append = ans[i] + to_string(figure);
      if (i + amount < N + 1 ? append.size() > ans[i + amount].size() ||
                                   (append.size() == ans[i + amount].size() &&
                                    append >= ans[i + amount])
                             : 0) {
        if (ans[i] != "X")
          ans[i + amount] = append;
      }
    }
    // cout << i << ": " << ans[i] << endl;
  }
  // test(3);
  cout << ans[N] << endl;
}