#include <bits/stdc++.h>
using namespace std;
int arr[5010];
int func(int start, int end, int h) {
  if (start > end) return 0;
  int m = start;
  for (int i = start; i <= end; i++)
    if (arr[i] < arr[m]) m = i;
  return min(end - start + 1, arr[m] - h + func(start, m - 1, arr[m]) +
                                  func(m + 1, end, arr[m]));
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int ans = func(0, n - 1, 0);
  cout << ans << endl;
}
