defmodule Main do
  def main do
    _ = IO.gets("")
    a = IO.gets("")
        |> String.trim()
        |> String.split(" ")
        |> Enum.map(&String.to_integer/1)

    a
    |> calc(0)
    |> IO.puts()

  end

  def calc([], acc), do: acc
  def calc([x, y | tail], acc) when x > y do
    calc([x | tail], acc + x-y)
  end
  def calc([_ | tail], acc) do
    calc(tail, acc)
  end

end
