import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????
		new Main().start();
	}
	void start(){
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] a = new int[n];
		for(int i = 0; i < n; i++){
			a[i] = in.nextInt();
		}
//		a = insertionSort(a,n);
//		a = selectionSort(a,n);
		a = bubbleSort(a,n);

		in.close();

	}
	int[] bubbleSort(int[] c, int n){
		int count = 0;
		for(int i = 0; i < n-1; i++){
			for(int j = n-1; j >= i+1; j--){
				if(c[j]  < c[j-1]){
					int tmp = c[j];
					c[j] = c[j-1];
					c[j-1] = tmp;
					count++;
				}
			}
		}
		printArray(c);
		System.out.println(count);
		return c;
	}
	int[] insertionSort(int[]a, int n){
		for(int i = 1; i < n; i++){
			printArray(a);
			int v = a[i];
			int j = i - 1;
			while(j >= 0 && a[j] > v){
				a[j+1] = a[j];
				j--;
			}
			a[j+1] = v;
		}
		return a;
	}
	int[] selectionSort(int[] a, int n){
		int count = 0;
		for(int i = 0; i < n; i++){
//			printArray(a);
			int minj = i;
			for(int j = i; j < n;j++){
				if(a[j] < a[minj]) minj = j;
			}
			if(minj != i){
				int tmp = a[i];
				a[i] = a[minj];
				a[minj] = tmp;
				count++;
			}
		}
		printArray(a);
		System.out.println(count);
		return a;
	}
	void printArray(int[] a){
		System.out.print(a[0]);
		for(int i= 1; i < a.length; i++){
			System.out.print(" " + a[i]);
		}
		System.out.println();
	}

} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????
		new Main().start();
	}
	void start(){
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] a = new int[n];
		for(int i = 0; i < n; i++){
			a[i] = in.nextInt();
		}
//		a = insertionSort(a,n);
//		a = selectionSort(a,n);
		a = bubbleSort(a,n);

		in.close();

	}
	int[] bubbleSort(int[] c, int n){
		int count = 0;
		for(int i = 0; i < n-1; i++){
			for(int j = n-1; j >= i+1; j--){
				if(c[j]  < c[j-1]){
					int tmp = c[j];
					c[j] = c[j-1];
					c[j-1] = tmp;
					count++;
				}
			}
		}
		printArray(c);
		System.out.println(count);
		return c;
	}
	int[] insertionSort(int[]a, int n){
		for(int i = 1; i < n; i++){
			printArray(a);
			int v = a[i];
			int j = i - 1;
			while(j >= 0 && a[j] > v){
				a[j+1] = a[j];
				j--;
			}
			a[j+1] = v;
		}
		return a;
	}
	int[] selectionSort(int[] a, int n){
		int count = 0;
		for(int i = 0; i < n; i++){
//			printArray(a);
			int minj = i;
			for(int j = i; j < n;j++){
				if(a[j] < a[minj]) minj = j;
			}
			if(minj != i){
				int tmp = a[i];
				a[i] = a[minj];
				a[minj] = tmp;
				count++;
			}
		}
		printArray(a);
		System.out.println(count);
		return a;
	}
	void printArray(int[] a){
		System.out.print(a[0]);
		for(int i= 1; i < a.length; i++){
			System.out.print(" " + a[i]);
		}
		System.out.println();
	}

} Main public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????
		new Main().start();
	} main {
		// TODO ?????????????????????????????????????????????
		new Main().start();
	} new Main().start() new Main().start new Main() String[] args args void start(){
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] a = new int[n];
		for(int i = 0; i < n; i++){
			a[i] = in.nextInt();
		}
//		a = insertionSort(a,n);
//		a = selectionSort(a,n);
		a = bubbleSort(a,n);

		in.close();

	} start {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] a = new int[n];
		for(int i = 0; i < n; i++){
			a[i] = in.nextInt();
		}
