import std.stdio;
import std.string;
import std.algorithm;
import std.conv;

void bubbleSortWithCount(ref int[] arr, out int cnt)
{
  for (auto i = 0; i < arr.length; i++) {
    for (auto j = arr.length-1; j >= i+1; j--) {
      if (arr[j] < arr[j-1]) {
        swap(arr[j], arr[j-1]);
        cnt++;
      }
    }
  }
}


void main(string[] args)
{
  int n = to!int(strip(readln()));
  int[] arr = to!(int[])(split(readln()));
  int cnt;
  bubbleSortWithCount(arr, cnt);
  writeln(join(arr.map!(text), " "));
  writeln(cnt);
}