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: Time Limit Exceeded)

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("20 4");
            WillReturn.Add("3 7 8 4");
            //777773
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("101 9");
            WillReturn.Add("9 8 7 6 5 4 3 2 1");
            //71111111111111111111111111111111111111111111111111
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("15 3");
            WillReturn.Add("5 4 6");
            //654
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct JyoutaiDef
    {
        internal int Rest;
        internal int CurrInd;
        internal string ConcatStr;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] wkArr = InputList[0].Split(' ').Select(X => int.Parse(X)).ToArray();
        int N = wkArr[0];

        int[] AArr = InputList[1].Split(' ').Select(X => int.Parse(X)).ToArray();

        var CostDict = new Dictionary<int, int>();
        if (AArr.Contains(1)) CostDict.Add(1, 2);
        if (AArr.Contains(2)) CostDict.Add(2, 5);
        if (AArr.Contains(3)) CostDict.Add(3, 5);
        if (AArr.Contains(4)) CostDict.Add(4, 4);
        if (AArr.Contains(5)) CostDict.Add(5, 5);
        if (AArr.Contains(6)) CostDict.Add(6, 6);
        if (AArr.Contains(7)) CostDict.Add(7, 3);
        if (AArr.Contains(8)) CostDict.Add(8, 7);
        if (AArr.Contains(9)) CostDict.Add(9, 6);

        if (CostDict.ContainsKey(1)) CostDict.Remove(4);
        if (CostDict.ContainsKey(1)) CostDict.Remove(6);
        if (CostDict.ContainsKey(1)) CostDict.Remove(9);

        if (CostDict.ContainsKey(3)) CostDict.Remove(2);

        if (CostDict.ContainsKey(5)) CostDict.Remove(2);
        if (CostDict.ContainsKey(5)) CostDict.Remove(3);

        if (CostDict.ContainsKey(7)) CostDict.Remove(6);
        if (CostDict.ContainsKey(7)) CostDict.Remove(9);

        if (CostDict.ContainsKey(9)) CostDict.Remove(6);

        if (CostDict.ContainsKey(1) && CostDict.ContainsKey(2)) CostDict.Remove(8);
        if (CostDict.ContainsKey(1) && CostDict.ContainsKey(3)) CostDict.Remove(8);
        if (CostDict.ContainsKey(1) && CostDict.ContainsKey(5)) CostDict.Remove(8);

        if (CostDict.ContainsKey(1) && CostDict.ContainsKey(4)) CostDict.Remove(6);
        if (CostDict.ContainsKey(1) && CostDict.ContainsKey(4)) CostDict.Remove(9);

        if (CostDict.ContainsKey(1) && CostDict.ContainsKey(7)) {
            CostDict.Remove(2);
            CostDict.Remove(3);
            CostDict.Remove(4);
            CostDict.Remove(5);
            CostDict.Remove(6);
            CostDict.Remove(8);
            CostDict.Remove(9);
        }

        if (CostDict.ContainsKey(4) && CostDict.ContainsKey(7)) CostDict.Remove(8);

        int[] SortedKeysArr = CostDict.Keys.OrderByDescending(X => X).ToArray();

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.Rest = N;
        WillPush.ConcatStr = "";
        WillPush.CurrInd = 0;
        Stk.Push(WillPush);

        var AnswerList = new List<string>();

        var MemoDict = new Dictionary<int, string>();
        while (Stk.Count > 0) {
            JyoutaiDef Popped = Stk.Pop();

            if (Popped.Rest == 0) {
                //Console.WriteLine("解候補{0}を発見", Popped.ConcatStr);
                AnswerList.Add(Popped.ConcatStr);
                continue;
            }

            for (int I = Popped.CurrInd; I <= SortedKeysArr.GetUpperBound(0); I++) {
                //使用数のループ
                var sb = new System.Text.StringBuilder();
                for (int J = 1; J < int.MaxValue; J++) {
                    sb.Append(SortedKeysArr[I]);
                    WillPush.Rest = Popped.Rest - J * CostDict[SortedKeysArr[I]];
                    if (WillPush.Rest < 0) break;

                    WillPush.ConcatStr = Popped.ConcatStr + sb.ToString();
                    WillPush.CurrInd = I + 1;

                    if (MemoDict.ContainsKey(WillPush.Rest)) {
                        string CurrMax = MemoDict[WillPush.Rest];
                        if (CurrMax.Length > WillPush.ConcatStr.Length) continue;
                        if (CurrMax.Length == WillPush.ConcatStr.Length &&
                            CurrMax.CompareTo(WillPush.ConcatStr) > 0) continue;
                    }
                    MemoDict[WillPush.Rest] = WillPush.ConcatStr;
                    Stk.Push(WillPush);
                }
            }
        }

        Console.WriteLine(AnswerList.OrderByDescending(X => X.Length).ThenByDescending(X => X).First());
    }
}