n = int(input())
a = list(map(int,input().split()))
MAX = max(a)
ans = 0
flg = False
for i in range(n):
    if a[i] == MAX:
        flg = True
        if i == 0:
            index = i + 1
        else:
            index = i
    if flg:
        ans += MAX - a[i]
for i in range(n):
    if a[i] == MAX:
        break
    else:
        if a[i] <= a[i+1]:
            pass
        else:
            ans += a[i] - a[i+1]
print(ans)