def main():

    N = int(eval(input()))

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



    A, count = bubbleSort(A, N)



    print((' '.join([str(a) for a in A])))

    print(count)



def bubbleSort(A, N):

    flag = True

    count = 0



    while flag:

        flag = False



        for j in range(N-1, 0, -1):

            if A[j] < A[j-1]:

                A[j], A[j-1] = A[j-1], A[j]

                count += 1

                flag = True



    return A, count



if __name__ == '__main__':

    main()
def bubble_sort(nums, n):

    c = 0



    loop = True

    while loop:

        loop = False



        for j in range(n-1, 0, -1):

            if nums[j] < nums[j-1]:

                nums[j], nums[j-1] = nums[j-1], nums[j]

                loop = True

                c += 1



    return (c, nums)



def main():

    n = int(eval(input()))

    nums = [int(x) for x in input().split()]



    c, nums = bubble_sort(nums, n)



    print((" ".join([str(x) for x in nums])))

    print(c)



if __name__ == '__main__':

    main()



def main():
    N = int(eval(input()))
    A = list(map(int, input().split(" ")))
    A, count = bubbleSort(A, N)
    print((" ".join([str(a) for a in A])))
    print(count)


def bubbleSort(A, N):
    flag = True
    count = 0
    while flag:
        flag = False
        for j in range(N - 1, 0, -1):
            if A[j] < A[j - 1]:
                A[j], A[j - 1] = A[j - 1], A[j]
                count += 1
                flag = True
    return A, count


if __name__ == "__main__":
    main()

def bubble_sort(nums, n):
    c = 0
    loop = True
    while loop:
        loop = False
        for j in range(n - 1, 0, -1):
            if nums[j] < nums[j - 1]:
                nums[j], nums[j - 1] = nums[j - 1], nums[j]
                loop = True
                c += 1
    return (c, nums)


def main():
    n = int(eval(input()))
    nums = [int(x) for x in input().split()]
    c, nums = bubble_sort(nums, n)
    print((" ".join([str(x) for x in nums])))
    print(c)


if __name__ == "__main__":
    main()

-def main(): -    N = int(eval(input())) -    A = list(map(int, input().split(" "))) -    A, count = bubbleSort(A, N) -    print((" ".join([str(a) for a in A]))) -    print(count) +def bubble_sort(nums, n): +    c = 0 +    loop = True +    while loop: +        loop = False +        for j in range(n - 1, 0, -1): +            if nums[j] < nums[j - 1]: +                nums[j], nums[j - 1] = nums[j - 1], nums[j] +                loop = True +                c += 1 +    return (c, nums) -def bubbleSort(A, N): -    flag = True -    count = 0 -    while flag: -        flag = False -        for j in range(N - 1, 0, -1): -            if A[j] < A[j - 1]: -                A[j], A[j - 1] = A[j - 1], A[j] -                count += 1 -                flag = True -    return A, count +def main(): +    n = int(eval(input())) +    nums = [int(x) for x in input().split()] +    c, nums = bubble_sort(nums, n) +    print((" ".join([str(x) for x in nums]))) +    print(c)