object Main {
  def main(args: Array[String]): Unit = {
    val scanner = new java.util.Scanner(System.in)

    val n = scanner.nextInt
    val a = Array.fill(n)(scanner.nextInt)

    var flag = true
    var c = 0
    while(flag) {
      flag = false
      for(j <- n - 1 to 1 by -1) {
        if(a(j) < a(j - 1)) {
          val t = a(j)
          a(j) = a(j - 1)
          a(j - 1) = t
          flag = true
          c = c + 1
        }
      }
    }
    println(a.mkString(" "))
    println(c)
  }
}

