'use strict';

const main = input => {
  input = input.split('\n');
  const N = Number(input[0]);
  const A = input[1]
    .trim()
    .split(' ')
    .map(Number);
  bubbleSort(A, N);
};

function bubbleSort(A, N) {
  let flag = true;
  let count = 0;
  while (flag) {
    flag = false;
    let i = 0;
    for (let j = N - 1; j >= i + 1; --j) {
      if (A[j - 1] > A[j]) {
        const tmp = A[j];
        A[j] = A[j - 1];
        A[j - 1] = tmp;
        flag = true;
        ++count;
      }
    }
    ++i;
  }
  console.log(A.join(' '));
  console.log(count);
}

main(require('fs').readFileSync('/dev/stdin', 'utf8'));

