N = int(input())
members = list(map(int, input().strip().split()))
stands = []

for i, member in enumerate(members):
  if i>0:
    if members[i] > members[i-1]:
      stands.append(0)
    else:
      stands.append(members[i-1] - members[i])
      
print(sum(stands))