func main() {
    let n = Int(readLine()!)!
    let aArray = readLine()!.split(separator: " ").map{Int($0)!}
    var sum = 0
    var add = 0
    for i in 0..<n-1 {
        if aArray[i+1] < aArray[i] + add {
            add = aArray[i] + add - aArray[i+1]
            sum += add
        } else {
            add = 0
        }
    }
    print(sum)
}

main()