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

b = []
b.append(a[0])
ans = 0
for i in range(1,n):
    if a[i] < max(b):
        humi = (max(b)-a[i])
        a[i] += humi
    else:
        humi = 0
    b.append(a[i])
    ans += humi

print(ans)
