fun swap(ary: IntArray, p: Int, q: Int) {
  val tmp = ary[p]
  ary[p] = ary[q]
  ary[q] = tmp
}

fun bubbleSort(ary: IntArray, n: Int): Int {
  var count = 0
  var flag = true
  var sorted = 0
  while (flag) {
    flag = false
    for (i in n-1 downTo sorted+1) {
      if (ary[i] < ary[i-1]) {
        swap(ary, i, i-1)
        flag = true
        ++count
      }
    }
    ++sorted
  }
  return count
}

fun main(args: Array<String>) {
  val n = readLine()!!.toInt()
  val ary = readLine()!!.split(' ').map(String::toInt).toIntArray()

  val ans = bubbleSort(ary, n)

  for (i in 0 until n) {
    if (i > 0) print(' ')
    print(ary[i])
  }
  println()
  println(ans) 
}

