import Control.Applicative import Data.Map import Data.List import Data.Maybe n = 10 ^ 6 dfs :: [Int] -> (Map Int Int) -> Int dfs [] dp = maximum . elems $ dp dfs (x : xs) dp = dfs xs $ Data.List.foldl' upd dp [x * 2, x * 3 .. n] where v = fromMaybe 0 $ Data.Map.lookup x dp upd dp i = if w <= v + 1 then insertWithKey' (\k x y -> x) i (v + 1) dp else dp where w = fromMaybe (10 ^ 6) $ Data.Map.lookup i dp main = do _ <- getLine x <- sort . Data.List.map read . words <$> getLine :: IO [Int] print $ dfs x (fromList (zip x (repeat 1)))