#include <bits/stdc++.h>
using namespace std;
int n;
int a[5009];
int ans;
int solve(int l, int r) {
  if (l == r) return 1;
  int mins = 1000000009;
  for (int i = l; i <= r; ++i) {
    if (a[i] < mins) mins = a[i];
  }
  if (mins >= r - l + 1) {
    return r - l + 1;
  } else {
    int b = l, e = l, w = mins;
    bool is = false;
    for (int i = l; i <= r; ++i) {
      a[i] -= mins;
      if (a[i] == 0 && !is) continue;
      if (a[i] != 0 && is) continue;
      if (!is)
        b = i, is = true;
      else {
        e = i - 1;
        is = false;
        w += solve(b, e);
      }
    }
    if (is) {
      w += solve(b, r);
    }
    if (w > r - l + 1) w = r - l + 1;
    return w;
  }
}
int main() {
  while (cin >> n) {
    for (int i = 0; i < n; ++i) cin >> a[i];
    a[n] = 0;
    a[n + 1] = 0;
    ans = solve(0, n - 1);
    cout << ans << endl;
  }
  return 0;
}
