import Control.Applicative
import Control.Monad.State
import Data.Array.IO

main = do
  n <- getLine
  list <- map read.words<$>getLine :: IO [Int]
  let (res,swaps) = bubbleSort list
  putStrLn.unwords.map show $ res
  print swaps

bubbleSort :: Ord a => [a] -> ([a], Int)
bubbleSort xs = runState (bubbles xs) 0

bubbles [] = return []
bubbles xs = do
        (y:ys) <- bswap xs
        ys' <- bubbles ys
        return (y:ys')

bswap :: (Ord a) => [a] -> State Int [a]
bswap [x] = return [x]
bswap (x:xs) = do
        (y:ys) <- bswap xs
        if x > y
            then do
                modify (+1)
                return $ y:x:ys
            else return $ x:y:ys
