import           Control.Applicative
import           Control.Monad

print' :: Show a => [a] -> IO ()
print' = putStrLn . unwords . fmap show

(<:>) :: Int -> ([Int], Int) -> ([Int], Int)
x <:> (xs, n) = (x : xs, n)

bubble' :: ([Int], Int) -> ([Int], Int)
bubble' (xs, n) = f n xs
  where
    f k []  = ([],  k)
    f k [a] = ([a], k)
    f k (a:b:cs)
        | a < b     = b <:> f (k + 1) (a : cs)
        | otherwise = a <:> f k       (b : cs)

bubble :: [Int] -> ([Int], Int)
bubble xs =
    let (xs', m) = foldr (.) id (replicate n bubble') (reverse xs, 0)
     in (reverse xs', m)
  where
    n = length xs

main :: IO ()
main = do
    getLine
    xs <- fmap read . words <$> getLine :: IO [Int]
    let (ys, n) = bubble xs
    print' ys
    print  n

