object Main {
  def main(args: Array[String]): Unit = {
    val n = io.StdIn.readLine().toInt
    val ints: Seq[Int] = io.StdIn.readLine().split(" ").map(_.toInt)
    val (arr, cnt) = if (n==1) bubbleSort(ints, n) else (ints, 0)
    println(arr.mkString(" "))
    println(cnt)
  }

  def bubbleSort(arr: Seq[Int], n: Int): (Seq[Int], Int) = {
    arr.recursiveFunc(0)
  }

  implicit class RichSeq(seq: Seq[Int]) {
    def recursiveFunc(sumSwapCnt: Int): (Seq[Int], Int) = {
      seq.bubble(seq.length - 1, 0) match {
        case (a, 0) => (a, sumSwapCnt)
        case (a, x) => a.recursiveFunc(sumSwapCnt + x)
      }
    }

    def bubble(idx: Int, swapCnt: Int): (Seq[Int], Int) = {
      (seq.sortSwap(idx, swapCnt), idx) match {
        case (t, 1) => t
        case (t, n) => t._1.bubble(idx - 1, t._2)
      }
    }

    def sortSwap(i: Int, cnt: Int): (Seq[Int], Int) = {
      if (seq(i) < seq(i - 1)) (seq.swap(i, i - 1), cnt+1) else (seq, cnt)
    }

    def swap(i: Int, j: Int): Seq[Int] = seq.updated(j, seq(i)).updated(i, seq(j))

  }

  implicit class RichObj[T](obj: T) {
    def tap[U](f: T => U): T = {
      f(obj);
      obj;
    }
  }
}
