let sort array n =
  let rec bubble_sort a c =
    let rec bubble c = function
      | 0 -> a,c
      | i ->
        bubble
          (if a.(i) < a.(i-1)
           then (let aj = a.(i-1) in
                 a.(i-1) <- a.(i); a.(i) <- aj; c+1)
           else c)
          (i-1)
    in
    match bubble 0 n with
      v,0 -> v,c
    | v,t -> bubble_sort v (c+t)
  in
  bubble_sort array 0
    
let () =
  let n = read_int () in
  let a = read_line () |> Str.split (Str.regexp " ")
          |> List.map int_of_string |> Array.of_list in
  let sorted,c = sort a (n-1) in
  Array.iteri
    (fun i x -> Printf.printf (if i=n-1 then "%d\n" else "%d ") x) sorted;
  Printf.printf "%d\n" c