import Data.List
import Data.Maybe

main :: IO()
main = getContents >>= putStr.format.(\xs-> (solve ([],xs), sort xs)).map read.words.(!!1).lines

format :: (([Int],[a]),[Int]) -> String
format ((ns, _), xs) = unlines [unwords $map show xs, show $sum ns] 

solve    :: ([Int],[Int]) -> ([Int], [Int])
solve (n, []) = (n, [])
solve (n, xs) =???let s = minimum xs in solve ((fromJust $elemIndex s xs):n, delete s xs)