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: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace AtCoder
{
	public class Program
	{
		//static int[] parent;
		//static long MAX_LONG = 100000000000;
		//static int MAX_NUM = 1000000000;
		static void Main(string[] args)
		{
			int[] input = Console.ReadLine().Split().Select(p => int.Parse(p)).ToArray();
			int N = input[0];
			int M = input[1];
			input = Console.ReadLine().Split().Select(p => int.Parse(p)).ToArray();

			// 数字?を作るのに必要なマッチの本数
			int[] match = new int[]{0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

			// dp[i] マッチ棒i本を使って作れる数字の最大
			string[] num = new string[N+10];
			// 初期値(使える数字一桁のところを埋める)
			for(int i=0; i<input.Length; i++)
			{
				int m = match[input[i]]; // input[i]を作るにはm本マッチが必要
				if(String.IsNullOrEmpty(num[m]))
				 	num[m] = input[i].ToString();
				else if(Int32.Parse(num[m]) < input[i])
					num[m] = input[i].ToString();
			}
			for(int i=2; i<N+1; i++)
			{
				// マッチ棒i本を使ってちょうど作れる数字があるか？
				if(!String.IsNullOrEmpty(num[i]))
				{
					// 初期値が入ってるところからしか数字を作れない
					for(int j=0; j<input.Length; j++)
					{
			//Console.Write(input[j] +  "を作るには");
						int m = match[input[j]]; // input[j]を作るにはm本マッチが必要
						//Console.WriteLine(m + "本マッチが必要");
						string s = createNewNum(num[i], input[j]); // sに新しい数字をくっつけて一桁大きくする
						//Console.WriteLine("新しい数字 " + s + " :" +(i+m) + ":" + num[i+m] + ".");
						if(String.IsNullOrEmpty(num[i+m]) || isBigger(s, num[i+m]))
						{
						//Console.WriteLine("入れ替えた");
							num[i+m] = s;
						}

					}
				}
			}

			Console.WriteLine(num[N]);
		}

		static bool isBigger(string big, string small)
		{
			// 桁数を比較する
			if(big.Length > small.Length)
				return true;
			else if(big.Length < small.Length)
				return false;
			else
			{
				if(big.Length == 0) return false;

				if(big == small) return false; // 全く同じ

				for(int i=0; i<big.Length; i++)
				{
					if(big[i] < small[i])
						return false;
				}
			}
			return true;
		}
		static string createNewNum(string org, int newnum)
		{
		//Console.WriteLine(org + " と " + newnum + " をくっつける");
			for(int i=0; i<org.Length; i++)
			{
				// i文字目以上の場所に入れる
				if((org[i]-'0') <= newnum)
				{
				//Console.WriteLine(" compare " + org[i] + " " + newnum);
					if(i == 0)
						return newnum.ToString() + org;

					// else
					return org.Substring(0, i) + newnum.ToString() + org.Substring(i);
				}
			}
			return org + newnum.ToString();
		}
	}
}