N = int(input())
A = list(map(int, input().split()))[::-1]
max_val = 0
ret = 0
for i in range(1,N):
    if A[i] > max_val:
        max_val = A[i]
        for j in range(0, i):
            ret += max_val - A[j]
print(ret)
