Language: C++

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

namespace NyaGadget
{

#define each(e, collection) for(auto& e: collection)              // for each
#define size(collection) ((ll) (collection).size())               // collection size

	struct BaseNP
	{	// N進数を表現するクラス
		long long cardinal_;	// 基数
		// シンボルそのものでなく、シンボルに対応するindexを格納していく
		deque<long long> symbol_index_collection_;
		string symbols_[36] =
		{	// 36進数まで対応
			"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
			"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
			"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
			"U", "V", "W", "X", "Y", "Z",
		};

		BaseNP(long long cardinal)
		{
			cardinal_ = cardinal;
			symbol_index_collection_.push_back(0);
		}
		void Inc()
		{	// 返り値なしのインクリメント
			bool carry = true;
			long long index = size(symbol_index_collection_) - 1;
			while (0 <= index && carry)
			{
				if (symbol_index_collection_[index] != cardinal_ - 1)
				{
					symbol_index_collection_[index]++;
					carry = false;
				}
				else
				{	// 桁上り
					symbol_index_collection_[index] = 0;
					if (index == 0)
						symbol_index_collection_.push_front(1);
					carry = true;
				}
				index--;
			}
		}
		BaseNP& operator ++()
		{	// 前置インクリメント
			Inc();
			return *this;
		}
		BaseNP operator ++(int)
		{  // 後置インクリメント
			BaseNP np = *this;
			++* this;
			return np;
		}
		/**
		@brief N進数取得関数
		@note
		 d桁のN進数として文字列で取得
		---------------------------------------------------------------------------
		[sample]
		long long main()
		{	// 16進数テスト
			NyaGadgetPositionalNotation pn(16);

			for (long long i = 0; i < 30; i++)
				pn++;

			cout << pn() << endl;		// 30を16進数で表示
			cout << pn(5) << endl;		// 5桁表示

			return 0;
		}
		---------------------------------------------------------------------------
		[output]
		1E
		0001E
		**/
		string operator()(long long d = 0)
		{
			std::string positional_nation;

			each(e, symbol_index_collection_)
				positional_nation += symbols_[e];

			if (d == 0 || d == size(positional_nation))
				return positional_nation;

			if (size(positional_nation) < d)
			{	// 表示桁に空きがあるときは0で埋める
				std::string zero(d - size(positional_nation), '0');
				positional_nation = zero + positional_nation;
			}

			return positional_nation;
		}
		/**
		@brief d桁目を取得する演算子
		@note
		 d桁目の数字を文字列で取得する。
		-------------------------------------------------------------------------- -
		[sample]
		long long main()
		{	// 16進数テスト
			NyaGadgetPositionalNotation pn(16);

			for (long long i = 0; i < 30; i++)
				pn++;

			cout << pn.() << endl;		// 30を16進数で表示
			cout << pn[0] << endl;		// １桁目を表示
			cout << pn[1] << endl;		// ２桁目を表示

			return 0;
		}
		-------------------------------------------------------------------------- -
		[output]
		1E
		E
		1
		**/
		char operator[](long long d)
		{	// d桁目の数字を取得する
			long long index = size(symbol_index_collection_) - 1 - d;
			if (index < 0)
				return symbols_[0][0];
			return symbols_[symbol_index_collection_[index]][0];
		}
	};

#undef each
#undef size

}

struct Match {
	ll v;
	ll n;

	bool operator < (const Match& m) const
	{
		if (n != m.n)
			return n < m.n;
		return v > m.v;
	}
};

#define all(collection) (collection).begin(), (collection).end()              // begin to end
#define divc(a, b) ceil((ld) a / (ld) b);		                              // div ceiling
#define each(e, i, v) ll i=0;for(auto e=v.begin();e != v.end(); e++, i++)     // for each
#define rep(i, begin, end) for (ll i = begin; i < end; i++)                   // repeat
#define repr(i, begin, end) for (ll i = begin; end < i; i--)                  // repeat reverse
#define size(collection) ((ll) (collection).size())                           // collection size

using namespace NyaGadget;

int main(void)
{
	ll N, M; cin >> N >> M;
	vector<Match> match(M);
	{
		each(e, i, match)
		{
			cin >> e->v;
			e->n= (e->v == 1) ? 2 : (e->v == 2) ? 5 : (e->v == 3) ? 5 : (e->v == 4) ? 4 : 
				(e->v == 5) ? 5 : (e->v == 6) ? 6 : (e->v == 7) ? 3 : (e->v == 8) ? 7 : (e->v == 9) ? 6 : 0 ;
		}
	}
	sort(all(match));

	ll sum = 0;
	BaseNP np(M);
	while (!(N - sum >= 0 && (N - sum) % match[0].n == 0))
	{
		++np;
		string s = np();

		sum = 0;
		each(e, i, s)
		{
			sum += match[((ll)*e - '0')].n;
		}
	}

	vector<ll> ans;
	{
		if (sum != 0)
		{
			string s = np();
			each(e, i, s)
				ans.push_back(match[(ll)* e - '0'].v);
		}
		rep(j, 0, (N - sum) / match[0].first)
			ans.push_back(match[0].v);
	}

	sort(all(ans), greater<ll>());
	each(e, i, ans)
		cout << *e;

	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:200:48: error: '__gnu_cxx::__alloc_traits<std::allocator<Match>, Match>::value_type' {aka 'struct Match'} has no member named 'first'
  200 |                 rep(j, 0, (N - sum) / match[0].first)
      |                                                ^~~~~
a.cc:158:51: note: in definition of macro 'rep'
  158 | #define rep(i, begin, end) for (ll i = begin; i < end; i++)                   // repeat
      |                                                   ^~~