#?????????????????????????????°??????

#swap???????????????????????????
def Swap(a, b)
    return b, a
end

#???????????????????????????????????????????????????
def BubbleSort(a, length)
    count = 0
    for i in 0..length-1
        for j in i+1..length-1
            k = (i + 1) + (length - 1) - j
            if a[k] < a[k-1] then
                count = count + 1
                a[k], a[k-1] = Swap(a[k], a[k-1])
            end
        end
    end
    #?????????????????????????????¨???????????°???main?????????
    return a, count
end

#????????????????????????????????¨?????????
def PrintBubbleSort(a, length, count)
    i = -1
    for i in 0..length-2
        print a[i], " "
    end
    print a[i+1], "\n"
    print count, "\n"
end

#?????????????????°?????\?????????
length = readline.to_i

#?????????????????????????????????????????§??\????????????
A = gets.chomp.split(" ").map(&:to_i)

B, count = BubbleSort(A, length)

PrintBubbleSort(B, length, count)