//		a = insertionSort(a,n);
//		a = selectionSort(a,n);
		a = bubbleSort(a,n);

		in.close();

	} Scanner in = new Scanner(System.in); in new Scanner(System.in) int n = in.nextInt(); n in.nextInt() in.nextInt in int[] a = new int[n]; a new int[n] n for(int i = 0; i < n; i++){
			a[i] = in.nextInt();
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			a[i] = in.nextInt();
		} {
			a[i] = in.nextInt();
		} a[i] = in.nextInt() a[i] a i in.nextInt() in.nextInt in a = bubbleSort(a,n) a bubbleSort(a,n) bubbleSort a n in.close() in.close in int[] bubbleSort(int[] c, int n){
		int count = 0;
		for(int i = 0; i < n-1; i++){
			for(int j = n-1; j >= i+1; j--){
				if(c[j]  < c[j-1]){
					int tmp = c[j];
					c[j] = c[j-1];
					c[j-1] = tmp;
					count++;
				}
			}
		}
		printArray(c);
		System.out.println(count);
		return c;
	} bubbleSort {
		int count = 0;
		for(int i = 0; i < n-1; i++){
			for(int j = n-1; j >= i+1; j--){
				if(c[j]  < c[j-1]){
					int tmp = c[j];
					c[j] = c[j-1];
					c[j-1] = tmp;
					count++;
				}
			}
		}
		printArray(c);
		System.out.println(count);
		return c;
	} int count = 0; count 0 for(int i = 0; i < n-1; i++){
			for(int j = n-1; j >= i+1; j--){
				if(c[j]  < c[j-1]){
					int tmp = c[j];
					c[j] = c[j-1];
					c[j-1] = tmp;
					count++;
				}
			}
		} int i = 0; int i = 0; i 0 i < n-1 i n-1 n 1 i++ i++ i {
			for(int j = n-1; j >= i+1; j--){
				if(c[j]  < c[j-1]){
					int tmp = c[j];
					c[j] = c[j-1];
					c[j-1] = tmp;
					count++;
				}
			}
		} {
			for(int j = n-1; j >= i+1; j--){
				if(c[j]  < c[j-1]){
					int tmp = c[j];
					c[j] = c[j-1];
					c[j-1] = tmp;
					count++;
				}
			}
		} for(int j = n-1; j >= i+1; j--){
				if(c[j]  < c[j-1]){
					int tmp = c[j];
					c[j] = c[j-1];
					c[j-1] = tmp;
					count++;
				}
			} 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(c[j]  < c[j-1]){
					int tmp = c[j];
					c[j] = c[j-1];
					c[j-1] = tmp;
					count++;
				}
			} {
				if(c[j]  < c[j-1]){
					int tmp = c[j];
					c[j] = c[j-1];
					c[j-1] = tmp;
					count++;
				}
			} if(c[j]  < c[j-1]){
					int tmp = c[j];
					c[j] = c[j-1];
					c[j-1] = tmp;
					count++;
				} c[j]  < c[j-1] c[j] c j c[j-1] c j-1 j 1 {
					int tmp = c[j];
					c[j] = c[j-1];
					c[j-1] = tmp;
					count++;
				} int tmp = c[j]; tmp c[j] c j c[j] = c[j-1] c[j] c j c[j-1] c j-1 j 1 c[j-1] = tmp c[j-1] c j-1 j 1 tmp count++ count printArray(c) printArray c System.out.println(count) System.out.println System.out System System.out count return c; c int[] c c int n n int[] insertionSort(int[]a, int n){
		for(int i = 1; i < n; i++){
			printArray(a);
			int v = a[i];
			int j = i - 1;
			while(j >= 0 && a[j] > v){
				a[j+1] = a[j];
				j--;
			}
			a[j+1] = v;
		}
		return a;
	} insertionSort {
		for(int i = 1; i < n; i++){
			printArray(a);
			int v = a[i];
			int j = i - 1;
			while(j >= 0 && a[j] > v){
				a[j+1] = a[j];
				j--;
			}
			a[j+1] = v;
		}
		return a;
	} for(int i = 1; i < n; i++){
			printArray(a);
			int v = a[i];
			int j = i - 1;
			while(j >= 0 && a[j] > v){
				a[j+1] = a[j];
				j--;
			}
			a[j+1] = v;
		} int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
			printArray(a);
			int v = a[i];
			int j = i - 1;
			while(j >= 0 && a[j] > v){
				a[j+1] = a[j];
				j--;
			}
			a[j+1] = v;
		} {
			printArray(a);
			int v = a[i];
			int j = i - 1;
			while(j >= 0 && a[j] > v){
				a[j+1] = a[j];
				j--;
			}
			a[j+1] = v;
		} printArray(a) printArray a int v = a[i]; v a[i] a i int j = i - 1; j i - 1 i 1 while(j >= 0 && a[j] > v){
				a[j+1] = a[j];
				j--;
			} j >= 0 && a[j] > v j >= 0 j 0 a[j] > v a[j] a j v {
				a[j+1] = a[j];
				j--;
			} a[j+1] = a[j] a[j+1] a j+1 j 1 a[j] a j j-- j a[j+1] = v a[j+1] a j+1 j 1 v return a; a int[]a a int n n int[] selectionSort(int[] a, int n){
		int count = 0;
		for(int i = 0; i < n; i++){
//			printArray(a);
			int minj = i;
			for(int j = i; j < n;j++){
				if(a[j] < a[minj]) minj = j;
			}
			if(minj != i){
				int tmp = a[i];
				a[i] = a[minj];
				a[minj] = tmp;
				count++;
			}
		}
		printArray(a);
		System.out.println(count);
		return a;
	} selectionSort {
		int count = 0;
		for(int i = 0; i < n; i++){
//			printArray(a);
			int minj = i;
			for(int j = i; j < n;j++){
				if(a[j] < a[minj]) minj = j;
			}
			if(minj != i){
				int tmp = a[i];
				a[i] = a[minj];
				a[minj] = tmp;
				count++;
			}
		}
		printArray(a);
		System.out.println(count);
		return a;
	} int count = 0; count 0 for(int i = 0; i < n; i++){
//			printArray(a);
			int minj = i;
			for(int j = i; j < n;j++){
				if(a[j] < a[minj]) minj = j;
			}
			if(minj != i){
				int tmp = a[i];
				a[i] = a[minj];
				a[minj] = tmp;
				count++;
			}
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
//			printArray(a);
			int minj = i;
			for(int j = i; j < n;j++){
				if(a[j] < a[minj]) minj = j;
			}
			if(minj != i){
				int tmp = a[i];
				a[i] = a[minj];
				a[minj] = tmp;
				count++;
			}
		} {
//			printArray(a);
			int minj = i;
			for(int j = i; j < n;j++){
				if(a[j] < a[minj]) minj = j;
			}
			if(minj != i){
				int tmp = a[i];
				a[i] = a[minj];
				a[minj] = tmp;
				count++;
			}
		} int minj = i; minj i for(int j = i; j < n;j++){
				if(a[j] < a[minj]) minj = j;
			} int j = i; int j = i; j i j < n j n j++ j++ j {
				if(a[j] < a[minj]) minj = j;
			} {
				if(a[j] < a[minj]) minj = j;
			} if(a[j] < a[minj]) minj = j; a[j] < a[minj] a[j] a j a[minj] a minj minj = j minj j if(minj != i){
				int tmp = a[i];
				a[i] = a[minj];
				a[minj] = tmp;
				count++;
			} minj != i minj i {
				int tmp = a[i];
				a[i] = a[minj];
				a[minj] = tmp;
				count++;
			} int tmp = a[i]; tmp a[i] a i a[i] = a[minj] a[i] a i a[minj] a minj a[minj] = tmp a[minj] a minj tmp count++ count printArray(a) printArray a System.out.println(count) System.out.println System.out System System.out count return a; a int[] a a int n n void printArray(int[] a){
		System.out.print(a[0]);
		for(int i= 1; i < a.length; i++){
			System.out.print(" " + a[i]);
		}
		System.out.println();
	} printArray {
		System.out.print(a[0]);
		for(int i= 1; i < a.length; i++){
			System.out.print(" " + a[i]);
		}
		System.out.println();
	} System.out.print(a[0]) System.out.print System.out System System.out a[0] a 0 for(int i= 1; i < a.length; i++){
			System.out.print(" " + a[i]);
		} int i= 1; int i= 1; i 1 i < a.length i a.length a a.length i++ i++ i {
			System.out.print(" " + a[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 int[] a a public class Main {

	public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????
		new Main().start();
	}
	void start(){
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] a = new int[n];
		for(int i = 0; i < n; i++){
			a[i] = in.nextInt();
		}
//		a = insertionSort(a,n);
//		a = selectionSort(a,n);
		a = bubbleSort(a,n);

		in.close();

	}
	int[] bubbleSort(int[] c, int n){
		int count = 0;
		for(int i = 0; i < n-1; i++){
			for(int j = n-1; j >= i+1; j--){
				if(c[j]  < c[j-1]){
					int tmp = c[j];
					c[j] = c[j-1];
					c[j-1] = tmp;
					count++;
				}
			}
		}
		printArray(c);
		System.out.println(count);
		return c;
	}
	int[] insertionSort(int[]a, int n){
		for(int i = 1; i < n; i++){
			printArray(a);
			int v = a[i];
			int j = i - 1;
			while(j >= 0 && a[j] > v){
				a[j+1] = a[j];
				j--;
			}
			a[j+1] = v;
		}
		return a;
	}
	int[] selectionSort(int[] a, int n){
		int count = 0;
		for(int i = 0; i < n; i++){
//			printArray(a);
			int minj = i;
			for(int j = i; j < n;j++){
				if(a[j] < a[minj]) minj = j;
			}
			if(minj != i){
				int tmp = a[i];
				a[i] = a[minj];
				a[minj] = tmp;
				count++;
			}
		}
		printArray(a);
		System.out.println(count);
		return a;
	}
	void printArray(int[] a){
		System.out.print(a[0]);
		for(int i= 1; i < a.length; i++){
			System.out.print(" " + a[i]);
		}
		System.out.println();
	}

} public class Main {

	public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????
		new Main().start();
	}
	void start(){
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] a = new int[n];
		for(int i = 0; i < n; i++){
			a[i] = in.nextInt();
		}
//		a = insertionSort(a,n);
//		a = selectionSort(a,n);
		a = bubbleSort(a,n);

		in.close();

	}
	int[] bubbleSort(int[] c, int n){
		int count = 0;
		for(int i = 0; i < n-1; i++){
			for(int j = n-1; j >= i+1; j--){
				if(c[j]  < c[j-1]){
					int tmp = c[j];
					c[j] = c[j-1];
					c[j-1] = tmp;
					count++;
				}
			}
		}
		printArray(c);
		System.out.println(count);
		return c;
	}
	int[] insertionSort(int[]a, int n){
		for(int i = 1; i < n; i++){
			printArray(a);
			int v = a[i];
			int j = i - 1;
			while(j >= 0 && a[j] > v){
				a[j+1] = a[j];
				j--;
			}
			a[j+1] = v;
		}
		return a;
	}
	int[] selectionSort(int[] a, int n){
		int count = 0;
		for(int i = 0; i < n; i++){
//			printArray(a);
			int minj = i;
			for(int j = i; j < n;j++){
				if(a[j] < a[minj]) minj = j;
			}
			if(minj != i){
				int tmp = a[i];
				a[i] = a[minj];
				a[minj] = tmp;
				count++;
			}
		}
		printArray(a);
		System.out.println(count);
		return a;
	}
	void printArray(int[] a){
		System.out.print(a[0]);
		for(int i= 1; i < a.length; i++){
			System.out.print(" " + a[i]);
		}
		System.out.println();
	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO ?????????????????????????????????????????????
		new Main().start();
	}
	void start(){
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] a = new int[n];
		for(int i = 0; i < n; i++){
			a[i] = in.nextInt();
		}
