import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int n = stdIn.nextInt();
		int[] a = new int[n];
		int i;
		
		for (i = 0; i < n; i++)
			a[i] =stdIn.nextInt();
		
		int count = BubbleSort(a, n);
		
		System.out.print(a[0]);
		for(i = 1; i < n; i++)
			System.out.print(" " + a[i]);
		System.out.println();
		System.out.println(count);
	}
	
	public static int BubbleSort(int[] a, int n) {
		int flag = 1, count = 0, i, t;
		while (flag != 0) {
			flag = 0;
			for (i = n - 1; i > 0; i--) {
				if (a[i] < a[i-1]) {
					t = a[i];
					a[i] = a[i-1];
					a[i-1] = t;
					count++;
					flag = 1;
				}
			}
		}
		return count;
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int n = stdIn.nextInt();
		int[] a = new int[n];
		int i;
		
		for (i = 0; i < n; i++)
			a[i] =stdIn.nextInt();
		
		int count = BubbleSort(a, n);
		
		System.out.print(a[0]);
		for(i = 1; i < n; i++)
			System.out.print(" " + a[i]);
		System.out.println();
		System.out.println(count);
	}
	
	public static int BubbleSort(int[] a, int n) {
		int flag = 1, count = 0, i, t;
		while (flag != 0) {
			flag = 0;
			for (i = n - 1; i > 0; i--) {
				if (a[i] < a[i-1]) {
					t = a[i];
					a[i] = a[i-1];
					a[i-1] = t;
					count++;
					flag = 1;
				}
			}
		}
		return count;
	}
} Main public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int n = stdIn.nextInt();
		int[] a = new int[n];
		int i;
		
		for (i = 0; i < n; i++)
			a[i] =stdIn.nextInt();
		
		int count = BubbleSort(a, n);
		
		System.out.print(a[0]);
		for(i = 1; i < n; i++)
			System.out.print(" " + a[i]);
		System.out.println();
		System.out.println(count);
	} main {
		Scanner stdIn = new Scanner(System.in);
		
		int n = stdIn.nextInt();
		int[] a = new int[n];
		int i;
		
		for (i = 0; i < n; i++)
			a[i] =stdIn.nextInt();
		
		int count = BubbleSort(a, n);
		
		System.out.print(a[0]);
		for(i = 1; i < n; i++)
			System.out.print(" " + a[i]);
		System.out.println();
		System.out.println(count);
	} Scanner stdIn = new Scanner(System.in); stdIn new Scanner(System.in) int n = stdIn.nextInt(); n stdIn.nextInt() stdIn.nextInt stdIn int[] a = new int[n]; a new int[n] n int i; i for (i = 0; i < n; i++)
			a[i] =stdIn.nextInt(); i = 0; i = 0 i 0 i < n i n i++ i++ i a[i] =stdIn.nextInt(); a[i] =stdIn.nextInt() a[i] a i stdIn.nextInt() stdIn.nextInt stdIn int count = BubbleSort(a, n); count BubbleSort(a, n) BubbleSort a n System.out.print(a[0]) System.out.print System.out System System.out a[0] a 0 for(i = 1; i < n; i++)
			System.out.print(" " + a[i]); i = 1; i = 1 i 1 i < n i n i++ i++ i 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() System.out.println System.out System System.out System.out.println(count) System.out.println System.out System System.out count String[] args args public static int BubbleSort(int[] a, int n) {
		int flag = 1, count = 0, i, t;
		while (flag != 0) {
			flag = 0;
			for (i = n - 1; i > 0; i--) {
				if (a[i] < a[i-1]) {
					t = a[i];
					a[i] = a[i-1];
					a[i-1] = t;
					count++;
					flag = 1;
				}
			}
		}
		return count;
	} BubbleSort {
		int flag = 1, count = 0, i, t;
		while (flag != 0) {
			flag = 0;
			for (i = n - 1; i > 0; i--) {
				if (a[i] < a[i-1]) {
					t = a[i];
					a[i] = a[i-1];
					a[i-1] = t;
					count++;
					flag = 1;
				}
			}
		}
		return count;
	} int flag = 1 flag 1 count = 0 count 0 i i t; t while (flag != 0) {
			flag = 0;
			for (i = n - 1; i > 0; i--) {
				if (a[i] < a[i-1]) {
					t = a[i];
					a[i] = a[i-1];
					a[i-1] = t;
					count++;
					flag = 1;
				}
			}
		} flag != 0 flag 0 {
			flag = 0;
			for (i = n - 1; i > 0; i--) {
				if (a[i] < a[i-1]) {
					t = a[i];
					a[i] = a[i-1];
					a[i-1] = t;
					count++;
					flag = 1;
				}
			}
		} flag = 0 flag 0 for (i = n - 1; i > 0; i--) {
				if (a[i] < a[i-1]) {
					t = a[i];
					a[i] = a[i-1];
					a[i-1] = t;
					count++;
					flag = 1;
				}
			} i = n - 1; i = n - 1 i n - 1 n 1 i > 0 i 0 i-- i-- i {
				if (a[i] < a[i-1]) {
					t = a[i];
					a[i] = a[i-1];
					a[i-1] = t;
					count++;
					flag = 1;
				}
			} {
				if (a[i] < a[i-1]) {
					t = a[i];
					a[i] = a[i-1];
					a[i-1] = t;
					count++;
					flag = 1;
				}
			} if (a[i] < a[i-1]) {
					t = a[i];
					a[i] = a[i-1];
					a[i-1] = t;
					count++;
					flag = 1;
				} a[i] < a[i-1] a[i] a i a[i-1] a i-1 i 1 {
					t = a[i];
					a[i] = a[i-1];
					a[i-1] = t;
					count++;
					flag = 1;
				} t = a[i] t a[i] a i a[i] = a[i-1] a[i] a i a[i-1] a i-1 i 1 a[i-1] = t a[i-1] a i-1 i 1 t count++ count flag = 1 flag 1 return count; count int[] a a int n n public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int n = stdIn.nextInt();
		int[] a = new int[n];
		int i;
		
		for (i = 0; i < n; i++)
			a[i] =stdIn.nextInt();
		
		int count = BubbleSort(a, n);
		
		System.out.print(a[0]);
		for(i = 1; i < n; i++)
			System.out.print(" " + a[i]);
		System.out.println();
		System.out.println(count);
	}
	
	public static int BubbleSort(int[] a, int n) {
		int flag = 1, count = 0, i, t;
		while (flag != 0) {
			flag = 0;
			for (i = n - 1; i > 0; i--) {
				if (a[i] < a[i-1]) {
					t = a[i];
					a[i] = a[i-1];
					a[i-1] = t;
					count++;
					flag = 1;
				}
			}
		}
		return count;
	}
} public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int n = stdIn.nextInt();
		int[] a = new int[n];
		int i;
		
		for (i = 0; i < n; i++)
			a[i] =stdIn.nextInt();
		
		int count = BubbleSort(a, n);
		
		System.out.print(a[0]);
		for(i = 1; i < n; i++)
			System.out.print(" " + a[i]);
		System.out.println();
		System.out.println(count);
	}
	
	public static int BubbleSort(int[] a, int n) {
		int flag = 1, count = 0, i, t;
		while (flag != 0) {
			flag = 0;
			for (i = n - 1; i > 0; i--) {
				if (a[i] < a[i-1]) {
					t = a[i];
					a[i] = a[i-1];
					a[i-1] = t;
					count++;
					flag = 1;
				}
			}
		}
		return count;
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int n = stdIn.nextInt();
		int[] a = new int[n];
		int i;
		
		for (i = 0; i < n; i++)
			a[i] =stdIn.nextInt();
		
		int count = BubbleSort(a, n);
		
		System.out.print(a[0]);
		for(i = 1; i < n; i++)
			System.out.print(" " + a[i]);
		System.out.println();
		System.out.println(count);
	}
	
	public static int BubbleSort(int[] a, int n) {
		int flag = 1, count = 0, i, t;
		while (flag != 0) {
			flag = 0;
			for (i = n - 1; i > 0; i--) {
				if (a[i] < a[i-1]) {
					t = a[i];
					a[i] = a[i-1];
					a[i-1] = t;
					count++;
					flag = 1;
				}
			}
		}
		return count;
	}
}