N = int(input())#整数
A = list(map(int, input().split()))#配列Int型

ans = 0

for i in range(N):
    for j in range(i + 1):
        if A[i] > A[j]:
            ans = A[i] - A[j]
            A[j] += ans
print(ans)