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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = ip(br.readLine());
        int[] list = createListInt(br.readLine());
        bubbleSort(list.clone(), n);
    }

    static void checkStable(Card[] list1, Card[] list2) {
        for (int i = 0; i < list1.length; i++) {
            if (list1[i].serialN != list2[i].serialN) {
                System.out.println("Not stable");
                return;
            }
        }
        System.out.println("Stable");
    }

    static void bubbleSort(int[] list, int n) {
        int count = 0;
        boolean flag = true;
        while (flag) {
            flag = false;
            for (int i = n - 1; i > 0; i--) {
                if (list[i] < list[i - 1]) {
                    swap(list, i, i - 1);
                    count++;
                    flag = true;
                }
            }
        }
        print(list);
        System.out.println(count);
    }

    private static void print(int[] list) {
        int n = list.length;
        String[] str = new String[n];
        for (int i = 0; i < n; i++) {
            str[i] = String.valueOf(list[i]);
        }
        System.out.println(String.join(" ", str));
    }

    static void swap(Card[] list, int a, int b) {
        Card temp = list[a];
        list[a] = list[b];
        list[b] = temp;
    }

    static void swap(int[] list, int a, int b) {
        int temp = list[a];
        list[a] = list[b];
        list[b] = temp;
    }

    static int[] createListInt(String line) {
        String[] lines = line.split(" ");
        int n = lines.length;
        int[] list = new int[n];
        while (n-- > 0) {
            list[n] = Integer.parseInt(lines[n]);
        }
        return list;
    }

    static long[] createListLong(String line) {
        String[] lines = line.split(" ");
        int n = lines.length;
        long[] list = new long[n];
        while (n-- > 0) {
            list[n] = Long.parseLong(lines[n]);
        }
        return list;
    }

    static int ip(String s) {
        return Integer.parseInt(s);
    }

    static long lp(String s) {
        return Long.parseLong(s);
    }
}

class Card {
    static int n = 0;
    int serialN;
    char suit;
    int rank;

