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

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27);
        thread.start();
        thread.join();
    }

    static class TaskAdapter implements Runnable {
        @Override
        public void run() {
            InputStream inputStream = System.in;
            OutputStream outputStream = System.out;
            FastReader in = new FastReader(inputStream);
            PrintWriter out = new PrintWriter(outputStream);
            EClearTheMultiset solver = new EClearTheMultiset();
            solver.solve(1, in, out);
            out.close();
        }
    }

    static class EClearTheMultiset {
        int[] a;
        int[] cur;
        int n;

        public void solve(int testNumber, FastReader s, PrintWriter w) {
            n = s.nextInt();
            a = new int[n];
            for (int i = 0; i < n; i++) a[i] = s.nextInt();
            cur = a.clone();
            int res = 0;
            int prev = 0;
            for (int i = 0; i < n; i++) {
                if (a[i] == 0) {
                    res += calc(prev, i - 1);
                    prev = i + 1;
                }
            }
            res += calc(prev, n - 1);
            w.println(res);
        }

        int calc(int l, int r) {
            if (l > r) return 0;
            if (l == r) {
                if (cur[l] == 0) return 0;
                else return 1;
            }
            boolean ok = false;
            for (int i = l; i <= r; i++)
                if (a[i] <= n) {
                    ok = true;
                    break;
                }
            if (!ok) return r - l + 1;
            for (int i = l; i <= r; i++) cur[i]--;
            int res = 1, prev = l;
            for (int i = l; i <= r; i++) {
                if (cur[i] == 0) {
                    //if (n == 4) System.err.println(prev + " " + (i - 1));
                    res += calc(prev, i - 1);
                    prev = i + 1;
                }
            }
            res += calc(prev, r);
            return Math.min(r - l + 1, res);
        }

    }

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

        public FastReader(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 c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

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

        }

    }
}

