#include <bits/stdc++.h>
using namespace std;
int a[5001], n;
int dfs(int l, int r) {
  int Min = a[l], ans = 0;
  for (int i = l + 1; i <= r; i++) Min = min(Min, a[i]);
  ans = Min;
  for (int i = l; i <= r; i++) a[i] -= Min;
  for (int i = l; i <= r; i++)
    if (a[i]) {
      int tmp = i + 1;
      while (tmp <= r && a[tmp]) ++tmp;
      ans += dfs(i, tmp - 1);
      i = tmp - 1;
    }
  return min(ans, r - l + 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  printf("%d\n", dfs(1, n));
  return 0;
}
