-- BubbleSort(A)
-- 1 for i = 0 to A.length-1
-- 2     for j = A.length-1 downto i+1
-- 3         if A[j] < A[j-1]
-- 4             swap A[j] and A[j-1]

import qualified Data.Vector         as V
import qualified Data.Vector.Mutable as VM
import Control.Applicative
import Control.Monad.ST
import Control.Monad
import Data.IORef

bubbleSort :: [Int] -> IO ([Int], Int)
bubbleSort xs = do
  aVec <- V.thaw $ V.fromList xs
  let len = length xs - 1
  flag  <- newIORef True
  count <- newIORef 0
  forM_ [0 .. len] $ \i -> do
    f <- readIORef flag
    when f $ do
      writeIORef flag False
      forM_ [len, len - 1 .. (i + 1)] $ \j -> do
        -- v <- V.freeze aVec
        -- print (i, j, v)
        v1 <- VM.read aVec j
        v2 <- VM.read aVec (j - 1)
        when (v1 < v2) $ do
          VM.swap aVec j (j - 1)
          writeIORef flag True
          modifyIORef count (+ 1)
  v <- V.toList <$> V.freeze aVec
  c <- readIORef count
  return (v, c)

main :: IO ()
main = do
  _  <- getLine
  xs <- map read . words <$> getLine
  (ys, c) <- bubbleSort xs
  putStrLn $ unwords $ map show ys
  print c

