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)

#if DEBUG
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Contest
{
    class Program
    {
        public static IO.StreamScanner scanner = new IO.StreamScanner(Console.OpenStandardInput());

        static void Main(string[] args)
        {
            var n = scanner.Integer();
            var m = scanner.Integer();
            var a = scanner.ScanLine().Split().Select(s => s[0]);
            var ans = GetAns(new HashSet<char>(a), n);
            IO.Printer.Out.WriteLine(new string(ans.ToArray()));
            IO.Printer.Out.Flush();
        }

        static int[] matches = new int[] { 0, 2, 5, 5, 4, 5, 6, 3, 7, 6 };

        static Dictionary<int, List<char>> _memo = new Dictionary<int, List<char>>();

        public static List<char> GetAns(HashSet<char> chars, int n)
        {
            if (_memo.ContainsKey(n)) return _memo[n];

            var ret = new List<List<char>>();
            if (n < 0) return null;
            if (n == 0) return new List<char>();

            // 1 - 9
            for (int i = 1; i <= 9; i++)
            {
                if (!chars.Contains((char)(i + '0'))) continue;
                var match = matches[i];
                var append = GetAns(chars, n - match);
                if (append == null) continue;

                var cand = new List<char> { (char)(i + '0') };
                cand.AddRange(append);
                ret.Add(cand);
            }

            if (!ret.Any())
            {
                _memo[n] = null;
                return null;
            }

            var ans = GetLargest(ret);
            _memo[n] = ans;

            return ans;
        }

        static IComparer<List<char>> _comp = new Comp();

        static List<char> GetLargest(List<List<char>> values)
        {
            values = values.Where(v => v.Count() == values.Max(w => w.Count())).ToList();
            if (values.Count() == 1) return values.First();

            return values.OrderByDescending(v => v, _comp).First();
        }
    }

    public class Comp : IComparer<List<char>>
    {
        public int Compare(List<char> x, List<char> y)
        {
            for(int i=0; i<x.Count(); i++)
            {
                if (x[i] > y[i]) return 1;
                if (x[i] < y[i]) return -1;
            }
            return 0;
        }
    }


#if DEBUG
    [TestClass]
    public class Test
    {
        [TestMethod]
        public void Test1()
        {
            var n = 20;
            var m = 4;
            var a = new HashSet<char> { '3', '7', '8', '4' };
            var exp = new List<char> { '7', '7', '7', '7', '7', '3' };
            var ans = Program.GetAns(a, n);
            Assert.IsTrue(IsEq(exp, ans));
        }

        [TestMethod]
        public void Test2()
        {
            var n = 101;
            var m = 9;
            var a = new HashSet<char>("9 8 7 6 5 4 3 2 1".Split().Select(s => s[0]));
            var exp = "71111111111111111111111111111111111111111111111111".ToCharArray();
            var ans = Program.GetAns(a, n);
            Assert.IsTrue(IsEq(exp, ans));
        }

        [TestMethod]
        public void Test3()
        {
            var n = 15;
            var m = 3;
            var a = new HashSet<char>("5 4 6".Split().Select(s => s[0]));
            var exp = "654".ToCharArray();
            var ans = Program.GetAns(a, n);
            Assert.IsTrue(IsEq(exp, ans));
        }


        bool IsEq(IEnumerable<char> l1, IEnumerable<char> l2)
        {
            if (l1.Count() != l2.Count()) return false;
            for (int i = 0; i > l1.Count(); i++)
            {
                if (l1.ElementAt(i) != l2.ElementAt(i)) return false;
            }
            return true;
        }

        bool IsEq(IEnumerable<int> l1, IEnumerable<int> l2)
        {
            if (l1.Count() != l2.Count()) return false;
            for(int i=0; i>l1.Count(); i++)
            {
                if (l1.ElementAt(i) != l2.ElementAt(i)) return false;
            }
            return true;
        }

        bool IsEq(IEnumerable<long> l1, IEnumerable<long> l2)
        {
            if (l1.Count() != l2.Count()) return false;
            for (int i = 0; i > l1.Count(); i++)
            {
                if (l1.ElementAt(i) != l2.ElementAt(i)) return false;
            }
            return true;
        }

        bool IsEq(IEnumerable<double> l1, IEnumerable<double> l2)
        {
            if (l1.Count() != l2.Count()) return false;
            for (int i = 0; i > l1.Count(); i++)
            {
                if (l1.ElementAt(i) != l2.ElementAt(i)) return false;
            }
            return true;
        }

        bool IsEq(double d1, double d2)
        {
            return Math.Abs(d2 - d1) < 1E10;
        }

    }
#endif

    namespace IO
    {
        using System.IO;
        using System.Text;
        using System.Globalization;

        public class Printer : StreamWriter
        {
            static Printer()
            {
                Out = new Printer(Console.OpenStandardOutput()) { AutoFlush = false };
            }

            public static Printer Out { get; set; }

            public override IFormatProvider FormatProvider
            {
                get { return CultureInfo.InvariantCulture; }
            }

            public Printer(Stream stream)
                : base(stream, new UTF8Encoding(false, true))
            {
            }

            public Printer(Stream stream, Encoding encoding)
                : base(stream, encoding)
            {
            }

            public void Write<T>(string format, T[] source)
            {
                base.Write(format, source.OfType<object>().ToArray());
            }

            public void WriteLine<T>(string format, T[] source)
            {
                base.WriteLine(format, source.OfType<object>().ToArray());
            }
        }

        public class StreamScanner
        {
            public StreamScanner(Stream stream)
            {
                str = stream;
            }

            public readonly Stream str;
            private readonly byte[] buf = new byte[1024];
            private int len, ptr;
            public bool isEof;

            public bool IsEndOfStream
            {
                get { return isEof; }
            }

            private byte read()
            {
                if (isEof) return 0;
                if (ptr < len) return buf[ptr++];
                ptr = 0;
                if ((len = str.Read(buf, 0, 1024)) > 0) return buf[ptr++];
                isEof = true;
                return 0;
            }

            public char Char()
            {
                byte b;
                do b = read(); while ((b < 33 || 126 < b) && !isEof);
                return (char)b;
            }

            public string Scan()
            {
                var sb = new StringBuilder();
                for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
                    sb.Append(b);
                return sb.ToString();
            }

            public string ScanLine()
            {
                var sb = new StringBuilder();
                for (var b = Char(); b != '\n'; b = (char)read())
                    if (b == 0) break;
                    else if (b != '\r') sb.Append(b);
                return sb.ToString();
            }

            public long Long()
            {
                if (isEof) return long.MinValue;
                long ret = 0;
                byte b;
                var ng = false;
                do b = read(); while (b != 0 && b != '-' && (b < '0' || '9' < b));
                if (b == 0) return long.MinValue;
                if (b == '-')
                {
                    ng = true;
                    b = read();
                }
                for (; ; b = read())
                {
                    if (b < '0' || '9' < b)
                        return ng ? -ret : ret;
                    ret = ret * 10 + b - '0';
                }
            }

            public int Integer()
            {
                return (isEof) ? int.MinValue : (int)Long();
            }

            public double Double()
            {
                var s = Scan();
                return s != "" ? double.Parse(s, CultureInfo.InvariantCulture) : double.NaN;
            }

            static T[] enumerate<T>(int n, Func<T> f)
            {
                var a = new T[n];
                for (int i = 0; i < n; ++i) a[i] = f();
                return a;
            }

            public char[] Char(int n)
            {
                return enumerate(n, Char);
            }

            public string[] Scan(int n)
            {
                return enumerate(n, Scan);
            }

            public double[] Double(int n)
            {
                return enumerate(n, Double);
            }

            public int[] Integer(int n)
            {
                return enumerate(n, Integer);
            }

            public long[] Long(int n)
            {
                return enumerate(n, Long);
            }
        }
    }
}