n = gets.to_i
arr = gets.chomp.split.map(&:to_i)

count = 0
1.upto(n - 1){|i|
    (n - 1).downto(i){|j|
        if arr[j - 1] > arr[j]
            arr[j - 1], arr[j] = arr[j], arr[j - 1]
            count += 1
        end
    }
}
puts arr.join(" ")
puts count