x = int(input())
lst = list(map(int, input().split()))
i = 1
cnt = 0
a = lst[0]
while True:
    b = lst[i]
    if b < a:
        cnt += abs(b - a)
        a = a
    else:
        a = b
    if i == x - 1:
        break
    i += 1
print(cnt)