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;
using System.Text;

// using System.Numerics;
using System.Runtime.CompilerServices;
using ll=System.Int64;

public class Contest
{
	static void Swap1<T>(ref T a, ref T b) { T t = a; a = b; b = t; }
	static void Swap2<T>(ref T a, ref T b) where T : IComparable { if (a.CompareTo(b)==1) Swap1(ref a, ref b); }  // b の方が小さければ交換
	static bool Chmin<T>(ref T a, T b) where T : IComparable { if (a.CompareTo(b)== 1) { a = b; return true; } return false; }  // b の方が小さければ a を更新
	static bool Chmax<T>(ref T a, T b) where T : IComparable { if (a.CompareTo(b)==-1) { a = b; return true; } return false; }  // b の方が大きければ a を更新
	static T[][] InitMat<T>(long len1, long len2, T val) where T : struct { T[][] m = new T[len1][]; for (int i = 0; i < m.Length; i++) m[i] = Enumerable.Repeat(val, (int)len2).ToArray(); return m; }
	static T[][][] InitMat<T>(long len1, long len2, long len3, T val) where T : struct { T[][][] m = new T[len1][][]; for (int i = 0; i < m.Length; i++) m[i] = InitMat(len2,len3,val); return m; }
	public const long MOD1 = 1000000007; // 10^9+7
	public const double EPS1 = 1e-8;

	public static void Main() {
		Solve_D();
 	}

