Language: C++

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

int main(){
	cin >> n >> m;
	memset(can, 0, sizeof(can));
	memset(out, 0, sizeof(out));
	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){out[1] += n/2;}
		else{
			if(can[3] == 7){
				out[7]++;
				out[1] += n/2-1;
			}
			if(can[3] == 0 && can[5] != 0){
				out[can[5]]++;
				out[1] += n/2-2;
			}
			if(can[3] == 0 && can[5] == 0){
				out[8]++;
				out[1] += n/2-3;
			}
		}
	}
	if(can[2] == 0 && can[3] == 7){
		if(n%3 == 0){out[7] += n/3;}
		if(n%3 == 1){
			if(can[4] == 4){
				out[7] += n/3-1;
				out[4]++;
			}
			if(can[4] == 0 && can[5] != 0){
				out[7] += n/3-2;
				out[can[5]] += 2;
			}
			if(can[4] == 0 && can[5] == 0){
				out[8]++;
				out[7] += n/3-2;
			}
			
		}
		if(n%3 == 2){
			if(can[5] != 0){
				out[7] += n/3-1;
				out[can[5]]++;
			}
			if(can[5] == 0 && can[4] == 4){
				out[7] += n/3-2;
				out[4] += 2;
			}
			if(can[5] == 0 && can[4] == 0){
				out[8]++;
				out[7] += n/3-2;
			}
		}
	}
	if(can[2] == 0 && can[3] == 0 && can[4] == 4){
		if(n%4 == 0){out[4] += n/4;}
		if(n%4 == 1){
			if(can[5] != 0){
				out[4] += n/4-1;
				out[can[5]]++;
			}
			else{
				out[8] += 2;
				out[4] += n/4-2;
			}
		}
		if(n%4 == 2){
			if(can[6] != 0){
				out[can[6]]++;
				out[4] += n/4-1;
			}
			else{
				out[can[5]] += 2;
				out[4] += n/4-2;
			}
		}
		if(n%4 == 3){
			if(can[6] == 9 && can[5] != 0){
				out[9]++;
				out[can[5]]++;
				out[4] += n/4-2;
			}
			if(can[6] != 9 && can[7] == 8){
				out[8]++;
				out[4] += n/4-1;
			}
			if(can[6] == 6 && can[7] == 0){
				out[6]++;
				out[can[5]]++;
				out[4] += n/4-2;
			}
			if(can[6] == 0 && can[7] == 0){
				out[can[5]] += 3;
				out[4] += n/4-3;
			}
		}
	}
	if(can[2] == 0 && can[3] == 0 && can[4] == 0 && can[5] != 0){
		if(n%5 == 0){out[can[5]] += n/5;}
		if(n%5 == 1){
			if(can[6] != 0){
				out[can[6]]++;
				out[can[5]] += n/5-1;
			}
			else{
				out[8] += 3
				out[can[5]] += n/5-2;
			}
		}
		if(n%5 == 2){
			if(can[6] > can[7]){
				out[9] += 2;
				out[can[5]] += n/5-2;
			}
			else{
				out[8]++;
				out[can[5]] += n/5-1;
			}
		}
		if(n%5 == 3){
			if(can[6] > can[7]){
				out[9] += 3;
				out[can[5]] += n/5-3;
			}
			else{
				out[8]++;
				out[6]++;
				out[can[5]] += n/5-2;
			}
		}
		if(n%5 == 4){
			if(can[6] > can[7]){
				out[can[6]] += 4;
				out[can[5]] += n/5-4;
			}
			else{
				out[8] += 2;
				out[can[5]] += n/5-2;
			}
		}
	}
	if(can[2] == 0 && can[3] == 0 && can[4] == 0 && can[5] == 0 && can[6] != 0){
		if(n%6 == 0){out[can[6]] += n/6;}
		if(n%6 != 0){
			out[can[6]] += n%6;
			out[8] += n/6-n%6;
		}
	}
	if(can[2] == 0 && can[3] == 0 && can[4] == 0 && can[5] == 0 && can[6] == 0){
		out[8] += n/7;
	}
	
	for(ll i = 9; i >= 1; i--){
		for(ll j = 1; j <= out[i]; j++){
			cout << i;
		}
	}
	cout << endl;
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:120:44: error: expected ';' before 'out'
  120 |                                 out[8] += 3
      |                                            ^
      |                                            ;
  121 |                                 out[can[5]] += n/5-2;
      |                                 ~~~