fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val array = readLine()!!.split(" ").map { it.toInt() } as MutableList<Int>
    val (sortedArray, exchanges) = bubbleSort(n, array)
    println(sortedArray.joinToString(" "))
    println(exchanges)
}

fun bubbleSort(n: Int, array: MutableList<Int>): Pair<List<Int>, Int> {
    var head = 0
    var exchanges = 0
    while (head < n - 1) {

        for (j in n - 1 downTo head + 1) {
            if (array[j] < array[j - 1]) {
                val small = array[j]
                val large = array[j - 1]
                array[j] = large
                array[j - 1] = small
                exchanges++
            }
        }
        head ++
    }
    return array to exchanges
}
