Language: C++

// https://ynucpc.github.io/blog/2018/04/15/cp_vscode/
// http://www.atmarkit.co.jp/ait/articles/1509/08/news019.html

#include <bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define rep1(i,n) for(ll i=1;i<=(ll)(n);i++)
#define rep3(i,n,m) for(ll i=(n);i<(ll)(m);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << (x) << endl
// #define max(a, b) ((a) > (b) ? (a) : (b))
// #define min(a, b) ((a) < (b) ? (a) : (b)) 
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
using ll = long long;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
int stoi (string s, int tmp=10){
	istringstream stm(s);
	int res;
	stm >> res;
	return res;
}
double stod(string s, int tmp=10){
	istringstream stm(s);
	double res;
	stm >> res;
	return res;
}

template<typename T>string to_string(const T& n){
	std::ostringstream stm;
	stm << n;
	return stm.str();
}


// 降順ソート: sort(A, A+N, greater<>());
// min: *min_element(A, A+N)
// argmin: min_element(A, A+N)-A

ll gcd(ll a, ll b){
	if (a==0){
		return b;
	}else{
		return gcd(b%a, a);
	}
}


ll m[10] = {0,2,5,5,4,5,6,3,7,6};
ll usable[10] = {};
V<ll> v;
ll aaaaa(ll a){
	//show(a);
	if (a==0)return 0;
	else if(a<0)return -1ll<<60;
	else{
		ll res = -1ll<<60;
		for(auto vv : v){
			ll r = aaaaa(a-vv);
			if (r < 0){continue;}
			ll re;
			if(r!=0){
				re = stoi(to_string(usable[vv]) + to_string(r));
				//show(re);
			}else{
				re = usable[vv];
			}
			//show(re);
			chmax(res, re);
		}
		return res;
	}
}

int main(){	
	// ll A;
    // cin >> A;
	// cout << "hello" << endl;
	// cout << stoi("14") << endl;
	// cout << max({1,3}) << endl;
	ll N, M;
	cin >> N >> M;
	ll A[M];
	
	rep(i, M){
		cin >> A[i];
		chmax(usable[m[A[i]]], A[i]);
	}
	ll mi;
	rep(i, 10)if(usable[i]!=0){
		mi = i;
		break;
	}
	ll keta = N/mi;
	ll mo = N%mi;
	
	rep(i,10)if(usable[i]!=0&&i!=mi)v.push_back(i);
	
	//show(v);
	while(1){
		keta--;
		mo+=mi;
		ll aa = aaaaa(mo);
		//show(aa);
		if (aa<0)continue;
		string saa = to_string(aa);
		bool f = true;
		rep(i, saa.size()){
			if (!f || saa[i]-'0'>usable[mi])cout << saa[i];
			else {
				rep(i, keta)cout << usable[mi];
				cout << saa[i];
				f = false;
			}
		}
		if (f)rep(i, keta)cout << usable[mi];
		cout << endl;
		break;

	}


}








Compiler output:
a.cc: In function 'll aaaaa(ll)':
a.cc:77:42: error: call of overloaded 'stoi(std::__cxx11::basic_string<char>)' is ambiguous
   77 |                                 re = stoi(to_string(usable[vv]) + to_string(r));
      |                                      ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:30:5: note: candidate: 'int stoi(std::string, int)'
   30 | int stoi (string s, int tmp=10){
      |     ^~~~
In file included from /usr/include/c++/14/string:54,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:4:
/usr/include/c++/14/bits/basic_string.h:4164:3: note: candidate: 'int std::__cxx11::stoi(const std::string&, std::size_t*, int)'
 4164 |   stoi(const string& __str, size_t* __idx = 0, int __base = 10)
      |   ^~~~