number = int(input())
values = [int(i) for i in input().split()]
sum = 0

max = max(values)
min = min(values)

while max != min:
  temp = max/2
  for element in values:
    if max >= element > temp:
      sum += (int(max) - element)
      print(max, element)
    
  if int(temp) <= min:
    sum += (int(max) - min)
      
  max = int(temp)

print(sum)



