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: Accepted)

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;
    }

    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();

        //最大の数字[使用する文字数]なDP表
        int UB = N;
        string[] DPArr = new string[UB + 1];
        for (int I = 0; I <= UB; I++)
            DPArr[I] = "";

        for (int I = 0; I <= SortedKeysArr.GetUpperBound(0); I++) {
            for (int J = 0; J <= UB; J++) {
                if (J > 0 && DPArr[J] == "") continue;

                int NewJ = J + CostDict[SortedKeysArr[I]];
                if (NewJ > UB) continue;

                string NewVal = DPArr[J] + SortedKeysArr[I].ToString();
                if (StringComp(NewVal, DPArr[NewJ]) == -1) continue;
                DPArr[NewJ] = NewVal;
            }
        }
        Console.WriteLine(DPArr[UB]);
    }

    //文字列を数値として比較した結果を返す
    //p1のほうが大きければ1
    //p2のほうが大きければ-1
    //p1とp2が等しければ0
    static int StringComp(string p1, string p2)
    {
        if (p1.Length < p2.Length) return -1;
        if (p1.Length > p2.Length) return 1;
        return p1.CompareTo(p2);
    }
}