Problem p03128 (C++)

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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

void Main() {
  const int INF = 1e9;
  int n, m;
  cin >> n >> m;
  vi num = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  vi a(m);
  rep(i, m) {
    cin >> a[i];
    a[i]--;
  }
  // dp[s] = s 本のマッチを使うときの実現可能な最大桁数
  vi dp(n + 5, -INF);
  dp[0] = 0;
  rep(i, m) dp[num[a[i]]] = 1;
  rep(s, n + 5) { rep(i, m) chmax(dp[s + num[a[i]]], dp[s] + 1); }

  int keta = dp[n];
  // cout << keta << endl;

  rep(k, keta) {
    int tmp = -1;
    rep(i, m) {
      if (n - num[a[i]] < 0)
        continue;
      if (dp[n - num[a[i]]] == dp[n] - 1) {
        chmax(tmp, a[i]);
      }
    }
    n -= num[tmp];
    cout << tmp + 1;
    // cout << "debug: " << n << " " << tmp+1 << " " << num[tmp] << endl;
  }
  cout << endl;
  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}

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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

void Main() {
  const int INF = 1e9;
  int n, m;
  cin >> n >> m;
  vi num = {2, 5, 5, 4, 5, 6, 3, 7, 6};
  vi a(m);
  rep(i, m) {
    cin >> a[i];
    a[i]--;
  }
  // dp[s] = s 本のマッチを使うときの実現可能な最大桁数
  vi dp(n + 5, -INF);
  dp[0] = 0;
  rep(i, m) dp[num[a[i]]] = 1;
  rep(s, n + 5) {
    rep(i, m) {
      if (s + num[a[i]] <= n + 2)
        chmax(dp[s + num[a[i]]], dp[s] + 1);
    }
  }

  int keta = dp[n];
  // cout << keta << endl;

  rep(k, keta) {
    int tmp = -1;
    rep(i, m) {
      if (n - num[a[i]] < 0)
        continue;
      if (dp[n - num[a[i]]] == dp[n] - 1) {
        chmax(tmp, a[i]);
      }
    }
    n -= num[tmp];
    cout << tmp + 1;
    // cout << "debug: " << n << " " << tmp+1 << " " << num[tmp] << endl;
  }
  cout << endl;
  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}