var lines = [];
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
reader.on('line', (line) => {
  lines.push(line);
});
reader.on('close', () => {
  const N = Number.parseInt(lines[0]);
  var array = new Array(N);
  for (var n = 0; n < N; n++) {
    array[n] = Number.parseInt(lines[1].split(' ')[n]);
  }
  
  bubbleSort(array, N);
});

function bubbleSort(A, N) {
  var cnt = 0;
  var flag = true;
  while (flag) {
    flag = false;
    for (var j = N - 1; j >= 0; j--) {
      if (A[j] < A[j - 1]) {
        var tmp = A[j];
        A[j] = A[j - 1];
        A[j - 1] = tmp;
        flag = true;
        cnt++;
      }
    }
  }
  
  // 結果出力
  console.log(A.join(' '));
  console.log(cnt);
}
