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

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

if a == 0
  line2A_1 = line2.map { |x| line2.max-x }
  counter += line2A_1.sum
else
  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
end

p counter