#include<stdio.h>
#include<stdint.h>
#include<inttypes.h>

uint8_t bubble_sort(uint8_t *a, uint8_t n)
{
    uint8_t n_loop        = 0,
            flag_unsorted = 1;
    while(flag_unsorted){
        flag_unsorted = 0;
        for(uint8_t i=n-1; i>0; --i){
            if(a[i] < a[i-1]){
                uint8_t work  = a[i  ];
                a[i  ]        = a[i-1];
                a[i-1]        = work  ;
                flag_unsorted = 1     ;
                ++n_loop              ;
            }
        }
    }

    return n_loop;
}

int main()
{
    uint8_t n;
    scanf("%"PRIu8, &n);

    uint8_t a[n];
    for(uint8_t i=0; i<n; ++i)
        scanf("%"PRIu8, a + i);

    uint8_t n_loop = bubble_sort(a, n);

    printf("%"PRIu8, a[0]);
    for(uint8_t i=1; i<n; ++i)
        printf(" %"PRIu8, a[i]);
    printf("\n%"PRIu8"\n", n_loop);

    return 0;
}