bubbleSort :: [Int] -> ([Int], Int)
bubbleSort xs = loop (1, 0, xs)
  where
    loop (0, count, xs) = (xs, count)
    loop (1, count, xs) = loop $ foldr f (0, count, xs) [0 .. length xs - 2]
    f i (flag, count, xs)
      | b > c     = (1, count + 1, as ++ c : b : ds)
      | otherwise = (flag, count, xs)
      where (as, b : c : ds) = splitAt i xs

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

