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

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

func mainC() {
    let N = readInt()
    let heights = readInts()

    var previous = heights[0]
    var baseHeights = 0
    for height in heights {
        if previous == height {
        } else if previous > height {
            baseHeights += previous - height
        } else if previous < height {
            previous = height
        }
    }

    print(baseHeights)
}
mainC()