Language: C++

#include <iostream>
#include <map>
#include <list>
using namespace std;
static size_t constexpr none = 9;
static size_t constexpr match [10] = {none, 2, 5, 5, 4, 5, 6, 3, 7, 6};
class AvailableNums {
    list<size_t> nums;
    size_t minNums;
public :
    void Initialize(set<size_t> const& tempNums) {
        minNums = none;
        for (auto i : tempNums) {
            if (match[i] < minNums) minNums = match[i];
            nums.push_back(i);
        }
    }

    void DeleteNeedless() {
        for (auto ite : nums) {
            auto ite_j = nums.begin();
            while (*ite_j != ite) {
                auto temp = ite_j;
                if (match[*ite_j] == match[ite]) {
                    temp++;
                    nums.erase(ite_j);
                    ite_j = temp;
                } else {
                    ite_j++;
                }
            }
        }
    }

    void Reverse() {nums.reverse();}
    size_t GetDigit(size_t N) const {
        return N / minNums;
    }

    size_t GetMin() const {return minNums;}
    auto Begin() const {
        return nums.cbegin();
    }

    auto End() const {
        return nums.cend();
    }
};

class SubMatcho {
    size_t const remainMatch;
    AvailableNums const& nums;
    size_t const digit;
public :
    SubMatcho(size_t const remain, AvailableNums const& nums_p, size_t const digit_p) :
        remainMatch(remain), nums(nums_p), digit(digit_p) {}
    bool TryNext(list<size_t>& result) const {
        if (digit == 0) return (remainMatch == 0);
        if (remainMatch < (nums.GetMin() * digit)) return false;
               
        for (auto ite = nums.Begin(); ite != nums.End(); ite++) {
            if (remainMatch < match[*ite]) continue;
            SubMatcho sub(remainMatch - match[*ite], nums, digit - 1);
            if (sub.TryNext(result)) {
                result.push_front(*ite);
                return true;
            }
        }
        return false;
    }
};

class Matcho {
    size_t matchMax;
    AvailableNums nums;
    size_t digit;
public:
    Matcho(size_t N) : matchMax(N), digit(0) {}
    void InputAvailableNums(size_t M) {
        size_t input = 0;
        set<size_t> tempNums;
        for (size_t i = 0; i < M; i++) {
            cin >> input;
            tempNums.insert(input);
        }
        nums.Initialize(tempNums);
        nums.DeleteNeedless();
        nums.Reverse();
        digit = nums.GetDigit(matchMax);
    }

    void GetResult(list<size_t>& result) {
        result.clear();
        for (auto ite = nums.Begin(); ite != nums.End(); ite++) {
            SubMatcho sub(matchMax - match[*ite], nums, digit - 1);
            if (sub.TryNext(result)) {
                result.push_front(*ite);
                break;
            }
        }
    }
};

int main() {
    size_t N, M;
    cin >> N >> M; 
    Matcho matcho(N);
    matcho.InputAvailableNums(M);

    list<size_t> result;
    matcho.GetResult(result);
    for (auto i : result) cout << i;
    cout << endl;

    return 0;
}



Compiler output:
a.cc:11:21: error: 'set' has not been declared
   11 |     void Initialize(set<size_t> const& tempNums) {
      |                     ^~~
a.cc:11:24: error: expected ',' or '...' before '<' token
   11 |     void Initialize(set<size_t> const& tempNums) {
      |                        ^
a.cc: In member function 'void AvailableNums::Initialize(int)':
a.cc:13:23: error: 'tempNums' was not declared in this scope
   13 |         for (auto i : tempNums) {
      |                       ^~~~~~~~
a.cc: In member function 'void Matcho::InputAvailableNums(size_t)':
a.cc:81:9: error: 'set' was not declared in this scope
   81 |         set<size_t> tempNums;
      |         ^~~
a.cc:4:1: note: 'std::set' is defined in header '<set>'; this is probably fixable by adding '#include <set>'
    3 | #include <list>
  +++ |+#include <set>
    4 | using namespace std;
a.cc:81:19: error: expected primary-expression before '>' token
   81 |         set<size_t> tempNums;
      |                   ^
a.cc:81:21: error: 'tempNums' was not declared in this scope
   81 |         set<size_t> tempNums;
      |                     ^~~~~~~~