def bubbleSort():
    N = int(input())
    lst = list(map(int, input().split()))
    
    flag = 1
    count = 0
    while flag:
        flag = 0
        for i in range(1, N)[::-1]:
            if lst[i-1] > lst[i]:
                lst[i], lst[i-1] = lst[i-1], lst[i]
                count += 1
                flag = 1
    print(*lst)
    print(count)

bubbleSort() def bubbleSort():
    N = int(input())
    lst = list(map(int, input().split()))
    
    flag = 1
    count = 0
    while flag:
        flag = 0
        for i in range(1, N)[::-1]:
            if lst[i-1] > lst[i]:
                lst[i], lst[i-1] = lst[i-1], lst[i]
                count += 1
                flag = 1
    print(*lst)
    print(count) bubbleSort N = int(input()) N int(input()) int input() input lst = list(map(int, input().split())) lst list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split flag = 1 flag 1 count = 0 count 0 while flag:
        flag = 0
        for i in range(1, N)[::-1]:
            if lst[i-1] > lst[i]:
                lst[i], lst[i-1] = lst[i-1], lst[i]
                count += 1
                flag = 1
     flag flag = 0 flag 0 for i in range(1, N)[::-1]:
            if lst[i-1] > lst[i]:
                lst[i], lst[i-1] = lst[i-1], lst[i]
                count += 1
                flag = 1
     i range(1, N)[::-1] (1, N) range 1 N ::-1 -1 if lst[i-1] > lst[i]:
                lst[i], lst[i-1] = lst[i-1], lst[i]
                count += 1
                flag = 1
     lst[i-1] > lst[i] lst[i-1] lst i-1 i 1 lst[i] lst i lst[i], lst[i-1] = lst[i-1], lst[i] lst[i] lst i lst[i-1] lst i-1 i 1 lst[i-1] lst i-1 i 1 lst[i] lst i count += 1 count 1 flag = 1 flag 1 print(*lst) print *lst lst print(count) print count bubbleSort() bubbleSort def bubbleSort():
    N = int(input())
    lst = list(map(int, input().split()))
    
    flag = 1
    count = 0
    while flag:
        flag = 0
        for i in range(1, N)[::-1]:
            if lst[i-1] > lst[i]:
                lst[i], lst[i-1] = lst[i-1], lst[i]
                count += 1
                flag = 1
    print(*lst)
    print(count) def bubbleSort():
    N = int(input())
    lst = list(map(int, input().split()))
    
    flag = 1
    count = 0
    while flag:
        flag = 0
        for i in range(1, N)[::-1]:
            if lst[i-1] > lst[i]:
                lst[i], lst[i-1] = lst[i-1], lst[i]
                count += 1
                flag = 1
    print(*lst)
    print(count) bubbleSort
def bubbleSort():
    N = int(input())
    lst = list(map(int, input().split()))
    
    flag = 1
    count = 0
    while flag:
        flag = 0
        for i in range(1, N)[::-1]:
            if lst[i-1] > lst[i]:
                lst[i], lst[i-1] = lst[i-1], lst[i]
                count += 1
                flag = 1
    print(*lst)
    print(count)

bubbleSort()