Problem p03128 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdbool.h>
#include <string>
#include <utility>
#include <vector>
typedef long long ll;
#define N (1000000000 + 7)
using namespace std;
typedef pair<int, int> P;
#define INF (-1000)

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int a[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int dp[10010] = {INF};

int main(void) {
  string ans = "";
  int n, m;
  cin >> n >> m;
  vector<int> digit(n);
  for (int i = 0; i < m; i++) {
    cin >> digit[i];
  }
  sort(digit.begin(), digit.end());
  reverse(digit.begin(), digit.end());
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < m; j++) {
      if (i - a[digit[j]] == 0) {
        dp[i] = 1;
      } else {
        if ((i - a[digit[j]]) > 0) {
          if (dp[i - a[digit[j]]] > 0) {
            dp[i] = max(dp[i], dp[i - a[digit[j]]] + 1);
          }
        }
      }
    }
    if (dp[i] == INF)
      dp[i] = 0;
  }
  int size = n;
  string t = "";
  for (int i = 0; i < dp[n]; i++) {
    if (i + 1 == dp[n]) {
      for (int j = 0; j < m; j++) {
        if (size - a[digit[j]] == 0) {
          if (dp[size] == dp[size - a[digit[j]]] + 1) {
            size -= a[digit[j]];
            t = digit[j] + '0';
            ans += t;
            break;
          }
        }
      }
    } else {
      for (int j = 0; j < m; j++) {
        if ((size - a[digit[j]] >= 0) &&
            (dp[size] == dp[size - a[digit[j]]] + 1)) {
          size -= a[digit[j]];
          t = digit[j] + '0';
          ans += t;
          break;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdbool.h>
#include <string>
#include <utility>
#include <vector>
typedef long long ll;
#define N (1000000000 + 7)
using namespace std;
typedef pair<int, int> P;
#define INF (-1000)

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int a[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
int dp[10010] = {INF};

int main(void) {
  string ans = "";
  int n, m;
  cin >> n >> m;
  vector<int> digit(m);
  for (int i = 0; i < m; i++) {
    cin >> digit[i];
  }
  sort(digit.begin(), digit.end());
  reverse(digit.begin(), digit.end());
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < m; j++) {
      if (i - a[digit[j]] == 0) {
        dp[i] = 1;
      } else {
        if ((i - a[digit[j]]) > 0) {
          if (dp[i - a[digit[j]]] > 0) {
            dp[i] = max(dp[i], dp[i - a[digit[j]]] + 1);
          }
        }
      }
    }
    if (dp[i] == INF)
      dp[i] = 0;
  }
  int size = n;
  string t = "";
  for (int i = 0; i < dp[n]; i++) {
    if (i + 1 == dp[n]) {
      for (int j = 0; j < m; j++) {
        if (size - a[digit[j]] == 0) {
          if (dp[size] == dp[size - a[digit[j]]] + 1) {
            size -= a[digit[j]];
            t = digit[j] + '0';
            ans += t;
            break;
          }
        }
      }
    } else {
      for (int j = 0; j < m; j++) {
        if ((size - a[digit[j]] >= 0) &&
            (dp[size] == dp[size - a[digit[j]]] + 1)) {
          size -= a[digit[j]];
          t = digit[j] + '0';
          ans += t;
          break;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}