function main()
    parse_int(x) = parse(Int, x)
    len = parse_int(readline())
    line = parse_int.(split(readline()))
    steps = 0
    max_index = len
    current_max = 0
    for i in reverse(range(2, stop=len))
        last = line[i]
        if i <= max_index
            current_max = maximum(line[1:i - 1])
            max_index = findfirst(isequal(current_max), line[1:i - 1])
        end
        if current_max > last
            steps += current_max - last 
        end
    end
    return steps
end

println(main())