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 MOD = 1e9 + 7;
const long long INF = 1e18;
const int MX = 2e5 + 5;
const string nl = "\n";
void fastscan(int &number) {
  bool negative = false;
  register int c;
  number = 0;
  c = getchar();
  if (c == '-') {
    negative = true;
    c = getchar();
  }
  for (; (c > 47 && c < 58); c = getchar()) number = number * 10 + c - 48;
  if (negative) number *= -1;
}
int modularPow(int x, unsigned int y, int p) {
  int res = 1;
  x = x % p;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
bool prime(int n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
vector<bool> sieve(int n) {
  vector<bool> prime(n + 1, true);
  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p) prime[i] = false;
    }
  }
  return prime;
}
int bsearch(int n, bool func(int)) {
  int x = -1;
  for (int b = n; b >= 1; b /= 2) {
    while (!func(x + b)) x += b;
  }
  int k = x + 1;
}
void input(string name) {
  ifstream fin(name + ".in");
  ofstream fout(name + ".out");
}
long long recur(vector<long long> arr) {
  long long minVal = MOD;
  long long newAns = 0;
  int start = -1;
  long long cnt = 0;
  vector<pair<pair<int, int>, int>> segments;
  for (int i = 0; i < arr.size(); i++) {
    if (arr[i] == 0) {
      if (start != -1) {
        segments.push_back({{start, i - 1}, minVal});
        start = -1;
        minVal = MOD;
      }
    } else {
      if (start == -1) start = i;
      minVal = min(minVal, arr[i]);
      cnt++;
    }
  }
  if (start != -1) segments.push_back({{start, arr.size() - 1}, minVal});
  vector<long long> copArr;
  for (auto el : segments) {
    for (int i = el.first.first; i <= el.first.second; i++) {
      copArr.push_back(arr[i] - el.second);
    }
    newAns += min(el.second + recur(copArr), (long long)copArr.size());
    copArr.clear();
  }
  return min(newAns, cnt);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<long long> arr(n);
  for (int i = 0; i < n; i++) cin >> arr[i];
  cout << recur(arr) << nl;
}
