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: Runtime Error)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder118D_3
{
    class Program
    {
        struct pair
        {
            public int num;
            public int matchstick;
        }
        static void Main(string[] args)
        {
            Dictionary<int, int> dictionary = new Dictionary<int, int>();
            dictionary[1] = 2;
            dictionary[2] = 5;
            dictionary[3] = 5;
            dictionary[4] = 4;
            dictionary[5] = 5;
            dictionary[6] = 6;
            dictionary[7] = 3;
            dictionary[8] = 7;
            dictionary[9] = 6;
            List<Dictionary<int, int>> counter = new List<Dictionary<int, int>>();


            string[] input = Console.ReadLine().Split(' ');
            int N = int.Parse(input[0]); // マッチ棒の本数
            int M = int.Parse(input[1]);// 使える数値の個数
            string[] input2 = Console.ReadLine().Split(' ');
            List<pair> A = new List<pair>();
            int[] skip = new int[M];
            for(var i = 0; i < M; i++)
            {
                skip[i]=int.Parse(input2[i]);// 省略できる数値かどうか、後でチェックするための変数
                pair number = new pair();
                number.num = skip[i];
                number.matchstick = dictionary[number.num];
                A.Add(number);
            }
            if (N % 2 == 0 && skip.Contains(1))
            {
                for (var i = 0; i<N / 2; i++) Console.Write("1");
                Environment.Exit(0);
            }
            if (N % 2 == 1 && skip.Contains(1) && skip.Contains(7))
            {
                Console.Write("7");
                for (var i = 0; i < (N - 3) / 2; i++) Console.Write("1");
                Environment.Exit(0);
            }
            // skip配列をチェックして、省略できる数値を削除する
            if (skip.Contains(5) && skip.Contains(3))
            {
                pair omission = new pair();
                omission.num = 3;
                omission.matchstick = 5;
                A.Remove(omission);
            }
            if (skip.Contains(5) && skip.Contains(2))
            {
                pair omission = new pair();
                omission.num = 2;
                omission.matchstick = 5;
                A.Remove(omission);
            }
            if (skip.Contains(3) && skip.Contains(2))
            {
                pair omission = new pair();
                omission.num = 2;
                omission.matchstick = 5;
                A.Remove(omission);
            }
            if (skip.Contains(9) && skip.Contains(6))
            {
                pair omission = new pair();
                omission.num = 6;
                omission.matchstick = 6;
                A.Remove(omission);
            }
            A.Sort((a, b) => b.matchstick - a.matchstick);//使うマッチ棒の多い順にソート
            int count_A = A.Count;
            pair px = new pair();
            px.num = 99;
            px.matchstick = N;
            for(var i = count_A; i < 6; i++)
            {
                A.Insert(0, px);
            }
            int[] arg = new int[6];//使える数値の最大個数は６個
            int max = 0;
            for(arg[0]=0;arg[0]<N/A[0].matchstick;arg[0]++)
            {
                for (arg[1] = 0; arg[1] < N / A[1].matchstick; arg[1]++)
                {
                    for (arg[2] = 0; arg[2] < N / A[2].matchstick; arg[2]++)
                    {
                        for (arg[3] = 0; arg[3] < N / A[3].matchstick; arg[3]++)
                        {
                            for (arg[4] = 0; arg[4] < N / A[4].matchstick; arg[4]++)
                            {
                                for (arg[5] = 0; arg[5] < N / A[5].matchstick; arg[5]++)
                                {
                                    if(N==arg[0]* A[0].matchstick+ arg[1] * A[1].matchstick + arg[2] * A[2].matchstick +
                                        arg[3] * A[3].matchstick +arg[4] * A[4].matchstick + arg[5] * A[5].matchstick)
                                    {
                                        int tmp = arg[0] + arg[1] + arg[2] + arg[3] + arg[4] + arg[5];
                                        if (max <= tmp) //桁の大きさが最大、または同じなら候補になる
                                        {
                                            max = tmp;
                                            Dictionary<int, int> c = new Dictionary<int, int>();
                                            for (var j = 1; j <= 9; j++) c[j] = 0;
                                            for (var i = 5; i >= 0; i--) c[A[i].num] = arg[i];
                                            counter.Add(c);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            int ansID = 0;
            int column_Max = 0;
            /*
            for(var i = 0; i < counter.Count; i++)
            {
                for(var j = 1; j < 9; j++)
                {
                if (counter[i][j] > ans_number)
                {
                    ans_number = i;
                        break;
                }
                }
            }
            */
            for(var i = 9; i >= 1; i--)
            {
                for(var j = 0; j < counter.Count; j++)
                {
                    if (counter[j][i] > column_Max)
                    {
                        column_Max = counter[j][i];
                        ansID = j;
                    }
                }
                    if (column_Max != 0) break;
            }
            //Console.WriteLine("ans_number=" + ans_number);
            for (var i = 9; i >=1; i--)
            {
                for(var j = 0; j<counter[ansID][i];j++)
                {
                Console.Write(i);
                }
            }
        }
    }
}