{-# LANGUAGE BangPatterns #-}

import Control.Applicative ((<$>))
import Data.Maybe (fromJust)
import Data.Vector.Unboxed (Vector, (!), unsafeFreeze, unsafeThaw, fromList, toList)
import Data.Vector.Unboxed.Mutable (IOVector, unsafeRead, unsafeSwap)
import qualified Data.Vector.Unboxed as V
import qualified Data.Vector.Unboxed.Mutable as MV
import qualified Data.ByteString.Char8 as BS

main
    =   getLine
    >>  fromList . map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine
    >>= unsafeThaw
    >>= partition
    >>= forAnswer
    >>= putStrLn


partition :: IOVector Int -> IO (Int, Vector Int)
partition !inputs = do
    pivot   <- unsafeRead inputs (MV.length inputs - 1)
    pos     <- checkAndSwap inputs pivot (-1) 0 (MV.length inputs - 1)
    inputs' <- unsafeFreeze inputs
    return (pos, inputs')


checkAndSwap :: IOVector Int -> Int -> Int -> Int -> Int -> IO Int
checkAndSwap !inputs !pivot !i !j !end
    | j == end  = do
        unsafeSwap inputs (i + 1) end
        return (i + 1)

    | otherwise = do
        v <- unsafeRead inputs j
        case v <= pivot of
            True -> do
                unsafeSwap inputs (i + 1) j
                checkAndSwap inputs pivot (i + 1) (j + 1) end

            False -> do
                checkAndSwap inputs pivot i (j + 1) end

forAnswer :: (Int, Vector Int) -> IO String
forAnswer (i, xs) = return $ before ++ mid ++ after
  where
    before = unwords . map show . toList $ V.take i xs
    after  = unwords . map show . toList $ V.drop (i + 1) xs
    mid    = " [" ++ show (xs ! i) ++ "] "
