output_description: Print the minimum number of watering operations required to satisfy the
  condition.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      h_1 h_2 h_3 ...... h_N
attempt: import sys
  
  sys.setrecursionlimit(10**6)
  # input = sys.stdin.readline    ####
  int1 = lambda x: int(x) - 1
  
  
  def II():
      return int(input())
  
  
  def MI():
      return map(int, input().split())
  
  
  def MI1():
      return map(int1, input().split())
  
  
  def LI():
      return list(map(int, input().split()))
  
  
  def LI1():
      return list(map(int1, input().split()))
  
  
  def LLI(rows_number):
      return [LI() for _ in range(rows_number)]
  
  
  def printlist(lst, k="\n"):
      print(k.join(list(map(str, lst))))
  
  
  INF = float("inf")
  
  from collections import deque
  
  
  def solve():
      n = II()
      H = LI()
  
      d = deque()
  
      def split_0(lst):
          res = []
          tmp = []
          for l in lst:
              if l == 0:
                  if len(tmp) > 0:
                      res.append(tmp)
                      tmp = []
              else:
                  tmp.append(l)
          if len(tmp) > 0:
              res.append(tmp)
          return res
  
      r = split_0(H)
      d.extend(r)
      # print(d)
  
      cnt = 0
      while len(d) > 0:
          lst = d.popleft()
          mn = min(lst)
          res = split_0(list(map(lambda x: x - mn, lst)))
          cnt += mn
          d.extend(res)
      print(cnt)
  
  
  if __name__ == "__main__":
      solve()
  
problem_description: Statement
  
  In a flower bed, there are N flowers, numbered 1,2,......,N. Initially, the
  heights of all flowers are 0. You are given a sequence
  h=\\{h_1,h_2,h_3,......\\} as input. You would like to change the height of
  Flower k to h_k for all k (1 \leq k \leq N), by repeating the following
  "watering" operation:
  
    * Specify integers l and r. Increase the height of Flower x by 1 for all x such that l \leq x \leq r.
  
  Find the minimum number of watering operations required to satisfy the
  condition.
samples: [{"input": "4\n    1 2 2 1", "output": "2\n    \n\nThe minimum number of watering operations required is 2. One way to achieve it\nis:\n\n  * Perform the operation with (l,r)=(1,3).\n  * Perform the operation with (l,r)=(2,4).\n\n* * *"}, {"input": "5\n    3 1 2 3 1", "output": "5\n    \n\n* * *"}, {"input": "8\n    4 23 75 0 23 96 50 100", "output": "221"}]