import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		int count = scanner.nextInt();
		int[] sortList = new int[count];
		for(int i = 0; i < count; i++){
			sortList[i] = scanner.nextInt();
		}
		int anser = bubbleSort(sortList, count);
		for(int i = 0; i < count; i++){
			if(i != count - 1){
				System.out.print(sortList[i] + " ");
			}else{
				System.out.println(sortList[i]);
			}
		}
		System.out.println(anser);
	}

	private static int bubbleSort(int[] sortList, int count){
		int exchange_count = 0;
		int temp = 0;
		for(int j = 0; j < count; j++){
			for(int i = count - 1; i > j; i--){
				if(sortList[i] < sortList[i - 1]){
					temp = sortList[i];
					sortList[i] = sortList[i - 1];
					sortList[i - 1] = temp;
					exchange_count++;
				}
			}
		}
		return exchange_count;
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		int count = scanner.nextInt();
		int[] sortList = new int[count];
		for(int i = 0; i < count; i++){
			sortList[i] = scanner.nextInt();
		}
		int anser = bubbleSort(sortList, count);
		for(int i = 0; i < count; i++){
			if(i != count - 1){
				System.out.print(sortList[i] + " ");
			}else{
				System.out.println(sortList[i]);
			}
		}
		System.out.println(anser);
	}

	private static int bubbleSort(int[] sortList, int count){
		int exchange_count = 0;
		int temp = 0;
		for(int j = 0; j < count; j++){
			for(int i = count - 1; i > j; i--){
				if(sortList[i] < sortList[i - 1]){
					temp = sortList[i];
					sortList[i] = sortList[i - 1];
					sortList[i - 1] = temp;
					exchange_count++;
				}
			}
		}
		return exchange_count;
	}
} Main public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		int count = scanner.nextInt();
		int[] sortList = new int[count];
		for(int i = 0; i < count; i++){
			sortList[i] = scanner.nextInt();
		}
		int anser = bubbleSort(sortList, count);
		for(int i = 0; i < count; i++){
			if(i != count - 1){
				System.out.print(sortList[i] + " ");
			}else{
				System.out.println(sortList[i]);
			}
		}
		System.out.println(anser);
	} main {
		Scanner scanner = new Scanner(System.in);
		int count = scanner.nextInt();
		int[] sortList = new int[count];
		for(int i = 0; i < count; i++){
			sortList[i] = scanner.nextInt();
		}
		int anser = bubbleSort(sortList, count);
		for(int i = 0; i < count; i++){
			if(i != count - 1){
				System.out.print(sortList[i] + " ");
			}else{
				System.out.println(sortList[i]);
			}
		}
		System.out.println(anser);
	} Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int count = scanner.nextInt(); count scanner.nextInt() scanner.nextInt scanner int[] sortList = new int[count]; sortList new int[count] count for(int i = 0; i < count; i++){
			sortList[i] = scanner.nextInt();
		} int i = 0; int i = 0; i 0 i < count i count i++ i++ i {
			sortList[i] = scanner.nextInt();
		} {
			sortList[i] = scanner.nextInt();
		} sortList[i] = scanner.nextInt() sortList[i] sortList i scanner.nextInt() scanner.nextInt scanner int anser = bubbleSort(sortList, count); anser bubbleSort(sortList, count) bubbleSort sortList count for(int i = 0; i < count; i++){
			if(i != count - 1){
				System.out.print(sortList[i] + " ");
			}else{
				System.out.println(sortList[i]);
			}
		} int i = 0; int i = 0; i 0 i < count i count i++ i++ i {
			if(i != count - 1){
				System.out.print(sortList[i] + " ");
			}else{
				System.out.println(sortList[i]);
			}
		} {
			if(i != count - 1){
				System.out.print(sortList[i] + " ");
			}else{
				System.out.println(sortList[i]);
			}
		} if(i != count - 1){
				System.out.print(sortList[i] + " ");
			}else{
				System.out.println(sortList[i]);
			} i != count - 1 i count - 1 count 1 {
				System.out.print(sortList[i] + " ");
			} System.out.print(sortList[i] + " ") System.out.print System.out System System.out sortList[i] + " " sortList[i] sortList i " " {
				System.out.println(sortList[i]);
			} System.out.println(sortList[i]) System.out.println System.out System System.out sortList[i] sortList i System.out.println(anser) System.out.println System.out System System.out anser String[] args args private static int bubbleSort(int[] sortList, int count){
		int exchange_count = 0;
		int temp = 0;
		for(int j = 0; j < count; j++){
			for(int i = count - 1; i > j; i--){
				if(sortList[i] < sortList[i - 1]){
					temp = sortList[i];
					sortList[i] = sortList[i - 1];
					sortList[i - 1] = temp;
					exchange_count++;
				}
			}
		}
		return exchange_count;
	} bubbleSort {
		int exchange_count = 0;
		int temp = 0;
		for(int j = 0; j < count; j++){
			for(int i = count - 1; i > j; i--){
				if(sortList[i] < sortList[i - 1]){
					temp = sortList[i];
					sortList[i] = sortList[i - 1];
					sortList[i - 1] = temp;
					exchange_count++;
				}
			}
		}
		return exchange_count;
	} int exchange_count = 0; exchange_count 0 int temp = 0; temp 0 for(int j = 0; j < count; j++){
			for(int i = count - 1; i > j; i--){
				if(sortList[i] < sortList[i - 1]){
					temp = sortList[i];
					sortList[i] = sortList[i - 1];
					sortList[i - 1] = temp;
					exchange_count++;
				}
			}
		} int j = 0; int j = 0; j 0 j < count j count j++ j++ j {
			for(int i = count - 1; i > j; i--){
				if(sortList[i] < sortList[i - 1]){
					temp = sortList[i];
					sortList[i] = sortList[i - 1];
					sortList[i - 1] = temp;
					exchange_count++;
				}
			}
		} {
			for(int i = count - 1; i > j; i--){
				if(sortList[i] < sortList[i - 1]){
					temp = sortList[i];
					sortList[i] = sortList[i - 1];
					sortList[i - 1] = temp;
					exchange_count++;
				}
			}
		} for(int i = count - 1; i > j; i--){
				if(sortList[i] < sortList[i - 1]){
					temp = sortList[i];
					sortList[i] = sortList[i - 1];
					sortList[i - 1] = temp;
					exchange_count++;
				}
			} int i = count - 1; int i = count - 1; i count - 1 count 1 i > j i j i-- i-- i {
				if(sortList[i] < sortList[i - 1]){
					temp = sortList[i];
					sortList[i] = sortList[i - 1];
					sortList[i - 1] = temp;
					exchange_count++;
				}
			} {
				if(sortList[i] < sortList[i - 1]){
					temp = sortList[i];
					sortList[i] = sortList[i - 1];
					sortList[i - 1] = temp;
					exchange_count++;
				}
			} if(sortList[i] < sortList[i - 1]){
					temp = sortList[i];
					sortList[i] = sortList[i - 1];
					sortList[i - 1] = temp;
					exchange_count++;
				} sortList[i] < sortList[i - 1] sortList[i] sortList i sortList[i - 1] sortList i - 1 i 1 {
					temp = sortList[i];
					sortList[i] = sortList[i - 1];
					sortList[i - 1] = temp;
					exchange_count++;
				} temp = sortList[i] temp sortList[i] sortList i sortList[i] = sortList[i - 1] sortList[i] sortList i sortList[i - 1] sortList i - 1 i 1 sortList[i - 1] = temp sortList[i - 1] sortList i - 1 i 1 temp exchange_count++ exchange_count return exchange_count; exchange_count int[] sortList sortList int count count public class Main {
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		int count = scanner.nextInt();
		int[] sortList = new int[count];
		for(int i = 0; i < count; i++){
			sortList[i] = scanner.nextInt();
		}
		int anser = bubbleSort(sortList, count);
		for(int i = 0; i < count; i++){
			if(i != count - 1){
				System.out.print(sortList[i] + " ");
			}else{
				System.out.println(sortList[i]);
			}
		}
		System.out.println(anser);
	}

	private static int bubbleSort(int[] sortList, int count){
		int exchange_count = 0;
		int temp = 0;
		for(int j = 0; j < count; j++){
			for(int i = count - 1; i > j; i--){
				if(sortList[i] < sortList[i - 1]){
					temp = sortList[i];
					sortList[i] = sortList[i - 1];
					sortList[i - 1] = temp;
					exchange_count++;
				}
			}
		}
		return exchange_count;
	}
} public class Main {
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		int count = scanner.nextInt();
		int[] sortList = new int[count];
		for(int i = 0; i < count; i++){
			sortList[i] = scanner.nextInt();
		}
		int anser = bubbleSort(sortList, count);
		for(int i = 0; i < count; i++){
			if(i != count - 1){
				System.out.print(sortList[i] + " ");
			}else{
				System.out.println(sortList[i]);
			}
		}
		System.out.println(anser);
	}

	private static int bubbleSort(int[] sortList, int count){
		int exchange_count = 0;
		int temp = 0;
		for(int j = 0; j < count; j++){
			for(int i = count - 1; i > j; i--){
				if(sortList[i] < sortList[i - 1]){
					temp = sortList[i];
					sortList[i] = sortList[i - 1];
					sortList[i - 1] = temp;
					exchange_count++;
				}
			}
		}
		return exchange_count;
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);
		int count = scanner.nextInt();
		int[] sortList = new int[count];
		for(int i = 0; i < count; i++){
			sortList[i] = scanner.nextInt();
		}
		int anser = bubbleSort(sortList, count);
		for(int i = 0; i < count; i++){
			if(i != count - 1){
				System.out.print(sortList[i] + " ");
			}else{
				System.out.println(sortList[i]);
			}
		}
		System.out.println(anser);
	}

	private static int bubbleSort(int[] sortList, int count){
		int exchange_count = 0;
		int temp = 0;
		for(int j = 0; j < count; j++){
			for(int i = count - 1; i > j; i--){
				if(sortList[i] < sortList[i - 1]){
					temp = sortList[i];
					sortList[i] = sortList[i - 1];
					sortList[i - 1] = temp;
					exchange_count++;
				}
			}
		}
		return exchange_count;
	}
}