def bubble_sort(n, as)
  count = 0
  i = 0
  while i < n do
    j = n - 1
    while j > i do
      target = as[j]
      if target < as[j - 1]
        as[j] = as[j - 1]
        as[j - 1] = target
        count = count + 1
      end
      j = j - 1
    end
    i = i + 1
  end
  puts as.join(' ')
  puts count
end

n = STDIN.gets.to_i
as = STDIN.gets.split(' ').map {|x| x.to_i }
bubble_sort(n, as)

