function main()
    parse_int(x) = parse(Int, x)
    readline()
    line = parse_int.(split(readline()))
    steps = 0
    while length(line) > 1
        last = pop!(line)
        if maximum(line) > last
            steps += maximum(line) - last 
        end
    end
    return steps
end

println(main())