function Main(input) {
  const lines = input.split("\n");
  const N = parseInt(lines[0])
  const AList = (lines[1]).split(" ").map(x => parseInt(x))
  let addHeightList = [0]
  for (let i = 0; i < AList.length; i++) {
    if (i == 0) continue
    const sabun = AList[i - 1] + addHeightList[i - 1] - AList[i]
    if (sabun < 0) {
      addHeightList.push(0)
    } else {
      addHeightList.push(sabun)
    }
  }
  const addTotal = addHeightList.reduce((x, y) => x + y )
  console.log(addTotal)
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));