#include <bits/stdc++.h>
using namespace std;
int a[5000];
long long g(int l, int r, int sub) {
  int m = INT_MAX;
  for (int i = l; i <= r; i++) m = min(m, a[i]);
  int last = l - 1;
  int ans = m - sub;
  sub = m;
  for (int i = l; i <= r; i++) {
    if (a[i] == m) {
      if (last < i - 1) ans += g(last + 1, i - 1, sub);
      last = i;
    }
  }
  if (last < r) ans += g(last + 1, r, sub);
  return min(ans, r - l + 1);
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  cout << g(0, n - 1, 0);
  return 0;
}
