module Main where

-- import Debug.Trace
import Data.List
import qualified Data.Sequence as S
import qualified Data.ByteString.Char8 as B
import Data.Int
import Data.Char
-- import Data.Array
import Data.Array.IO
import Data.Maybe
import qualified Data.Map as M
import Data.Ord
import Data.Functor
import Control.Applicative
import Control.Monad
import Text.Printf

solve :: Int -> [Int] -> IO [Int]
solve n l = iter (newListArray (0, n) (minBound:l)) (div n 2) where
    iter :: IO (IOUArray Int Int) -> Int -> IO [Int]
    iter ar 0 = ar >>= getElems >>= (\x -> return (drop 1 x)) 
    iter ar i = iter (maxHeapify ar i) (i-1) where
        maxHeapify ar i = do
            a <- ar
            iv <- readArray a i              
            lv <- readArray a l
            rv <- readArray a r
            if iv > lv
                then if iv > rv then return a
                                else swap a i iv r rv
                else if lv > rv then swap a i iv l lv 
                                else swap a i iv r rv where
                swap :: IOUArray Int Int -> Int -> Int -> Int -> Int -> IO (IOUArray Int Int)
                swap a i iv m mv = do
                    writeArray a i mv
                    writeArray a m iv
                    maxHeapify (return a) m
                l = if 2*i <= n then 2*i else 0
                r = if 0 < l && l < n then l+1 else 0

readIntLn:: IO Int
readIntLn = (fst . fromJust . B.readInt) <$> B.getLine

readIntList:: IO [Int]
readIntList = map (fst . fromJust . B.readInt) . B.words <$> B.getLine

print' :: [Int] -> IO()
print' [] = putStrLn "" 
print' (x:xs) = do 
    putStr $ ' ':show x 
    print' xs

{-
printIOUArray :: IOUArray Int Int -> IO ()
printIOUArray ioa = do
    l <- getElems ioa
    print l
 -}

main :: IO() 
main = do
    n <- readIntLn
    l <- readIntList
    r <- solve n l 
    print' r

