import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = sc.nextInt();
        int[] s = new int[count];
        for (int i = 0; i < count; i++) {
            s[i] = sc.nextInt();
        }
        bubbleSort(s);
    }

    private static int[] bubbleSort(int[] a) {
        int count = 0;
        boolean flag = true;
        while (flag) {
            flag = false;
            for (int i = 1; i < a.length; i++) {
                if (a[i - 1] > a[i]) {
                    int tmp = a[i];
                    a[i] = a[i - 1];
                    a[i - 1] = tmp;
                    flag = true;
                    count++;
                }
            }
        }
        printArray(a);
        System.out.println(count);
        return a;
    }

    private static void printArray(int[] array) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            sb.append(array[i]);
            if (i != array.length - 1) {
                sb.append(" ");
            }
        }
        System.out.println(sb);
    }
} import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = sc.nextInt();
        int[] s = new int[count];
        for (int i = 0; i < count; i++) {
            s[i] = sc.nextInt();
        }
        bubbleSort(s);
    }

    private static int[] bubbleSort(int[] a) {
        int count = 0;
        boolean flag = true;
        while (flag) {
            flag = false;
            for (int i = 1; i < a.length; i++) {
                if (a[i - 1] > a[i]) {
                    int tmp = a[i];
                    a[i] = a[i - 1];
                    a[i - 1] = tmp;
                    flag = true;
                    count++;
                }
            }
        }
        printArray(a);
        System.out.println(count);
        return a;
    }

    private static void printArray(int[] array) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            sb.append(array[i]);
            if (i != array.length - 1) {
                sb.append(" ");
            }
        }
        System.out.println(sb);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = sc.nextInt();
        int[] s = new int[count];
        for (int i = 0; i < count; i++) {
            s[i] = sc.nextInt();
        }
        bubbleSort(s);
    } main {
        Scanner sc = new Scanner(System.in);
        int count = sc.nextInt();
        int[] s = new int[count];
        for (int i = 0; i < count; i++) {
            s[i] = sc.nextInt();
        }
        bubbleSort(s);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int count = sc.nextInt(); count sc.nextInt() sc.nextInt sc int[] s = new int[count]; s new int[count] count for (int i = 0; i < count; i++) {
            s[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < count i count i++ i++ i {
            s[i] = sc.nextInt();
        } {
            s[i] = sc.nextInt();
        } s[i] = sc.nextInt() s[i] s i sc.nextInt() sc.nextInt sc bubbleSort(s) bubbleSort s String[] args args private static int[] bubbleSort(int[] a) {
        int count = 0;
        boolean flag = true;
        while (flag) {
            flag = false;
            for (int i = 1; i < a.length; i++) {
                if (a[i - 1] > a[i]) {
                    int tmp = a[i];
                    a[i] = a[i - 1];
                    a[i - 1] = tmp;
                    flag = true;
                    count++;
                }
            }
        }
        printArray(a);
        System.out.println(count);
        return a;
    } bubbleSort {
        int count = 0;
        boolean flag = true;
        while (flag) {
            flag = false;
            for (int i = 1; i < a.length; i++) {
                if (a[i - 1] > a[i]) {
                    int tmp = a[i];
                    a[i] = a[i - 1];
                    a[i - 1] = tmp;
                    flag = true;
                    count++;
                }
            }
        }
        printArray(a);
        System.out.println(count);
        return a;
    } int count = 0; count 0 boolean flag = true; flag true while (flag) {
            flag = false;
            for (int i = 1; i < a.length; i++) {
                if (a[i - 1] > a[i]) {
                    int tmp = a[i];
                    a[i] = a[i - 1];
                    a[i - 1] = tmp;
                    flag = true;
                    count++;
                }
            }
        } flag {
            flag = false;
            for (int i = 1; i < a.length; i++) {
                if (a[i - 1] > a[i]) {
                    int tmp = a[i];
                    a[i] = a[i - 1];
                    a[i - 1] = tmp;
                    flag = true;
                    count++;
                }
            }
        } flag = false flag false for (int i = 1; i < a.length; i++) {
                if (a[i - 1] > a[i]) {
                    int tmp = a[i];
                    a[i] = a[i - 1];
                    a[i - 1] = tmp;
                    flag = true;
                    count++;
                }
            } int i = 1; int i = 1; i 1 i < a.length i a.length a a.length i++ i++ i {
                if (a[i - 1] > a[i]) {
                    int tmp = a[i];
                    a[i] = a[i - 1];
                    a[i - 1] = tmp;
                    flag = true;
                    count++;
                }
            } {
                if (a[i - 1] > a[i]) {
                    int tmp = a[i];
                    a[i] = a[i - 1];
                    a[i - 1] = tmp;
                    flag = true;
                    count++;
                }
            } if (a[i - 1] > a[i]) {
                    int tmp = a[i];
                    a[i] = a[i - 1];
                    a[i - 1] = tmp;
                    flag = true;
                    count++;
                } a[i - 1] > a[i] a[i - 1] a i - 1 i 1 a[i] a i {
                    int tmp = a[i];
                    a[i] = a[i - 1];
                    a[i - 1] = tmp;
                    flag = true;
                    count++;
                } int tmp = a[i]; tmp a[i] a i a[i] = a[i - 1] a[i] a i a[i - 1] a i - 1 i 1 a[i - 1] = tmp a[i - 1] a i - 1 i 1 tmp flag = true flag true count++ count printArray(a) printArray a System.out.println(count) System.out.println System.out System System.out count return a; a int[] a a private static void printArray(int[] array) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            sb.append(array[i]);
            if (i != array.length - 1) {
                sb.append(" ");
            }
        }
        System.out.println(sb);
    } printArray {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            sb.append(array[i]);
            if (i != array.length - 1) {
                sb.append(" ");
            }
        }
        System.out.println(sb);
    } StringBuilder sb = new StringBuilder(); sb new StringBuilder() for (int i = 0; i < array.length; i++) {
            sb.append(array[i]);
            if (i != array.length - 1) {
                sb.append(" ");
            }
        } int i = 0; int i = 0; i 0 i < array.length i array.length array array.length i++ i++ i {
            sb.append(array[i]);
            if (i != array.length - 1) {
                sb.append(" ");
            }
        } {
            sb.append(array[i]);
            if (i != array.length - 1) {
                sb.append(" ");
            }
        } sb.append(array[i]) sb.append sb array[i] array i if (i != array.length - 1) {
                sb.append(" ");
            } i != array.length - 1 i array.length - 1 array.length array array.length 1 {
                sb.append(" ");
            } sb.append(" ") sb.append sb " " System.out.println(sb) System.out.println System.out System System.out sb int[] array array public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = sc.nextInt();
        int[] s = new int[count];
        for (int i = 0; i < count; i++) {
            s[i] = sc.nextInt();
        }
        bubbleSort(s);
    }

    private static int[] bubbleSort(int[] a) {
        int count = 0;
        boolean flag = true;
        while (flag) {
            flag = false;
            for (int i = 1; i < a.length; i++) {
                if (a[i - 1] > a[i]) {
                    int tmp = a[i];
                    a[i] = a[i - 1];
                    a[i - 1] = tmp;
                    flag = true;
                    count++;
                }
            }
        }
        printArray(a);
        System.out.println(count);
        return a;
    }

    private static void printArray(int[] array) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            sb.append(array[i]);
            if (i != array.length - 1) {
                sb.append(" ");
            }
        }
        System.out.println(sb);
    }
} public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = sc.nextInt();
        int[] s = new int[count];
        for (int i = 0; i < count; i++) {
            s[i] = sc.nextInt();
        }
        bubbleSort(s);
    }

    private static int[] bubbleSort(int[] a) {
        int count = 0;
        boolean flag = true;
        while (flag) {
            flag = false;
            for (int i = 1; i < a.length; i++) {
                if (a[i - 1] > a[i]) {
                    int tmp = a[i];
                    a[i] = a[i - 1];
                    a[i - 1] = tmp;
                    flag = true;
                    count++;
                }
            }
        }
        printArray(a);
        System.out.println(count);
        return a;
    }

    private static void printArray(int[] array) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            sb.append(array[i]);
            if (i != array.length - 1) {
                sb.append(" ");
            }
        }
        System.out.println(sb);
    }
} Main
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = sc.nextInt();
        int[] s = new int[count];
        for (int i = 0; i < count; i++) {
            s[i] = sc.nextInt();
        }
        bubbleSort(s);
    }

    private static int[] bubbleSort(int[] a) {
        int count = 0;
        boolean flag = true;
        while (flag) {
            flag = false;
            for (int i = 1; i < a.length; i++) {
                if (a[i - 1] > a[i]) {
                    int tmp = a[i];
                    a[i] = a[i - 1];
                    a[i - 1] = tmp;
                    flag = true;
                    count++;
                }
            }
        }
        printArray(a);
        System.out.println(count);
        return a;
    }

    private static void printArray(int[] array) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            sb.append(array[i]);
            if (i != array.length - 1) {
                sb.append(" ");
            }
        }
        System.out.println(sb);
    }
}