import * as fs from "fs";

const input = fs
  .readFileSync("/dev/stdin", "utf8")
  .split("\n")[1]
  .split(" ")
  .map(Number);
let ans = 0;
let max = input[0];
input.forEach((x,i,arr) => {
  // 後ろの値が小さければ追加の処理
  if(arr[i] > max ) 
    ans += arr[i] - max;
	max = arr[i];
  }
});
console.log(ans);