#C
n = int(input())
a_ls = list(map(int, input().split()))

# n = 5
# a_ls = [2, 2, 2, 2, 2]
humi_ls = []

for i in range(n):
  humi = a_ls[i] - max(a_ls[0:i+1])
  humi_ls = humi_ls + [abs(humi)]

print(sum(humi_ls))