
import java.util.HashMap;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int[] arr = new int[n];
		HashMap<Integer, Integer> hmap = new HashMap<>();

		for (int i = 0; i < n; i++) {
			arr[i] = scn.nextInt();
			hmap.put(arr[i], i);

		}
		System.out.println(solve(hmap, arr, 0, n - 1));
	}

	public static int solve(HashMap<Integer, Integer> hmap, int[] arr, int i, int j) {
		if (i > j) {
			//System.out.println("pp " + i + " " + j);
			return 0;
		}
		int min = Integer.MAX_VALUE, idx = 0;
		//System.out.println(hmap + " " + i + " " + j);
		for (int k = i; k <= j; k++) {
			if (arr[k] < min) {
				min = arr[k];
				idx = k;
			}
		}
		HashMap<Integer, Integer> map = new HashMap<>();
		for (int k = i; k <= j; k++) {
			arr[k] -= min;
		}

		for (int k = i; k <= idx - 1; k++) {
			map.put(arr[k], k);
		}
		int x = solve(map, arr, i, idx - 1);

		map = new HashMap<>();

		for (int k = idx + 1; k <= hmap.get(min) - 1; k++) {
			map.put(arr[k], k);
		}
		int y = solve(map, arr, idx + 1, hmap.get(min) - 1);
		
		map = new HashMap<>();

		for (int k = hmap.get(min) + 1; k <= j; k++) {
			map.put(arr[k], k);
		}

		int z = solve(map, arr, hmap.get(min) + 1, j);
		
		return x + y + z + min;
	}

} import java.util.HashMap; HashMap java.util import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int[] arr = new int[n];
		HashMap<Integer, Integer> hmap = new HashMap<>();

		for (int i = 0; i < n; i++) {
			arr[i] = scn.nextInt();
			hmap.put(arr[i], i);

		}
		System.out.println(solve(hmap, arr, 0, n - 1));
	}

	public static int solve(HashMap<Integer, Integer> hmap, int[] arr, int i, int j) {
		if (i > j) {
			//System.out.println("pp " + i + " " + j);
			return 0;
		}
		int min = Integer.MAX_VALUE, idx = 0;
		//System.out.println(hmap + " " + i + " " + j);
		for (int k = i; k <= j; k++) {
			if (arr[k] < min) {
				min = arr[k];
				idx = k;
			}
		}
		HashMap<Integer, Integer> map = new HashMap<>();
		for (int k = i; k <= j; k++) {
			arr[k] -= min;
		}

		for (int k = i; k <= idx - 1; k++) {
			map.put(arr[k], k);
		}
		int x = solve(map, arr, i, idx - 1);

		map = new HashMap<>();

		for (int k = idx + 1; k <= hmap.get(min) - 1; k++) {
			map.put(arr[k], k);
		}
		int y = solve(map, arr, idx + 1, hmap.get(min) - 1);
		
		map = new HashMap<>();

		for (int k = hmap.get(min) + 1; k <= j; k++) {
			map.put(arr[k], k);
		}

		int z = solve(map, arr, hmap.get(min) + 1, j);
		
		return x + y + z + min;
	}

} Main public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int[] arr = new int[n];
		HashMap<Integer, Integer> hmap = new HashMap<>();

		for (int i = 0; i < n; i++) {
			arr[i] = scn.nextInt();
			hmap.put(arr[i], i);

		}
		System.out.println(solve(hmap, arr, 0, n - 1));
	} main {
		// TODO Auto-generated method stub
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int[] arr = new int[n];
		HashMap<Integer, Integer> hmap = new HashMap<>();

		for (int i = 0; i < n; i++) {
			arr[i] = scn.nextInt();
			hmap.put(arr[i], i);

		}
		System.out.println(solve(hmap, arr, 0, n - 1));
	} Scanner scn = new Scanner(System.in); scn new Scanner(System.in) int n = scn.nextInt(); n scn.nextInt() scn.nextInt scn int[] arr = new int[n]; arr new int[n] n HashMap<Integer, Integer> hmap = new HashMap<>(); hmap new HashMap<>() for (int i = 0; i < n; i++) {
			arr[i] = scn.nextInt();
			hmap.put(arr[i], i);

		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			arr[i] = scn.nextInt();
			hmap.put(arr[i], i);

		} {
			arr[i] = scn.nextInt();
			hmap.put(arr[i], i);

		} arr[i] = scn.nextInt() arr[i] arr i scn.nextInt() scn.nextInt scn hmap.put(arr[i], i) hmap.put hmap arr[i] arr i i System.out.println(solve(hmap, arr, 0, n - 1)) System.out.println System.out System System.out solve(hmap, arr, 0, n - 1) solve hmap arr 0 n - 1 n 1 String[] args args public static int solve(HashMap<Integer, Integer> hmap, int[] arr, int i, int j) {
		if (i > j) {
			//System.out.println("pp " + i + " " + j);
			return 0;
		}
		int min = Integer.MAX_VALUE, idx = 0;
		//System.out.println(hmap + " " + i + " " + j);
		for (int k = i; k <= j; k++) {
			if (arr[k] < min) {
				min = arr[k];
				idx = k;
			}
		}
		HashMap<Integer, Integer> map = new HashMap<>();
		for (int k = i; k <= j; k++) {
			arr[k] -= min;
		}

		for (int k = i; k <= idx - 1; k++) {
			map.put(arr[k], k);
		}
		int x = solve(map, arr, i, idx - 1);

		map = new HashMap<>();

		for (int k = idx + 1; k <= hmap.get(min) - 1; k++) {
			map.put(arr[k], k);
		}
		int y = solve(map, arr, idx + 1, hmap.get(min) - 1);
		
		map = new HashMap<>();

		for (int k = hmap.get(min) + 1; k <= j; k++) {
			map.put(arr[k], k);
		}

		int z = solve(map, arr, hmap.get(min) + 1, j);
		
		return x + y + z + min;
	} solve {
		if (i > j) {
			//System.out.println("pp " + i + " " + j);
			return 0;
		}
		int min = Integer.MAX_VALUE, idx = 0;
		//System.out.println(hmap + " " + i + " " + j);
		for (int k = i; k <= j; k++) {
			if (arr[k] < min) {
				min = arr[k];
				idx = k;
			}
		}
		HashMap<Integer, Integer> map = new HashMap<>();
		for (int k = i; k <= j; k++) {
			arr[k] -= min;
		}

		for (int k = i; k <= idx - 1; k++) {
			map.put(arr[k], k);
		}
		int x = solve(map, arr, i, idx - 1);

		map = new HashMap<>();

		for (int k = idx + 1; k <= hmap.get(min) - 1; k++) {
			map.put(arr[k], k);
		}
		int y = solve(map, arr, idx + 1, hmap.get(min) - 1);
		
		map = new HashMap<>();

		for (int k = hmap.get(min) + 1; k <= j; k++) {
			map.put(arr[k], k);
		}

		int z = solve(map, arr, hmap.get(min) + 1, j);
		
		return x + y + z + min;
	} if (i > j) {
			//System.out.println("pp " + i + " " + j);
			return 0;
		} i > j i j {
			//System.out.println("pp " + i + " " + j);
			return 0;
		} return 0; 0 int min = Integer.MAX_VALUE min Integer.MAX_VALUE Integer Integer.MAX_VALUE idx = 0; idx 0 for (int k = i; k <= j; k++) {
			if (arr[k] < min) {
				min = arr[k];
				idx = k;
			}
		} int k = i; int k = i; k i k <= j k j k++ k++ k {
			if (arr[k] < min) {
				min = arr[k];
				idx = k;
			}
		} {
			if (arr[k] < min) {
				min = arr[k];
				idx = k;
			}
		} if (arr[k] < min) {
				min = arr[k];
				idx = k;
			} arr[k] < min arr[k] arr k min {
				min = arr[k];
				idx = k;
			} min = arr[k] min arr[k] arr k idx = k idx k HashMap<Integer, Integer> map = new HashMap<>(); map new HashMap<>() for (int k = i; k <= j; k++) {
			arr[k] -= min;
		} int k = i; int k = i; k i k <= j k j k++ k++ k {
			arr[k] -= min;
		} {
			arr[k] -= min;
		} arr[k] -= min arr[k] arr k min for (int k = i; k <= idx - 1; k++) {
			map.put(arr[k], k);
		} int k = i; int k = i; k i k <= idx - 1 k idx - 1 idx 1 k++ k++ k {
			map.put(arr[k], k);
		} {
			map.put(arr[k], k);
		} map.put(arr[k], k) map.put map arr[k] arr k k int x = solve(map, arr, i, idx - 1); x solve(map, arr, i, idx - 1) solve map arr i idx - 1 idx 1 map = new HashMap<>() map new HashMap<>() for (int k = idx + 1; k <= hmap.get(min) - 1; k++) {
			map.put(arr[k], k);
		} int k = idx + 1; int k = idx + 1; k idx + 1 idx 1 k <= hmap.get(min) - 1 k hmap.get(min) - 1 hmap.get(min) hmap.get hmap min 1 k++ k++ k {
			map.put(arr[k], k);
		} {
			map.put(arr[k], k);
		} map.put(arr[k], k) map.put map arr[k] arr k k int y = solve(map, arr, idx + 1, hmap.get(min) - 1); y solve(map, arr, idx + 1, hmap.get(min) - 1) solve map arr idx + 1 idx 1 hmap.get(min) - 1 hmap.get(min) hmap.get hmap min 1 map = new HashMap<>() map new HashMap<>() for (int k = hmap.get(min) + 1; k <= j; k++) {
			map.put(arr[k], k);
		} int k = hmap.get(min) + 1; int k = hmap.get(min) + 1; k hmap.get(min) + 1 hmap.get(min) hmap.get hmap min 1 k <= j k j k++ k++ k {
			map.put(arr[k], k);
		} {
			map.put(arr[k], k);
		} map.put(arr[k], k) map.put map arr[k] arr k k int z = solve(map, arr, hmap.get(min) + 1, j); z solve(map, arr, hmap.get(min) + 1, j) solve map arr hmap.get(min) + 1 hmap.get(min) hmap.get hmap min 1 j return x + y + z + min; x + y + z + min z x + y + z + min x y z min HashMap<Integer, Integer> hmap hmap int[] arr arr int i i int j j public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int[] arr = new int[n];
		HashMap<Integer, Integer> hmap = new HashMap<>();

		for (int i = 0; i < n; i++) {
			arr[i] = scn.nextInt();
			hmap.put(arr[i], i);

		}
		System.out.println(solve(hmap, arr, 0, n - 1));
	}

	public static int solve(HashMap<Integer, Integer> hmap, int[] arr, int i, int j) {
		if (i > j) {
			//System.out.println("pp " + i + " " + j);
			return 0;
		}
		int min = Integer.MAX_VALUE, idx = 0;
		//System.out.println(hmap + " " + i + " " + j);
		for (int k = i; k <= j; k++) {
			if (arr[k] < min) {
				min = arr[k];
				idx = k;
			}
		}
		HashMap<Integer, Integer> map = new HashMap<>();
		for (int k = i; k <= j; k++) {
			arr[k] -= min;
		}

		for (int k = i; k <= idx - 1; k++) {
			map.put(arr[k], k);
		}
		int x = solve(map, arr, i, idx - 1);

		map = new HashMap<>();

		for (int k = idx + 1; k <= hmap.get(min) - 1; k++) {
			map.put(arr[k], k);
		}
		int y = solve(map, arr, idx + 1, hmap.get(min) - 1);
		
		map = new HashMap<>();

		for (int k = hmap.get(min) + 1; k <= j; k++) {
			map.put(arr[k], k);
		}

		int z = solve(map, arr, hmap.get(min) + 1, j);
		
		return x + y + z + min;
	}

} public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int[] arr = new int[n];
		HashMap<Integer, Integer> hmap = new HashMap<>();

		for (int i = 0; i < n; i++) {
			arr[i] = scn.nextInt();
			hmap.put(arr[i], i);

		}
		System.out.println(solve(hmap, arr, 0, n - 1));
	}

	public static int solve(HashMap<Integer, Integer> hmap, int[] arr, int i, int j) {
		if (i > j) {
			//System.out.println("pp " + i + " " + j);
			return 0;
		}
		int min = Integer.MAX_VALUE, idx = 0;
		//System.out.println(hmap + " " + i + " " + j);
		for (int k = i; k <= j; k++) {
			if (arr[k] < min) {
				min = arr[k];
				idx = k;
			}
		}
		HashMap<Integer, Integer> map = new HashMap<>();
		for (int k = i; k <= j; k++) {
			arr[k] -= min;
		}

		for (int k = i; k <= idx - 1; k++) {
			map.put(arr[k], k);
		}
		int x = solve(map, arr, i, idx - 1);

		map = new HashMap<>();

		for (int k = idx + 1; k <= hmap.get(min) - 1; k++) {
			map.put(arr[k], k);
		}
		int y = solve(map, arr, idx + 1, hmap.get(min) - 1);
		
		map = new HashMap<>();

		for (int k = hmap.get(min) + 1; k <= j; k++) {
			map.put(arr[k], k);
		}

		int z = solve(map, arr, hmap.get(min) + 1, j);
		
		return x + y + z + min;
	}

} Main