    Card(char suit, int rank) {
        serialN = n++;
        this.suit = suit;
        this.rank = rank;
    }

} 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 br = new BufferedReader(new InputStreamReader(System.in));
        int n = ip(br.readLine());
        int[] list = createListInt(br.readLine());
        bubbleSort(list.clone(), n);
    }

    static void checkStable(Card[] list1, Card[] list2) {
        for (int i = 0; i < list1.length; i++) {
            if (list1[i].serialN != list2[i].serialN) {
                System.out.println("Not stable");
                return;
            }
        }
        System.out.println("Stable");
    }

    static void bubbleSort(int[] list, int n) {
        int count = 0;
        boolean flag = true;
        while (flag) {
            flag = false;
            for (int i = n - 1; i > 0; i--) {
                if (list[i] < list[i - 1]) {
                    swap(list, i, i - 1);
                    count++;
                    flag = true;
                }
            }
        }
        print(list);
        System.out.println(count);
    }

    private static void print(int[] list) {
        int n = list.length;
        String[] str = new String[n];
        for (int i = 0; i < n; i++) {
            str[i] = String.valueOf(list[i]);
        }
        System.out.println(String.join(" ", str));
    }

    static void swap(Card[] list, int a, int b) {
        Card temp = list[a];
        list[a] = list[b];
        list[b] = temp;
    }

    static void swap(int[] list, int a, int b) {
        int temp = list[a];
        list[a] = list[b];
        list[b] = temp;
    }

    static int[] createListInt(String line) {
        String[] lines = line.split(" ");
        int n = lines.length;
        int[] list = new int[n];
        while (n-- > 0) {
            list[n] = Integer.parseInt(lines[n]);
        }
        return list;
    }

    static long[] createListLong(String line) {
        String[] lines = line.split(" ");
        int n = lines.length;
        long[] list = new long[n];
        while (n-- > 0) {
            list[n] = Long.parseLong(lines[n]);
        }
        return list;
    }

    static int ip(String s) {
        return Integer.parseInt(s);
    }

    static long lp(String s) {
        return Long.parseLong(s);
    }
} Main public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = ip(br.readLine());
        int[] list = createListInt(br.readLine());
        bubbleSort(list.clone(), n);
    } main {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = ip(br.readLine());
        int[] list = createListInt(br.readLine());
        bubbleSort(list.clone(), n);
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int n = ip(br.readLine()); n ip(br.readLine()) ip br.readLine() br.readLine br int[] list = createListInt(br.readLine()); list createListInt(br.readLine()) createListInt br.readLine() br.readLine br bubbleSort(list.clone(), n) bubbleSort list.clone() list.clone list n String[] args args static void checkStable(Card[] list1, Card[] list2) {
        for (int i = 0; i < list1.length; i++) {
            if (list1[i].serialN != list2[i].serialN) {
                System.out.println("Not stable");
                return;
            }
        }
        System.out.println("Stable");
    } checkStable {
        for (int i = 0; i < list1.length; i++) {
            if (list1[i].serialN != list2[i].serialN) {
                System.out.println("Not stable");
                return;
            }
        }
        System.out.println("Stable");
    } for (int i = 0; i < list1.length; i++) {
            if (list1[i].serialN != list2[i].serialN) {
                System.out.println("Not stable");
                return;
            }
        } int i = 0; int i = 0; i 0 i < list1.length i list1.length list1 list1.length i++ i++ i {
            if (list1[i].serialN != list2[i].serialN) {
                System.out.println("Not stable");
                return;
            }
        } {
            if (list1[i].serialN != list2[i].serialN) {
                System.out.println("Not stable");
                return;
            }
        } if (list1[i].serialN != list2[i].serialN) {
                System.out.println("Not stable");
                return;
            } list1[i].serialN != list2[i].serialN list1[i].serialN list1[i] list1 i list1[i].serialN list2[i].serialN list2[i] list2 i list2[i].serialN {
                System.out.println("Not stable");
                return;
            } System.out.println("Not stable") System.out.println System.out System System.out "Not stable" return; System.out.println("Stable") System.out.println System.out System System.out "Stable" Card[] list1 list1 Card[] list2 list2 static void bubbleSort(int[] list, int n) {
        int count = 0;
        boolean flag = true;
        while (flag) {
            flag = false;
            for (int i = n - 1; i > 0; i--) {
                if (list[i] < list[i - 1]) {
                    swap(list, i, i - 1);
                    count++;
                    flag = true;
                }
            }
        }
        print(list);
        System.out.println(count);
    } bubbleSort {
        int count = 0;
        boolean flag = true;
        while (flag) {
            flag = false;
            for (int i = n - 1; i > 0; i--) {
                if (list[i] < list[i - 1]) {
                    swap(list, i, i - 1);
                    count++;
                    flag = true;
                }
            }
        }
        print(list);
        System.out.println(count);
    } int count = 0; count 0 boolean flag = true; flag true while (flag) {
            flag = false;
            for (int i = n - 1; i > 0; i--) {
                if (list[i] < list[i - 1]) {
                    swap(list, i, i - 1);
                    count++;
                    flag = true;
                }
            }
        } flag {
            flag = false;
            for (int i = n - 1; i > 0; i--) {
                if (list[i] < list[i - 1]) {
                    swap(list, i, i - 1);
                    count++;
                    flag = true;
                }
            }
        } flag = false flag false for (int i = n - 1; i > 0; i--) {
                if (list[i] < list[i - 1]) {
                    swap(list, i, i - 1);
                    count++;
                    flag = true;
                }
            } int i = n - 1; int i = n - 1; i n - 1 n 1 i > 0 i 0 i-- i-- i {
                if (list[i] < list[i - 1]) {
                    swap(list, i, i - 1);
                    count++;
                    flag = true;
                }
            } {
                if (list[i] < list[i - 1]) {
                    swap(list, i, i - 1);
                    count++;
                    flag = true;
                }
            } if (list[i] < list[i - 1]) {
                    swap(list, i, i - 1);
                    count++;
                    flag = true;
                } list[i] < list[i - 1] list[i] list i list[i - 1] list i - 1 i 1 {
                    swap(list, i, i - 1);
                    count++;
                    flag = true;
                } swap(list, i, i - 1) swap list i i - 1 i 1 count++ count flag = true flag true print(list) print list System.out.println(count) System.out.println System.out System System.out count int[] list list int n n private static void print(int[] list) {
        int n = list.length;
        String[] str = new String[n];
        for (int i = 0; i < n; i++) {
            str[i] = String.valueOf(list[i]);
        }
        System.out.println(String.join(" ", str));
    } print {
        int n = list.length;
        String[] str = new String[n];
        for (int i = 0; i < n; i++) {
            str[i] = String.valueOf(list[i]);
        }
        System.out.println(String.join(" ", str));
    } int n = list.length; n list.length list list.length String[] str = new String[n]; str new String[n] n for (int i = 0; i < n; i++) {
            str[i] = String.valueOf(list[i]);
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            str[i] = String.valueOf(list[i]);
        } {
            str[i] = String.valueOf(list[i]);
        } str[i] = String.valueOf(list[i]) str[i] str i String.valueOf(list[i]) String.valueOf String list[i] list i System.out.println(String.join(" ", str)) System.out.println System.out System System.out String.join(" ", str) String.join String " " str int[] list list static void swap(Card[] list, int a, int b) {
        Card temp = list[a];
        list[a] = list[b];
        list[b] = temp;
    } swap {
        Card temp = list[a];
        list[a] = list[b];
        list[b] = temp;
    } Card temp = list[a]; temp list[a] list a list[a] = list[b] list[a] list a list[b] list b list[b] = temp list[b] list b temp Card[] list list int a a int b b static void swap(int[] list, int a, int b) {
        int temp = list[a];
        list[a] = list[b];
        list[b] = temp;
    } swap {
        int temp = list[a];
        list[a] = list[b];
        list[b] = temp;
    } int temp = list[a]; temp list[a] list a list[a] = list[b] list[a] list a list[b] list b list[b] = temp list[b] list b temp int[] list list int a a int b b static int[] createListInt(String line) {
        String[] lines = line.split(" ");
        int n = lines.length;
        int[] list = new int[n];
        while (n-- > 0) {
            list[n] = Integer.parseInt(lines[n]);
        }
        return list;
    } createListInt {
        String[] lines = line.split(" ");
        int n = lines.length;
        int[] list = new int[n];
        while (n-- > 0) {
            list[n] = Integer.parseInt(lines[n]);
        }
        return list;
    } String[] lines = line.split(" "); lines line.split(" ") line.split line " " int n = lines.length; n lines.length lines lines.length int[] list = new int[n]; list new int[n] n while (n-- > 0) {
            list[n] = Integer.parseInt(lines[n]);
        } n-- > 0 n-- n 0 {
            list[n] = Integer.parseInt(lines[n]);
        } list[n] = Integer.parseInt(lines[n]) list[n] list n Integer.parseInt(lines[n]) Integer.parseInt Integer lines[n] lines n return list; list String line line static long[] createListLong(String line) {
        String[] lines = line.split(" ");
        int n = lines.length;
        long[] list = new long[n];
        while (n-- > 0) {
            list[n] = Long.parseLong(lines[n]);
        }
        return list;
    } createListLong {
        String[] lines = line.split(" ");
        int n = lines.length;
        long[] list = new long[n];
        while (n-- > 0) {
            list[n] = Long.parseLong(lines[n]);
        }
        return list;
    } String[] lines = line.split(" "); lines line.split(" ") line.split line " " int n = lines.length; n lines.length lines lines.length long[] list = new long[n]; list new long[n] n while (n-- > 0) {
            list[n] = Long.parseLong(lines[n]);
        } n-- > 0 n-- n 0 {
            list[n] = Long.parseLong(lines[n]);
        } list[n] = Long.parseLong(lines[n]) list[n] list n Long.parseLong(lines[n]) Long.parseLong Long lines[n] lines n return list; list String line line static int ip(String s) {
        return Integer.parseInt(s);
    } ip {
        return Integer.parseInt(s);
    } return Integer.parseInt(s); Integer.parseInt(s) Integer.parseInt Integer s String s s static long lp(String s) {
        return Long.parseLong(s);
    } lp {
        return Long.parseLong(s);
    } return Long.parseLong(s); Long.parseLong(s) Long.parseLong Long s String s s class Card {
    static int n = 0;
    int serialN;
    char suit;
    int rank;

    Card(char suit, int rank) {
        serialN = n++;
        this.suit = suit;
        this.rank = rank;
    }

} Card static int n = 0; n 0 int serialN; serialN char suit; suit int rank; rank Card(char suit, int rank) {
        serialN = n++;
        this.suit = suit;
        this.rank = rank;
    } Card {
        serialN = n++;
        this.suit = suit;
        this.rank = rank;
    } serialN = n++ serialN n++ n this.suit = suit this.suit this this.suit suit this.rank = rank this.rank this this.rank rank char suit suit int rank rank public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = ip(br.readLine());
        int[] list = createListInt(br.readLine());
        bubbleSort(list.clone(), n);
    }

    static void checkStable(Card[] list1, Card[] list2) {
        for (int i = 0; i < list1.length; i++) {
            if (list1[i].serialN != list2[i].serialN) {
                System.out.println("Not stable");
                return;
            }
        }
        System.out.println("Stable");
    }

    static void bubbleSort(int[] list, int n) {
        int count = 0;
        boolean flag = true;
        while (flag) {
            flag = false;
            for (int i = n - 1; i > 0; i--) {
                if (list[i] < list[i - 1]) {
                    swap(list, i, i - 1);
                    count++;
                    flag = true;
                }
            }
        }
        print(list);
        System.out.println(count);
    }

    private static void print(int[] list) {
        int n = list.length;
        String[] str = new String[n];
        for (int i = 0; i < n; i++) {
            str[i] = String.valueOf(list[i]);
        }
        System.out.println(String.join(" ", str));
    }

    static void swap(Card[] list, int a, int b) {
        Card temp = list[a];
        list[a] = list[b];
        list[b] = temp;
    }

    static void swap(int[] list, int a, int b) {
        int temp = list[a];
        list[a] = list[b];
        list[b] = temp;
    }

    static int[] createListInt(String line) {
        String[] lines = line.split(" ");
        int n = lines.length;
        int[] list = new int[n];
        while (n-- > 0) {
            list[n] = Integer.parseInt(lines[n]);
        }
        return list;
    }

    static long[] createListLong(String line) {
        String[] lines = line.split(" ");
        int n = lines.length;
        long[] list = new long[n];
        while (n-- > 0) {
            list[n] = Long.parseLong(lines[n]);
        }
        return list;
    }

    static int ip(String s) {
        return Integer.parseInt(s);
    }

    static long lp(String s) {
        return Long.parseLong(s);
    }
} public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = ip(br.readLine());
        int[] list = createListInt(br.readLine());
        bubbleSort(list.clone(), n);
    }

    static void checkStable(Card[] list1, Card[] list2) {
        for (int i = 0; i < list1.length; i++) {
            if (list1[i].serialN != list2[i].serialN) {
                System.out.println("Not stable");
                return;
            }
        }
        System.out.println("Stable");
    }

    static void bubbleSort(int[] list, int n) {
        int count = 0;
        boolean flag = true;
        while (flag) {
            flag = false;
            for (int i = n - 1; i > 0; i--) {
                if (list[i] < list[i - 1]) {
                    swap(list, i, i - 1);
                    count++;
                    flag = true;
                }
            }
        }
        print(list);
        System.out.println(count);
    }

    private static void print(int[] list) {
        int n = list.length;
        String[] str = new String[n];
        for (int i = 0; i < n; i++) {
            str[i] = String.valueOf(list[i]);
        }
        System.out.println(String.join(" ", str));
    }

    static void swap(Card[] list, int a, int b) {
        Card temp = list[a];
        list[a] = list[b];
        list[b] = temp;
    }

    static void swap(int[] list, int a, int b) {
        int temp = list[a];
        list[a] = list[b];
        list[b] = temp;
    }

    static int[] createListInt(String line) {
        String[] lines = line.split(" ");
        int n = lines.length;
        int[] list = new int[n];
        while (n-- > 0) {
            list[n] = Integer.parseInt(lines[n]);
        }
        return list;
    }

    static long[] createListLong(String line) {
        String[] lines = line.split(" ");
        int n = lines.length;
        long[] list = new long[n];
        while (n-- > 0) {
            list[n] = Long.parseLong(lines[n]);
        }
        return list;
    }

    static int ip(String s) {
        return Integer.parseInt(s);
    }

    static long lp(String s) {
        return Long.parseLong(s);
    }
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = ip(br.readLine());
        int[] list = createListInt(br.readLine());
        bubbleSort(list.clone(), n);
    }

    static void checkStable(Card[] list1, Card[] list2) {
        for (int i = 0; i < list1.length; i++) {
            if (list1[i].serialN != list2[i].serialN) {
                System.out.println("Not stable");
                return;
            }
        }
        System.out.println("Stable");
    }

    static void bubbleSort(int[] list, int n) {
        int count = 0;
        boolean flag = true;
        while (flag) {
            flag = false;
            for (int i = n - 1; i > 0; i--) {
                if (list[i] < list[i - 1]) {
                    swap(list, i, i - 1);
                    count++;
                    flag = true;
                }
            }
        }
        print(list);
        System.out.println(count);
    }

    private static void print(int[] list) {
        int n = list.length;
        String[] str = new String[n];
        for (int i = 0; i < n; i++) {
            str[i] = String.valueOf(list[i]);
        }
        System.out.println(String.join(" ", str));
    }

    static void swap(Card[] list, int a, int b) {
        Card temp = list[a];
        list[a] = list[b];
        list[b] = temp;
    }

    static void swap(int[] list, int a, int b) {
        int temp = list[a];
        list[a] = list[b];
        list[b] = temp;
    }

    static int[] createListInt(String line) {
        String[] lines = line.split(" ");
        int n = lines.length;
        int[] list = new int[n];
        while (n-- > 0) {
            list[n] = Integer.parseInt(lines[n]);
        }
        return list;
    }

    static long[] createListLong(String line) {
        String[] lines = line.split(" ");
        int n = lines.length;
        long[] list = new long[n];
        while (n-- > 0) {
            list[n] = Long.parseLong(lines[n]);
        }
        return list;
    }

    static int ip(String s) {
        return Integer.parseInt(s);
    }

    static long lp(String s) {
        return Long.parseLong(s);
    }
}

class Card {
    static int n = 0;
    int serialN;
    char suit;
    int rank;

    Card(char suit, int rank) {
        serialN = n++;
        this.suit = suit;
        this.rank = rank;
    }

}