import java.util.Scanner;

public class Main {
	private static Scanner sc;
	public static void main(String[] args){
		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();
		}
		int ans=bubbleSort(a,n);
		write(a,n);
		System.out.println(ans);
	}
	static void write(int[] a,int n){
		for(int i=0;i<n-1;i++){
			System.out.print(a[i]+" ");
		}
		System.out.println(a[n-1]);
	}
	static int bubbleSort(int[] a, int n){
		boolean flag=true;
		int i=0;
		int count=0;
		while(flag){
			flag=false;
			for(int j=n-1;j>i;j--){
				if(a[j]<a[j-1]){
					int temp=a[j];
					a[j]=a[j-1];
					a[j-1]=temp;
					flag=true;
					count++;
				}
			}
			i++;
		}
		return count;
	}

} import java.util.Scanner; Scanner java.util public class Main {
	private static Scanner sc;
	public static void main(String[] args){
		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();
		}
		int ans=bubbleSort(a,n);
		write(a,n);
		System.out.println(ans);
	}
	static void write(int[] a,int n){
		for(int i=0;i<n-1;i++){
			System.out.print(a[i]+" ");
		}
		System.out.println(a[n-1]);
	}
	static int bubbleSort(int[] a, int n){
		boolean flag=true;
		int i=0;
		int count=0;
		while(flag){
			flag=false;
			for(int j=n-1;j>i;j--){
				if(a[j]<a[j-1]){
					int temp=a[j];
					a[j]=a[j-1];
					a[j-1]=temp;
					flag=true;
					count++;
				}
			}
			i++;
		}
		return count;
	}

} Main private static Scanner sc; sc public static void main(String[] args){
		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();
		}
		int ans=bubbleSort(a,n);
		write(a,n);
		System.out.println(ans);
	} main {
		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();
		}
		int ans=bubbleSort(a,n);
		write(a,n);
		System.out.println(ans);
	} 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 int ans=bubbleSort(a,n); ans bubbleSort(a,n) bubbleSort a n write(a,n) write a n System.out.println(ans) System.out.println System.out System System.out ans String[] args args static void write(int[] a,int n){
		for(int i=0;i<n-1;i++){
			System.out.print(a[i]+" ");
		}
		System.out.println(a[n-1]);
	} write {
		for(int i=0;i<n-1;i++){
			System.out.print(a[i]+" ");
		}
		System.out.println(a[n-1]);
	} for(int i=0;i<n-1;i++){
			System.out.print(a[i]+" ");
		} int i=0; int i=0; i 0 i<n-1 i n-1 n 1 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(a[n-1]) System.out.println System.out System System.out a[n-1] a n-1 n 1 int[] a a int n n static int bubbleSort(int[] a, int n){
		boolean flag=true;
		int i=0;
		int count=0;
		while(flag){
			flag=false;
			for(int j=n-1;j>i;j--){
				if(a[j]<a[j-1]){
					int temp=a[j];
					a[j]=a[j-1];
					a[j-1]=temp;
					flag=true;
					count++;
				}
			}
			i++;
		}
		return count;
	} bubbleSort {
		boolean flag=true;
		int i=0;
		int count=0;
		while(flag){
			flag=false;
			for(int j=n-1;j>i;j--){
				if(a[j]<a[j-1]){
					int temp=a[j];
					a[j]=a[j-1];
					a[j-1]=temp;
					flag=true;
					count++;
				}
			}
			i++;
		}
		return count;
	} boolean flag=true; flag true int i=0; i 0 int count=0; count 0 while(flag){
			flag=false;
			for(int j=n-1;j>i;j--){
				if(a[j]<a[j-1]){
					int temp=a[j];
					a[j]=a[j-1];
					a[j-1]=temp;
					flag=true;
					count++;
				}
			}
			i++;
		} flag {
			flag=false;
			for(int j=n-1;j>i;j--){
				if(a[j]<a[j-1]){
					int temp=a[j];
					a[j]=a[j-1];
					a[j-1]=temp;
					flag=true;
					count++;
				}
			}
			i++;
		} flag=false flag false for(int j=n-1;j>i;j--){
				if(a[j]<a[j-1]){
					int temp=a[j];
					a[j]=a[j-1];
					a[j-1]=temp;
					flag=true;
					count++;
				}
			} int j=n-1; int j=n-1; j n-1 n 1 j>i j i j-- j-- j {
				if(a[j]<a[j-1]){
					int temp=a[j];
					a[j]=a[j-1];
					a[j-1]=temp;
					flag=true;
					count++;
				}
			} {
				if(a[j]<a[j-1]){
					int temp=a[j];
					a[j]=a[j-1];
					a[j-1]=temp;
					flag=true;
					count++;
				}
			} if(a[j]<a[j-1]){
					int temp=a[j];
					a[j]=a[j-1];
					a[j-1]=temp;
					flag=true;
					count++;
				} a[j]<a[j-1] a[j] a j a[j-1] a j-1 j 1 {
					int temp=a[j];
					a[j]=a[j-1];
					a[j-1]=temp;
					flag=true;
					count++;
				} int temp=a[j]; temp a[j] a j a[j]=a[j-1] a[j] a j a[j-1] a j-1 j 1 a[j-1]=temp a[j-1] a j-1 j 1 temp flag=true flag true count++ count i++ i return count; count int[] a a int n n public class Main {
	private static Scanner sc;
	public static void main(String[] args){
		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();
		}
		int ans=bubbleSort(a,n);
		write(a,n);
		System.out.println(ans);
	}
	static void write(int[] a,int n){
		for(int i=0;i<n-1;i++){
			System.out.print(a[i]+" ");
		}
		System.out.println(a[n-1]);
	}
	static int bubbleSort(int[] a, int n){
		boolean flag=true;
		int i=0;
		int count=0;
		while(flag){
			flag=false;
			for(int j=n-1;j>i;j--){
				if(a[j]<a[j-1]){
					int temp=a[j];
					a[j]=a[j-1];
					a[j-1]=temp;
					flag=true;
					count++;
				}
			}
			i++;
		}
		return count;
	}

} public class Main {
	private static Scanner sc;
	public static void main(String[] args){
		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();
		}
		int ans=bubbleSort(a,n);
		write(a,n);
		System.out.println(ans);
	}
	static void write(int[] a,int n){
		for(int i=0;i<n-1;i++){
			System.out.print(a[i]+" ");
		}
		System.out.println(a[n-1]);
	}
	static int bubbleSort(int[] a, int n){
		boolean flag=true;
		int i=0;
		int count=0;
		while(flag){
			flag=false;
			for(int j=n-1;j>i;j--){
				if(a[j]<a[j-1]){
					int temp=a[j];
					a[j]=a[j-1];
					a[j-1]=temp;
					flag=true;
					count++;
				}
			}
			i++;
		}
		return count;
	}

} Main
import java.util.Scanner;

public class Main {
	private static Scanner sc;
	public static void main(String[] args){
		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();
		}
		int ans=bubbleSort(a,n);
		write(a,n);
		System.out.println(ans);
	}
	static void write(int[] a,int n){
		for(int i=0;i<n-1;i++){
			System.out.print(a[i]+" ");
		}
		System.out.println(a[n-1]);
	}
	static int bubbleSort(int[] a, int n){
		boolean flag=true;
		int i=0;
		int count=0;
		while(flag){
			flag=false;
			for(int j=n-1;j>i;j--){
				if(a[j]<a[j-1]){
					int temp=a[j];
					a[j]=a[j-1];
					a[j-1]=temp;
					flag=true;
					count++;
				}
			}
			i++;
		}
		return count;
	}

}