import java.util.*;
public class Main{
    static Scanner sc = new Scanner(System.in);
      static int n = sc.nextInt();
      static int[] h = new int[n];
    public static void main(String[] args){
      for(int i = 0;i < n;i++)h[i]=sc.nextInt();
      System.out.println(div(1,n));
    }
    public static int div(int x,int y){
      if(y==x) return h[x-1];
      if(y < x) return 0;
      int min = 10000000;
      for(int i = x;i <= y;i++){
      	min = Math.min(min,h[i-1]);
      }
      for(int i = x;i <= y;i++){
      	h[i-1]-=min;
      }
      for(int i = x;i <= y;i++){
      	if(h[i-1]==0)return div(x,i-1)+div(i+1,y)+min;
      }
      return 0;
    }
}
/tmp/hf-datasets-cache/medium/datasets/42704881117311-config-parquet-and-info-qiankunmu-Project_CodeNet-896c9d36/hub/datasets--qiankunmu--Project_CodeNet_Python800_and_Java250/snapshots/c899fde44a77ba1cc14973ed3de064e4ff3aadc7/Project_CodeNet_Java250.tar.gz