#include<stdio.h>


int main(void){
 int flag = 1;
int i,j,n,v,a[10000]; 
int c =0;

    scanf("%d", &n);
     for ( i = 0; i < n; i++ ){ 
scanf("%d", &a[i]);
}
 
 while ( flag ) {
        flag = 0;
        for ( j = n - 1; j >= 1; j-- ) {
            if ( a[j] < a[j - 1] ) {
                v = a[j];
                a[j] = a[j - 1];
                a[j - 1] = v;
                c++;
                flag = 1;
            }
        }
    }
}
   
    for ( i = 0; i < n; i++ ) {
if(i>0){
printf(" ");
}

        printf("%d\n", a[i]);
}
printf("\n");
  
    

    printf("%d\n",c);
    return 0;
}
 