gt::(Int, Int)->(Int, Int)->Bool
gt (x1, x2) (y1, y2) = x2 > y2
inc::(Int,  Int)->(Int, Int)
inc (x1, x2) = (x1+1, x2)
swap::[(Int, Int)]->[(Int, Int)]
swap [x] = [x]
swap (x:xs)
  | gt x y = inc y : x : ys
  | otherwise = x:y:ys
    where
      (y:ys) = swap xs
bsort::[(Int, Int)]->[(Int, Int)]
bsort [] = []
bsort xs = y : bsort ys
  where
    (y:ys) = swap xs
format (xs, ys) = unwords (map show ys) ++ "\n" ++ show (sum xs)
pair::a->b->(a, b)
pair x y = (x, y)
toInt::String->Int
toInt = read
main = do
  getLine
  getLine >>=
    putStrLn.format.unzip.bsort.(map ((pair 0).toInt)).words
