import Data.List
import Control.Applicative
import Control.Monad.Writer
import Data.Monoid

main = do
  getLine
  input <- map read . words <$> getContents
  let result = runWriter $ solve input
  putStrLn . intercalate " " . map show $ fst result
  putStrLn . show . getSum . snd $ result

myfunc :: [Int] -> (Int, [Int], Int)
myfunc (x:xs) = foldl func (x, [], 0) xs
  where func (m, xs, c) x = if x <= m
                              then (x, m:xs, c)
                              else (m, x:xs, c+1)

solve :: [Int] -> Writer (Sum Int) [Int]
solve [] = return []
solve xs = do
    let (h, ts, c) = myfunc $ reverse xs
    tell (Sum c)
    xs' <- solve ts
    return (h:xs')

