Problem p03128 (C++)

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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define INF (1 << 30)
#define ALEN(ARR) (sizeof(ARR) / sizeof((ARR)[0]))
#define MP make_pair
#define mp make_pair
#define pb push_back
#define PB push_back

#if DEBUG
#define _DEBUG(x) cout << #x << ": " << x << endl
#define _DDEBUG(x, y) cout << #x << ": " << x << ", " << #y << ": " << y << endl
#else
#define _DEBUG(x) ;
#define _DDEBUG(x, y) ;
#endif

#define ll long long
#define ull unsigned long long
#define MOD 1000000007

/** FOR VECTOR DEBUG */
template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  if (!v.empty()) {
    out << '[';
    copy(v.begin(), v.end(), ostream_iterator<T>(out, ", "));
    out << "\b\b]";
  }
  return out;
}

/* template end */
const int limit = 11729;
string dp[limit];
int match[10] = {-INF, 2, 5, 5, 4, 5, 6, 3, 7, 6};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout.precision(16);

  int n, m;
  cin >> n >> m;

  vector<pair<int, string>> al(m);
  REP(i, m) {
    int t;
    cin >> t;
    al[i] = mp(t, to_string(t));
  }
  sort(al.begin(), al.end(), [](auto a, auto b) { return b.first > a.first; });
  REP(i, limit) { dp[i] = "-1"; }
  dp[0] = "";
  REP(i, limit) {
    if (dp[i].find("-") != string::npos) {
      continue;
    }
    for (auto j : al) {
      int idx = i + match[j.first];
      dp[idx] = [&](string s, string t) {
        if (s.find("-") != string::npos || t.size() > s.size() || t > s) {
          return t;
        }
        return s;
      }(dp[idx], dp[i] + j.second);
    }
  }

  cout << dp[n] << endl;

  return 0;
}


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

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define INF (1 << 30)
#define ALEN(ARR) (sizeof(ARR) / sizeof((ARR)[0]))
#define MP make_pair
#define mp make_pair
#define pb push_back
#define PB push_back

#if DEBUG
#define _DEBUG(x) cout << #x << ": " << x << endl
#define _DDEBUG(x, y) cout << #x << ": " << x << ", " << #y << ": " << y << endl
#else
#define _DEBUG(x) ;
#define _DDEBUG(x, y) ;
#endif

#define ll long long
#define ull unsigned long long
#define MOD 1000000007

/** FOR VECTOR DEBUG */
template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  if (!v.empty()) {
    out << '[';
    copy(v.begin(), v.end(), ostream_iterator<T>(out, ", "));
    out << "\b\b]";
  }
  return out;
}

/* template end */
const int limit = 11729;
string dp[limit];
int match[10] = {-INF, 2, 5, 5, 4, 5, 6, 3, 7, 6};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout.precision(16);

  int n, m;
  cin >> n >> m;

  vector<pair<int, string>> al(m);
  REP(i, m) {
    int t;
    cin >> t;
    al[i] = mp(t, to_string(t));
  }
  sort(al.begin(), al.end(), [](auto a, auto b) { return b.first > a.first; });
  REP(i, limit) { dp[i] = "-1"; }
  dp[0] = "";
  REP(i, n) {
    if (dp[i].find("-") != string::npos) {
      continue;
    }
    for (auto j : al) {
      int idx = i + match[j.first];
      dp[idx] = [&](string s, string t) {
        if (s.find("-") != string::npos || t.size() > s.size() || t > s) {
          return t;
        }
        return s;
      }(dp[idx], dp[i] + j.second);
    }
  }

  cout << dp[n] << endl;

  return 0;
}