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

list_ans = []

for j in range(1,n):
    stepping_stone = a[j] - a[j-1]
    if stepping_stone > 0:
        list_ans.append(stepping_stone)
    else:
        list_ans.append(0)

ans = sum(list_ans)
print(ans)