import qualified Data.IntMap.Lazy as M main :: IO () main = do n <- readLn is <- map read . words <$> getLine let m = createMap n result = yukisort n 1 m is putStrLn . unwords . map show $ result yukisort :: Int -> Int -> M.IntMap [(Int, Int)] -> [Int] -> [Int] yukisort n i map xs | i >= 2 * n - 3 = xs | otherwise = yukisort n (i + 1) map $ foldl (\a v -> proc v a) xs ((M.!) map i) where proc (p, q) ys | ys !! p > ys !! q = swap p q ys | otherwise = ys createMap :: Int -> M.IntMap [(Int, Int)] createMap n = foldl (\m (p, q) -> M.insertWith insertFunc (p + q) [(p, q)] m) M.empty comb where comb = [(p, q) | p <- [0..n - 1], q <- [0..n - 1], p < q] insertFunc n o = (n !! 0) : o swap :: Int -> Int -> [Int] -> [Int] swap i j as@(x:xs) | i == 0 || j == 0 = let m = max i j in as !! m : take (m - 1) xs ++ x : drop m xs | otherwise = x : swap (i - 1) (j - 1) xs