import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Read as T
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as VU
import Data.List (sortBy)
import Data.Maybe (fromJust)

main :: IO ()
main = do
  [n, m] <- map unsafeTextToInt . T.words <$> T.getLine :: IO [Int]
  as <- sortBy (flip compare) . map unsafeTextToInt . T.words <$> T.getLine :: IO [Int]
  let
    table = V.constructN (succ n) rec :: V.Vector (Maybe (Int, [Int]))
      where
        rec :: V.Vector (Maybe (Int, [Int])) -> (Maybe (Int, [Int]))
        rec v | l == 0    = Just (0, [])
              | otherwise = maximum . map f $ as
          where
            l = V.length v :: Int
            f :: Int -> Maybe (Int, [Int])
            f d | l < k     = Nothing
                | otherwise = ((,) <$> succ . fst <*> (d :) . snd) <$> v V.! (l - k)
              where
                k = matchNums VU.! d :: Int
  mapM_ (putStr . show) . snd . fromJust $ table V.! n
  putStrLn ""

matchNums = VU.fromList [6, 2, 5, 5, 4, 5, 6, 3, 7, 6] :: VU.Vector Int
{-# INLINE matchNums #-}

unsafeTextToInt :: T.Text -> Int
unsafeTextToInt s = case T.signed T.decimal s of
  Right (n, _) -> n
{-# INLINE unsafeTextToInt #-}
