N = int(input())
heights = list(map(int, input().split()))
steps = [0] * N
for i in range(1, N):
    if heights[i-1] + steps[i-1] > heights[i]:
        steps[i] = 0
    else:
        steps[i] = heights[i] - heights[i-1]
print (sum(steps))