
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();
		int tmp = sc.nextInt();
		int tmp2 = tmp;
		List<Integer> list = new ArrayList<>();
		if(N == 1) {
			list.add(tmp);
		}
		boolean up = false, down = false;
		for(int i = 1; i < N; i++) {
			tmp2 = sc.nextInt();
			if(tmp2 > tmp) {
				if(!up) {
					list.add(tmp);
				}
				up = true;
				down = false;
			} else if(tmp2 < tmp) {
				if(!down) {
					list.add(tmp);
				}
				down = true;
				up = false;
			}
			if(i == N - 1) {
				list.add(tmp2);
			}
			tmp = tmp2;
		}
		int ans = list.get(0);
		int high = list.get(0);
		list.remove(0);
		while(!list.isEmpty()) {
			if(high < list.get(0)) {
				ans += list.get(0) - high;
			}
			high = list.get(0);
			list.remove(0);
		}
		System.out.println(ans);
	}

} 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();
		int tmp = sc.nextInt();
		int tmp2 = tmp;
		List<Integer> list = new ArrayList<>();
		if(N == 1) {
			list.add(tmp);
		}
		boolean up = false, down = false;
		for(int i = 1; i < N; i++) {
			tmp2 = sc.nextInt();
			if(tmp2 > tmp) {
				if(!up) {
					list.add(tmp);
				}
				up = true;
				down = false;
			} else if(tmp2 < tmp) {
				if(!down) {
					list.add(tmp);
				}
				down = true;
				up = false;
			}
			if(i == N - 1) {
				list.add(tmp2);
			}
			tmp = tmp2;
		}
		int ans = list.get(0);
		int high = list.get(0);
		list.remove(0);
		while(!list.isEmpty()) {
			if(high < list.get(0)) {
				ans += list.get(0) - high;
			}
			high = list.get(0);
			list.remove(0);
		}
		System.out.println(ans);
	}

} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int tmp = sc.nextInt();
		int tmp2 = tmp;
		List<Integer> list = new ArrayList<>();
		if(N == 1) {
			list.add(tmp);
		}
		boolean up = false, down = false;
		for(int i = 1; i < N; i++) {
			tmp2 = sc.nextInt();
			if(tmp2 > tmp) {
				if(!up) {
					list.add(tmp);
				}
				up = true;
				down = false;
			} else if(tmp2 < tmp) {
				if(!down) {
					list.add(tmp);
				}
				down = true;
				up = false;
			}
			if(i == N - 1) {
				list.add(tmp2);
			}
			tmp = tmp2;
		}
		int ans = list.get(0);
		int high = list.get(0);
		list.remove(0);
		while(!list.isEmpty()) {
			if(high < list.get(0)) {
				ans += list.get(0) - high;
			}
			high = list.get(0);
			list.remove(0);
		}
		System.out.println(ans);
	} main {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int tmp = sc.nextInt();
		int tmp2 = tmp;
		List<Integer> list = new ArrayList<>();
		if(N == 1) {
			list.add(tmp);
		}
		boolean up = false, down = false;
		for(int i = 1; i < N; i++) {
			tmp2 = sc.nextInt();
			if(tmp2 > tmp) {
				if(!up) {
					list.add(tmp);
				}
				up = true;
				down = false;
			} else if(tmp2 < tmp) {
				if(!down) {
					list.add(tmp);
				}
				down = true;
				up = false;
			}
			if(i == N - 1) {
				list.add(tmp2);
			}
			tmp = tmp2;
		}
		int ans = list.get(0);
		int high = list.get(0);
		list.remove(0);
		while(!list.isEmpty()) {
			if(high < list.get(0)) {
				ans += list.get(0) - high;
			}
			high = list.get(0);
			list.remove(0);
		}
		System.out.println(ans);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int tmp = sc.nextInt(); tmp sc.nextInt() sc.nextInt sc int tmp2 = tmp; tmp2 tmp List<Integer> list = new ArrayList<>(); list new ArrayList<>() if(N == 1) {
			list.add(tmp);
		} N == 1 N 1 {
			list.add(tmp);
		} list.add(tmp) list.add list tmp boolean up = false up false down = false; down false for(int i = 1; i < N; i++) {
			tmp2 = sc.nextInt();
			if(tmp2 > tmp) {
				if(!up) {
					list.add(tmp);
				}
				up = true;
				down = false;
			} else if(tmp2 < tmp) {
				if(!down) {
					list.add(tmp);
				}
				down = true;
				up = false;
			}
			if(i == N - 1) {
				list.add(tmp2);
			}
			tmp = tmp2;
		} int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
			tmp2 = sc.nextInt();
			if(tmp2 > tmp) {
				if(!up) {
					list.add(tmp);
				}
				up = true;
				down = false;
			} else if(tmp2 < tmp) {
				if(!down) {
					list.add(tmp);
				}
				down = true;
				up = false;
			}
			if(i == N - 1) {
				list.add(tmp2);
			}
			tmp = tmp2;
		} {
			tmp2 = sc.nextInt();
			if(tmp2 > tmp) {
				if(!up) {
					list.add(tmp);
				}
				up = true;
				down = false;
			} else if(tmp2 < tmp) {
				if(!down) {
					list.add(tmp);
				}
				down = true;
				up = false;
			}
			if(i == N - 1) {
				list.add(tmp2);
			}
			tmp = tmp2;
		} tmp2 = sc.nextInt() tmp2 sc.nextInt() sc.nextInt sc if(tmp2 > tmp) {
				if(!up) {
					list.add(tmp);
				}
				up = true;
				down = false;
			} else if(tmp2 < tmp) {
				if(!down) {
					list.add(tmp);
				}
				down = true;
				up = false;
			} tmp2 > tmp tmp2 tmp {
				if(!up) {
					list.add(tmp);
				}
				up = true;
				down = false;
			} if(!up) {
					list.add(tmp);
				} !up up {
					list.add(tmp);
				} list.add(tmp) list.add list tmp up = true up true down = false down false if(tmp2 < tmp) {
				if(!down) {
					list.add(tmp);
				}
				down = true;
				up = false;
			} tmp2 < tmp tmp2 tmp {
				if(!down) {
					list.add(tmp);
				}
				down = true;
				up = false;
			} if(!down) {
					list.add(tmp);
				} !down down {
					list.add(tmp);
				} list.add(tmp) list.add list tmp down = true down true up = false up false if(i == N - 1) {
				list.add(tmp2);
			} i == N - 1 i N - 1 N 1 {
				list.add(tmp2);
			} list.add(tmp2) list.add list tmp2 tmp = tmp2 tmp tmp2 int ans = list.get(0); ans list.get(0) list.get list 0 int high = list.get(0); high list.get(0) list.get list 0 list.remove(0) list.remove list 0 while(!list.isEmpty()) {
			if(high < list.get(0)) {
				ans += list.get(0) - high;
			}
			high = list.get(0);
			list.remove(0);
		} !list.isEmpty() list.isEmpty() list.isEmpty list {
			if(high < list.get(0)) {
				ans += list.get(0) - high;
			}
			high = list.get(0);
			list.remove(0);
		} if(high < list.get(0)) {
				ans += list.get(0) - high;
			} high < list.get(0) high list.get(0) list.get list 0 {
				ans += list.get(0) - high;
			} ans += list.get(0) - high ans list.get(0) - high list.get(0) list.get list 0 high high = list.get(0) high list.get(0) list.get list 0 list.remove(0) list.remove list 0 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 sc = new Scanner(System.in);
		int N = sc.nextInt();
		int tmp = sc.nextInt();
		int tmp2 = tmp;
		List<Integer> list = new ArrayList<>();
		if(N == 1) {
			list.add(tmp);
		}
		boolean up = false, down = false;
		for(int i = 1; i < N; i++) {
			tmp2 = sc.nextInt();
			if(tmp2 > tmp) {
				if(!up) {
					list.add(tmp);
				}
				up = true;
				down = false;
			} else if(tmp2 < tmp) {
				if(!down) {
					list.add(tmp);
				}
				down = true;
				up = false;
			}
			if(i == N - 1) {
				list.add(tmp2);
			}
			tmp = tmp2;
		}
		int ans = list.get(0);
		int high = list.get(0);
		list.remove(0);
		while(!list.isEmpty()) {
			if(high < list.get(0)) {
				ans += list.get(0) - high;
			}
			high = list.get(0);
			list.remove(0);
		}
		System.out.println(ans);
	}

} public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int tmp = sc.nextInt();
		int tmp2 = tmp;
		List<Integer> list = new ArrayList<>();
		if(N == 1) {
			list.add(tmp);
		}
		boolean up = false, down = false;
		for(int i = 1; i < N; i++) {
			tmp2 = sc.nextInt();
			if(tmp2 > tmp) {
				if(!up) {
					list.add(tmp);
				}
				up = true;
				down = false;
			} else if(tmp2 < tmp) {
				if(!down) {
					list.add(tmp);
				}
				down = true;
				up = false;
			}
			if(i == N - 1) {
				list.add(tmp2);
			}
			tmp = tmp2;
		}
		int ans = list.get(0);
		int high = list.get(0);
		list.remove(0);
		while(!list.isEmpty()) {
			if(high < list.get(0)) {
				ans += list.get(0) - high;
			}
			high = list.get(0);
			list.remove(0);
		}
		System.out.println(ans);
	}

} 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();
		int tmp = sc.nextInt();
		int tmp2 = tmp;
		List<Integer> list = new ArrayList<>();
		if(N == 1) {
			list.add(tmp);
		}
		boolean up = false, down = false;
		for(int i = 1; i < N; i++) {
			tmp2 = sc.nextInt();
			if(tmp2 > tmp) {
				if(!up) {
					list.add(tmp);
				}
				up = true;
				down = false;
			} else if(tmp2 < tmp) {
				if(!down) {
					list.add(tmp);
				}
				down = true;
				up = false;
			}
			if(i == N - 1) {
				list.add(tmp2);
			}
			tmp = tmp2;
		}
		int ans = list.get(0);
		int high = list.get(0);
		list.remove(0);
		while(!list.isEmpty()) {
			if(high < list.get(0)) {
				ans += list.get(0) - high;
			}
			high = list.get(0);
			list.remove(0);
		}
		System.out.println(ans);
	}

}
