import java.util.*;
public class Main {

  public static void main(String[] args) { 
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      int[] heights = new int[n];

      int minHeight = Integer.MAX_VALUE;
      for(int i = 0; i < n; i++) {
        heights[i] = sc.nextInt();
        if(heights[i] > 0)
          minHeight = Math.min(minHeight, heights[i]);
      }

      int cnt = 0;

      while(minHeight != Integer.MAX_VALUE) {
        
        int nextMinHeight = Integer.MAX_VALUE;
        int i = 0;
        int seg = 1;
        for(; i < n; i++) {
          if(heights[i] != 0) {
            i++;
            break;
          }
        }
        for(; i < n; i++) {
          if(heights[i-1] == 0 && heights[i] != 0)
            seg++;
        }
        cnt += minHeight * seg;
        for(i = 0; i < n; i++) {
          if(heights[i] == 0)  continue;
          heights[i] -= minHeight;
          nextMinHeight = Math.min(nextMinHeight, heights[i]);
        }
        minHeight = nextMinHeight;
      }
      System.out.println(cnt);
  }
} import java.util.*; util.* java public class Main {

  public static void main(String[] args) { 
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      int[] heights = new int[n];

      int minHeight = Integer.MAX_VALUE;
      for(int i = 0; i < n; i++) {
        heights[i] = sc.nextInt();
        if(heights[i] > 0)
          minHeight = Math.min(minHeight, heights[i]);
      }

      int cnt = 0;

      while(minHeight != Integer.MAX_VALUE) {
        
        int nextMinHeight = Integer.MAX_VALUE;
        int i = 0;
        int seg = 1;
        for(; i < n; i++) {
          if(heights[i] != 0) {
            i++;
            break;
          }
        }
        for(; i < n; i++) {
          if(heights[i-1] == 0 && heights[i] != 0)
            seg++;
        }
        cnt += minHeight * seg;
        for(i = 0; i < n; i++) {
          if(heights[i] == 0)  continue;
          heights[i] -= minHeight;
          nextMinHeight = Math.min(nextMinHeight, heights[i]);
        }
        minHeight = nextMinHeight;
      }
      System.out.println(cnt);
  }
} Main public static void main(String[] args) { 
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      int[] heights = new int[n];

      int minHeight = Integer.MAX_VALUE;
      for(int i = 0; i < n; i++) {
        heights[i] = sc.nextInt();
        if(heights[i] > 0)
          minHeight = Math.min(minHeight, heights[i]);
      }

      int cnt = 0;

      while(minHeight != Integer.MAX_VALUE) {
        
        int nextMinHeight = Integer.MAX_VALUE;
        int i = 0;
        int seg = 1;
        for(; i < n; i++) {
          if(heights[i] != 0) {
            i++;
            break;
          }
        }
        for(; i < n; i++) {
          if(heights[i-1] == 0 && heights[i] != 0)
            seg++;
        }
        cnt += minHeight * seg;
        for(i = 0; i < n; i++) {
          if(heights[i] == 0)  continue;
          heights[i] -= minHeight;
          nextMinHeight = Math.min(nextMinHeight, heights[i]);
        }
        minHeight = nextMinHeight;
      }
      System.out.println(cnt);
  } main { 
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      int[] heights = new int[n];

      int minHeight = Integer.MAX_VALUE;
      for(int i = 0; i < n; i++) {
        heights[i] = sc.nextInt();
        if(heights[i] > 0)
          minHeight = Math.min(minHeight, heights[i]);
      }

      int cnt = 0;

      while(minHeight != Integer.MAX_VALUE) {
        
        int nextMinHeight = Integer.MAX_VALUE;
        int i = 0;
        int seg = 1;
        for(; i < n; i++) {
          if(heights[i] != 0) {
            i++;
            break;
          }
        }
        for(; i < n; i++) {
          if(heights[i-1] == 0 && heights[i] != 0)
            seg++;
        }
        cnt += minHeight * seg;
        for(i = 0; i < n; i++) {
          if(heights[i] == 0)  continue;
          heights[i] -= minHeight;
          nextMinHeight = Math.min(nextMinHeight, heights[i]);
        }
        minHeight = nextMinHeight;
      }
      System.out.println(cnt);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] heights = new int[n]; heights new int[n] n int minHeight = Integer.MAX_VALUE; minHeight Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int i = 0; i < n; i++) {
        heights[i] = sc.nextInt();
        if(heights[i] > 0)
          minHeight = Math.min(minHeight, heights[i]);
      } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
        heights[i] = sc.nextInt();
        if(heights[i] > 0)
          minHeight = Math.min(minHeight, heights[i]);
      } {
        heights[i] = sc.nextInt();
        if(heights[i] > 0)
          minHeight = Math.min(minHeight, heights[i]);
      } heights[i] = sc.nextInt() heights[i] heights i sc.nextInt() sc.nextInt sc if(heights[i] > 0)
          minHeight = Math.min(minHeight, heights[i]); heights[i] > 0 heights[i] heights i 0 minHeight = Math.min(minHeight, heights[i]) minHeight Math.min(minHeight, heights[i]) Math.min Math minHeight heights[i] heights i int cnt = 0; cnt 0 while(minHeight != Integer.MAX_VALUE) {
        
        int nextMinHeight = Integer.MAX_VALUE;
        int i = 0;
        int seg = 1;
        for(; i < n; i++) {
          if(heights[i] != 0) {
            i++;
            break;
          }
        }
        for(; i < n; i++) {
          if(heights[i-1] == 0 && heights[i] != 0)
            seg++;
        }
        cnt += minHeight * seg;
        for(i = 0; i < n; i++) {
          if(heights[i] == 0)  continue;
          heights[i] -= minHeight;
          nextMinHeight = Math.min(nextMinHeight, heights[i]);
        }
        minHeight = nextMinHeight;
      } minHeight != Integer.MAX_VALUE minHeight Integer.MAX_VALUE Integer Integer.MAX_VALUE {
        
        int nextMinHeight = Integer.MAX_VALUE;
        int i = 0;
        int seg = 1;
        for(; i < n; i++) {
          if(heights[i] != 0) {
            i++;
            break;
          }
        }
        for(; i < n; i++) {
          if(heights[i-1] == 0 && heights[i] != 0)
            seg++;
        }
        cnt += minHeight * seg;
        for(i = 0; i < n; i++) {
          if(heights[i] == 0)  continue;
          heights[i] -= minHeight;
          nextMinHeight = Math.min(nextMinHeight, heights[i]);
        }
        minHeight = nextMinHeight;
      } int nextMinHeight = Integer.MAX_VALUE; nextMinHeight Integer.MAX_VALUE Integer Integer.MAX_VALUE int i = 0; i 0 int seg = 1; seg 1 for(; i < n; i++) {
          if(heights[i] != 0) {
            i++;
            break;
          }
        } ; i < n i n i++ i++ i {
          if(heights[i] != 0) {
            i++;
            break;
          }
        } {
          if(heights[i] != 0) {
            i++;
            break;
          }
        } if(heights[i] != 0) {
            i++;
            break;
          } heights[i] != 0 heights[i] heights i 0 {
            i++;
            break;
          } i++ i break; for(; i < n; i++) {
          if(heights[i-1] == 0 && heights[i] != 0)
            seg++;
        } ; i < n i n i++ i++ i {
          if(heights[i-1] == 0 && heights[i] != 0)
            seg++;
        } {
          if(heights[i-1] == 0 && heights[i] != 0)
            seg++;
        } if(heights[i-1] == 0 && heights[i] != 0)
            seg++; heights[i-1] == 0 && heights[i] != 0 heights[i-1] == 0 heights[i-1] heights i-1 i 1 0 heights[i] != 0 heights[i] heights i 0 seg++ seg cnt += minHeight * seg cnt minHeight * seg minHeight seg for(i = 0; i < n; i++) {
          if(heights[i] == 0)  continue;
          heights[i] -= minHeight;
          nextMinHeight = Math.min(nextMinHeight, heights[i]);
        } i = 0; i = 0 i 0 i < n i n i++ i++ i {
          if(heights[i] == 0)  continue;
          heights[i] -= minHeight;
          nextMinHeight = Math.min(nextMinHeight, heights[i]);
        } {
          if(heights[i] == 0)  continue;
          heights[i] -= minHeight;
          nextMinHeight = Math.min(nextMinHeight, heights[i]);
        } if(heights[i] == 0)  continue; heights[i] == 0 heights[i] heights i 0 continue; heights[i] -= minHeight heights[i] heights i minHeight nextMinHeight = Math.min(nextMinHeight, heights[i]) nextMinHeight Math.min(nextMinHeight, heights[i]) Math.min Math nextMinHeight heights[i] heights i minHeight = nextMinHeight minHeight nextMinHeight System.out.println(cnt) System.out.println System.out System System.out cnt String[] args args public class Main {

  public static void main(String[] args) { 
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      int[] heights = new int[n];

      int minHeight = Integer.MAX_VALUE;
      for(int i = 0; i < n; i++) {
        heights[i] = sc.nextInt();
        if(heights[i] > 0)
          minHeight = Math.min(minHeight, heights[i]);
      }

      int cnt = 0;

      while(minHeight != Integer.MAX_VALUE) {
        
        int nextMinHeight = Integer.MAX_VALUE;
        int i = 0;
        int seg = 1;
        for(; i < n; i++) {
          if(heights[i] != 0) {
            i++;
            break;
          }
        }
        for(; i < n; i++) {
          if(heights[i-1] == 0 && heights[i] != 0)
            seg++;
        }
        cnt += minHeight * seg;
        for(i = 0; i < n; i++) {
          if(heights[i] == 0)  continue;
          heights[i] -= minHeight;
          nextMinHeight = Math.min(nextMinHeight, heights[i]);
        }
        minHeight = nextMinHeight;
      }
      System.out.println(cnt);
  }
} public class Main {

  public static void main(String[] args) { 
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      int[] heights = new int[n];

      int minHeight = Integer.MAX_VALUE;
      for(int i = 0; i < n; i++) {
        heights[i] = sc.nextInt();
        if(heights[i] > 0)
          minHeight = Math.min(minHeight, heights[i]);
      }

      int cnt = 0;

      while(minHeight != Integer.MAX_VALUE) {
        
        int nextMinHeight = Integer.MAX_VALUE;
        int i = 0;
        int seg = 1;
        for(; i < n; i++) {
          if(heights[i] != 0) {
            i++;
            break;
          }
        }
        for(; i < n; i++) {
          if(heights[i-1] == 0 && heights[i] != 0)
            seg++;
        }
        cnt += minHeight * seg;
        for(i = 0; i < n; i++) {
          if(heights[i] == 0)  continue;
          heights[i] -= minHeight;
          nextMinHeight = Math.min(nextMinHeight, heights[i]);
        }
        minHeight = nextMinHeight;
      }
      System.out.println(cnt);
  }
} Main
import java.util.*;
public class Main {

  public static void main(String[] args) { 
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      int[] heights = new int[n];

      int minHeight = Integer.MAX_VALUE;
      for(int i = 0; i < n; i++) {
        heights[i] = sc.nextInt();
        if(heights[i] > 0)
          minHeight = Math.min(minHeight, heights[i]);
      }

      int cnt = 0;

      while(minHeight != Integer.MAX_VALUE) {
        
        int nextMinHeight = Integer.MAX_VALUE;
        int i = 0;
        int seg = 1;
        for(; i < n; i++) {
          if(heights[i] != 0) {
            i++;
            break;
          }
        }
        for(; i < n; i++) {
          if(heights[i-1] == 0 && heights[i] != 0)
            seg++;
        }
        cnt += minHeight * seg;
        for(i = 0; i < n; i++) {
          if(heights[i] == 0)  continue;
          heights[i] -= minHeight;
          nextMinHeight = Math.min(nextMinHeight, heights[i]);
        }
        minHeight = nextMinHeight;
      }
      System.out.println(cnt);
  }
}