Problem p03128 (C++)

Buggy submission (status: Runtime Error):

#if 1
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

auto &in = std::cin;
auto &out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI =
    3.141592653589793238462643383279502884197169399375105820974944;

bool able[10];
const int use[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
std::pair<int32_t, std::string> dp[10000];
const std::pair<int32_t, std::string> &func(int i) {
  if (i == 0) {
    dp[i].first = 0;
    return dp[i];
  }
  if (dp[i].first != -1) {
    return dp[i];
  }

  char add = '0';
  for (size_t n = 1; n < 10; n++) {
    if (able[n] && i >= use[n]) {
      const auto &next = func(i - use[n]);
      if (next.first < 0) {
        continue;
      }
      if (dp[i].first <= next.first) {
        dp[i] = next;
        add = '0' + n;
      }
    }
  }
  if (add != '0') {
    dp[i].first += 1;
    dp[i].second = add + dp[i].second;
  }
  return dp[i];
}

template <typename T, typename U>
std::enable_if_t<std::rank<T>::value == 0> fill_all(T &arr, const U &v) {
  arr = v;
}
template <typename ARR, typename U>
std::enable_if_t<std::rank<ARR>::value != 0> fill_all(ARR &arr, const U &v) {
  for (auto &i : arr) {
    fill_all(i, v);
  }
}
int main() {
  fill_all(dp, std::pair<int32_t, std::string>{-1, ""});

  using std::endl;
  in.sync_with_stdio(false);
  out.sync_with_stdio(false);
  in.tie(nullptr);
  out.tie(nullptr);

  int32_t N, M;
  in >> N >> M;
  for (size_t i = 0; i < M; i++) {
    int v;
    in >> v;
    able[v] = true;
  }
  out << func(N).second << endl;

  return 0;
}
#endif


Fixed submission:

#if 1
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

auto &in = std::cin;
auto &out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI =
    3.141592653589793238462643383279502884197169399375105820974944;

bool able[10];
const int use[10] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};
std::pair<int32_t, std::string> dp[11000];
const std::pair<int32_t, std::string> &func(int i) {
  if (i == 0) {
    dp[i].first = 0;
    return dp[i];
  }
  if (dp[i].first != -1) {
    return dp[i];
  }

  char add = '0';
  for (size_t n = 1; n < 10; n++) {
    if (able[n] && i >= use[n]) {
      const auto &next = func(i - use[n]);
      if (next.first < 0) {
        continue;
      }
      if (dp[i].first <= next.first) {
        dp[i] = next;
        add = '0' + n;
      }
    }
  }
  if (add != '0') {
    dp[i].first += 1;
    dp[i].second = add + dp[i].second;
  }
  return dp[i];
}

template <typename T, typename U>
std::enable_if_t<std::rank<T>::value == 0> fill_all(T &arr, const U &v) {
  arr = v;
}
template <typename ARR, typename U>
std::enable_if_t<std::rank<ARR>::value != 0> fill_all(ARR &arr, const U &v) {
  for (auto &i : arr) {
    fill_all(i, v);
  }
}
int main() {
  fill_all(dp, std::pair<int32_t, std::string>{-1, ""});

  using std::endl;
  in.sync_with_stdio(false);
  out.sync_with_stdio(false);
  in.tie(nullptr);
  out.tie(nullptr);

  int32_t N, M;
  in >> N >> M;
  for (size_t i = 0; i < M; i++) {
    int v;
    in >> v;
    able[v] = true;
  }
  out << func(N).second << endl;

  return 0;
}
#endif