main :: IO () main = do n <- readLn is <- map read . words <$> getLine let result = yukisort n 1 (comb n) is putStrLn . unwords . map show $ result yukisort :: Int -> Int -> [(Int, Int)] -> [Int] -> [Int] yukisort n i c xs | i >= 2 * n - 3 = xs | otherwise = yukisort n (i + 1) c $ foldl (\a v -> proc v a) xs (comb' c) where comb' = filter (\(p, q) -> p + q == i) proc (p, q) ys | ys !! p > ys !! q = swap p q ys | otherwise = ys comb :: Int -> [(Int, Int)] comb n = [(p, q) | p <- [0..n - 1], q <- [0..n - 1], p < q] 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