def test(N, A):
  B = 0
  mx = A[0]
  if N != 1: 
    for i in range(1,N):
      if A[i] - mx < 0:
        B = B + mx - A[i]
      mx = max(A[1:i+1])  
  return B

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

B = test(N, A)

print(B)