Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using ll = long long;
#define int ll
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define erep(e, v) for (auto &&e : v)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? (int)1e9 : (int)1e18)
#define EPS 0.0000000001
using namespace std;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <typename Head, typename Value>
auto vectors(const Head &head, const Value &v) {
  return vector<Value>(head, v);
}
template <typename Head, typename... Tail> auto vectors(Head x, Tail... tail) {
  auto inner = vectors(tail...);
  return vector<decltype(inner)>(x, inner);
}
template <class T> void join(T a) {
  for (auto itr : a) {
    if (itr != *a.begin())
      cout << " ";
    cout << itr;
  }
}
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
int W, H;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool valid(int x, int y) { return (0 <= x && x < W) && (0 <= y && y < H); }
int f[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> v(m);
  for (auto &e : v)
    cin >> e;
  map<int, int> mp;
  for (auto e : v)
    cmax(mp[f[e]], e);
  vector<int> u;
  for (auto itr : mp)
    u.push_back(itr.second);
  vector<string> dp(n + 1, "");
  rep(i, dp.size()) {
    rep(j, u.size()) {
      if (i == 0) {
        dp[f[u[j]]] = to_string(u[j]);
      } else if (dp[i] != "") {
        string t = dp[i];
        string comp = dp[i] + to_string(u[j]);
        sort(all(comp), greater<>());
        if (i + f[u[j]] <= n) {
          if (comp.size() > dp[i + f[u[j]]].size() or
              (comp.size() == dp[i + f[u[j]]].size() and
               comp > dp[i + f[u[j]]]))
            dp[i + f[u[j]]] = comp;
        }
      }
    }
  }
  cout << dp[n] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using ll = long long;
#define int ll
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define erep(e, v) for (auto &&e : v)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? (int)1e9 : (int)1e18)
#define EPS 0.0000000001
using namespace std;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <typename Head, typename Value>
auto vectors(const Head &head, const Value &v) {
  return vector<Value>(head, v);
}
template <typename Head, typename... Tail> auto vectors(Head x, Tail... tail) {
  auto inner = vectors(tail...);
  return vector<decltype(inner)>(x, inner);
}
template <class T> void join(T a) {
  for (auto itr : a) {
    if (itr != *a.begin())
      cout << " ";
    cout << itr;
  }
}
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
int W, H;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool valid(int x, int y) { return (0 <= x && x < W) && (0 <= y && y < H); }
int f[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> v(m);
  for (auto &e : v)
    cin >> e;
  map<int, int> mp;
  for (auto e : v)
    cmax(mp[f[e]], e);
  vector<int> u;
  for (auto itr : mp)
    u.push_back(itr.second);
  vector<string> dp(n + 1, "");
  rep(i, dp.size()) {
    rep(j, u.size()) {
      if (i == 0) {
        if (f[u[j]] <= n)
          dp[f[u[j]]] = to_string(u[j]);
      } else if (dp[i] != "") {
        string t = dp[i];
        string comp = dp[i] + to_string(u[j]);
        sort(all(comp), greater<>());
        if (i + f[u[j]] <= n) {
          if (comp.size() > dp[i + f[u[j]]].size() or
              (comp.size() == dp[i + f[u[j]]].size() and
               comp > dp[i + f[u[j]]]))
            dp[i + f[u[j]]] = comp;
        }
      }
    }
  }
  cout << dp[n] << endl;
}