import scala.io.StdIn._

object Main extends App {

  def bubbleSort(a: Array[Int], n: Int) {
    var cnt = 0
    for (i <- (1 to n-1)) {
      for (j <- (n-1 to i by -1)) {
        val x = a(j)
        if (a(j-1) > a(j)) {
          a(j) = a(j-1)
          a(j-1) = x
          cnt +=1
        }
      }
    }
    println(a.mkString(" "))
    println(cnt)    
  }
  
  val n = readLine.toInt
  val a: Array[Int] = readLine.split(" ")
                              .map(s => s.toInt)
                              .toArray
  bubbleSort(a, n)
}
