n = int(input())
a = list(map(int,input().split()))

ans = 0

#a.insert(0,0)
a.append(999999999)
tmp = 0
for i in range(n):
  #print(tmp)
  if a[i] <= a[i+1]:
    tmp = 0
    continue
  else:
    tmp = a[i] - a[i+1]
    a[i+1] += tmp
    ans += tmp
    
print(ans)
#print(a)