import sys
input = lambda : sys.stdin.readline().rstrip()

n = int(input())
nums = map(int, input().split(" "))

out = 0
for i, num in enumerate(nums):
    if i == 0:
        cur_max = num
    else:
        if cur_max > num:
            out += cur_max - num
        else:
            cur_max = num

print(out)