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();
	}

}
/tmp/hf-datasets-cache/medium/datasets/42704881117311-config-parquet-and-info-qiankunmu-Project_CodeNet-896c9d36/hub/datasets--qiankunmu--Project_CodeNet_Python800_and_Java250/snapshots/c899fde44a77ba1cc14973ed3de064e4ff3aadc7/Project_CodeNet_Java250.tar.gz