Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define print(s) cout << s << endl
#define acc(v) accumulate(v.begin(), v.end(), 0)
#define cinv(n, v) rep(i, n) cin >> v[i]
using namespace std;
const int INF = 1e9;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vint;
typedef vector<string> vstr;
typedef vector<char> vchar;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
// const lint MOD = 998244353;
int x_pos[4] = {1, 0, -1, 0}, y_pos[4] = {0, 1, 0, -1};
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}

char upper(char c) { return c - 0x20; }

char lower(char c) { return c + 0x20; }

void unique_vector(vector<int> &v) {
  sort(all(v));
  v.erase(unique(all(v)), v.end());
}

// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

// k!
ll facctorialMethod(ll k) {
  ll sum = 1;
  for (ll i = 1; i <= k; ++i) {
    sum = sum % MOD * i % MOD;
  }
  return sum % MOD;
}

const int MAX = 510000;
long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> A(m);
  rep(i, m) cin >> A[i];
  sort(rall(A));
  int Need[9] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  int dp[1010];
  Fill(dp, -1);
  dp[0] = 0;
  rep(i, n + 1) {
    rep(j, m) {
      int need = Need[A[j] - 1];
      if (i < need)
        continue;
      dp[i] = max(dp[i], dp[i - need] + 1);
    }
  }
  int remain = dp[n];
  string ans = "";
  while (remain != 0) {
    rep(i, m) {
      int need = Need[A[i] - 1];
      if (n < need)
        continue;
      if (dp[n - need] == remain - 1) {
        ans += to_string(A[i]);
        n -= need;
        remain--;
        break;
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define print(s) cout << s << endl
#define acc(v) accumulate(v.begin(), v.end(), 0)
#define cinv(n, v) rep(i, n) cin >> v[i]
using namespace std;
const int INF = 1e9;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vint;
typedef vector<string> vstr;
typedef vector<char> vchar;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
// const lint MOD = 998244353;
int x_pos[4] = {1, 0, -1, 0}, y_pos[4] = {0, 1, 0, -1};
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}

char upper(char c) { return c - 0x20; }

char lower(char c) { return c + 0x20; }

void unique_vector(vector<int> &v) {
  sort(all(v));
  v.erase(unique(all(v)), v.end());
}

// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

// k!
ll facctorialMethod(ll k) {
  ll sum = 1;
  for (ll i = 1; i <= k; ++i) {
    sum = sum % MOD * i % MOD;
  }
  return sum % MOD;
}

const int MAX = 510000;
long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> A(m);
  rep(i, m) cin >> A[i];
  sort(rall(A));
  int Need[9] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  int dp[10010];
  Fill(dp, -1);
  dp[0] = 0;
  rep(i, n + 1) {
    rep(j, m) {
      int need = Need[A[j] - 1];
      if (i < need)
        continue;
      dp[i] = max(dp[i], dp[i - need] + 1);
    }
  }
  int remain = dp[n];
  string ans = "";
  while (remain != 0) {
    rep(i, m) {
      int need = Need[A[i] - 1];
      if (n < need)
        continue;
      if (dp[n - need] == remain - 1) {
        ans += to_string(A[i]);
        n -= need;
        remain--;
        break;
      }
    }
  }
  cout << ans << endl;
}