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()