def bubble_sort(A, n):
	flag = 1
	i = 0
	count = 0

	while flag:
		flag = 0
		for j in range(n-1, i, -1):
			if A[j] < A[j-1]:
				A[j], A[j-1] = A[j-1], A[j]
				flag = 1
				count += 1

		i += 1
	
	print(' '.join(map(str, A)))
	print(count)


if __name__ == '__main__':
	n = int(input())
	A = list(map(int, input().split()))

	bubble_sort(A, n) def bubble_sort(A, n):
	flag = 1
	i = 0
	count = 0

	while flag:
		flag = 0
		for j in range(n-1, i, -1):
			if A[j] < A[j-1]:
				A[j], A[j-1] = A[j-1], A[j]
				flag = 1
				count += 1

		i += 1
	
	print(' '.join(map(str, A)))
	print(count) bubble_sort flag = 1 flag 1 i = 0 i 0 count = 0 count 0 while flag:
		flag = 0
		for j in range(n-1, i, -1):
			if A[j] < A[j-1]:
				A[j], A[j-1] = A[j-1], A[j]
				flag = 1
				count += 1

		i += 1
	
	 flag flag = 0 flag 0 for j in range(n-1, i, -1):
			if A[j] < A[j-1]:
				A[j], A[j-1] = A[j-1], A[j]
				flag = 1
				count += 1

		 j range(n-1, i, -1) range n-1 n 1 i -1 if A[j] < A[j-1]:
				A[j], A[j-1] = A[j-1], A[j]
				flag = 1
				count += 1

		 A[j] < A[j-1] A[j] A j A[j-1] A j-1 j 1 A[j], A[j-1] = A[j-1], A[j] A[j] A j A[j-1] A j-1 j 1 A[j-1] A j-1 j 1 A[j] A j flag = 1 flag 1 count += 1 count 1 i += 1 i 1 print(' '.join(map(str, A))) print ' '.join(map(str, A)) ' '.join ' ' join map(str, A) map str A print(count) print count A A n n if __name__ == '__main__':
	n = int(input())
	A = list(map(int, input().split()))

	bubble_sort(A, n) __name__ == '__main__' __name__ '__main__' 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 n = int(input()) int(input()) n A = list(map(int, input().split())) list(map(int, input().split())) A def bubble_sort(A, n):
	flag = 1
	i = 0
	count = 0

	while flag:
		flag = 0
		for j in range(n-1, i, -1):
			if A[j] < A[j-1]:
				A[j], A[j-1] = A[j-1], A[j]
				flag = 1
				count += 1

		i += 1
	
	print(' '.join(map(str, A)))
	print(count) def bubble_sort(A, n):
	flag = 1
	i = 0
	count = 0

	while flag:
		flag = 0
		for j in range(n-1, i, -1):
			if A[j] < A[j-1]:
				A[j], A[j-1] = A[j-1], A[j]
				flag = 1
				count += 1

		i += 1
	
	print(' '.join(map(str, A)))
	print(count) bubble_sort
def bubble_sort(A, n):
	flag = 1
	i = 0
	count = 0

	while flag:
		flag = 0
		for j in range(n-1, i, -1):
			if A[j] < A[j-1]:
				A[j], A[j-1] = A[j-1], A[j]
				flag = 1
				count += 1

		i += 1
	
	print(' '.join(map(str, A)))
	print(count)


if __name__ == '__main__':
	n = int(input())
	A = list(map(int, input().split()))

	bubble_sort(A, n)