#include <bits/stdc++.h>
using namespace std;
const int maxn = 5000 + 10;
int a[maxn], n;
int solve(int l, int r) {
  int mn = 1e9 + 1;
  for (int i = l; i <= r; i++) mn = min(mn, a[i]);
  for (int i = l; i <= r; i++) a[i] -= mn;
  int res = mn;
  int las = l;
  for (int i = l; i <= r; i++) {
    if (a[i] == 0) {
      res += solve(las, i - 1);
      las = i + 1;
    }
  }
  if (las <= r) res += solve(las, r);
  return min(res, r - l + 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  printf("%d\n", solve(1, n));
  return 0;
}
