n = int(input())
if n < 3:
    print(0)
    exit()
nums = list(map(int, input().split()))
i, j = 0, n - 1
left_max = nums[0]
right_max = nums[-1]
res = 0
while i < j:
    if nums[i] < nums[j]:
        i += 1
        if nums[i] < left_max:
            res += left_max - nums[i]
        else:
            left_max = nums[i]
    else:
        j -= 1
        if nums[j] < right_max:
            res += right_max - nums[j]
        else:
            right_max = nums[j]
print(res)