#include <bits/stdc++.h>
using namespace std;
const int maxn = 5010;
int n, v[maxn];
int get_min(const int l, const int r) {
  int k = l;
  for (int i = l; i <= r; ++i)
    if (v[i] < v[k]) k = i;
  return k;
}
int dfs(const int l, const int r) {
  if (r < l) return 0;
  int k = get_min(l, r);
  int w = v[k];
  if (r - l + 1 < w) {
    for (int i = l; i <= r; ++i) v[i] = 0;
    return r - l + 1;
  }
  if (w)
    for (int i = l; i <= r; ++i) v[i] -= w;
  return min(w + dfs(l, k - 1) + dfs(k + 1, r), r - l + 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &v[i]);
  printf("%d\n", dfs(1, n));
  return 0;
}
