#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 11;
int arr[N];
int n;
int solve(int l, int r) {
  int minf = INF;
  for (int i = l; i <= r; ++i)
    if (arr[i] < minf) minf = arr[i];
  if (minf >= r - l + 1) return r - l + 1;
  int last = -1;
  int sum = 0;
  for (int i = l; i <= r; ++i) {
    arr[i] -= minf;
    if (arr[i] == 0) {
      if (last != -1) sum += solve(last, i - 1);
      last = -1;
    } else {
      if (last == -1) last = i;
    }
  }
  if (last != -1) sum += solve(last, r);
  return min(sum + minf, r - l + 1);
}
int main() {
  while (scanf("%d", &n) == 1) {
    for (int i = 1; i <= n; ++i) scanf("%d", &arr[i]);
    printf("%d\n", solve(1, n));
  }
}
