N = int(input())
array = list(map(int,input().split()))
d,f = 0,0
ans = [0]
for i in range(1,N):
  n = array[i]
  d = f - n
  if d >= 0:
    ans.append(d)
  else:
    ans.append(0)
  f = max(ans[0:i])
print( sum(ans) )
  
  