#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 1;
int n, a[N];
int cal(int l, int r, int mn) {
  if (l > r) return 0;
  int cmn = (int)1e9 + 1;
  for (int i = l; i <= r; i++) {
    cmn = min(cmn, a[i]);
  }
  int cur = cmn - mn, tmp = l - 1;
  for (int i = l; i <= r; i++) {
    if (a[i] != cmn) continue;
    cur += cal(tmp + 1, i - 1, cmn);
    tmp = i;
  }
  cur += cal(tmp + 1, r, cmn);
  return min(cur, r - l + 1);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  cout << cal(1, n, 0) << endl;
}
