#include<stdio.h>

int main(void){
	int X,Y, N;
	int A[1000];
	int i=0;
	while((scanf("%d",&X))!='\n'){
		N=X; //????´???°
	}
	while((scanf("%d",&Y))!=EOF){
		A[i] = Y;
		i++;
	}
	int flag=1;
	int change=0; //???????????°
	while(flag){
		flag=0;
		for(int j=N-1; i>=1; j--){
			if(A[j]<A[j-1]){
				int stock = A[j];
				A[j] = A[j-1];
				A[j-1] = stock;
				flag=1; 
				change++;
			}
		}
	}
	for(i=0; i<N; i++){
		printf("%d ",A[i]);
	}
	printf("\n");
	printf("%d",change);
	return 0;
}