import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO ?????????????????????????????????????????????
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		
		String[] strArray = br.readLine().split(" ");
		int[] array = new int[n];
		for(int i = 0; i < n; i++){
			array[i] = Integer.parseInt(strArray[i]);
		}
		
		int count = bubbleSor(array, n);
		
		printArray(array);
		System.out.println(count);
		
	}
	
	static int bubbleSor(int[] array, int n){
		int flag = 1;
		int count = 0;
		while(flag == 1){
			flag = 0;
			for(int j = n - 1; j >= 1; j--){
				if(array[j] < array[j - 1]){
					int tmp = array[j];
					array[j]  = array[j - 1];
					array[j - 1] = tmp;
					flag = 1;
					count++;
				}
			}
		}
		
		return count;
	}
	
	static void printArray(int[] array){
		for(int i = 0; i < array.length ;i++){
			System.out.print(array[i]);
			if(i != array.length - 1){
				System.out.print(" ");
			}
		}
		System.out.println();
	}

} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO ?????????????????????????????????????????????
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		
		String[] strArray = br.readLine().split(" ");
		int[] array = new int[n];
		for(int i = 0; i < n; i++){
			array[i] = Integer.parseInt(strArray[i]);
		}
		
		int count = bubbleSor(array, n);
		
		printArray(array);
		System.out.println(count);
		
	}
	
	static int bubbleSor(int[] array, int n){
		int flag = 1;
		int count = 0;
		while(flag == 1){
			flag = 0;
			for(int j = n - 1; j >= 1; j--){
				if(array[j] < array[j - 1]){
					int tmp = array[j];
					array[j]  = array[j - 1];
					array[j - 1] = tmp;
					flag = 1;
					count++;
				}
			}
		}
		
		return count;
	}
	
	static void printArray(int[] array){
		for(int i = 0; i < array.length ;i++){
			System.out.print(array[i]);
			if(i != array.length - 1){
				System.out.print(" ");
			}
		}
		System.out.println();
	}

} Main public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO ?????????????????????????????????????????????
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		
		String[] strArray = br.readLine().split(" ");
		int[] array = new int[n];
		for(int i = 0; i < n; i++){
			array[i] = Integer.parseInt(strArray[i]);
		}
		
		int count = bubbleSor(array, n);
		
		printArray(array);
		System.out.println(count);
		
	} main {
		// TODO ?????????????????????????????????????????????
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		
		String[] strArray = br.readLine().split(" ");
		int[] array = new int[n];
		for(int i = 0; i < n; i++){
			array[i] = Integer.parseInt(strArray[i]);
		}
		
		int count = bubbleSor(array, n);
		
		printArray(array);
		System.out.println(count);
		
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int n = Integer.parseInt(br.readLine()); n Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br String[] strArray = br.readLine().split(" "); strArray br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int[] array = new int[n]; array new int[n] n for(int i = 0; i < n; i++){
			array[i] = Integer.parseInt(strArray[i]);
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			array[i] = Integer.parseInt(strArray[i]);
		} {
			array[i] = Integer.parseInt(strArray[i]);
		} array[i] = Integer.parseInt(strArray[i]) array[i] array i Integer.parseInt(strArray[i]) Integer.parseInt Integer strArray[i] strArray i int count = bubbleSor(array, n); count bubbleSor(array, n) bubbleSor array n printArray(array) printArray array System.out.println(count) System.out.println System.out System System.out count String[] args args static int bubbleSor(int[] array, int n){
		int flag = 1;
		int count = 0;
		while(flag == 1){
			flag = 0;
			for(int j = n - 1; j >= 1; j--){
				if(array[j] < array[j - 1]){
					int tmp = array[j];
					array[j]  = array[j - 1];
					array[j - 1] = tmp;
					flag = 1;
					count++;
				}
			}
		}
		
		return count;
	} bubbleSor {
		int flag = 1;
		int count = 0;
		while(flag == 1){
			flag = 0;
			for(int j = n - 1; j >= 1; j--){
				if(array[j] < array[j - 1]){
					int tmp = array[j];
					array[j]  = array[j - 1];
					array[j - 1] = tmp;
					flag = 1;
					count++;
				}
			}
		}
		
		return count;
	} int flag = 1; flag 1 int count = 0; count 0 while(flag == 1){
			flag = 0;
			for(int j = n - 1; j >= 1; j--){
				if(array[j] < array[j - 1]){
					int tmp = array[j];
					array[j]  = array[j - 1];
					array[j - 1] = tmp;
					flag = 1;
					count++;
				}
			}
		} flag == 1 flag 1 {
			flag = 0;
			for(int j = n - 1; j >= 1; j--){
				if(array[j] < array[j - 1]){
					int tmp = array[j];
					array[j]  = array[j - 1];
					array[j - 1] = tmp;
					flag = 1;
					count++;
				}
			}
		} flag = 0 flag 0 for(int j = n - 1; j >= 1; j--){
				if(array[j] < array[j - 1]){
					int tmp = array[j];
					array[j]  = array[j - 1];
					array[j - 1] = tmp;
					flag = 1;
					count++;
				}
			} int j = n - 1; int j = n - 1; j n - 1 n 1 j >= 1 j 1 j-- j-- j {
				if(array[j] < array[j - 1]){
					int tmp = array[j];
					array[j]  = array[j - 1];
					array[j - 1] = tmp;
					flag = 1;
					count++;
				}
			} {
				if(array[j] < array[j - 1]){
					int tmp = array[j];
					array[j]  = array[j - 1];
					array[j - 1] = tmp;
					flag = 1;
					count++;
				}
			} if(array[j] < array[j - 1]){
					int tmp = array[j];
					array[j]  = array[j - 1];
					array[j - 1] = tmp;
					flag = 1;
					count++;
				} array[j] < array[j - 1] array[j] array j array[j - 1] array j - 1 j 1 {
					int tmp = array[j];
					array[j]  = array[j - 1];
					array[j - 1] = tmp;
					flag = 1;
					count++;
				} int tmp = array[j]; tmp array[j] array j array[j]  = array[j - 1] array[j] array j array[j - 1] array j - 1 j 1 array[j - 1] = tmp array[j - 1] array j - 1 j 1 tmp flag = 1 flag 1 count++ count return count; count int[] array array int n n static void printArray(int[] array){
		for(int i = 0; i < array.length ;i++){
			System.out.print(array[i]);
			if(i != array.length - 1){
				System.out.print(" ");
			}
		}
		System.out.println();
	} printArray {
		for(int i = 0; i < array.length ;i++){
			System.out.print(array[i]);
			if(i != array.length - 1){
				System.out.print(" ");
			}
		}
		System.out.println();
	} for(int i = 0; i < array.length ;i++){
			System.out.print(array[i]);
			if(i != array.length - 1){
				System.out.print(" ");
			}
		} int i = 0; int i = 0; i 0 i < array.length i array.length array array.length i++ i++ i {
			System.out.print(array[i]);
			if(i != array.length - 1){
				System.out.print(" ");
			}
		} {
			System.out.print(array[i]);
			if(i != array.length - 1){
				System.out.print(" ");
			}
		} System.out.print(array[i]) System.out.print System.out System System.out array[i] array i if(i != array.length - 1){
				System.out.print(" ");
			} i != array.length - 1 i array.length - 1 array.length array array.length 1 {
				System.out.print(" ");
			} System.out.print(" ") System.out.print System.out System System.out " " System.out.println() System.out.println System.out System System.out int[] array array public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO ?????????????????????????????????????????????
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		
		String[] strArray = br.readLine().split(" ");
		int[] array = new int[n];
		for(int i = 0; i < n; i++){
			array[i] = Integer.parseInt(strArray[i]);
		}
		
		int count = bubbleSor(array, n);
		
		printArray(array);
		System.out.println(count);
		
	}
	
	static int bubbleSor(int[] array, int n){
		int flag = 1;
		int count = 0;
		while(flag == 1){
			flag = 0;
			for(int j = n - 1; j >= 1; j--){
				if(array[j] < array[j - 1]){
					int tmp = array[j];
					array[j]  = array[j - 1];
					array[j - 1] = tmp;
					flag = 1;
					count++;
				}
			}
		}
		
		return count;
	}
	
	static void printArray(int[] array){
		for(int i = 0; i < array.length ;i++){
			System.out.print(array[i]);
			if(i != array.length - 1){
				System.out.print(" ");
			}
		}
		System.out.println();
	}

} public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO ?????????????????????????????????????????????
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		
		String[] strArray = br.readLine().split(" ");
		int[] array = new int[n];
		for(int i = 0; i < n; i++){
			array[i] = Integer.parseInt(strArray[i]);
		}
		
		int count = bubbleSor(array, n);
		
		printArray(array);
		System.out.println(count);
		
	}
	
	static int bubbleSor(int[] array, int n){
		int flag = 1;
		int count = 0;
		while(flag == 1){
			flag = 0;
			for(int j = n - 1; j >= 1; j--){
				if(array[j] < array[j - 1]){
					int tmp = array[j];
					array[j]  = array[j - 1];
					array[j - 1] = tmp;
					flag = 1;
					count++;
				}
			}
		}
		
		return count;
	}
	
	static void printArray(int[] array){
		for(int i = 0; i < array.length ;i++){
			System.out.print(array[i]);
			if(i != array.length - 1){
				System.out.print(" ");
			}
		}
		System.out.println();
	}

} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO ?????????????????????????????????????????????
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		
		String[] strArray = br.readLine().split(" ");
		int[] array = new int[n];
		for(int i = 0; i < n; i++){
			array[i] = Integer.parseInt(strArray[i]);
		}
		
		int count = bubbleSor(array, n);
		
		printArray(array);
		System.out.println(count);
		
	}
	
	static int bubbleSor(int[] array, int n){
		int flag = 1;
		int count = 0;
		while(flag == 1){
			flag = 0;
			for(int j = n - 1; j >= 1; j--){
				if(array[j] < array[j - 1]){
					int tmp = array[j];
					array[j]  = array[j - 1];
					array[j - 1] = tmp;
					flag = 1;
					count++;
				}
			}
		}
		
		return count;
	}
	
	static void printArray(int[] array){
		for(int i = 0; i < array.length ;i++){
			System.out.print(array[i]);
			if(i != array.length - 1){
				System.out.print(" ");
			}
		}
		System.out.println();
	}

}