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
{
    struct pair
    {
        public int num;
        public int weight;
    }
    struct union
    {
        public int[] arg;// 構造体の中の配列、使うときに初期化する
    }
    class Program
    {
        static void Main(string[] args)
        {
            // 整数の入力
            //int a = int.Parse(Console.ReadLine());
            // スペース区切りの整数の入力

            Dictionary<int, int> dict = new Dictionary<int, int>();
            dict[1] = 2;
            dict[2] = 5;
            dict[3] = 5;
            dict[4] = 4;
            dict[5] = 5;
            dict[6] = 6;
            dict[7] = 3;
            dict[8] = 7;
            dict[9] = 6;

            string[] input = Console.ReadLine().Split(' ');
            int N = int.Parse(input[0]);
            int M = int.Parse(input[1]);
            //

            List<pair> A = new List<pair>();
            List<union> B = new List<union>();
            var candidate = new union();

            for(var i = 1; i <= 9; i++)
            {
                var data = new pair();
                data.num = i;
                data.weight = N + 1;
                A.Add(data);
            }
            string[] input2 = Console.ReadLine().Split(' ');
            for (var i = 0; i < M; i++)
            {
                var data = new pair();
                data.num = int.Parse(input2[i]);
                data.weight = N + 1;
                A.Remove(data);
                data.weight = dict[int.Parse(input2[i])];
                A.Add(data);
            }
            //
            var s1 = new pair();
            var s2 = new pair();
            var s3 = new pair();
            var s4 = new pair();
            var s5 = new pair();
            s1.num = 5;
            s1.weight = 5;
            s2.num = 3;
            s2.weight = 5;
            s3.num = 2;
            s3.weight = 5;
            s4.num = 9;
            s4.weight = 6;
            s5.num = 6;
            s5.weight = 6;
            if(A.Contains(s1) && A.Contains(s2))
            {
                A.Remove(s2);
                s2.weight = N + 1;
                A.Add(s2);
            }
            if (A.Contains(s1) && A.Contains(s3))
            {
                A.Remove(s3);
                s3.weight = N + 1;
                A.Add(s3);
            }
            if (A.Contains(s2) && A.Contains(s3))
            {
                A.Remove(s3);
                s3.weight = N + 1;
                A.Add(s3);
            }
            if (A.Contains(s4) && A.Contains(s5))
            {
                A.Remove(s5);
                s5.weight = N + 1;
                A.Add(s5);
            }
            A.Sort((a, b) => b.num - a.num);
            A.Sort((a, b) => a.weight - b.weight);
            foreach(pair member in A)
            {
                //Console.WriteLine(member.num+" "+member.weight);
            }
            int sum = 0;
            int max = 0;
            int tmp = 0;
            int[] arg = new int[A.Count];

            for (arg[8] = 0; arg[8] <= N / A[8].weight; arg[8]++)
            {
                for (arg[7] = 0; arg[7] <= N / A[7].weight; arg[7]++)
                {
                    for (arg[6] = 0; arg[6] <= N / A[6].weight; arg[6]++)
                    {
                        for (arg[5] = 0; arg[5] <= N / A[5].weight; arg[5]++)
                        {
                            for (arg[4] = 0; arg[4] <= N / A[4].weight; arg[4]++)
                            {
                                for (arg[3] = 0; arg[3] <= N / A[3].weight; arg[3]++)
                                {
                                    for (arg[2] = 0; arg[2] <= N / A[2].weight; arg[2]++)
                                    {
                                        for (arg[1] = 0; arg[1] <= N / A[1].weight; arg[1]++)
                                        {
                                            for (arg[0] = 0; arg[0] <= N / A[0].weight; arg[0]++)
                                            {
                                                sum = 0;
                                                tmp = 0;
                                                for (var i = 0; i < A.Count; i++)
                                                {
                                                    sum += A[i].weight * arg[i];
                                                }
                                                if (sum > N)
                                                {
                                                    break;
                                                }
                                                else if (sum == N) // マッチの本数がぴったりの時
                                                {
                                                    for(var j = 0; j < 9; j++)
                                                    {
                                                        tmp += arg[j];
                                                    }
                                                    if (tmp >= max) // 桁数が大きいときだけ記録する
                                                    {
                                                        candidate.arg = new int[] { arg[0], arg[1], arg[2], arg[3], arg[4], arg[5], arg[6], arg[7], arg[8] };
                                                        B.Add(candidate);
                                                        max = tmp;
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            //
            List<pair>[] AS = new List<pair>[B.Count];
            for(var i = 0; i < B.Count; i++)
            {
                AS[i] = new List<pair>();
            }
            pair a2 = new pair();
            ulong max_ans = 0;
            //foreach (union  member in B) // Bは使う数値の組み合わせで桁数の多いもの　候補
            if (B.Count > 1)
            {
                for (var m = 0; m < B.Count; m++)
                {
                        for(var i = 0; i < 9; i++)
                        {
                            if (B[m].arg[i] != 0)
                            {
                            a2.num = A[i].num;
                            a2.weight = B[m].arg[i];
                            AS[m].Add(a2);
                            }
                        }
                    AS[m].Sort((a, b) => b.num - a.num);// numの大きい順
                    int cc = 0;
                    foreach(pair p in AS[m])
                    {
                    //Console.WriteLine(p.num+" "+p.weight); //ここのweightには使う回数を入れた
                    cc += p.weight;
                    }
                    //Console.WriteLine(cc);
                    string ans = "";
                    for(var n = 0; n <AS[m].Count; n++)
                    {
                        for(var p = 0; p < AS[m][n].weight; p++)
                        {
                        ans += AS[m][n].num.ToString();
                        }
                    }
                    //Console.WriteLine("ans="+ans);
                    if (ulong.Parse(ans) > max_ans)
                    {
                    max_ans = ulong.Parse(ans);
                    }
                }
                Console.WriteLine(max_ans);
            }
            else
            {
                    for (var i = 0; i < 9; i++)
                    {
                        if (B[0].arg[i] != 0)
                        {
                            a2.num = A[i].num;
                            a2.weight = B[0].arg[i];
                            AS[0].Add(a2);
                        }
                    }
                    AS[0].Sort((a, b) => b.num - a.num);// numの大きい順
                    int cc = 0;
                    foreach (pair p in AS[0])
                    {
                        //Console.WriteLine(p.num + " " + p.weight); //ここのweightには使う回数を入れた
                        cc += p.weight;
                    }
                    //Console.WriteLine(cc);
                    for (var n = 0; n < AS[0].Count; n++)
                    {
                        for (var p = 0; p < AS[0][n].weight; p++)
                        {
                        Console.Write(AS[0][n].num); 
                        }
                        
                    }
                Console.WriteLine();
            }
        }
    }
}