import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Main {
	public int solve(int N, int[] hs) {
		return getTimes(hs, 0, 0, N-1);
	}
	
	public int getTimes(int[] hs, int height, int start, int end) {
		if (start > end) {
			return 0;
		} else if (start == end) {
			return hs[start] - height;
		}
		
		List<Integer> list = new LinkedList<Integer>();
		int min = Integer.MAX_VALUE;
		
		for (int i=start; i<=end; i++) {
			if (hs[i] < min) {
				min = hs[i];
				list.clear();
				list.add(i);
			} else if (hs[i] == min) {
				list.add(i);
			}
		}

		int result = min - height;	// start~endの範囲の一番低い高さまで水を入れる
		
		int nextIndex = start;
		for (int x : list) {
			// minの高さで区切られた領域の回数を数える
			result += getTimes(hs, min, nextIndex, x-1);
			nextIndex = x+1;
		}
		// 最後のminのIndex～endの領域も忘れない
		result += getTimes(hs, min, nextIndex, end);

		return result;
	}
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int[] hs = new int[N];
		
		for (int i=0; i<N; i++) {
			hs[i] = in.nextInt();
		}
		in.close();
		
		Main main = new Main();
		int result = main.solve(N, hs);
		
		System.out.println(result);
	}
} import java.util.ArrayList; ArrayList java.util import java.util.LinkedList; LinkedList java.util import java.util.List; List java.util import java.util.Scanner; Scanner java.util public class Main {
	public int solve(int N, int[] hs) {
		return getTimes(hs, 0, 0, N-1);
	}
	
	public int getTimes(int[] hs, int height, int start, int end) {
		if (start > end) {
			return 0;
		} else if (start == end) {
			return hs[start] - height;
		}
		
		List<Integer> list = new LinkedList<Integer>();
		int min = Integer.MAX_VALUE;
		
		for (int i=start; i<=end; i++) {
			if (hs[i] < min) {
				min = hs[i];
				list.clear();
				list.add(i);
			} else if (hs[i] == min) {
				list.add(i);
			}
		}

		int result = min - height;	// start~endの範囲の一番低い高さまで水を入れる
		
		int nextIndex = start;
		for (int x : list) {
			// minの高さで区切られた領域の回数を数える
			result += getTimes(hs, min, nextIndex, x-1);
			nextIndex = x+1;
		}
		// 最後のminのIndex～endの領域も忘れない
		result += getTimes(hs, min, nextIndex, end);

		return result;
	}
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int[] hs = new int[N];
		
		for (int i=0; i<N; i++) {
			hs[i] = in.nextInt();
		}
		in.close();
		
		Main main = new Main();
		int result = main.solve(N, hs);
		
		System.out.println(result);
	}
} Main public int solve(int N, int[] hs) {
		return getTimes(hs, 0, 0, N-1);
	} solve {
		return getTimes(hs, 0, 0, N-1);
	} return getTimes(hs, 0, 0, N-1); getTimes(hs, 0, 0, N-1) getTimes hs 0 0 N-1 N 1 int N N int[] hs hs public int getTimes(int[] hs, int height, int start, int end) {
		if (start > end) {
			return 0;
		} else if (start == end) {
			return hs[start] - height;
		}
		
		List<Integer> list = new LinkedList<Integer>();
		int min = Integer.MAX_VALUE;
		
		for (int i=start; i<=end; i++) {
			if (hs[i] < min) {
				min = hs[i];
				list.clear();
				list.add(i);
			} else if (hs[i] == min) {
				list.add(i);
			}
		}

		int result = min - height;	// start~endの範囲の一番低い高さまで水を入れる
		
		int nextIndex = start;
		for (int x : list) {
			// minの高さで区切られた領域の回数を数える
			result += getTimes(hs, min, nextIndex, x-1);
			nextIndex = x+1;
		}
		// 最後のminのIndex～endの領域も忘れない
		result += getTimes(hs, min, nextIndex, end);

		return result;
	} getTimes {
		if (start > end) {
			return 0;
		} else if (start == end) {
			return hs[start] - height;
		}
		
		List<Integer> list = new LinkedList<Integer>();
		int min = Integer.MAX_VALUE;
		
		for (int i=start; i<=end; i++) {
			if (hs[i] < min) {
				min = hs[i];
				list.clear();
				list.add(i);
			} else if (hs[i] == min) {
				list.add(i);
			}
		}

		int result = min - height;	// start~endの範囲の一番低い高さまで水を入れる
		
		int nextIndex = start;
		for (int x : list) {
			// minの高さで区切られた領域の回数を数える
			result += getTimes(hs, min, nextIndex, x-1);
			nextIndex = x+1;
		}
		// 最後のminのIndex～endの領域も忘れない
		result += getTimes(hs, min, nextIndex, end);

		return result;
	} if (start > end) {
			return 0;
		} else if (start == end) {
			return hs[start] - height;
		} start > end start end {
			return 0;
		} return 0; 0 if (start == end) {
			return hs[start] - height;
		} start == end start end {
			return hs[start] - height;
		} return hs[start] - height; hs[start] - height hs[start] hs start height List<Integer> list = new LinkedList<Integer>(); list new LinkedList<Integer>() int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int i=start; i<=end; i++) {
			if (hs[i] < min) {
				min = hs[i];
				list.clear();
				list.add(i);
			} else if (hs[i] == min) {
				list.add(i);
			}
		} int i=start; int i=start; i start i<=end i end i++ i++ i {
			if (hs[i] < min) {
				min = hs[i];
				list.clear();
				list.add(i);
			} else if (hs[i] == min) {
				list.add(i);
			}
		} {
			if (hs[i] < min) {
				min = hs[i];
				list.clear();
				list.add(i);
			} else if (hs[i] == min) {
				list.add(i);
			}
		} if (hs[i] < min) {
				min = hs[i];
				list.clear();
				list.add(i);
			} else if (hs[i] == min) {
				list.add(i);
			} hs[i] < min hs[i] hs i min {
				min = hs[i];
				list.clear();
				list.add(i);
			} min = hs[i] min hs[i] hs i list.clear() list.clear list list.add(i) list.add list i if (hs[i] == min) {
				list.add(i);
			} hs[i] == min hs[i] hs i min {
				list.add(i);
			} list.add(i) list.add list i int result = min - height; result min - height min height int nextIndex = start; nextIndex start for (int x : list) {
			// minの高さで区切られた領域の回数を数える
			result += getTimes(hs, min, nextIndex, x-1);
			nextIndex = x+1;
		} int x list {
			// minの高さで区切られた領域の回数を数える
			result += getTimes(hs, min, nextIndex, x-1);
			nextIndex = x+1;
		} {
			// minの高さで区切られた領域の回数を数える
			result += getTimes(hs, min, nextIndex, x-1);
			nextIndex = x+1;
		} result += getTimes(hs, min, nextIndex, x-1) result getTimes(hs, min, nextIndex, x-1) getTimes hs min nextIndex x-1 x 1 nextIndex = x+1 nextIndex x+1 x 1 result += getTimes(hs, min, nextIndex, end) result getTimes(hs, min, nextIndex, end) getTimes hs min nextIndex end return result; result int[] hs hs int height height int start start int end end public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int[] hs = new int[N];
		
		for (int i=0; i<N; i++) {
			hs[i] = in.nextInt();
		}
		in.close();
		
		Main main = new Main();
		int result = main.solve(N, hs);
		
		System.out.println(result);
	} main {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int[] hs = new int[N];
		
		for (int i=0; i<N; i++) {
			hs[i] = in.nextInt();
		}
		in.close();
		
		Main main = new Main();
		int result = main.solve(N, hs);
		
		System.out.println(result);
	} Scanner in = new Scanner(System.in); in new Scanner(System.in) int N = in.nextInt(); N in.nextInt() in.nextInt in int[] hs = new int[N]; hs new int[N] N for (int i=0; i<N; i++) {
			hs[i] = in.nextInt();
		} int i=0; int i=0; i 0 i<N i N i++ i++ i {
			hs[i] = in.nextInt();
		} {
			hs[i] = in.nextInt();
		} hs[i] = in.nextInt() hs[i] hs i in.nextInt() in.nextInt in in.close() in.close in Main main = new Main(); main new Main() int result = main.solve(N, hs); result main.solve(N, hs) main.solve main N hs System.out.println(result) System.out.println System.out System System.out result String[] args args public class Main {
	public int solve(int N, int[] hs) {
		return getTimes(hs, 0, 0, N-1);
	}
	
	public int getTimes(int[] hs, int height, int start, int end) {
		if (start > end) {
			return 0;
		} else if (start == end) {
			return hs[start] - height;
		}
		
		List<Integer> list = new LinkedList<Integer>();
		int min = Integer.MAX_VALUE;
		
		for (int i=start; i<=end; i++) {
			if (hs[i] < min) {
				min = hs[i];
				list.clear();
				list.add(i);
			} else if (hs[i] == min) {
				list.add(i);
			}
		}

		int result = min - height;	// start~endの範囲の一番低い高さまで水を入れる
		
		int nextIndex = start;
		for (int x : list) {
			// minの高さで区切られた領域の回数を数える
			result += getTimes(hs, min, nextIndex, x-1);
			nextIndex = x+1;
		}
		// 最後のminのIndex～endの領域も忘れない
		result += getTimes(hs, min, nextIndex, end);

		return result;
	}
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int[] hs = new int[N];
		
		for (int i=0; i<N; i++) {
			hs[i] = in.nextInt();
		}
		in.close();
		
		Main main = new Main();
		int result = main.solve(N, hs);
		
		System.out.println(result);
	}
} public class Main {
	public int solve(int N, int[] hs) {
		return getTimes(hs, 0, 0, N-1);
	}
	
	public int getTimes(int[] hs, int height, int start, int end) {
		if (start > end) {
			return 0;
		} else if (start == end) {
			return hs[start] - height;
		}
		
		List<Integer> list = new LinkedList<Integer>();
		int min = Integer.MAX_VALUE;
		
		for (int i=start; i<=end; i++) {
			if (hs[i] < min) {
				min = hs[i];
				list.clear();
				list.add(i);
			} else if (hs[i] == min) {
				list.add(i);
			}
		}

		int result = min - height;	// start~endの範囲の一番低い高さまで水を入れる
		
		int nextIndex = start;
		for (int x : list) {
			// minの高さで区切られた領域の回数を数える
			result += getTimes(hs, min, nextIndex, x-1);
			nextIndex = x+1;
		}
		// 最後のminのIndex～endの領域も忘れない
		result += getTimes(hs, min, nextIndex, end);

		return result;
	}
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int[] hs = new int[N];
		
		for (int i=0; i<N; i++) {
			hs[i] = in.nextInt();
		}
		in.close();
		
		Main main = new Main();
		int result = main.solve(N, hs);
		
		System.out.println(result);
	}
} Main
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Main {
	public int solve(int N, int[] hs) {
		return getTimes(hs, 0, 0, N-1);
	}
	
	public int getTimes(int[] hs, int height, int start, int end) {
		if (start > end) {
			return 0;
		} else if (start == end) {
			return hs[start] - height;
		}
		
		List<Integer> list = new LinkedList<Integer>();
		int min = Integer.MAX_VALUE;
		
		for (int i=start; i<=end; i++) {
			if (hs[i] < min) {
				min = hs[i];
				list.clear();
				list.add(i);
			} else if (hs[i] == min) {
				list.add(i);
			}
		}

		int result = min - height;	// start~endの範囲の一番低い高さまで水を入れる
		
		int nextIndex = start;
		for (int x : list) {
			// minの高さで区切られた領域の回数を数える
			result += getTimes(hs, min, nextIndex, x-1);
			nextIndex = x+1;
		}
		// 最後のminのIndex～endの領域も忘れない
		result += getTimes(hs, min, nextIndex, end);

		return result;
	}
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int[] hs = new int[N];
		
		for (int i=0; i<N; i++) {
			hs[i] = in.nextInt();
		}
		in.close();
		
		Main main = new Main();
		int result = main.solve(N, hs);
		
		System.out.println(result);
	}
}