--バブルソート

import Data.Array
import Data.Array.ST
import Data.STRef
import Control.Monad.ST
import Control.Monad

--アレイ境界、アレイ、ソート済み先頭インデックス、アクション
bubble::(Int,Int)->STArray s Int Int -> Int -> ST s ()
bubble (s,e) ar idx = if idx >= (e-1) then return () else  do
  forM_ [((-j),(-j)+1)|j<-[(-(e-1))..(-(idx+1))]] (\(i,j)-> do
                                                  v_i<-readArray ar i
                                                  v_j<-readArray ar j
                                                  if v_i <= v_j then return ()
                                                                else do
                                                                  writeArray ar j v_i
                                                                  writeArray ar i v_j)

bubble_c::(Int,Int)->STArray s Int Int -> Int -> ST s Int
bubble_c (s,e) ar idx = if idx >= (e-1) then return 0 else  do
  counter<-newSTRef 0
  forM_ [((-j),(-j)+1)|j<-[(-(e-1))..(-(idx+1))]] (\(i,j)-> do
                                                  v_i<-readArray ar i
                                                  v_j<-readArray ar j
                                                  if v_i <= v_j then return ()
                                                                else do
                                                                  writeArray ar j v_i
                                                                  writeArray ar i v_j
                                                                  v<-readSTRef counter
                                                                  writeSTRef counter (v+1))
  c<-readSTRef counter
  return c

bubble_sort::(Int,Int)->STArray s Int Int -> ST s()
bubble_sort (s,e) ar = do
  forM_ [s-1..e-1] (bubble (s,e) ar)

bubble_sort_with_c::(Int,Int)->STArray s Int Int -> ST s Int
bubble_sort_with_c (s,e) ar = do
  ct <- forM [s-1..e-1] (bubble_c (s,e) ar)
  return (sum ct)

b_sort_with_c::Array Int Int -> (Int,Array Int Int)
b_sort_with_c ar = runST $ do
   let (s,e) = bounds ar
   mar<-thaw ar
   c<-bubble_sort_with_c (s,e) mar
   iar<-freeze mar
   return (c,iar)


main::IO()
main=do
  nc<-getLine
  let n = read nc
  d<-getLine
  let ar = listArray (1,n) (map read (words d))
  let (c,out)=b_sort_with_c ar
  putStr ((ls_show (elems out))++"\n")
  print c

ls_show::[Int]->String
ls_show [] = []
ls_show (x:xs) = (show x)++(if xs == [] then (ls_show xs) else ' ':(ls_show xs))
