def bubble_sort(array):
    isEnd = False
    count_swap = 0
    while isEnd is False:
        isEnd = True
        for j in reversed(range(1,len(array))):
            if array[j - 1] > array[j]:
                tmp = array[j - 1]
                array[j - 1] = array[j]
                array[j] = tmp
                count_swap += 1
                isEnd = False
    return count_swap

def print_array(array):
     print(str(array)[1:-1].replace(', ', ' '))

def main():
    N = int(input())
    array = [int(s) for s in input().split(' ')]
    count = bubble_sort(array)
    print_array(array)
    print(count)

if __name__ == '__main__':
    main() def bubble_sort(array):
    isEnd = False
    count_swap = 0
    while isEnd is False:
        isEnd = True
        for j in reversed(range(1,len(array))):
            if array[j - 1] > array[j]:
                tmp = array[j - 1]
                array[j - 1] = array[j]
                array[j] = tmp
                count_swap += 1
                isEnd = False
    return count_swap bubble_sort isEnd = False isEnd False count_swap = 0 count_swap 0 while isEnd is False:
        isEnd = True
        for j in reversed(range(1,len(array))):
            if array[j - 1] > array[j]:
                tmp = array[j - 1]
                array[j - 1] = array[j]
                array[j] = tmp
                count_swap += 1
                isEnd = False
     isEnd is False isEnd False isEnd = True isEnd True for j in reversed(range(1,len(array))):
            if array[j - 1] > array[j]:
                tmp = array[j - 1]
                array[j - 1] = array[j]
                array[j] = tmp
                count_swap += 1
                isEnd = False
     j reversed(range(1,len(array))) reversed range(1,len(array)) range 1 len(array) len array if array[j - 1] > array[j]:
                tmp = array[j - 1]
                array[j - 1] = array[j]
                array[j] = tmp
                count_swap += 1
                isEnd = False
     array[j - 1] > array[j] array[j - 1] array j - 1 j 1 array[j] array j tmp = array[j - 1] tmp array[j - 1] array j - 1 j 1 array[j - 1] = array[j] array[j - 1] array j - 1 j 1 array[j] array j array[j] = tmp array[j] array j tmp count_swap += 1 count_swap 1 isEnd = False isEnd False return count_swap count_swap array array def print_array(array):
     print(str(array)[1:-1].replace(', ', ' ')) print_array print(str(array)[1:-1].replace(', ', ' ')) print str(array)[1:-1].replace(', ', ' ') [1:-1].replace [1:-1] (array) str array 1:-1 1 -1 replace ', ' ' ' array array def main():
    N = int(input())
    array = [int(s) for s in input().split(' ')]
    count = bubble_sort(array)
    print_array(array)
    print(count) main N = int(input()) N int(input()) int input() input int(s) for s in input().split(' ') for s in input().split(' ') s input().split(' ') ().split () input split ' ' for s in input().split(' ') int(s) int s array = [int(s) for s in input().split(' ')] array [int(s) for s in input().split(' ')] count = bubble_sort(array) count bubble_sort(array) bubble_sort array print_array(array) print_array array print(count) print count if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def print_array(array):
     print(str(array)[1:-1].replace(', ', ' ')) def print_array(array):
     print(str(array)[1:-1].replace(', ', ' ')) print_array def main():
    N = int(input())
    array = [int(s) for s in input().split(' ')]
    count = bubble_sort(array)
    print_array(array)
    print(count) def main():
    N = int(input())
    array = [int(s) for s in input().split(' ')]
    count = bubble_sort(array)
    print_array(array)
    print(count) main def bubble_sort(array):
    isEnd = False
    count_swap = 0
    while isEnd is False:
        isEnd = True
        for j in reversed(range(1,len(array))):
            if array[j - 1] > array[j]:
                tmp = array[j - 1]
                array[j - 1] = array[j]
                array[j] = tmp
                count_swap += 1
                isEnd = False
    return count_swap def bubble_sort(array):
    isEnd = False
    count_swap = 0
    while isEnd is False:
        isEnd = True
        for j in reversed(range(1,len(array))):
            if array[j - 1] > array[j]:
                tmp = array[j - 1]
                array[j - 1] = array[j]
                array[j] = tmp
                count_swap += 1
                isEnd = False
    return count_swap bubble_sort
def bubble_sort(array):
    isEnd = False
    count_swap = 0
    while isEnd is False:
        isEnd = True
        for j in reversed(range(1,len(array))):
            if array[j - 1] > array[j]:
                tmp = array[j - 1]
                array[j - 1] = array[j]
                array[j] = tmp
                count_swap += 1
                isEnd = False
    return count_swap

def print_array(array):
     print(str(array)[1:-1].replace(', ', ' '))

def main():
    N = int(input())
    array = [int(s) for s in input().split(' ')]
    count = bubble_sort(array)
    print_array(array)
    print(count)

if __name__ == '__main__':
    main()