import java.io.*;
import java.math.*;
import java.util.*;
public class Main { 
 
	public static void main(String[] args) { //X^0=X, therefore for null case, return 0
		FastScanner I = new FastScanner(); //Input
		OutPut O = new OutPut(); //Output
		int N = I.nextInt();
		int ans = 0;
		int[] h = new int[N];
		for (int i = 0; i < N; i++) h[i] = I.nextInt();
		while (true) { //Find longest sub-array such that h[L...R] all >= 1 in each state of h
		//Then subtract
			//for (int i = 0; i < N; i++) O.p(h[i]+" ");
			O.p("\n");
			int L = 0;
			int R = 0;
			int max = 0;
			int curmax = 0;
			boolean sub = false;
			for (int i = 0; i < N; i++) {
				if (h[i]>0) {
					curmax++;
					sub=true;
					for (int next = i+1; next<N; next++) {
						if (h[next]==0) {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						}else curmax++;
					}
					if (curmax>0) break; //End of array has been reached
				}
			}
			if (curmax>max) {
				max=curmax;
				L=N-1-curmax;
				L++; //Length 1 means same thing, therefore ++ for nullifying over-counting
				R=N-1;
			}
			if (!sub) break;
			for (int chk = L; chk<=R; chk++) h[chk]--; //Decrementing best sub-array
			ans++;
		}
		O.pln(ans);
	}
	public static void build(long[] segtree, long[] a, int start, int end, int node) {
		if (start==end) segtree[node] = a[start];
		else{ //Need else statements for void methods
			int mid = (start+end)/2;
			build(segtree,a,start,mid,2*node); //Build from left child onwards
			build(segtree,a,mid+1,end,2*node+1); //Build from right child onwards
			segtree[node] = segtree[2*node]^segtree[2*node+1]; //Current node stores XOR of children
			//(Order of XOR doesn't matter, just occurrences of 1 bit in a certain slot
		}
	}
	public static long getXOR(long[] segtree, long[] a, int start, int end, int L, int R, int node) {
		if (R<start||L>end) return 0; //Returning answer that has no effect on final answer
		if (L<=start&&R>=end) return segtree[node]; //If range encapsulates current node of 
		//segment tree
		int mid = (start+end)/2; //Partial case --> Recurse deeper into segment tree
		long left = getXOR(segtree,a,start,mid,L,R,node*2);
		long right = getXOR(segtree,a,mid+1,end,L,R,2*node+1);
		return left^right;
	}
	public static long ceil(long num, long den) {long ans = num/den; if (num%den!=0) 
	ans++; return ans;}
	public static long GCD(long a, long b) {
		if (a==0||b==0) return Math.max(a,b);
		return GCD(Math.min(a, b),Math.max(a, b)%Math.min(a, b));
	}
	public static long FastExp(long base, long exp, long mod) {
		long ans=1;
		while (exp>0) {
			if (exp%2==1) ans*=base;
			exp/=2;
			base*=base;
			base%=mod;
			ans%=mod;
		}
		return ans;
	}
	public static long ModInv(long num,long mod) {return FastExp(num,mod-2,mod);}
	public static int pop(long x) { //Returns number of bits within a number
		int cnt = 0;
		while (x>0) {
			if (x%2==1) cnt++;
			x/=2;
		}
		return cnt;
	}
	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		int nextInt() {return Integer.parseInt(next());}
		long nextLong() {return Long.parseLong(next());};
	}
	static class OutPut{
		PrintWriter w = new PrintWriter(System.out);
		void pln(int x) {w.println(x);w.flush();}
		void pln(long x) {w.println(x);w.flush();}
		void pln(String x) {w.println(x);w.flush();}
		void pln(char x) {w.println(x);w.flush();}
		void pln(StringBuilder x) {w.println(x);w.flush();}
		void pln(BigInteger x) {w.println(x);w.flush();}
		void p(int x) {w.print(x);w.flush();}
		void p(long x) {w.print(x);w.flush();}
		void p(String x) {w.print(x);w.flush();}
		void p(char x) {w.print(x);w.flush();}
		void p(StringBuilder x) {w.print(x);w.flush();}
		void p(BigInteger x) {w.print(x);w.flush();}
	}
} import java.io.*; io.* java import java.math.*; math.* java import java.util.*; util.* java public class Main { 
 
	public static void main(String[] args) { //X^0=X, therefore for null case, return 0
		FastScanner I = new FastScanner(); //Input
		OutPut O = new OutPut(); //Output
		int N = I.nextInt();
		int ans = 0;
		int[] h = new int[N];
		for (int i = 0; i < N; i++) h[i] = I.nextInt();
		while (true) { //Find longest sub-array such that h[L...R] all >= 1 in each state of h
		//Then subtract
			//for (int i = 0; i < N; i++) O.p(h[i]+" ");
			O.p("\n");
			int L = 0;
			int R = 0;
			int max = 0;
			int curmax = 0;
			boolean sub = false;
			for (int i = 0; i < N; i++) {
				if (h[i]>0) {
					curmax++;
					sub=true;
					for (int next = i+1; next<N; next++) {
						if (h[next]==0) {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						}else curmax++;
					}
					if (curmax>0) break; //End of array has been reached
				}
			}
			if (curmax>max) {
				max=curmax;
				L=N-1-curmax;
				L++; //Length 1 means same thing, therefore ++ for nullifying over-counting
				R=N-1;
			}
			if (!sub) break;
			for (int chk = L; chk<=R; chk++) h[chk]--; //Decrementing best sub-array
			ans++;
		}
		O.pln(ans);
	}
	public static void build(long[] segtree, long[] a, int start, int end, int node) {
		if (start==end) segtree[node] = a[start];
		else{ //Need else statements for void methods
			int mid = (start+end)/2;
			build(segtree,a,start,mid,2*node); //Build from left child onwards
			build(segtree,a,mid+1,end,2*node+1); //Build from right child onwards
			segtree[node] = segtree[2*node]^segtree[2*node+1]; //Current node stores XOR of children
			//(Order of XOR doesn't matter, just occurrences of 1 bit in a certain slot
		}
	}
	public static long getXOR(long[] segtree, long[] a, int start, int end, int L, int R, int node) {
		if (R<start||L>end) return 0; //Returning answer that has no effect on final answer
		if (L<=start&&R>=end) return segtree[node]; //If range encapsulates current node of 
		//segment tree
		int mid = (start+end)/2; //Partial case --> Recurse deeper into segment tree
		long left = getXOR(segtree,a,start,mid,L,R,node*2);
		long right = getXOR(segtree,a,mid+1,end,L,R,2*node+1);
		return left^right;
	}
	public static long ceil(long num, long den) {long ans = num/den; if (num%den!=0) 
	ans++; return ans;}
	public static long GCD(long a, long b) {
		if (a==0||b==0) return Math.max(a,b);
		return GCD(Math.min(a, b),Math.max(a, b)%Math.min(a, b));
	}
	public static long FastExp(long base, long exp, long mod) {
		long ans=1;
		while (exp>0) {
			if (exp%2==1) ans*=base;
			exp/=2;
			base*=base;
			base%=mod;
			ans%=mod;
		}
		return ans;
	}
	public static long ModInv(long num,long mod) {return FastExp(num,mod-2,mod);}
	public static int pop(long x) { //Returns number of bits within a number
		int cnt = 0;
		while (x>0) {
			if (x%2==1) cnt++;
			x/=2;
		}
		return cnt;
	}
	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		int nextInt() {return Integer.parseInt(next());}
		long nextLong() {return Long.parseLong(next());};
	}
	static class OutPut{
		PrintWriter w = new PrintWriter(System.out);
		void pln(int x) {w.println(x);w.flush();}
		void pln(long x) {w.println(x);w.flush();}
		void pln(String x) {w.println(x);w.flush();}
		void pln(char x) {w.println(x);w.flush();}
		void pln(StringBuilder x) {w.println(x);w.flush();}
		void pln(BigInteger x) {w.println(x);w.flush();}
		void p(int x) {w.print(x);w.flush();}
		void p(long x) {w.print(x);w.flush();}
		void p(String x) {w.print(x);w.flush();}
		void p(char x) {w.print(x);w.flush();}
		void p(StringBuilder x) {w.print(x);w.flush();}
		void p(BigInteger x) {w.print(x);w.flush();}
	}
} Main public static void main(String[] args) { //X^0=X, therefore for null case, return 0
		FastScanner I = new FastScanner(); //Input
		OutPut O = new OutPut(); //Output
		int N = I.nextInt();
		int ans = 0;
		int[] h = new int[N];
		for (int i = 0; i < N; i++) h[i] = I.nextInt();
		while (true) { //Find longest sub-array such that h[L...R] all >= 1 in each state of h
		//Then subtract
			//for (int i = 0; i < N; i++) O.p(h[i]+" ");
			O.p("\n");
			int L = 0;
			int R = 0;
			int max = 0;
			int curmax = 0;
			boolean sub = false;
			for (int i = 0; i < N; i++) {
				if (h[i]>0) {
					curmax++;
					sub=true;
					for (int next = i+1; next<N; next++) {
						if (h[next]==0) {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						}else curmax++;
					}
					if (curmax>0) break; //End of array has been reached
				}
			}
			if (curmax>max) {
				max=curmax;
				L=N-1-curmax;
				L++; //Length 1 means same thing, therefore ++ for nullifying over-counting
				R=N-1;
			}
			if (!sub) break;
			for (int chk = L; chk<=R; chk++) h[chk]--; //Decrementing best sub-array
			ans++;
		}
		O.pln(ans);
	} main { //X^0=X, therefore for null case, return 0
		FastScanner I = new FastScanner(); //Input
		OutPut O = new OutPut(); //Output
		int N = I.nextInt();
		int ans = 0;
		int[] h = new int[N];
		for (int i = 0; i < N; i++) h[i] = I.nextInt();
		while (true) { //Find longest sub-array such that h[L...R] all >= 1 in each state of h
		//Then subtract
			//for (int i = 0; i < N; i++) O.p(h[i]+" ");
			O.p("\n");
			int L = 0;
			int R = 0;
			int max = 0;
			int curmax = 0;
			boolean sub = false;
			for (int i = 0; i < N; i++) {
				if (h[i]>0) {
					curmax++;
					sub=true;
					for (int next = i+1; next<N; next++) {
						if (h[next]==0) {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						}else curmax++;
					}
					if (curmax>0) break; //End of array has been reached
				}
			}
			if (curmax>max) {
				max=curmax;
				L=N-1-curmax;
				L++; //Length 1 means same thing, therefore ++ for nullifying over-counting
				R=N-1;
			}
			if (!sub) break;
			for (int chk = L; chk<=R; chk++) h[chk]--; //Decrementing best sub-array
			ans++;
		}
		O.pln(ans);
	} FastScanner I = new FastScanner(); I new FastScanner() OutPut O = new OutPut(); O new OutPut() int N = I.nextInt(); N I.nextInt() I.nextInt I int ans = 0; ans 0 int[] h = new int[N]; h new int[N] N for (int i = 0; i < N; i++) h[i] = I.nextInt(); int i = 0; int i = 0; i 0 i < N i N i++ i++ i h[i] = I.nextInt(); h[i] = I.nextInt() h[i] h i I.nextInt() I.nextInt I while (true) { //Find longest sub-array such that h[L...R] all >= 1 in each state of h
		//Then subtract
			//for (int i = 0; i < N; i++) O.p(h[i]+" ");
			O.p("\n");
			int L = 0;
			int R = 0;
			int max = 0;
			int curmax = 0;
			boolean sub = false;
			for (int i = 0; i < N; i++) {
				if (h[i]>0) {
					curmax++;
					sub=true;
					for (int next = i+1; next<N; next++) {
						if (h[next]==0) {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						}else curmax++;
					}
					if (curmax>0) break; //End of array has been reached
				}
			}
			if (curmax>max) {
				max=curmax;
				L=N-1-curmax;
				L++; //Length 1 means same thing, therefore ++ for nullifying over-counting
				R=N-1;
			}
			if (!sub) break;
			for (int chk = L; chk<=R; chk++) h[chk]--; //Decrementing best sub-array
			ans++;
		} true { //Find longest sub-array such that h[L...R] all >= 1 in each state of h
		//Then subtract
			//for (int i = 0; i < N; i++) O.p(h[i]+" ");
			O.p("\n");
			int L = 0;
			int R = 0;
			int max = 0;
			int curmax = 0;
			boolean sub = false;
			for (int i = 0; i < N; i++) {
				if (h[i]>0) {
					curmax++;
					sub=true;
					for (int next = i+1; next<N; next++) {
						if (h[next]==0) {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						}else curmax++;
					}
					if (curmax>0) break; //End of array has been reached
				}
			}
			if (curmax>max) {
				max=curmax;
				L=N-1-curmax;
				L++; //Length 1 means same thing, therefore ++ for nullifying over-counting
				R=N-1;
			}
			if (!sub) break;
			for (int chk = L; chk<=R; chk++) h[chk]--; //Decrementing best sub-array
			ans++;
		} O.p("\n") O.p O "\n" int L = 0; L 0 int R = 0; R 0 int max = 0; max 0 int curmax = 0; curmax 0 boolean sub = false; sub false for (int i = 0; i < N; i++) {
				if (h[i]>0) {
					curmax++;
					sub=true;
					for (int next = i+1; next<N; next++) {
						if (h[next]==0) {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						}else curmax++;
					}
					if (curmax>0) break; //End of array has been reached
				}
			} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
				if (h[i]>0) {
					curmax++;
					sub=true;
					for (int next = i+1; next<N; next++) {
						if (h[next]==0) {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						}else curmax++;
					}
					if (curmax>0) break; //End of array has been reached
				}
			} {
				if (h[i]>0) {
					curmax++;
					sub=true;
					for (int next = i+1; next<N; next++) {
						if (h[next]==0) {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						}else curmax++;
					}
					if (curmax>0) break; //End of array has been reached
				}
			} if (h[i]>0) {
					curmax++;
					sub=true;
					for (int next = i+1; next<N; next++) {
						if (h[next]==0) {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						}else curmax++;
					}
					if (curmax>0) break; //End of array has been reached
				} h[i]>0 h[i] h i 0 {
					curmax++;
					sub=true;
					for (int next = i+1; next<N; next++) {
						if (h[next]==0) {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						}else curmax++;
					}
					if (curmax>0) break; //End of array has been reached
				} curmax++ curmax sub=true sub true for (int next = i+1; next<N; next++) {
						if (h[next]==0) {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						}else curmax++;
					} int next = i+1; int next = i+1; next i+1 i 1 next<N next N next++ next++ next {
						if (h[next]==0) {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						}else curmax++;
					} {
						if (h[next]==0) {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						}else curmax++;
					} if (h[next]==0) {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						}else curmax++; h[next]==0 h[next] h next 0 {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						} if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							} curmax>max curmax max {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							} max=curmax max curmax L=i L i R=next-1 R next-1 next 1 i=next-1 i next-1 next 1 curmax=0 curmax 0 break; curmax++ curmax if (curmax>0) break; //End of array has been reached curmax>0 curmax 0 break; //End of array has been reached if (curmax>max) {
				max=curmax;
				L=N-1-curmax;
				L++; //Length 1 means same thing, therefore ++ for nullifying over-counting
				R=N-1;
			} curmax>max curmax max {
				max=curmax;
				L=N-1-curmax;
				L++; //Length 1 means same thing, therefore ++ for nullifying over-counting
				R=N-1;
			} max=curmax max curmax L=N-1-curmax L N-1-curmax N-1-curmax N 1 curmax L++ L R=N-1 R N-1 N 1 if (!sub) break; !sub sub break; for (int chk = L; chk<=R; chk++) h[chk]--; //Decrementing best sub-array int chk = L; int chk = L; chk L chk<=R chk R chk++ chk++ chk h[chk]--; //Decrementing best sub-array h[chk]-- h[chk] h chk ans++ ans O.pln(ans) O.pln O ans String[] args args public static void build(long[] segtree, long[] a, int start, int end, int node) {
		if (start==end) segtree[node] = a[start];
		else{ //Need else statements for void methods
			int mid = (start+end)/2;
			build(segtree,a,start,mid,2*node); //Build from left child onwards
			build(segtree,a,mid+1,end,2*node+1); //Build from right child onwards
			segtree[node] = segtree[2*node]^segtree[2*node+1]; //Current node stores XOR of children
			//(Order of XOR doesn't matter, just occurrences of 1 bit in a certain slot
		}
	} build {
		if (start==end) segtree[node] = a[start];
		else{ //Need else statements for void methods
			int mid = (start+end)/2;
			build(segtree,a,start,mid,2*node); //Build from left child onwards
			build(segtree,a,mid+1,end,2*node+1); //Build from right child onwards
			segtree[node] = segtree[2*node]^segtree[2*node+1]; //Current node stores XOR of children
			//(Order of XOR doesn't matter, just occurrences of 1 bit in a certain slot
		}
	} if (start==end) segtree[node] = a[start];
		else{ //Need else statements for void methods
			int mid = (start+end)/2;
			build(segtree,a,start,mid,2*node); //Build from left child onwards
			build(segtree,a,mid+1,end,2*node+1); //Build from right child onwards
			segtree[node] = segtree[2*node]^segtree[2*node+1]; //Current node stores XOR of children
			//(Order of XOR doesn't matter, just occurrences of 1 bit in a certain slot
		} start==end start end segtree[node] = a[start] segtree[node] segtree node a[start] a start { //Need else statements for void methods
			int mid = (start+end)/2;
			build(segtree,a,start,mid,2*node); //Build from left child onwards
			build(segtree,a,mid+1,end,2*node+1); //Build from right child onwards
			segtree[node] = segtree[2*node]^segtree[2*node+1]; //Current node stores XOR of children
			//(Order of XOR doesn't matter, just occurrences of 1 bit in a certain slot
		} int mid = (start+end)/2; mid (start+end)/2 (start+end) start end 2 build(segtree,a,start,mid,2*node) build segtree a start mid 2*node 2 node build(segtree,a,mid+1,end,2*node+1) build segtree a mid+1 mid 1 end 2*node+1 2*node 2 node 1 segtree[node] = segtree[2*node]^segtree[2*node+1] segtree[node] segtree node segtree[2*node]^segtree[2*node+1] segtree[2*node] segtree 2*node 2 node segtree[2*node+1] segtree 2*node+1 2*node 2 node 1 long[] segtree segtree long[] a a int start start int end end int node node public static long getXOR(long[] segtree, long[] a, int start, int end, int L, int R, int node) {
		if (R<start||L>end) return 0; //Returning answer that has no effect on final answer
		if (L<=start&&R>=end) return segtree[node]; //If range encapsulates current node of 
		//segment tree
		int mid = (start+end)/2; //Partial case --> Recurse deeper into segment tree
		long left = getXOR(segtree,a,start,mid,L,R,node*2);
		long right = getXOR(segtree,a,mid+1,end,L,R,2*node+1);
		return left^right;
	} getXOR {
		if (R<start||L>end) return 0; //Returning answer that has no effect on final answer
		if (L<=start&&R>=end) return segtree[node]; //If range encapsulates current node of 
		//segment tree
		int mid = (start+end)/2; //Partial case --> Recurse deeper into segment tree
		long left = getXOR(segtree,a,start,mid,L,R,node*2);
		long right = getXOR(segtree,a,mid+1,end,L,R,2*node+1);
		return left^right;
	} if (R<start||L>end) return 0; //Returning answer that has no effect on final answer R<start||L>end R<start R start L>end L end return 0; //Returning answer that has no effect on final answer 0 if (L<=start&&R>=end) return segtree[node]; //If range encapsulates current node of  L<=start&&R>=end L<=start L start R>=end R end return segtree[node]; //If range encapsulates current node of  segtree[node] segtree node int mid = (start+end)/2; mid (start+end)/2 (start+end) start end 2 long left = getXOR(segtree,a,start,mid,L,R,node*2); left getXOR(segtree,a,start,mid,L,R,node*2) getXOR segtree a start mid L R node*2 node 2 long right = getXOR(segtree,a,mid+1,end,L,R,2*node+1); right getXOR(segtree,a,mid+1,end,L,R,2*node+1) getXOR segtree a mid+1 mid 1 end L R 2*node+1 2*node 2 node 1 return left^right; left^right left right long[] segtree segtree long[] a a int start start int end end int L L int R R int node node public static long ceil(long num, long den) {long ans = num/den; if (num%den!=0) 
	ans++; return ans;} ceil {long ans = num/den; if (num%den!=0) 
	ans++; return ans;} long ans = num/den; ans num/den num den if (num%den!=0) 
	ans++; num%den!=0 num%den num den 0 ans++ ans return ans; ans long num num long den den public static long GCD(long a, long b) {
		if (a==0||b==0) return Math.max(a,b);
		return GCD(Math.min(a, b),Math.max(a, b)%Math.min(a, b));
	} GCD {
		if (a==0||b==0) return Math.max(a,b);
		return GCD(Math.min(a, b),Math.max(a, b)%Math.min(a, b));
	} if (a==0||b==0) return Math.max(a,b); a==0||b==0 a==0 a 0 b==0 b 0 return Math.max(a,b); Math.max(a,b) Math.max Math a b return GCD(Math.min(a, b),Math.max(a, b)%Math.min(a, b)); GCD(Math.min(a, b),Math.max(a, b)%Math.min(a, b)) GCD Math.min(a, b) Math.min Math a b Math.max(a, b)%Math.min(a, b) Math.max(a, b) Math.max Math a b Math.min(a, b) Math.min Math a b long a a long b b public static long FastExp(long base, long exp, long mod) {
		long ans=1;
		while (exp>0) {
			if (exp%2==1) ans*=base;
			exp/=2;
			base*=base;
			base%=mod;
			ans%=mod;
		}
		return ans;
	} FastExp {
		long ans=1;
		while (exp>0) {
			if (exp%2==1) ans*=base;
			exp/=2;
			base*=base;
			base%=mod;
			ans%=mod;
		}
		return ans;
	} long ans=1; ans 1 while (exp>0) {
			if (exp%2==1) ans*=base;
			exp/=2;
			base*=base;
			base%=mod;
			ans%=mod;
		} exp>0 exp 0 {
			if (exp%2==1) ans*=base;
			exp/=2;
			base*=base;
			base%=mod;
			ans%=mod;
		} if (exp%2==1) ans*=base; exp%2==1 exp%2 exp 2 1 ans*=base ans base exp/=2 exp 2 base*=base base base base%=mod base mod ans%=mod ans mod return ans; ans long base base long exp exp long mod mod public static long ModInv(long num,long mod) {return FastExp(num,mod-2,mod);} ModInv {return FastExp(num,mod-2,mod);} return FastExp(num,mod-2,mod); FastExp(num,mod-2,mod) FastExp num mod-2 mod 2 mod long num num long mod mod public static int pop(long x) { //Returns number of bits within a number
		int cnt = 0;
		while (x>0) {
			if (x%2==1) cnt++;
			x/=2;
		}
		return cnt;
	} pop { //Returns number of bits within a number
		int cnt = 0;
		while (x>0) {
			if (x%2==1) cnt++;
			x/=2;
		}
		return cnt;
	} int cnt = 0; cnt 0 while (x>0) {
			if (x%2==1) cnt++;
			x/=2;
		} x>0 x 0 {
			if (x%2==1) cnt++;
			x/=2;
		} if (x%2==1) cnt++; x%2==1 x%2 x 2 1 cnt++ cnt x/=2 x 2 return cnt; cnt long x x static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		int nextInt() {return Integer.parseInt(next());}
		long nextLong() {return Long.parseLong(next());};
	} FastScanner BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) StringTokenizer st=new StringTokenizer(""); st new StringTokenizer("") String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		} next {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		} while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				} !st.hasMoreTokens() st.hasMoreTokens() st.hasMoreTokens st try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} IOException e {
					e.printStackTrace();
				} e.printStackTrace() e.printStackTrace e {
					st=new StringTokenizer(br.readLine());
				} st=new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st int nextInt() {return Integer.parseInt(next());} nextInt {return Integer.parseInt(next());} return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next long nextLong() {return Long.parseLong(next());} nextLong {return Long.parseLong(next());} return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next static class OutPut{
		PrintWriter w = new PrintWriter(System.out);
		void pln(int x) {w.println(x);w.flush();}
		void pln(long x) {w.println(x);w.flush();}
		void pln(String x) {w.println(x);w.flush();}
		void pln(char x) {w.println(x);w.flush();}
		void pln(StringBuilder x) {w.println(x);w.flush();}
		void pln(BigInteger x) {w.println(x);w.flush();}
		void p(int x) {w.print(x);w.flush();}
		void p(long x) {w.print(x);w.flush();}
		void p(String x) {w.print(x);w.flush();}
		void p(char x) {w.print(x);w.flush();}
		void p(StringBuilder x) {w.print(x);w.flush();}
		void p(BigInteger x) {w.print(x);w.flush();}
	} OutPut PrintWriter w = new PrintWriter(System.out); w new PrintWriter(System.out) void pln(int x) {w.println(x);w.flush();} pln {w.println(x);w.flush();} w.println(x) w.println w x w.flush() w.flush w int x x void pln(long x) {w.println(x);w.flush();} pln {w.println(x);w.flush();} w.println(x) w.println w x w.flush() w.flush w long x x void pln(String x) {w.println(x);w.flush();} pln {w.println(x);w.flush();} w.println(x) w.println w x w.flush() w.flush w String x x void pln(char x) {w.println(x);w.flush();} pln {w.println(x);w.flush();} w.println(x) w.println w x w.flush() w.flush w char x x void pln(StringBuilder x) {w.println(x);w.flush();} pln {w.println(x);w.flush();} w.println(x) w.println w x w.flush() w.flush w StringBuilder x x void pln(BigInteger x) {w.println(x);w.flush();} pln {w.println(x);w.flush();} w.println(x) w.println w x w.flush() w.flush w BigInteger x x void p(int x) {w.print(x);w.flush();} p {w.print(x);w.flush();} w.print(x) w.print w x w.flush() w.flush w int x x void p(long x) {w.print(x);w.flush();} p {w.print(x);w.flush();} w.print(x) w.print w x w.flush() w.flush w long x x void p(String x) {w.print(x);w.flush();} p {w.print(x);w.flush();} w.print(x) w.print w x w.flush() w.flush w String x x void p(char x) {w.print(x);w.flush();} p {w.print(x);w.flush();} w.print(x) w.print w x w.flush() w.flush w char x x void p(StringBuilder x) {w.print(x);w.flush();} p {w.print(x);w.flush();} w.print(x) w.print w x w.flush() w.flush w StringBuilder x x void p(BigInteger x) {w.print(x);w.flush();} p {w.print(x);w.flush();} w.print(x) w.print w x w.flush() w.flush w BigInteger x x public class Main { 
 
	public static void main(String[] args) { //X^0=X, therefore for null case, return 0
		FastScanner I = new FastScanner(); //Input
		OutPut O = new OutPut(); //Output
		int N = I.nextInt();
		int ans = 0;
		int[] h = new int[N];
		for (int i = 0; i < N; i++) h[i] = I.nextInt();
		while (true) { //Find longest sub-array such that h[L...R] all >= 1 in each state of h
		//Then subtract
			//for (int i = 0; i < N; i++) O.p(h[i]+" ");
			O.p("\n");
			int L = 0;
			int R = 0;
			int max = 0;
			int curmax = 0;
			boolean sub = false;
			for (int i = 0; i < N; i++) {
				if (h[i]>0) {
					curmax++;
					sub=true;
					for (int next = i+1; next<N; next++) {
						if (h[next]==0) {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						}else curmax++;
					}
					if (curmax>0) break; //End of array has been reached
				}
			}
			if (curmax>max) {
				max=curmax;
				L=N-1-curmax;
				L++; //Length 1 means same thing, therefore ++ for nullifying over-counting
				R=N-1;
			}
			if (!sub) break;
			for (int chk = L; chk<=R; chk++) h[chk]--; //Decrementing best sub-array
			ans++;
		}
		O.pln(ans);
	}
	public static void build(long[] segtree, long[] a, int start, int end, int node) {
		if (start==end) segtree[node] = a[start];
		else{ //Need else statements for void methods
			int mid = (start+end)/2;
			build(segtree,a,start,mid,2*node); //Build from left child onwards
			build(segtree,a,mid+1,end,2*node+1); //Build from right child onwards
			segtree[node] = segtree[2*node]^segtree[2*node+1]; //Current node stores XOR of children
			//(Order of XOR doesn't matter, just occurrences of 1 bit in a certain slot
		}
	}
	public static long getXOR(long[] segtree, long[] a, int start, int end, int L, int R, int node) {
		if (R<start||L>end) return 0; //Returning answer that has no effect on final answer
		if (L<=start&&R>=end) return segtree[node]; //If range encapsulates current node of 
		//segment tree
		int mid = (start+end)/2; //Partial case --> Recurse deeper into segment tree
		long left = getXOR(segtree,a,start,mid,L,R,node*2);
		long right = getXOR(segtree,a,mid+1,end,L,R,2*node+1);
		return left^right;
	}
	public static long ceil(long num, long den) {long ans = num/den; if (num%den!=0) 
	ans++; return ans;}
	public static long GCD(long a, long b) {
		if (a==0||b==0) return Math.max(a,b);
		return GCD(Math.min(a, b),Math.max(a, b)%Math.min(a, b));
	}
	public static long FastExp(long base, long exp, long mod) {
		long ans=1;
		while (exp>0) {
			if (exp%2==1) ans*=base;
			exp/=2;
			base*=base;
			base%=mod;
			ans%=mod;
		}
		return ans;
	}
	public static long ModInv(long num,long mod) {return FastExp(num,mod-2,mod);}
	public static int pop(long x) { //Returns number of bits within a number
		int cnt = 0;
		while (x>0) {
			if (x%2==1) cnt++;
			x/=2;
		}
		return cnt;
	}
	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		int nextInt() {return Integer.parseInt(next());}
		long nextLong() {return Long.parseLong(next());};
	}
	static class OutPut{
		PrintWriter w = new PrintWriter(System.out);
		void pln(int x) {w.println(x);w.flush();}
		void pln(long x) {w.println(x);w.flush();}
		void pln(String x) {w.println(x);w.flush();}
		void pln(char x) {w.println(x);w.flush();}
		void pln(StringBuilder x) {w.println(x);w.flush();}
		void pln(BigInteger x) {w.println(x);w.flush();}
		void p(int x) {w.print(x);w.flush();}
		void p(long x) {w.print(x);w.flush();}
		void p(String x) {w.print(x);w.flush();}
		void p(char x) {w.print(x);w.flush();}
		void p(StringBuilder x) {w.print(x);w.flush();}
		void p(BigInteger x) {w.print(x);w.flush();}
	}
} public class Main { 
 
	public static void main(String[] args) { //X^0=X, therefore for null case, return 0
		FastScanner I = new FastScanner(); //Input
		OutPut O = new OutPut(); //Output
		int N = I.nextInt();
		int ans = 0;
		int[] h = new int[N];
		for (int i = 0; i < N; i++) h[i] = I.nextInt();
		while (true) { //Find longest sub-array such that h[L...R] all >= 1 in each state of h
		//Then subtract
			//for (int i = 0; i < N; i++) O.p(h[i]+" ");
			O.p("\n");
			int L = 0;
			int R = 0;
			int max = 0;
			int curmax = 0;
			boolean sub = false;
			for (int i = 0; i < N; i++) {
				if (h[i]>0) {
					curmax++;
					sub=true;
					for (int next = i+1; next<N; next++) {
						if (h[next]==0) {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						}else curmax++;
					}
					if (curmax>0) break; //End of array has been reached
				}
			}
			if (curmax>max) {
				max=curmax;
				L=N-1-curmax;
				L++; //Length 1 means same thing, therefore ++ for nullifying over-counting
				R=N-1;
			}
			if (!sub) break;
			for (int chk = L; chk<=R; chk++) h[chk]--; //Decrementing best sub-array
			ans++;
		}
		O.pln(ans);
	}
	public static void build(long[] segtree, long[] a, int start, int end, int node) {
		if (start==end) segtree[node] = a[start];
		else{ //Need else statements for void methods
			int mid = (start+end)/2;
			build(segtree,a,start,mid,2*node); //Build from left child onwards
			build(segtree,a,mid+1,end,2*node+1); //Build from right child onwards
			segtree[node] = segtree[2*node]^segtree[2*node+1]; //Current node stores XOR of children
			//(Order of XOR doesn't matter, just occurrences of 1 bit in a certain slot
		}
	}
	public static long getXOR(long[] segtree, long[] a, int start, int end, int L, int R, int node) {
		if (R<start||L>end) return 0; //Returning answer that has no effect on final answer
		if (L<=start&&R>=end) return segtree[node]; //If range encapsulates current node of 
		//segment tree
		int mid = (start+end)/2; //Partial case --> Recurse deeper into segment tree
		long left = getXOR(segtree,a,start,mid,L,R,node*2);
		long right = getXOR(segtree,a,mid+1,end,L,R,2*node+1);
		return left^right;
	}
	public static long ceil(long num, long den) {long ans = num/den; if (num%den!=0) 
	ans++; return ans;}
	public static long GCD(long a, long b) {
		if (a==0||b==0) return Math.max(a,b);
		return GCD(Math.min(a, b),Math.max(a, b)%Math.min(a, b));
	}
	public static long FastExp(long base, long exp, long mod) {
		long ans=1;
		while (exp>0) {
			if (exp%2==1) ans*=base;
			exp/=2;
			base*=base;
			base%=mod;
			ans%=mod;
		}
		return ans;
	}
	public static long ModInv(long num,long mod) {return FastExp(num,mod-2,mod);}
	public static int pop(long x) { //Returns number of bits within a number
		int cnt = 0;
		while (x>0) {
			if (x%2==1) cnt++;
			x/=2;
		}
		return cnt;
	}
	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		int nextInt() {return Integer.parseInt(next());}
		long nextLong() {return Long.parseLong(next());};
	}
	static class OutPut{
		PrintWriter w = new PrintWriter(System.out);
		void pln(int x) {w.println(x);w.flush();}
		void pln(long x) {w.println(x);w.flush();}
		void pln(String x) {w.println(x);w.flush();}
		void pln(char x) {w.println(x);w.flush();}
		void pln(StringBuilder x) {w.println(x);w.flush();}
		void pln(BigInteger x) {w.println(x);w.flush();}
		void p(int x) {w.print(x);w.flush();}
		void p(long x) {w.print(x);w.flush();}
		void p(String x) {w.print(x);w.flush();}
		void p(char x) {w.print(x);w.flush();}
		void p(StringBuilder x) {w.print(x);w.flush();}
		void p(BigInteger x) {w.print(x);w.flush();}
	}
} Main
import java.io.*;
import java.math.*;
import java.util.*;
public class Main { 
 
