N = int(input())
array = list(map(int, input().split()))
ans = 0
m = 0

max_index = len(array) - 1

while(True):
    if len(array) == 0:
        break
    m = max(array)
    max_index = array.index(m)

    t = 0
    for x in range(max_index + 1,len(array)):
        t += m - array[x]
    ans += t
    del array[max_index :]

print(ans)
