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;
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() {
		int  n, m;
		MyIO.ReadMulti(out n, out m);
		int[] a;
		MyIO.ReadArray(out a);
 
		Array.Sort(a);
		SortedList<int,int> dn = new SortedList<int, int>();
		foreach(var e in a){
			if(e==1) {
				dn[1] = 2;
			}
			if(e==2) {
				dn[2] = 5;
			}
			if(e==3) {
				if(dn.ContainsKey(2)) dn.Remove(2);
				dn[3] = 5;
			}
			if(e==4) {
				dn[4] = 4;
			}
			if(e==5) {
				if(dn.ContainsKey(2)) dn.Remove(2);
				if(dn.ContainsKey(3)) dn.Remove(3);
				dn[5] = 5;
			}
			if(e==6) {
				dn[6] = 6;
			}
			if(e==7) {
				dn[7] = 3;
			}
			if(e==8) {
				dn[8] = 7;
			}
			if(e==9) {
				if(dn.ContainsKey(6)) dn.Remove(6);
				dn[9] = 6;
			}
		}
		if(dn.ContainsKey(1)) {
			if(dn.ContainsKey(4)) dn.Remove(4);
			if(dn.ContainsKey(6)) dn.Remove(6);
			if(dn.ContainsKey(9)) dn.Remove(9);
		}
		if(dn.ContainsKey(7)) {
			if(dn.ContainsKey(6)) dn.Remove(6);
			if(dn.ContainsKey(9)) dn.Remove(9);
		}
 
		List<KeyValuePair<int,int>> ds = new List<KeyValuePair<int, int>>();
		StringBuilder ans = new StringBuilder();
		if(dn.Count==1){
			ds.Add(new KeyValuePair<int, int>(dn.Keys[0],n/dn.Values[0]));
		}
		if(dn.Count==2){
			ll maxs = 0;
			int[][] maxa = new int[2][];
			maxa[0] = new int[2]{dn.Keys[0], 0};
			maxa[1] = new int[2]{dn.Keys[1], 0};
			for (int i = 0; i <= n/dn.Values[0]; i++)
			{
				if((n-i*dn.Values[0])%dn.Values[1]==0){
					int j = (n-i*dn.Values[0])/dn.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);
			ds.Add(new KeyValuePair<int, int>(maxa[0][0],maxa[0][1]));
			ds.Add(new KeyValuePair<int, int>(maxa[1][0],maxa[1][1]));
		}
		if(dn.Count==3){
			int maxs = 0;
			int[][] maxa = new int[3][];
			maxa[0] = new int[2]{dn.Keys[2], 0};
			maxa[1] = new int[2]{dn.Keys[1], 0};
			maxa[2] = new int[2]{dn.Keys[0], 0};
			for (int i = 0; i <= n/dn.Values[2]; i++)
			{
				int jlim = (n-i*dn.Values[2])/dn.Values[1];
				for (int j = 0; j <= jlim; j++)
				{
					int s = n-i*dn.Values[2]-j*dn.Values[1];
					if(s%dn.Values[0]==0){
						int k = s/dn.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);
			ds.Add(new KeyValuePair<int, int>(maxa[0][0],maxa[0][1]));
			ds.Add(new KeyValuePair<int, int>(maxa[1][0],maxa[1][1]));
			ds.Add(new KeyValuePair<int, int>(maxa[2][0],maxa[2][1]));
		}
		if(dn.Count==4){
			Dictionary<int,List<VT<int,int>>> d1 = new Dictionary<int, List<VT<int, int>>>();
			Dictionary<int,List<VT<int,int>>> d2 = new Dictionary<int, List<VT<int, int>>>();
			for (int i = 0; i <= n/dn.Values[3]; i++)
			{
				int jlim = (n-i*dn.Values[3])/dn.Values[2];
				for (int j = 0; j <= jlim; j++)
				{
					int s = i*dn.Values[3]+j*dn.Values[2];
					if(!d1.ContainsKey(s)) d1[s] = new List<VT<int, int>>();
					d1[s].Add(new VT<int,int>(i,j));
				}
			}
			for (int i = 0; i <= n/dn.Values[1]; i++)
			{
				int jlim = (n-i*dn.Values[1])/dn.Values[0];
				for (int j = 0; j <= jlim; j++)
				{
					int s = n-i*dn.Values[1]-j*dn.Values[0];
					if(!d2.ContainsKey(s)) d2[s] = new List<VT<int, int>>();
					d2[s].Add(new VT<int,int>(i,j));
				}
			}
 
			int[][] maxa = new int[4][];
			maxa[0] = new int[2]{dn.Keys[3], 0};
			maxa[1] = new int[2]{dn.Keys[2], 0};
			maxa[2] = new int[2]{dn.Keys[1], 0};
			maxa[3] = new int[2]{dn.Keys[0], 0};
			int maxs = 0;
 
			Comparer<VT<int,int>> nor = Comparer<VT<int,int>>.Create((x,y)=>{
				int s1 = y.Item1+y.Item2-x.Item1-x.Item2;
				if(s1==0){
					return y.Item1-x.Item1;
				}else{
					return s1;
				}
			});

			foreach (var ei in d1)
			{
				if(!d2.ContainsKey(ei.Key)) continue;
				var ee = ei.Value;
				var ff = d2[ei.Key];
				ee.Sort(nor);
				ff.Sort(nor);

				int s = ee[0].Item1+ee[0].Item2+ff[0].Item1+ff[0].Item2;
				if(maxs<s){
					maxs = s;
					maxa[0][1] = ee[0].Item1;
					maxa[1][1] = ee[0].Item2;
					maxa[2][1] = ff[0].Item1;
					maxa[3][1] = ff[0].Item2;
				}else if(maxs==s){
					if((maxa[0][1]<ee[0].Item1) || (maxa[0][1]==ee[0].Item1&&maxa[1][1]<ee[0].Item2) || (maxa[0][1]==ee[0].Item1&&maxa[1][1]==ee[0].Item2&&maxa[2][1]<ff[0].Item1)){
						maxa[0][1] = ee[0].Item1;
						maxa[1][1] = ee[0].Item2;
						maxa[2][1] = ff[0].Item1;
						maxa[3][1] = ff[0].Item2;	
					}
				}
			}
			Array.Sort(maxa,(x,y)=>x[0].CompareTo(y[0]));
			Array.Reverse(maxa);
			ds.Add(new KeyValuePair<int,int>(maxa[0][0],maxa[0][1]));
			ds.Add(new KeyValuePair<int,int>(maxa[1][0],maxa[1][1]));
			ds.Add(new KeyValuePair<int,int>(maxa[2][0],maxa[2][1]));
			ds.Add(new KeyValuePair<int,int>(maxa[3][0],maxa[3][1]));
		}
 
 
		foreach (var kv in ds)
		{
			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);
	}
















}




















#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