def bubble_sort(array):
	N = len(array)
	count = 0
	
	for i in range(N - 1):
		flag = 0
		
		for j in range(N - 1, i, -1):
			if array[j - 1] > array[j]:
				array[j - 1], array[j] = array[j], array[j - 1]
				count += 1
				flag = 1
				
		if flag == 0:
			break
	
	return array, count
	
n = int(input())
array = [int(i) for i in input().split()]
result = bubble_sort(array)
print(*result[0])
print(result[1]) def bubble_sort(array):
	N = len(array)
	count = 0
	
	for i in range(N - 1):
		flag = 0
		
		for j in range(N - 1, i, -1):
			if array[j - 1] > array[j]:
				array[j - 1], array[j] = array[j], array[j - 1]
				count += 1
				flag = 1
				
		if flag == 0:
			break
	
	return array, count
	 bubble_sort N = len(array) N len(array) len array count = 0 count 0 for i in range(N - 1):
		flag = 0
		
		for j in range(N - 1, i, -1):
			if array[j - 1] > array[j]:
				array[j - 1], array[j] = array[j], array[j - 1]
				count += 1
				flag = 1
				
		if flag == 0:
			break
	
	 i range(N - 1) range N - 1 N 1 flag = 0 flag 0 for j in range(N - 1, i, -1):
			if array[j - 1] > array[j]:
				array[j - 1], array[j] = array[j], array[j - 1]
				count += 1
				flag = 1
				
		 j range(N - 1, i, -1) range N - 1 N 1 i -1 if array[j - 1] > array[j]:
				array[j - 1], array[j] = array[j], array[j - 1]
				count += 1
				flag = 1
				
		 array[j - 1] > array[j] array[j - 1] array j - 1 j 1 array[j] array j array[j - 1], array[j] = array[j], array[j - 1] array[j - 1] array j - 1 j 1 array[j] array j array[j] array j array[j - 1] array j - 1 j 1 count += 1 count 1 flag = 1 flag 1 if flag == 0:
			break
	
	 flag == 0 flag 0 break return array, count return array, count array count array array n = int(input()) n int(input()) int input() input int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i array = [int(i) for i in input().split()] array [int(i) for i in input().split()] result = bubble_sort(array) result bubble_sort(array) bubble_sort array print(*result[0]) print *result[0] result[0] result 0 print(result[1]) print result[1] result 1 def bubble_sort(array):
	N = len(array)
	count = 0
	
	for i in range(N - 1):
		flag = 0
		
		for j in range(N - 1, i, -1):
			if array[j - 1] > array[j]:
				array[j - 1], array[j] = array[j], array[j - 1]
				count += 1
				flag = 1
				
		if flag == 0:
			break
	
	return array, count
	 def bubble_sort(array):
	N = len(array)
	count = 0
	
	for i in range(N - 1):
		flag = 0
		
		for j in range(N - 1, i, -1):
			if array[j - 1] > array[j]:
				array[j - 1], array[j] = array[j], array[j - 1]
				count += 1
				flag = 1
				
		if flag == 0:
			break
	
	return array, count
	 bubble_sort array = [int(i) for i in input().split()] [int(i) for i in input().split()] array n = int(input()) int(input()) n result = bubble_sort(array) bubble_sort(array) result
def bubble_sort(array):
	N = len(array)
	count = 0
	
	for i in range(N - 1):
		flag = 0
		
		for j in range(N - 1, i, -1):
			if array[j - 1] > array[j]:
				array[j - 1], array[j] = array[j], array[j - 1]
				count += 1
				flag = 1
				
		if flag == 0:
			break
	
	return array, count
	
n = int(input())
array = [int(i) for i in input().split()]
result = bubble_sort(array)
print(*result[0])
print(result[1])