def bubbleSort( nums )
	cnt = 0
	0.upto( nums.length-1 ) { |i|
		( nums.length-1 ).downto( i+1 ) { |j|
			if nums[j] < nums[ j-1 ]
				nums[j], nums[ j-1 ] = nums[ j-1 ], nums[j]
				cnt += 1
			end
		}
	}
	puts( nums.join( " " ) )
	puts( cnt )
end

n = gets.to_i
nums = gets.split( " " ).map { |val| val.to_i }
bubbleSort( nums )