a = int(input())
b = list(map(int,input().split()))
bmax = 0
ans = 0
for i in range(a):
    if i == 0:
        bmax = a[i]
    else:
        if bmax < a[i]:
            bmax = a[i]
        elif bmax > a[i]:
            ans += bmax - a[i]
print(str(ans))