import scala.io.StdIn.{readInt, readLine}
object Main {
    def main(args: Array[String]): Unit = {
        val len = readInt
        val arr = readLine.split(" ").map(_.toInt)
        val (ret, cnt) = bubbleSort(len, arr)
        println(ret.mkString(" ") + "\n" + cnt)
    }
    
    def bubbleSort(len: Int, arr: Array[Int]): (Array[Int], Int) = {
        val ret = new Array[Int](len)
        arr.copyToArray(ret)
        var cnt = 0
        (1 until len).foreach { i =>
            (i until len).reverse.foreach { j =>
                if (ret(j - 1) > ret(j)) {
                    cnt += 1
                    swap(ret, j, j - 1)
                }
            }
        }
        (ret, cnt)
    }
    
    def swap(arr: Array[Int], a: Int, b: Int): Unit = {
        val tmp = arr(a)
        arr(a) = arr(b)
        arr(b) = tmp
    }
}
