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

public class Main {
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            // int num = 5;
            // int[] arr = {5, 3, 2, 4, 1};
            
            int num = Integer.parseInt(br.readLine());
            String[] temp = br.readLine().split("\\s");
            int[] arr = new int[num];
            
            for(int i=0; i<num; i++) {
                arr[i] = Integer.parseInt(temp[i]);
            }
            
            Sort sort = bubbleSort(num, arr);
            
            String[] output = new String[num];
            
            for(int i=0; i<num; i++){
                output[i] = String.valueOf(sort.array[i]);
            }
            System.out.println(String.valueOf(String.join(" ", output)));
            System.out.println(String.valueOf(sort.count));
            
            
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
    
    static Sort bubbleSort(int num, int[] arr) {
        int count = 0;
        for(int i=0; i<num; i++) {
            for(int j=num-1; i<j; j--){
                if(arr[j] < arr[j-1]) {
                    int tmp = arr[j];
                    arr[j]  = arr[j-1];
                    arr[j-1] =  tmp;
                    count += 1;
                }
            }
        }
        return new Sort(count, arr);
    }
}

class Sort{
    public int count;
    public int[] array;
    
    public Sort(int count, int[] array){
        this.count = count;
        this.array = array;
    }
} 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) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            // int num = 5;
            // int[] arr = {5, 3, 2, 4, 1};
            
            int num = Integer.parseInt(br.readLine());
            String[] temp = br.readLine().split("\\s");
            int[] arr = new int[num];
            
            for(int i=0; i<num; i++) {
                arr[i] = Integer.parseInt(temp[i]);
            }
            
            Sort sort = bubbleSort(num, arr);
            
            String[] output = new String[num];
            
            for(int i=0; i<num; i++){
                output[i] = String.valueOf(sort.array[i]);
            }
            System.out.println(String.valueOf(String.join(" ", output)));
            System.out.println(String.valueOf(sort.count));
            
            
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
    
    static Sort bubbleSort(int num, int[] arr) {
        int count = 0;
        for(int i=0; i<num; i++) {
            for(int j=num-1; i<j; j--){
                if(arr[j] < arr[j-1]) {
                    int tmp = arr[j];
                    arr[j]  = arr[j-1];
                    arr[j-1] =  tmp;
                    count += 1;
                }
            }
        }
        return new Sort(count, arr);
    }
} Main public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            // int num = 5;
            // int[] arr = {5, 3, 2, 4, 1};
            
            int num = Integer.parseInt(br.readLine());
            String[] temp = br.readLine().split("\\s");
            int[] arr = new int[num];
            
            for(int i=0; i<num; i++) {
                arr[i] = Integer.parseInt(temp[i]);
            }
            
            Sort sort = bubbleSort(num, arr);
            
            String[] output = new String[num];
            
            for(int i=0; i<num; i++){
                output[i] = String.valueOf(sort.array[i]);
            }
            System.out.println(String.valueOf(String.join(" ", output)));
            System.out.println(String.valueOf(sort.count));
            
            
        } catch(IOException e) {
            e.printStackTrace();
        }
    } main {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            // int num = 5;
            // int[] arr = {5, 3, 2, 4, 1};
            
            int num = Integer.parseInt(br.readLine());
            String[] temp = br.readLine().split("\\s");
            int[] arr = new int[num];
            
            for(int i=0; i<num; i++) {
                arr[i] = Integer.parseInt(temp[i]);
            }
            
            Sort sort = bubbleSort(num, arr);
            
            String[] output = new String[num];
            
            for(int i=0; i<num; i++){
                output[i] = String.valueOf(sort.array[i]);
            }
            System.out.println(String.valueOf(String.join(" ", output)));
            System.out.println(String.valueOf(sort.count));
            
            
        } catch(IOException e) {
            e.printStackTrace();
        }
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) try {
            // int num = 5;
            // int[] arr = {5, 3, 2, 4, 1};
            
            int num = Integer.parseInt(br.readLine());
            String[] temp = br.readLine().split("\\s");
            int[] arr = new int[num];
            
            for(int i=0; i<num; i++) {
                arr[i] = Integer.parseInt(temp[i]);
            }
            
            Sort sort = bubbleSort(num, arr);
            
            String[] output = new String[num];
            
            for(int i=0; i<num; i++){
                output[i] = String.valueOf(sort.array[i]);
            }
            System.out.println(String.valueOf(String.join(" ", output)));
            System.out.println(String.valueOf(sort.count));
            
            
        } catch(IOException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        } IOException e {
            e.printStackTrace();
        } e.printStackTrace() e.printStackTrace e {
            // int num = 5;
            // int[] arr = {5, 3, 2, 4, 1};
            
            int num = Integer.parseInt(br.readLine());
            String[] temp = br.readLine().split("\\s");
            int[] arr = new int[num];
            
            for(int i=0; i<num; i++) {
                arr[i] = Integer.parseInt(temp[i]);
            }
            
            Sort sort = bubbleSort(num, arr);
            
            String[] output = new String[num];
            
            for(int i=0; i<num; i++){
                output[i] = String.valueOf(sort.array[i]);
            }
            System.out.println(String.valueOf(String.join(" ", output)));
            System.out.println(String.valueOf(sort.count));
            
            
        } int num = Integer.parseInt(br.readLine()); num Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br String[] temp = br.readLine().split("\\s"); temp br.readLine().split("\\s") br.readLine().split br.readLine() br.readLine br "\\s" int[] arr = new int[num]; arr new int[num] num for(int i=0; i<num; i++) {
                arr[i] = Integer.parseInt(temp[i]);
            } int i=0; int i=0; i 0 i<num i num i++ i++ i {
                arr[i] = Integer.parseInt(temp[i]);
            } {
                arr[i] = Integer.parseInt(temp[i]);
            } arr[i] = Integer.parseInt(temp[i]) arr[i] arr i Integer.parseInt(temp[i]) Integer.parseInt Integer temp[i] temp i Sort sort = bubbleSort(num, arr); sort bubbleSort(num, arr) bubbleSort num arr String[] output = new String[num]; output new String[num] num for(int i=0; i<num; i++){
                output[i] = String.valueOf(sort.array[i]);
            } int i=0; int i=0; i 0 i<num i num i++ i++ i {
                output[i] = String.valueOf(sort.array[i]);
            } {
                output[i] = String.valueOf(sort.array[i]);
            } output[i] = String.valueOf(sort.array[i]) output[i] output i String.valueOf(sort.array[i]) String.valueOf String sort.array[i] sort.array sort sort.array i System.out.println(String.valueOf(String.join(" ", output))) System.out.println System.out System System.out String.valueOf(String.join(" ", output)) String.valueOf String String.join(" ", output) String.join String " " output System.out.println(String.valueOf(sort.count)) System.out.println System.out System System.out String.valueOf(sort.count) String.valueOf String sort.count sort sort.count String[] args args static Sort bubbleSort(int num, int[] arr) {
        int count = 0;
        for(int i=0; i<num; i++) {
            for(int j=num-1; i<j; j--){
                if(arr[j] < arr[j-1]) {
                    int tmp = arr[j];
                    arr[j]  = arr[j-1];
                    arr[j-1] =  tmp;
                    count += 1;
                }
            }
        }
        return new Sort(count, arr);
    } bubbleSort {
        int count = 0;
        for(int i=0; i<num; i++) {
            for(int j=num-1; i<j; j--){
                if(arr[j] < arr[j-1]) {
                    int tmp = arr[j];
                    arr[j]  = arr[j-1];
                    arr[j-1] =  tmp;
                    count += 1;
                }
            }
        }
        return new Sort(count, arr);
    } int count = 0; count 0 for(int i=0; i<num; i++) {
            for(int j=num-1; i<j; j--){
                if(arr[j] < arr[j-1]) {
                    int tmp = arr[j];
                    arr[j]  = arr[j-1];
                    arr[j-1] =  tmp;
                    count += 1;
                }
            }
        } int i=0; int i=0; i 0 i<num i num i++ i++ i {
            for(int j=num-1; i<j; j--){
                if(arr[j] < arr[j-1]) {
                    int tmp = arr[j];
                    arr[j]  = arr[j-1];
                    arr[j-1] =  tmp;
                    count += 1;
                }
            }
        } {
            for(int j=num-1; i<j; j--){
                if(arr[j] < arr[j-1]) {
                    int tmp = arr[j];
                    arr[j]  = arr[j-1];
                    arr[j-1] =  tmp;
                    count += 1;
                }
            }
        } for(int j=num-1; i<j; j--){
                if(arr[j] < arr[j-1]) {
                    int tmp = arr[j];
                    arr[j]  = arr[j-1];
                    arr[j-1] =  tmp;
                    count += 1;
                }
            } int j=num-1; int j=num-1; j num-1 num 1 i<j i j j-- j-- j {
                if(arr[j] < arr[j-1]) {
                    int tmp = arr[j];
                    arr[j]  = arr[j-1];
                    arr[j-1] =  tmp;
                    count += 1;
                }
            } {
                if(arr[j] < arr[j-1]) {
                    int tmp = arr[j];
                    arr[j]  = arr[j-1];
                    arr[j-1] =  tmp;
                    count += 1;
                }
            } if(arr[j] < arr[j-1]) {
                    int tmp = arr[j];
                    arr[j]  = arr[j-1];
                    arr[j-1] =  tmp;
                    count += 1;
                } arr[j] < arr[j-1] arr[j] arr j arr[j-1] arr j-1 j 1 {
                    int tmp = arr[j];
                    arr[j]  = arr[j-1];
                    arr[j-1] =  tmp;
                    count += 1;
                } int tmp = arr[j]; tmp arr[j] arr j arr[j]  = arr[j-1] arr[j] arr j arr[j-1] arr j-1 j 1 arr[j-1] =  tmp arr[j-1] arr j-1 j 1 tmp count += 1 count 1 return new Sort(count, arr); new Sort(count, arr) int num num int[] arr arr class Sort{
    public int count;
    public int[] array;
    
    public Sort(int count, int[] array){
        this.count = count;
        this.array = array;
    }
} Sort public int count; count public int[] array; array public Sort(int count, int[] array){
        this.count = count;
        this.array = array;
    } Sort {
        this.count = count;
        this.array = array;
    } this.count = count this.count this this.count count this.array = array this.array this this.array array int count count int[] array array public class Main {
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            // int num = 5;
            // int[] arr = {5, 3, 2, 4, 1};
            
            int num = Integer.parseInt(br.readLine());
            String[] temp = br.readLine().split("\\s");
            int[] arr = new int[num];
            
            for(int i=0; i<num; i++) {
                arr[i] = Integer.parseInt(temp[i]);
            }
            
            Sort sort = bubbleSort(num, arr);
            
            String[] output = new String[num];
            
            for(int i=0; i<num; i++){
                output[i] = String.valueOf(sort.array[i]);
            }
            System.out.println(String.valueOf(String.join(" ", output)));
            System.out.println(String.valueOf(sort.count));
            
            
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
    
    static Sort bubbleSort(int num, int[] arr) {
        int count = 0;
        for(int i=0; i<num; i++) {
            for(int j=num-1; i<j; j--){
                if(arr[j] < arr[j-1]) {
                    int tmp = arr[j];
                    arr[j]  = arr[j-1];
                    arr[j-1] =  tmp;
                    count += 1;
                }
            }
        }
        return new Sort(count, arr);
    }
} public class Main {
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            // int num = 5;
            // int[] arr = {5, 3, 2, 4, 1};
            
            int num = Integer.parseInt(br.readLine());
            String[] temp = br.readLine().split("\\s");
            int[] arr = new int[num];
            
            for(int i=0; i<num; i++) {
                arr[i] = Integer.parseInt(temp[i]);
            }
            
            Sort sort = bubbleSort(num, arr);
            
            String[] output = new String[num];
            
            for(int i=0; i<num; i++){
                output[i] = String.valueOf(sort.array[i]);
            }
            System.out.println(String.valueOf(String.join(" ", output)));
            System.out.println(String.valueOf(sort.count));
            
            
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
    
    static Sort bubbleSort(int num, int[] arr) {
        int count = 0;
        for(int i=0; i<num; i++) {
            for(int j=num-1; i<j; j--){
                if(arr[j] < arr[j-1]) {
                    int tmp = arr[j];
                    arr[j]  = arr[j-1];
                    arr[j-1] =  tmp;
                    count += 1;
                }
            }
        }
        return new Sort(count, arr);
    }
} Main
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            // int num = 5;
            // int[] arr = {5, 3, 2, 4, 1};
            
            int num = Integer.parseInt(br.readLine());
            String[] temp = br.readLine().split("\\s");
            int[] arr = new int[num];
            
            for(int i=0; i<num; i++) {
                arr[i] = Integer.parseInt(temp[i]);
            }
            
            Sort sort = bubbleSort(num, arr);
            
            String[] output = new String[num];
            
            for(int i=0; i<num; i++){
                output[i] = String.valueOf(sort.array[i]);
            }
            System.out.println(String.valueOf(String.join(" ", output)));
            System.out.println(String.valueOf(sort.count));
            
            
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
    
    static Sort bubbleSort(int num, int[] arr) {
        int count = 0;
        for(int i=0; i<num; i++) {
            for(int j=num-1; i<j; j--){
                if(arr[j] < arr[j-1]) {
                    int tmp = arr[j];
                    arr[j]  = arr[j-1];
                    arr[j-1] =  tmp;
                    count += 1;
                }
            }
        }
        return new Sort(count, arr);
    }
}

class Sort{
    public int count;
    public int[] array;
    
    public Sort(int count, int[] array){
        this.count = count;
        this.array = array;
    }
}
