let cnt = ref 0

let print_array a =
  Array.iteri (fun i n -> Printf.printf (if i = 0 then "%d" else " %d") n) a;
  print_newline ()

let bubble_sort a n =
  let swap i j = let tmp = a.(i) in a.(i) <- a.(j); a.(j) <- tmp in
  let rec bubble j flag =
    if j <= 0 then flag
    else if a.(j - 1) <= a.(j) then bubble (j - 1) flag
    else (swap j (j - 1); incr cnt; bubble (j - 1) true)
  in
  let rec doit flag =
    if not flag then ()
    else doit (bubble (n - 1) false)
  in
  doit true

let () =
  let n = read_int () in
  let a = Array.of_list (List.map int_of_string (Str.split (Str.regexp " ") (read_line ()))) in
  bubble_sort a n;
  print_array a;
  Printf.printf "%d\n" !cnt