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


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

def answer(values, max, min):
  sum = 0
  if max == min: 
    return 0
  if max < min:
    return min
  average = max / 2
  for index in range(len(values)):
    if max > values[index] > average:
      sum += int(max) - values[index]

  return sum + answer(values,average,min) 

temp = answer(values, max, min)
print(temp)




