N = int(input())
A = list(map(int, input().split()))
ans = 0

while True:
    max_num = 0
    max_index = 0
    max_num = max(A)
    max_index = int(A.index(max_num))
    for i in range(max_index+1, len(A)):
        ans += max_num - A[i]
    del A[max_index:]

    if(max_index == 0):
        break
    else:
        pass

print(ans)