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;
const int N = 5e3 + 5;
const int INF = 1 << 30;
struct Node {
  int x;
  Node(int _x) : x(_x) {}
  Node() : x(INF) {}
};
int n;
int st[N];
int a[N];
int lazy[N << 2];
Node t[N << 2];
using pi = pair<int, int>;
Node combine(Node lhs, Node rhs) {
  Node temp;
  temp.x = min(lhs.x, rhs.x);
  return temp;
}
auto build(int v, int l, int r) -> void {
  if (l == r) {
    t[v] = a[l];
    return;
  }
  int mid = (l + r) >> 1;
  build(v << 1, l, mid);
  build(v << 1 | 1, mid + 1, r);
  t[v] = combine(t[v << 1], t[v << 1 | 1]);
}
auto push(int v, int l, int r) -> void {
  t[v].x += lazy[v];
  if (l < r) {
    lazy[v << 1] += lazy[v];
    lazy[(v << 1) ^ 1] += lazy[v];
  }
  lazy[v] = 0;
}
void update(int v, int l, int r, int x, int y, int to_add) {
  push(v, l, r);
  if (l > y || r < x) return;
  if (l >= x && r <= y) {
    lazy[v] += to_add;
    push(v, l, r);
    return;
  }
  int mid = (l + r) >> 1;
  update(v << 1, l, mid, x, y, to_add);
  update((v << 1) ^ 1, mid + 1, r, x, y, to_add);
  t[v] = combine(t[v << 1], t[(v << 1) ^ 1]);
}
Node get(int v, int l, int r, int x, int y) {
  if (l > y || r < x) return {INF};
  push(v, l, r);
  if (l >= x && r <= y) return t[v];
  int mid = (l + r) >> 1;
  return combine(get(v << 1, l, mid, x, y),
                 get((v << 1) ^ 1, mid + 1, r, x, y));
}
const pi mdefault = make_pair(INF, -1);
auto ref(int l, int r) -> int {
  if (l > r) return 0;
  auto lo = get(1, 1, n, l, r).x;
  if (lo == 0) {
    pi cur{INF, -1};
    vector<pi> nxt;
    for (int i = l; i <= r; ++i) {
      if (get(1, 1, n, i, i).x != 0) {
        cur.first = min(cur.first, i);
        cur.second = max(cur.second, i);
      } else {
        if (cur != mdefault) {
          nxt.emplace_back(cur);
        }
        cur = mdefault;
      }
    }
    if (cur != mdefault) {
      nxt.emplace_back(cur);
    }
    auto res = 0;
    for (auto &v : nxt) res += ref(v.first, v.second);
    return res;
  } else {
    update(1, 1, n, l, r, -lo);
    return min(r + 1 - l, lo + ref(l, r));
  }
}
auto solve() -> void {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  build(1, 1, n);
  auto res = ref(1, n);
  cout << res << '\n';
}
auto main() -> int {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int tc = 1;
  while (tc--) {
    solve();
  }
  return 0;
}
