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