import java.util.*;
import static java.lang.Math.*;
import java.math.BigInteger;

public class Main {
    static int cnt = 0;
    
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		// 入力
		int n = sc.nextInt();
		int[] h = new int[n];
		for(int i = 0; i < n; i++){
		    h[i] = sc.nextInt();
		}

		// 計算
		int result = 0;
		dfs(h);
		result = cnt;
		// 出力
		System.out.println(result);
        
	}
	
	static void dfs(int[] a){
	    //System.out.println(Arrays.toString(a));
	    ArrayList<Integer> list = new ArrayList<Integer>();
	    int min = 1000;
	    for(int i = 0; i < a.length; i++){
	        if(a[i] < min) min = a[i];
	    }
	    cnt += min;
	    for(int i = 0; i < a.length; i++){
	        if(a[i] == min) list.add(i);
	    }
	    list.add(a.length);
	    int l = 0;
	    for(int i = 0; i < list.size(); i++){
	        int r = list.get(i);
	        int len = r - l;
	        if(len > 0){
	            int[] next = new int[len];
	            for(int j = l; j < l + len; j++){
	                next[j - l] = a[j] - min;
	            }
	            dfs(next);
	        }
	        l = r + 1;
	    }
	}
} import java.util.*; util.* java import static java.lang.Math.*; import static java.lang.Math.*; import static java.lang.Math.*; import java.math.BigInteger; BigInteger java.math public class Main {
    static int cnt = 0;
    
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		// 入力
		int n = sc.nextInt();
		int[] h = new int[n];
		for(int i = 0; i < n; i++){
		    h[i] = sc.nextInt();
		}

		// 計算
		int result = 0;
		dfs(h);
		result = cnt;
		// 出力
		System.out.println(result);
        
	}
	
	static void dfs(int[] a){
	    //System.out.println(Arrays.toString(a));
	    ArrayList<Integer> list = new ArrayList<Integer>();
	    int min = 1000;
	    for(int i = 0; i < a.length; i++){
	        if(a[i] < min) min = a[i];
	    }
	    cnt += min;
	    for(int i = 0; i < a.length; i++){
	        if(a[i] == min) list.add(i);
	    }
	    list.add(a.length);
	    int l = 0;
	    for(int i = 0; i < list.size(); i++){
	        int r = list.get(i);
	        int len = r - l;
	        if(len > 0){
	            int[] next = new int[len];
	            for(int j = l; j < l + len; j++){
	                next[j - l] = a[j] - min;
	            }
	            dfs(next);
	        }
	        l = r + 1;
	    }
	}
} Main static int cnt = 0; cnt 0 public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		// 入力
		int n = sc.nextInt();
		int[] h = new int[n];
		for(int i = 0; i < n; i++){
		    h[i] = sc.nextInt();
		}

		// 計算
		int result = 0;
		dfs(h);
		result = cnt;
		// 出力
		System.out.println(result);
        
	} main {
		Scanner sc = new Scanner(System.in);
		
		// 入力
		int n = sc.nextInt();
		int[] h = new int[n];
		for(int i = 0; i < n; i++){
		    h[i] = sc.nextInt();
		}

		// 計算
		int result = 0;
		dfs(h);
		result = cnt;
		// 出力
		System.out.println(result);
        
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] h = new int[n]; h new int[n] n for(int i = 0; i < n; i++){
		    h[i] = sc.nextInt();
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
		    h[i] = sc.nextInt();
		} {
		    h[i] = sc.nextInt();
		} h[i] = sc.nextInt() h[i] h i sc.nextInt() sc.nextInt sc int result = 0; result 0 dfs(h) dfs h result = cnt result cnt System.out.println(result) System.out.println System.out System System.out result String[] args args static void dfs(int[] a){
	    //System.out.println(Arrays.toString(a));
	    ArrayList<Integer> list = new ArrayList<Integer>();
	    int min = 1000;
	    for(int i = 0; i < a.length; i++){
	        if(a[i] < min) min = a[i];
	    }
	    cnt += min;
	    for(int i = 0; i < a.length; i++){
	        if(a[i] == min) list.add(i);
	    }
	    list.add(a.length);
	    int l = 0;
	    for(int i = 0; i < list.size(); i++){
	        int r = list.get(i);
	        int len = r - l;
	        if(len > 0){
	            int[] next = new int[len];
	            for(int j = l; j < l + len; j++){
	                next[j - l] = a[j] - min;
	            }
	            dfs(next);
	        }
	        l = r + 1;
	    }
	} dfs {
	    //System.out.println(Arrays.toString(a));
	    ArrayList<Integer> list = new ArrayList<Integer>();
	    int min = 1000;
	    for(int i = 0; i < a.length; i++){
	        if(a[i] < min) min = a[i];
	    }
	    cnt += min;
	    for(int i = 0; i < a.length; i++){
	        if(a[i] == min) list.add(i);
	    }
	    list.add(a.length);
	    int l = 0;
	    for(int i = 0; i < list.size(); i++){
	        int r = list.get(i);
	        int len = r - l;
	        if(len > 0){
	            int[] next = new int[len];
	            for(int j = l; j < l + len; j++){
	                next[j - l] = a[j] - min;
	            }
	            dfs(next);
	        }
	        l = r + 1;
	    }
	} ArrayList<Integer> list = new ArrayList<Integer>(); list new ArrayList<Integer>() int min = 1000; min 1000 for(int i = 0; i < a.length; i++){
	        if(a[i] < min) min = a[i];
	    } int i = 0; int i = 0; i 0 i < a.length i a.length a a.length i++ i++ i {
	        if(a[i] < min) min = a[i];
	    } {
	        if(a[i] < min) min = a[i];
	    } if(a[i] < min) min = a[i]; a[i] < min a[i] a i min min = a[i] min a[i] a i cnt += min cnt min for(int i = 0; i < a.length; i++){
	        if(a[i] == min) list.add(i);
	    } int i = 0; int i = 0; i 0 i < a.length i a.length a a.length i++ i++ i {
	        if(a[i] == min) list.add(i);
	    } {
	        if(a[i] == min) list.add(i);
	    } if(a[i] == min) list.add(i); a[i] == min a[i] a i min list.add(i) list.add list i list.add(a.length) list.add list a.length a a.length int l = 0; l 0 for(int i = 0; i < list.size(); i++){
	        int r = list.get(i);
	        int len = r - l;
	        if(len > 0){
	            int[] next = new int[len];
	            for(int j = l; j < l + len; j++){
	                next[j - l] = a[j] - min;
	            }
	            dfs(next);
	        }
	        l = r + 1;
	    } int i = 0; int i = 0; i 0 i < list.size() i list.size() list.size list i++ i++ i {
	        int r = list.get(i);
	        int len = r - l;
	        if(len > 0){
	            int[] next = new int[len];
	            for(int j = l; j < l + len; j++){
	                next[j - l] = a[j] - min;
	            }
	            dfs(next);
	        }
	        l = r + 1;
	    } {
	        int r = list.get(i);
	        int len = r - l;
	        if(len > 0){
	            int[] next = new int[len];
	            for(int j = l; j < l + len; j++){
	                next[j - l] = a[j] - min;
	            }
	            dfs(next);
	        }
	        l = r + 1;
	    } int r = list.get(i); r list.get(i) list.get list i int len = r - l; len r - l r l if(len > 0){
	            int[] next = new int[len];
	            for(int j = l; j < l + len; j++){
	                next[j - l] = a[j] - min;
	            }
	            dfs(next);
	        } len > 0 len 0 {
	            int[] next = new int[len];
	            for(int j = l; j < l + len; j++){
	                next[j - l] = a[j] - min;
	            }
	            dfs(next);
	        } int[] next = new int[len]; next new int[len] len for(int j = l; j < l + len; j++){
	                next[j - l] = a[j] - min;
	            } int j = l; int j = l; j l j < l + len j l + len l len j++ j++ j {
	                next[j - l] = a[j] - min;
	            } {
	                next[j - l] = a[j] - min;
	            } next[j - l] = a[j] - min next[j - l] next j - l j l a[j] - min a[j] a j min dfs(next) dfs next l = r + 1 l r + 1 r 1 int[] a a public class Main {
    static int cnt = 0;
    
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		// 入力
		int n = sc.nextInt();
		int[] h = new int[n];
		for(int i = 0; i < n; i++){
		    h[i] = sc.nextInt();
		}

		// 計算
		int result = 0;
		dfs(h);
		result = cnt;
		// 出力
		System.out.println(result);
        
	}
	
	static void dfs(int[] a){
	    //System.out.println(Arrays.toString(a));
	    ArrayList<Integer> list = new ArrayList<Integer>();
	    int min = 1000;
	    for(int i = 0; i < a.length; i++){
	        if(a[i] < min) min = a[i];
	    }
	    cnt += min;
	    for(int i = 0; i < a.length; i++){
	        if(a[i] == min) list.add(i);
	    }
	    list.add(a.length);
	    int l = 0;
	    for(int i = 0; i < list.size(); i++){
	        int r = list.get(i);
	        int len = r - l;
	        if(len > 0){
	            int[] next = new int[len];
	            for(int j = l; j < l + len; j++){
	                next[j - l] = a[j] - min;
	            }
	            dfs(next);
	        }
	        l = r + 1;
	    }
	}
} public class Main {
    static int cnt = 0;
    
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		// 入力
		int n = sc.nextInt();
		int[] h = new int[n];
		for(int i = 0; i < n; i++){
		    h[i] = sc.nextInt();
		}

		// 計算
		int result = 0;
		dfs(h);
		result = cnt;
		// 出力
		System.out.println(result);
        
	}
	
	static void dfs(int[] a){
	    //System.out.println(Arrays.toString(a));
	    ArrayList<Integer> list = new ArrayList<Integer>();
	    int min = 1000;
	    for(int i = 0; i < a.length; i++){
	        if(a[i] < min) min = a[i];
	    }
	    cnt += min;
	    for(int i = 0; i < a.length; i++){
	        if(a[i] == min) list.add(i);
	    }
	    list.add(a.length);
	    int l = 0;
	    for(int i = 0; i < list.size(); i++){
	        int r = list.get(i);
	        int len = r - l;
	        if(len > 0){
	            int[] next = new int[len];
	            for(int j = l; j < l + len; j++){
	                next[j - l] = a[j] - min;
	            }
	            dfs(next);
	        }
	        l = r + 1;
	    }
	}
} Main
import java.util.*;
import static java.lang.Math.*;
import java.math.BigInteger;

public class Main {
    static int cnt = 0;
    
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		// 入力
		int n = sc.nextInt();
		int[] h = new int[n];
		for(int i = 0; i < n; i++){
		    h[i] = sc.nextInt();
		}

		// 計算
		int result = 0;
		dfs(h);
		result = cnt;
		// 出力
		System.out.println(result);
        
	}
	
	static void dfs(int[] a){
	    //System.out.println(Arrays.toString(a));
	    ArrayList<Integer> list = new ArrayList<Integer>();
	    int min = 1000;
	    for(int i = 0; i < a.length; i++){
	        if(a[i] < min) min = a[i];
	    }
	    cnt += min;
	    for(int i = 0; i < a.length; i++){
	        if(a[i] == min) list.add(i);
	    }
	    list.add(a.length);
	    int l = 0;
	    for(int i = 0; i < list.size(); i++){
	        int r = list.get(i);
	        int len = r - l;
	        if(len > 0){
	            int[] next = new int[len];
	            for(int j = l; j < l + len; j++){
	                next[j - l] = a[j] - min;
	            }
	            dfs(next);
	        }
	        l = r + 1;
	    }
	}
}
