Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define inf (ll)1e9
#define mod (ll)(1e9 + 7)
#define d(x) cerr << #x << "=" << x << endl;
#define p(x) cout << (x) << endl
#define ps(x) cout << (x);
#define pk(x) cout << (x) << " ";
#define pfix(d, x) cout << fixed << setprecision(d) << x << endl;
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define minel(v) *min_element(all(v))
#define minind(v) distance((v).begin(), min_element(all(v)))
#define maxel(v) *max_element(all(v))
#define maxind(v) distance((v).begin(), max_element(all(v)))
#define fi first
#define se second
ll a, b, c, d, n, m, x[100001], y[100001], z, h, w, k, sum,
    mi = inf, ma = -inf, ans, s, t, l, r, res, tmp, itr, p, q;
bool flag;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
char cha;
string st;
typedef map<ll, ll> imap;
typedef pair<ll, ll> P;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<pair<ll, ll>> VP;

ll dp[10000];
ll al[] = {2, 5, 5, 4, 5, 6, 3, 7, 6};

int main(void) {
  cin >> n >> m;
  rep(i, n + 1) dp[i] = -inf;
  VL v(m);
  rep(i, m) cin >> v[i];
  dp[0] = 0;
  rep(i, n + 1) {
    for (ll x : v) {
      if (i - al[x - 1] < 0)
        continue;
      dp[i] = max(dp[i], dp[i - al[x - 1]] + 1);
    }
  }
  sort(all(v));
  string ans;
  int i = 0;
  n++;
  rep(tmp, dp[n - 1]) {
    rep(j, m) {
      if (dp[n - 1 - i - al[v[m - 1 - j] - 1]] < 0 ||
          n - 1 - i - al[v[m - 1 - j] - 1] < 0)
        continue;
      if (dp[n - 1 - i] == dp[n - 1 - i - al[v[m - 1 - j] - 1]] + 1) {
        ans += to_string(v[m - 1 - j]);
        i += al[v[m - 1 - j] - 1];
        break;
      }
    }
  }
  p(ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define inf (ll)1e9
#define mod (ll)(1e9 + 7)
#define d(x) cerr << #x << "=" << x << endl;
#define p(x) cout << (x) << endl
#define ps(x) cout << (x);
#define pk(x) cout << (x) << " ";
#define pfix(d, x) cout << fixed << setprecision(d) << x << endl;
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define minel(v) *min_element(all(v))
#define minind(v) distance((v).begin(), min_element(all(v)))
#define maxel(v) *max_element(all(v))
#define maxind(v) distance((v).begin(), max_element(all(v)))
#define fi first
#define se second
ll a, b, c, d, n, m, x[100001], y[100001], z, h, w, k, sum,
    mi = inf, ma = -inf, ans, s, t, l, r, res, tmp, itr, p, q;
bool flag;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
char cha;
string st;
typedef map<ll, ll> imap;
typedef pair<ll, ll> P;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<pair<ll, ll>> VP;

ll dp[10010];
ll al[] = {2, 5, 5, 4, 5, 6, 3, 7, 6};

int main(void) {
  cin >> n >> m;
  rep(i, n + 1) dp[i] = -inf;
  VL v(m);
  rep(i, m) cin >> v[i];
  dp[0] = 0;
  rep(i, n + 1) {
    for (ll x : v) {
      if (i - al[x - 1] < 0)
        continue;
      dp[i] = max(dp[i], dp[i - al[x - 1]] + 1);
    }
  }
  sort(all(v));
  string ans;
  int i = 0;
  n++;
  rep(tmp, dp[n - 1]) {
    rep(j, m) {
      if (dp[n - 1 - i - al[v[m - 1 - j] - 1]] < 0 ||
          n - 1 - i - al[v[m - 1 - j] - 1] < 0)
        continue;
      if (dp[n - 1 - i] == dp[n - 1 - i - al[v[m - 1 - j] - 1]] + 1) {
        ans += to_string(v[m - 1 - j]);
        i += al[v[m - 1 - j] - 1];
        break;
      }
    }
  }
  p(ans);
  return 0;
}