
def bubble_sort(buff):
	count = 0
	k = len(buff) - 1
	for i in xrange(k):
		for j in xrange(k, i, -1):
			if buff[j - 1] > buff[j]:
				count += 1
				temp = buff[j]
				buff[j] = buff[j - 1]
				buff[j - 1] = temp
	print " ".join(map(str, buff))
	print count

N = int(raw_input())
buff = map(int, raw_input().split(' '))
bubble_sort(buff) def bubble_sort(buff):
	count = 0
	k = len(buff) - 1
	for i in xrange(k):
		for j in xrange(k, i, -1):
			if buff[j - 1] > buff[j]:
				count += 1
				temp = buff[j]
				buff[j] = buff[j - 1]
				buff[j - 1] = temp
	print " ".join(map(str, buff))
	print count bubble_sort count = 0 count 0 k = len(buff) - 1 k len(buff) - 1 len(buff) len buff 1 for i in xrange(k):
		for j in xrange(k, i, -1):
			if buff[j - 1] > buff[j]:
				count += 1
				temp = buff[j]
				buff[j] = buff[j - 1]
				buff[j - 1] = temp
	 i xrange(k) xrange k for j in xrange(k, i, -1):
			if buff[j - 1] > buff[j]:
				count += 1
				temp = buff[j]
				buff[j] = buff[j - 1]
				buff[j - 1] = temp
	 j xrange(k, i, -1) xrange k i -1 if buff[j - 1] > buff[j]:
				count += 1
				temp = buff[j]
				buff[j] = buff[j - 1]
				buff[j - 1] = temp
	 buff[j - 1] > buff[j] buff[j - 1] buff j - 1 j 1 buff[j] buff j count += 1 count 1 temp = buff[j] temp buff[j] buff j buff[j] = buff[j - 1] buff[j] buff j buff[j - 1] buff j - 1 j 1 buff[j - 1] = temp buff[j - 1] buff j - 1 j 1 temp print " ".join(map(str, buff)) " ".join " " join map(str, buff) map str buff print buff buff N = int(raw_input()) N int(raw_input()) int raw_input() raw_input buff = map(int, raw_input().split(' ')) buff map(int, raw_input().split(' ')) map int raw_input().split(' ') ().split () raw_input split ' ' bubble_sort(buff) bubble_sort buff def bubble_sort(buff):
	count = 0
	k = len(buff) - 1
	for i in xrange(k):
		for j in xrange(k, i, -1):
			if buff[j - 1] > buff[j]:
				count += 1
				temp = buff[j]
				buff[j] = buff[j - 1]
				buff[j - 1] = temp
	print " ".join(map(str, buff))
	print count def bubble_sort(buff):
	count = 0
	k = len(buff) - 1
	for i in xrange(k):
		for j in xrange(k, i, -1):
			if buff[j - 1] > buff[j]:
				count += 1
				temp = buff[j]
				buff[j] = buff[j - 1]
				buff[j - 1] = temp
	print " ".join(map(str, buff))
	print count bubble_sort N = int(raw_input()) int(raw_input()) N buff = map(int, raw_input().split(' ')) map(int, raw_input().split(' ')) buff

def bubble_sort(buff):
	count = 0
	k = len(buff) - 1
	for i in xrange(k):
		for j in xrange(k, i, -1):
			if buff[j - 1] > buff[j]:
				count += 1
				temp = buff[j]
				buff[j] = buff[j - 1]
				buff[j - 1] = temp
	print " ".join(map(str, buff))
	print count

N = int(raw_input())
buff = map(int, raw_input().split(' '))
bubble_sort(buff)