
def bubblesort(a, n):
	flag = 1
	count = 0
	while flag:
		flag = 0
		for j in range(n-1, 0, -1):
			if a[j] < a[j-1]:
				count += 1
				a[j], a[j-1] = a[j-1], a[j]
				flag = 1
	return count

def main():
	n = int(input())
	data = list(map(int, input().split()))

	c = bubblesort(data, n)
	print(*data)
	print(c)

if __name__ == "__main__":
	main() def bubblesort(a, n):
	flag = 1
	count = 0
	while flag:
		flag = 0
		for j in range(n-1, 0, -1):
			if a[j] < a[j-1]:
				count += 1
				a[j], a[j-1] = a[j-1], a[j]
				flag = 1
	return count bubblesort flag = 1 flag 1 count = 0 count 0 while flag:
		flag = 0
		for j in range(n-1, 0, -1):
			if a[j] < a[j-1]:
				count += 1
				a[j], a[j-1] = a[j-1], a[j]
				flag = 1
	 flag flag = 0 flag 0 for j in range(n-1, 0, -1):
			if a[j] < a[j-1]:
				count += 1
				a[j], a[j-1] = a[j-1], a[j]
				flag = 1
	 j range(n-1, 0, -1) range n-1 n 1 0 -1 if a[j] < a[j-1]:
				count += 1
				a[j], a[j-1] = a[j-1], a[j]
				flag = 1
	 a[j] < a[j-1] a[j] a j a[j-1] a j-1 j 1 count += 1 count 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 return count count a a n n def main():
	n = int(input())
	data = list(map(int, input().split()))

	c = bubblesort(data, n)
	print(*data)
	print(c) main n = int(input()) n int(input()) int input() input data = list(map(int, input().split())) data list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split c = bubblesort(data, n) c bubblesort(data, n) bubblesort data n print(*data) print *data data print(c) print c if __name__ == "__main__":
	main() __name__ == "__main__" __name__ "__main__" main() main def main():
	n = int(input())
	data = list(map(int, input().split()))

	c = bubblesort(data, n)
	print(*data)
	print(c) def main():
	n = int(input())
	data = list(map(int, input().split()))

	c = bubblesort(data, n)
	print(*data)
	print(c) main def bubblesort(a, n):
	flag = 1
	count = 0
	while flag:
		flag = 0
		for j in range(n-1, 0, -1):
			if a[j] < a[j-1]:
				count += 1
				a[j], a[j-1] = a[j-1], a[j]
				flag = 1
	return count def bubblesort(a, n):
	flag = 1
	count = 0
	while flag:
		flag = 0
		for j in range(n-1, 0, -1):
			if a[j] < a[j-1]:
				count += 1
				a[j], a[j-1] = a[j-1], a[j]
				flag = 1
	return count bubblesort

def bubblesort(a, n):
	flag = 1
	count = 0
	while flag:
		flag = 0
		for j in range(n-1, 0, -1):
			if a[j] < a[j-1]:
				count += 1
				a[j], a[j-1] = a[j-1], a[j]
				flag = 1
	return count

def main():
	n = int(input())
	data = list(map(int, input().split()))

	c = bubblesort(data, n)
	print(*data)
	print(c)

if __name__ == "__main__":
	main()