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

high_sum = 0

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