let content = '';
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', buf => {
  content += buf;
});

process.stdin.on('end', () => {
  let [, nums] = content
    .trim()
    .split('\n')
    .map(line => line.split(' ').map(n => +n));
  bubbleSort(nums);
});

const bubbleSort = nums => {
  let flag = true;
  let cnt = 0;
  let min = 1;
  while(flag) {
    flag = false;
    for (let i = nums.length - 1; i >= min; i--) {
      if (nums[i] < nums[i - 1]) {
        flag = true;
        const tmp = nums[i];
        nums[i] = nums[i - 1];
        nums[i - 1] = tmp;
        cnt++;
      }
    }
    min++;
  }

  console.log(nums.join(' '));
  console.log(cnt);
};
