import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		ArrayList<Integer> list = new ArrayList<>();
		int min = Integer.MAX_VALUE;
		for (int i = 0; i < n; i++) {
			int h = sc.nextInt();
			if (min > h) {
				min = h;
			}
			list.add(h);
		}
		System.out.println(count(list, min) + min);
	}
	
	static int count(ArrayList<Integer> list, int min) {
		ArrayList<Integer> nlist = new ArrayList<>();
		int nmin = Integer.MAX_VALUE;
		int total = 0;
		for (int i = 0; i < list.size(); i++) {
			int x = list.get(i);
			if (x == min) {
				if (nlist.size() != 0) {
					total += count(nlist, nmin) + nmin - min;
					nlist = new ArrayList<>();
					nmin = Integer.MAX_VALUE;
				}
			} else {
				nlist.add(x);
				if (nmin > x) {
					nmin = x;
				}
				if (i == list.size() - 1) {
					total += count(nlist, nmin) + nmin - min;
				}
			}
		}
		return total;
	}
} import java.util.*; util.* java public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		ArrayList<Integer> list = new ArrayList<>();
		int min = Integer.MAX_VALUE;
		for (int i = 0; i < n; i++) {
			int h = sc.nextInt();
			if (min > h) {
				min = h;
			}
			list.add(h);
		}
		System.out.println(count(list, min) + min);
	}
	
	static int count(ArrayList<Integer> list, int min) {
		ArrayList<Integer> nlist = new ArrayList<>();
		int nmin = Integer.MAX_VALUE;
		int total = 0;
		for (int i = 0; i < list.size(); i++) {
			int x = list.get(i);
			if (x == min) {
				if (nlist.size() != 0) {
					total += count(nlist, nmin) + nmin - min;
					nlist = new ArrayList<>();
					nmin = Integer.MAX_VALUE;
				}
			} else {
				nlist.add(x);
				if (nmin > x) {
					nmin = x;
				}
				if (i == list.size() - 1) {
					total += count(nlist, nmin) + nmin - min;
				}
			}
		}
		return total;
	}
} Main public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		ArrayList<Integer> list = new ArrayList<>();
		int min = Integer.MAX_VALUE;
		for (int i = 0; i < n; i++) {
			int h = sc.nextInt();
			if (min > h) {
				min = h;
			}
			list.add(h);
		}
		System.out.println(count(list, min) + min);
	} main {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		ArrayList<Integer> list = new ArrayList<>();
		int min = Integer.MAX_VALUE;
		for (int i = 0; i < n; i++) {
			int h = sc.nextInt();
			if (min > h) {
				min = h;
			}
			list.add(h);
		}
		System.out.println(count(list, min) + min);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc ArrayList<Integer> list = new ArrayList<>(); list new ArrayList<>() int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int i = 0; i < n; i++) {
			int h = sc.nextInt();
			if (min > h) {
				min = h;
			}
			list.add(h);
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			int h = sc.nextInt();
			if (min > h) {
				min = h;
			}
			list.add(h);
		} {
			int h = sc.nextInt();
			if (min > h) {
				min = h;
			}
			list.add(h);
		} int h = sc.nextInt(); h sc.nextInt() sc.nextInt sc if (min > h) {
				min = h;
			} min > h min h {
				min = h;
			} min = h min h list.add(h) list.add list h System.out.println(count(list, min) + min) System.out.println System.out System System.out count(list, min) + min count(list, min) count list min min String[] args args static int count(ArrayList<Integer> list, int min) {
		ArrayList<Integer> nlist = new ArrayList<>();
		int nmin = Integer.MAX_VALUE;
		int total = 0;
		for (int i = 0; i < list.size(); i++) {
			int x = list.get(i);
			if (x == min) {
				if (nlist.size() != 0) {
					total += count(nlist, nmin) + nmin - min;
					nlist = new ArrayList<>();
					nmin = Integer.MAX_VALUE;
				}
			} else {
				nlist.add(x);
				if (nmin > x) {
					nmin = x;
				}
				if (i == list.size() - 1) {
					total += count(nlist, nmin) + nmin - min;
				}
			}
		}
		return total;
	} count {
		ArrayList<Integer> nlist = new ArrayList<>();
		int nmin = Integer.MAX_VALUE;
		int total = 0;
		for (int i = 0; i < list.size(); i++) {
			int x = list.get(i);
			if (x == min) {
				if (nlist.size() != 0) {
					total += count(nlist, nmin) + nmin - min;
					nlist = new ArrayList<>();
					nmin = Integer.MAX_VALUE;
				}
			} else {
				nlist.add(x);
				if (nmin > x) {
					nmin = x;
				}
				if (i == list.size() - 1) {
					total += count(nlist, nmin) + nmin - min;
				}
			}
		}
		return total;
	} ArrayList<Integer> nlist = new ArrayList<>(); nlist new ArrayList<>() int nmin = Integer.MAX_VALUE; nmin Integer.MAX_VALUE Integer Integer.MAX_VALUE int total = 0; total 0 for (int i = 0; i < list.size(); i++) {
			int x = list.get(i);
			if (x == min) {
				if (nlist.size() != 0) {
					total += count(nlist, nmin) + nmin - min;
					nlist = new ArrayList<>();
					nmin = Integer.MAX_VALUE;
				}
			} else {
				nlist.add(x);
				if (nmin > x) {
					nmin = x;
				}
				if (i == list.size() - 1) {
					total += count(nlist, nmin) + nmin - min;
				}
			}
		} int i = 0; int i = 0; i 0 i < list.size() i list.size() list.size list i++ i++ i {
			int x = list.get(i);
			if (x == min) {
				if (nlist.size() != 0) {
					total += count(nlist, nmin) + nmin - min;
					nlist = new ArrayList<>();
					nmin = Integer.MAX_VALUE;
				}
			} else {
				nlist.add(x);
				if (nmin > x) {
					nmin = x;
				}
				if (i == list.size() - 1) {
					total += count(nlist, nmin) + nmin - min;
				}
			}
		} {
			int x = list.get(i);
			if (x == min) {
				if (nlist.size() != 0) {
					total += count(nlist, nmin) + nmin - min;
					nlist = new ArrayList<>();
					nmin = Integer.MAX_VALUE;
				}
			} else {
				nlist.add(x);
				if (nmin > x) {
					nmin = x;
				}
				if (i == list.size() - 1) {
					total += count(nlist, nmin) + nmin - min;
				}
			}
		} int x = list.get(i); x list.get(i) list.get list i if (x == min) {
				if (nlist.size() != 0) {
					total += count(nlist, nmin) + nmin - min;
					nlist = new ArrayList<>();
					nmin = Integer.MAX_VALUE;
				}
			} else {
				nlist.add(x);
				if (nmin > x) {
					nmin = x;
				}
				if (i == list.size() - 1) {
					total += count(nlist, nmin) + nmin - min;
				}
			} x == min x min {
				if (nlist.size() != 0) {
					total += count(nlist, nmin) + nmin - min;
					nlist = new ArrayList<>();
					nmin = Integer.MAX_VALUE;
				}
			} if (nlist.size() != 0) {
					total += count(nlist, nmin) + nmin - min;
					nlist = new ArrayList<>();
					nmin = Integer.MAX_VALUE;
				} nlist.size() != 0 nlist.size() nlist.size nlist 0 {
					total += count(nlist, nmin) + nmin - min;
					nlist = new ArrayList<>();
					nmin = Integer.MAX_VALUE;
				} total += count(nlist, nmin) + nmin - min total count(nlist, nmin) + nmin - min count(nlist, nmin) + nmin count(nlist, nmin) count nlist nmin nmin min nlist = new ArrayList<>() nlist new ArrayList<>() nmin = Integer.MAX_VALUE nmin Integer.MAX_VALUE Integer Integer.MAX_VALUE {
				nlist.add(x);
				if (nmin > x) {
					nmin = x;
				}
				if (i == list.size() - 1) {
					total += count(nlist, nmin) + nmin - min;
				}
			} nlist.add(x) nlist.add nlist x if (nmin > x) {
					nmin = x;
				} nmin > x nmin x {
					nmin = x;
				} nmin = x nmin x if (i == list.size() - 1) {
					total += count(nlist, nmin) + nmin - min;
				} i == list.size() - 1 i list.size() - 1 list.size() list.size list 1 {
					total += count(nlist, nmin) + nmin - min;
				} total += count(nlist, nmin) + nmin - min total count(nlist, nmin) + nmin - min count(nlist, nmin) + nmin count(nlist, nmin) count nlist nmin nmin min return total; total ArrayList<Integer> list list int min min public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		ArrayList<Integer> list = new ArrayList<>();
		int min = Integer.MAX_VALUE;
		for (int i = 0; i < n; i++) {
			int h = sc.nextInt();
			if (min > h) {
				min = h;
			}
			list.add(h);
		}
		System.out.println(count(list, min) + min);
	}
	
	static int count(ArrayList<Integer> list, int min) {
		ArrayList<Integer> nlist = new ArrayList<>();
		int nmin = Integer.MAX_VALUE;
		int total = 0;
		for (int i = 0; i < list.size(); i++) {
			int x = list.get(i);
			if (x == min) {
				if (nlist.size() != 0) {
					total += count(nlist, nmin) + nmin - min;
					nlist = new ArrayList<>();
					nmin = Integer.MAX_VALUE;
				}
			} else {
				nlist.add(x);
				if (nmin > x) {
					nmin = x;
				}
				if (i == list.size() - 1) {
					total += count(nlist, nmin) + nmin - min;
				}
			}
		}
		return total;
	}
} public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		ArrayList<Integer> list = new ArrayList<>();
		int min = Integer.MAX_VALUE;
		for (int i = 0; i < n; i++) {
			int h = sc.nextInt();
			if (min > h) {
				min = h;
			}
			list.add(h);
		}
		System.out.println(count(list, min) + min);
	}
	
	static int count(ArrayList<Integer> list, int min) {
		ArrayList<Integer> nlist = new ArrayList<>();
		int nmin = Integer.MAX_VALUE;
		int total = 0;
		for (int i = 0; i < list.size(); i++) {
			int x = list.get(i);
			if (x == min) {
				if (nlist.size() != 0) {
					total += count(nlist, nmin) + nmin - min;
					nlist = new ArrayList<>();
					nmin = Integer.MAX_VALUE;
				}
			} else {
				nlist.add(x);
				if (nmin > x) {
					nmin = x;
				}
				if (i == list.size() - 1) {
					total += count(nlist, nmin) + nmin - min;
				}
			}
		}
		return total;
	}
} Main
import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		ArrayList<Integer> list = new ArrayList<>();
		int min = Integer.MAX_VALUE;
		for (int i = 0; i < n; i++) {
			int h = sc.nextInt();
			if (min > h) {
				min = h;
			}
			list.add(h);
		}
		System.out.println(count(list, min) + min);
	}
	
	static int count(ArrayList<Integer> list, int min) {
		ArrayList<Integer> nlist = new ArrayList<>();
		int nmin = Integer.MAX_VALUE;
		int total = 0;
		for (int i = 0; i < list.size(); i++) {
			int x = list.get(i);
			if (x == min) {
				if (nlist.size() != 0) {
					total += count(nlist, nmin) + nmin - min;
					nlist = new ArrayList<>();
					nmin = Integer.MAX_VALUE;
				}
			} else {
				nlist.add(x);
				if (nmin > x) {
					nmin = x;
				}
				if (i == list.size() - 1) {
					total += count(nlist, nmin) + nmin - min;
				}
			}
		}
		return total;
	}
}
