Problem:
Score : 400 points

Problem Statement

Find the largest integer that can be formed with exactly N matchsticks, under the following conditions:

Every digit in the integer must be one of the digits A_1, A_2, ..., A_M (1 \leq A_i \leq 9).

The number of matchsticks used to form digits 1, 2, 3, 4, 5, 6, 7, 8, 9 should be 2, 5, 5, 4, 5, 6, 3, 7, 6, respectively.

Constraints

All values in input are integers.

2 \leq N \leq 10^4

1 \leq M \leq 9

1 \leq A_i \leq 9

A_i are all different.

There exists an integer that can be formed by exactly N matchsticks under the conditions.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_M

Output

Print the largest integer that can be formed with exactly N matchsticks under the conditions in the problem statement.

Sample Input 1

20 4
3 7 8 4

Sample Output 1

777773

The integer 777773 can be formed with 3 + 3 + 3 + 3 + 3 + 5 = 20 matchsticks, and this is the largest integer that can be formed by 20 matchsticks under the conditions.

Sample Input 2

101 9
9 8 7 6 5 4 3 2 1

Sample Output 2

71111111111111111111111111111111111111111111111111

The output may not fit into a 64-bit integer type.

Sample Input 3

15 3
5 4 6

Sample Output 3

654

Solution in C (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<assert.h>
int a[10];
int b[10];
int c[10]={0,2,5,5,4,5,6,3,7,6};
int t,n,m;

int f(int d){
	if(a[d]){
		putchar('0'+d);
		n-=c[d];
		assert(n>=0);
		return 1;
	}
	return 0;
}

int g(int d){
	if(a[d]){
		b[d]++;
		n-=c[d];
		assert(n>=0);
		return 1;
	}
	return 0;
}

int main(){
	scanf("%d%d",&n,&m);
	for(int i=0;i<m;i++){
		scanf("%d",&t);
		a[t]++;
	}
	if(a[1]){
		//1を使う
		if(n%2==1){
			//1,1
			if(f(7));
			//3,1
			else if(f(5));
			else if(f(3));
			else if(f(2));
			//5,1
			else if(f(8));
			else assert(0);
		}
		for(int i=0;i<n/2;i++)putchar('1');
	}else if(a[7]){
		//7を使う
		if(n%3==1){
			//1,1
			if(f(4));
			//4,1
			else if(f(8));
			//4,2
			else if(g(5))g(5);
			else if(g(3))g(3);
			else if(g(2))g(2);
			else assert(0);
		}else if(n%3==2){
			//2,1
			if(g(5));
			else if(g(3));
			else if(g(2));
			//2,2
			else if(g(4))g(4);
			//8,2
			else if(f(8))f(8);
			else assert(0);
		}
		for(int i=0;i<n/3;i++)putchar('7');
	}else if(a[4]){
		//4を使う
		if(n%4==1){
			//1,1
			if(f(5));
			else if(g(3));
			else if(g(2));
			else if(a[8]){
				//5,2
				if(f(9))f(8);
				else if(a[6])f(8),f(6);
				//9,3
				else if(f(8))f(8),f(8);
				else assert(0);
			}
			else assert(0);
		}else if(n%4==2){
			//2,1
			if(f(9));
			else if(f(6));
			//2,2
			else if(f(5))f(5);
			else if(g(3))g(3);
			else if(g(2))g(2);
			//6,2
			else if(f(8))f(8);
			else assert(0);			
		}else if(n%4==3){
			//3,2
			if(n>=11&&a[9]&&(a[5]||a[3]||a[2])){
				f(9);
				if(f(5));
				else if(g(3));
				else if(g(2));
				else assert(0);
			}
			//3,1
			else if(f(8));
			//3,2
			else if(a[6]&&(a[5]||a[3]||a[2])){
				f(6);
				if(f(5));
				else if(g(3));
				else if(g(2));
				else assert(0);
			}
			//3,3
			else if(f(5))f(5),f(5);
			else if(g(3))g(3),g(3);
			else if(g(2))g(2),g(2);
			else assert(0);
		}
		for(int i=0;i<n/4;i++)putchar('4');
	}else if(a[5]||a[3]||a[2]){
		//532を使う
		if(n%5==1){
			//1,1
			if(f(9));
			else if(f(6));
			//6,3
			else if(f(8))f(8),f(8);
			else assert(0);
		}else if(n%5==2){
			//2,2
			if(n>=12&&f(9))f(9);
			//2,1
			else if(f(8));
			//2,2
			else if(f(6))f(6);
			else assert(0);
		}else if(n%5==3){
			//3,3
			if(n>=18&&f(9))f(9),f(9);
			//3,2
			else if(a[8]&&(a[6]||a[9])){
				if(a[9])f(9),f(8);
				else if(a[6])f(8),f(6);
				else assert(0);
			}
			//3,3
			else if(f(6))f(6),f(6);
			//8,4
			else if(f(8))f(8),f(8),f(8);
			else assert(0);
		}else if(n%5==4){
			//4,4
			if(n>=24&&f(9))f(9),f(9),f(9);
			//4,2
			else if(f(8))f(8);
			//4,4
			else if(f(6))f(6),f(6),f(6);
			else assert(0);
		}
		for(int i=0;i<n/5;i++)putchar(a[5]?'5':a[3]?'3':'2');
	}else if(a[9]){
		while(n%6)g(8);
		for(int i=0;i<n/6;i++)putchar('9');		
	}else if(a[6]){
		while(n%6)f(8);
		for(int i=0;i<n/6;i++)putchar('6');		
	}else if(a[8]){
		for(int i=0;i<n/7;i++)putchar('8');		
	}else assert(0);
	for(int i=9;i>0;i--)while(b[i]--)putchar('0'+i);
}