lst = []
count = 0
n = int(input())
lst = list(map(int, input().split(' ')[:n]))
for j in range(len(lst)-1):
    if lst[j]<=lst[j+1]:
        count = count + (lst[j+1]-lst[j])
    else:
        count = count + 0
print(count)