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

ans = 0
i = 0
maxi = a[i]

while True:
    if i == n - 1:
        break
    if a[i + 1] < a[i]:
        ans += (maxi - a[i + 1])
    else:
        maxi = a[i + 1]

    i += 1

print(ans)
