import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        
        //set array
        String[] st = br.readLine().split(" ");
        int[] array = new int[n];
        for (int i = 0; i < n; i++){
            int a = Integer.parseInt(st[i]);
            array[i] = a;
        }
        
        int count = bubbleSort(array);
        printArray(array);
        System.out.println(count);
        
    }
    static void printArray(int[] arr){
        for (int i = 0; i < arr.length - 1; i++){
            System.out.print(arr[i] + " ");
        }
        System.out.println(arr[arr.length-1]);
    }
    static void printArray2(int[] arr){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length - 1; i++){
            sb.append(arr[i]);
            sb.append(" ");
        }
        sb.append(arr[arr.length - 1]);
        System.out.println(sb.toString());
    }
    static int bubbleSort(int[] arr){
        //bubble sort and count swaps
        int swapcnt = 0;
        for (int i = 0; i < arr.length; i++){
            for (int j = arr.length - 1; j > i; j--){
                if ( arr[j] < arr[j-1]){
                    swap(arr, j, j - 1);
                    swapcnt += 1;
                }
            }
        }
        return swapcnt;
    }
    static void swap(int[] arr, int i, int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io import java.io.IOException; IOException java.io public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        
        //set array
        String[] st = br.readLine().split(" ");
        int[] array = new int[n];
        for (int i = 0; i < n; i++){
            int a = Integer.parseInt(st[i]);
            array[i] = a;
        }
        
        int count = bubbleSort(array);
        printArray(array);
        System.out.println(count);
        
    }
    static void printArray(int[] arr){
        for (int i = 0; i < arr.length - 1; i++){
            System.out.print(arr[i] + " ");
        }
        System.out.println(arr[arr.length-1]);
    }
    static void printArray2(int[] arr){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length - 1; i++){
            sb.append(arr[i]);
            sb.append(" ");
        }
        sb.append(arr[arr.length - 1]);
        System.out.println(sb.toString());
    }
    static int bubbleSort(int[] arr){
        //bubble sort and count swaps
        int swapcnt = 0;
        for (int i = 0; i < arr.length; i++){
            for (int j = arr.length - 1; j > i; j--){
                if ( arr[j] < arr[j-1]){
                    swap(arr, j, j - 1);
                    swapcnt += 1;
                }
            }
        }
        return swapcnt;
    }
    static void swap(int[] arr, int i, int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
} Main public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        
        //set array
        String[] st = br.readLine().split(" ");
        int[] array = new int[n];
        for (int i = 0; i < n; i++){
            int a = Integer.parseInt(st[i]);
            array[i] = a;
        }
        
        int count = bubbleSort(array);
        printArray(array);
        System.out.println(count);
        
    } main {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        
        //set array
        String[] st = br.readLine().split(" ");
        int[] array = new int[n];
        for (int i = 0; i < n; i++){
            int a = Integer.parseInt(st[i]);
            array[i] = a;
        }
        
        int count = bubbleSort(array);
        printArray(array);
        System.out.println(count);
        
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int n = Integer.parseInt(br.readLine()); n Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br String[] st = br.readLine().split(" "); st br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int[] array = new int[n]; array new int[n] n for (int i = 0; i < n; i++){
            int a = Integer.parseInt(st[i]);
            array[i] = a;
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            int a = Integer.parseInt(st[i]);
            array[i] = a;
        } {
            int a = Integer.parseInt(st[i]);
            array[i] = a;
        } int a = Integer.parseInt(st[i]); a Integer.parseInt(st[i]) Integer.parseInt Integer st[i] st i array[i] = a array[i] array i a int count = bubbleSort(array); count bubbleSort(array) bubbleSort array printArray(array) printArray array System.out.println(count) System.out.println System.out System System.out count String[] args args static void printArray(int[] arr){
        for (int i = 0; i < arr.length - 1; i++){
            System.out.print(arr[i] + " ");
        }
        System.out.println(arr[arr.length-1]);
    } printArray {
        for (int i = 0; i < arr.length - 1; i++){
            System.out.print(arr[i] + " ");
        }
        System.out.println(arr[arr.length-1]);
    } for (int i = 0; i < arr.length - 1; i++){
            System.out.print(arr[i] + " ");
        } int i = 0; int i = 0; i 0 i < arr.length - 1 i arr.length - 1 arr.length arr arr.length 1 i++ i++ i {
            System.out.print(arr[i] + " ");
        } {
            System.out.print(arr[i] + " ");
        } System.out.print(arr[i] + " ") System.out.print System.out System System.out arr[i] + " " arr[i] arr i " " System.out.println(arr[arr.length-1]) System.out.println System.out System System.out arr[arr.length-1] arr arr.length-1 arr.length arr arr.length 1 int[] arr arr static void printArray2(int[] arr){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length - 1; i++){
            sb.append(arr[i]);
            sb.append(" ");
        }
        sb.append(arr[arr.length - 1]);
        System.out.println(sb.toString());
    } printArray2 {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length - 1; i++){
            sb.append(arr[i]);
            sb.append(" ");
        }
        sb.append(arr[arr.length - 1]);
        System.out.println(sb.toString());
    } StringBuilder sb = new StringBuilder(); sb new StringBuilder() for (int i = 0; i < arr.length - 1; i++){
            sb.append(arr[i]);
            sb.append(" ");
        } int i = 0; int i = 0; i 0 i < arr.length - 1 i arr.length - 1 arr.length arr arr.length 1 i++ i++ i {
            sb.append(arr[i]);
            sb.append(" ");
        } {
            sb.append(arr[i]);
            sb.append(" ");
        } sb.append(arr[i]) sb.append sb arr[i] arr i sb.append(" ") sb.append sb " " sb.append(arr[arr.length - 1]) sb.append sb arr[arr.length - 1] arr arr.length - 1 arr.length arr arr.length 1 System.out.println(sb.toString()) System.out.println System.out System System.out sb.toString() sb.toString sb int[] arr arr static int bubbleSort(int[] arr){
        //bubble sort and count swaps
        int swapcnt = 0;
        for (int i = 0; i < arr.length; i++){
            for (int j = arr.length - 1; j > i; j--){
                if ( arr[j] < arr[j-1]){
                    swap(arr, j, j - 1);
                    swapcnt += 1;
                }
            }
        }
        return swapcnt;
    } bubbleSort {
        //bubble sort and count swaps
        int swapcnt = 0;
        for (int i = 0; i < arr.length; i++){
            for (int j = arr.length - 1; j > i; j--){
                if ( arr[j] < arr[j-1]){
                    swap(arr, j, j - 1);
                    swapcnt += 1;
                }
            }
        }
        return swapcnt;
    } int swapcnt = 0; swapcnt 0 for (int i = 0; i < arr.length; i++){
            for (int j = arr.length - 1; j > i; j--){
                if ( arr[j] < arr[j-1]){
                    swap(arr, j, j - 1);
                    swapcnt += 1;
                }
            }
        } int i = 0; int i = 0; i 0 i < arr.length i arr.length arr arr.length i++ i++ i {
            for (int j = arr.length - 1; j > i; j--){
                if ( arr[j] < arr[j-1]){
                    swap(arr, j, j - 1);
                    swapcnt += 1;
                }
            }
        } {
            for (int j = arr.length - 1; j > i; j--){
                if ( arr[j] < arr[j-1]){
                    swap(arr, j, j - 1);
                    swapcnt += 1;
                }
            }
        } for (int j = arr.length - 1; j > i; j--){
                if ( arr[j] < arr[j-1]){
                    swap(arr, j, j - 1);
                    swapcnt += 1;
                }
            } int j = arr.length - 1; int j = arr.length - 1; j arr.length - 1 arr.length arr arr.length 1 j > i j i j-- j-- j {
                if ( arr[j] < arr[j-1]){
                    swap(arr, j, j - 1);
                    swapcnt += 1;
                }
            } {
                if ( arr[j] < arr[j-1]){
                    swap(arr, j, j - 1);
                    swapcnt += 1;
                }
            } if ( arr[j] < arr[j-1]){
                    swap(arr, j, j - 1);
                    swapcnt += 1;
                } arr[j] < arr[j-1] arr[j] arr j arr[j-1] arr j-1 j 1 {
                    swap(arr, j, j - 1);
                    swapcnt += 1;
                } swap(arr, j, j - 1) swap arr j j - 1 j 1 swapcnt += 1 swapcnt 1 return swapcnt; swapcnt int[] arr arr static void swap(int[] arr, int i, int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    } swap {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    } int tmp = arr[i]; tmp arr[i] arr i arr[i] = arr[j] arr[i] arr i arr[j] arr j arr[j] = tmp arr[j] arr j tmp int[] arr arr int i i int j j public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        
        //set array
        String[] st = br.readLine().split(" ");
        int[] array = new int[n];
        for (int i = 0; i < n; i++){
            int a = Integer.parseInt(st[i]);
            array[i] = a;
        }
        
        int count = bubbleSort(array);
        printArray(array);
        System.out.println(count);
        
    }
    static void printArray(int[] arr){
        for (int i = 0; i < arr.length - 1; i++){
            System.out.print(arr[i] + " ");
        }
        System.out.println(arr[arr.length-1]);
    }
    static void printArray2(int[] arr){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length - 1; i++){
            sb.append(arr[i]);
            sb.append(" ");
        }
        sb.append(arr[arr.length - 1]);
        System.out.println(sb.toString());
    }
    static int bubbleSort(int[] arr){
        //bubble sort and count swaps
        int swapcnt = 0;
        for (int i = 0; i < arr.length; i++){
            for (int j = arr.length - 1; j > i; j--){
                if ( arr[j] < arr[j-1]){
                    swap(arr, j, j - 1);
                    swapcnt += 1;
                }
            }
        }
        return swapcnt;
    }
    static void swap(int[] arr, int i, int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
} public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        
        //set array
        String[] st = br.readLine().split(" ");
        int[] array = new int[n];
        for (int i = 0; i < n; i++){
            int a = Integer.parseInt(st[i]);
            array[i] = a;
        }
        
        int count = bubbleSort(array);
        printArray(array);
        System.out.println(count);
        
    }
    static void printArray(int[] arr){
        for (int i = 0; i < arr.length - 1; i++){
            System.out.print(arr[i] + " ");
        }
        System.out.println(arr[arr.length-1]);
    }
    static void printArray2(int[] arr){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length - 1; i++){
            sb.append(arr[i]);
            sb.append(" ");
        }
        sb.append(arr[arr.length - 1]);
        System.out.println(sb.toString());
    }
    static int bubbleSort(int[] arr){
        //bubble sort and count swaps
        int swapcnt = 0;
        for (int i = 0; i < arr.length; i++){
            for (int j = arr.length - 1; j > i; j--){
                if ( arr[j] < arr[j-1]){
                    swap(arr, j, j - 1);
                    swapcnt += 1;
                }
            }
        }
        return swapcnt;
    }
    static void swap(int[] arr, int i, int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
} Main
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        
        //set array
        String[] st = br.readLine().split(" ");
        int[] array = new int[n];
        for (int i = 0; i < n; i++){
            int a = Integer.parseInt(st[i]);
            array[i] = a;
        }
        
        int count = bubbleSort(array);
        printArray(array);
        System.out.println(count);
        
    }
    static void printArray(int[] arr){
        for (int i = 0; i < arr.length - 1; i++){
            System.out.print(arr[i] + " ");
        }
        System.out.println(arr[arr.length-1]);
    }
    static void printArray2(int[] arr){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length - 1; i++){
            sb.append(arr[i]);
            sb.append(" ");
        }
        sb.append(arr[arr.length - 1]);
        System.out.println(sb.toString());
    }
    static int bubbleSort(int[] arr){
        //bubble sort and count swaps
        int swapcnt = 0;
        for (int i = 0; i < arr.length; i++){
            for (int j = arr.length - 1; j > i; j--){
                if ( arr[j] < arr[j-1]){
                    swap(arr, j, j - 1);
                    swapcnt += 1;
                }
            }
        }
        return swapcnt;
    }
    static void swap(int[] arr, int i, int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}