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();
		}

		Main main = new Main();
		int ans = main.bubbleSort(a, n);

		for (int i = 0; i < a.length; i++) {
			if (i != a.length - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}

		}
		System.out.println(ans);

	}

	private int bubbleSort(int[] a, int n) {
		boolean hasSwapped = true; // 要素の入れ替えを行った
		int cnt = 0;
		// 未ソート部分の先頭を表す i を持つ外側のループ
		for (int i = 0; hasSwapped; i++) {
			hasSwapped = false;
			// 末尾の要素からi+1まで入れ替え処理を行う
			for (int j = n - 1; j >= i + 1; j--) {
				if (a[j] < a[j - 1]) {
					int tmp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = tmp;
					cnt++;
					hasSwapped = true;
				}
			}

		}
		return cnt;
	}

}
 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();
		}

		Main main = new Main();
		int ans = main.bubbleSort(a, n);

		for (int i = 0; i < a.length; i++) {
			if (i != a.length - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}

		}
		System.out.println(ans);

	}

	private int bubbleSort(int[] a, int n) {
		boolean hasSwapped = true; // 要素の入れ替えを行った
		int cnt = 0;
		// 未ソート部分の先頭を表す i を持つ外側のループ
		for (int i = 0; hasSwapped; i++) {
			hasSwapped = false;
			// 末尾の要素からi+1まで入れ替え処理を行う
			for (int j = n - 1; j >= i + 1; j--) {
				if (a[j] < a[j - 1]) {
					int tmp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = tmp;
					cnt++;
					hasSwapped = true;
				}
			}

		}
		return cnt;
	}

} 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();
		}

		Main main = new Main();
		int ans = main.bubbleSort(a, n);

		for (int i = 0; i < a.length; i++) {
			if (i != a.length - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}

		}
		System.out.println(ans);

	} 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();
		}

		Main main = new Main();
		int ans = main.bubbleSort(a, n);

		for (int i = 0; i < a.length; i++) {
			if (i != a.length - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}

		}
		System.out.println(ans);

	} 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 Main main = new Main(); main new Main() int ans = main.bubbleSort(a, n); ans main.bubbleSort(a, n) main.bubbleSort main a n for (int i = 0; i < a.length; i++) {
			if (i != a.length - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}

		} int i = 0; int i = 0; i 0 i < a.length i a.length a a.length i++ i++ i {
			if (i != a.length - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}

		} {
			if (i != a.length - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}

		} if (i != a.length - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			} i != a.length - 1 i a.length - 1 a.length a a.length 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(ans) System.out.println System.out System System.out ans String[] args args private int bubbleSort(int[] a, int n) {
		boolean hasSwapped = true; // 要素の入れ替えを行った
		int cnt = 0;
		// 未ソート部分の先頭を表す i を持つ外側のループ
		for (int i = 0; hasSwapped; i++) {
			hasSwapped = false;
			// 末尾の要素からi+1まで入れ替え処理を行う
			for (int j = n - 1; j >= i + 1; j--) {
				if (a[j] < a[j - 1]) {
					int tmp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = tmp;
					cnt++;
					hasSwapped = true;
				}
			}

		}
		return cnt;
	} bubbleSort {
		boolean hasSwapped = true; // 要素の入れ替えを行った
		int cnt = 0;
		// 未ソート部分の先頭を表す i を持つ外側のループ
		for (int i = 0; hasSwapped; i++) {
			hasSwapped = false;
			// 末尾の要素からi+1まで入れ替え処理を行う
			for (int j = n - 1; j >= i + 1; j--) {
				if (a[j] < a[j - 1]) {
					int tmp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = tmp;
					cnt++;
					hasSwapped = true;
				}
			}

		}
		return cnt;
	} boolean hasSwapped = true; hasSwapped true int cnt = 0; cnt 0 for (int i = 0; hasSwapped; i++) {
			hasSwapped = false;
			// 末尾の要素からi+1まで入れ替え処理を行う
			for (int j = n - 1; j >= i + 1; j--) {
				if (a[j] < a[j - 1]) {
					int tmp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = tmp;
					cnt++;
					hasSwapped = true;
				}
			}

		} int i = 0; int i = 0; i 0 hasSwapped i++ i++ i {
			hasSwapped = false;
			// 末尾の要素からi+1まで入れ替え処理を行う
			for (int j = n - 1; j >= i + 1; j--) {
				if (a[j] < a[j - 1]) {
					int tmp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = tmp;
					cnt++;
					hasSwapped = true;
				}
			}

		} {
			hasSwapped = false;
			// 末尾の要素からi+1まで入れ替え処理を行う
			for (int j = n - 1; j >= i + 1; j--) {
				if (a[j] < a[j - 1]) {
					int tmp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = tmp;
					cnt++;
					hasSwapped = true;
				}
			}

		} hasSwapped = false hasSwapped false for (int j = n - 1; j >= i + 1; j--) {
				if (a[j] < a[j - 1]) {
					int tmp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = tmp;
					cnt++;
					hasSwapped = true;
				}
			} int j = n - 1; int j = n - 1; j n - 1 n 1 j >= i + 1 j i + 1 i 1 j-- j-- j {
				if (a[j] < a[j - 1]) {
					int tmp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = tmp;
					cnt++;
					hasSwapped = true;
				}
			} {
				if (a[j] < a[j - 1]) {
					int tmp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = tmp;
					cnt++;
					hasSwapped = true;
				}
			} if (a[j] < a[j - 1]) {
					int tmp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = tmp;
					cnt++;
					hasSwapped = true;
				} a[j] < a[j - 1] a[j] a j a[j - 1] a j - 1 j 1 {
					int tmp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = tmp;
					cnt++;
					hasSwapped = true;
				} int tmp = a[j]; tmp a[j] a j a[j] = a[j - 1] a[j] a j a[j - 1] a j - 1 j 1 a[j - 1] = tmp a[j - 1] a j - 1 j 1 tmp cnt++ cnt hasSwapped = true hasSwapped true return cnt; cnt 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();
		}

		Main main = new Main();
		int ans = main.bubbleSort(a, n);

		for (int i = 0; i < a.length; i++) {
			if (i != a.length - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}

		}
		System.out.println(ans);

	}

	private int bubbleSort(int[] a, int n) {
		boolean hasSwapped = true; // 要素の入れ替えを行った
		int cnt = 0;
		// 未ソート部分の先頭を表す i を持つ外側のループ
		for (int i = 0; hasSwapped; i++) {
			hasSwapped = false;
			// 末尾の要素からi+1まで入れ替え処理を行う
			for (int j = n - 1; j >= i + 1; j--) {
				if (a[j] < a[j - 1]) {
					int tmp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = tmp;
					cnt++;
					hasSwapped = true;
				}
			}

		}
		return cnt;
	}

} 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();
		}

		Main main = new Main();
		int ans = main.bubbleSort(a, n);

		for (int i = 0; i < a.length; i++) {
			if (i != a.length - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}

		}
		System.out.println(ans);

	}

	private int bubbleSort(int[] a, int n) {
		boolean hasSwapped = true; // 要素の入れ替えを行った
		int cnt = 0;
		// 未ソート部分の先頭を表す i を持つ外側のループ
		for (int i = 0; hasSwapped; i++) {
			hasSwapped = false;
			// 末尾の要素からi+1まで入れ替え処理を行う
			for (int j = n - 1; j >= i + 1; j--) {
				if (a[j] < a[j - 1]) {
					int tmp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = tmp;
					cnt++;
					hasSwapped = true;
				}
			}

		}
		return cnt;
	}

} 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();
		}

		Main main = new Main();
		int ans = main.bubbleSort(a, n);

		for (int i = 0; i < a.length; i++) {
			if (i != a.length - 1) {
				System.out.print(a[i] + " ");
			} else {
				System.out.println(a[i]);
			}

		}
		System.out.println(ans);

	}

	private int bubbleSort(int[] a, int n) {
		boolean hasSwapped = true; // 要素の入れ替えを行った
		int cnt = 0;
		// 未ソート部分の先頭を表す i を持つ外側のループ
		for (int i = 0; hasSwapped; i++) {
			hasSwapped = false;
			// 末尾の要素からi+1まで入れ替え処理を行う
			for (int j = n - 1; j >= i + 1; j--) {
				if (a[j] < a[j - 1]) {
					int tmp = a[j];
					a[j] = a[j - 1];
					a[j - 1] = tmp;
					cnt++;
					hasSwapped = true;
				}
			}

		}
		return cnt;
	}

}

