n = int(input())
a = list(map(int, input().split()))
l = []
for i in range(0, n-1):
    if(a[i] > a[i+1]):
        l.append(a[i]-a[i+1])
    else:
        l.append(0)
for i in range(0, len(l)-1):
    if((l[i] > 0) and (l[i+1] != 0)):
        l[i+1] = l[i+1] + l[i]
print(sum(l))
        
