let cnt = ref 0

let split str delim =
  let rec doit s acc =
    match
      try Some (String.index s delim)
      with _ -> None
    with
    | None -> List.rev (s :: acc)
    | Some i ->
        let s1 = String.sub s 0 i in
        let s2 = String.sub s (i + 1) (String.length s - String.length s1 - 1) in
        doit s2 (s1 :: acc)
  in
  doit str []

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 rec bubble j flag =
    if j <= 0 then flag
    else begin
      if a.(j - 1) <= a.(j) then bubble (j - 1) flag
      else begin
        let tmp = a.(j) in
        a.(j) <- a.(j - 1);
        a.(j - 1) <- tmp;
        incr cnt;
        bubble (j - 1) true;
      end
    end
  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 (split (read_line ()) ' ')) in
  bubble_sort a n;
  print_array a;
  Printf.printf "%d\n" !cnt