import java.util.Scanner;
import java.util.Stack;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] k = new int[n];
		for(int i=0;i<n;i++) {
			k[i] = in.nextInt();
		}
		Stack<int[]> stack = new Stack<>();
		int[] start = new int[2];
		start[0] = 0;
		start[1] = n-1;
		stack.push(start);
		int ans = 0;
		while(!stack.isEmpty()) {
			int[] tmp = stack.pop();
			int idx_l = tmp[0];
			int idx_r = tmp[1];
			int min = Integer.MAX_VALUE;
			int min_idx = -1;
			for(int i=idx_l;i<=idx_r;i++) {
				if(k[i]<min) {
					min = k[i];
					min_idx = i;
				}
			}
			ans += min;
			for(int i=idx_l;i<=idx_r;i++) {
				k[i] -= min;
			}
			if(idx_l<=min_idx-1) {
				int[] left = new int[2];
				left[0] = idx_l;
				left[1] = min_idx-1;
				stack.push(left);
			}
			if(min_idx+1<=idx_r) {
				int[] right = new int[2];
				right[0] = min_idx+1;
				right[1] = idx_r;
				stack.push(right);
			}	
		}
		System.out.println(ans);
		in.close();
	}

} import java.util.Scanner; Scanner java.util import java.util.Stack; Stack java.util public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] k = new int[n];
		for(int i=0;i<n;i++) {
			k[i] = in.nextInt();
		}
		Stack<int[]> stack = new Stack<>();
		int[] start = new int[2];
		start[0] = 0;
		start[1] = n-1;
		stack.push(start);
		int ans = 0;
		while(!stack.isEmpty()) {
			int[] tmp = stack.pop();
			int idx_l = tmp[0];
			int idx_r = tmp[1];
			int min = Integer.MAX_VALUE;
			int min_idx = -1;
			for(int i=idx_l;i<=idx_r;i++) {
				if(k[i]<min) {
					min = k[i];
					min_idx = i;
				}
			}
			ans += min;
			for(int i=idx_l;i<=idx_r;i++) {
				k[i] -= min;
			}
			if(idx_l<=min_idx-1) {
				int[] left = new int[2];
				left[0] = idx_l;
				left[1] = min_idx-1;
				stack.push(left);
			}
			if(min_idx+1<=idx_r) {
				int[] right = new int[2];
				right[0] = min_idx+1;
				right[1] = idx_r;
				stack.push(right);
			}	
		}
		System.out.println(ans);
		in.close();
	}

} Main public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] k = new int[n];
		for(int i=0;i<n;i++) {
			k[i] = in.nextInt();
		}
		Stack<int[]> stack = new Stack<>();
		int[] start = new int[2];
		start[0] = 0;
		start[1] = n-1;
		stack.push(start);
		int ans = 0;
		while(!stack.isEmpty()) {
			int[] tmp = stack.pop();
			int idx_l = tmp[0];
			int idx_r = tmp[1];
			int min = Integer.MAX_VALUE;
			int min_idx = -1;
			for(int i=idx_l;i<=idx_r;i++) {
				if(k[i]<min) {
					min = k[i];
					min_idx = i;
				}
			}
			ans += min;
			for(int i=idx_l;i<=idx_r;i++) {
				k[i] -= min;
			}
			if(idx_l<=min_idx-1) {
				int[] left = new int[2];
				left[0] = idx_l;
				left[1] = min_idx-1;
				stack.push(left);
			}
			if(min_idx+1<=idx_r) {
				int[] right = new int[2];
				right[0] = min_idx+1;
				right[1] = idx_r;
				stack.push(right);
			}	
		}
		System.out.println(ans);
		in.close();
	} main {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] k = new int[n];
		for(int i=0;i<n;i++) {
			k[i] = in.nextInt();
		}
		Stack<int[]> stack = new Stack<>();
		int[] start = new int[2];
		start[0] = 0;
		start[1] = n-1;
		stack.push(start);
		int ans = 0;
		while(!stack.isEmpty()) {
			int[] tmp = stack.pop();
			int idx_l = tmp[0];
			int idx_r = tmp[1];
			int min = Integer.MAX_VALUE;
			int min_idx = -1;
			for(int i=idx_l;i<=idx_r;i++) {
				if(k[i]<min) {
					min = k[i];
					min_idx = i;
				}
			}
			ans += min;
			for(int i=idx_l;i<=idx_r;i++) {
				k[i] -= min;
			}
			if(idx_l<=min_idx-1) {
				int[] left = new int[2];
				left[0] = idx_l;
				left[1] = min_idx-1;
				stack.push(left);
			}
			if(min_idx+1<=idx_r) {
				int[] right = new int[2];
				right[0] = min_idx+1;
				right[1] = idx_r;
				stack.push(right);
			}	
		}
		System.out.println(ans);
		in.close();
	} Scanner in = new Scanner(System.in); in new Scanner(System.in) int n = in.nextInt(); n in.nextInt() in.nextInt in int[] k = new int[n]; k new int[n] n for(int i=0;i<n;i++) {
			k[i] = in.nextInt();
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			k[i] = in.nextInt();
		} {
			k[i] = in.nextInt();
		} k[i] = in.nextInt() k[i] k i in.nextInt() in.nextInt in Stack<int[]> stack = new Stack<>(); stack new Stack<>() int[] start = new int[2]; start new int[2] 2 start[0] = 0 start[0] start 0 0 start[1] = n-1 start[1] start 1 n-1 n 1 stack.push(start) stack.push stack start int ans = 0; ans 0 while(!stack.isEmpty()) {
			int[] tmp = stack.pop();
			int idx_l = tmp[0];
			int idx_r = tmp[1];
			int min = Integer.MAX_VALUE;
			int min_idx = -1;
			for(int i=idx_l;i<=idx_r;i++) {
				if(k[i]<min) {
					min = k[i];
					min_idx = i;
				}
			}
			ans += min;
			for(int i=idx_l;i<=idx_r;i++) {
				k[i] -= min;
			}
			if(idx_l<=min_idx-1) {
				int[] left = new int[2];
				left[0] = idx_l;
				left[1] = min_idx-1;
				stack.push(left);
			}
			if(min_idx+1<=idx_r) {
				int[] right = new int[2];
				right[0] = min_idx+1;
				right[1] = idx_r;
				stack.push(right);
			}	
		} !stack.isEmpty() stack.isEmpty() stack.isEmpty stack {
			int[] tmp = stack.pop();
			int idx_l = tmp[0];
			int idx_r = tmp[1];
			int min = Integer.MAX_VALUE;
			int min_idx = -1;
			for(int i=idx_l;i<=idx_r;i++) {
				if(k[i]<min) {
					min = k[i];
					min_idx = i;
				}
			}
			ans += min;
			for(int i=idx_l;i<=idx_r;i++) {
				k[i] -= min;
			}
			if(idx_l<=min_idx-1) {
				int[] left = new int[2];
				left[0] = idx_l;
				left[1] = min_idx-1;
				stack.push(left);
			}
			if(min_idx+1<=idx_r) {
				int[] right = new int[2];
				right[0] = min_idx+1;
				right[1] = idx_r;
				stack.push(right);
			}	
		} int[] tmp = stack.pop(); tmp stack.pop() stack.pop stack int idx_l = tmp[0]; idx_l tmp[0] tmp 0 int idx_r = tmp[1]; idx_r tmp[1] tmp 1 int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE int min_idx = -1; min_idx -1 1 for(int i=idx_l;i<=idx_r;i++) {
				if(k[i]<min) {
					min = k[i];
					min_idx = i;
				}
			} int i=idx_l; int i=idx_l; i idx_l i<=idx_r i idx_r i++ i++ i {
				if(k[i]<min) {
					min = k[i];
					min_idx = i;
				}
			} {
				if(k[i]<min) {
					min = k[i];
					min_idx = i;
				}
			} if(k[i]<min) {
					min = k[i];
					min_idx = i;
				} k[i]<min k[i] k i min {
					min = k[i];
					min_idx = i;
				} min = k[i] min k[i] k i min_idx = i min_idx i ans += min ans min for(int i=idx_l;i<=idx_r;i++) {
				k[i] -= min;
			} int i=idx_l; int i=idx_l; i idx_l i<=idx_r i idx_r i++ i++ i {
				k[i] -= min;
			} {
				k[i] -= min;
			} k[i] -= min k[i] k i min if(idx_l<=min_idx-1) {
				int[] left = new int[2];
				left[0] = idx_l;
				left[1] = min_idx-1;
				stack.push(left);
			} idx_l<=min_idx-1 idx_l min_idx-1 min_idx 1 {
				int[] left = new int[2];
				left[0] = idx_l;
				left[1] = min_idx-1;
				stack.push(left);
			} int[] left = new int[2]; left new int[2] 2 left[0] = idx_l left[0] left 0 idx_l left[1] = min_idx-1 left[1] left 1 min_idx-1 min_idx 1 stack.push(left) stack.push stack left if(min_idx+1<=idx_r) {
				int[] right = new int[2];
				right[0] = min_idx+1;
				right[1] = idx_r;
				stack.push(right);
			} min_idx+1<=idx_r min_idx+1 min_idx 1 idx_r {
				int[] right = new int[2];
				right[0] = min_idx+1;
				right[1] = idx_r;
				stack.push(right);
			} int[] right = new int[2]; right new int[2] 2 right[0] = min_idx+1 right[0] right 0 min_idx+1 min_idx 1 right[1] = idx_r right[1] right 1 idx_r stack.push(right) stack.push stack right System.out.println(ans) System.out.println System.out System System.out ans in.close() in.close in String[] args args public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] k = new int[n];
		for(int i=0;i<n;i++) {
			k[i] = in.nextInt();
		}
		Stack<int[]> stack = new Stack<>();
		int[] start = new int[2];
		start[0] = 0;
		start[1] = n-1;
		stack.push(start);
		int ans = 0;
		while(!stack.isEmpty()) {
			int[] tmp = stack.pop();
			int idx_l = tmp[0];
			int idx_r = tmp[1];
			int min = Integer.MAX_VALUE;
			int min_idx = -1;
			for(int i=idx_l;i<=idx_r;i++) {
				if(k[i]<min) {
					min = k[i];
					min_idx = i;
				}
			}
			ans += min;
			for(int i=idx_l;i<=idx_r;i++) {
				k[i] -= min;
			}
			if(idx_l<=min_idx-1) {
				int[] left = new int[2];
				left[0] = idx_l;
				left[1] = min_idx-1;
				stack.push(left);
			}
			if(min_idx+1<=idx_r) {
				int[] right = new int[2];
				right[0] = min_idx+1;
				right[1] = idx_r;
				stack.push(right);
			}	
		}
		System.out.println(ans);
		in.close();
	}

} public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] k = new int[n];
		for(int i=0;i<n;i++) {
			k[i] = in.nextInt();
		}
		Stack<int[]> stack = new Stack<>();
		int[] start = new int[2];
		start[0] = 0;
		start[1] = n-1;
		stack.push(start);
		int ans = 0;
		while(!stack.isEmpty()) {
			int[] tmp = stack.pop();
			int idx_l = tmp[0];
			int idx_r = tmp[1];
			int min = Integer.MAX_VALUE;
			int min_idx = -1;
			for(int i=idx_l;i<=idx_r;i++) {
				if(k[i]<min) {
					min = k[i];
					min_idx = i;
				}
			}
			ans += min;
			for(int i=idx_l;i<=idx_r;i++) {
				k[i] -= min;
			}
			if(idx_l<=min_idx-1) {
				int[] left = new int[2];
				left[0] = idx_l;
				left[1] = min_idx-1;
				stack.push(left);
			}
			if(min_idx+1<=idx_r) {
				int[] right = new int[2];
				right[0] = min_idx+1;
				right[1] = idx_r;
				stack.push(right);
			}	
		}
		System.out.println(ans);
		in.close();
	}

} Main
import java.util.Scanner;
import java.util.Stack;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int[] k = new int[n];
		for(int i=0;i<n;i++) {
			k[i] = in.nextInt();
		}
		Stack<int[]> stack = new Stack<>();
		int[] start = new int[2];
		start[0] = 0;
		start[1] = n-1;
		stack.push(start);
		int ans = 0;
		while(!stack.isEmpty()) {
			int[] tmp = stack.pop();
			int idx_l = tmp[0];
			int idx_r = tmp[1];
			int min = Integer.MAX_VALUE;
			int min_idx = -1;
			for(int i=idx_l;i<=idx_r;i++) {
				if(k[i]<min) {
					min = k[i];
					min_idx = i;
				}
			}
			ans += min;
			for(int i=idx_l;i<=idx_r;i++) {
				k[i] -= min;
			}
			if(idx_l<=min_idx-1) {
				int[] left = new int[2];
				left[0] = idx_l;
				left[1] = min_idx-1;
				stack.push(left);
			}
			if(min_idx+1<=idx_r) {
				int[] right = new int[2];
				right[0] = min_idx+1;
				right[1] = idx_r;
				stack.push(right);
			}	
		}
		System.out.println(ans);
		in.close();
	}

}
