#coding:utf-8
#1_2_A
def bubble_sort(ary, n):
    count = 0
    flag = 1
    i = 0
    while flag:
        flag = 0
        for j in reversed(range(i+1, n)):
            if ary[j] < ary[j-1]:
                ary[j], ary[j-1] = ary[j-1], ary[j]
                count += 1
                flag = 1
        i += 1

    print(' '.join(map(str, ary)))
    print(count)

n = int(input())
numbers = list(map(int, input().split()))
bubble_sort(numbers, n) def bubble_sort(ary, n):
    count = 0
    flag = 1
    i = 0
    while flag:
        flag = 0
        for j in reversed(range(i+1, n)):
            if ary[j] < ary[j-1]:
                ary[j], ary[j-1] = ary[j-1], ary[j]
                count += 1
                flag = 1
        i += 1

    print(' '.join(map(str, ary)))
    print(count) bubble_sort count = 0 count 0 flag = 1 flag 1 i = 0 i 0 while flag:
        flag = 0
        for j in reversed(range(i+1, n)):
            if ary[j] < ary[j-1]:
                ary[j], ary[j-1] = ary[j-1], ary[j]
                count += 1
                flag = 1
        i += 1

     flag flag = 0 flag 0 for j in reversed(range(i+1, n)):
            if ary[j] < ary[j-1]:
                ary[j], ary[j-1] = ary[j-1], ary[j]
                count += 1
                flag = 1
         j reversed(range(i+1, n)) reversed range(i+1, n) range i+1 i 1 n if ary[j] < ary[j-1]:
                ary[j], ary[j-1] = ary[j-1], ary[j]
                count += 1
                flag = 1
         ary[j] < ary[j-1] ary[j] ary j ary[j-1] ary j-1 j 1 ary[j], ary[j-1] = ary[j-1], ary[j] ary[j] ary j ary[j-1] ary j-1 j 1 ary[j-1] ary j-1 j 1 ary[j] ary j count += 1 count 1 flag = 1 flag 1 i += 1 i 1 print(' '.join(map(str, ary))) print ' '.join(map(str, ary)) ' '.join ' ' join map(str, ary) map str ary print(count) print count ary ary n n n = int(input()) n int(input()) int input() input numbers = list(map(int, input().split())) numbers list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split bubble_sort(numbers, n) bubble_sort numbers n def bubble_sort(ary, n):
    count = 0
    flag = 1
    i = 0
    while flag:
        flag = 0
        for j in reversed(range(i+1, n)):
            if ary[j] < ary[j-1]:
                ary[j], ary[j-1] = ary[j-1], ary[j]
                count += 1
                flag = 1
        i += 1

    print(' '.join(map(str, ary)))
    print(count) def bubble_sort(ary, n):
    count = 0
    flag = 1
    i = 0
    while flag:
        flag = 0
        for j in reversed(range(i+1, n)):
            if ary[j] < ary[j-1]:
                ary[j], ary[j-1] = ary[j-1], ary[j]
                count += 1
                flag = 1
        i += 1

    print(' '.join(map(str, ary)))
    print(count) bubble_sort n = int(input()) int(input()) n numbers = list(map(int, input().split())) list(map(int, input().split())) numbers
#coding:utf-8
#1_2_A
def bubble_sort(ary, n):
    count = 0
    flag = 1
    i = 0
    while flag:
        flag = 0
        for j in reversed(range(i+1, n)):
            if ary[j] < ary[j-1]:
                ary[j], ary[j-1] = ary[j-1], ary[j]
                count += 1
                flag = 1
        i += 1

    print(' '.join(map(str, ary)))
    print(count)

n = int(input())
numbers = list(map(int, input().split()))
bubble_sort(numbers, n)