defmodule Main do
 def step(heights), do: step(heights, 0)
 defp step([], acc), do: acc
 defp step([head|tail], acc) do
   cond do
     head <= Enum.at(tail,0) -> step(tail,acc)
     head > Enum.at(tail,0) -> step(func(head,tail), acc+ head - Enum.at(tail,0))
   end
 end
 defp func(fst, list), do: [fst | Enum.slice(list,1..(Enum.count(list)-1))]
 
 def main do
    _num = IO.read(:line) |> String.trim() |> String.to_integer
    heights = IO.read(:line) |> String.trim() |> String.split(" ") |>  Enum.map(&String.to_integer/1)
    IO.inspect step(heights)
 end
end