Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pie 3.141592653589793238462643383279
#define int long long
#define mod 1000000007
#define INF 1145141919364364
#define all(vec) vec.begin(), vec.end()
#define P pair<int, int>
#define S second
#define F first
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
bool prime(int x) {
  for (int i = 2; i <= sqrt(x); i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}

int kai(int x) {
  if (x == 0)
    return 1;
  return kai(x - 1) * x % mod;
}

int mod_pow(int x, int y, int mood) {
  int res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x % mood;
    x = x * x % mood;
    y >>= 1;
  }
  return res;
}

int comb(int x, int y) {
  return kai(x) * mod_pow(kai(x - y), mod - 2, mod) % mod *
         mod_pow(kai(y), mod - 2, mod) % mod;
}

int n, m, a;
int ta[9] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
vector<P> vec;
int dp[1010];
vector<int> cnt;

signed main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a;
    vec.push_back({a, ta[a - 1]});
  }
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (int)vec.size(); j++) {
      if (vec[j].second <= i && dp[i - vec[j].second] != INF)
        dp[i] = max(dp[i], dp[i - vec[j].second] + 1);
    }
    if (dp[i] == 0)
      dp[i] = INF;
  }
  sort(all(vec));
  reverse(all(vec));
  int b = n;
  while (dp[b] != 0) {
    bool bl = false;
    for (int j = 0; j < (int)vec.size(); j++) {
      if (!bl && b - vec[j].second >= 0 && dp[b] == dp[b - vec[j].second] + 1) {
        b -= vec[j].second;
        cnt.push_back(vec[j].first);
        bl = true;
      }
    }
  }
  for (int i = 0; i < (int)cnt.size(); i++) {
    cout << cnt[i];
  }
  cout << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define pie 3.141592653589793238462643383279
#define int long long
#define mod 1000000007
#define INF 1145141919364364
#define all(vec) vec.begin(), vec.end()
#define P pair<int, int>
#define S second
#define F first
int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
bool prime(int x) {
  for (int i = 2; i <= sqrt(x); i++) {
    if (x % i == 0)
      return false;
  }
  return true;
}

int kai(int x) {
  if (x == 0)
    return 1;
  return kai(x - 1) * x % mod;
}

int mod_pow(int x, int y, int mood) {
  int res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x % mood;
    x = x * x % mood;
    y >>= 1;
  }
  return res;
}

int comb(int x, int y) {
  return kai(x) * mod_pow(kai(x - y), mod - 2, mod) % mod *
         mod_pow(kai(y), mod - 2, mod) % mod;
}

int n, m, a;
int ta[9] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
vector<P> vec;
int dp[10010];
vector<int> cnt;

signed main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a;
    vec.push_back({a, ta[a - 1]});
  }
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < (int)vec.size(); j++) {
      if (vec[j].second <= i && dp[i - vec[j].second] != INF)
        dp[i] = max(dp[i], dp[i - vec[j].second] + 1);
    }
    if (dp[i] == 0)
      dp[i] = INF;
  }
  sort(all(vec));
  reverse(all(vec));
  int b = n;
  while (dp[b] != 0) {
    bool bl = false;
    for (int j = 0; j < (int)vec.size(); j++) {
      if (!bl && b - vec[j].second >= 0 && dp[b] == dp[b - vec[j].second] + 1) {
        b -= vec[j].second;
        cnt.push_back(vec[j].first);
        bl = true;
      }
    }
  }
  for (int i = 0; i < (int)cnt.size(); i++) {
    cout << cnt[i];
  }
  cout << endl;
}