{-# LANGUAGE BangPatterns #-}

main :: IO ()
main = do
  _ <- getLine
  as <- fmap (map read . words) getLine :: IO [Int]
  let (sorted, n) = bubbleSortC as
  putStrLn . unwords . map show $ sorted
  print n

bubbleSortC :: (Ord a) => [a] -> ([a], Int)
bubbleSortC [] = ([], 0)
bubbleSortC as = cons' (x, i) $ bubbleSortC $ xs
  where
    ((x:xs), i) = foldr bubble ([], 0) as

    bubble :: (Ord a) => a -> ([a], Int) -> ([a], Int)
    bubble x ([], i) = ([x], i)
    bubble x ((a:as), i)
      | a < x     = (a:x:as, i + 1)
      | otherwise = (x:a:as, i)

    cons' :: (Ord a) => (a, Int) -> ([a], Int) -> ([a], Int)
    cons' a b = let (x, i) = a
                    (xs, j) = b
                in (x:xs, i + j)

