import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * メインクラス
 *
 */
public class Main {

	/**
	 * メインメソッド
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {

		// ============================================================
		// 標準入力を変数に設定
		// ============================================================

		// 標準入力を取得
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line = br.readLine();

		long N = Long.parseLong(line.split(" ")[0]);

		line = br.readLine();
		String[] str = line.split(" ");
		long[] hana = new long[str.length];

		for (int i = 0; i < str.length; i++) {
			hana[i] = Long.parseLong(str[i]);
		}
		long answer = 0;
		while (true) {

			boolean b = false;
			int start = 0;
			int end = 0;
			for (int i = 0; i < hana.length; i++) {

				if (hana[i] > 0 && !b) {
					b = true;
					start = i;
				}

				if (b && (hana[i] == 0 && end == 0) || i == hana.length - 1) {

					if (i == hana.length - 1 && hana[i] != 0) {
						end = i;
					} else {
						end = i - 1;
					}

					long min = 0;
					for (int j = start; j <= end; j++) {
						if (min == 0 || min > hana[j]) {
							min = hana[j];
						}

					}
					for (int j = start; j <= end; j++) {
						hana[j] = hana[j] - min;
					}
					b = false;
					start = 0;
					end = 0;
					answer += min;
				}
			}

			boolean has = false;
			for (int i = 0; i < hana.length; i++) {
				if (hana[i] > 0) {
					has = true;
					break;
				}
			}

			if (!has) {
				System.out.println(answer);
				return;
			}
		}

	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {

	/**
	 * メインメソッド
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {

		// ============================================================
		// 標準入力を変数に設定
		// ============================================================

		// 標準入力を取得
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line = br.readLine();

		long N = Long.parseLong(line.split(" ")[0]);

		line = br.readLine();
		String[] str = line.split(" ");
		long[] hana = new long[str.length];

		for (int i = 0; i < str.length; i++) {
			hana[i] = Long.parseLong(str[i]);
		}
		long answer = 0;
		while (true) {

			boolean b = false;
			int start = 0;
			int end = 0;
			for (int i = 0; i < hana.length; i++) {

				if (hana[i] > 0 && !b) {
					b = true;
					start = i;
				}

				if (b && (hana[i] == 0 && end == 0) || i == hana.length - 1) {

					if (i == hana.length - 1 && hana[i] != 0) {
						end = i;
					} else {
						end = i - 1;
					}

					long min = 0;
					for (int j = start; j <= end; j++) {
						if (min == 0 || min > hana[j]) {
							min = hana[j];
						}

					}
					for (int j = start; j <= end; j++) {
						hana[j] = hana[j] - min;
					}
					b = false;
					start = 0;
					end = 0;
					answer += min;
				}
			}

			boolean has = false;
			for (int i = 0; i < hana.length; i++) {
				if (hana[i] > 0) {
					has = true;
					break;
				}
			}

			if (!has) {
				System.out.println(answer);
				return;
			}
		}

	}
} Main /**
	 * メインメソッド
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {

		// ============================================================
		// 標準入力を変数に設定
		// ============================================================

		// 標準入力を取得
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line = br.readLine();

		long N = Long.parseLong(line.split(" ")[0]);

		line = br.readLine();
		String[] str = line.split(" ");
		long[] hana = new long[str.length];

		for (int i = 0; i < str.length; i++) {
			hana[i] = Long.parseLong(str[i]);
		}
		long answer = 0;
		while (true) {

			boolean b = false;
			int start = 0;
			int end = 0;
			for (int i = 0; i < hana.length; i++) {

				if (hana[i] > 0 && !b) {
					b = true;
					start = i;
				}

				if (b && (hana[i] == 0 && end == 0) || i == hana.length - 1) {

					if (i == hana.length - 1 && hana[i] != 0) {
						end = i;
					} else {
						end = i - 1;
					}

					long min = 0;
					for (int j = start; j <= end; j++) {
						if (min == 0 || min > hana[j]) {
							min = hana[j];
						}

					}
					for (int j = start; j <= end; j++) {
						hana[j] = hana[j] - min;
					}
					b = false;
					start = 0;
					end = 0;
					answer += min;
				}
			}

			boolean has = false;
			for (int i = 0; i < hana.length; i++) {
				if (hana[i] > 0) {
					has = true;
					break;
				}
			}

			if (!has) {
				System.out.println(answer);
				return;
			}
		}

	} main {

		// ============================================================
		// 標準入力を変数に設定
		// ============================================================

		// 標準入力を取得
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line = br.readLine();

		long N = Long.parseLong(line.split(" ")[0]);

		line = br.readLine();
		String[] str = line.split(" ");
		long[] hana = new long[str.length];

		for (int i = 0; i < str.length; i++) {
			hana[i] = Long.parseLong(str[i]);
		}
		long answer = 0;
		while (true) {

			boolean b = false;
			int start = 0;
			int end = 0;
			for (int i = 0; i < hana.length; i++) {

				if (hana[i] > 0 && !b) {
					b = true;
					start = i;
				}

				if (b && (hana[i] == 0 && end == 0) || i == hana.length - 1) {

					if (i == hana.length - 1 && hana[i] != 0) {
						end = i;
					} else {
						end = i - 1;
					}

					long min = 0;
					for (int j = start; j <= end; j++) {
						if (min == 0 || min > hana[j]) {
							min = hana[j];
						}

					}
					for (int j = start; j <= end; j++) {
						hana[j] = hana[j] - min;
					}
					b = false;
					start = 0;
					end = 0;
					answer += min;
				}
			}

			boolean has = false;
			for (int i = 0; i < hana.length; i++) {
				if (hana[i] > 0) {
					has = true;
					break;
				}
			}

			if (!has) {
				System.out.println(answer);
				return;
			}
		}

	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String line = br.readLine(); line br.readLine() br.readLine br long N = Long.parseLong(line.split(" ")[0]); N Long.parseLong(line.split(" ")[0]) Long.parseLong Long line.split(" ")[0] line.split(" ") line.split line " " 0 line = br.readLine() line br.readLine() br.readLine br String[] str = line.split(" "); str line.split(" ") line.split line " " long[] hana = new long[str.length]; hana new long[str.length] str.length str str.length for (int i = 0; i < str.length; i++) {
			hana[i] = Long.parseLong(str[i]);
		} int i = 0; int i = 0; i 0 i < str.length i str.length str str.length i++ i++ i {
			hana[i] = Long.parseLong(str[i]);
		} {
			hana[i] = Long.parseLong(str[i]);
		} hana[i] = Long.parseLong(str[i]) hana[i] hana i Long.parseLong(str[i]) Long.parseLong Long str[i] str i long answer = 0; answer 0 while (true) {

			boolean b = false;
			int start = 0;
			int end = 0;
			for (int i = 0; i < hana.length; i++) {

				if (hana[i] > 0 && !b) {
					b = true;
					start = i;
				}

				if (b && (hana[i] == 0 && end == 0) || i == hana.length - 1) {

					if (i == hana.length - 1 && hana[i] != 0) {
						end = i;
					} else {
						end = i - 1;
					}

					long min = 0;
					for (int j = start; j <= end; j++) {
						if (min == 0 || min > hana[j]) {
							min = hana[j];
						}

					}
					for (int j = start; j <= end; j++) {
						hana[j] = hana[j] - min;
					}
					b = false;
					start = 0;
					end = 0;
					answer += min;
				}
			}

			boolean has = false;
			for (int i = 0; i < hana.length; i++) {
				if (hana[i] > 0) {
					has = true;
					break;
				}
			}

			if (!has) {
				System.out.println(answer);
				return;
			}
		} true {

			boolean b = false;
			int start = 0;
			int end = 0;
			for (int i = 0; i < hana.length; i++) {

				if (hana[i] > 0 && !b) {
					b = true;
					start = i;
				}

				if (b && (hana[i] == 0 && end == 0) || i == hana.length - 1) {

					if (i == hana.length - 1 && hana[i] != 0) {
						end = i;
					} else {
						end = i - 1;
					}

					long min = 0;
					for (int j = start; j <= end; j++) {
						if (min == 0 || min > hana[j]) {
							min = hana[j];
						}

					}
					for (int j = start; j <= end; j++) {
						hana[j] = hana[j] - min;
					}
					b = false;
					start = 0;
					end = 0;
					answer += min;
				}
			}

			boolean has = false;
			for (int i = 0; i < hana.length; i++) {
				if (hana[i] > 0) {
					has = true;
					break;
				}
			}

			if (!has) {
				System.out.println(answer);
				return;
			}
		} boolean b = false; b false int start = 0; start 0 int end = 0; end 0 for (int i = 0; i < hana.length; i++) {

				if (hana[i] > 0 && !b) {
					b = true;
					start = i;
				}

				if (b && (hana[i] == 0 && end == 0) || i == hana.length - 1) {

					if (i == hana.length - 1 && hana[i] != 0) {
						end = i;
					} else {
						end = i - 1;
					}

					long min = 0;
					for (int j = start; j <= end; j++) {
						if (min == 0 || min > hana[j]) {
							min = hana[j];
						}

					}
					for (int j = start; j <= end; j++) {
						hana[j] = hana[j] - min;
					}
					b = false;
					start = 0;
					end = 0;
					answer += min;
				}
			} int i = 0; int i = 0; i 0 i < hana.length i hana.length hana hana.length i++ i++ i {

				if (hana[i] > 0 && !b) {
					b = true;
					start = i;
				}

				if (b && (hana[i] == 0 && end == 0) || i == hana.length - 1) {

					if (i == hana.length - 1 && hana[i] != 0) {
						end = i;
					} else {
						end = i - 1;
					}

					long min = 0;
					for (int j = start; j <= end; j++) {
						if (min == 0 || min > hana[j]) {
							min = hana[j];
						}

					}
					for (int j = start; j <= end; j++) {
						hana[j] = hana[j] - min;
					}
					b = false;
					start = 0;
					end = 0;
					answer += min;
				}
			} {

				if (hana[i] > 0 && !b) {
					b = true;
					start = i;
				}

				if (b && (hana[i] == 0 && end == 0) || i == hana.length - 1) {

					if (i == hana.length - 1 && hana[i] != 0) {
						end = i;
					} else {
						end = i - 1;
					}

					long min = 0;
					for (int j = start; j <= end; j++) {
						if (min == 0 || min > hana[j]) {
							min = hana[j];
						}

					}
					for (int j = start; j <= end; j++) {
						hana[j] = hana[j] - min;
					}
					b = false;
					start = 0;
					end = 0;
					answer += min;
				}
			} if (hana[i] > 0 && !b) {
					b = true;
					start = i;
				} hana[i] > 0 && !b hana[i] > 0 hana[i] hana i 0 !b b {
					b = true;
					start = i;
				} b = true b true start = i start i if (b && (hana[i] == 0 && end == 0) || i == hana.length - 1) {

					if (i == hana.length - 1 && hana[i] != 0) {
						end = i;
					} else {
						end = i - 1;
					}

					long min = 0;
					for (int j = start; j <= end; j++) {
						if (min == 0 || min > hana[j]) {
							min = hana[j];
						}

					}
					for (int j = start; j <= end; j++) {
						hana[j] = hana[j] - min;
					}
					b = false;
					start = 0;
					end = 0;
					answer += min;
				} b && (hana[i] == 0 && end == 0) || i == hana.length - 1 b && (hana[i] == 0 && end == 0) b (hana[i] == 0 && end == 0) hana[i] == 0 hana[i] hana i 0 end == 0 end 0 i == hana.length - 1 i hana.length - 1 hana.length hana hana.length 1 {

					if (i == hana.length - 1 && hana[i] != 0) {
						end = i;
					} else {
						end = i - 1;
					}

					long min = 0;
					for (int j = start; j <= end; j++) {
						if (min == 0 || min > hana[j]) {
							min = hana[j];
						}

					}
					for (int j = start; j <= end; j++) {
						hana[j] = hana[j] - min;
					}
					b = false;
					start = 0;
					end = 0;
					answer += min;
				} if (i == hana.length - 1 && hana[i] != 0) {
						end = i;
					} else {
						end = i - 1;
					} i == hana.length - 1 && hana[i] != 0 i == hana.length - 1 i hana.length - 1 hana.length hana hana.length 1 hana[i] != 0 hana[i] hana i 0 {
						end = i;
					} end = i end i {
						end = i - 1;
					} end = i - 1 end i - 1 i 1 long min = 0; min 0 for (int j = start; j <= end; j++) {
						if (min == 0 || min > hana[j]) {
							min = hana[j];
						}

					} int j = start; int j = start; j start j <= end j end j++ j++ j {
						if (min == 0 || min > hana[j]) {
							min = hana[j];
						}

					} {
						if (min == 0 || min > hana[j]) {
							min = hana[j];
						}

					} if (min == 0 || min > hana[j]) {
							min = hana[j];
						} min == 0 || min > hana[j] min == 0 min 0 min > hana[j] min hana[j] hana j {
							min = hana[j];
						} min = hana[j] min hana[j] hana j for (int j = start; j <= end; j++) {
						hana[j] = hana[j] - min;
					} int j = start; int j = start; j start j <= end j end j++ j++ j {
						hana[j] = hana[j] - min;
					} {
						hana[j] = hana[j] - min;
					} hana[j] = hana[j] - min hana[j] hana j hana[j] - min hana[j] hana j min b = false b false start = 0 start 0 end = 0 end 0 answer += min answer min boolean has = false; has false for (int i = 0; i < hana.length; i++) {
				if (hana[i] > 0) {
					has = true;
					break;
				}
			} int i = 0; int i = 0; i 0 i < hana.length i hana.length hana hana.length i++ i++ i {
				if (hana[i] > 0) {
					has = true;
					break;
				}
			} {
				if (hana[i] > 0) {
					has = true;
					break;
				}
			} if (hana[i] > 0) {
					has = true;
					break;
				} hana[i] > 0 hana[i] hana i 0 {
					has = true;
					break;
				} has = true has true break; if (!has) {
				System.out.println(answer);
				return;
			} !has has {
				System.out.println(answer);
				return;
			} System.out.println(answer) System.out.println System.out System System.out answer return; String[] args args public class Main {

	/**
	 * メインメソッド
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {

		// ============================================================
		// 標準入力を変数に設定
		// ============================================================

		// 標準入力を取得
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line = br.readLine();

		long N = Long.parseLong(line.split(" ")[0]);

		line = br.readLine();
		String[] str = line.split(" ");
		long[] hana = new long[str.length];

		for (int i = 0; i < str.length; i++) {
			hana[i] = Long.parseLong(str[i]);
		}
		long answer = 0;
		while (true) {

			boolean b = false;
			int start = 0;
			int end = 0;
			for (int i = 0; i < hana.length; i++) {

				if (hana[i] > 0 && !b) {
					b = true;
					start = i;
				}

				if (b && (hana[i] == 0 && end == 0) || i == hana.length - 1) {

					if (i == hana.length - 1 && hana[i] != 0) {
						end = i;
					} else {
						end = i - 1;
					}

					long min = 0;
					for (int j = start; j <= end; j++) {
						if (min == 0 || min > hana[j]) {
							min = hana[j];
						}

					}
					for (int j = start; j <= end; j++) {
						hana[j] = hana[j] - min;
					}
					b = false;
					start = 0;
					end = 0;
					answer += min;
				}
			}

			boolean has = false;
			for (int i = 0; i < hana.length; i++) {
				if (hana[i] > 0) {
					has = true;
					break;
				}
			}

			if (!has) {
				System.out.println(answer);
				return;
			}
		}

	}
} public class Main {

	/**
	 * メインメソッド
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {

		// ============================================================
		// 標準入力を変数に設定
		// ============================================================

		// 標準入力を取得
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line = br.readLine();

		long N = Long.parseLong(line.split(" ")[0]);

		line = br.readLine();
		String[] str = line.split(" ");
		long[] hana = new long[str.length];

		for (int i = 0; i < str.length; i++) {
			hana[i] = Long.parseLong(str[i]);
		}
		long answer = 0;
		while (true) {

			boolean b = false;
			int start = 0;
			int end = 0;
			for (int i = 0; i < hana.length; i++) {

				if (hana[i] > 0 && !b) {
					b = true;
					start = i;
				}

				if (b && (hana[i] == 0 && end == 0) || i == hana.length - 1) {

					if (i == hana.length - 1 && hana[i] != 0) {
						end = i;
					} else {
						end = i - 1;
					}

					long min = 0;
					for (int j = start; j <= end; j++) {
						if (min == 0 || min > hana[j]) {
							min = hana[j];
						}

					}
					for (int j = start; j <= end; j++) {
						hana[j] = hana[j] - min;
					}
					b = false;
					start = 0;
					end = 0;
					answer += min;
				}
			}

			boolean has = false;
			for (int i = 0; i < hana.length; i++) {
				if (hana[i] > 0) {
					has = true;
					break;
				}
			}

			if (!has) {
				System.out.println(answer);
				return;
			}
		}

	}
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * メインクラス
 *
 */
public class Main {

