import Data.List (intersect)
import qualified Data.IntMap as M

main :: IO ()
main = do
  n <- read . head . words <$> getLine
  as <- map read . words <$> getLine
  let
      ps = map (id >< head) $ filter (not . null . snd) $ map (id >< (intersect as)) priority
      dp = M.fromAscList $ map (\i -> (i, f i)) [0..n]
        where
          f 0 = [(0, [])]
          f i = do
              (j, l) <- ps
              (k, ls) <- dp M.! (pred i)
              if j + k <= n
                then
                  return (j + k, map (l * 10^(pred i) +) ls)
                else
                  []
  print $ maximum $ head $ reverse [0..n] >>= \i -> (dp M.! i) >>= \(k, ls) -> if k == n then return ls else []

priority = [(2, [1]), (3, [7]), (4, [4]), (5, [5, 3, 2]), (6, [9, 6]), (7, [8])]

(f >< g) (x, y) = (f x, g y)
infixr 5 ><
