Problem p03128 (C++)

Buggy submission (status: Runtime Error):
// 繝・Φ繝励Ξ
#include <bits/stdc++.h>
#define int long long
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ALL(v) (v).begin(), (v).end()
#define PAIR pair<int, int>
using namespace std;
// constexpr int MOD = 10e9+7;
// constexpr int INF = 1LL << 60;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
// UP = 0;RIGHT = 1; DOWN = 2; LEFT =3;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1},
          dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
int gcd(int a, int b) {
  if (a < b)
    gcd(b, a);
  int r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
//

int req[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int dp[10010];
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int N, M;
  cin >> N >> M;
  int A[M];
  REP(i, M) cin >> A[i];
  fill(dp, dp + N, (int)-1e9);
  dp[0] = 0;
  REP(i, N) {
    REP(j, M) {
      if (i + 1 - req[A[j]] >= 0) {
        chmax(dp[i + 1], dp[i + 1 - req[A[j]]] + 1);
      }
    }
  }
  sort(A, A + M, greater<int>());
  string ans{""};
  int n = N;
  REP(i, dp[N]) {
    REP(j, M) {
      if (n >= A[j] && dp[n] == dp[n - req[A[j]]] + 1) {
        ans.push_back(A[j] + '0');
        n -= req[A[j]];
        break;
      }
    }
  }
  assert(n == 0);
  cout << ans << endl;
}

// 驛ｨ蛻・・譛蟆丞､
// 譖ｴ譁ｰ縺励↑縺・ｴ蜷遺・邏ｯ遨阪ｏ
// 莠梧ｬ｡蜈・↓縺励◆縺・・縺・ｂ縺呎ｳ・
// 荳邂・園繧剃ｽ募屓繧よ峩譁ｰ縺励◆縺・・Segment
// Tree(縺溘□縺怜叙蠕涌(logN)) 遽・峇繧剃ｽ募屓繧よ峩譁ｰ縺励◆縺・・Lazy Segment
// Tree


Fixed submission:
// 繝・Φ繝励Ξ
#include <bits/stdc++.h>
#define int long long
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define ALL(v) (v).begin(), (v).end()
#define PAIR pair<int, int>
using namespace std;
// constexpr int MOD = 10e9+7;
// constexpr int INF = 1LL << 60;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
// UP = 0;RIGHT = 1; DOWN = 2; LEFT =3;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1},
          dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
int gcd(int a, int b) {
  if (a < b)
    gcd(b, a);
  int r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
//

int req[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int dp[10010];
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int N, M;
  cin >> N >> M;
  int A[M];
  REP(i, M) cin >> A[i];
  fill(dp, dp + N, (int)-1e9);
  dp[0] = 0;
  REP(i, N) {
    REP(j, M) {
      if (i + 1 - req[A[j]] >= 0) {
        chmax(dp[i + 1], dp[i + 1 - req[A[j]]] + 1);
      }
    }
  }
  sort(A, A + M, greater<int>());
  string ans{""};
  int n = N;
  REP(i, dp[N]) {
    REP(j, M) {
      if (n >= req[A[j]] && dp[n] == dp[n - req[A[j]]] + 1) {
        ans.push_back(A[j] + '0');
        n -= req[A[j]];
        break;
      }
    }
  }
  assert(n == 0);
  cout << ans << endl;
}

// 驛ｨ蛻・・譛蟆丞､
// 譖ｴ譁ｰ縺励↑縺・ｴ蜷遺・邏ｯ遨阪ｏ
// 莠梧ｬ｡蜈・↓縺励◆縺・・縺・ｂ縺呎ｳ・
// 荳邂・園繧剃ｽ募屓繧よ峩譁ｰ縺励◆縺・・Segment
// Tree(縺溘□縺怜叙蠕涌(logN)) 遽・峇繧剃ｽ募屓繧よ峩譁ｰ縺励◆縺・・Lazy Segment
// Tree