n = int(input())
As = list(map(int, input().split()))
s = 0

for i in range(n):
    if i == 0:
        continue
    if max(As[0:i]) > As[i]:
        s += max(As[0:i])-As[i]
    else:
        continue

print(s)
