from sys import stdin


length = int(stdin.readline())
heights = list(stdin.readline().replace(" ", ""))

stool = 0
for i in range(1, length+1):
  if heights[i-1] > heights[i]:
    stool += (int(heights[i-1]) - int(heights[i]))
    heights[i] = heights[i-1]
print(int(stool))