fun main(args: Array<String>) {
  readLine()
  val array = readLine()
    ?.split("\\s+".toRegex())
    ?.map(String::toInt)
    ?.toIntArray()
    ?:intArrayOf()
  var cnt = 0
  for (i in array.indices) {
    for (j in array.size - 2 downTo i) {
      if (array[j] > array[j+1]) {
        array.swap(j, j+1)
        cnt++
      }
    }
  }
  println(array.joinToString(" "))
  println(cnt)
}

fun IntArray.swap(i: Int, j: Int) {
  val tmp = this[i]
  this[i] = this[j]
  this[j] = tmp
}
