n = int(input())
a = list(map(int,input().split()))
hights = int()
for i in range(n-1):
    if(i == n):
        break
    if(a[i] > a[i+1]):
        hights =hights + a[i] - a[i+1]
#        a[i+1] =a[i+1] + (a[i] - a[i+1])
    else:
        continue
#print(a)
print(hights)