
function bubbleSort (A) {
    let n = A.length;
    let count = 0;
    let flag = 1;
    let i = 0;
    while (flag === 1) {
        flag = 0;
          for (let j = n - 1; j >= i + 1; j--) {
             if (A[j] < A[j-1]) {
                 let tmp = A[j];
                 A[j] = A[j-1];
                 A[j-1] = tmp;
                 flag = 1;
                 count++;
         }
        }
        i++;
    } 
    return [A, count];
}

const input = require('fs').readFileSync('/dev/stdin', 'utf8').split('\n');
input.shift();
let Arr = input[0].split(' ').map(Number);
let result = bubbleSort(Arr);
console.log(result[0].join(' '));
console.log(result[1]);
