n = int(input())
inputs = list(map(int,input().split()))
As = []
sum = 0
step =0
for i in inputs:
    As.append(int(i))
try:
    for i in range(len(As)):
        if As[i] < As[i + 1]:
            step = As[i + 1] - As[i]
            sum += step 
except:
    pass

print(sum)