Language: C++

#include <iostream>
#include <set>
#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++) {
            if (remainMatch < match[*ite]) continue;
            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: In member function 'void Matcho::GetResult(std::__cxx11::list<long unsigned int>&)':
a.cc:95:17: error: 'remainMatch' was not declared in this scope
   95 |             if (remainMatch < match[*ite]) continue;
      |                 ^~~~~~~~~~~