	public static void Solve_A() {
		long  n, m;
		MyIO.ReadMulti(out n, out m);



		long ans = 0;


		MyIO.WriteMulti(ans);
	}
	public static void Solve_B() {
		long  n, m;
		MyIO.ReadMulti(out n, out m);



		long ans = 0;


		MyIO.WriteMulti(ans);
	}
	public static void Solve_C() {
		long  n, m;
		MyIO.ReadMulti(out n, out m);



		long ans = 0;


		MyIO.WriteMulti(ans);
	}
	public static void Solve_D() {
		long  n, m;
		MyIO.ReadMulti(out n, out m);
		ll[] a;
		MyIO.ReadArray(out a);

		Array.Sort(a);
		SortedList<ll,ll> d1 = new SortedList<ll, ll>();
		foreach(var e in a){
			if(e==1) {
				d1[1] = 2;
			}
			if(e==2) {
				d1[2] = 5;
			}
			if(e==3) {
				if(d1.ContainsKey(2)) d1.Remove(2);
				d1[3] = 5;
			}
			if(e==4) {
				d1[4] = 4;
			}
			if(e==5) {
				if(d1.ContainsKey(2)) d1.Remove(2);
				if(d1.ContainsKey(3)) d1.Remove(3);
				d1[5] = 5;
			}
			if(e==6) {
				d1[6] = 6;
			}
			if(e==7) {
				d1[7] = 3;
			}
			if(e==8) {
				d1[8] = 7;
			}
			if(e==9) {
				if(d1.ContainsKey(6)) d1.Remove(6);
				d1[9] = 6;
			}
		}
		if(d1.ContainsKey(1)) {
			if(d1.ContainsKey(4)) d1.Remove(4);
			if(d1.ContainsKey(6)) d1.Remove(6);
			if(d1.ContainsKey(9)) d1.Remove(9);
		}
		if(d1.ContainsKey(7)) {
			if(d1.ContainsKey(6)) d1.Remove(6);
			if(d1.ContainsKey(9)) d1.Remove(9);
		}

		List<KeyValuePair<ll,ll>> dl = new List<KeyValuePair<ll, ll>>();
		StringBuilder ans = new StringBuilder();
		if(d1.Count==1){
			dl.Add(new KeyValuePair<ll, ll>(d1.Keys[0],n/d1.Values[0]));
		}
		if(d1.Count==2){
			ll maxs = 0;
			ll[][] maxa = new ll[2][];
			maxa[0] = new ll[2]{d1.Keys[0], 0};
			maxa[1] = new ll[2]{d1.Keys[1], 0};
			for (int i = 0; i <= n/d1.Values[0]; i++)
			{
				if((n-i*d1.Values[0])%d1.Values[1]==0){
					ll j = (n-i*d1.Values[0])/d1.Values[1];
					if(maxs<i+j){
						maxs = i+j;
						maxa[0][1] = i;
						maxa[1][1] = j;
					}
				}
			}
			Array.Sort(maxa,(x,y)=>x[0].CompareTo(y[0]));
			Array.Reverse(maxa);
			dl.Add(new KeyValuePair<ll, ll>(maxa[0][0],maxa[0][1]));
			dl.Add(new KeyValuePair<ll, ll>(maxa[1][0],maxa[1][1]));
		}
		if(d1.Count==3){
			ll maxs = 0;
			ll[][] maxa = new ll[3][];
			maxa[0] = new ll[2]{d1.Keys[2], 0};
			maxa[1] = new ll[2]{d1.Keys[1], 0};
			maxa[2] = new ll[2]{d1.Keys[0], 0};
			for (int i = 0; i <= n/d1.Values[2]; i++)
			{
				ll jlim = (n-i*d1.Values[2])/d1.Values[1];
				for (int j = 0; j <= jlim; j++)
				{
					if((n-i*d1.Values[2]-j*d1.Values[1])%d1.Values[0]==0){
						ll k = (n-i*d1.Values[2]-j*d1.Values[1])/d1.Values[0];
						if(maxs<i+j+k){
							maxs = i+j+k;
							maxa[0][1] = i;
							maxa[1][1] = j;
							maxa[2][1] = k;
						}else if(maxs==i+j+k){
							if((maxa[0][1]<i)||(maxa[0][1]==i&&maxa[1][1]<j)){
								maxa[0][1] = i;
								maxa[1][1] = j;
								maxa[2][1] = k;
							}
						}
					}
				}
			}
			Array.Sort(maxa,(x,y)=>x[0].CompareTo(y[0]));
			Array.Reverse(maxa);
			dl.Add(new KeyValuePair<ll, ll>(maxa[0][0],maxa[0][1]));
			dl.Add(new KeyValuePair<ll, ll>(maxa[1][0],maxa[1][1]));
			dl.Add(new KeyValuePair<ll, ll>(maxa[2][0],maxa[2][1]));
		}
		if(d1.Count==4){
			List<VT<ll,ll,ll>> vt1 = new List<VT<ll, ll, ll>>();
			for (int i = 0; i <= n/d1.Values[3]; i++)
			{
				ll jlim = (n-i*d1.Values[3])/d1.Values[2];
				for (int j = 0; j <= jlim; j++)
				{
					vt1.Add(new VT<ll, ll, ll>(i,j,i*d1.Values[3]+j*d1.Values[2]));
				}
			}
			List<VT<ll,ll,ll>> vt2 = new List<VT<ll, ll, ll>>();
			for (int i = 0; i <= n/d1.Values[1]; i++)
			{
				ll jlim = (n-i*d1.Values[1])/d1.Values[0];
				for (int j = 0; j <= jlim; j++)
				{
					vt2.Add(new VT<ll, ll, ll>(i,j,n-i*d1.Values[1]-j*d1.Values[0]));
				}
			}
			vt2.Sort((x,y)=>x.Item3.CompareTo(y.Item3));

			ll[][] maxa = new ll[4][];
			maxa[0] = new ll[2]{d1.Keys[3], 0};
			maxa[1] = new ll[2]{d1.Keys[2], 0};
			maxa[2] = new ll[2]{d1.Keys[1], 0};
			maxa[3] = new ll[2]{d1.Keys[0], 0};
			ll maxs = 0;
    	 	Comparer<VT<ll,ll,ll>> nor = Comparer<VT<ll,ll,ll>>.Create((x,y)=>x.Item3.CompareTo(y.Item3));

			foreach (var e in vt1)
			{
				int ib = (int)LowerBound(vt2,0,vt2.Count,e,nor);
				int ie = (int)UpperBound(vt2,0,vt2.Count,e,nor);
				for (int i = ib; i < ie; i++)
				{
					ll s = e.Item1+e.Item2+vt2[i].Item1+vt2[i].Item2;
					if(maxs<s){
						maxs = s;
						maxa[0][1] = e.Item1;
						maxa[1][1] = e.Item2;
						maxa[2][1] = vt2[i].Item1;
						maxa[3][1] = vt2[i].Item2;
					}else if(maxs==s){
						if((maxa[0][1]<e.Item1) || (maxa[0][1]==e.Item1&&maxa[1][1]<e.Item2) || (maxa[0][1]==e.Item1&&maxa[1][1]==e.Item2&&maxa[2][1]<vt2[i].Item1)){
							maxa[0][1] = e.Item1;
							maxa[1][1] = e.Item2;
							maxa[2][1] = vt2[i].Item1;
							maxa[3][1] = vt2[i].Item2;	
						}
					}
				}
			}
			Array.Sort(maxa,(x,y)=>x[0].CompareTo(y[0]));
			Array.Reverse(maxa);
			dl.Add(new KeyValuePair<ll, ll>(maxa[0][0],maxa[0][1]));
			dl.Add(new KeyValuePair<ll, ll>(maxa[1][0],maxa[1][1]));
			dl.Add(new KeyValuePair<ll, ll>(maxa[2][0],maxa[2][1]));
			dl.Add(new KeyValuePair<ll, ll>(maxa[3][0],maxa[3][1]));
		}


		foreach (var kv in dl)
		{
			for (int i = 0; i < kv.Value; i++)
			{
				ans.Append(kv.Key);
			}
		}





		MyIO.WriteMulti(ans.ToString());
	}
	public static void Solve_E() {
		long  n, m;
		MyIO.ReadMulti(out n, out m);



		long ans = 0;


		MyIO.WriteMulti(ans);
	}
	public static void Solve_F() {
		long  n, m;
		MyIO.ReadMulti(out n, out m);



		long ans = 0;


		MyIO.WriteMulti(ans);
	}