	public static void main(String[] args) { //X^0=X, therefore for null case, return 0
		FastScanner I = new FastScanner(); //Input
		OutPut O = new OutPut(); //Output
		int N = I.nextInt();
		int ans = 0;
		int[] h = new int[N];
		for (int i = 0; i < N; i++) h[i] = I.nextInt();
		while (true) { //Find longest sub-array such that h[L...R] all >= 1 in each state of h
		//Then subtract
			//for (int i = 0; i < N; i++) O.p(h[i]+" ");
			O.p("\n");
			int L = 0;
			int R = 0;
			int max = 0;
			int curmax = 0;
			boolean sub = false;
			for (int i = 0; i < N; i++) {
				if (h[i]>0) {
					curmax++;
					sub=true;
					for (int next = i+1; next<N; next++) {
						if (h[next]==0) {
							if (curmax>max) {
								max=curmax;
								L=i;
								R=next-1;
								i=next-1;
								curmax=0; //Refresh
							}
							break;
						}else curmax++;
					}
					if (curmax>0) break; //End of array has been reached
				}
			}
			if (curmax>max) {
				max=curmax;
				L=N-1-curmax;
				L++; //Length 1 means same thing, therefore ++ for nullifying over-counting
				R=N-1;
			}
			if (!sub) break;
			for (int chk = L; chk<=R; chk++) h[chk]--; //Decrementing best sub-array
			ans++;
		}
		O.pln(ans);
	}
	public static void build(long[] segtree, long[] a, int start, int end, int node) {
		if (start==end) segtree[node] = a[start];
		else{ //Need else statements for void methods
			int mid = (start+end)/2;
			build(segtree,a,start,mid,2*node); //Build from left child onwards
			build(segtree,a,mid+1,end,2*node+1); //Build from right child onwards
			segtree[node] = segtree[2*node]^segtree[2*node+1]; //Current node stores XOR of children
			//(Order of XOR doesn't matter, just occurrences of 1 bit in a certain slot
		}
	}
	public static long getXOR(long[] segtree, long[] a, int start, int end, int L, int R, int node) {
		if (R<start||L>end) return 0; //Returning answer that has no effect on final answer
		if (L<=start&&R>=end) return segtree[node]; //If range encapsulates current node of 
		//segment tree
		int mid = (start+end)/2; //Partial case --> Recurse deeper into segment tree
		long left = getXOR(segtree,a,start,mid,L,R,node*2);
		long right = getXOR(segtree,a,mid+1,end,L,R,2*node+1);
		return left^right;
	}
	public static long ceil(long num, long den) {long ans = num/den; if (num%den!=0) 
	ans++; return ans;}
	public static long GCD(long a, long b) {
		if (a==0||b==0) return Math.max(a,b);
		return GCD(Math.min(a, b),Math.max(a, b)%Math.min(a, b));
	}
	public static long FastExp(long base, long exp, long mod) {
		long ans=1;
		while (exp>0) {
			if (exp%2==1) ans*=base;
			exp/=2;
			base*=base;
			base%=mod;
			ans%=mod;
		}
		return ans;
	}
	public static long ModInv(long num,long mod) {return FastExp(num,mod-2,mod);}
	public static int pop(long x) { //Returns number of bits within a number
		int cnt = 0;
		while (x>0) {
			if (x%2==1) cnt++;
			x/=2;
		}
		return cnt;
	}
	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		int nextInt() {return Integer.parseInt(next());}
		long nextLong() {return Long.parseLong(next());};
	}
	static class OutPut{
		PrintWriter w = new PrintWriter(System.out);
		void pln(int x) {w.println(x);w.flush();}
		void pln(long x) {w.println(x);w.flush();}
		void pln(String x) {w.println(x);w.flush();}
		void pln(char x) {w.println(x);w.flush();}
		void pln(StringBuilder x) {w.println(x);w.flush();}
		void pln(BigInteger x) {w.println(x);w.flush();}
		void p(int x) {w.print(x);w.flush();}
		void p(long x) {w.print(x);w.flush();}
		void p(String x) {w.print(x);w.flush();}
		void p(char x) {w.print(x);w.flush();}
		void p(StringBuilder x) {w.print(x);w.flush();}
		void p(BigInteger x) {w.print(x);w.flush();}
	}
}
