let () =
  let swap arr n =
    let tmp = arr.(n) in
    arr.(n) <- arr.(n-1); arr.(n-1) <- tmp in
  let rec bubbleSort arr n flag counter =
    if n = 0 then (flag, counter)
    else if arr.(n) < arr.(n-1) then (swap arr n; bubbleSort arr (n-1) true (counter+1))
    else bubbleSort arr (n-1) flag counter in
  Scanf.scanf "%d\n" @@ fun n ->
  let arr = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun d -> d in
  let flag = ref true in
  let counter = ref 0 in
  while !flag do
    let (f, c) = bubbleSort arr (n-1) false 0 in
    counter := !counter + c; flag := f
  done;
  for i = 0 to n - 2 do
    Printf.printf "%d " arr.(i)
  done;
  Printf.printf "%d\n" arr.(n-1);
  Printf.printf "%d\n" !counter
