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

ans = 0
maxV = max(list)
for i in reversed(range(0,len(list))):
    if i != 0:
        sl = list[:i]
        maxV = max(sl)
        if list[i] < maxV:
            ans += maxV - list[i]

print(ans)
