N = int(input())
kk = list(map(int, input().split()))
a = sum(kk)

if N == 1:
  print(0)
else:
  for k in range (N-1):
    if kk[N-k-2] > kk[N-k-1]:
      c = kk[N-k-1:N]
      d=kk[N-k-2]
      c_=[d if i<d else i for i in c]
      kk[N-k-1:N]=c_
  b = sum(kk)
  print(b-a)