n = gets.to_i
num = gets.strip.split.map(&:to_i)
ary = Array.new(n,0)
ary[0] = num[0]
for i in 1..n-1
  if ary[i-1] < num[i]
    ary[i] = num[i]
  else
    ary[i] = ary[i-1]
  end
end
#最大値をaryに記録した
puts ary.sum-num.sum