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