    public static long LowerBound<T>(IList<T> seq, int start, int end, T value, IComparer<T> comparer) {
        int mid;
        while (start < end) {
            mid = ((end - start) >> 1) + start;
            if (comparer.Compare(seq[mid], value) < 0)
            start = mid + 1;
            else
            end = mid;
        }
        return start;
    }
    //引数省略のオーバーロード
    public static long LowerBound<T>(IList<T> seq, T value) where T : IComparable => LowerBound(seq, 0, seq.Count, value, Comparer<T>.Default);

    public static long UpperBound<T>(IList<T> seq, int start, int end, T value, IComparer<T> comparer) {
        int mid;
        while (start < end) {
            mid = ((end - start) >> 1) + start;
            if (comparer.Compare(seq[mid], value) <= 0)
            start = mid + 1;
            else
            end = mid;
        }
        return start;
    }
    //引数省略のオーバーロード
    public static long UpperBound<T>(IList<T> seq, T value) where T : IComparable => UpperBound(seq, 0, seq.Count, value, Comparer<T>.Default);










}




















#region MyIO

public struct VT<T, U>
{
	public T Item1; public U Item2;
    public VT(T a, U b) { Item1 = a; Item2 = b; }
}
public struct VT<T, U, V>
{
	public T Item1; public U Item2; public V Item3;
    public VT(T a, U b, V c) { Item1 = a; Item2 = b; Item3 = c; }
}
public struct VT<T, U, V, W>
{
	public T Item1; public U Item2; public V Item3; public W Item4;
    public VT(T a, U b, V c, W d) { Item1 = a; Item2 = b; Item3 = c; Item4 = d; }
}

