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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
class Program
{
	static void Main(string[] args)
	{
		string[] str = Console.ReadLine().Split();
		int N = int.Parse(str[0]);
		int M = int.Parse(str[1]);
		int[]A = new int[M];
		string[] str2 = Console.ReadLine().Split();
		for(var i=0;i<M;i++){
			A[i] = int.Parse(str2[i]);
		}
		int[]E = new int[8];
		int[]EM = new int[8];
		for(var i=0;i<8;i++){
			E[i] = 99999;
		}
		int[]G = new int[10]{0,2,5,5,4,5,6,3,7,6};
		for(var i=0;i<M;i++){
			if(A[i]==1){
				E[2] = 2;
				EM[2] = A[i];
			} else if(A[i]==7){
				E[3] = 3;
				EM[3] = A[i];
			} else if(A[i]==4){
				E[4] = 4;
				EM[4] = A[i];
			} else if(A[i]==2 || A[i]==3 || A[i]==5){
				E[5] = 5;
				EM[5] = Math.Max(EM[5],A[i]);
			} else if(A[i]==6 || A[i]==9){
				E[6] = 6;
				EM[6] = Math.Max(EM[6],A[i]);
			} else if(A[i]==8){
				E[7] = 7;
				EM[7] = A[i];
			}
		}
		int[]DP = new int[N+1];
		int[,]DA = new int[N+1,8];
		for(var i=1;i<=N;i++){
			DP[i] = -1;
		}
		for(var i=1;i<=N;i++){
			for(var j=7;j>=2;j--){
				if(i-j>=0 && DP[i-j]!=-1){
					if(E[j] < 8){
						bool flag = false;
						if(DP[i]==-1 || DP[i-j]+1>DP[i]){
							flag = true;
						} else if(DP[i-j]+1==DP[i]){
							for(var k=9;k>=1;k--){
								if(E[G[k]]<8){
									int ta = 0;
									if(G[k]==j){
										ta = 1;
									}
									if(DA[i-j,G[k]]+ta>DA[i,G[k]]){
										flag = true;
										break;
									} else if(DA[i-j,G[k]]+ta<DA[i,G[k]]){
										flag = false;
										break;
									}
								}
							}
						}
						if(flag){
							for(var k=0;k<8;k++){
								DA[i,k] = DA[i-j,k];
							}
							DP[i] = DP[i-j]+1;
							DA[i,j] += 1;
						}
					}
				}
			}
		}
		//for(var i=0;i<=N;i++){
		//	for(var j=0;j<8;j++){
		//		Console.Write(DA[i,j]);
		//	}
		//	Console.WriteLine(" "+DP[i]);
		//}
		var list = new List<int>();
		int dn = N;
		for(var i=0;i<DP[N];i++){
			for(var j=9;j>=1;j--){
				if(EM[G[j]]==j && E[G[j]]<8){
					if(dn-E[G[j]]>=0 && DP[dn-E[G[j]]] == DP[dn]-1){
						list.Add(j);
						dn -= G[j];
						break;
					}
				}
			}
		}
		for(var i=0;i<list.Count;i++){
			Console.Write(list[i]);
		}
	}
}