{-# LANGUAGE BangPatterns #-}
module Main where

import Data.Bool
import Data.Functor
import Data.List

main :: IO ()
main = do
  { [n] <- getInts
  ; xs  <- getInts
  ; case bubbleSort xs of
      (c,ys) -> do
        { putInts ys
        ; putInts [c]
        }
  }

getInts :: IO [Int]
getInts = map read . words <$> getLine

putInts :: [Int] -> IO ()
putInts = putStrLn . unwords . map show

bubbleSort :: Ord a => [a] -> (Int,[a])
bubbleSort = step (0,[])
  where
    step cxs    [] = cxs
    step (c,xs) ys = case bubble ys of
      ((z,d),zs) -> step (c+d,z:xs) zs

bubble :: Ord a => [a] -> ((a,Int),[a])
bubble (x:xs) = mapAccumL swap (x,0) xs

swap :: Ord a => (a,Int) -> a -> ((a,Int),a)
swap (x,!c) y = bool ((y,c),x) ((x,succ c),y) (x > y)