import java.util.Arrays;
import java.util.Scanner;


public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) nums[i] = scanner.nextInt();

        bubbleSort(n, nums);
    }

    private static void bubbleSort(int n, int[] a) {
    	boolean flg = true;
    	int i = 0;
    	int sw = 0;
    	while (flg) {
    		flg = false;
    		for (int j = n - 1; j > i; j--) {
    			if (a[j] < a[j-1]) {
    				int w = a[j];
    				a[j] = a[j-1];
    				a[j-1] = w;
    				sw++;
    				flg = true;
    			}
    		}
    		i++;
    	}
    	trace(a);
    	System.out.println(sw);
    }

    private static void trace(int[] nums) {
    	System.out.println(Arrays.toString(nums).replaceAll("[\\[\\],]", ""));
    }

} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) nums[i] = scanner.nextInt();

        bubbleSort(n, nums);
    }

    private static void bubbleSort(int n, int[] a) {
    	boolean flg = true;
    	int i = 0;
    	int sw = 0;
    	while (flg) {
    		flg = false;
    		for (int j = n - 1; j > i; j--) {
    			if (a[j] < a[j-1]) {
    				int w = a[j];
    				a[j] = a[j-1];
    				a[j-1] = w;
    				sw++;
    				flg = true;
    			}
    		}
    		i++;
    	}
    	trace(a);
    	System.out.println(sw);
    }

    private static void trace(int[] nums) {
    	System.out.println(Arrays.toString(nums).replaceAll("[\\[\\],]", ""));
    }

} Main public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) nums[i] = scanner.nextInt();

        bubbleSort(n, nums);
    } main {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) nums[i] = scanner.nextInt();

        bubbleSort(n, nums);
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int n = scanner.nextInt(); n scanner.nextInt() scanner.nextInt scanner int[] nums = new int[n]; nums new int[n] n for (int i = 0; i < n; i++) nums[i] = scanner.nextInt(); int i = 0; int i = 0; i 0 i < n i n i++ i++ i nums[i] = scanner.nextInt(); nums[i] = scanner.nextInt() nums[i] nums i scanner.nextInt() scanner.nextInt scanner bubbleSort(n, nums) bubbleSort n nums String[] args args private static void bubbleSort(int n, int[] a) {
    	boolean flg = true;
    	int i = 0;
    	int sw = 0;
    	while (flg) {
    		flg = false;
    		for (int j = n - 1; j > i; j--) {
    			if (a[j] < a[j-1]) {
    				int w = a[j];
    				a[j] = a[j-1];
    				a[j-1] = w;
    				sw++;
    				flg = true;
    			}
    		}
    		i++;
    	}
    	trace(a);
    	System.out.println(sw);
    } bubbleSort {
    	boolean flg = true;
    	int i = 0;
    	int sw = 0;
    	while (flg) {
    		flg = false;
    		for (int j = n - 1; j > i; j--) {
    			if (a[j] < a[j-1]) {
    				int w = a[j];
    				a[j] = a[j-1];
    				a[j-1] = w;
    				sw++;
    				flg = true;
    			}
    		}
    		i++;
    	}
    	trace(a);
    	System.out.println(sw);
    } boolean flg = true; flg true int i = 0; i 0 int sw = 0; sw 0 while (flg) {
    		flg = false;
    		for (int j = n - 1; j > i; j--) {
    			if (a[j] < a[j-1]) {
    				int w = a[j];
    				a[j] = a[j-1];
    				a[j-1] = w;
    				sw++;
    				flg = true;
    			}
    		}
    		i++;
    	} flg {
    		flg = false;
    		for (int j = n - 1; j > i; j--) {
    			if (a[j] < a[j-1]) {
    				int w = a[j];
    				a[j] = a[j-1];
    				a[j-1] = w;
    				sw++;
    				flg = true;
    			}
    		}
    		i++;
    	} flg = false flg false for (int j = n - 1; j > i; j--) {
    			if (a[j] < a[j-1]) {
    				int w = a[j];
    				a[j] = a[j-1];
    				a[j-1] = w;
    				sw++;
    				flg = true;
    			}
    		} int j = n - 1; int j = n - 1; j n - 1 n 1 j > i j i j-- j-- j {
    			if (a[j] < a[j-1]) {
    				int w = a[j];
    				a[j] = a[j-1];
    				a[j-1] = w;
    				sw++;
    				flg = true;
    			}
    		} {
    			if (a[j] < a[j-1]) {
    				int w = a[j];
    				a[j] = a[j-1];
    				a[j-1] = w;
    				sw++;
    				flg = true;
    			}
    		} if (a[j] < a[j-1]) {
    				int w = a[j];
    				a[j] = a[j-1];
    				a[j-1] = w;
    				sw++;
    				flg = true;
    			} a[j] < a[j-1] a[j] a j a[j-1] a j-1 j 1 {
    				int w = a[j];
    				a[j] = a[j-1];
    				a[j-1] = w;
    				sw++;
    				flg = true;
    			} int w = a[j]; w a[j] a j a[j] = a[j-1] a[j] a j a[j-1] a j-1 j 1 a[j-1] = w a[j-1] a j-1 j 1 w sw++ sw flg = true flg true i++ i trace(a) trace a System.out.println(sw) System.out.println System.out System System.out sw int n n int[] a a private static void trace(int[] nums) {
    	System.out.println(Arrays.toString(nums).replaceAll("[\\[\\],]", ""));
    } trace {
    	System.out.println(Arrays.toString(nums).replaceAll("[\\[\\],]", ""));
    } System.out.println(Arrays.toString(nums).replaceAll("[\\[\\],]", "")) System.out.println System.out System System.out Arrays.toString(nums).replaceAll("[\\[\\],]", "") Arrays.toString(nums).replaceAll Arrays.toString(nums) Arrays.toString Arrays nums "[\\[\\],]" "" int[] nums nums public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) nums[i] = scanner.nextInt();

        bubbleSort(n, nums);
    }

    private static void bubbleSort(int n, int[] a) {
    	boolean flg = true;
    	int i = 0;
    	int sw = 0;
    	while (flg) {
    		flg = false;
    		for (int j = n - 1; j > i; j--) {
    			if (a[j] < a[j-1]) {
    				int w = a[j];
    				a[j] = a[j-1];
    				a[j-1] = w;
    				sw++;
    				flg = true;
    			}
    		}
    		i++;
    	}
    	trace(a);
    	System.out.println(sw);
    }

    private static void trace(int[] nums) {
    	System.out.println(Arrays.toString(nums).replaceAll("[\\[\\],]", ""));
    }

} public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) nums[i] = scanner.nextInt();

        bubbleSort(n, nums);
    }

    private static void bubbleSort(int n, int[] a) {
    	boolean flg = true;
    	int i = 0;
    	int sw = 0;
    	while (flg) {
    		flg = false;
    		for (int j = n - 1; j > i; j--) {
    			if (a[j] < a[j-1]) {
    				int w = a[j];
    				a[j] = a[j-1];
    				a[j-1] = w;
    				sw++;
    				flg = true;
    			}
    		}
    		i++;
    	}
    	trace(a);
    	System.out.println(sw);
    }

    private static void trace(int[] nums) {
    	System.out.println(Arrays.toString(nums).replaceAll("[\\[\\],]", ""));
    }

} Main
import java.util.Arrays;
import java.util.Scanner;


public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int[] nums = new int[n];
        for (int i = 0; i < n; i++) nums[i] = scanner.nextInt();

        bubbleSort(n, nums);
    }

    private static void bubbleSort(int n, int[] a) {
    	boolean flg = true;
    	int i = 0;
    	int sw = 0;
    	while (flg) {
    		flg = false;
    		for (int j = n - 1; j > i; j--) {
    			if (a[j] < a[j-1]) {
    				int w = a[j];
    				a[j] = a[j-1];
    				a[j-1] = w;
    				sw++;
    				flg = true;
    			}
    		}
    		i++;
    	}
    	trace(a);
    	System.out.println(sw);
    }

    private static void trace(int[] nums) {
    	System.out.println(Arrays.toString(nums).replaceAll("[\\[\\],]", ""));
    }

}
