#include <bits/stdc++.h>
using namespace std;
int n;
int minh(int arr[], int l, int r) {
  int h = INT_MAX;
  for (int i = l; i <= r; i++) {
    h = min(h, arr[i]);
  }
  return h;
}
int func(int l, int r, int arr[], int hi) {
  int ans = 0;
  if (r < l)
    return 0;
  else if (l == r) {
    if (arr[l] == hi) return 0;
    return 1;
  }
  int h = minh(arr, l, r);
  ans += h - hi;
  int st = l;
  for (int i = l; i <= r; i++) {
    if (arr[i] == h) {
      ans += min(i - st, func(st, i - 1, arr, h));
      st = i + 1;
    }
  }
  ans += min(r - st + 1, func(st, r, arr, h));
  return ans;
}
int main() {
  cin >> n;
  int arr[n];
  for (int i = 1; i < n + 1; i++) cin >> arr[i];
  int ans;
  ans = min(n, func(1, n, arr, 0));
  cout << ans << endl;
  return 0;
}
