let cnt = ref 0

let bubble_sort a n =
  let rec bubble i flag =
    if i = 0 then doit flag
    else if a.(j-1) <= a.(j) then bubble (j - 1) flag
    else (let tmp = a.(j) in a.(j) <- a.(j-1); a.(j-1) <- tmp; incr cnt; bubble (j - 1) true)
  and doit flag =
    if flag then 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;
  Array.iteri (fun i n -> Printf.printf (if i = 0 then "%d" else " %d") n) a;
  print_newline ();
  Printf.printf "%d\n" !cnt