N = int(input())
array = list(map(int,input().split()))
d,f = 0,0
ans = []
for i in range(N):
  if i == 0:
    ans.append(0)
    f = array[0]
    continue
  n = array[i]
  d = n - f
  if d >= 0:
    ans.append(d)
  else:
    ans.append(0)
  f = n
print( sum(ans) )
  
  