N = input()
lists = list(map(int, input().split()))

high_sum = 0

for x in range(int(N)):
    if lists[x - 1] < lists[x]:
        a = lists[x] - lists[x - 1]
        high_sum += a
    else:
        continue
        
print(high_sum)