using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;

namespace Bubble_Sort
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            var input = Console.ReadLine().Split(' ');
            int[] array = new int[n];
            for (int i = 0; i < n; ++i) array[i] = int.Parse(input[i]);
            int count = My_Algorithm.Bubble_Sort_Count(ref array);
            for (int i = 0; i < n - 1; ++i) Console.Write("{0} ", array[i]);
            Console.WriteLine(array[array.Length - 1]);
            Console.WriteLine(count);
        }
    }

    //??????????????? array of int,long and double
    class Int_Array
    {
        private int[] array;
        public int Length()
        {
            return this.array.Length;
        }
        public int this[int n]
        {
            set
            {
                this.array[n] = value;
            }
            get
            {
                return this.array[n];
            }
        }
        public Int_Array(String[] input)
        {
            this.array = new int[input.Length];
            for (int i = 0; i < input.Length; ++i)
            {
                this.array[i] = int.Parse(input[i]);
            }
        }
        public Int_Array(int[] input)
        {
            this.array = new int[input.Length];
            for (int i = 0; i < input.Length; ++i)
            {
                this.array[i] = input[i];
            }
        }
    }
    class Long_Array
    {
        private long[] array;
        public int Length()
        {
            return this.array.Length;
        }
        public long this[long n]
        {
            set
            {
                this.array[n] = value;
            }
            get
            {
                return this.array[n];
            }
        }
        public Long_Array(String[] input)
        {
            this.array = new long[input.Length];
            for (long i = 0; i < input.Length; ++i)
            {
                this.array[i] = long.Parse(input[i]);
            }
        }
        public Long_Array(long[] input)
        {
            this.array = new long[input.Length];
            for (long i = 0; i < input.Length; ++i)
            {
                this.array[i] = input[i];
            }
        }
    }
    class Double_Array
    {
        private double[] array;
        public int Length()
        {
            return this.array.Length;
        }
        public double this[int n]
        {
            set
            {
                this.array[n] = value;
            }
            get
            {
                return this.array[n];
            }
        }
        public Double_Array(String[] input)
        {
            this.array = new double[input.Length];
            int seisu, zissu;
            for (int i = 0; i < input.Length; ++i)
            {
                var work = input[i].Split('.');
                seisu = int.Parse(work[0]);
                zissu = int.Parse(work[1]);
                array[i] = seisu + zissu / Math.Pow(10, My_Algorithm.Digits(zissu));
            }
        }
        public Double_Array(double[] input)
        {
            this.array = new double[input.Length];
            for(int i=0;i<input.Length;++i)
            {
                this.array[i] = input[i];
            }
        }
    }

    public static class My_Algorithm
    {
        //?????\????????? Insertion_Sort
        public static void Insertion_Sort(ref int[] array)
        {
            for (int i = 1; i < array.Length; ++i)
            {
                int v = array[i];
                int j = i - 1;
                while (j >= 0 && array[j] > v)
                {
                    array[j + 1] = array[j];
                    --j;
                }
                array[j + 1] = v;
                //for (int k = 0; k < array.Length - 1; ++k) Console.Write("{0} ", array[k]);
                //Console.WriteLine(array[array.Length - 1]);
            }
        }

        //?????????????????? Bubble_Sort
        public static void Bubble_Sort(ref int[] array)
        {
            for (int i = array.Length - 1; i > 0; --i)
            {
                for (int j = i - 1; j >= 0; --j)
                {
                    if(array[j-1] > array[j])
                    {
                        My_Algorithm.Swap(ref array[j - 1], ref array[j]);
                    }
                }
            }
        }
        //??????????????????(??\??????????????°????????????) Bubble_Sort_Count
        public static int Bubble_Sort_Count(ref int[] array)
        {
            int count = 0;
            for (int i = 0; i < array.Length - 1; ++i)
            {
                for (int j = array.Length - 1; j > i; --j)
                {
                    if (array[j] < array[j - 1])
                    {
                        My_Algorithm.Swap(ref array[j], ref array[j - 1]);
                        ++count;
                    }
                }
                //for (int j = 0; j < array.Length - 1; ++j) Console.Write("{0} ", array[j]); Console.WriteLine(array[array.Length - 1]);
            }
            return count;
        }

        //?????§??¬?´???° Greatest_Common_Divisor
        public static int GCD(int m, int n)
        {
            if ((m == 0) || (n == 0)) return 0;
            while ((m != 0) && (n != 0))
            {
                if (m > n) m = m % n;
                else n = n % m;
            }
            if (m == 0) return n;
            else return m;
        }
        public static long GCD(long m, long n)
        {
            if ((m == 0) || (n == 0)) return 0;
            while ((m != 0) && (n != 0))
            {
                if (m > n) m = m % n;
                else n = n % m;
            }
            if (m == 0) return n;
            else return m;
        }
        //????°???¬?????° Least_Common_Multiple
        public static int LCM(int m, int n)
        {
            if ((m == 0) || (n == 0)) return 0;
            return m * n / My_Algorithm.GCD(m, n);
        }
        public static long LCM(long m, long n)
        {
            if ((m == 0) || (n == 0)) return 0;
            return m * n / My_Algorithm.GCD(m, n);
        }

        //??¨???????????????????????? Eratosthenes's_Sieve
        public static List<int> Prime_Number_List(int n)
        {
            bool k;
            List<int> list = new List<int>();
            list.Add(2);
            for (int i = 3; i <= n; i += 2)
            {
                k = true;
                for (int j = 3; j * j <= i; j += 2)
                {
                    if (i % j == 0)
                    {
                        k = false;
                        break;
                    }
                }
                if (k) list.Add(i);
            }
            return list;
        }
        public static List<long> Prime_Number_List(long n)
        {
            bool k;
            List<long> list = new List<long>();
            list.Add(2);
            for (int i = 3; i <= n; i += 2)
            {
                k = true;
                for (int j = 3; j * j <= i; j += 2)
                {
                    if (i % j == 0)
                    {
                        k = false;
                        break;
                    }
                }
                if (k) list.Add(i);
            }
            return list;
        }

        //?´???°???????????? Is_Prime?
        public static bool Is_Prime(int n)
        {
            if (n < 2) return false;
            if (n == 2) return true;
            if (n % 2 == 0) return false;
            for (int i = 3; i < n; ++i) if (n % i == 0) return false;
            return true;
        }
        public static bool Is_Prime(long n)
        {
            if (n < 2) return false;
            if (n == 2) return true;
            if (n % 2 == 0) return false;
            for (int i = 3; i < n; ++i) if (n % i == 0) return false;
            return true;
        }

        //???????????°????±???????(??´??°) determine the number of digits(integer)
        public static int Digits(int n)
        {
            return n.ToString("D").Length;
        }
        public static long Digits(long n)
        {
            return n.ToString("D").Length;
        }

        //????´??????\????????? Swap
        public static void Swap<T>(ref T a, ref T b)
        {
            var work = a;
            a = b;
            b = work;
        }

    }
}


Measured memory_bytes: 9136