var bubbleSort = function(arr, n) {
  var isSwapped = 1;
  var numSwapped = 0;
  while (isSwapped) {
    isSwapped = 0;
    for (var i = n - 1; i >= 1; i -= 1) {
      if (arr[i] < arr[i - 1]) {
        var temp = arr[i];
        arr[i] = arr[i - 1];
        arr[i - 1] = temp;
        numSwapped += 1;
        isSwapped = 1;
      }
    }
  }
  return numSwapped;
};

var n = 0;
var arr = [];
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
input.split('\n').forEach(function(line, i) {
  if (line !== '') {
    if (i % 2 === 0) {
      n = parseInt(line, 10);
    } else {
      arr = line.trim().split(' ').map(Number);
      var num = bubbleSort(arr, n);
      console.log(arr.join(' '));
      console.log(num);
    }
  }
});