import Foundation

gets()
let a = getsA().lazy.map { Int($0)! }

let (res, _) = a.reduce((0, 0)) { acc, num in
  return acc.1 > num ? (acc.0 &+ acc.1 &- num, acc.1) : (acc.0, num)
}

print(res)

@discardableResult
@inlinable @inline(__always) func gets() -> String { readLine()! }
@inlinable @inline(__always) func geti() -> Int    { Int(gets())! }
@inlinable @inline(__always) func getd() -> Double { Double(gets())! }

@inlinable @inline(__always) func getsA(_ c: Character = " ") -> [String] { gets().split(separator: c).map(String.init) }
@inlinable @inline(__always) func getiA(_ c: Character = " ") -> [Int]    { getsA(c).map { Int($0)! } }
@inlinable @inline(__always) func getdA(_ c: Character = " ") -> [Double] { getsA(c).map { Double($0)! } }

@inlinable @inline(__always)
func printf(_ format: String, _ args: CVarArg...) -> Void {
  print(String(format: format, arguments: args), terminator: "")
}
