def bubble_sort(A):

    count = 0

    for i in reversed(list(range(len(A)))):

        for j in range(i):

            if A[j] > A[j+1]:

                temp = A[j]

                A[j] = A[j+1]

                A[j+1] = temp

                count += 1

    return count



N = int(eval(input()))

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



count = bubble_sort(A)

print((" ".join(map(str,A))))

print(count)
def bubble_sort(A, N):

	count = 0

	flag = 1

	while flag:

		flag = 0

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

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

				temp = A[i]

				A[i] = A[i-1]

				A[i-1] = temp

				count += 1

				flag = 1



	return A, count



N = int(eval(input()))

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



A, count = bubble_sort(A, N)

print((' '.join(map(str, A))))

print(count)

def bubble_sort(A):
    count = 0
    for i in reversed(list(range(len(A)))):
        for j in range(i):
            if A[j] > A[j + 1]:
                temp = A[j]
                A[j] = A[j + 1]
                A[j + 1] = temp
                count += 1
    return count


N = int(eval(input()))
A = list(map(int, input().split()))
count = bubble_sort(A)
print((" ".join(map(str, A))))
print(count)

def bubble_sort(A, N):
    count = 0
    flag = 1
    while flag:
        flag = 0
        for i in range(N - 1, 0, -1):
            if A[i] < A[i - 1]:
                temp = A[i]
                A[i] = A[i - 1]
                A[i - 1] = temp
                count += 1
                flag = 1
    return A, count


N = int(eval(input()))
A = list(map(int, input().split()))
A, count = bubble_sort(A, N)
print((" ".join(map(str, A))))
print(count)

-def bubble_sort(A): +def bubble_sort(A, N): -    for i in reversed(list(range(len(A)))): -        for j in range(i): -            if A[j] > A[j + 1]: -                temp = A[j] -                A[j] = A[j + 1] -                A[j + 1] = temp +    flag = 1 +    while flag: +        flag = 0 +        for i in range(N - 1, 0, -1): +            if A[i] < A[i - 1]: +                temp = A[i] +                A[i] = A[i - 1] +                A[i - 1] = temp -    return count +                flag = 1 +    return A, count -count = bubble_sort(A) +A, count = bubble_sort(A, N)