module Main where import qualified Data.ByteString.Char8 as BS import Data.Char import Data.String import Data.List import Data.Maybe import Control.Monad type Width = Int type Height = Int type Rectangle = (Width, Height) type Target = [Int] type OutputValue = [[BS.ByteString]] type InitialValue = OutputValue main :: IO () main = do [h, w] <- map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine xn <- replicateM h $ map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine flatten <- return . sort $ concat xn let r = toOutputValue (w, h) flatten [] mapM_ (putStrLn . BS.unpack . BS.unwords) r toOutputValue :: Rectangle -> Target -> InitialValue -> OutputValue toOutputValue (n, count) ls init | count > 0 = toOutputValue (n, count - 1) (drop n ls) (map (BS.pack . show) (take n ls) : init) | otherwise = reverse init