import java.io.IOException;
import java.util.Scanner;

/**
 * バブルソート
 * 
 *
 */

public class Main {
	public static void main(String[] args) throws IOException {
		Scanner s = new Scanner(System.in);

		int n = s.nextInt();
		
		int[] arr = new int[n];
		for (int i = 0; i < n; i++) {
			arr[i] = s.nextInt();
		}
		
		bubbleSort(arr, n);
		
	}
	
	public static void bubbleSort(int[] arr, int n) {
		boolean flag = true;
		int count = 0;
		while (flag) {
			flag = false;
			for (int j = n-1; j > 0; j--) {
				if (arr[j] < arr[j-1]) {
					int tmp = arr[j];
					arr[j] = arr[j-1];
					arr[j-1] = tmp;
					flag = true;
					count += 1;
				}
			}
		}
		printArray(arr);
		System.out.println(count);
	}
	
	public static void printArray(int[] arr) {
		String ans = "";
		for (int i = 0; i < arr.length; i++) {
			ans += arr[i] + " ";
		}
		System.out.println(ans.trim());
	}
} import java.io.IOException; IOException java.io import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) throws IOException {
		Scanner s = new Scanner(System.in);

		int n = s.nextInt();
		
		int[] arr = new int[n];
		for (int i = 0; i < n; i++) {
			arr[i] = s.nextInt();
		}
		
		bubbleSort(arr, n);
		
	}
	
	public static void bubbleSort(int[] arr, int n) {
		boolean flag = true;
		int count = 0;
		while (flag) {
			flag = false;
			for (int j = n-1; j > 0; j--) {
				if (arr[j] < arr[j-1]) {
					int tmp = arr[j];
					arr[j] = arr[j-1];
					arr[j-1] = tmp;
					flag = true;
					count += 1;
				}
			}
		}
		printArray(arr);
		System.out.println(count);
	}
	
	public static void printArray(int[] arr) {
		String ans = "";
		for (int i = 0; i < arr.length; i++) {
			ans += arr[i] + " ";
		}
		System.out.println(ans.trim());
	}
} Main public static void main(String[] args) throws IOException {
		Scanner s = new Scanner(System.in);

		int n = s.nextInt();
		
		int[] arr = new int[n];
		for (int i = 0; i < n; i++) {
			arr[i] = s.nextInt();
		}
		
		bubbleSort(arr, n);
		
	} main {
		Scanner s = new Scanner(System.in);

		int n = s.nextInt();
		
		int[] arr = new int[n];
		for (int i = 0; i < n; i++) {
			arr[i] = s.nextInt();
		}
		
		bubbleSort(arr, n);
		
	} Scanner s = new Scanner(System.in); s new Scanner(System.in) int n = s.nextInt(); n s.nextInt() s.nextInt s int[] arr = new int[n]; arr new int[n] n for (int i = 0; i < n; i++) {
			arr[i] = s.nextInt();
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			arr[i] = s.nextInt();
		} {
			arr[i] = s.nextInt();
		} arr[i] = s.nextInt() arr[i] arr i s.nextInt() s.nextInt s bubbleSort(arr, n) bubbleSort arr n String[] args args public static void bubbleSort(int[] arr, int n) {
		boolean flag = true;
		int count = 0;
		while (flag) {
			flag = false;
			for (int j = n-1; j > 0; j--) {
				if (arr[j] < arr[j-1]) {
					int tmp = arr[j];
					arr[j] = arr[j-1];
					arr[j-1] = tmp;
					flag = true;
					count += 1;
				}
			}
		}
		printArray(arr);
		System.out.println(count);
	} bubbleSort {
		boolean flag = true;
		int count = 0;
		while (flag) {
			flag = false;
			for (int j = n-1; j > 0; j--) {
				if (arr[j] < arr[j-1]) {
					int tmp = arr[j];
					arr[j] = arr[j-1];
					arr[j-1] = tmp;
					flag = true;
					count += 1;
				}
			}
		}
		printArray(arr);
		System.out.println(count);
	} boolean flag = true; flag true int count = 0; count 0 while (flag) {
			flag = false;
			for (int j = n-1; j > 0; j--) {
				if (arr[j] < arr[j-1]) {
					int tmp = arr[j];
					arr[j] = arr[j-1];
					arr[j-1] = tmp;
					flag = true;
					count += 1;
				}
			}
		} flag {
			flag = false;
			for (int j = n-1; j > 0; j--) {
				if (arr[j] < arr[j-1]) {
					int tmp = arr[j];
					arr[j] = arr[j-1];
					arr[j-1] = tmp;
					flag = true;
					count += 1;
				}
			}
		} flag = false flag false for (int j = n-1; j > 0; j--) {
				if (arr[j] < arr[j-1]) {
					int tmp = arr[j];
					arr[j] = arr[j-1];
					arr[j-1] = tmp;
					flag = true;
					count += 1;
				}
			} int j = n-1; int j = n-1; j n-1 n 1 j > 0 j 0 j-- j-- j {
				if (arr[j] < arr[j-1]) {
					int tmp = arr[j];
					arr[j] = arr[j-1];
					arr[j-1] = tmp;
					flag = true;
					count += 1;
				}
			} {
				if (arr[j] < arr[j-1]) {
					int tmp = arr[j];
					arr[j] = arr[j-1];
					arr[j-1] = tmp;
					flag = true;
					count += 1;
				}
			} if (arr[j] < arr[j-1]) {
					int tmp = arr[j];
					arr[j] = arr[j-1];
					arr[j-1] = tmp;
					flag = true;
					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;
					flag = true;
					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 flag = true flag true count += 1 count 1 printArray(arr) printArray arr System.out.println(count) System.out.println System.out System System.out count int[] arr arr int n n public static void printArray(int[] arr) {
		String ans = "";
		for (int i = 0; i < arr.length; i++) {
			ans += arr[i] + " ";
		}
		System.out.println(ans.trim());
	} printArray {
		String ans = "";
		for (int i = 0; i < arr.length; i++) {
			ans += arr[i] + " ";
		}
		System.out.println(ans.trim());
	} String ans = ""; ans "" for (int i = 0; i < arr.length; i++) {
			ans += arr[i] + " ";
		} int i = 0; int i = 0; i 0 i < arr.length i arr.length arr arr.length i++ i++ i {
			ans += arr[i] + " ";
		} {
			ans += arr[i] + " ";
		} ans += arr[i] + " " ans arr[i] + " " arr[i] arr i " " System.out.println(ans.trim()) System.out.println System.out System System.out ans.trim() ans.trim ans int[] arr arr public class Main {
	public static void main(String[] args) throws IOException {
		Scanner s = new Scanner(System.in);

		int n = s.nextInt();
		
		int[] arr = new int[n];
		for (int i = 0; i < n; i++) {
			arr[i] = s.nextInt();
		}
		
		bubbleSort(arr, n);
		
	}
	
	public static void bubbleSort(int[] arr, int n) {
		boolean flag = true;
		int count = 0;
		while (flag) {
			flag = false;
			for (int j = n-1; j > 0; j--) {
				if (arr[j] < arr[j-1]) {
					int tmp = arr[j];
					arr[j] = arr[j-1];
					arr[j-1] = tmp;
					flag = true;
					count += 1;
				}
			}
		}
		printArray(arr);
		System.out.println(count);
	}
	
	public static void printArray(int[] arr) {
		String ans = "";
		for (int i = 0; i < arr.length; i++) {
			ans += arr[i] + " ";
		}
		System.out.println(ans.trim());
	}
} public class Main {
	public static void main(String[] args) throws IOException {
		Scanner s = new Scanner(System.in);

		int n = s.nextInt();
		
		int[] arr = new int[n];
		for (int i = 0; i < n; i++) {
			arr[i] = s.nextInt();
		}
		
		bubbleSort(arr, n);
		
	}
	
	public static void bubbleSort(int[] arr, int n) {
		boolean flag = true;
		int count = 0;
		while (flag) {
			flag = false;
			for (int j = n-1; j > 0; j--) {
				if (arr[j] < arr[j-1]) {
					int tmp = arr[j];
					arr[j] = arr[j-1];
					arr[j-1] = tmp;
					flag = true;
					count += 1;
				}
			}
		}
		printArray(arr);
		System.out.println(count);
	}
	
	public static void printArray(int[] arr) {
		String ans = "";
		for (int i = 0; i < arr.length; i++) {
			ans += arr[i] + " ";
		}
		System.out.println(ans.trim());
	}
} Main
import java.io.IOException;
import java.util.Scanner;

/**
 * バブルソート
 * 
 *
 */

public class Main {
	public static void main(String[] args) throws IOException {
		Scanner s = new Scanner(System.in);

		int n = s.nextInt();
		
		int[] arr = new int[n];
		for (int i = 0; i < n; i++) {
			arr[i] = s.nextInt();
		}
		
		bubbleSort(arr, n);
		
	}
	
	public static void bubbleSort(int[] arr, int n) {
		boolean flag = true;
		int count = 0;
		while (flag) {
			flag = false;
			for (int j = n-1; j > 0; j--) {
				if (arr[j] < arr[j-1]) {
					int tmp = arr[j];
					arr[j] = arr[j-1];
					arr[j-1] = tmp;
					flag = true;
					count += 1;
				}
			}
		}
		printArray(arr);
		System.out.println(count);
	}
	
	public static void printArray(int[] arr) {
		String ans = "";
		for (int i = 0; i < arr.length; i++) {
			ans += arr[i] + " ";
		}
		System.out.println(ans.trim());
	}
}
