#include <bits/stdc++.h>
using namespace std;
const int N = 5010, inf = 1000000007;
int n, a[N];
int ca(int l, int r, int h) {
  if (r < l) return 0;
  int c = inf, p = 0;
  for (int i = l; i <= r; i++)
    if (a[i] < c) c = a[i], p = i;
  return min(r - l + 1, ca(l, p - 1, c) + ca(p + 1, r, c) + c - h);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  printf("%d\n", ca(1, n, 0));
  return 0;
}
