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

public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		List<Integer> heights = new ArrayList<Integer>();
		for(int i = 0; i < N; i++) {
			heights.add(sc.nextInt());
		}
		sc.close();
		int number = getHeight(heights);
		System.out.println(number);
	}
	
	static int getHeight(List<Integer> heights) {
		if(heights.size() == 0) return 0;
		if(heights.size() == 1) return heights.get(0);
		int number = findSmallest(heights);
		List<Integer> updatedList = updateHeights(heights, number);
		List<List<Integer>> partitions = divide(updatedList);
		for(List<Integer> list: partitions) {
			number += getHeight(list);
		}
		return number;
		
	}
	
	static int findSmallest(List<Integer> heights) {
		int temp = heights.get(0);
		for(int x : heights) {
			if(temp > x) {
				temp = x;
			}
		}
		return temp;
	}
	
	static List<Integer> updateHeights(List<Integer>heights, int number) {
		List<Integer> list = new ArrayList<Integer>();
		for(int x: heights) {
			list.add(x - number);;
		}
		return list;
	}
	
	static List<List<Integer>> divide(List<Integer> heights){
		List<List<Integer>> partitions = new ArrayList<List<Integer>>();
		List<Integer> list = null;
		for(int i = 0; i < heights.size(); i++) {
			if(i == 0 ||  heights.get(i) == 0) {
				if(list != null) {
					partitions.add(list);
				}
				list = new ArrayList<Integer>();
			}
			if(heights.get(i) != 0) {
				list.add(heights.get(i));
				if(i == heights.size() - 1) {
					partitions.add(list);
				}
			}
		}
		return partitions;
	}
		
} import java.util.ArrayList; ArrayList java.util import java.util.List; List java.util import java.util.Scanner; Scanner java.util public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		List<Integer> heights = new ArrayList<Integer>();
		for(int i = 0; i < N; i++) {
			heights.add(sc.nextInt());
		}
		sc.close();
		int number = getHeight(heights);
		System.out.println(number);
	}
	
	static int getHeight(List<Integer> heights) {
		if(heights.size() == 0) return 0;
		if(heights.size() == 1) return heights.get(0);
		int number = findSmallest(heights);
		List<Integer> updatedList = updateHeights(heights, number);
		List<List<Integer>> partitions = divide(updatedList);
		for(List<Integer> list: partitions) {
			number += getHeight(list);
		}
		return number;
		
	}
	
	static int findSmallest(List<Integer> heights) {
		int temp = heights.get(0);
		for(int x : heights) {
			if(temp > x) {
				temp = x;
			}
		}
		return temp;
	}
	
	static List<Integer> updateHeights(List<Integer>heights, int number) {
		List<Integer> list = new ArrayList<Integer>();
		for(int x: heights) {
			list.add(x - number);;
		}
		return list;
	}
	
	static List<List<Integer>> divide(List<Integer> heights){
		List<List<Integer>> partitions = new ArrayList<List<Integer>>();
		List<Integer> list = null;
		for(int i = 0; i < heights.size(); i++) {
			if(i == 0 ||  heights.get(i) == 0) {
				if(list != null) {
					partitions.add(list);
				}
				list = new ArrayList<Integer>();
			}
			if(heights.get(i) != 0) {
				list.add(heights.get(i));
				if(i == heights.size() - 1) {
					partitions.add(list);
				}
			}
		}
		return partitions;
	}
		
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		List<Integer> heights = new ArrayList<Integer>();
		for(int i = 0; i < N; i++) {
			heights.add(sc.nextInt());
		}
		sc.close();
		int number = getHeight(heights);
		System.out.println(number);
	} main {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		List<Integer> heights = new ArrayList<Integer>();
		for(int i = 0; i < N; i++) {
			heights.add(sc.nextInt());
		}
		sc.close();
		int number = getHeight(heights);
		System.out.println(number);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc List<Integer> heights = new ArrayList<Integer>(); heights new ArrayList<Integer>() for(int i = 0; i < N; i++) {
			heights.add(sc.nextInt());
		} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
			heights.add(sc.nextInt());
		} {
			heights.add(sc.nextInt());
		} heights.add(sc.nextInt()) heights.add heights sc.nextInt() sc.nextInt sc sc.close() sc.close sc int number = getHeight(heights); number getHeight(heights) getHeight heights System.out.println(number) System.out.println System.out System System.out number String[] args args static int getHeight(List<Integer> heights) {
		if(heights.size() == 0) return 0;
		if(heights.size() == 1) return heights.get(0);
		int number = findSmallest(heights);
		List<Integer> updatedList = updateHeights(heights, number);
		List<List<Integer>> partitions = divide(updatedList);
		for(List<Integer> list: partitions) {
			number += getHeight(list);
		}
		return number;
		
	} getHeight {
		if(heights.size() == 0) return 0;
		if(heights.size() == 1) return heights.get(0);
		int number = findSmallest(heights);
		List<Integer> updatedList = updateHeights(heights, number);
		List<List<Integer>> partitions = divide(updatedList);
		for(List<Integer> list: partitions) {
			number += getHeight(list);
		}
		return number;
		
	} if(heights.size() == 0) return 0; heights.size() == 0 heights.size() heights.size heights 0 return 0; 0 if(heights.size() == 1) return heights.get(0); heights.size() == 1 heights.size() heights.size heights 1 return heights.get(0); heights.get(0) heights.get heights 0 int number = findSmallest(heights); number findSmallest(heights) findSmallest heights List<Integer> updatedList = updateHeights(heights, number); updatedList updateHeights(heights, number) updateHeights heights number List<List<Integer>> partitions = divide(updatedList); partitions divide(updatedList) divide updatedList for(List<Integer> list: partitions) {
			number += getHeight(list);
		} List<Integer> list partitions {
			number += getHeight(list);
		} {
			number += getHeight(list);
		} number += getHeight(list) number getHeight(list) getHeight list return number; number List<Integer> heights heights static int findSmallest(List<Integer> heights) {
		int temp = heights.get(0);
		for(int x : heights) {
			if(temp > x) {
				temp = x;
			}
		}
		return temp;
	} findSmallest {
		int temp = heights.get(0);
		for(int x : heights) {
			if(temp > x) {
				temp = x;
			}
		}
		return temp;
	} int temp = heights.get(0); temp heights.get(0) heights.get heights 0 for(int x : heights) {
			if(temp > x) {
				temp = x;
			}
		} int x heights {
			if(temp > x) {
				temp = x;
			}
		} {
			if(temp > x) {
				temp = x;
			}
		} if(temp > x) {
				temp = x;
			} temp > x temp x {
				temp = x;
			} temp = x temp x return temp; temp List<Integer> heights heights static List<Integer> updateHeights(List<Integer>heights, int number) {
		List<Integer> list = new ArrayList<Integer>();
		for(int x: heights) {
			list.add(x - number);;
		}
		return list;
	} updateHeights {
		List<Integer> list = new ArrayList<Integer>();
		for(int x: heights) {
			list.add(x - number);;
		}
		return list;
	} List<Integer> list = new ArrayList<Integer>(); list new ArrayList<Integer>() for(int x: heights) {
			list.add(x - number);;
		} int x heights {
			list.add(x - number);;
		} {
			list.add(x - number);;
		} list.add(x - number) list.add list x - number x number return list; list List<Integer>heights heights int number number static List<List<Integer>> divide(List<Integer> heights){
		List<List<Integer>> partitions = new ArrayList<List<Integer>>();
		List<Integer> list = null;
		for(int i = 0; i < heights.size(); i++) {
			if(i == 0 ||  heights.get(i) == 0) {
				if(list != null) {
					partitions.add(list);
				}
				list = new ArrayList<Integer>();
			}
			if(heights.get(i) != 0) {
				list.add(heights.get(i));
				if(i == heights.size() - 1) {
					partitions.add(list);
				}
			}
		}
		return partitions;
	} divide {
		List<List<Integer>> partitions = new ArrayList<List<Integer>>();
		List<Integer> list = null;
		for(int i = 0; i < heights.size(); i++) {
			if(i == 0 ||  heights.get(i) == 0) {
				if(list != null) {
					partitions.add(list);
				}
				list = new ArrayList<Integer>();
			}
			if(heights.get(i) != 0) {
				list.add(heights.get(i));
				if(i == heights.size() - 1) {
					partitions.add(list);
				}
			}
		}
		return partitions;
	} List<List<Integer>> partitions = new ArrayList<List<Integer>>(); partitions new ArrayList<List<Integer>>() List<Integer> list = null; list null for(int i = 0; i < heights.size(); i++) {
			if(i == 0 ||  heights.get(i) == 0) {
				if(list != null) {
					partitions.add(list);
				}
				list = new ArrayList<Integer>();
			}
			if(heights.get(i) != 0) {
				list.add(heights.get(i));
				if(i == heights.size() - 1) {
					partitions.add(list);
				}
			}
		} int i = 0; int i = 0; i 0 i < heights.size() i heights.size() heights.size heights i++ i++ i {
			if(i == 0 ||  heights.get(i) == 0) {
				if(list != null) {
					partitions.add(list);
				}
				list = new ArrayList<Integer>();
			}
			if(heights.get(i) != 0) {
				list.add(heights.get(i));
				if(i == heights.size() - 1) {
					partitions.add(list);
				}
			}
		} {
			if(i == 0 ||  heights.get(i) == 0) {
				if(list != null) {
					partitions.add(list);
				}
				list = new ArrayList<Integer>();
			}
			if(heights.get(i) != 0) {
				list.add(heights.get(i));
				if(i == heights.size() - 1) {
					partitions.add(list);
				}
			}
		} if(i == 0 ||  heights.get(i) == 0) {
				if(list != null) {
					partitions.add(list);
				}
				list = new ArrayList<Integer>();
			} i == 0 ||  heights.get(i) == 0 i == 0 i 0 heights.get(i) == 0 heights.get(i) heights.get heights i 0 {
				if(list != null) {
					partitions.add(list);
				}
				list = new ArrayList<Integer>();
			} if(list != null) {
					partitions.add(list);
				} list != null list null {
					partitions.add(list);
				} partitions.add(list) partitions.add partitions list list = new ArrayList<Integer>() list new ArrayList<Integer>() if(heights.get(i) != 0) {
				list.add(heights.get(i));
				if(i == heights.size() - 1) {
					partitions.add(list);
				}
			} heights.get(i) != 0 heights.get(i) heights.get heights i 0 {
				list.add(heights.get(i));
				if(i == heights.size() - 1) {
					partitions.add(list);
				}
			} list.add(heights.get(i)) list.add list heights.get(i) heights.get heights i if(i == heights.size() - 1) {
					partitions.add(list);
				} i == heights.size() - 1 i heights.size() - 1 heights.size() heights.size heights 1 {
					partitions.add(list);
				} partitions.add(list) partitions.add partitions list return partitions; partitions List<Integer> heights heights public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		List<Integer> heights = new ArrayList<Integer>();
		for(int i = 0; i < N; i++) {
			heights.add(sc.nextInt());
		}
		sc.close();
		int number = getHeight(heights);
		System.out.println(number);
	}
	
	static int getHeight(List<Integer> heights) {
		if(heights.size() == 0) return 0;
		if(heights.size() == 1) return heights.get(0);
		int number = findSmallest(heights);
		List<Integer> updatedList = updateHeights(heights, number);
		List<List<Integer>> partitions = divide(updatedList);
		for(List<Integer> list: partitions) {
			number += getHeight(list);
		}
		return number;
		
	}
	
	static int findSmallest(List<Integer> heights) {
		int temp = heights.get(0);
		for(int x : heights) {
			if(temp > x) {
				temp = x;
			}
		}
		return temp;
	}
	
	static List<Integer> updateHeights(List<Integer>heights, int number) {
		List<Integer> list = new ArrayList<Integer>();
		for(int x: heights) {
			list.add(x - number);;
		}
		return list;
	}
	
	static List<List<Integer>> divide(List<Integer> heights){
		List<List<Integer>> partitions = new ArrayList<List<Integer>>();
		List<Integer> list = null;
		for(int i = 0; i < heights.size(); i++) {
			if(i == 0 ||  heights.get(i) == 0) {
				if(list != null) {
					partitions.add(list);
				}
				list = new ArrayList<Integer>();
			}
			if(heights.get(i) != 0) {
				list.add(heights.get(i));
				if(i == heights.size() - 1) {
					partitions.add(list);
				}
			}
		}
		return partitions;
	}
		
} public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		List<Integer> heights = new ArrayList<Integer>();
		for(int i = 0; i < N; i++) {
			heights.add(sc.nextInt());
		}
		sc.close();
		int number = getHeight(heights);
		System.out.println(number);
	}
	
	static int getHeight(List<Integer> heights) {
		if(heights.size() == 0) return 0;
		if(heights.size() == 1) return heights.get(0);
		int number = findSmallest(heights);
		List<Integer> updatedList = updateHeights(heights, number);
		List<List<Integer>> partitions = divide(updatedList);
		for(List<Integer> list: partitions) {
			number += getHeight(list);
		}
		return number;
		
	}
	
	static int findSmallest(List<Integer> heights) {
		int temp = heights.get(0);
		for(int x : heights) {
			if(temp > x) {
				temp = x;
			}
		}
		return temp;
	}
	
	static List<Integer> updateHeights(List<Integer>heights, int number) {
		List<Integer> list = new ArrayList<Integer>();
		for(int x: heights) {
			list.add(x - number);;
		}
		return list;
	}
	
	static List<List<Integer>> divide(List<Integer> heights){
		List<List<Integer>> partitions = new ArrayList<List<Integer>>();
		List<Integer> list = null;
		for(int i = 0; i < heights.size(); i++) {
			if(i == 0 ||  heights.get(i) == 0) {
				if(list != null) {
					partitions.add(list);
				}
				list = new ArrayList<Integer>();
			}
			if(heights.get(i) != 0) {
				list.add(heights.get(i));
				if(i == heights.size() - 1) {
					partitions.add(list);
				}
			}
		}
		return partitions;
	}
		
} Main
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		List<Integer> heights = new ArrayList<Integer>();
		for(int i = 0; i < N; i++) {
			heights.add(sc.nextInt());
		}
		sc.close();
		int number = getHeight(heights);
		System.out.println(number);
	}
	
	static int getHeight(List<Integer> heights) {
		if(heights.size() == 0) return 0;
		if(heights.size() == 1) return heights.get(0);
		int number = findSmallest(heights);
		List<Integer> updatedList = updateHeights(heights, number);
		List<List<Integer>> partitions = divide(updatedList);
		for(List<Integer> list: partitions) {
			number += getHeight(list);
		}
		return number;
		
	}
	
	static int findSmallest(List<Integer> heights) {
		int temp = heights.get(0);
		for(int x : heights) {
			if(temp > x) {
				temp = x;
			}
		}
		return temp;
	}
	
	static List<Integer> updateHeights(List<Integer>heights, int number) {
		List<Integer> list = new ArrayList<Integer>();
		for(int x: heights) {
			list.add(x - number);;
		}
		return list;
	}
	
	static List<List<Integer>> divide(List<Integer> heights){
		List<List<Integer>> partitions = new ArrayList<List<Integer>>();
		List<Integer> list = null;
		for(int i = 0; i < heights.size(); i++) {
			if(i == 0 ||  heights.get(i) == 0) {
				if(list != null) {
					partitions.add(list);
				}
				list = new ArrayList<Integer>();
			}
			if(heights.get(i) != 0) {
				list.add(heights.get(i));
				if(i == heights.size() - 1) {
					partitions.add(list);
				}
			}
		}
		return partitions;
	}
		
}