n = int(input())
As = list(map(int,input().split()))
sum = 0
step =0

try:
    for i in range(n):
        if As[i] > As[i + 1]:
            step = As[i] - As[i + 1]
            print(step)
            As[i + 1] += step
            print("As["+str(i + 1)+"]:"+str(As[i + 1]))
            sum += step 
except IndexError:
    pass

print(sum)