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

/**
 * Created by hwzheng on 2017/3/27.
 */
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader Reader = new BufferedReader (new InputStreamReader(System.in));
        int N = Integer.parseInt(Reader.readLine());
        String A[] = Reader.readLine().split(" ",0);
        int[] arr = new int[N];
        for (int i=0; i < N; i++){
            arr[i] = Integer.parseInt(A[i]);
        }
        bubbleSort2(arr,N);
    }

    private static void bubbleSort(int[] arr, int n) {
        boolean flag = true;
        int exCount = 0;
        while (flag){
            flag = false;
            for (int i = n - 1; i > 0; i--){
                if (arr[i] < arr[i-1]){
                    exch(arr,i,i-1);
                    exCount++;
                    flag = true;
                }
            }
        }
        String str = "";
        for (int i = 0; i < arr.length; i++){
            str += arr[i] + " ";
        }
        System.out.println(str.substring(0,str.length()-1));
        System.out.println(exCount);
    }
    
    private static void bubbleSort2(int[] arr, int n){
        int exCount = 0;
        for (int i = 0; i < n; i++){
            for (int j = n-1; j>i; j--){
                if (arr[j] < arr[j-1]){
                    exch(arr,j,j-1);
                    exCount++;
                }
            }
        }
        String str = "";
        for (int i = 0; i < arr.length; i++){
            str += arr[i] + " ";
        }
        System.out.println(str.substring(0,str.length()-1));
        System.out.println(exCount);
    }

    private static void exch(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.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader Reader = new BufferedReader (new InputStreamReader(System.in));
        int N = Integer.parseInt(Reader.readLine());
        String A[] = Reader.readLine().split(" ",0);
        int[] arr = new int[N];
        for (int i=0; i < N; i++){
            arr[i] = Integer.parseInt(A[i]);
        }
        bubbleSort2(arr,N);
    }

    private static void bubbleSort(int[] arr, int n) {
        boolean flag = true;
        int exCount = 0;
        while (flag){
            flag = false;
            for (int i = n - 1; i > 0; i--){
                if (arr[i] < arr[i-1]){
                    exch(arr,i,i-1);
                    exCount++;
                    flag = true;
                }
            }
        }
        String str = "";
        for (int i = 0; i < arr.length; i++){
            str += arr[i] + " ";
        }
        System.out.println(str.substring(0,str.length()-1));
        System.out.println(exCount);
    }
    
    private static void bubbleSort2(int[] arr, int n){
        int exCount = 0;
        for (int i = 0; i < n; i++){
            for (int j = n-1; j>i; j--){
                if (arr[j] < arr[j-1]){
                    exch(arr,j,j-1);
                    exCount++;
                }
            }
        }
        String str = "";
        for (int i = 0; i < arr.length; i++){
            str += arr[i] + " ";
        }
        System.out.println(str.substring(0,str.length()-1));
        System.out.println(exCount);
    }

    private static void exch(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 Reader = new BufferedReader (new InputStreamReader(System.in));
        int N = Integer.parseInt(Reader.readLine());
        String A[] = Reader.readLine().split(" ",0);
        int[] arr = new int[N];
        for (int i=0; i < N; i++){
            arr[i] = Integer.parseInt(A[i]);
        }
        bubbleSort2(arr,N);
    } main {
        BufferedReader Reader = new BufferedReader (new InputStreamReader(System.in));
        int N = Integer.parseInt(Reader.readLine());
        String A[] = Reader.readLine().split(" ",0);
        int[] arr = new int[N];
        for (int i=0; i < N; i++){
            arr[i] = Integer.parseInt(A[i]);
        }
        bubbleSort2(arr,N);
    } BufferedReader Reader = new BufferedReader (new InputStreamReader(System.in)); Reader new BufferedReader (new InputStreamReader(System.in)) int N = Integer.parseInt(Reader.readLine()); N Integer.parseInt(Reader.readLine()) Integer.parseInt Integer Reader.readLine() Reader.readLine Reader String A[] = Reader.readLine().split(" ",0); A Reader.readLine().split(" ",0) Reader.readLine().split Reader.readLine() Reader.readLine Reader " " 0 int[] arr = new int[N]; arr new int[N] N for (int i=0; i < N; i++){
            arr[i] = Integer.parseInt(A[i]);
        } int i=0; int i=0; i 0 i < N i N i++ i++ i {
            arr[i] = Integer.parseInt(A[i]);
        } {
            arr[i] = Integer.parseInt(A[i]);
        } arr[i] = Integer.parseInt(A[i]) arr[i] arr i Integer.parseInt(A[i]) Integer.parseInt Integer A[i] A i bubbleSort2(arr,N) bubbleSort2 arr N String[] args args private static void bubbleSort(int[] arr, int n) {
        boolean flag = true;
        int exCount = 0;
        while (flag){
            flag = false;
            for (int i = n - 1; i > 0; i--){
                if (arr[i] < arr[i-1]){
                    exch(arr,i,i-1);
                    exCount++;
                    flag = true;
                }
            }
        }
        String str = "";
        for (int i = 0; i < arr.length; i++){
            str += arr[i] + " ";
        }
        System.out.println(str.substring(0,str.length()-1));
        System.out.println(exCount);
    } bubbleSort {
        boolean flag = true;
        int exCount = 0;
        while (flag){
            flag = false;
            for (int i = n - 1; i > 0; i--){
                if (arr[i] < arr[i-1]){
                    exch(arr,i,i-1);
                    exCount++;
                    flag = true;
                }
            }
        }
        String str = "";
        for (int i = 0; i < arr.length; i++){
            str += arr[i] + " ";
        }
        System.out.println(str.substring(0,str.length()-1));
        System.out.println(exCount);
    } boolean flag = true; flag true int exCount = 0; exCount 0 while (flag){
            flag = false;
            for (int i = n - 1; i > 0; i--){
                if (arr[i] < arr[i-1]){
                    exch(arr,i,i-1);
                    exCount++;
                    flag = true;
                }
            }
        } flag {
            flag = false;
            for (int i = n - 1; i > 0; i--){
                if (arr[i] < arr[i-1]){
                    exch(arr,i,i-1);
                    exCount++;
                    flag = true;
                }
            }
        } flag = false flag false for (int i = n - 1; i > 0; i--){
                if (arr[i] < arr[i-1]){
                    exch(arr,i,i-1);
                    exCount++;
                    flag = true;
                }
            } int i = n - 1; int i = n - 1; i n - 1 n 1 i > 0 i 0 i-- i-- i {
                if (arr[i] < arr[i-1]){
                    exch(arr,i,i-1);
                    exCount++;
                    flag = true;
                }
            } {
                if (arr[i] < arr[i-1]){
                    exch(arr,i,i-1);
                    exCount++;
                    flag = true;
                }
            } if (arr[i] < arr[i-1]){
                    exch(arr,i,i-1);
                    exCount++;
                    flag = true;
                } arr[i] < arr[i-1] arr[i] arr i arr[i-1] arr i-1 i 1 {
                    exch(arr,i,i-1);
                    exCount++;
                    flag = true;
                } exch(arr,i,i-1) exch arr i i-1 i 1 exCount++ exCount flag = true flag true String str = ""; str "" for (int i = 0; i < arr.length; i++){
            str += arr[i] + " ";
        } int i = 0; int i = 0; i 0 i < arr.length i arr.length arr arr.length i++ i++ i {
            str += arr[i] + " ";
        } {
            str += arr[i] + " ";
        } str += arr[i] + " " str arr[i] + " " arr[i] arr i " " System.out.println(str.substring(0,str.length()-1)) System.out.println System.out System System.out str.substring(0,str.length()-1) str.substring str 0 str.length()-1 str.length() str.length str 1 System.out.println(exCount) System.out.println System.out System System.out exCount int[] arr arr int n n private static void bubbleSort2(int[] arr, int n){
        int exCount = 0;
        for (int i = 0; i < n; i++){
            for (int j = n-1; j>i; j--){
                if (arr[j] < arr[j-1]){
                    exch(arr,j,j-1);
                    exCount++;
                }
            }
        }
        String str = "";
        for (int i = 0; i < arr.length; i++){
            str += arr[i] + " ";
        }
        System.out.println(str.substring(0,str.length()-1));
        System.out.println(exCount);
    } bubbleSort2 {
        int exCount = 0;
        for (int i = 0; i < n; i++){
            for (int j = n-1; j>i; j--){
                if (arr[j] < arr[j-1]){
                    exch(arr,j,j-1);
                    exCount++;
                }
            }
        }
        String str = "";
        for (int i = 0; i < arr.length; i++){
            str += arr[i] + " ";
        }
        System.out.println(str.substring(0,str.length()-1));
        System.out.println(exCount);
    } int exCount = 0; exCount 0 for (int i = 0; i < n; i++){
            for (int j = n-1; j>i; j--){
                if (arr[j] < arr[j-1]){
                    exch(arr,j,j-1);
                    exCount++;
                }
            }
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            for (int j = n-1; j>i; j--){
                if (arr[j] < arr[j-1]){
                    exch(arr,j,j-1);
                    exCount++;
                }
            }
        } {
            for (int j = n-1; j>i; j--){
                if (arr[j] < arr[j-1]){
                    exch(arr,j,j-1);
                    exCount++;
                }
            }
        } for (int j = n-1; j>i; j--){
                if (arr[j] < arr[j-1]){
                    exch(arr,j,j-1);
                    exCount++;
                }
            } int j = n-1; int j = n-1; j n-1 n 1 j>i j i j-- j-- j {
                if (arr[j] < arr[j-1]){
                    exch(arr,j,j-1);
                    exCount++;
                }
            } {
                if (arr[j] < arr[j-1]){
                    exch(arr,j,j-1);
                    exCount++;
                }
            } if (arr[j] < arr[j-1]){
                    exch(arr,j,j-1);
                    exCount++;
                } arr[j] < arr[j-1] arr[j] arr j arr[j-1] arr j-1 j 1 {
                    exch(arr,j,j-1);
                    exCount++;
                } exch(arr,j,j-1) exch arr j j-1 j 1 exCount++ exCount String str = ""; str "" for (int i = 0; i < arr.length; i++){
            str += arr[i] + " ";
        } int i = 0; int i = 0; i 0 i < arr.length i arr.length arr arr.length i++ i++ i {
            str += arr[i] + " ";
        } {
            str += arr[i] + " ";
        } str += arr[i] + " " str arr[i] + " " arr[i] arr i " " System.out.println(str.substring(0,str.length()-1)) System.out.println System.out System System.out str.substring(0,str.length()-1) str.substring str 0 str.length()-1 str.length() str.length str 1 System.out.println(exCount) System.out.println System.out System System.out exCount int[] arr arr int n n private static void exch(int[] arr, int i, int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    } exch {
        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 Reader = new BufferedReader (new InputStreamReader(System.in));
        int N = Integer.parseInt(Reader.readLine());
        String A[] = Reader.readLine().split(" ",0);
        int[] arr = new int[N];
        for (int i=0; i < N; i++){
            arr[i] = Integer.parseInt(A[i]);
        }
        bubbleSort2(arr,N);
    }

    private static void bubbleSort(int[] arr, int n) {
        boolean flag = true;
        int exCount = 0;
        while (flag){
            flag = false;
            for (int i = n - 1; i > 0; i--){
                if (arr[i] < arr[i-1]){
                    exch(arr,i,i-1);
                    exCount++;
                    flag = true;
                }
            }
        }
        String str = "";
        for (int i = 0; i < arr.length; i++){
            str += arr[i] + " ";
        }
        System.out.println(str.substring(0,str.length()-1));
        System.out.println(exCount);
    }
    
    private static void bubbleSort2(int[] arr, int n){
        int exCount = 0;
        for (int i = 0; i < n; i++){
            for (int j = n-1; j>i; j--){
                if (arr[j] < arr[j-1]){
                    exch(arr,j,j-1);
                    exCount++;
                }
            }
        }
        String str = "";
        for (int i = 0; i < arr.length; i++){
            str += arr[i] + " ";
        }
        System.out.println(str.substring(0,str.length()-1));
        System.out.println(exCount);
    }

    private static void exch(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 Reader = new BufferedReader (new InputStreamReader(System.in));
        int N = Integer.parseInt(Reader.readLine());
        String A[] = Reader.readLine().split(" ",0);
        int[] arr = new int[N];
        for (int i=0; i < N; i++){
            arr[i] = Integer.parseInt(A[i]);
        }
        bubbleSort2(arr,N);
    }

    private static void bubbleSort(int[] arr, int n) {
        boolean flag = true;
        int exCount = 0;
        while (flag){
            flag = false;
            for (int i = n - 1; i > 0; i--){
                if (arr[i] < arr[i-1]){
                    exch(arr,i,i-1);
                    exCount++;
                    flag = true;
                }
            }
        }
        String str = "";
        for (int i = 0; i < arr.length; i++){
            str += arr[i] + " ";
        }
        System.out.println(str.substring(0,str.length()-1));
        System.out.println(exCount);
    }
    
    private static void bubbleSort2(int[] arr, int n){
        int exCount = 0;
        for (int i = 0; i < n; i++){
            for (int j = n-1; j>i; j--){
                if (arr[j] < arr[j-1]){
                    exch(arr,j,j-1);
                    exCount++;
                }
            }
        }
        String str = "";
        for (int i = 0; i < arr.length; i++){
            str += arr[i] + " ";
        }
        System.out.println(str.substring(0,str.length()-1));
        System.out.println(exCount);
    }

    private static void exch(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.IOException;
import java.io.InputStreamReader;

/**
 * Created by hwzheng on 2017/3/27.
 */
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader Reader = new BufferedReader (new InputStreamReader(System.in));
        int N = Integer.parseInt(Reader.readLine());
        String A[] = Reader.readLine().split(" ",0);
        int[] arr = new int[N];
        for (int i=0; i < N; i++){
            arr[i] = Integer.parseInt(A[i]);
        }
        bubbleSort2(arr,N);
    }

    private static void bubbleSort(int[] arr, int n) {
        boolean flag = true;
        int exCount = 0;
        while (flag){
            flag = false;
            for (int i = n - 1; i > 0; i--){
                if (arr[i] < arr[i-1]){
                    exch(arr,i,i-1);
                    exCount++;
                    flag = true;
                }
            }
        }
        String str = "";
        for (int i = 0; i < arr.length; i++){
            str += arr[i] + " ";
        }
        System.out.println(str.substring(0,str.length()-1));
        System.out.println(exCount);
    }
    
    private static void bubbleSort2(int[] arr, int n){
        int exCount = 0;
        for (int i = 0; i < n; i++){
            for (int j = n-1; j>i; j--){
                if (arr[j] < arr[j-1]){
                    exch(arr,j,j-1);
                    exCount++;
                }
            }
        }
        String str = "";
        for (int i = 0; i < arr.length; i++){
            str += arr[i] + " ";
        }
        System.out.println(str.substring(0,str.length()-1));
        System.out.println(exCount);
    }

    private static void exch(int[] arr, int i, int j){
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}