
n = int(input())
a = list(map(int,input().split()))
m = max(a)
s = 0

ind = a.index(m)
for i in range(n):
    if i == n-1:
        break
    else:
        if a[i]>a[i+1] and i < ind:
            s += a[i]-a[i+1]

        else:
            a[i]>a[i+1]
            s += a[ind]-a[i+1]
            
print(s)