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

a_tmp = a_list[0]

sum = 0
for i in range(1, len(a_list)):
    if a_list[i] > a_tmp:
        sum += a_list[i] - a_tmp
        a_tmp = a_list[i]
    else:
        a_tmp = a_list[i]

print(sum)