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();
		}
		bubbleSort(A,N);
	}

	public static void bubbleSort(int [] A,int N){
		int flag =1;
		int cnt =0;
		while(flag == 1){
			flag = 0;
			for(int j = N-1; j >= 1;j--){
				if(A[j] < A[j-1]){
					int tmp = A[j-1];
					A[j-1] = A[j];
					A[j] = tmp;
					flag =1;
					cnt++;
				}
			}
		}
		show(A);
		System.out.println(cnt);
		return ;
	}
	public static void show(int[] A){
		StringBuilder sb = new StringBuilder();
		for (int i = 0 ; i < A.length; i++){
			sb.append(A[i]);
			if(i != A.length -1){
				sb.append(" ");
			}
		}
		System.out.println(sb); 
	}
	
} 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();
		}
		bubbleSort(A,N);
	}

	public static void bubbleSort(int [] A,int N){
		int flag =1;
		int cnt =0;
		while(flag == 1){
			flag = 0;
			for(int j = N-1; j >= 1;j--){
				if(A[j] < A[j-1]){
					int tmp = A[j-1];
					A[j-1] = A[j];
					A[j] = tmp;
					flag =1;
					cnt++;
				}
			}
		}
		show(A);
		System.out.println(cnt);
		return ;
	}
	public static void show(int[] A){
		StringBuilder sb = new StringBuilder();
		for (int i = 0 ; i < A.length; i++){
			sb.append(A[i]);
			if(i != A.length -1){
				sb.append(" ");
			}
		}
		System.out.println(sb); 
	}
	
} 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();
		}
		bubbleSort(A,N);
	} 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();
		}
		bubbleSort(A,N);
	} 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 bubbleSort(A,N) bubbleSort A N String[] args args public static void bubbleSort(int [] A,int N){
		int flag =1;
		int cnt =0;
		while(flag == 1){
			flag = 0;
			for(int j = N-1; j >= 1;j--){
				if(A[j] < A[j-1]){
					int tmp = A[j-1];
					A[j-1] = A[j];
					A[j] = tmp;
					flag =1;
					cnt++;
				}
			}
		}
		show(A);
		System.out.println(cnt);
		return ;
	} bubbleSort {
		int flag =1;
		int cnt =0;
		while(flag == 1){
			flag = 0;
			for(int j = N-1; j >= 1;j--){
				if(A[j] < A[j-1]){
					int tmp = A[j-1];
					A[j-1] = A[j];
					A[j] = tmp;
					flag =1;
					cnt++;
				}
			}
		}
		show(A);
		System.out.println(cnt);
		return ;
	} int flag =1; flag 1 int cnt =0; cnt 0 while(flag == 1){
			flag = 0;
			for(int j = N-1; j >= 1;j--){
				if(A[j] < A[j-1]){
					int tmp = A[j-1];
					A[j-1] = A[j];
					A[j] = tmp;
					flag =1;
					cnt++;
				}
			}
		} flag == 1 flag 1 {
			flag = 0;
			for(int j = N-1; j >= 1;j--){
				if(A[j] < A[j-1]){
					int tmp = A[j-1];
					A[j-1] = A[j];
					A[j] = tmp;
					flag =1;
					cnt++;
				}
			}
		} flag = 0 flag 0 for(int j = N-1; j >= 1;j--){
				if(A[j] < A[j-1]){
					int tmp = A[j-1];
					A[j-1] = A[j];
					A[j] = tmp;
					flag =1;
					cnt++;
				}
			} int j = N-1; int j = N-1; j N-1 N 1 j >= 1 j 1 j-- j-- j {
				if(A[j] < A[j-1]){
					int tmp = A[j-1];
					A[j-1] = A[j];
					A[j] = tmp;
					flag =1;
					cnt++;
				}
			} {
				if(A[j] < A[j-1]){
					int tmp = A[j-1];
					A[j-1] = A[j];
					A[j] = tmp;
					flag =1;
					cnt++;
				}
			} if(A[j] < A[j-1]){
					int tmp = A[j-1];
					A[j-1] = A[j];
					A[j] = tmp;
					flag =1;
					cnt++;
				} A[j] < A[j-1] A[j] A j A[j-1] A j-1 j 1 {
					int tmp = A[j-1];
					A[j-1] = A[j];
					A[j] = tmp;
					flag =1;
					cnt++;
				} int tmp = A[j-1]; tmp A[j-1] A j-1 j 1 A[j-1] = A[j] A[j-1] A j-1 j 1 A[j] A j A[j] = tmp A[j] A j tmp flag =1 flag 1 cnt++ cnt show(A) show A System.out.println(cnt) System.out.println System.out System System.out cnt return ; int [] A A int N N public static void show(int[] A){
		StringBuilder sb = new StringBuilder();
		for (int i = 0 ; i < A.length; i++){
			sb.append(A[i]);
			if(i != A.length -1){
				sb.append(" ");
			}
		}
		System.out.println(sb); 
	} show {
		StringBuilder sb = new StringBuilder();
		for (int i = 0 ; i < A.length; i++){
			sb.append(A[i]);
			if(i != A.length -1){
				sb.append(" ");
			}
		}
		System.out.println(sb); 
	} StringBuilder sb = new StringBuilder(); sb new StringBuilder() for (int i = 0 ; i < A.length; i++){
			sb.append(A[i]);
			if(i != A.length -1){
				sb.append(" ");
			}
		} int i = 0 ; int i = 0 ; i 0 i < A.length i A.length A A.length i++ i++ i {
			sb.append(A[i]);
			if(i != A.length -1){
				sb.append(" ");
			}
		} {
			sb.append(A[i]);
			if(i != A.length -1){
				sb.append(" ");
			}
		} sb.append(A[i]) sb.append sb A[i] A i if(i != A.length -1){
				sb.append(" ");
			} i != A.length -1 i A.length -1 A.length A A.length 1 {
				sb.append(" ");
			} sb.append(" ") sb.append sb " " System.out.println(sb) System.out.println System.out System System.out sb int[] A A 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();
		}
		bubbleSort(A,N);
	}

	public static void bubbleSort(int [] A,int N){
		int flag =1;
		int cnt =0;
		while(flag == 1){
			flag = 0;
			for(int j = N-1; j >= 1;j--){
				if(A[j] < A[j-1]){
					int tmp = A[j-1];
					A[j-1] = A[j];
					A[j] = tmp;
					flag =1;
					cnt++;
				}
			}
		}
		show(A);
		System.out.println(cnt);
		return ;
	}
	public static void show(int[] A){
		StringBuilder sb = new StringBuilder();
		for (int i = 0 ; i < A.length; i++){
			sb.append(A[i]);
			if(i != A.length -1){
				sb.append(" ");
			}
		}
		System.out.println(sb); 
	}
	
} 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();
		}
		bubbleSort(A,N);
	}

	public static void bubbleSort(int [] A,int N){
		int flag =1;
		int cnt =0;
		while(flag == 1){
			flag = 0;
			for(int j = N-1; j >= 1;j--){
				if(A[j] < A[j-1]){
					int tmp = A[j-1];
					A[j-1] = A[j];
					A[j] = tmp;
					flag =1;
					cnt++;
				}
			}
		}
		show(A);
		System.out.println(cnt);
		return ;
	}
	public static void show(int[] A){
		StringBuilder sb = new StringBuilder();
		for (int i = 0 ; i < A.length; i++){
			sb.append(A[i]);
			if(i != A.length -1){
				sb.append(" ");
			}
		}
		System.out.println(sb); 
	}
	
} 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();
		}
		bubbleSort(A,N);
	}

	public static void bubbleSort(int [] A,int N){
		int flag =1;
		int cnt =0;
		while(flag == 1){
			flag = 0;
			for(int j = N-1; j >= 1;j--){
				if(A[j] < A[j-1]){
					int tmp = A[j-1];
					A[j-1] = A[j];
					A[j] = tmp;
					flag =1;
					cnt++;
				}
			}
		}
		show(A);
		System.out.println(cnt);
		return ;
	}
	public static void show(int[] A){
		StringBuilder sb = new StringBuilder();
		for (int i = 0 ; i < A.length; i++){
			sb.append(A[i]);
			if(i != A.length -1){
				sb.append(" ");
			}
		}
		System.out.println(sb); 
	}
	
}