import java.util.Scanner;
import java.util.Stack;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int answer = 0;
		Stack<Integer> st = new Stack<>();
		for (int i = 0; i < n; i++) {
			int x = sc.nextInt();
			if (st.isEmpty() || st.peek() <= x) st.push(x);
			else {
				int prev = st.pop();
				while (!st.isEmpty() && st.peek() > x) st.pop();
				answer += prev - x;
				st.push(x);
			}
		}

		if (!st.isEmpty()) answer += st.peek();
		System.out.println(answer);
	}

} import java.util.Scanner; Scanner java.util import java.util.Stack; Stack java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int answer = 0;
		Stack<Integer> st = new Stack<>();
		for (int i = 0; i < n; i++) {
			int x = sc.nextInt();
			if (st.isEmpty() || st.peek() <= x) st.push(x);
			else {
				int prev = st.pop();
				while (!st.isEmpty() && st.peek() > x) st.pop();
				answer += prev - x;
				st.push(x);
			}
		}

		if (!st.isEmpty()) answer += st.peek();
		System.out.println(answer);
	}

} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int answer = 0;
		Stack<Integer> st = new Stack<>();
		for (int i = 0; i < n; i++) {
			int x = sc.nextInt();
			if (st.isEmpty() || st.peek() <= x) st.push(x);
			else {
				int prev = st.pop();
				while (!st.isEmpty() && st.peek() > x) st.pop();
				answer += prev - x;
				st.push(x);
			}
		}

		if (!st.isEmpty()) answer += st.peek();
		System.out.println(answer);
	} main {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int answer = 0;
		Stack<Integer> st = new Stack<>();
		for (int i = 0; i < n; i++) {
			int x = sc.nextInt();
			if (st.isEmpty() || st.peek() <= x) st.push(x);
			else {
				int prev = st.pop();
				while (!st.isEmpty() && st.peek() > x) st.pop();
				answer += prev - x;
				st.push(x);
			}
		}

		if (!st.isEmpty()) answer += st.peek();
		System.out.println(answer);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int answer = 0; answer 0 Stack<Integer> st = new Stack<>(); st new Stack<>() for (int i = 0; i < n; i++) {
			int x = sc.nextInt();
			if (st.isEmpty() || st.peek() <= x) st.push(x);
			else {
				int prev = st.pop();
				while (!st.isEmpty() && st.peek() > x) st.pop();
				answer += prev - x;
				st.push(x);
			}
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			int x = sc.nextInt();
			if (st.isEmpty() || st.peek() <= x) st.push(x);
			else {
				int prev = st.pop();
				while (!st.isEmpty() && st.peek() > x) st.pop();
				answer += prev - x;
				st.push(x);
			}
		} {
			int x = sc.nextInt();
			if (st.isEmpty() || st.peek() <= x) st.push(x);
			else {
				int prev = st.pop();
				while (!st.isEmpty() && st.peek() > x) st.pop();
				answer += prev - x;
				st.push(x);
			}
		} int x = sc.nextInt(); x sc.nextInt() sc.nextInt sc if (st.isEmpty() || st.peek() <= x) st.push(x);
			else {
				int prev = st.pop();
				while (!st.isEmpty() && st.peek() > x) st.pop();
				answer += prev - x;
				st.push(x);
			} st.isEmpty() || st.peek() <= x st.isEmpty() st.isEmpty st st.peek() <= x st.peek() st.peek st x st.push(x) st.push st x {
				int prev = st.pop();
				while (!st.isEmpty() && st.peek() > x) st.pop();
				answer += prev - x;
				st.push(x);
			} int prev = st.pop(); prev st.pop() st.pop st while (!st.isEmpty() && st.peek() > x) st.pop(); !st.isEmpty() && st.peek() > x !st.isEmpty() st.isEmpty() st.isEmpty st st.peek() > x st.peek() st.peek st x st.pop() st.pop st answer += prev - x answer prev - x prev x st.push(x) st.push st x if (!st.isEmpty()) answer += st.peek(); !st.isEmpty() st.isEmpty() st.isEmpty st answer += st.peek() answer st.peek() st.peek st System.out.println(answer) System.out.println System.out System System.out answer String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int answer = 0;
		Stack<Integer> st = new Stack<>();
		for (int i = 0; i < n; i++) {
			int x = sc.nextInt();
			if (st.isEmpty() || st.peek() <= x) st.push(x);
			else {
				int prev = st.pop();
				while (!st.isEmpty() && st.peek() > x) st.pop();
				answer += prev - x;
				st.push(x);
			}
		}

		if (!st.isEmpty()) answer += st.peek();
		System.out.println(answer);
	}

} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int answer = 0;
		Stack<Integer> st = new Stack<>();
		for (int i = 0; i < n; i++) {
			int x = sc.nextInt();
			if (st.isEmpty() || st.peek() <= x) st.push(x);
			else {
				int prev = st.pop();
				while (!st.isEmpty() && st.peek() > x) st.pop();
				answer += prev - x;
				st.push(x);
			}
		}

		if (!st.isEmpty()) answer += st.peek();
		System.out.println(answer);
	}

} Main
import java.util.Scanner;
import java.util.Stack;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();

		int answer = 0;
		Stack<Integer> st = new Stack<>();
		for (int i = 0; i < n; i++) {
			int x = sc.nextInt();
			if (st.isEmpty() || st.peek() <= x) st.push(x);
			else {
				int prev = st.pop();
				while (!st.isEmpty() && st.peek() > x) st.pop();
				answer += prev - x;
				st.push(x);
			}
		}

		if (!st.isEmpty()) answer += st.peek();
		System.out.println(answer);
	}

}
