line1 = gets.split(' ')
line2 = gets.split(' ')
line1 = line1.map { |x| x.to_i }
line2 = line2.map { |x| x.to_i }

counter = 0
b = line1[0]
a = line2.index(line2.max)

while a != 0
  a = line2.index(line2.max)
  line2A = line2[a..b-1]
  line2A_1 = line2A.map { |x| line2.max-x }
  counter += line2A_1.sum
  line2 = line2[0..a-1]
  linemax = line2.max
  b -= a
end

p counter