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

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 <*> g) [0..n]
        g 0 = 0
        g i = maximum $ map ((1 +) . (dp M.!) . (max 0) . (i -) . num) as
    l = maxLength n
    ds = head . filter ((== n) . sum . map num) . sequence . replicate l $ as
  print $ foldl (\a i -> 10 * a + i) 0 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)]
