import Foundation

func readInt() -> Int {
    return Int(readLine()!)!
}

func readString() -> String {
    return readLine()!
}

func readLines(times: Int) -> [String] {
    var array = [String]()
    for _ in 0..<times {
        array.append(readLine()!)
    }
    return array
}

func readSeparatedStrings() -> [String] {
    return readLine()!.split(separator: " ").map {String($0)}
}

func readStringToStrings() -> [String] {
    return readLine()!.map{String($0)}
}

func readSeparatedInts() -> [Int] {
    return readLine()!.split(separator: " ").map {Int($0)!}
}
func readSeparatedDouble() -> [Double] {
    return readLine()!.split(separator: " ").map {Double($0)!}
}

// 二進数に変換
func convertIntToRadix2(n: Int, radix: Int = 2) -> String {
    return String(n, radix: radix)
}

//配列を逆順にする reverced ソートする　sorted
//最初数文字取得 prefix


let N = readInt()
let students = readSeparatedInts()

var top = 0
var result = 0


for i in 0..<N {
    let h = students[i]
    
    if i == 0 {
        top = h
    } else {
        if top > h {
            result += top - h
        } else if top == h {
            
        } else {
            top = h
        }
    }
}
print(result)
