import java.util.Scanner;

public class Main {
	static int[] g;
	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();
		}
		sc.close();
		int cnt = 0;
		g = new int[N];
		while(true) {
			int m = 1;
			int l = 0;
			int r = 0;
			int c = 0;
			int flag = 0;
			for(int i = 0; i < N; i++) {
				if(h[i] == g[i]) {
					flag++;
					if(flag == N) {
						System.out.println(cnt);
						System.exit(0);
					}
				}
				if(h[i] - g[i] > 0) {
					c++;
					if(c == 1) {
						m = h[i] - g[i];
					}else {
						m = Math.min(m, h[i] - g[i]);
					}
					if(i == N - 1) {
						m = Math.min(m, h[i] - g[i]);
						//System.out.println(i +" " + c);
						add(i - c + 1, i, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}else {
					if(c == 0) {
						continue;
					}else {
						add(i - c, i - 1, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}
			}
		}
	}

	static void disp(int[] a) {
		for(int i : a) {
			System.out.print(i +" ");
		}
		System.out.println();
	}
	static void add(int l, int r, int m) {
		for(int i = l; i <= r; i++) {
			g[i] += m;
		}
	}
} import java.util.Scanner; Scanner java.util public class Main {
	static int[] g;
	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();
		}
		sc.close();
		int cnt = 0;
		g = new int[N];
		while(true) {
			int m = 1;
			int l = 0;
			int r = 0;
			int c = 0;
			int flag = 0;
			for(int i = 0; i < N; i++) {
				if(h[i] == g[i]) {
					flag++;
					if(flag == N) {
						System.out.println(cnt);
						System.exit(0);
					}
				}
				if(h[i] - g[i] > 0) {
					c++;
					if(c == 1) {
						m = h[i] - g[i];
					}else {
						m = Math.min(m, h[i] - g[i]);
					}
					if(i == N - 1) {
						m = Math.min(m, h[i] - g[i]);
						//System.out.println(i +" " + c);
						add(i - c + 1, i, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}else {
					if(c == 0) {
						continue;
					}else {
						add(i - c, i - 1, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}
			}
		}
	}

	static void disp(int[] a) {
		for(int i : a) {
			System.out.print(i +" ");
		}
		System.out.println();
	}
	static void add(int l, int r, int m) {
		for(int i = l; i <= r; i++) {
			g[i] += m;
		}
	}
} Main static int[] g; g 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();
		}
		sc.close();
		int cnt = 0;
		g = new int[N];
		while(true) {
			int m = 1;
			int l = 0;
			int r = 0;
			int c = 0;
			int flag = 0;
			for(int i = 0; i < N; i++) {
				if(h[i] == g[i]) {
					flag++;
					if(flag == N) {
						System.out.println(cnt);
						System.exit(0);
					}
				}
				if(h[i] - g[i] > 0) {
					c++;
					if(c == 1) {
						m = h[i] - g[i];
					}else {
						m = Math.min(m, h[i] - g[i]);
					}
					if(i == N - 1) {
						m = Math.min(m, h[i] - g[i]);
						//System.out.println(i +" " + c);
						add(i - c + 1, i, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}else {
					if(c == 0) {
						continue;
					}else {
						add(i - c, i - 1, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}
			}
		}
	} 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();
		}
		sc.close();
		int cnt = 0;
		g = new int[N];
		while(true) {
			int m = 1;
			int l = 0;
			int r = 0;
			int c = 0;
			int flag = 0;
			for(int i = 0; i < N; i++) {
				if(h[i] == g[i]) {
					flag++;
					if(flag == N) {
						System.out.println(cnt);
						System.exit(0);
					}
				}
				if(h[i] - g[i] > 0) {
					c++;
					if(c == 1) {
						m = h[i] - g[i];
					}else {
						m = Math.min(m, h[i] - g[i]);
					}
					if(i == N - 1) {
						m = Math.min(m, h[i] - g[i]);
						//System.out.println(i +" " + c);
						add(i - c + 1, i, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}else {
					if(c == 0) {
						continue;
					}else {
						add(i - c, i - 1, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}
			}
		}
	} 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 sc.close() sc.close sc int cnt = 0; cnt 0 g = new int[N] g new int[N] N while(true) {
			int m = 1;
			int l = 0;
			int r = 0;
			int c = 0;
			int flag = 0;
			for(int i = 0; i < N; i++) {
				if(h[i] == g[i]) {
					flag++;
					if(flag == N) {
						System.out.println(cnt);
						System.exit(0);
					}
				}
				if(h[i] - g[i] > 0) {
					c++;
					if(c == 1) {
						m = h[i] - g[i];
					}else {
						m = Math.min(m, h[i] - g[i]);
					}
					if(i == N - 1) {
						m = Math.min(m, h[i] - g[i]);
						//System.out.println(i +" " + c);
						add(i - c + 1, i, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}else {
					if(c == 0) {
						continue;
					}else {
						add(i - c, i - 1, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}
			}
		} true {
			int m = 1;
			int l = 0;
			int r = 0;
			int c = 0;
			int flag = 0;
			for(int i = 0; i < N; i++) {
				if(h[i] == g[i]) {
					flag++;
					if(flag == N) {
						System.out.println(cnt);
						System.exit(0);
					}
				}
				if(h[i] - g[i] > 0) {
					c++;
					if(c == 1) {
						m = h[i] - g[i];
					}else {
						m = Math.min(m, h[i] - g[i]);
					}
					if(i == N - 1) {
						m = Math.min(m, h[i] - g[i]);
						//System.out.println(i +" " + c);
						add(i - c + 1, i, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}else {
					if(c == 0) {
						continue;
					}else {
						add(i - c, i - 1, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}
			}
		} int m = 1; m 1 int l = 0; l 0 int r = 0; r 0 int c = 0; c 0 int flag = 0; flag 0 for(int i = 0; i < N; i++) {
				if(h[i] == g[i]) {
					flag++;
					if(flag == N) {
						System.out.println(cnt);
						System.exit(0);
					}
				}
				if(h[i] - g[i] > 0) {
					c++;
					if(c == 1) {
						m = h[i] - g[i];
					}else {
						m = Math.min(m, h[i] - g[i]);
					}
					if(i == N - 1) {
						m = Math.min(m, h[i] - g[i]);
						//System.out.println(i +" " + c);
						add(i - c + 1, i, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}else {
					if(c == 0) {
						continue;
					}else {
						add(i - c, i - 1, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}
			} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
				if(h[i] == g[i]) {
					flag++;
					if(flag == N) {
						System.out.println(cnt);
						System.exit(0);
					}
				}
				if(h[i] - g[i] > 0) {
					c++;
					if(c == 1) {
						m = h[i] - g[i];
					}else {
						m = Math.min(m, h[i] - g[i]);
					}
					if(i == N - 1) {
						m = Math.min(m, h[i] - g[i]);
						//System.out.println(i +" " + c);
						add(i - c + 1, i, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}else {
					if(c == 0) {
						continue;
					}else {
						add(i - c, i - 1, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}
			} {
				if(h[i] == g[i]) {
					flag++;
					if(flag == N) {
						System.out.println(cnt);
						System.exit(0);
					}
				}
				if(h[i] - g[i] > 0) {
					c++;
					if(c == 1) {
						m = h[i] - g[i];
					}else {
						m = Math.min(m, h[i] - g[i]);
					}
					if(i == N - 1) {
						m = Math.min(m, h[i] - g[i]);
						//System.out.println(i +" " + c);
						add(i - c + 1, i, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}else {
					if(c == 0) {
						continue;
					}else {
						add(i - c, i - 1, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}
			} if(h[i] == g[i]) {
					flag++;
					if(flag == N) {
						System.out.println(cnt);
						System.exit(0);
					}
				} h[i] == g[i] h[i] h i g[i] g i {
					flag++;
					if(flag == N) {
						System.out.println(cnt);
						System.exit(0);
					}
				} flag++ flag if(flag == N) {
						System.out.println(cnt);
						System.exit(0);
					} flag == N flag N {
						System.out.println(cnt);
						System.exit(0);
					} System.out.println(cnt) System.out.println System.out System System.out cnt System.exit(0) System.exit System 0 if(h[i] - g[i] > 0) {
					c++;
					if(c == 1) {
						m = h[i] - g[i];
					}else {
						m = Math.min(m, h[i] - g[i]);
					}
					if(i == N - 1) {
						m = Math.min(m, h[i] - g[i]);
						//System.out.println(i +" " + c);
						add(i - c + 1, i, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}else {
					if(c == 0) {
						continue;
					}else {
						add(i - c, i - 1, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				} h[i] - g[i] > 0 h[i] - g[i] h[i] h i g[i] g i 0 {
					c++;
					if(c == 1) {
						m = h[i] - g[i];
					}else {
						m = Math.min(m, h[i] - g[i]);
					}
					if(i == N - 1) {
						m = Math.min(m, h[i] - g[i]);
						//System.out.println(i +" " + c);
						add(i - c + 1, i, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				} c++ c if(c == 1) {
						m = h[i] - g[i];
					}else {
						m = Math.min(m, h[i] - g[i]);
					} c == 1 c 1 {
						m = h[i] - g[i];
					} m = h[i] - g[i] m h[i] - g[i] h[i] h i g[i] g i {
						m = Math.min(m, h[i] - g[i]);
					} m = Math.min(m, h[i] - g[i]) m Math.min(m, h[i] - g[i]) Math.min Math m h[i] - g[i] h[i] h i g[i] g i if(i == N - 1) {
						m = Math.min(m, h[i] - g[i]);
						//System.out.println(i +" " + c);
						add(i - c + 1, i, m);
						//disp(g);
						c = 0;
						cnt += m;
					} i == N - 1 i N - 1 N 1 {
						m = Math.min(m, h[i] - g[i]);
						//System.out.println(i +" " + c);
						add(i - c + 1, i, m);
						//disp(g);
						c = 0;
						cnt += m;
					} m = Math.min(m, h[i] - g[i]) m Math.min(m, h[i] - g[i]) Math.min Math m h[i] - g[i] h[i] h i g[i] g i add(i - c + 1, i, m) add i - c + 1 i - c i c 1 i m c = 0 c 0 cnt += m cnt m {
					if(c == 0) {
						continue;
					}else {
						add(i - c, i - 1, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				} if(c == 0) {
						continue;
					}else {
						add(i - c, i - 1, m);
						//disp(g);
						c = 0;
						cnt += m;
					} c == 0 c 0 {
						continue;
					} continue; {
						add(i - c, i - 1, m);
						//disp(g);
						c = 0;
						cnt += m;
					} add(i - c, i - 1, m) add i - c i c i - 1 i 1 m c = 0 c 0 cnt += m cnt m String[] args args static void disp(int[] a) {
		for(int i : a) {
			System.out.print(i +" ");
		}
		System.out.println();
	} disp {
		for(int i : a) {
			System.out.print(i +" ");
		}
		System.out.println();
	} for(int i : a) {
			System.out.print(i +" ");
		} int i a {
			System.out.print(i +" ");
		} {
			System.out.print(i +" ");
		} System.out.print(i +" ") System.out.print System.out System System.out i +" " i " " System.out.println() System.out.println System.out System System.out int[] a a static void add(int l, int r, int m) {
		for(int i = l; i <= r; i++) {
			g[i] += m;
		}
	} add {
		for(int i = l; i <= r; i++) {
			g[i] += m;
		}
	} for(int i = l; i <= r; i++) {
			g[i] += m;
		} int i = l; int i = l; i l i <= r i r i++ i++ i {
			g[i] += m;
		} {
			g[i] += m;
		} g[i] += m g[i] g i m int l l int r r int m m public class Main {
	static int[] g;
	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();
		}
		sc.close();
		int cnt = 0;
		g = new int[N];
		while(true) {
			int m = 1;
			int l = 0;
			int r = 0;
			int c = 0;
			int flag = 0;
			for(int i = 0; i < N; i++) {
				if(h[i] == g[i]) {
					flag++;
					if(flag == N) {
						System.out.println(cnt);
						System.exit(0);
					}
				}
				if(h[i] - g[i] > 0) {
					c++;
					if(c == 1) {
						m = h[i] - g[i];
					}else {
						m = Math.min(m, h[i] - g[i]);
					}
					if(i == N - 1) {
						m = Math.min(m, h[i] - g[i]);
						//System.out.println(i +" " + c);
						add(i - c + 1, i, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}else {
					if(c == 0) {
						continue;
					}else {
						add(i - c, i - 1, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}
			}
		}
	}

	static void disp(int[] a) {
		for(int i : a) {
			System.out.print(i +" ");
		}
		System.out.println();
	}
	static void add(int l, int r, int m) {
		for(int i = l; i <= r; i++) {
			g[i] += m;
		}
	}
} public class Main {
	static int[] g;
	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();
		}
		sc.close();
		int cnt = 0;
		g = new int[N];
		while(true) {
			int m = 1;
			int l = 0;
			int r = 0;
			int c = 0;
			int flag = 0;
			for(int i = 0; i < N; i++) {
				if(h[i] == g[i]) {
					flag++;
					if(flag == N) {
						System.out.println(cnt);
						System.exit(0);
					}
				}
				if(h[i] - g[i] > 0) {
					c++;
					if(c == 1) {
						m = h[i] - g[i];
					}else {
						m = Math.min(m, h[i] - g[i]);
					}
					if(i == N - 1) {
						m = Math.min(m, h[i] - g[i]);
						//System.out.println(i +" " + c);
						add(i - c + 1, i, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}else {
					if(c == 0) {
						continue;
					}else {
						add(i - c, i - 1, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}
			}
		}
	}

	static void disp(int[] a) {
		for(int i : a) {
			System.out.print(i +" ");
		}
		System.out.println();
	}
	static void add(int l, int r, int m) {
		for(int i = l; i <= r; i++) {
			g[i] += m;
		}
	}
} Main
import java.util.Scanner;

public class Main {
	static int[] g;
	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();
		}
		sc.close();
		int cnt = 0;
		g = new int[N];
		while(true) {
			int m = 1;
			int l = 0;
			int r = 0;
			int c = 0;
			int flag = 0;
			for(int i = 0; i < N; i++) {
				if(h[i] == g[i]) {
					flag++;
					if(flag == N) {
						System.out.println(cnt);
						System.exit(0);
					}
				}
				if(h[i] - g[i] > 0) {
					c++;
					if(c == 1) {
						m = h[i] - g[i];
					}else {
						m = Math.min(m, h[i] - g[i]);
					}
					if(i == N - 1) {
						m = Math.min(m, h[i] - g[i]);
						//System.out.println(i +" " + c);
						add(i - c + 1, i, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}else {
					if(c == 0) {
						continue;
					}else {
						add(i - c, i - 1, m);
						//disp(g);
						c = 0;
						cnt += m;
					}
				}
			}
		}
	}

	static void disp(int[] a) {
		for(int i : a) {
			System.out.print(i +" ");
		}
		System.out.println();
	}
	static void add(int l, int r, int m) {
		for(int i = l; i <= r; i++) {
			g[i] += m;
		}
	}
}