n = int(input())
a = list(map(int, input().split()))
l = []

for i in range(n):
    if i == 0:
        l.append(a[0])
    else:
        if max(l) <= a[i]:
            l.append(a[i])
        else:
            l.append(max(l))
            
print(sum(l) - sum(a))