{-
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]
-}

main = do
    s1 <- getLine
    let n = read s1 :: Int
    s2 <- getLine
    let a = map (\x -> read x :: Int) $ words s2
    let (swapTimes, sorted) = bubbleSort a
    putStrLn $ unwords $ map show sorted
    putStrLn $ show swapTimes

bubbleSort :: [Int] -> (Int, [Int])
bubbleSort a = bubbleSorti (0,a) 0

bubbleSorti :: (Int,[Int]) -> Int -> (Int,[Int])
bubbleSorti (swapTimes, a) i
  | i > (length a-1) = (swapTimes, a)
  | otherwise        = bubbleSorti (bubbleSortj (swapTimes, a) i (length a - 1)) (i+1)

bubbleSortj :: (Int,[Int]) -> Int -> Int -> (Int,[Int])
bubbleSortj (swapTimes, a) i j
  | j < (i+1)       = (swapTimes, a)
  | a!!j < a!!(j-1) = bubbleSortj (swapTimes+1, swap a j (j-1)) i (j-1)
  | otherwise       = bubbleSortj (swapTimes, a) i (j-1)

swap :: [a] -> Int -> Int -> [a]
swap a i j = (take i1 a) ++ [a!!i2] ++ (take (i2-i1-1) $ drop (i1+1) a) ++ [a!!i1] ++ (drop (i2+1) a)
  where (i1, i2) = (min i j, max i j)
