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

object Main {
  def bubbleSort(a:Array[Int]):Int = {
    var cnt = 0
    for(i <- 0 until a.length) {
      for(j <- a.length-1 to i+1 by -1) {
        if(a(j) < a(j-1)) {
          val t = a(j)
          a(j) = a(j-1)
          a(j-1) = t
          cnt += 1
        }
      }
    }
    cnt
  }

  def main(args:Array[String]) = {
    val n = readInt
    val a = readLine.split(" ").map(_.toInt)
    val cnt = bubbleSort(a)

    println(a.mkString(" "))
    println(cnt)
  }
}