import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class Main{



	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		int n        = Integer.parseInt(scanner.nextLine());
		String[] tmp = scanner.nextLine().split(" ");
		int[] list   = new int[n];
		int ans    	 = 0;

		for(int i = 0; i < n; i++) {
			list[i] = Integer.parseInt(tmp[i]);
		}


		for(int i = 0; i < n; i++) {

			int r   = i;
			int min = Integer.MAX_VALUE;

			while(list[i] != 0) {
				while(r < n && list[r] != 0) {

					min = Math.min(min, list[r]);
					r++;
				}

				for(int k = i; k < r; k++) {

					list[k] -= min;
				}

				ans += min;
				r = i;
			}

		}

		System.out.println(ans);
	}
} import java.util.ArrayList; ArrayList java.util import java.util.Arrays; Arrays java.util import java.util.HashMap; HashMap java.util import java.util.List; List java.util import java.util.Map; Map java.util import java.util.Map.Entry; Entry java.util.Map import java.util.Scanner; Scanner java.util public class Main{



	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		int n        = Integer.parseInt(scanner.nextLine());
		String[] tmp = scanner.nextLine().split(" ");
		int[] list   = new int[n];
		int ans    	 = 0;

		for(int i = 0; i < n; i++) {
			list[i] = Integer.parseInt(tmp[i]);
		}


		for(int i = 0; i < n; i++) {

			int r   = i;
			int min = Integer.MAX_VALUE;

			while(list[i] != 0) {
				while(r < n && list[r] != 0) {

					min = Math.min(min, list[r]);
					r++;
				}

				for(int k = i; k < r; k++) {

					list[k] -= min;
				}

				ans += min;
				r = i;
			}

		}

		System.out.println(ans);
	}
} Main public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		int n        = Integer.parseInt(scanner.nextLine());
		String[] tmp = scanner.nextLine().split(" ");
		int[] list   = new int[n];
		int ans    	 = 0;

		for(int i = 0; i < n; i++) {
			list[i] = Integer.parseInt(tmp[i]);
		}


		for(int i = 0; i < n; i++) {

			int r   = i;
			int min = Integer.MAX_VALUE;

			while(list[i] != 0) {
				while(r < n && list[r] != 0) {

					min = Math.min(min, list[r]);
					r++;
				}

				for(int k = i; k < r; k++) {

					list[k] -= min;
				}

				ans += min;
				r = i;
			}

		}

		System.out.println(ans);
	} main {

		Scanner scanner = new Scanner(System.in);

		int n        = Integer.parseInt(scanner.nextLine());
		String[] tmp = scanner.nextLine().split(" ");
		int[] list   = new int[n];
		int ans    	 = 0;

		for(int i = 0; i < n; i++) {
			list[i] = Integer.parseInt(tmp[i]);
		}


		for(int i = 0; i < n; i++) {

			int r   = i;
			int min = Integer.MAX_VALUE;

			while(list[i] != 0) {
				while(r < n && list[r] != 0) {

					min = Math.min(min, list[r]);
					r++;
				}

				for(int k = i; k < r; k++) {

					list[k] -= min;
				}

				ans += min;
				r = i;
			}

		}

		System.out.println(ans);
	} Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int n        = Integer.parseInt(scanner.nextLine()); n Integer.parseInt(scanner.nextLine()) Integer.parseInt Integer scanner.nextLine() scanner.nextLine scanner String[] tmp = scanner.nextLine().split(" "); tmp scanner.nextLine().split(" ") scanner.nextLine().split scanner.nextLine() scanner.nextLine scanner " " int[] list   = new int[n]; list new int[n] n int ans    	 = 0; ans 0 for(int i = 0; i < n; i++) {
			list[i] = Integer.parseInt(tmp[i]);
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			list[i] = Integer.parseInt(tmp[i]);
		} {
			list[i] = Integer.parseInt(tmp[i]);
		} list[i] = Integer.parseInt(tmp[i]) list[i] list i Integer.parseInt(tmp[i]) Integer.parseInt Integer tmp[i] tmp i for(int i = 0; i < n; i++) {

			int r   = i;
			int min = Integer.MAX_VALUE;

			while(list[i] != 0) {
				while(r < n && list[r] != 0) {

					min = Math.min(min, list[r]);
					r++;
				}

				for(int k = i; k < r; k++) {

					list[k] -= min;
				}

				ans += min;
				r = i;
			}

		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {

			int r   = i;
			int min = Integer.MAX_VALUE;

			while(list[i] != 0) {
				while(r < n && list[r] != 0) {

					min = Math.min(min, list[r]);
					r++;
				}

				for(int k = i; k < r; k++) {

					list[k] -= min;
				}

				ans += min;
				r = i;
			}

		} {

			int r   = i;
			int min = Integer.MAX_VALUE;

			while(list[i] != 0) {
				while(r < n && list[r] != 0) {

					min = Math.min(min, list[r]);
					r++;
				}

				for(int k = i; k < r; k++) {

					list[k] -= min;
				}

				ans += min;
				r = i;
			}

		} int r   = i; r i int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE while(list[i] != 0) {
				while(r < n && list[r] != 0) {

					min = Math.min(min, list[r]);
					r++;
				}

				for(int k = i; k < r; k++) {

					list[k] -= min;
				}

				ans += min;
				r = i;
			} list[i] != 0 list[i] list i 0 {
				while(r < n && list[r] != 0) {

					min = Math.min(min, list[r]);
					r++;
				}

				for(int k = i; k < r; k++) {

					list[k] -= min;
				}

				ans += min;
				r = i;
			} while(r < n && list[r] != 0) {

					min = Math.min(min, list[r]);
					r++;
				} r < n && list[r] != 0 r < n r n list[r] != 0 list[r] list r 0 {

					min = Math.min(min, list[r]);
					r++;
				} min = Math.min(min, list[r]) min Math.min(min, list[r]) Math.min Math min list[r] list r r++ r for(int k = i; k < r; k++) {

					list[k] -= min;
				} int k = i; int k = i; k i k < r k r k++ k++ k {

					list[k] -= min;
				} {

					list[k] -= min;
				} list[k] -= min list[k] list k min ans += min ans min r = i r i System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main{



	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		int n        = Integer.parseInt(scanner.nextLine());
		String[] tmp = scanner.nextLine().split(" ");
		int[] list   = new int[n];
		int ans    	 = 0;

		for(int i = 0; i < n; i++) {
			list[i] = Integer.parseInt(tmp[i]);
		}


		for(int i = 0; i < n; i++) {

			int r   = i;
			int min = Integer.MAX_VALUE;

			while(list[i] != 0) {
				while(r < n && list[r] != 0) {

					min = Math.min(min, list[r]);
					r++;
				}

				for(int k = i; k < r; k++) {

					list[k] -= min;
				}

				ans += min;
				r = i;
			}

		}

		System.out.println(ans);
	}
} public class Main{



	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		int n        = Integer.parseInt(scanner.nextLine());
		String[] tmp = scanner.nextLine().split(" ");
		int[] list   = new int[n];
		int ans    	 = 0;

		for(int i = 0; i < n; i++) {
			list[i] = Integer.parseInt(tmp[i]);
		}


		for(int i = 0; i < n; i++) {

			int r   = i;
			int min = Integer.MAX_VALUE;

			while(list[i] != 0) {
				while(r < n && list[r] != 0) {

					min = Math.min(min, list[r]);
					r++;
				}

				for(int k = i; k < r; k++) {

					list[k] -= min;
				}

				ans += min;
				r = i;
			}

		}

		System.out.println(ans);
	}
} Main
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class Main{



	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		int n        = Integer.parseInt(scanner.nextLine());
		String[] tmp = scanner.nextLine().split(" ");
		int[] list   = new int[n];
		int ans    	 = 0;

		for(int i = 0; i < n; i++) {
			list[i] = Integer.parseInt(tmp[i]);
		}


		for(int i = 0; i < n; i++) {

			int r   = i;
			int min = Integer.MAX_VALUE;

			while(list[i] != 0) {
				while(r < n && list[r] != 0) {

					min = Math.min(min, list[r]);
					r++;
				}

				for(int k = i; k < r; k++) {

					list[k] -= min;
				}

				ans += min;
				r = i;
			}

		}

		System.out.println(ans);
	}
}
