import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		for(int i = 0 ; i < n ; i++) {
			a[i] = sc.nextInt();
		}
		bubbleSort(a, n);
	}

	static int[] bubbleSort(int[] a, int n) {
		int flg = 1;
		int cnt = 0;
		while(flg == 1){
			flg = 0;
			for(int j = n - 1 ; j >= 1 ; j--) {
				if(a[j] < a[j - 1]) {
					int v = a[j];
					a[j] = a[j - 1];
					a[j - 1] = v;
					flg = 1;
					cnt++;
				}
			}
		}
		for(int i = 0 ; i < n ; i++) {
			if(i != n - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}
		}
		System.out.println(cnt);
		return a;
	}

} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		for(int i = 0 ; i < n ; i++) {
			a[i] = sc.nextInt();
		}
		bubbleSort(a, n);
	}

	static int[] bubbleSort(int[] a, int n) {
		int flg = 1;
		int cnt = 0;
		while(flg == 1){
			flg = 0;
			for(int j = n - 1 ; j >= 1 ; j--) {
				if(a[j] < a[j - 1]) {
					int v = a[j];
					a[j] = a[j - 1];
					a[j - 1] = v;
					flg = 1;
					cnt++;
				}
			}
		}
		for(int i = 0 ; i < n ; i++) {
			if(i != n - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}
		}
		System.out.println(cnt);
		return a;
	}

} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		for(int i = 0 ; i < n ; i++) {
			a[i] = sc.nextInt();
		}
		bubbleSort(a, n);
	} main {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		for(int i = 0 ; i < n ; i++) {
			a[i] = sc.nextInt();
		}
		bubbleSort(a, n);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] a = new int[n]; a new int[n] n for(int i = 0 ; i < n ; i++) {
			a[i] = sc.nextInt();
		} int i = 0 ; int i = 0 ; i 0 i < n i n i++ i++ i {
			a[i] = sc.nextInt();
		} {
			a[i] = sc.nextInt();
		} a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc bubbleSort(a, n) bubbleSort a n String[] args args static int[] bubbleSort(int[] a, int n) {
		int flg = 1;
		int cnt = 0;
		while(flg == 1){
			flg = 0;
			for(int j = n - 1 ; j >= 1 ; j--) {
				if(a[j] < a[j - 1]) {
					int v = a[j];
					a[j] = a[j - 1];
					a[j - 1] = v;
					flg = 1;
					cnt++;
				}
			}
		}
		for(int i = 0 ; i < n ; i++) {
			if(i != n - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}
		}
		System.out.println(cnt);
		return a;
	} bubbleSort {
		int flg = 1;
		int cnt = 0;
		while(flg == 1){
			flg = 0;
			for(int j = n - 1 ; j >= 1 ; j--) {
				if(a[j] < a[j - 1]) {
					int v = a[j];
					a[j] = a[j - 1];
					a[j - 1] = v;
					flg = 1;
					cnt++;
				}
			}
		}
		for(int i = 0 ; i < n ; i++) {
			if(i != n - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}
		}
		System.out.println(cnt);
		return a;
	} int flg = 1; flg 1 int cnt = 0; cnt 0 while(flg == 1){
			flg = 0;
			for(int j = n - 1 ; j >= 1 ; j--) {
				if(a[j] < a[j - 1]) {
					int v = a[j];
					a[j] = a[j - 1];
					a[j - 1] = v;
					flg = 1;
					cnt++;
				}
			}
		} flg == 1 flg 1 {
			flg = 0;
			for(int j = n - 1 ; j >= 1 ; j--) {
				if(a[j] < a[j - 1]) {
					int v = a[j];
					a[j] = a[j - 1];
					a[j - 1] = v;
					flg = 1;
					cnt++;
				}
			}
		} flg = 0 flg 0 for(int j = n - 1 ; j >= 1 ; j--) {
				if(a[j] < a[j - 1]) {
					int v = a[j];
					a[j] = a[j - 1];
					a[j - 1] = v;
					flg = 1;
					cnt++;
				}
			} int j = n - 1 ; int j = n - 1 ; j n - 1 n 1 j >= 1 j 1 j-- j-- j {
				if(a[j] < a[j - 1]) {
					int v = a[j];
					a[j] = a[j - 1];
					a[j - 1] = v;
					flg = 1;
					cnt++;
				}
			} {
				if(a[j] < a[j - 1]) {
					int v = a[j];
					a[j] = a[j - 1];
					a[j - 1] = v;
					flg = 1;
					cnt++;
				}
			} if(a[j] < a[j - 1]) {
					int v = a[j];
					a[j] = a[j - 1];
					a[j - 1] = v;
					flg = 1;
					cnt++;
				} a[j] < a[j - 1] a[j] a j a[j - 1] a j - 1 j 1 {
					int v = a[j];
					a[j] = a[j - 1];
					a[j - 1] = v;
					flg = 1;
					cnt++;
				} int v = a[j]; v a[j] a j a[j] = a[j - 1] a[j] a j a[j - 1] a j - 1 j 1 a[j - 1] = v a[j - 1] a j - 1 j 1 v flg = 1 flg 1 cnt++ cnt for(int i = 0 ; i < n ; i++) {
			if(i != n - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}
		} int i = 0 ; int i = 0 ; i 0 i < n i n i++ i++ i {
			if(i != n - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}
		} {
			if(i != n - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}
		} if(i != n - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			} i != n - 1 i n - 1 n 1 {
				System.out.print(a[i] + " ");
			} System.out.print(a[i] + " ") System.out.print System.out System System.out a[i] + " " a[i] a i " " {
				System.out.println(a[i]);
			} System.out.println(a[i]) System.out.println System.out System System.out a[i] a i System.out.println(cnt) System.out.println System.out System System.out cnt return a; a int[] a a int n n public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		for(int i = 0 ; i < n ; i++) {
			a[i] = sc.nextInt();
		}
		bubbleSort(a, n);
	}

	static int[] bubbleSort(int[] a, int n) {
		int flg = 1;
		int cnt = 0;
		while(flg == 1){
			flg = 0;
			for(int j = n - 1 ; j >= 1 ; j--) {
				if(a[j] < a[j - 1]) {
					int v = a[j];
					a[j] = a[j - 1];
					a[j - 1] = v;
					flg = 1;
					cnt++;
				}
			}
		}
		for(int i = 0 ; i < n ; i++) {
			if(i != n - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}
		}
		System.out.println(cnt);
		return a;
	}

} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		for(int i = 0 ; i < n ; i++) {
			a[i] = sc.nextInt();
		}
		bubbleSort(a, n);
	}

	static int[] bubbleSort(int[] a, int n) {
		int flg = 1;
		int cnt = 0;
		while(flg == 1){
			flg = 0;
			for(int j = n - 1 ; j >= 1 ; j--) {
				if(a[j] < a[j - 1]) {
					int v = a[j];
					a[j] = a[j - 1];
					a[j - 1] = v;
					flg = 1;
					cnt++;
				}
			}
		}
		for(int i = 0 ; i < n ; i++) {
			if(i != n - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}
		}
		System.out.println(cnt);
		return a;
	}

} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		for(int i = 0 ; i < n ; i++) {
			a[i] = sc.nextInt();
		}
		bubbleSort(a, n);
	}

	static int[] bubbleSort(int[] a, int n) {
		int flg = 1;
		int cnt = 0;
		while(flg == 1){
			flg = 0;
			for(int j = n - 1 ; j >= 1 ; j--) {
				if(a[j] < a[j - 1]) {
					int v = a[j];
					a[j] = a[j - 1];
					a[j - 1] = v;
					flg = 1;
					cnt++;
				}
			}
		}
		for(int i = 0 ; i < n ; i++) {
			if(i != n - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}
		}
		System.out.println(cnt);
		return a;
	}

}