	/**
	 * メインメソッド
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {

		// ============================================================
		// 標準入力を変数に設定
		// ============================================================

		// 標準入力を取得
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line = br.readLine();

		long N = Long.parseLong(line.split(" ")[0]);

		line = br.readLine();
		String[] str = line.split(" ");
		long[] hana = new long[str.length];

		for (int i = 0; i < str.length; i++) {
			hana[i] = Long.parseLong(str[i]);
		}
		long answer = 0;
		while (true) {

			boolean b = false;
			int start = 0;
			int end = 0;
			for (int i = 0; i < hana.length; i++) {

				if (hana[i] > 0 && !b) {
					b = true;
					start = i;
				}

				if (b && (hana[i] == 0 && end == 0) || i == hana.length - 1) {

					if (i == hana.length - 1 && hana[i] != 0) {
						end = i;
					} else {
						end = i - 1;
					}

					long min = 0;
					for (int j = start; j <= end; j++) {
						if (min == 0 || min > hana[j]) {
							min = hana[j];
						}

					}
					for (int j = start; j <= end; j++) {
						hana[j] = hana[j] - min;
					}
					b = false;
					start = 0;
					end = 0;
					answer += min;
				}
			}

			boolean has = false;
			for (int i = 0; i < hana.length; i++) {
				if (hana[i] > 0) {
					has = true;
					break;
				}
			}

			if (!has) {
				System.out.println(answer);
				return;
			}
		}

	}
}