import Foundation

let input1 = readLine()!
let input2 = readLine()!
let n = Int(input1)!
let a = input2.components(separatedBy: " ").map { Double($0)! }
var current = a[0]
var res = 0.0
for i in (1..<n) {
  if (a[i] > current) {
    res += (a[i] - current)
  }
  current = a[i]
}
print(Int(res))