
import java.util.Scanner;

public class Main {

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

	}

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

	public static void printArray(int array[], int n) {
		for (int i = 0; i < n - 1; i++) {
			System.out.print(array[i] + " ");
		}
		System.out.println(array[n - 1]);
	}

	public static void swap(int array[], int a, int b) {
		int tmp = array[a];
		array[a] = array[b];
		array[b] = tmp;
	}

}
 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 array[] = new int[n];
		for (int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		}
		bubbleSort(array, n);

	}

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

	public static void printArray(int array[], int n) {
		for (int i = 0; i < n - 1; i++) {
			System.out.print(array[i] + " ");
		}
		System.out.println(array[n - 1]);
	}

	public static void swap(int array[], int a, int b) {
		int tmp = array[a];
		array[a] = array[b];
		array[b] = tmp;
	}

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

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

	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int array[] = new int[n]; array new int[n] n for (int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			array[i] = sc.nextInt();
		} {
			array[i] = sc.nextInt();
		} array[i] = sc.nextInt() array[i] array i sc.nextInt() sc.nextInt sc bubbleSort(array, n) bubbleSort array n String[] args args public static void bubbleSort(int array[], int n) {
		int count = 0;
		boolean flag = true;
		while (flag) {
			flag = false;
			for (int j = n - 1; j > 0; j--) {
				if (array[j] < array[j - 1]) {
					swap(array, j, j - 1);
					count++;
					flag = true;
				}
			}
		}
		printArray(array, n);
		System.out.println(count);
	} bubbleSort {
		int count = 0;
		boolean flag = true;
		while (flag) {
			flag = false;
			for (int j = n - 1; j > 0; j--) {
				if (array[j] < array[j - 1]) {
					swap(array, j, j - 1);
					count++;
					flag = true;
				}
			}
		}
		printArray(array, n);
		System.out.println(count);
	} int count = 0; count 0 boolean flag = true; flag true while (flag) {
			flag = false;
			for (int j = n - 1; j > 0; j--) {
				if (array[j] < array[j - 1]) {
					swap(array, j, j - 1);
					count++;
					flag = true;
				}
			}
		} flag {
			flag = false;
			for (int j = n - 1; j > 0; j--) {
				if (array[j] < array[j - 1]) {
					swap(array, j, j - 1);
					count++;
					flag = true;
				}
			}
		} flag = false flag false for (int j = n - 1; j > 0; j--) {
				if (array[j] < array[j - 1]) {
					swap(array, j, j - 1);
					count++;
					flag = true;
				}
			} int j = n - 1; int j = n - 1; j n - 1 n 1 j > 0 j 0 j-- j-- j {
				if (array[j] < array[j - 1]) {
					swap(array, j, j - 1);
					count++;
					flag = true;
				}
			} {
				if (array[j] < array[j - 1]) {
					swap(array, j, j - 1);
					count++;
					flag = true;
				}
			} if (array[j] < array[j - 1]) {
					swap(array, j, j - 1);
					count++;
					flag = true;
				} array[j] < array[j - 1] array[j] array j array[j - 1] array j - 1 j 1 {
					swap(array, j, j - 1);
					count++;
					flag = true;
				} swap(array, j, j - 1) swap array j j - 1 j 1 count++ count flag = true flag true printArray(array, n) printArray array n System.out.println(count) System.out.println System.out System System.out count int array[] array int n n public static void printArray(int array[], int n) {
		for (int i = 0; i < n - 1; i++) {
			System.out.print(array[i] + " ");
		}
		System.out.println(array[n - 1]);
	} printArray {
		for (int i = 0; i < n - 1; i++) {
			System.out.print(array[i] + " ");
		}
		System.out.println(array[n - 1]);
	} for (int i = 0; i < n - 1; i++) {
			System.out.print(array[i] + " ");
		} int i = 0; int i = 0; i 0 i < n - 1 i n - 1 n 1 i++ i++ i {
			System.out.print(array[i] + " ");
		} {
			System.out.print(array[i] + " ");
		} System.out.print(array[i] + " ") System.out.print System.out System System.out array[i] + " " array[i] array i " " System.out.println(array[n - 1]) System.out.println System.out System System.out array[n - 1] array n - 1 n 1 int array[] array int n n public static void swap(int array[], int a, int b) {
		int tmp = array[a];
		array[a] = array[b];
		array[b] = tmp;
	} swap {
		int tmp = array[a];
		array[a] = array[b];
		array[b] = tmp;
	} int tmp = array[a]; tmp array[a] array a array[a] = array[b] array[a] array a array[b] array b array[b] = tmp array[b] array b tmp int array[] array int a a int b b public class Main {

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

	}

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

	public static void printArray(int array[], int n) {
		for (int i = 0; i < n - 1; i++) {
			System.out.print(array[i] + " ");
		}
		System.out.println(array[n - 1]);
	}

	public static void swap(int array[], int a, int b) {
		int tmp = array[a];
		array[a] = array[b];
		array[b] = tmp;
	}

} public class Main {

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

	}

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

	public static void printArray(int array[], int n) {
		for (int i = 0; i < n - 1; i++) {
			System.out.print(array[i] + " ");
		}
		System.out.println(array[n - 1]);
	}

	public static void swap(int array[], int a, int b) {
		int tmp = array[a];
		array[a] = array[b];
		array[b] = tmp;
	}

} 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 array[] = new int[n];
		for (int i = 0; i < n; i++) {
			array[i] = sc.nextInt();
		}
		bubbleSort(array, n);

	}

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

	public static void printArray(int array[], int n) {
		for (int i = 0; i < n - 1; i++) {
			System.out.print(array[i] + " ");
		}
		System.out.println(array[n - 1]);
	}

	public static void swap(int array[], int a, int b) {
		int tmp = array[a];
		array[a] = array[b];
		array[b] = tmp;
	}

}

