#include <bits/stdc++.h>
using namespace std;
int i, j, k, n, m, an;
int a[5010];
inline long long work(int l, int r, int cen) {
  int i, last = l, mi = a[l];
  long long s = r - l + 1, sum;
  for (i = l; i <= r; ++i) mi = min(mi, a[i]);
  sum = mi - cen;
  for (i = l; i <= r; ++i)
    if (a[i] == mi) {
      if (last < i) sum += work(last, i - 1, mi);
      last = i + 1;
    }
  if (last <= r) sum += work(last, r, mi);
  return min(s, sum);
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; ++i) scanf("%d", &a[i]);
  an = work(1, n, 0);
  printf("%I64d\n", an);
  return 0;
}