public class Printer : System.IO.StreamWriter
{
	public override IFormatProvider FormatProvider { get { return System.Globalization.CultureInfo.InvariantCulture; } }
	public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
	public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { }
}
public static class MyIO
{
	private static char[] splitter = new char[] {' '};
	private static bool eq<T, U>() => typeof(T).Equals(typeof(U));
	private static T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T));
	private static T cv<T>(string s) =>
		eq<T, int>()    ? ct<T, int>(int.Parse(s))
	  : eq<T, long>()   ? ct<T, long>(long.Parse(s))
	  : eq<T, double>() ? ct<T, double>(double.Parse(s, System.Globalization.CultureInfo.InvariantCulture))
	  : eq<T, char>()   ? ct<T, char>(s[0])
						: ct<T, string>(s);
			
	public static string[] ReadMulti<T>(out T a) {
		var ar = Console.ReadLine().Split(splitter, StringSplitOptions.RemoveEmptyEntries); 
		a = cv<T>(ar[0]);
		return ar;
	}
	public static string[] ReadMulti<T, U>(out T a, out U b) {
		var ar = ReadMulti<T>(out a); 
        b = cv<U>(ar[1]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V>(out T a, out U b, out V c) {
		var ar = ReadMulti<T, U>(out a, out b); 
        c = cv<V>(ar[2]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W>(out T a, out U b, out V c, out W d) {
		var ar = ReadMulti<T, U, V>(out a, out b, out c); 
        d = cv<W>(ar[3]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) {
		var ar = ReadMulti<T, U, V, W>(out a, out b, out c, out d); 
        e = cv<X>(ar[4]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W, X, Y>(out T a, out U b, out V c, out W d, out X e, out Y f) {
		var ar = ReadMulti<T, U, V, W, X>(out a, out b, out c, out d, out e); 
        f = cv<Y>(ar[5]);
		return ar;
	}
	public static string[] ReadArray<T>(out T[] a) {		
		var ar = Console.ReadLine().Split(splitter, StringSplitOptions.RemoveEmptyEntries);
		a = ar.Select(cv<T>).ToArray();
		return ar;
	}		
	public static string[][] ReadLines<T>(long n, out T[] a) {
		a = new T[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) arr[i] = ReadMulti(out a[i]);
		return arr;
	}
	public static string[][] ReadLines<T, U>(long n, out VT<T, U>[] a) {
		a = new VT<T, U>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p;
			arr[i] = ReadMulti(out o, out p);
			a[i] = new VT<T, U>(o, p);
		}
		return arr;
	}
	public static string[][] ReadLines<T, U, V>(long n, out VT<T, U, V>[] a) {
		a = new VT<T, U, V>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p; V q;
			arr[i] = ReadMulti(out o, out p, out q);
			a[i] = new VT<T, U, V>(o, p, q);
		}
		return arr;
	}
	public static string[][] ReadLines<T, U, V, W>(long n, out VT<T, U, V, W>[] a) {
		a = new VT<T, U, V, W>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p; V q; W r;
			arr[i] = ReadMulti(out o, out p, out q, out r);
			a[i] = new VT<T, U, V, W>(o, p, q, r);
		}
		return arr;
	}
	public static string[][] ReadMatrix<T>(long n, out T[][] a) {
		a = new T[n][];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) arr[i] = ReadArray(out a[i]);
		return arr;
	}

	private static void wwp(Action act){
		var sw = new Printer(Console.OpenStandardOutput()){AutoFlush = false};
		Console.SetOut(sw);
		act();
		Console.Out.Flush();
		sw.AutoFlush = true;
		Console.SetOut(sw);
	}
	private static string wfm(Type t) =>t.Equals(typeof(double)) ? "{0:F10}" : "{0}";
	public static void WriteMulti(params object[] a) {
		wwp(()=>{
			for(int i=0;i<a.Length;i++) {
				string fs = wfm(a[i].GetType()) + ((i<a.Length-1) ? " " : Environment.NewLine);
				Console.Write(fs, a[i]);
			}
		});
	}
	public static void WriteArray<T>(T[] a) {
		wwp(()=>{
			string fs = wfm(typeof(T));
			string fs1 = fs + " ";
			string fs2 = fs + Environment.NewLine;
			for(int i=0;i<a.Length;i++) Console.Write( ((i<a.Length-1) ? fs1 : fs2), a[i]);
		});
	}
	public static void WriteLines<T>(T[] a) {
		wwp(()=>{
			string fs = wfm(typeof(T)) + Environment.NewLine;
			foreach(T x in a) Console.Write(fs, x);
		});
	}


}

#endregion