const config = {
    input: '/dev/stdin',
    newline: '\n'
};

const line = require('fs').readFileSync(config.input, 'utf-8').split(config.newline);

let count = 0;

const bsort = arr => {
    let flag = true;
    while (flag) {
        flag = false;
        for (let i = arr.length-1; i > 0; i--) {
            if (arr[i] < arr[i-1]) {
                const tmp = arr[i];
                arr[i] = arr[i-1];
                arr[i-1] = tmp;
                flag = true;
                count++;
            }
        }
    }

    return arr;
};

console.log(bsort(line[1].split(' ').map(x => parseInt(x))).join(' '));
console.log(count);