import java.util.HashMap;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int[] arr = new int[n];
		HashMap<Integer, Integer> hmap = new HashMap<>();

		for (int i = 0; i < n; i++) {
			arr[i] = scn.nextInt();
			hmap.put(arr[i], i);

		}
		System.out.println(solve(hmap, arr, 0, n - 1));
	}

	public static int solve(HashMap<Integer, Integer> hmap, int[] arr, int i, int j) {
		if (i > j) {
			//System.out.println("pp " + i + " " + j);
			return 0;
		}
		int min = Integer.MAX_VALUE, idx = 0;
		//System.out.println(hmap + " " + i + " " + j);
		for (int k = i; k <= j; k++) {
			if (arr[k] < min) {
				min = arr[k];
				idx = k;
			}
		}
		HashMap<Integer, Integer> map = new HashMap<>();
		for (int k = i; k <= j; k++) {
			arr[k] -= min;
		}

		for (int k = i; k <= idx - 1; k++) {
			map.put(arr[k], k);
		}
		int x = solve(map, arr, i, idx - 1);

		map = new HashMap<>();

		for (int k = idx + 1; k <= hmap.get(min) - 1; k++) {
			map.put(arr[k], k);
		}
		int y = solve(map, arr, idx + 1, hmap.get(min) - 1);
		
		map = new HashMap<>();

		for (int k = hmap.get(min) + 1; k <= j; k++) {
			map.put(arr[k], k);
		}

		int z = solve(map, arr, hmap.get(min) + 1, j);
		
		return x + y + z + min;
	}

}
