Language: C++

#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define start_routine() int begtime = clock();
#define end_routine() int endtime = clock(); \
					  cerr << "\n\n" << "Time elapsed: " << \
					  (endtime - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n"; \
					  return 0
#define ll long long int
#define ull unsigned long long int
#define db long double
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound 
#define pii pair<int , int>
#define pdd pair<db , db>
#define pll pair<ll , ll>
#define vpl vector<pll >
#define vll vector<ll >
#define mod 1000000007
#define inf 100000000000000007
#define eps 0.000001
#define stp fixed<<setprecision(20)
#define endl '\n'

ll p1=101,mod1=17093097961,p2=53,mod2=491845979;
pair<pll,ll> dp[10005];
pll dp1[10005];
pll prv[10005];
pll prv1[10005];
bool vis[10005];
bool act[10];
ll a[10]={0,2,5,5,4,5,6,3,7,6};
string s,s1;

inline void rec(pll node)
{
	char c=node.ss+'0';
	s+=c;
	if(node.ff==0)
		return;

	rec(prv[node.ff]);
}
inline void rec1(pll node)
{
	char c=node.ss+'0';
	s+=c;
	if(node.ff==0)
		return;

	rec1(prv1[node.ff]);
}

int main()
{
	fastio;
	#ifdef APNA_IO
		start_routine();
        freopen("input.txt", "rt", stdin);
        freopen("output.txt", "wt", stdout);
    #endif
 	
 	vis[0]=1;
 	vis1[0]=1;
 	ll n,m;cin>>n>>m;
 	for(ll i=0;i<m;i++)
 	{
 		ll x;cin>>x;
 		act[x]=1;
 	}
 	
 	for(ll i=1;i<=n;i++)
 	{
 		for(ll k=1;k<=9;k++)
 		{
 			if(act[k])
 			{
 				if(i-a[k]<0)
 					continue;

 				if(!vis[i-a[k]])
 					continue;

 				vis[i]=1;
 				ll x1=((dp[i-a[k]].ff.ff*p1)%mod1+k)%mod1;
 				ll x2=((dp[i-a[k]].ff.ss*p2)%mod2+k)%mod2;
 				ll y=dp[i-a[k]].ss+1;
 				ll y1=dp1[i-a[k]].ss+1;
 				if(y>dp[i].ss)
 				{
 					dp[i].ss=y;
 					dp[i].ff={x1,x2};
 					prv[i]={i-a[k],k};
 				}
 				else if(y==dp[i].ss && x1>dp[i].ff.ff && x2>dp[i].ff.ss)
 				{
 					dp[i].ff={x1,x2};
 					prv[i]={max(i-a[k],0ll),k};
 				}
 				else if(y==dp[i].ss && x1==dp[i].ff.ff && x2==dp[i].ff.ss && k>prv[i].ss)
 				{
 						prv[i].ss=k;
 				}
 			}

 			if(act[k])
 			{
 				if(i-a[k]<0)
 					continue;

 				if(!vis1[i-a[k]])
 					continue;

 				vis1[i]=1;
 				ll x1=((dp1[i-a[k]].ff*p1)%mod1+k)%mod1;
 				ll y1=dp1[i-a[k]].ss+1;
 				if(y1>dp1[i].ss)
 				{
 					dp1[i].ss=y1;
 					dp1[i].ff=x1;
 					prv1[i]={i-a[k],k};
 				}
 				else if(y1==dp1[i].ss && x1>dp1[i].ff)
 				{
 					dp1[i].ff=x1;
 					prv1[i]={max(i-a[k],0ll),k};
 				}
 				else if(y1==dp1[i].ss && x1==dp1[i].ff && k>prv1[i].ss)
 				{
 						prv1[i].ss=k;
 				}
 			}
 		}
 	}

 	// cout<<prv[2].ff<<" "<<prv[2].ss<<endl;

 	rec(prv[n]);
 	sort(s.begin(), s.end());
 	reverse(s.begin(), s.end());
 	string s1=s;
 	// cout<<s1<<endl;

 	s="";
 	rec1(prv1[n]);
 	sort(s.begin(), s.end());
 	reverse(s.begin(), s.end());
 	// cout<<s<<endl;
 	
 	if(s1.length()>s.length())
 		s=s1;
 	else if(s1.length()==s.length() && s1>s)
 		s=s1;
 	cout<<s;

	#ifdef APNA_IO
		end_routine();
    #endif
}



Compiler output:
a.cc: In function 'int main()':
a.cc:68:9: error: 'vis1' was not declared in this scope; did you mean 'vis'?
   68 |         vis1[0]=1;
      |         ^~~~
      |         vis