//		a = insertionSort(a,n);
//		a = selectionSort(a,n);
		a = bubbleSort(a,n);

		in.close();

	}
	int[] bubbleSort(int[] c, int n){
		int count = 0;
		for(int i = 0; i < n-1; i++){
			for(int j = n-1; j >= i+1; j--){
				if(c[j]  < c[j-1]){
					int tmp = c[j];
					c[j] = c[j-1];
					c[j-1] = tmp;
					count++;
				}
			}
		}
		printArray(c);
		System.out.println(count);
		return c;
	}
	int[] insertionSort(int[]a, int n){
		for(int i = 1; i < n; i++){
			printArray(a);
			int v = a[i];
			int j = i - 1;
			while(j >= 0 && a[j] > v){
				a[j+1] = a[j];
				j--;
			}
			a[j+1] = v;
		}
		return a;
	}
	int[] selectionSort(int[] a, int n){
		int count = 0;
		for(int i = 0; i < n; i++){
//			printArray(a);
			int minj = i;
			for(int j = i; j < n;j++){
				if(a[j] < a[minj]) minj = j;
			}
			if(minj != i){
				int tmp = a[i];
				a[i] = a[minj];
				a[minj] = tmp;
				count++;
			}
		}
		printArray(a);
		System.out.println(count);
		return a;
	}
	void printArray(int[] a){
		System.out.print(a[0]);
		for(int i= 1; i < a.length; i++){
			System.out.print(" " + a[i]);
		}
		System.out.println();
	}

}