import Foundation

let n = Int(readLine()!)!
let a = readLine()!.components(separatedBy: " ")

var acc = 0
var sub = 0
var mem = 0

for str in a {
  let tmp = Int(str)!
  sub = mem + sub - tmp
  sub = sub > 0 ? sub : 0
  acc = acc + sub
  mem = tmp
}

print(acc)
