N = int(input())
A = list(map(int,input().split()))
ans = 0
for i in range(N):
  if(i == 0):
    pass
  else:
    if(A[i] - A[i-1] <= 0):
      pass
    else:
      tmp = A[i] - A[i-1]
      ans += tmp
      A[i] += tmp
print(ans)