#include <bits/stdc++.h>
const int INF = 2147483647;
using namespace std;
int n, a[50005];
int solve(int l, int r) {
  int num = r - l + 1;
  if (num <= 0) return 0;
  if (num == 1) return 1;
  int times = 0, minh = INF;
  for (int i = l; i <= r; i++) minh = min(minh, a[i]);
  times = minh;
  for (int i = l; i <= r; i++) a[i] -= minh;
  int stp = l;
  while (a[stp] == 0 && stp <= r) stp++;
  for (int i = stp; i <= r; i++) {
    if (a[i] == 0) {
      times += solve(stp, i - 1);
      stp = i + 1;
    }
  }
  times += solve(stp, r);
  return min(times, num);
}
int main() {
  while (~scanf("%d", &n)) {
    int minh = INF;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      minh = min(minh, a[i]);
    }
    printf("%d\n", solve(1, n));
  }
  return 0;
}
