object Main {
  def main(args: Array[String]): Unit = {
    val n = io.StdIn.readInt()
    val a = io.StdIn.readLine().split(" ").map(_.toInt)
    val count = bubbleSort(a, n)
    println(a.mkString(" "))
    println(count)
  }

  def bubbleSort(a: Array[Int], n: Int): Int = {
    var flag = true
    var count = 0
    while (flag) {
      val c = (n - 1 to 1 by -1).count { j =>
        if (a(j) < a(j - 1)) {
          val v = a(j)
          a(j) = a(j - 1)
          a(j - 1) = v
          true
        } else {
          false
        }
      }
      count += c
      flag =
        if (c > 0) true
        else false
    }
    count
  }
}