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

try:
    for i in range(len(As)):
        if As[i] < As[i + 1]:
            step = As[i + 1] - As[i]
            As[i] += step
            sum += step 
except IndexError:
    pass

print(sum)