#include <bits/stdc++.h>
using namespace std;
double pi = acos(-1.0);
const int maxn = 7e2 + 38;
const int mod = 998244353;
int n, m;
int a[10000];
int dfs(int l, int r, int h) {
  if (l == r && a[l] > h) return 1;
  if (l == r && a[l] <= h) return 0;
  if (l > r) return 0;
  int mm = a[l];
  for (int i = l; i <= r; i++)
    if (a[i] < mm) mm = a[i];
  int cnt = 0;
  for (int i = l; i <= r; i++)
    if (a[i] == mm) cnt++;
  int ans1 = r - l + 1;
  int ans = mm - h;
  if (ans < ans1) {
    int st = l;
    for (int i = l; i <= r; i++) {
      if (a[i] == mm) {
        ans += dfs(st, i - 1, mm);
        st = i + 1;
        if (ans > ans1) break;
      }
    }
    if (ans <= ans1 && a[r] != mm) ans += dfs(st, r, mm);
  }
  return min(ans, ans1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  printf("%d", dfs(1, n, 0));
}
