import math

#N = int(input())
N =int(input())
A =list(map(int, input().split())
        
#print(N)
cnt = 0
cur = A[0]
for i in range(1,N):
  if cur > A[i]:
    cnt += cur - A[i]
  else:
    cur = A[i]

print(cnt)