N = int(input())
A = list(map(int, input().split()))

gokei = 0
change = 1

def calc(lst, gokei):
    change = 0
    for i in range(N - 1, 0, -1):
        if lst[i] < lst[i - 1]:
            gokei += (lst[i - 1] - lst[i])
            lst[i] = lst[i - 1]
            change += 1

    return lst, gokei, change


while change != 0:
    A, gokei, change = calc(A, gokei)
print(gokei)