
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}

import Data.Array (Array, listArray, (!))
import Data.Array.ST (STArray, MArray, runSTArray, readArray, writeArray, newArray)
import Data.Maybe (fromJust, catMaybes)
import Data.Foldable (maximumBy)

import Control.Monad (forM_, when)
import Control.Monad.ST (ST)

type StrL = (String, Int)
cons x (xs, n) = (x:xs, n+1)

cmp (s1,l1) (s2,l2)
  | l1 < l2 = LT
  | l1 > l2 = GT
  | otherwise = compare s1 s2

match = listArray ('1', '9') [2,5,5,4,5,6,3,7,6]

mm :: [Char] -> Int -> (forall s. ST s (STArray s Int (Maybe StrL)))
mm as n = do
    arr <- newArray (0,n) Nothing
    writeArray arr 0 (Just ("", 0))
    forM_ [2..n] $ \i -> nth arr i >>= writeArray arr i
    return arr
  where
    nth arr i = do
      cands <- fmap catMaybes . sequence $
                [fmap (cons a)<$>readArray arr (i-m)|a<-as, let m=match!a, i-m>=0]
      case cands of
        [] -> return Nothing
        xs -> return $ Just $ maximumBy cmp xs

main :: IO ()
main = do
  n <- read . head . words <$> getLine
  as <- map head . words <$> getLine
  let result = runSTArray $ mm as n
  putStrLn . fst . fromJust $ result!n
