import java.util.*

fun bubbleSort(A: Array<Int>) {
    var count = 0
    var flg = true
    var i = 1
    while(flg) {
        flg = false
        for (j in A.size - 1 downTo i) {
            if (A[j] < A[j - 1]) {
                A[j] = A[j - 1].also { A[j - 1] = A[j] }
                flg = true
                count++
            }
        }
        i++
    }
    println(A.joinToString(" "))
    println(count)
}

fun main(args: Array<String>) {
    val scanner = Scanner(System.`in`)
    val N = scanner.nextInt()
    val list = mutableListOf<Int>()
    for (i in 0 until N) {
        list.add(scanner.nextInt())
    }
    bubbleSort(list.toTypedArray())
}
