# 入力：人数 num
n = gets.to_i
# 入力：身長の配列 heights
heights = gets.chomp.split.map(&:to_i)
# 踏み台の高さの合計の変数 steps を宣言
steps = 0
for i in 0..(n-1) do
  # 身長差 diff
  diff = heights[i+1] - heights[i]
  if diff < 0
    steps += diff
    heights[i] += diff
  end
end
# 出力：身長の差が正の数のものを合計
puts steps