#include <bits/stdc++.h>
int a[5000];
int solve(int start, int end) {
  int vAns, hAns, min = a[start];
  vAns = end - start + 1;
  for (int i = start + 1; i < end + 1; i++) {
    if (min > a[i]) min = a[i];
  }
  hAns = min;
  int nSt, nEn, flag = 1;
  for (int i = start; i < end + 1; i++) {
    a[i] -= min;
    if (a[i] != 0) {
      if (flag) {
        nSt = i;
        flag = 0;
      }
      if (i == end) {
        nEn = i;
        hAns += solve(nSt, nEn);
      }
    } else if (!flag) {
      nEn = i - 1;
      flag = 1;
      hAns += solve(nSt, nEn);
    }
  }
  if (vAns > hAns) return hAns;
  return vAns;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  printf("%d", solve(0, n - 1));
  return 0;
}
