const bubbleSort = (n, arr) => {
  let isChanged = true;
  let counter = 0;
  while (isChanged) {
    isChanged = false;
    for (let j = n - 1; j >= 1; j--) {
      if (arr[j - 1] > arr[j]) {
        const tmp = arr[j];
        arr[j] = arr[j - 1];
        arr[j - 1] = tmp;
        isChanged = true;
        counter += 1;
      }
    }
  }
  console.log(arr.join(" "));
  console.log(counter);
};

process.stdin.resume();
process.stdin.setEncoding("utf8");
let input_string = "";

process.stdin.on("data", chunk => {
  input_string += chunk;
});

process.stdin.on("end", () => {
  const lines = input_string.split("\n").filter(v => {
    return v !== "";
  });
  bubbleSort(lines[0], lines[1].split(" "));
});

