fun main(args: Array<String>) {
  val N = readLine()!!.toInt()
  val A = readLine()!!.split(" ").map(String::toInt).toMutableList()

  var sortedCount = 0
  var count = 0
  var flag = true
  while (flag) {
    flag = false
    (N - 1 downTo sortedCount + 1).forEach { i ->
      if (A[i] < A[i-1]) {
        val tmp = A[i]
        A[i] = A[i-1]
        A[i-1] = tmp
        flag = true
        count += 1
      }
    }
    sortedCount += 1
  }

  println(A.joinToString(" "))
  println(count)
}
