let print_array l =
  Array.fold_left (fun x y -> (if x = "" then "" else x^" ")^(string_of_int y)) "" l |> print_endline;;
let to_list s = Str.split (Str.regexp_string " ") s |> List.map (fun x -> int_of_string x) |> Array.of_list;;

let sort a =
  let len = Array.length a in
  let rec loop ct = function
      i when i = len -> ct
    | i -> let rec loop2 ct2 = function
               j when j = i -> ct2
             | j -> let swap =
                      if a.(j) < a.(j-1) then
                          let temp = a.(j-1) in
                          a.(j-1) <- a.(j); a.(j) <- temp; 1
                      else 0 in
                    loop2 (ct2+swap) (j-1)
           in loop (loop2 ct (len-1)) (i+1)
  in loop 0 0;;

let _ = read_int () and
    a = read_line () |> to_list;;
let () =
  let ct = sort a in
  print_array a;print_int ct;print_newline ();;