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;
        }
        struct combination
        {
            public int arg0_count;
            public int arg1_count;
            public int arg2_count;
            public int arg3_count;
            public int arg4_count;
            public int arg5_count;
        }
        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;
            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];//使える数値の最大個数は６個
            List<combination> candidate = new List<combination>(); //答えの候補を入れるリスト
            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;
                                            combination c = new combination();
                                            c.arg0_count = arg[0];
                                            c.arg1_count = arg[1];
                                            c.arg2_count = arg[2];
                                            c.arg3_count = arg[3];
                                            c.arg4_count = arg[4];
                                            c.arg5_count = arg[5];
                                            candidate.Add(c);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            string[] ans = new string[candidate.Count];
            int[] ans_int = new int[candidate.Count];
            int max_ans = 0;
            int max_candidateID = 0;
            for(var i = 0; i < candidate.Count; i++)
            {
                if(A[5].num!=99) for (var j = 0; j < candidate[i].arg5_count; j++) ans[i] += A[5].num.ToString();
                if(A[4].num!=99) for (var j = 0; j < candidate[i].arg4_count; j++) ans[i] += A[4].num.ToString();
                if(A[3].num != 99) for (var j = 0; j < candidate[i].arg3_count; j++) ans[i] += A[3].num.ToString();
                if(A[2].num != 99) for (var j = 0; j < candidate[i].arg2_count; j++) ans[i] += A[2].num.ToString();
                if(A[1].num != 99) for (var j = 0; j < candidate[i].arg1_count; j++) ans[i] += A[1].num.ToString();
                if(A[0].num != 99) for (var j = 0; j < candidate[i].arg0_count; j++) ans[i] += A[0].num.ToString();
            }
            for(var i = 0; i < candidate.Count; i++)
            {
                ans_int[i] = int.Parse(ans[i]);
                if (ans_int[i] > max_ans)
                {
                    max_ans = ans_int[i];
                    max_candidateID = i;
                }
            }
            List<pair> A2 = new List<pair>(); // リストA2
            pair[] p = new pair[6];
            p[0].num = A[0].num;
            p[0].matchstick = candidate[max_candidateID].arg0_count;
            p[1].num = A[1].num;
            p[1].matchstick = candidate[max_candidateID].arg1_count;
            p[2].num = A[2].num;
            p[2].matchstick = candidate[max_candidateID].arg2_count;
            p[3].num = A[3].num;
            p[3].matchstick = candidate[max_candidateID].arg3_count;
            p[4].num = A[4].num;
            p[4].matchstick = candidate[max_candidateID].arg4_count;
            p[5].num = A[5].num;
            p[5].matchstick = candidate[max_candidateID].arg5_count;
            for(var i = 0; i < 6; i++)
                {
                    A2.Add(p[i]);
                }
            A2.Sort((a, b) => b.num - a.num);//数値の大きい順 A2のmatchstickには使う回数を入れた
            for(var i = 0; i < 6; i++)
            {
                    if(A2[i].num != 99)
                    {
                    for (var j = 0; j < A2[i].matchstick; j++) Console.Write(A2[i].num);
                    }
            }
        }
    }
}