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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author CLown1331
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        Task448C solver = new Task448C();
        solver.solve(1, in, out);
        out.close();
    }

    static class Task448C {
        int[] ar;
        int[] tree;
        int n;

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            n = in.nextInt();
            ar = new int[n];
            tree = new int[n * 4];
            for (int i = 0; i < n; i++) ar[i] = in.nextInt();
            build(1, 0, n - 1);
            out.println(recur(0, n - 1, 0));
        }

        void build(int node, int b, int e) {
            if (b == e) {
                tree[node] = b;
                return;
            }
            int mid = (b + e) >> 1;
            int left = node << 1;
            int right = left | 1;
            build(left, b, mid);
            build(right, mid + 1, e);
            if (ar[tree[left]] < ar[tree[right]]) {
                tree[node] = tree[left];
            } else tree[node] = tree[right];
        }

        int qu(int node, int b, int e, int i, int j) {
            if (i > e || j < b) return -1;
            if (b >= i && e <= j) return tree[node];
            int mid = (b + e) >> 1;
            int left = node << 1;
            int right = left | 1;
            int p1, p2;
            p1 = qu(left, b, mid, i, j);
            p2 = qu(right, mid + 1, e, i, j);
            if (p1 == -1) return p2;
            if (p2 == -1) return p1;
            if (ar[p1] < ar[p2]) return p1;
            return p2;
        }

        int recur(int b, int e, int h) {
            if (b > e) return 0;
            int mn = qu(1, 0, n - 1, b, e);
            int ans = Math.min(e - b + 1, recur(b, mn - 1, ar[mn]) + recur(mn + 1, e, ar[mn]) + ar[mn] - h);
            return ans;
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}
 