import Foundation

let rawData = FileHandle.standardInput.availableData
let scanner = Scanner(string: String(data: rawData, encoding: .utf8)!)

let n = scanner.scanInt()!

var acc = 0
var pre = 0
var dif = 0

for _ in 0..<n {
  let cur = scanner.scanInt()!
  dif = pre + dif - cur > 0 ? pre + dif - cur : 0
  acc = acc + dif
  pre = cur
}

print(acc)
