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
#include <bits/stdc++.h>
using namespace std;
template <typename T>
class SegmentTree {
  int left(int i) { return (2 * i + 1); }
  int right(int i) { return (2 * i + 2); }
  int pa(int i) { return (i - 1) / 2; }

 public:
  SegmentTree(const std::vector<T>& data,
              std::function<T(T, T)> f = std::plus<T>()) {
    this->f = f;
    this->n = data.size();
    levels = 1, el = 1;
    while (el < static_cast<int>(data.size())) {
      el <<= 1;
      ++levels;
    }
    tree.assign(2 * el, T());
    {
      for (size_t i = 0; i < data.size(); ++i) tree[i + el - 1] = data[i];
      for (int i = el - 2; i >= 0; --i)
        tree[i] = f(tree[2 * i + 1], tree[2 * i + 2]);
    }
  }
  SegmentTree(int n, T val = T(), std::function<T(T, T)> f = std::plus<T>())
      : SegmentTree(std::vector<T>(n, val), f) {}
  T get(int l, int r) { return query(l, r, 0, el, 0); }
  T get(int index) { return tree[el - 1 + index]; }
  void set(int index, T value) {
    int i = el - 1 + index;
    tree[i] = value;
    while (i > 0) {
      i = (i - 1) / 2;
      tree[i] = f(tree[i * 2 + 1], tree[i * 2 + 2]);
    }
  }

 private:
  std::vector<T> tree;
  int levels;
  int n;
  int el;
  std::function<T(T, T)> f;
  T query(int l, int r, int st, int fi, int node) {
    if (l <= st && r >= fi - 1) {
      return tree[node];
    }
    if (l == r) return get(l);
    int mid = (st + fi) / 2;
    if (r < mid) {
      return query(l, r, st, mid, left(node));
    } else if (l >= mid) {
      return query(l, r, mid, fi, right(node));
    } else {
      T lv = query(l, mid - 1, st, mid, left(node));
      T rv = query(mid, r, mid, fi, right(node));
      return f(lv, rv);
    }
  }
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int& v : a) cin >> v;
    std::function<int(int, int)> f = [](int a, int b) { return min(a, b); };
    std::function<int(int, int, int)> res = [&](int l, int r, int base) {
      if (l == r) return 0;
      int id = -1;
      int mi = INT_MAX;
      for (int i = l; i < r; ++i) {
        if (a[i] < mi) {
          mi = a[i];
          id = i;
        }
      }
      if (a[id] == base) {
        return res(l, id, base) + res(id + 1, r, base);
      }
      return min(r - l,
                 a[id] - base + res(l, id, a[id]) + res(id + 1, r, a[id]));
    };
    cout << res(0, n, 0);
  }
}
