fun main(args: Array<String>){
    val n = readLine()!!.toInt()
    val list = readLine()!!.split(" ").map{it.toInt()}.toMutableList()
    val count = bubbleSort(list, n)
    for(i in 0 until list.size) {
        print(list[i].toString())
        if(i != list.size-1) {
            print(" ")
        } else {
            println()
        }
    }
    println(count)
}

fun bubbleSort(A: MutableList<Int>, n: Int): Int {
    var count = 0
    var flag = 1
    while(flag != 0){
        flag  = 0
        for(i in n-1 downTo 1){
            if(A[i] < A[i-1]) {
                val temp = A[i]
                A[i] = A[i-1]
                A[i-1] = temp
                flag = 1
                count++
            }
        }
    }
    return count
}
