unittest
{
	assert( [ "20 4", "3 7 8 4" ].parse.expand.solve == "777773" );
	assert( [ "101 9", "9 8 7 6 5 4 3 2 1" ].parse.expand.solve == "71111111111111111111111111111111111111111111111111" );
	assert( [ "15 3", "5 4 6" ].parse.expand.solve == "654" );
}



import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.typecons;

void main()
{
	stdin.byLineCopy.parse.expand.solve.writeln;
}

auto parse( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto nm = input.front.split.to!( long[] );
	input.popFront;
	auto as = input.front.split.to!( long[] );
	return tuple( nm[ 0 ], as );
}

auto solve( long n, long[] as )
{
	const cs = [ 2L, 5L, 5L, 4L, 5L, 6L, 3L, 7L, 6L ];
	
	auto dp_v = new string[][]( n + 1, as.length );
	auto dp_d = new bool[][]( n + 1, as.length );
	foreach( ref d; dp_d[ 0 ] )
	{
		d = true;
	}
	
	for( auto ni = 1L; ni < dp_v.length; ni++ )
	{
		for( auto mi = 0L; mi < dp_v[ ni ].length; mi++ )
		{
			auto v = "";
			auto d = false;
			auto c = cs[ as[ mi ] - 1 ];
			if( 0 <= ni - c )
			{
				for( auto pi = 0L; pi < dp_v[ ni ].length; pi++ )
				{
					if( !dp_d[ ni - c ][ pi ] ) continue;
					
					auto w = dp_v[ ni - c ][ pi ] ~ as[ mi ].to!string;
					if(      v.length <  w.length ) v = w;
					else if( v.length == w.length ) v = max( v, w );
					d = true;
				}
			}
			dp_v[ ni ][ mi ] = v;
			dp_d[ ni ][ mi ] = d;
		}
	}
	
	auto ans = "";
	foreach( v, d; lockstep( dp_v[ $ - 1 ], dp_d[ $ - 1 ] ) )
	{
		if( !d ) continue;
		
		if(      ans.length <  v.length ) ans = v;
		else if( ans.length == v.length ) ans = max( ans, v );
	}
	
	return ans;
}
