def bubble_sort(array):
    count = 0
    n = len(array)
    for i in range(n-1):
        for j in range(n-1, i, -1):
            if array[j] < array[j-1]:
                tmp = array[j]
                array[j] = array[j-1]
                array[j-1] = tmp
                count += 1

    print(" ".join(map(str,array)))
    print(count)

times = int(input())
array = list(map(int,input().split()))

bubble_sort(array) def bubble_sort(array):
    count = 0
    n = len(array)
    for i in range(n-1):
        for j in range(n-1, i, -1):
            if array[j] < array[j-1]:
                tmp = array[j]
                array[j] = array[j-1]
                array[j-1] = tmp
                count += 1

    print(" ".join(map(str,array)))
    print(count) bubble_sort count = 0 count 0 n = len(array) n len(array) len array for i in range(n-1):
        for j in range(n-1, i, -1):
            if array[j] < array[j-1]:
                tmp = array[j]
                array[j] = array[j-1]
                array[j-1] = tmp
                count += 1

     i range(n-1) range n-1 n 1 for j in range(n-1, i, -1):
            if array[j] < array[j-1]:
                tmp = array[j]
                array[j] = array[j-1]
                array[j-1] = tmp
                count += 1

     j range(n-1, i, -1) range n-1 n 1 i -1 if array[j] < array[j-1]:
                tmp = array[j]
                array[j] = array[j-1]
                array[j-1] = tmp
                count += 1

     array[j] < array[j-1] array[j] array j array[j-1] array j-1 j 1 tmp = array[j] tmp array[j] array j array[j] = array[j-1] array[j] array j array[j-1] array j-1 j 1 array[j-1] = tmp array[j-1] array j-1 j 1 tmp count += 1 count 1 print(" ".join(map(str,array))) print " ".join(map(str,array)) " ".join " " join map(str,array) map str array print(count) print count array array times = int(input()) times int(input()) int input() input array = list(map(int,input().split())) array list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split bubble_sort(array) bubble_sort array times = int(input()) int(input()) times array = list(map(int,input().split())) list(map(int,input().split())) array def bubble_sort(array):
    count = 0
    n = len(array)
    for i in range(n-1):
        for j in range(n-1, i, -1):
            if array[j] < array[j-1]:
                tmp = array[j]
                array[j] = array[j-1]
                array[j-1] = tmp
                count += 1

    print(" ".join(map(str,array)))
    print(count) def bubble_sort(array):
    count = 0
    n = len(array)
    for i in range(n-1):
        for j in range(n-1, i, -1):
            if array[j] < array[j-1]:
                tmp = array[j]
                array[j] = array[j-1]
                array[j-1] = tmp
                count += 1

    print(" ".join(map(str,array)))
    print(count) bubble_sort
def bubble_sort(array):
    count = 0
    n = len(array)
    for i in range(n-1):
        for j in range(n-1, i, -1):
            if array[j] < array[j-1]:
                tmp = array[j]
                array[j] = array[j-1]
                array[j-1] = tmp
                count += 1

    print(" ".join(map(str,array)))
    print(count)

times = int(input())
array = list(map(int,input().split()))

bubble_sort(array)