fork download
  1. import Control.Monad
  2. import System.IO
  3. import Data.List
  4. child [] n = []
  5. child (x:xs) n = child xs n ++ (if 2*x+1<=n then [2*x,2*x+1] else if 2*x<=n then [2*x] else [])
  6.  
  7.  
  8. get [] p = []
  9. get (x:xs) p = (get xs p) ++ [p!!(x-1)]
  10.  
  11. solve [] p n = []
  12. solve xs p n = [maximum (get xs p)] ++ solve (child xs n) p n
  13.  
  14. readInts :: IO [Int]
  15. readInts = fmap (map read.words) getLine
  16.  
  17. main :: IO ()
  18. main = do
  19. x <- readInts
  20. y <- readInts
  21. putStr (intercalate " " (map show (solve [1] y (x!!0))))
Success #stdin #stdout 0s 8388607KB
stdin
7
1 2 3 2 3 4 2 
stdout
1 3 4