import scala.io.StdIn.{readInt, readLine}

object Main extends App {
  val N = readInt()
  val arr = readLine.split(" ").map(_.toInt)

  def bubbleSort(A: Array[Int], N: Int): Int = {
    var flag = true
    var count = 0
    while (flag) {
      flag = false
      for (j <- N - 1 to 1 by -1) {
        if (A(j) < A(j - 1)) {
          val tmp = A(j)
          A(j) = A(j - 1)
          A(j - 1) = tmp
          count += 1
          flag = true
        }
      }
    }
    count
  }

  val ans = bubbleSort(arr, arr.length)

  println(arr.mkString(" "))
  println(ans)
}
