#include <bits/stdc++.h>
using namespace std;
int h[5005];
int n;
int solve(int l, int r, int k) {
  if (l == r) return 1;
  int mini = 0x3f3f3f3f;
  for (int i = l; i <= r; i++) {
    mini = min(mini, h[i]);
  }
  if (mini == 0) return 0;
  int acc = 0;
  int a = -1, b = 0;
  for (int i = l; i <= r; i++) {
    if (h[i] == mini) {
      if (a != -1) acc += solve(a, b, mini);
      a = -1;
      continue;
    }
    if (a == -1) a = i;
    b = i;
  }
  if (a != -1) acc += solve(a, b, mini);
  return min(r - l + 1, mini - k + acc);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d", h + i);
  printf("%d\n", solve(0, n - 1, 0));
}
