def print_list(A):
    print(*A, sep=" ")

def swap(a, b):
    return b, a

def bubble_sort(A, n):
    flg = 1 #逆の隣接要素が存在する
    i = 0
    replace_num = 0
    while flg:
        flg = 0
        for j in range(n-1, i, -1):
            if A[j-1] > A[j]:
                A[j-1], A[j] = swap(A[j-1], A[j])
                flg = 1
                replace_num += 1 #交換回数
        i += 1
    print_list(A)
    print(replace_num)

n = int(input())
A = list(map(int,input().split()))

bubble_sort(A, n) def print_list(A):
    print(*A, sep=" ") print_list print(*A, sep=" ") print *A A sep=" " sep " " A A def swap(a, b):
    return b, a swap return b, a return b, a b a a a b b def bubble_sort(A, n):
    flg = 1 #逆の隣接要素が存在する
    i = 0
    replace_num = 0
    while flg:
        flg = 0
        for j in range(n-1, i, -1):
            if A[j-1] > A[j]:
                A[j-1], A[j] = swap(A[j-1], A[j])
                flg = 1
                replace_num += 1 #交換回数
        i += 1
    print_list(A)
    print(replace_num) bubble_sort flg = 1 flg 1 i = 0 i 0 replace_num = 0 replace_num 0 while flg:
        flg = 0
        for j in range(n-1, i, -1):
            if A[j-1] > A[j]:
                A[j-1], A[j] = swap(A[j-1], A[j])
                flg = 1
                replace_num += 1 #交換回数
        i += 1
     flg flg = 0 flg 0 for j in range(n-1, i, -1):
            if A[j-1] > A[j]:
                A[j-1], A[j] = swap(A[j-1], A[j])
                flg = 1
                replace_num += 1 #交換回数
         j range(n-1, i, -1) range n-1 n 1 i -1 if A[j-1] > A[j]:
                A[j-1], A[j] = swap(A[j-1], A[j])
                flg = 1
                replace_num += 1 #交換回数
         A[j-1] > A[j] A[j-1] A j-1 j 1 A[j] A j A[j-1], A[j] = swap(A[j-1], A[j]) A[j-1] A j-1 j 1 swap(A[j-1], A[j]) swap A[j-1] A j-1 j 1 A[j] A j A[j] A j flg = 1 flg 1 replace_num += 1 replace_num 1 i += 1 i 1 print_list(A) print_list A print(replace_num) print replace_num A A n n n = int(input()) n int(input()) int input() input A = list(map(int,input().split())) A list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split bubble_sort(A, n) bubble_sort A n def swap(a, b):
    return b, a def swap(a, b):
    return b, a swap A = list(map(int,input().split())) list(map(int,input().split())) A n = int(input()) int(input()) n def print_list(A):
    print(*A, sep=" ") def print_list(A):
    print(*A, sep=" ") print_list def bubble_sort(A, n):
    flg = 1 #逆の隣接要素が存在する
    i = 0
    replace_num = 0
    while flg:
        flg = 0
        for j in range(n-1, i, -1):
            if A[j-1] > A[j]:
                A[j-1], A[j] = swap(A[j-1], A[j])
                flg = 1
                replace_num += 1 #交換回数
        i += 1
    print_list(A)
    print(replace_num) def bubble_sort(A, n):
    flg = 1 #逆の隣接要素が存在する
    i = 0
    replace_num = 0
    while flg:
        flg = 0
        for j in range(n-1, i, -1):
            if A[j-1] > A[j]:
                A[j-1], A[j] = swap(A[j-1], A[j])
                flg = 1
                replace_num += 1 #交換回数
        i += 1
    print_list(A)
    print(replace_num) bubble_sort
def print_list(A):
    print(*A, sep=" ")

def swap(a, b):
    return b, a

def bubble_sort(A, n):
    flg = 1 #逆の隣接要素が存在する
    i = 0
    replace_num = 0
    while flg:
        flg = 0
        for j in range(n-1, i, -1):
            if A[j-1] > A[j]:
                A[j-1], A[j] = swap(A[j-1], A[j])
                flg = 1
                replace_num += 1 #交換回数
        i += 1
    print_list(A)
    print(replace_num)

n = int(input())
A = list(map(int,input().split()))

bubble_sort(A, n)
