def bubble_sort(a, n)
    swap = 0
    for i in 1..(n - 1) do
        flag = false

        (n - 1).downto(i) do |j|
            if a[j - 1] > a[j]
                a[j - 1], a[j] = a[j], a[j - 1]
                swap += 1
                flag = true
            end
        end
        break unless flag
    end
    puts a.join(" ")
    return swap
end

n = gets.to_i
a = gets.split.map(&:to_i)

puts bubble_sort(a, n)

