#include <bits/stdc++.h>
using namespace std;
int n, arr[5005];
int magic(int l, int r) {
  if (l > r) return 0;
  int mn = (int)2e9;
  for (int i = l; i <= r; i++) {
    mn = min(mn, arr[i]);
  }
  int prev = l, res = mn;
  for (int i = l; i <= r; i++) {
    arr[i] -= mn;
    if (arr[i] == 0) {
      res += magic(prev, i - 1);
      prev = i + 1;
    }
    if (i == r && arr[i]) {
      res += magic(prev, i);
    }
  }
  res = min(res, r - l + 1);
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
  }
  cout << magic(0, n - 1) << '\n';
  return 0;
}
