Language: C++

#include <iostream>
#include <cstring>
#include <string>    
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
ll n, m, can[10], buf;
int need[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

int main(){
	cin >> n >> m;
	memset(can, 0, sizeof(can));
	for(ll i = 1; i <= m; i++){
		cin >> buf;
		if(buf > can[need[buf]]){can[need[buf]] = buf;}
	}
	
	if(can[2] == 1){
		if(n%2 == 0){for(ll i = 1; i <= n/2; i++){cout << 1;} cout << endl;}
		else{
			if(can[3] != 0){cout << can[3]; for(ll i = 1; i <= n/2-1; i++){cout << 1;} cout << endl;}
			if(can[3] == 0 && can[5] != 0){cout << can[5]; for(ll i = 1; i <= n/2-2; i++){cout << 1;} cout << endl;}
			if(can[3] == 0 && can[5] == 0){cout << 8; for(ll i = 1; i <= n/2-3; i++){cout << 1;} cout << endl;}
		}
	}
	if(can[2] == 0 && can[3] == 7){
		if(n%3 == 0){for(ll i = 1; i <= n/3; i++){cout << 7;} cout << endl;}
		if(n%3 == 1){
			if(can[4] == 4){for(ll i = 1; i <= n/3-1; i++){cout << 7;} cout << 4 << endl;}
			if(can[4] == 0 && can[7] == 8){cout << 8; for(ll i = 1; i <= n/3-2; i++){cout << 7;} cout << endl;}
			if(can[4] == 0 && can[7] == 0){
				for(ll i = 1; i <= n/3-2; i++){cout << 7;}
				cout << can[5] << can[5] << endl;
			}
		}
		if(n%3 == 2){
			if(can[5] > can[4]){for(ll i = 1; i <= n/3-1; i++){cout << 7;} cout << can[5] << endl;}
			if(can[5] < can[4]){for(ll i = 1; i <= n/3-1; i++){cout << 7;} cout << can[4] << endl;}
			if(can[5] == can[4]){
				cout << 8; 
				for(ll i = 1; i <= n/3-2; i++){cout << 7;}
				cout << endl;
			}
		}
	}
	if(can[2] == 0 && can[3] == 0 && can[4] == 4){
		if(n%4 == 0){for(ll i = 1; i <= n/4; i++){cout << 4;} cout << endl;}
		if(n%4 == 1){
			if(can[5] == 5){cout << 5; for(ll i = 1; i <= n/4-1; i++){cout << 4;} cout << endl;}
			else{for(ll i = 1; i <= n/4-1; i++){cout << 4;} cout << can[5] << endl;}
		}
		if(n%4 == 2){
			if(can[6] != 0){
				cout << can[6]; for(ll i = 1; i <= n/4-1; i++){cout << 4;} cout << endl;
			}
			else{
				if(can[5] == 5){cout << 55; for(ll i = 1; i <= n/4-2; i++){cout << 4;} cout << endl;}
				else{for(ll i = 1; i <= n/4-2; i++){cout << 4;} cout << can[5] << can[5] << endl;}
			}
		}
		if(n%4 == 3){
			if(can[6] == 9 && can[5] != 0){
				if(can[5] == 5){cout << 95; for(ll i = 1; i <= n/4-2; i++){cout << 4;} cout << endl;}
				else{cout << 9; for(ll i = 1; i <= n/4-2; i++){cout << 4;} cout << can[5] << endl;}
			}
			if(can[6] != 9 && can[7] == 8){
				cout << 8; for(ll i = 1; i <= n/4-1; i++){cout << 4;} cout << endl;
			}
			if(can[6] == 6 && can[7] == 0){
				if(can[5] == 5){cout << 65; for(ll i = 1; i <= n/4-2; i++){cout << 4;} cout << endl;}
				else{cout << 6; for(ll i = 1; i <= n/4-2; i++){cout << 4;} cout << can[5] << endl;}
			}
			if(can[6] == 0 && can[7] == 0){
				if(can[5] == 5){cout << 555; for(ll i = 1; i <= n/4-3; i++){cout << 4;} cout << endl;}
				else{cout << 6; for(ll i = 1; i <= n/4-2; i++){cout << 4;} cout << can[5] << can[5] << can[5] << endl;}
			}
		}
	}
	if(can[2] == 0 && can[3] == 0 && can[4] == 0 && can[5] != 0){
		if(n%5 == 0){for(ll i = 1; i <= n/5; i++){cout << can[5];} cout << endl;}
		if(n%5 == 1){
			if(can[6] != 0){
				cout << can[6];
				for(ll i = 1; i <= n/5-1; i++){cout << can[5];}
				cout << endl;
			}
			else{
				cout << 888;
				for(ll i = 1; i <= n/5-2; i++){cout << can[5];}
				cout << endl;
			}
		}
		if(n%5 == 2){
			if(can[6] == 9){
				cout << 99;
				for(ll i = 1; i <= n/5-2; i++){cout << can[5];}
				cout << endl
			}
			if(can[6] != 9 && can[7] == 8){
				cout << 8;
				for(ll i = 1; i <= n/5-1; i++){cout << can[5];}
				cout << endl
			}
			if(can[6] == 6 && can[7] == 0){
				cout << 66;
				for(ll i = 1; i <= n/5-2; i++){cout << can[5];}
				cout << endl
			}
		}
		if(n%5 == 3){
			if(can[6] == 9){
				cout << 999;
				for(ll i = 1; i <= n/5-3; i++){cout << can[5];}
				cout << endl
			}
			if(can[6] == 6 && can[7] == 8){
				cout << 86;
				for(ll i = 1; i <= n/5-2; i++){cout << can[5];}
				cout << endl
			}
		}
		if(n%5 == 4){
			if(can[6] == 9){
				cout << 9999;
				for(ll i = 1; i <= n/5-4; i++){cout << can[5];}
				cout << endl
			}
			if(can[6] != 9 && can[7] == 8){
				cout << 88;
				for(ll i = 1; i <= n/5-2; i++){cout << can[5];}
				cout << endl
			}
			if(can[6] == 6 && can[7] == 0){
				cout << 6666;
				for(ll i = 1; i <= n/5-4; i++){cout << can[5];}
				cout << endl
			}
		}
	}
	if(can[2] == 0 && can[3] == 0 && can[4] == 0 && can[5] == 0 && can[6] != 0){
		if(n%6 == 0){
			for(ll i = 1; i <= n/6; i++){cout << can[6];}
			cout << endl;
		}
		if(n%6 != 0){
			if(can[6] == 6){
				for(ll i = 1; i <= n/6-n%6; i++){cout << 8;}
				for(ll i = 1; i <= n%6; i++){
					cout << 6 << endl;
				}
			}
			if(can[6] == 9){
				cout << 9;
				for(ll i = 1; i <= n/6-1; i++){cout << 8;}
				cout << endl;
			}
		}
	}
	if(can[2] == 0 && can[3] == 0 && can[4] == 0 && can[5] == 0 && can[6] == 0){
		for(ll i = 1; i <= n/7; i++){cout << 8;}
		cout << endl;
	}
	
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:100:45: error: expected ';' before '}' token
  100 |                                 cout << endl
      |                                             ^
      |                                             ;
  101 |                         }
      |                         ~                    
a.cc:105:45: error: expected ';' before '}' token
  105 |                                 cout << endl
      |                                             ^
      |                                             ;
  106 |                         }
      |                         ~                    
a.cc:110:45: error: expected ';' before '}' token
  110 |                                 cout << endl
      |                                             ^
      |                                             ;
  111 |                         }
      |                         ~                    
a.cc:117:45: error: expected ';' before '}' token
  117 |                                 cout << endl
      |                                             ^
      |                                             ;
  118 |                         }
      |                         ~                    
a.cc:122:45: error: expected ';' before '}' token
  122 |                                 cout << endl
      |                                             ^
      |                                             ;
  123 |                         }
      |                         ~                    
a.cc:129:45: error: expected ';' before '}' token
  129 |                                 cout << endl
      |                                             ^
      |                                             ;
  130 |                         }
      |                         ~                    
a.cc:134:45: error: expected ';' before '}' token
  134 |                                 cout << endl
      |                                             ^
      |                                             ;
  135 |                         }
      |                         ~                    
a.cc:139:45: error: expected ';' before '}' token
  139 |                                 cout << endl
      |                                             ^
      |                                             ;
  140 |                         }
      |                         ~