import Data.List (sort)
import qualified Data.IntMap as M
import Data.List (unfoldr)

main :: IO ()
main = do
  n <- read . head . words <$> getLine
  as <- reverse . sort . map read . words <$> getLine
  let
    maxLength = (dp M.!)
      where
        dp = M.fromAscList $ map ((,) <$> id <*> f) [0..n]
        f 0 = 0
        f i = maximum . (0 :) . map ((1 +) . (dp M.!)) . filter (>= 0) . map ((i -) . num) $ as
    l = maxLength n
    ds = unfoldr (\(i, k) -> foldr (\d r -> if i > 0 && k > 0 && (pred k `elem` ls (i - num d)) then Just (d, (i - num d, pred k)) else r) Nothing as) (n, l)
  print . foldl (\a i -> 10 * a + i) 0 . map toInteger $ ds

num = (matches M.!)
  where
    matches = M.fromAscList [(1, 2), (2, 5), (3, 5), (4, 4), (5, 5), (6, 6), (7, 3), (8, 7), (9, 6)]
