#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline void mina(A& x, B y) {
  if (x > y) x = y;
}
template <typename A, typename B>
inline void maxa(A& x, B y) {
  if (x < y) x = y;
}
template <class T1, class T2>
ostream& operator<<(ostream& out, pair<T1, T2> P) {
  return out << "(" << P.first << ", " << P.second << ")";
}
template <class T>
void _spn_print_cont_for(T const& C) {
  int first = 1;
  cout << "[";
  for (auto a : C) {
    if (!first) printf(", ");
    first = 0;
    cout << a;
  }
  cout << "]" << endl;
}
template <class T>
void _spn_print_vector_lr(vector<T> const& C, int l, int r) {
  if (l < 0 || r >= C.size()) {
    printf("ARRAY OUT OF BOUND [0,%d]\n\n", (int)C.size() - 1);
    return;
  }
  int first = 1;
  cout << "[";
  for (int i = l; i <= r; ++i) {
    if (!first) printf(", ");
    first = 0;
    cout << C[i];
  }
  cout << "]" << endl;
}
int A[(5005)];
int N;
int calc(int l, int r, int h) {
  int minh = 1e9;
  for (int _b = (r + 1), i = (l); i < _b; ++i) mina(minh, A[i]);
  if (minh >= N) return r - l + 1;
  int i, j;
  i = l;
  while (i <= r && A[i] == minh) ++i;
  if (i > r) return min<int>(r - l + 1, minh - h);
  long long sum = 0;
  while (i <= r) {
    for (j = i; j <= r && A[j] > minh; ++j)
      ;
    sum += calc(i, j - 1, minh);
    while (j <= r && A[j] == minh) ++j;
    i = j;
  }
  return min<long long>(r - l + 1, sum + minh - h);
}
int main() {
  scanf("%d", &N);
  for (int _b = (N), i = (0); i < _b; ++i) scanf("%d", A + i);
  int sol = calc(0, N - 1, 0);
  printf("%d\n", sol);
  return 0;
}
