#include <bits/stdc++.h>
using namespace std;
int arr[5005];
int go(int beg, int en) {
  int sz = 0;
  int mini = 1e9;
  for (int i = beg; i < en; i++) {
    mini = min(mini, arr[i]);
    if (arr[i]) sz++;
  }
  for (int i = beg; i < en; i++) arr[i] = max(0, arr[i] - mini);
  int ans = mini;
  int begi = -1;
  for (int i = beg; i <= en; i++) {
    if (i == en) {
      if (begi != -1)
        ans += go(begi, i);
      else
        continue;
    }
    if (arr[i] && begi == -1) {
      begi = i;
    } else if (arr[i] == 0 && begi != -1) {
      ans += go(begi, i);
      begi = -1;
    }
  }
  return min(ans, sz);
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr[i];
  int ans = n, mini = 1e9;
  cout << go(0, n);
}
