import java.util.*;
public class Main {
   public static void main(String[] args) {  
      Scanner sc = new Scanner(System.in);
      
      int a = sc.nextInt();
      int[] h = new int[a];
      for(int i = 0; i < a; i++) {
         h[i] = sc.nextInt();
      }
      System.out.println(answer(h, a));
   }
   
   public static int answer(int[] h, int minIndex) {
      if(minIndex == 0)
         return 0;
      if(minIndex == 1)
         return h[0];
      int min = 100000;
      int ind = 0;
      for(int i = 0; i < minIndex; i++) {
         if(min > h[i]) {
            min = h[i];
            ind = i;
         }
      }
      int[] a = new int[ind];
      for(int i = 0; i < ind; i++) {
         a[i] = h[i] - min;
      }
      int[] b = new int[minIndex - ind - 1];
      for(int i = ind + 1; i < minIndex; i++) {
         b[i - ind - 1] = h[i] - min;
      } 
      return answer(a, ind) + answer(b, minIndex - ind - 1) + min;
   }
} import java.util.*; util.* java public class Main {
   public static void main(String[] args) {  
      Scanner sc = new Scanner(System.in);
      
      int a = sc.nextInt();
      int[] h = new int[a];
      for(int i = 0; i < a; i++) {
         h[i] = sc.nextInt();
      }
      System.out.println(answer(h, a));
   }
   
   public static int answer(int[] h, int minIndex) {
      if(minIndex == 0)
         return 0;
      if(minIndex == 1)
         return h[0];
      int min = 100000;
      int ind = 0;
      for(int i = 0; i < minIndex; i++) {
         if(min > h[i]) {
            min = h[i];
            ind = i;
         }
      }
      int[] a = new int[ind];
      for(int i = 0; i < ind; i++) {
         a[i] = h[i] - min;
      }
      int[] b = new int[minIndex - ind - 1];
      for(int i = ind + 1; i < minIndex; i++) {
         b[i - ind - 1] = h[i] - min;
      } 
      return answer(a, ind) + answer(b, minIndex - ind - 1) + min;
   }
} Main public static void main(String[] args) {  
      Scanner sc = new Scanner(System.in);
      
      int a = sc.nextInt();
      int[] h = new int[a];
      for(int i = 0; i < a; i++) {
         h[i] = sc.nextInt();
      }
      System.out.println(answer(h, a));
   } main {  
      Scanner sc = new Scanner(System.in);
      
      int a = sc.nextInt();
      int[] h = new int[a];
      for(int i = 0; i < a; i++) {
         h[i] = sc.nextInt();
      }
      System.out.println(answer(h, a));
   } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc int[] h = new int[a]; h new int[a] a for(int i = 0; i < a; i++) {
         h[i] = sc.nextInt();
      } int i = 0; int i = 0; i 0 i < a i a i++ i++ i {
         h[i] = sc.nextInt();
      } {
         h[i] = sc.nextInt();
      } h[i] = sc.nextInt() h[i] h i sc.nextInt() sc.nextInt sc System.out.println(answer(h, a)) System.out.println System.out System System.out answer(h, a) answer h a String[] args args public static int answer(int[] h, int minIndex) {
      if(minIndex == 0)
         return 0;
      if(minIndex == 1)
         return h[0];
      int min = 100000;
      int ind = 0;
      for(int i = 0; i < minIndex; i++) {
         if(min > h[i]) {
            min = h[i];
            ind = i;
         }
      }
      int[] a = new int[ind];
      for(int i = 0; i < ind; i++) {
         a[i] = h[i] - min;
      }
      int[] b = new int[minIndex - ind - 1];
      for(int i = ind + 1; i < minIndex; i++) {
         b[i - ind - 1] = h[i] - min;
      } 
      return answer(a, ind) + answer(b, minIndex - ind - 1) + min;
   } answer {
      if(minIndex == 0)
         return 0;
      if(minIndex == 1)
         return h[0];
      int min = 100000;
      int ind = 0;
      for(int i = 0; i < minIndex; i++) {
         if(min > h[i]) {
            min = h[i];
            ind = i;
         }
      }
      int[] a = new int[ind];
      for(int i = 0; i < ind; i++) {
         a[i] = h[i] - min;
      }
      int[] b = new int[minIndex - ind - 1];
      for(int i = ind + 1; i < minIndex; i++) {
         b[i - ind - 1] = h[i] - min;
      } 
      return answer(a, ind) + answer(b, minIndex - ind - 1) + min;
   } if(minIndex == 0)
         return 0; minIndex == 0 minIndex 0 return 0; 0 if(minIndex == 1)
         return h[0]; minIndex == 1 minIndex 1 return h[0]; h[0] h 0 int min = 100000; min 100000 int ind = 0; ind 0 for(int i = 0; i < minIndex; i++) {
         if(min > h[i]) {
            min = h[i];
            ind = i;
         }
      } int i = 0; int i = 0; i 0 i < minIndex i minIndex i++ i++ i {
         if(min > h[i]) {
            min = h[i];
            ind = i;
         }
      } {
         if(min > h[i]) {
            min = h[i];
            ind = i;
         }
      } if(min > h[i]) {
            min = h[i];
            ind = i;
         } min > h[i] min h[i] h i {
            min = h[i];
            ind = i;
         } min = h[i] min h[i] h i ind = i ind i int[] a = new int[ind]; a new int[ind] ind for(int i = 0; i < ind; i++) {
         a[i] = h[i] - min;
      } int i = 0; int i = 0; i 0 i < ind i ind i++ i++ i {
         a[i] = h[i] - min;
      } {
         a[i] = h[i] - min;
      } a[i] = h[i] - min a[i] a i h[i] - min h[i] h i min int[] b = new int[minIndex - ind - 1]; b new int[minIndex - ind - 1] minIndex - ind - 1 minIndex - ind - 1 minIndex ind 1 for(int i = ind + 1; i < minIndex; i++) {
         b[i - ind - 1] = h[i] - min;
      } int i = ind + 1; int i = ind + 1; i ind + 1 ind 1 i < minIndex i minIndex i++ i++ i {
         b[i - ind - 1] = h[i] - min;
      } {
         b[i - ind - 1] = h[i] - min;
      } b[i - ind - 1] = h[i] - min b[i - ind - 1] b i - ind - 1 i - ind - 1 i ind 1 h[i] - min h[i] h i min return answer(a, ind) + answer(b, minIndex - ind - 1) + min; answer(a, ind) + answer(b, minIndex - ind - 1) + min answer(a, ind) + answer(b, minIndex - ind - 1) + min answer(a, ind) answer a ind answer(b, minIndex - ind - 1) answer b minIndex - ind - 1 minIndex - ind - 1 minIndex ind 1 min int[] h h int minIndex minIndex public class Main {
   public static void main(String[] args) {  
      Scanner sc = new Scanner(System.in);
      
      int a = sc.nextInt();
      int[] h = new int[a];
      for(int i = 0; i < a; i++) {
         h[i] = sc.nextInt();
      }
      System.out.println(answer(h, a));
   }
   
   public static int answer(int[] h, int minIndex) {
      if(minIndex == 0)
         return 0;
      if(minIndex == 1)
         return h[0];
      int min = 100000;
      int ind = 0;
      for(int i = 0; i < minIndex; i++) {
         if(min > h[i]) {
            min = h[i];
            ind = i;
         }
      }
      int[] a = new int[ind];
      for(int i = 0; i < ind; i++) {
         a[i] = h[i] - min;
      }
      int[] b = new int[minIndex - ind - 1];
      for(int i = ind + 1; i < minIndex; i++) {
         b[i - ind - 1] = h[i] - min;
      } 
      return answer(a, ind) + answer(b, minIndex - ind - 1) + min;
   }
} public class Main {
   public static void main(String[] args) {  
      Scanner sc = new Scanner(System.in);
      
      int a = sc.nextInt();
      int[] h = new int[a];
      for(int i = 0; i < a; i++) {
         h[i] = sc.nextInt();
      }
      System.out.println(answer(h, a));
   }
   
   public static int answer(int[] h, int minIndex) {
      if(minIndex == 0)
         return 0;
      if(minIndex == 1)
         return h[0];
      int min = 100000;
      int ind = 0;
      for(int i = 0; i < minIndex; i++) {
         if(min > h[i]) {
            min = h[i];
            ind = i;
         }
      }
      int[] a = new int[ind];
      for(int i = 0; i < ind; i++) {
         a[i] = h[i] - min;
      }
      int[] b = new int[minIndex - ind - 1];
      for(int i = ind + 1; i < minIndex; i++) {
         b[i - ind - 1] = h[i] - min;
      } 
      return answer(a, ind) + answer(b, minIndex - ind - 1) + min;
   }
} Main
import java.util.*;
public class Main {
   public static void main(String[] args) {  
      Scanner sc = new Scanner(System.in);
      
      int a = sc.nextInt();
      int[] h = new int[a];
      for(int i = 0; i < a; i++) {
         h[i] = sc.nextInt();
      }
      System.out.println(answer(h, a));
   }
   
   public static int answer(int[] h, int minIndex) {
      if(minIndex == 0)
         return 0;
      if(minIndex == 1)
         return h[0];
      int min = 100000;
      int ind = 0;
      for(int i = 0; i < minIndex; i++) {
         if(min > h[i]) {
            min = h[i];
            ind = i;
         }
      }
      int[] a = new int[ind];
      for(int i = 0; i < ind; i++) {
         a[i] = h[i] - min;
      }
      int[] b = new int[minIndex - ind - 1];
      for(int i = ind + 1; i < minIndex; i++) {
         b[i - ind - 1] = h[i] - min;
      } 
      return answer(a, ind) + answer(b, minIndex - ind - 1) + min;
   }
}