You have a multiset containing several integers. Initially, it contains a_1 elements equal to 1, a_2 elements equal to 2, ..., a_n elements equal to n.

You may apply two types of operations:

  * choose two integers l and r (l ≤ r), then remove one occurrence of l, one occurrence of l + 1, ..., one occurrence of r from the multiset. This operation can be applied only if each number from l to r occurs at least once in the multiset; 
  * choose two integers i and x (x ≥ 1), then remove x occurrences of i from the multiset. This operation can be applied only if the multiset contains at least x occurrences of i. 



What is the minimum number of operations required to delete all elements from the multiset?

Input

The first line contains one integer n (1 ≤ n ≤ 5000).

The second line contains n integers a_1, a_2, ..., a_n (0 ≤ a_i ≤ 10^9).

Output

Print one integer — the minimum number of operations required to delete all elements from the multiset.

Examples

Input


4
1 4 1 1


Output


2


Input


5
1 0 1 0 1


Output


3
import java.io.*;
import java.util.*;

public class A {
	public static void main (String[] args) { new A(); }

	int n;
	int[] a, nxt[], min[];
	int[][] delta;
	HashMap<Integer, Integer> map;
	
	public A() {
		FastScanner fs = new FastScanner();
		PrintWriter out = new PrintWriter(System.out);
		System.err.println("");

		n = fs.nextInt();
		a = fs.nextIntArray(n);
		map = new HashMap<>();
		for(int i = 0; i < n; i++) {
			if(!map.containsKey(a[i])) {
				map.put(a[i], map.size());
			}
		}
		
		nxt = new int[n][map.size()];
		for(int i = 0; i < n; i++) Arrays.fill(nxt[i], n);
		for(int i = n-1; i >= 0; i--) {
			for(int j = 0; i+1 < n && j < map.size(); j++)
				nxt[i][j] = nxt[i+1][j];
			nxt[i][map.get(a[i])] = i;
		}
		
		min = new int[n][n];
		for(int i = 0; i < n; i++) {
			min[i][i] = a[i];
			for(int j = i + 1; j < n; j++) {
				min[i][j] = Math.min(min[i][j-1], a[j]);
			}
		}
		
		long res = dfs(0, n-1, 0);
		
		out.println(res);
		
		out.close();
	}

	int dfs(int L, int R, int sum) {
		if(R < L) return 0;

		int res = R-L+1;
		
		int curL = L;
		int minVal = min[L][R];
		int idx = map.get(minVal);
		
		if(minVal > 1000000) {
			return res;
		}
		int add = minVal-sum;
		int res1 = add;
		while(curL < n && nxt[curL][idx] <= R) {
			int pos = nxt[curL][idx];
			long ret = dfs(curL, pos-1, sum+add);
			res1 += ret;
			curL = pos+1;
		}
		res1 += dfs(curL, R, sum+add);
		
		res = Math.min(res, res1);
		return res;
	}
	

	class FastScanner {
		public int BS = 1<<16;
		public char NC = (char)0;
		byte[] buf = new byte[BS];
		int bId = 0, size = 0;
		char c = NC;
		double num = 1;
		BufferedInputStream in;

		public FastScanner() {
			in = new BufferedInputStream(System.in, BS);
		}

		public FastScanner(String s) {
			try {
				in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
			}
			catch (Exception e) {
				in = new BufferedInputStream(System.in, BS);
			}
		}

		public char nextChar(){
			while(bId==size) {
				try {
					size = in.read(buf);
				}catch(Exception e) {
					return NC;
				}                
				if(size==-1)return NC;
				bId=0;
			}
			return (char)buf[bId++];
		}

		public int nextInt() {
			return (int)nextLong();
		}

		public long nextLong() {
			num=1;
			boolean neg = false;
			if(c==NC)c=nextChar();
			for(;(c<'0' || c>'9'); c = nextChar()) {
				if(c=='-')neg=true;
			}
			long res = 0;
			for(; c>='0' && c <='9'; c=nextChar()) {
				res = (res<<3)+(res<<1)+c-'0';
				num*=10;
			}
			return neg?-res:res;
		}

		public double nextDouble() {
			double cur = nextLong();
			return c!='.' ? cur:cur+nextLong()/num;
		}

		public String next() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c>32) {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public String nextLine() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c!='\n') {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public boolean hasNext() {
			if(c>32)return true;
			while(true) {
				c=nextChar();
				if(c==NC)return false;
				else if(c>32)return true;
			}
		}
		
		public int[] nextIntArray(int n) {
			int[] res = new int[n];
			for(int i = 0; i < n; i++) res[i] = nextInt();
			return res;
		}
		
	}

}