#include <bits/stdc++.h>
using namespace std;
long long n;
std::vector<long long> a(5007);
long long func(long long l, long long r) {
  if (l == r) {
    if (a[l] == 0)
      return 0;
    else
      return 1;
  }
  long long fl = 0;
  long long res = 0;
  long long st = -1;
  for (long long int i = l; i < r + 1; i++) {
    if (a[i] == 0) {
      if (st == -1) {
        fl = 1;
        continue;
      } else {
        res += func(st, i - 1);
        fl = 1;
      }
      st = -1;
    } else {
      if (st == -1) {
        st = i;
      }
    }
  }
  if (fl) {
    if (st != -1) {
      res += func(st, r);
    }
  }
  if (!fl) {
    long long mn = 1e18;
    for (long long int i = l; i < r + 1; i++) {
      mn = min(mn, a[i]);
    }
    res += mn;
    for (long long int i = l; i < r + 1; i++) {
      a[i] -= mn;
    }
    st = -1;
    for (long long int i = l; i < r + 1; i++) {
      if (a[i] == 0) {
        if (st == -1) {
          continue;
        } else {
          res += func(st, i - 1);
          fl = 1;
        }
        st = -1;
      } else {
        if (st == -1) {
          st = i;
        }
      }
    }
    if (st != -1) {
      res += func(st, r);
    }
  }
  return min(r - l + 1, res);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  long long cnt = 0;
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] != 0) cnt++;
  }
  cout << min(cnt, func(0, n - 1));
}
