let inputString = '';

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});
process.stdin.on('end', function() {
    inputString = inputString.replace(/\s*$/, '')
        .split('\n')
        .map(str => str.replace(/\s*$/, ''));

    bubbleSort(inputString[1].split(' ').map(str => Number(str)));
});

const bubbleSort = (arr) => {
  let swap = 0;
  let swapped = true;
  for (let i=0; swapped; i++) {
    swapped = false;
    for (let j=arr.length-1; j>i; j--) {
      const left = arr[j-1];
      const right = arr[j];
      if (left > right) {
        arr[j-1] = right;
        arr[j] = left;
        swap++;
        swapped = true;
      }
    }
  }

  console.log(arr.join(' '));
  console.log(swap);
};
